| 75 | } |
| 76 | |
| 77 | bool ExternalSigner::SignTransaction(PartiallySignedTransaction& psbtx, std::string& error) |
| 78 | { |
| 79 | // Serialize the PSBT |
| 80 | DataStream ssTx{}; |
| 81 | ssTx << psbtx; |
| 82 | // parse ExternalSigner master fingerprint |
| 83 | std::vector<unsigned char> parsed_m_fingerprint = ParseHex(m_fingerprint); |
| 84 | // Check if signer fingerprint matches any input master key fingerprint |
| 85 | auto matches_signer_fingerprint = [&](const PSBTInput& input) { |
| 86 | for (const auto& entry : input.hd_keypaths) { |
| 87 | if (std::ranges::equal(parsed_m_fingerprint, entry.second.fingerprint)) return true; |
| 88 | } |
| 89 | for (const auto& entry : input.m_tap_bip32_paths) { |
| 90 | if (std::ranges::equal(parsed_m_fingerprint, entry.second.second.fingerprint)) return true; |
| 91 | } |
| 92 | return false; |
| 93 | }; |
| 94 | |
| 95 | if (!std::any_of(psbtx.inputs.begin(), psbtx.inputs.end(), matches_signer_fingerprint)) { |
| 96 | error = "Signer fingerprint " + m_fingerprint + " does not match any of the inputs:\n" + EncodeBase64(ssTx.str()); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | const std::vector<std::string> command = Cat(m_command, Cat({"--stdin", "--fingerprint", m_fingerprint}, NetworkArg())); |
| 101 | const std::string stdinStr = "signtx " + EncodeBase64(ssTx.str()); |
| 102 | |
| 103 | const UniValue signer_result = RunCommandParseJSON(command, stdinStr); |
| 104 | |
| 105 | if (signer_result.find_value("error").isStr()) { |
| 106 | error = signer_result.find_value("error").get_str(); |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | if (!signer_result.find_value("psbt").isStr()) { |
| 111 | error = "Unexpected result from signer"; |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | util::Result<PartiallySignedTransaction> signer_psbtx = DecodeBase64PSBT(signer_result.find_value("psbt").get_str()); |
| 116 | if (!signer_psbtx) { |
| 117 | error = strprintf("TX decode failed %s", util::ErrorString(signer_psbtx).original); |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | psbtx = *signer_psbtx; |
| 122 | |
| 123 | return true; |
| 124 | } |
nothing calls this directly
no test coverage detected