Skip to content

Commit e7af2f3

Browse files
committed
Merge #21666: Miscellaneous external signer changes
c8f469c external_signer: remove ExternalSignerException (fanquake) 9e0b199 external_signer: use const where appropriate (fanquake) aaa4e5a wallet: remove CWallet::GetExternalSigner() (fanquake) 06a0673 external_signer: remove ignore_errors from Enumerate() (fanquake) 8fdbb89 refactor: unify external wallet runtime errors (fanquake) f4652bf refactor: add missing includes to external signer code (fanquake) 54569cc refactor: move all signer code inside ENABLE_EXTERNAL_SIGNER #ifdefs (fanquake) Pull request description: These are a few followups after #21467. ACKs for top commit: Sjors: tACK c8f469c instagibbs: utACK bitcoin/bitcoin@c8f469c Tree-SHA512: 3d5ac5df81680075e71e0e4a7595c520d746c3e37f016cf168c1e10da15541ebb1595aecaf2c08575636e9ff77d499644cae53180232b7049cfae0b923106e4e
2 parents a1f0b8b + c8f469c commit e7af2f3

File tree

8 files changed

+49
-55
lines changed

8 files changed

+49
-55
lines changed

src/external_signer.cpp

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,44 @@
99
#include <util/system.h>
1010
#include <external_signer.h>
1111

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) {}
12+
#include <stdexcept>
13+
#include <string>
14+
#include <vector>
15+
16+
#ifdef ENABLE_EXTERNAL_SIGNER
17+
18+
ExternalSigner::ExternalSigner(const std::string& command, const std::string& fingerprint, const std::string chain, const std::string name): m_command(command), m_fingerprint(fingerprint), m_chain(chain), m_name(name) {}
1319

1420
const std::string ExternalSigner::NetworkArg() const
1521
{
1622
return " --chain " + m_chain;
1723
}
1824

19-
#ifdef ENABLE_EXTERNAL_SIGNER
20-
21-
bool ExternalSigner::Enumerate(const std::string& command, std::vector<ExternalSigner>& signers, std::string chain, bool ignore_errors)
25+
bool ExternalSigner::Enumerate(const std::string& command, std::vector<ExternalSigner>& signers, const std::string chain)
2226
{
2327
// Call <command> enumerate
2428
const UniValue result = RunCommandParseJSON(command + " enumerate");
2529
if (!result.isArray()) {
26-
if (ignore_errors) return false;
27-
throw ExternalSignerException(strprintf("'%s' received invalid response, expected array of signers", command));
30+
throw std::runtime_error(strprintf("'%s' received invalid response, expected array of signers", command));
2831
}
2932
for (UniValue signer : result.getValues()) {
3033
// Check for error
3134
const UniValue& error = find_value(signer, "error");
3235
if (!error.isNull()) {
33-
if (ignore_errors) return false;
3436
if (!error.isStr()) {
35-
throw ExternalSignerException(strprintf("'%s' error", command));
37+
throw std::runtime_error(strprintf("'%s' error", command));
3638
}
37-
throw ExternalSignerException(strprintf("'%s' error: %s", command, error.getValStr()));
39+
throw std::runtime_error(strprintf("'%s' error: %s", command, error.getValStr()));
3840
}
3941
// Check if fingerprint is present
4042
const UniValue& fingerprint = find_value(signer, "fingerprint");
4143
if (fingerprint.isNull()) {
42-
if (ignore_errors) return false;
43-
throw ExternalSignerException(strprintf("'%s' received invalid response, missing signer fingerprint", command));
44+
throw std::runtime_error(strprintf("'%s' received invalid response, missing signer fingerprint", command));
4445
}
45-
std::string fingerprintStr = fingerprint.get_str();
46+
const std::string fingerprintStr = fingerprint.get_str();
4647
// Skip duplicate signer
4748
bool duplicate = false;
48-
for (ExternalSigner signer : signers) {
49+
for (const ExternalSigner& signer : signers) {
4950
if (signer.m_fingerprint.compare(fingerprintStr) == 0) duplicate = true;
5051
}
5152
if (duplicate) break;
@@ -64,7 +65,7 @@ UniValue ExternalSigner::DisplayAddress(const std::string& descriptor) const
6465
return RunCommandParseJSON(m_command + " --fingerprint \"" + m_fingerprint + "\"" + NetworkArg() + " displayaddress --desc \"" + descriptor + "\"");
6566
}
6667

67-
UniValue ExternalSigner::GetDescriptors(int account)
68+
UniValue ExternalSigner::GetDescriptors(const int account)
6869
{
6970
return RunCommandParseJSON(m_command + " --fingerprint \"" + m_fingerprint + "\"" + NetworkArg() + " getdescriptors --account " + strprintf("%d", account));
7071
}
@@ -79,7 +80,7 @@ bool ExternalSigner::SignTransaction(PartiallySignedTransaction& psbtx, std::str
7980
bool match = false;
8081
for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) {
8182
const PSBTInput& input = psbtx.inputs[i];
82-
for (auto entry : input.hd_keypaths) {
83+
for (const auto& entry : input.hd_keypaths) {
8384
if (m_fingerprint == strprintf("%08x", ReadBE32(entry.second.fingerprint))) match = true;
8485
}
8586
}
@@ -89,8 +90,8 @@ bool ExternalSigner::SignTransaction(PartiallySignedTransaction& psbtx, std::str
8990
return false;
9091
}
9192

92-
std::string command = m_command + " --stdin --fingerprint \"" + m_fingerprint + "\"" + NetworkArg();
93-
std::string stdinStr = "signtx \"" + EncodeBase64(ssTx.str()) + "\"";
93+
const std::string command = m_command + " --stdin --fingerprint \"" + m_fingerprint + "\"" + NetworkArg();
94+
const std::string stdinStr = "signtx \"" + EncodeBase64(ssTx.str()) + "\"";
9495

9596
const UniValue signer_result = RunCommandParseJSON(command, stdinStr);
9697

@@ -116,4 +117,4 @@ bool ExternalSigner::SignTransaction(PartiallySignedTransaction& psbtx, std::str
116117
return true;
117118
}
118119

119-
#endif
120+
#endif // ENABLE_EXTERNAL_SIGNER

src/external_signer.h

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@
55
#ifndef BITCOIN_EXTERNAL_SIGNER_H
66
#define BITCOIN_EXTERNAL_SIGNER_H
77

8-
#include <stdexcept>
9-
#include <string>
108
#include <univalue.h>
119
#include <util/system.h>
1210

13-
struct PartiallySignedTransaction;
11+
#include <string>
12+
#include <vector>
1413

15-
class ExternalSignerException : public std::runtime_error {
16-
public:
17-
using std::runtime_error::runtime_error;
18-
};
14+
#ifdef ENABLE_EXTERNAL_SIGNER
15+
16+
struct PartiallySignedTransaction;
1917

2018
//! Enables interaction with an external signing device or service, such as
2119
//! a hardware wallet. See doc/external-signer.md
@@ -30,7 +28,7 @@ class ExternalSigner
3028
//! @param[in] fingerprint master key fingerprint of the signer
3129
//! @param[in] chain "main", "test", "regtest" or "signet"
3230
//! @param[in] name device name
33-
ExternalSigner(const std::string& command, const std::string& fingerprint, std::string chain, std::string name);
31+
ExternalSigner(const std::string& command, const std::string& fingerprint, const std::string chain, const std::string name);
3432

3533
//! Master key fingerprint of the signer
3634
std::string m_fingerprint;
@@ -43,13 +41,12 @@ class ExternalSigner
4341

4442
const std::string NetworkArg() const;
4543

46-
#ifdef ENABLE_EXTERNAL_SIGNER
4744
//! Obtain a list of signers. Calls `<command> enumerate`.
4845
//! @param[in] command the command which handles interaction with the external signer
4946
//! @param[in,out] signers vector to which new signers (with a unique master key fingerprint) are added
5047
//! @param chain "main", "test", "regtest" or "signet"
5148
//! @returns success
52-
static bool Enumerate(const std::string& command, std::vector<ExternalSigner>& signers, std::string chain, bool ignore_errors = false);
49+
static bool Enumerate(const std::string& command, std::vector<ExternalSigner>& signers, const std::string chain);
5350

5451
//! Display address on the device. Calls `<command> displayaddress --desc <descriptor>`.
5552
//! @param[in] descriptor Descriptor specifying which address to display.
@@ -60,14 +57,14 @@ class ExternalSigner
6057
//! Calls `<command> getdescriptors --account <account>`
6158
//! @param[in] account which BIP32 account to use (e.g. `m/44'/0'/account'`)
6259
//! @returns see doc/external-signer.md
63-
UniValue GetDescriptors(int account);
60+
UniValue GetDescriptors(const int account);
6461

6562
//! Sign PartiallySignedTransaction on the device.
6663
//! Calls `<command> signtransaction` and passes the PSBT via stdin.
6764
//! @param[in,out] psbt PartiallySignedTransaction to be signed
6865
bool SignTransaction(PartiallySignedTransaction& psbt, std::string& error);
69-
70-
#endif
7166
};
7267

68+
#endif // ENABLE_EXTERNAL_SIGNER
69+
7370
#endif // BITCOIN_EXTERNAL_SIGNER_H

src/rpc/external_signer.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#include <util/strencodings.h>
1010
#include <rpc/protocol.h>
1111

12+
#include <string>
13+
#include <vector>
14+
1215
#ifdef ENABLE_EXTERNAL_SIGNER
1316

1417
static RPCHelpMan enumeratesigners()
@@ -35,18 +38,18 @@ static RPCHelpMan enumeratesigners()
3538
{
3639
const std::string command = gArgs.GetArg("-signer", "");
3740
if (command == "") throw JSONRPCError(RPC_MISC_ERROR, "Error: restart bitcoind with -signer=<cmd>");
38-
std::string chain = gArgs.GetChainName();
41+
const std::string chain = gArgs.GetChainName();
3942
UniValue signers_res = UniValue::VARR;
4043
try {
4144
std::vector<ExternalSigner> signers;
4245
ExternalSigner::Enumerate(command, signers, chain);
43-
for (ExternalSigner signer : signers) {
46+
for (const ExternalSigner& signer : signers) {
4447
UniValue signer_res = UniValue::VOBJ;
4548
signer_res.pushKV("fingerprint", signer.m_fingerprint);
4649
signer_res.pushKV("name", signer.m_name);
4750
signers_res.push_back(signer_res);
4851
}
49-
} catch (const ExternalSignerException& e) {
52+
} catch (const std::exception& e) {
5053
throw JSONRPCError(RPC_MISC_ERROR, e.what());
5154
}
5255
UniValue result(UniValue::VOBJ);

src/wallet/external_signer_scriptpubkeyman.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
#include <external_signer.h>
77
#include <wallet/external_signer_scriptpubkeyman.h>
88

9+
#include <iostream>
10+
#include <memory>
11+
#include <stdexcept>
12+
#include <string>
13+
#include <utility>
14+
#include <vector>
15+
916
#ifdef ENABLE_EXTERNAL_SIGNER
1017

1118
bool ExternalSignerScriptPubKeyMan::SetupDescriptor(std::unique_ptr<Descriptor> desc)

src/wallet/external_signer_scriptpubkeyman.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#ifdef ENABLE_EXTERNAL_SIGNER
99
#include <wallet/scriptpubkeyman.h>
1010

11+
#include <memory>
12+
1113
class ExternalSignerScriptPubKeyMan : public DescriptorScriptPubKeyMan
1214
{
1315
public:

src/wallet/rpcwallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2750,7 +2750,7 @@ static RPCHelpMan createwallet()
27502750
#ifdef ENABLE_EXTERNAL_SIGNER
27512751
flags |= WALLET_FLAG_EXTERNAL_SIGNER;
27522752
#else
2753-
throw JSONRPCError(RPC_WALLET_ERROR, "Configure with --enable-external-signer to use this");
2753+
throw JSONRPCError(RPC_WALLET_ERROR, "Compiled without external signing support (required for external signing)");
27542754
#endif
27552755
}
27562756

src/wallet/wallet.cpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3594,19 +3594,6 @@ void ReserveDestination::ReturnDestination()
35943594
address = CNoDestination();
35953595
}
35963596

3597-
#ifdef ENABLE_EXTERNAL_SIGNER
3598-
ExternalSigner CWallet::GetExternalSigner()
3599-
{
3600-
const std::string command = gArgs.GetArg("-signer", "");
3601-
if (command == "") throw std::runtime_error(std::string(__func__) + ": restart bitcoind with -signer=<cmd>");
3602-
std::vector<ExternalSigner> signers;
3603-
ExternalSigner::Enumerate(command, signers, Params().NetworkIDString());
3604-
if (signers.empty()) throw std::runtime_error(std::string(__func__) + ": No external signers found");
3605-
// TODO: add fingerprint argument in case of multiple signers
3606-
return signers[0];
3607-
}
3608-
#endif
3609-
36103597
bool CWallet::DisplayAddress(const CTxDestination& dest)
36113598
{
36123599
#ifdef ENABLE_EXTERNAL_SIGNER
@@ -3619,7 +3606,7 @@ bool CWallet::DisplayAddress(const CTxDestination& dest)
36193606
if (signer_spk_man == nullptr) {
36203607
return false;
36213608
}
3622-
ExternalSigner signer = GetExternalSigner(); // TODO: move signer in spk_man
3609+
ExternalSigner signer = ExternalSignerScriptPubKeyMan::GetExternalSigner();
36233610
return signer_spk_man->DisplayAddress(scriptPubKey, signer);
36243611
#else
36253612
return false;
@@ -4516,7 +4503,7 @@ void CWallet::LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor& desc)
45164503
auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(new ExternalSignerScriptPubKeyMan(*this, desc));
45174504
m_spk_managers[id] = std::move(spk_manager);
45184505
#else
4519-
throw std::runtime_error(std::string(__func__) + ": Configure with --enable-external-signer to use external signer wallets");
4506+
throw std::runtime_error(std::string(__func__) + ": Compiled without external signing support (required for external signing)");
45204507
#endif
45214508
} else {
45224509
auto spk_manager = std::unique_ptr<ScriptPubKeyMan>(new DescriptorScriptPubKeyMan(*this, desc));
@@ -4585,8 +4572,8 @@ void CWallet::SetupDescriptorScriptPubKeyMans()
45854572
}
45864573
}
45874574
#else
4588-
throw std::runtime_error(std::string(__func__) + ": Wallets with external signers require Boost::Process library.");
4589-
#endif
4575+
throw std::runtime_error(std::string(__func__) + ": Compiled without external signing support (required for external signing)");
4576+
#endif // ENABLE_EXTERNAL_SIGNER
45904577
}
45914578
}
45924579

src/wallet/wallet.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -845,9 +845,6 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
845845

846846
std::vector<OutputGroup> GroupOutputs(const std::vector<COutput>& outputs, bool separate_coins, const CFeeRate& effective_feerate, const CFeeRate& long_term_feerate, const CoinEligibilityFilter& filter, bool positive_only) const;
847847

848-
#ifdef ENABLE_EXTERNAL_SIGNER
849-
ExternalSigner GetExternalSigner() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
850-
#endif
851848
/** Display address on an external signer. Returns false if external signer support is not compiled */
852849
bool DisplayAddress(const CTxDestination& dest) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
853850

0 commit comments

Comments
 (0)