Bitcoin Core 31.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#include <util/subprocess.h>
13
14#include <algorithm>
15#include <stdexcept>
16#include <string>
17#include <vector>
18
19ExternalSigner::ExternalSigner(std::vector<std::string> command, std::string chain, std::string fingerprint, std::string name)
20 : m_command{std::move(command)}, m_chain{std::move(chain)}, m_fingerprint{std::move(fingerprint)}, m_name{std::move(name)} {}
21
22std::vector<std::string> ExternalSigner::NetworkArg() const
23{
24 return {"--chain", m_chain};
25}
26
27bool ExternalSigner::Enumerate(const std::string& command, std::vector<ExternalSigner>& signers, const std::string& chain)
28{
29 // Call <command> enumerate
30 std::vector<std::string> cmd_args = Cat(subprocess::util::split(command), {"enumerate"});
31
32 const UniValue result = RunCommandParseJSON(cmd_args, "");
33 if (!result.isArray()) {
34 throw std::runtime_error(strprintf("'%s' received invalid response, expected array of signers", command));
35 }
36 for (const UniValue& signer : result.getValues()) {
37 // Check for error
38 const UniValue& error = signer.find_value("error");
39 if (!error.isNull()) {
40 if (!error.isStr()) {
41 throw std::runtime_error(strprintf("'%s' error", command));
42 }
43 throw std::runtime_error(strprintf("'%s' error: %s", command, error.getValStr()));
44 }
45 // Check if fingerprint is present
46 const UniValue& fingerprint = signer.find_value("fingerprint");
47 if (fingerprint.isNull()) {
48 throw std::runtime_error(strprintf("'%s' received invalid response, missing signer fingerprint", command));
49 }
50 const std::string& fingerprintStr{fingerprint.get_str()};
51 if (fingerprintStr.size() != 8 || !IsHex(fingerprintStr)) {
52 throw std::runtime_error(strprintf("'%s' received invalid fingerprint, must be 8 hex characters", command));
53 }
54 // Skip duplicate signer
55 bool duplicate = false;
56 for (const ExternalSigner& signer : signers) {
57 if (signer.m_fingerprint.compare(fingerprintStr) == 0) duplicate = true;
58 }
59 if (duplicate) continue;
60 std::string name;
61 const UniValue& model_field = signer.find_value("model");
62 if (model_field.isStr() && model_field.getValStr() != "") {
63 name += model_field.getValStr();
64 }
65 signers.emplace_back(subprocess::util::split(command), chain, fingerprintStr, name);
66 }
67 return true;
68}
69
70UniValue ExternalSigner::DisplayAddress(const std::string& descriptor) const
71{
72 return RunCommandParseJSON(Cat(m_command, Cat(Cat({"--fingerprint", m_fingerprint}, NetworkArg()), {"displayaddress", "--desc", descriptor})), "");
73}
74
76{
77 return RunCommandParseJSON(Cat(m_command, Cat(Cat({"--fingerprint", m_fingerprint}, NetworkArg()), {"getdescriptors", "--account", strprintf("%d", account)})), "");
78}
79
81{
82 // Serialize the PSBT
83 DataStream ssTx{};
84 ssTx << psbtx;
85 // parse ExternalSigner master fingerprint
86 std::vector<unsigned char> parsed_m_fingerprint = ParseHex(m_fingerprint);
87 // Check if signer fingerprint matches any input master key fingerprint
88 auto matches_signer_fingerprint = [&](const PSBTInput& input) {
89 for (const auto& entry : input.hd_keypaths) {
90 if (std::ranges::equal(parsed_m_fingerprint, entry.second.fingerprint)) return true;
91 }
92 for (const auto& entry : input.m_tap_bip32_paths) {
93 if (std::ranges::equal(parsed_m_fingerprint, entry.second.second.fingerprint)) return true;
94 }
95 return false;
96 };
97
98 if (!std::any_of(psbtx.inputs.begin(), psbtx.inputs.end(), matches_signer_fingerprint)) {
99 error = "Signer fingerprint " + m_fingerprint + " does not match any of the inputs:\n" + EncodeBase64(ssTx.str());
100 return false;
101 }
102
103 const std::vector<std::string> command = Cat(m_command, Cat({"--stdin", "--fingerprint", m_fingerprint}, NetworkArg()));
104 const std::string stdinStr = "signtx " + EncodeBase64(ssTx.str());
105
106 const UniValue signer_result = RunCommandParseJSON(command, stdinStr);
107
108 if (signer_result.find_value("error").isStr()) {
109 error = signer_result.find_value("error").get_str();
110 return false;
111 }
112
113 if (!signer_result.find_value("psbt").isStr()) {
114 error = "Unexpected result from signer";
115 return false;
116 }
117
118 util::Result<PartiallySignedTransaction> signer_psbtx = DecodeBase64PSBT(signer_result.find_value("psbt").get_str());
119 if (!signer_psbtx) {
120 error = strprintf("TX decode failed %s", util::ErrorString(signer_psbtx).original);
121 return false;
122 }
123
124 psbtx = *signer_psbtx;
125
126 return true;
127}
const auto command
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:165
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.
std::vector< std::string > NetworkArg() const
bool SignTransaction(PartiallySignedTransaction &psbt, std::string &error)
Sign PartiallySignedTransaction on the device.
std::vector< std::string > m_command
The command which handles interaction with the external signer.
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.
ExternalSigner(std::vector< std::string > command, std::string chain, std::string fingerprint, std::string name)
A structure for PSBTs which contain per-input information.
Definition: psbt.h:281
A version of CTransaction with the PSBT format.
Definition: psbt.h:1240
std::vector< PSBTInput > inputs
Definition: psbt.h:1249
const std::string & get_str() const
const UniValue & find_value(std::string_view key) const
Definition: univalue.cpp:232
bool isNull() const
Definition: univalue.h:81
const std::string & getValStr() const
Definition: univalue.h:68
bool isStr() const
Definition: univalue.h:85
static std::vector< std::string > split(const std::string &str, const std::string &delims=" \t")
Definition: subprocess.h:311
bilingual_str ErrorString(const Result< T > &result)
Definition: result.h:93
CRPCCommand m_command
Definition: interfaces.cpp:554
util::Result< PartiallySignedTransaction > DecodeBase64PSBT(const std::string &base64_tx)
Decode a base64ed PSBT into a PartiallySignedTransaction.
Definition: psbt.cpp:864
const char * name
Definition: rest.cpp:50
UniValue RunCommandParseJSON(const std::vector< std::string > &cmd_args, const std::string &str_std_in)
Execute a command which returns JSON, and parse the result.
Definition: run_command.cpp:17
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
Definition: strencodings.h:68
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
bool IsHex(std::string_view str)
std::string EncodeBase64(std::span< const unsigned char > input)
V Cat(V v1, V &&v2)
Concatenate two vectors, moving elements.
Definition: vector.h:34