Bitcoin Core  21.99.0
P2P Digital Currency
external_signer.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018-2021 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 <core_io.h>
7 #include <psbt.h>
8 #include <util/strencodings.h>
9 #include <util/system.h>
10 #include <wallet/external_signer.h>
11 
12 ExternalSigner::ExternalSigner(const std::string& command, const std::string& fingerprint, std::string chain, std::string name): m_command(command), m_fingerprint(fingerprint), m_chain(chain), m_name(name) {}
13 
14 const std::string ExternalSigner::NetworkArg() const
15 {
16  return " --chain " + m_chain;
17 }
18 
19 #ifdef ENABLE_EXTERNAL_SIGNER
20 
21 bool ExternalSigner::Enumerate(const std::string& command, std::vector<ExternalSigner>& signers, std::string chain, bool ignore_errors)
22 {
23  // Call <command> enumerate
24  const UniValue result = RunCommandParseJSON(command + " enumerate");
25  if (!result.isArray()) {
26  if (ignore_errors) return false;
27  throw ExternalSignerException(strprintf("'%s' received invalid response, expected array of signers", command));
28  }
29  for (UniValue signer : result.getValues()) {
30  // Check for error
31  const UniValue& error = find_value(signer, "error");
32  if (!error.isNull()) {
33  if (ignore_errors) return false;
34  if (!error.isStr()) {
35  throw ExternalSignerException(strprintf("'%s' error", command));
36  }
37  throw ExternalSignerException(strprintf("'%s' error: %s", command, error.getValStr()));
38  }
39  // Check if fingerprint is present
40  const UniValue& fingerprint = find_value(signer, "fingerprint");
41  if (fingerprint.isNull()) {
42  if (ignore_errors) return false;
43  throw ExternalSignerException(strprintf("'%s' received invalid response, missing signer fingerprint", command));
44  }
45  std::string fingerprintStr = fingerprint.get_str();
46  // Skip duplicate signer
47  bool duplicate = false;
48  for (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, fingerprintStr, chain, 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 
78  // Check if signer fingerprint matches any input master key fingerprint
79  bool match = false;
80  for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) {
81  const PSBTInput& input = psbtx.inputs[i];
82  for (auto entry : input.hd_keypaths) {
83  if (m_fingerprint == strprintf("%08x", ReadBE32(entry.second.fingerprint))) match = true;
84  }
85  }
86 
87  if (!match) {
88  error = "Signer fingerprint " + m_fingerprint + " does not match any of the inputs:\n" + EncodeBase64(ssTx.str());
89  return false;
90  }
91 
92  std::string command = m_command + " --stdin --fingerprint \"" + m_fingerprint + "\"" + NetworkArg();
93  std::string stdinStr = "signtx \"" + EncodeBase64(ssTx.str()) + "\"";
94 
95  const UniValue signer_result = RunCommandParseJSON(command, stdinStr);
96 
97  if (find_value(signer_result, "error").isStr()) {
98  error = find_value(signer_result, "error").get_str();
99  return false;
100  }
101 
102  if (!find_value(signer_result, "psbt").isStr()) {
103  error = "Unexpected result from signer";
104  return false;
105  }
106 
107  PartiallySignedTransaction signer_psbtx;
108  std::string signer_psbt_error;
109  if (!DecodeBase64PSBT(signer_psbtx, find_value(signer_result, "psbt").get_str(), signer_psbt_error)) {
110  error = strprintf("TX decode failed %s", signer_psbt_error);
111  return false;
112  }
113 
114  psbtx = signer_psbtx;
115 
116  return true;
117 }
118 
119 #endif
ExternalSigner::DisplayAddress
UniValue DisplayAddress(const std::string &descriptor) const
Display address on the device.
Definition: external_signer.cpp:62
ExternalSigner::m_chain
std::string m_chain
Bitcoin mainnet, testnet, etc.
Definition: external_signer.h:39
RunCommandParseJSON
UniValue RunCommandParseJSON(const std::string &str_command, const std::string &str_std_in)
Execute a command which returns JSON, and parse the result.
Definition: system.cpp:1251
ExternalSignerException
Definition: external_signer.h:15
UniValue::isNull
bool isNull() const
Definition: univalue.h:77
EncodeBase64
std::string EncodeBase64(Span< const unsigned char > input)
Definition: strencodings.cpp:131
chainparams.h
core_io.h
DecodeBase64PSBT
bool DecodeBase64PSBT(PartiallySignedTransaction &psbt, const std::string &base64_tx, std::string &error)
Decode a base64ed PSBT into a PartiallySignedTransaction.
Definition: psbt.cpp:353
UniValue
Definition: univalue.h:19
UniValue::get_str
const std::string & get_str() const
Definition: univalue_get.cpp:97
strencodings.h
UniValue::isStr
bool isStr() const
Definition: univalue.h:81
ExternalSigner::SignTransaction
bool SignTransaction(PartiallySignedTransaction &psbt, std::string &error)
Sign PartiallySignedTransaction on the device.
Definition: external_signer.cpp:72
external_signer.h
ReadBE32
static uint32_t ReadBE32(const unsigned char *ptr)
Definition: common.h:63
PSBTInput::hd_keypaths
std::map< CPubKey, KeyOriginInfo > hd_keypaths
Definition: psbt.h:57
ExternalSigner::GetDescriptors
UniValue GetDescriptors(int account)
Get receive and change Descriptor(s) from device for a given account.
Definition: external_signer.cpp:67
UniValue::isArray
bool isArray() const
Definition: univalue.h:83
ExternalSigner
Enables interaction with an external signing device or service, such as a hardware wallet.
Definition: external_signer.h:22
ExternalSigner::ExternalSigner
ExternalSigner(const std::string &command, const std::string &fingerprint, std::string chain, std::string name)
Definition: external_signer.cpp:12
name
const char * name
Definition: rest.cpp:42
system.h
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
UniValue::getValStr
const std::string & getValStr() const
Definition: univalue.h:65
PSBTInput
A structure for PSBTs which contain per-input information.
Definition: psbt.h:49
ExternalSigner::Enumerate
static bool Enumerate(const std::string &command, std::vector< ExternalSigner > &signers, std::string chain, bool ignore_errors=false)
Obtain a list of signers.
Definition: external_signer.cpp:21
PartiallySignedTransaction
A version of CTransaction with the PSBT format.
Definition: psbt.h:391
ExternalSigner::m_command
std::string m_command
The command which handles interaction with the external signer.
Definition: external_signer.h:26
UniValue::getValues
const std::vector< UniValue > & getValues() const
Definition: univalue_get.cpp:83
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:204
ExternalSigner::NetworkArg
const std::string NetworkArg() const
Definition: external_signer.cpp:14
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:166
m_command
CRPCCommand m_command
Definition: interfaces.cpp:423
find_value
const UniValue & find_value(const UniValue &obj, const std::string &name)
Definition: univalue.cpp:234
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:50
CDataStream::str
std::string str() const
Definition: streams.h:242
ExternalSigner::m_fingerprint
std::string m_fingerprint
Master key fingerprint of the signer.
Definition: external_signer.h:36
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12