Bitcoin Core 30.99.0
P2P Digital Currency
external_signer.cpp
Go to the documentation of this file.
1// Copyright (c) 2018-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <external_signer.h>
6
7#include <chainparams.h>
9#include <core_io.h>
10#include <psbt.h>
11#include <util/strencodings.h>
12
13#include <algorithm>
14#include <stdexcept>
15#include <string>
16#include <vector>
17
18ExternalSigner::ExternalSigner(std::string command, std::string chain, std::string fingerprint, std::string name)
19 : m_command{std::move(command)}, m_chain{std::move(chain)}, m_fingerprint{std::move(fingerprint)}, m_name{std::move(name)} {}
20
21std::string ExternalSigner::NetworkArg() const
22{
23 return " --chain " + m_chain;
24}
25
26bool ExternalSigner::Enumerate(const std::string& command, std::vector<ExternalSigner>& signers, const std::string& chain)
27{
28 // Call <command> enumerate
29 const UniValue result = RunCommandParseJSON(command + " enumerate");
30 if (!result.isArray()) {
31 throw std::runtime_error(strprintf("'%s' received invalid response, expected array of signers", command));
32 }
33 for (const UniValue& signer : result.getValues()) {
34 // Check for error
35 const UniValue& error = signer.find_value("error");
36 if (!error.isNull()) {
37 if (!error.isStr()) {
38 throw std::runtime_error(strprintf("'%s' error", command));
39 }
40 throw std::runtime_error(strprintf("'%s' error: %s", command, error.getValStr()));
41 }
42 // Check if fingerprint is present
43 const UniValue& fingerprint = signer.find_value("fingerprint");
44 if (fingerprint.isNull()) {
45 throw std::runtime_error(strprintf("'%s' received invalid response, missing signer fingerprint", command));
46 }
47 const std::string& fingerprintStr{fingerprint.get_str()};
48 // Skip duplicate signer
49 bool duplicate = false;
50 for (const ExternalSigner& signer : signers) {
51 if (signer.m_fingerprint.compare(fingerprintStr) == 0) duplicate = true;
52 }
53 if (duplicate) break;
54 std::string name;
55 const UniValue& model_field = signer.find_value("model");
56 if (model_field.isStr() && model_field.getValStr() != "") {
57 name += model_field.getValStr();
58 }
59 signers.emplace_back(command, chain, fingerprintStr, name);
60 }
61 return true;
62}
63
64UniValue ExternalSigner::DisplayAddress(const std::string& descriptor) const
65{
66 return RunCommandParseJSON(m_command + " --fingerprint " + m_fingerprint + NetworkArg() + " displayaddress --desc " + descriptor);
67}
68
70{
71 return RunCommandParseJSON(m_command + " --fingerprint " + m_fingerprint + NetworkArg() + " getdescriptors --account " + strprintf("%d", account));
72}
73
75{
76 // Serialize the PSBT
77 DataStream ssTx{};
78 ssTx << psbtx;
79 // parse ExternalSigner master fingerprint
80 std::vector<unsigned char> parsed_m_fingerprint = ParseHex(m_fingerprint);
81 // Check if signer fingerprint matches any input master key fingerprint
82 auto matches_signer_fingerprint = [&](const PSBTInput& input) {
83 for (const auto& entry : input.hd_keypaths) {
84 if (std::ranges::equal(parsed_m_fingerprint, entry.second.fingerprint)) return true;
85 }
86 for (const auto& entry : input.m_tap_bip32_paths) {
87 if (std::ranges::equal(parsed_m_fingerprint, entry.second.second.fingerprint)) return true;
88 }
89 return false;
90 };
91
92 if (!std::any_of(psbtx.inputs.begin(), psbtx.inputs.end(), matches_signer_fingerprint)) {
93 error = "Signer fingerprint " + m_fingerprint + " does not match any of the inputs:\n" + EncodeBase64(ssTx.str());
94 return false;
95 }
96
97 const std::string command = m_command + " --stdin --fingerprint " + m_fingerprint + NetworkArg();
98 const std::string stdinStr = "signtx " + EncodeBase64(ssTx.str());
99
100 const UniValue signer_result = RunCommandParseJSON(command, stdinStr);
101
102 if (signer_result.find_value("error").isStr()) {
103 error = signer_result.find_value("error").get_str();
104 return false;
105 }
106
107 if (!signer_result.find_value("psbt").isStr()) {
108 error = "Unexpected result from signer";
109 return false;
110 }
111
112 PartiallySignedTransaction signer_psbtx;
113 std::string signer_psbt_error;
114 if (!DecodeBase64PSBT(signer_psbtx, signer_result.find_value("psbt").get_str(), signer_psbt_error)) {
115 error = strprintf("TX decode failed %s", signer_psbt_error);
116 return false;
117 }
118
119 psbtx = signer_psbtx;
120
121 return true;
122}
const auto command
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:130
Enables interaction with an external signing device or service, such as a hardware wallet.
std::string m_chain
Bitcoin mainnet, testnet, etc.
static bool Enumerate(const std::string &command, std::vector< ExternalSigner > &signers, const std::string &chain)
Obtain a list of signers.
UniValue DisplayAddress(const std::string &descriptor) const
Display address on the device.
bool SignTransaction(PartiallySignedTransaction &psbt, std::string &error)
Sign PartiallySignedTransaction on the device.
std::string NetworkArg() const
UniValue GetDescriptors(int account)
Get receive and change Descriptor(s) from device for a given account.
std::string m_fingerprint
Master key fingerprint of the signer.
std::string m_command
The command which handles interaction with the external signer.
ExternalSigner(std::string command, std::string chain, std::string fingerprint, std::string name)
const std::string & get_str() const
bool isArray() const
Definition: univalue.h:87
const UniValue & find_value(std::string_view key) const
Definition: univalue.cpp:233
bool isNull() const
Definition: univalue.h:81
const std::string & getValStr() const
Definition: univalue.h:68
const std::vector< UniValue > & getValues() const
bool isStr() const
Definition: univalue.h:85
CRPCCommand m_command
Definition: interfaces.cpp:540
bool DecodeBase64PSBT(PartiallySignedTransaction &psbt, const std::string &base64_tx, std::string &error)
Decode a base64ed PSBT into a PartiallySignedTransaction.
Definition: psbt.cpp:608
const char * name
Definition: rest.cpp:48
UniValue RunCommandParseJSON(const std::string &str_command, const std::string &str_std_in)
Execute a command which returns JSON, and parse the result.
Definition: run_command.cpp:16
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
Definition: strencodings.h:69
A structure for PSBTs which contain per-input information.
Definition: psbt.h:255
A version of CTransaction with the PSBT format.
Definition: psbt.h:1132
std::vector< PSBTInput > inputs
Definition: psbt.h:1137
Definition: musig.c:31
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
std::string EncodeBase64(std::span< const unsigned char > input)