Bitcoin Core  24.99.0
P2P Digital Currency
external_signer.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018-2022 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 <chainparams.h>
6 #include <common/run_command.h>
7 #include <core_io.h>
8 #include <psbt.h>
9 #include <util/strencodings.h>
10 #include <external_signer.h>
11 
12 #include <algorithm>
13 #include <stdexcept>
14 #include <string>
15 #include <vector>
16 
17 ExternalSigner::ExternalSigner(const std::string& command, const std::string chain, const std::string& fingerprint, const std::string name): m_command(command), m_chain(chain), m_fingerprint(fingerprint), m_name(name) {}
18 
19 const std::string ExternalSigner::NetworkArg() const
20 {
21  return " --chain " + m_chain;
22 }
23 
24 bool ExternalSigner::Enumerate(const std::string& command, std::vector<ExternalSigner>& signers, const std::string chain)
25 {
26  // Call <command> enumerate
27  const UniValue result = RunCommandParseJSON(command + " enumerate");
28  if (!result.isArray()) {
29  throw std::runtime_error(strprintf("'%s' received invalid response, expected array of signers", command));
30  }
31  for (const UniValue& signer : result.getValues()) {
32  // Check for error
33  const UniValue& error = find_value(signer, "error");
34  if (!error.isNull()) {
35  if (!error.isStr()) {
36  throw std::runtime_error(strprintf("'%s' error", command));
37  }
38  throw std::runtime_error(strprintf("'%s' error: %s", command, error.getValStr()));
39  }
40  // Check if fingerprint is present
41  const UniValue& fingerprint = find_value(signer, "fingerprint");
42  if (fingerprint.isNull()) {
43  throw std::runtime_error(strprintf("'%s' received invalid response, missing signer fingerprint", command));
44  }
45  const std::string fingerprintStr = fingerprint.get_str();
46  // Skip duplicate signer
47  bool duplicate = false;
48  for (const ExternalSigner& signer : signers) {
49  if (signer.m_fingerprint.compare(fingerprintStr) == 0) duplicate = true;
50  }
51  if (duplicate) break;
52  std::string name;
53  const UniValue& model_field = find_value(signer, "model");
54  if (model_field.isStr() && model_field.getValStr() != "") {
55  name += model_field.getValStr();
56  }
57  signers.push_back(ExternalSigner(command, chain, fingerprintStr, name));
58  }
59  return true;
60 }
61 
62 UniValue ExternalSigner::DisplayAddress(const std::string& descriptor) const
63 {
64  return RunCommandParseJSON(m_command + " --fingerprint \"" + m_fingerprint + "\"" + NetworkArg() + " displayaddress --desc \"" + descriptor + "\"");
65 }
66 
68 {
69  return RunCommandParseJSON(m_command + " --fingerprint \"" + m_fingerprint + "\"" + NetworkArg() + " getdescriptors --account " + strprintf("%d", account));
70 }
71 
73 {
74  // Serialize the PSBT
76  ssTx << psbtx;
77  // parse ExternalSigner master fingerprint
78  std::vector<unsigned char> parsed_m_fingerprint = ParseHex(m_fingerprint);
79  // Check if signer fingerprint matches any input master key fingerprint
80  auto matches_signer_fingerprint = [&](const PSBTInput& input) {
81  for (const auto& entry : input.hd_keypaths) {
82  if (parsed_m_fingerprint == MakeUCharSpan(entry.second.fingerprint)) return true;
83  }
84  for (const auto& entry : input.m_tap_bip32_paths) {
85  if (parsed_m_fingerprint == MakeUCharSpan(entry.second.second.fingerprint)) return true;
86  }
87  return false;
88  };
89 
90  if (!std::any_of(psbtx.inputs.begin(), psbtx.inputs.end(), matches_signer_fingerprint)) {
91  error = "Signer fingerprint " + m_fingerprint + " does not match any of the inputs:\n" + EncodeBase64(ssTx.str());
92  return false;
93  }
94 
95  const std::string command = m_command + " --stdin --fingerprint \"" + m_fingerprint + "\"" + NetworkArg();
96  const std::string stdinStr = "signtx \"" + EncodeBase64(ssTx.str()) + "\"";
97 
98  const UniValue signer_result = RunCommandParseJSON(command, stdinStr);
99 
100  if (find_value(signer_result, "error").isStr()) {
101  error = find_value(signer_result, "error").get_str();
102  return false;
103  }
104 
105  if (!find_value(signer_result, "psbt").isStr()) {
106  error = "Unexpected result from signer";
107  return false;
108  }
109 
110  PartiallySignedTransaction signer_psbtx;
111  std::string signer_psbt_error;
112  if (!DecodeBase64PSBT(signer_psbtx, find_value(signer_result, "psbt").get_str(), signer_psbt_error)) {
113  error = strprintf("TX decode failed %s", signer_psbt_error);
114  return false;
115  }
116 
117  psbtx = signer_psbtx;
118 
119  return true;
120 }
const auto command
std::string str() const
Definition: streams.h:207
Enables interaction with an external signing device or service, such as a hardware wallet.
std::string m_chain
Bitcoin mainnet, testnet, etc.
const std::string NetworkArg() const
UniValue DisplayAddress(const std::string &descriptor) const
Display address on the device.
UniValue GetDescriptors(const int account)
Get receive and change Descriptor(s) from device for a given account.
bool SignTransaction(PartiallySignedTransaction &psbt, std::string &error)
Sign PartiallySignedTransaction on the device.
std::string m_fingerprint
Master key fingerprint of the signer.
std::string m_command
The command which handles interaction with the external signer.
static bool Enumerate(const std::string &command, std::vector< ExternalSigner > &signers, const std::string chain)
Obtain a list of signers.
ExternalSigner(const std::string &command, const std::string chain, const std::string &fingerprint, const std::string name)
const std::string & get_str() const
bool isArray() const
Definition: univalue.h:81
bool isNull() const
Definition: univalue.h:75
const std::string & getValStr() const
Definition: univalue.h:64
const std::vector< UniValue > & getValues() const
bool isStr() const
Definition: univalue.h:79
CRPCCommand m_command
Definition: interfaces.cpp:499
bool DecodeBase64PSBT(PartiallySignedTransaction &psbt, const std::string &base64_tx, std::string &error)
Decode a base64ed PSBT into a PartiallySignedTransaction.
Definition: psbt.cpp:489
const char * name
Definition: rest.cpp:46
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:27
@ SER_NETWORK
Definition: serialize.h:131
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span{std::forward< V >(v)}))
Like the Span constructor, but for (const) unsigned char member types only.
Definition: span.h:285
A structure for PSBTs which contain per-input information.
Definition: psbt.h:192
A version of CTransaction with the PSBT format.
Definition: psbt.h:947
std::vector< PSBTInput > inputs
Definition: psbt.h:952
bool error(const char *fmt, const Args &... args)
Definition: system.h:48
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1165
const UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:233
std::vector< Byte > ParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
std::string EncodeBase64(Span< const unsigned char > input)
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12