From c6fec9425bb99a17768e9e4745307b074fc1b222 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Fri, 4 Jul 2025 13:38:13 +0000 Subject: [PATCH 01/72] wallet: add option to avoid script path spends --- src/common/types.h | 6 ++++++ src/psbt.cpp | 2 +- src/script/sign.cpp | 8 ++++++++ src/script/sign.h | 3 +++ src/wallet/wallet.h | 1 + 5 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/common/types.h b/src/common/types.h index 1ffcd392d609..efcebc9062df 100644 --- a/src/common/types.h +++ b/src/common/types.h @@ -48,6 +48,12 @@ struct PSBTFillOptions { * Whether to fill in bip32 derivation information if available. */ bool bip32_derivs{true}; + + /** + * Only add new Taproot key-path data, and only sign and finalize Taproot + * inputs using the key path. Existing script-path data is left intact. + */ + bool taproot_keypath_only{false}; }; } // namespace common diff --git a/src/psbt.cpp b/src/psbt.cpp index 51fb19591a56..5345fb12f4b8 100644 --- a/src/psbt.cpp +++ b/src/psbt.cpp @@ -726,7 +726,7 @@ util::Expected SignPSBTInput(const SigningProvider& provider, P if (txdata == nullptr) { sig_complete = ProduceSignature(provider, DUMMY_SIGNATURE_CREATOR, utxo.scriptPubKey, sigdata); } else { - MutableTransactionSignatureCreator creator(tx, index, utxo.nValue, txdata, {.sighash_type = sighash}); + MutableTransactionSignatureCreator creator(tx, index, utxo.nValue, txdata, {.sighash_type = sighash, .taproot_keypath_only = options.taproot_keypath_only}); sig_complete = ProduceSignature(provider, creator, utxo.scriptPubKey, sigdata); } // Verify that a witness signature was produced in case one was required. diff --git a/src/script/sign.cpp b/src/script/sign.cpp index 3b382543b5cd..b1101cbe1644 100644 --- a/src/script/sign.cpp +++ b/src/script/sign.cpp @@ -564,9 +564,13 @@ static bool SignTaproot(const SigningProvider& provider, const BaseSignatureCrea { TaprootSpendData spenddata; TaprootBuilder builder; + const bool taproot_keypath_only{creator.Options().taproot_keypath_only}; // Gather information about this output. if (provider.GetTaprootSpendData(output, spenddata)) { + // Avoid merging newly provided script path data. Existing taproot + // script path fields in sigdata (e.g. from a PSBT) are left intact. + if (taproot_keypath_only) spenddata.scripts.clear(); sigdata.tr_spenddata.Merge(spenddata); } if (provider.GetTaprootBuilder(output, builder)) { @@ -618,6 +622,10 @@ static bool SignTaproot(const SigningProvider& provider, const BaseSignatureCrea } } + // Key path signing failed. In keypath-only mode, stop here instead of + // attempting a script path signature. + if (taproot_keypath_only) return false; + // Try script path spending. std::vector> smallest_result_stack; for (const auto& [key, control_blocks] : sigdata.tr_spenddata.scripts) { diff --git a/src/script/sign.h b/src/script/sign.h index 107abb9e8492..558d7749f3f8 100644 --- a/src/script/sign.h +++ b/src/script/sign.h @@ -33,6 +33,7 @@ struct SignatureData; struct SignOptions { int sighash_type{SIGHASH_DEFAULT}; + bool taproot_keypath_only{false}; }; /** Interface for signature creators. */ @@ -40,6 +41,7 @@ class BaseSignatureCreator { public: virtual ~BaseSignatureCreator() = default; virtual const BaseSignatureChecker& Checker() const =0; + virtual SignOptions Options() const { return {}; } /** Create a singular (non-script) signature. */ virtual bool CreateSig(const SigningProvider& provider, std::vector& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const =0; @@ -65,6 +67,7 @@ class MutableTransactionSignatureCreator : public BaseSignatureCreator MutableTransactionSignatureCreator(const CMutableTransaction& tx LIFETIMEBOUND, unsigned int input_idx, const CAmount& amount, const SignOptions& options); MutableTransactionSignatureCreator(const CMutableTransaction& tx LIFETIMEBOUND, unsigned int input_idx, const CAmount& amount, const PrecomputedTransactionData* txdata, const SignOptions& options); const BaseSignatureChecker& Checker() const override { return checker; } + SignOptions Options() const override { return m_options; } bool CreateSig(const SigningProvider& provider, std::vector& vchSig, const CKeyID& keyid, const CScript& scriptCode, SigVersion sigversion) const override; bool CreateSchnorrSig(const SigningProvider& provider, std::vector& sig, const XOnlyPubKey& pubkey, const uint256* leaf_hash, const uint256* merkle_root, SigVersion sigversion) const override; std::vector CreateMuSig2Nonce(const SigningProvider& provider, const CPubKey& aggregate_pubkey, const CPubKey& script_pubkey, const CPubKey& part_pubkey, const uint256* leaf_hash, const uint256* merkle_root, SigVersion sigversion, const SignatureData& sigdata) const override; diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 350d7e4db109..0ce32ae7c836 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -136,6 +136,7 @@ inline constexpr bool DEFAULT_WALLET_RBF = true; inline constexpr bool DEFAULT_WALLETBROADCAST = true; inline constexpr bool DEFAULT_DISABLE_WALLET = false; inline constexpr bool DEFAULT_WALLETCROSSCHAIN = false; +inline constexpr bool DEFAULT_SIGN_TAPROOT_KEYPATH_ONLY{false}; //! -maxtxfee default inline constexpr CAmount DEFAULT_TRANSACTION_MAXFEE{COIN / 10}; //! -maxfeerate default From c4d69b22bf4345f99e2d248eccfc9888e4f9c1ae Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Fri, 6 Nov 2026 05:11:17 +0000 Subject: [PATCH 02/72] rpc: add keypath_only to walletprocesspsbt --- src/rpc/client.cpp | 1 + src/wallet/rpc/spend.cpp | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index ad5bcddf19e6..08d4c9f6df06 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -226,6 +226,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "walletprocesspsbt", 2, "sighashtype", ParamFormat::STRING }, { "walletprocesspsbt", 3, "bip32derivs" }, { "walletprocesspsbt", 4, "finalize" }, + { "walletprocesspsbt", 5, "keypath_only"}, { "descriptorprocesspsbt", 0, "psbt", ParamFormat::STRING }, { "descriptorprocesspsbt", 1, "descriptors"}, { "descriptorprocesspsbt", 2, "sighashtype", ParamFormat::STRING }, diff --git a/src/wallet/rpc/spend.cpp b/src/wallet/rpc/spend.cpp index aba40e385584..9bd93d5d36d7 100644 --- a/src/wallet/rpc/spend.cpp +++ b/src/wallet/rpc/spend.cpp @@ -1654,6 +1654,7 @@ RPCMethod walletprocesspsbt() " \"SINGLE|ANYONECANPAY\""}, {"bip32derivs", RPCArg::Type::BOOL, RPCArg::Default{true}, "Include BIP 32 derivation paths for public keys if we know them"}, {"finalize", RPCArg::Type::BOOL, RPCArg::Default{true}, "Also finalize inputs if possible"}, + {"keypath_only", RPCArg::Type::BOOL, RPCArg::Default{DEFAULT_SIGN_TAPROOT_KEYPATH_ONLY}, "Only add Taproot key-path data, and only sign and finalize Taproot inputs using the key path. Existing script-path data is not signed or finalized."}, }, RPCResult{ RPCResult::Type::OBJ, "", "", @@ -1690,11 +1691,12 @@ RPCMethod walletprocesspsbt() const bool sign{self.Arg("sign")}; const bool bip32derivs{self.Arg("bip32derivs")}; const bool finalize{self.Arg("finalize")}; + const bool keypath_only{self.Arg("keypath_only")}; bool complete = true; if (sign) EnsureWalletIsUnlocked(*pwallet); - const auto err{wallet.FillPSBT(psbtx, {.sign = sign, .sighash_type = nHashType, .finalize = finalize, .bip32_derivs = bip32derivs}, complete)}; + const auto err{wallet.FillPSBT(psbtx, {.sign = sign, .sighash_type = nHashType, .finalize = finalize, .bip32_derivs = bip32derivs, .taproot_keypath_only = keypath_only}, complete)}; if (err) { throw JSONRPCPSBTError(*err); } From 33355238556f924bd5c7ebef2b4b75f438750fb0 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Thu, 3 Jul 2025 12:36:42 +0000 Subject: [PATCH 03/72] test: cover keypath_only in wallet_taproot.py Expand taproot tests to cover avoid_script_path in walletprocesspsbt. When avoiding script paths, there's no need for the workaround that increases fee_rate to compensate for the wallet's inability to estimate fees for script path spends. We use this to indirectly test that key path was used. We also check that taproot_script_path_sigs is not set. Finally, for transactions that can't be signed using their key path, we try again by allowing the script path. Additional test extended private keys were extracted from other tests. Co-authored-by: rkrux --- test/functional/wallet_taproot.py | 38 +++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/test/functional/wallet_taproot.py b/test/functional/wallet_taproot.py index 430235c3f262..b82e2ff4f4cd 100755 --- a/test/functional/wallet_taproot.py +++ b/test/functional/wallet_taproot.py @@ -170,8 +170,8 @@ def do_test_sendtoaddress(self, comment, pattern, privmap, treefn, keys_pay, key assert rpc_online.gettransaction(txid)["confirmations"] > 0 rpc_online.unloadwallet() - def do_test_psbt(self, comment, pattern, privmap, treefn, keys_pay, keys_change): - self.log.info("Testing %s through PSBT" % comment) + def do_test_psbt(self, comment, pattern, privmap, treefn, keys_pay, keys_change, keypath_only): + self.log.info(f"Testing {comment} through PSBT { '(key path only)' if keypath_only else '' }") # Create wallets wallet_uuid = uuid.uuid4().hex @@ -211,12 +211,16 @@ def do_test_psbt(self, comment, pattern, privmap, treefn, keys_pay, keys_change) self.generatetoaddress(self.nodes[0], 1, self.boring.getnewaddress(), sync_fun=self.no_op) test_balance = int(psbt_online.getbalance() * 100000000) ret_amnt = random.randrange(100000, test_balance) - # Increase fee_rate to compensate for the wallet's inability to estimate fees for script path spends. - psbt = psbt_online.walletcreatefundedpsbt([], [{self.boring.getnewaddress(): Decimal(ret_amnt) / 100000000}], None, {"subtractFeeFromOutputs":[0], "fee_rate": 200, "change_type": address_type})['psbt'] - res = psbt_offline.walletprocesspsbt(psbt=psbt, finalize=False) + fee_rate = 1 + if not keypath_only: + # Increase fee_rate to compensate for the wallet's inability to estimate fees for script path spends. + fee_rate = 200 + psbt = psbt_online.walletcreatefundedpsbt([], [{self.boring.getnewaddress(): Decimal(ret_amnt) / 100000000}], None, {"subtractFeeFromOutputs":[0], "fee_rate": fee_rate, "change_type": address_type})['psbt'] + res = psbt_offline.walletprocesspsbt(psbt=psbt, finalize=False, keypath_only=keypath_only) for wallet in [psbt_offline, key_only_wallet]: - res = wallet.walletprocesspsbt(psbt=psbt, finalize=False) + res = wallet.walletprocesspsbt(psbt=psbt, finalize=False, keypath_only=keypath_only) + retry = False decoded = wallet.decodepsbt(res["psbt"]) if pattern.startswith("tr("): for psbtin in decoded["inputs"]: @@ -224,13 +228,25 @@ def do_test_psbt(self, comment, pattern, privmap, treefn, keys_pay, keys_change) assert "witness_utxo" in psbtin assert "taproot_internal_key" in psbtin assert "taproot_bip32_derivs" in psbtin - assert "taproot_key_path_sig" in psbtin or "taproot_script_path_sigs" in psbtin + if keypath_only: + assert "taproot_script_path_sigs" not in psbtin + if "taproot_key_path_sig" not in psbtin: + retry = True + else: + assert "taproot_key_path_sig" in psbtin or "taproot_script_path_sigs" in psbtin if "taproot_script_path_sigs" in psbtin: assert "taproot_merkle_root" in psbtin assert "taproot_scripts" in psbtin + if retry: + self.log.debug("Retry with script path") + fee_rate = 200 + psbt = psbt_online.walletcreatefundedpsbt([], [{self.boring.getnewaddress(): Decimal(ret_amnt) / 100000000}], None, {"subtractFeeFromOutputs":[0], "fee_rate": fee_rate, "change_type": address_type})['psbt'] + res = wallet.walletprocesspsbt(psbt=psbt, finalize=False, keypath_only=False) + rawtx = self.nodes[0].finalizepsbt(res['psbt'])['hex'] res = self.nodes[0].testmempoolaccept([rawtx]) + self.log.debug(res) assert res[0]["allowed"] txid = self.nodes[0].sendrawtransaction(rawtx) @@ -250,13 +266,15 @@ def do_test_psbt(self, comment, pattern, privmap, treefn, keys_pay, keys_change) def do_test(self, comment, pattern, privmap, treefn): nkeys = len(privmap) - keys = random.sample(self.keys, nkeys * 4) + keys = random.sample(self.keys, nkeys * 6) self.do_test_addr(comment, pattern, privmap, treefn, keys[0:nkeys]) self.do_test_sendtoaddress(comment, pattern, privmap, treefn, keys[0:nkeys], keys[nkeys:2*nkeys]) - self.do_test_psbt(comment, pattern, privmap, treefn, keys[2*nkeys:3*nkeys], keys[3*nkeys:4*nkeys]) + self.do_test_psbt(comment, pattern, privmap, treefn, keys[2*nkeys:3*nkeys], keys[3*nkeys:4*nkeys], keypath_only=False) + if 'tr' in pattern: + self.do_test_psbt(comment, pattern, privmap, treefn, keys[4*nkeys:5*nkeys], keys[5*nkeys:6*nkeys], keypath_only=True) def generate_test_keys(self): - xprvs = [ExtendedPrivateKey.generate() for _ in range(0, 13)] + xprvs = [ExtendedPrivateKey.generate() for _ in range(0, 18)] return [{ "xprv": xprv.to_string(), "xpub": xprv.pubkey().to_string(), From 45ee9769556b7a3581d88ccbd3419b4ce9cb8c50 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Fri, 6 Nov 2026 05:14:25 +0000 Subject: [PATCH 04/72] rpc: add keypath_only to send and sendall --- src/rpc/client.cpp | 2 ++ src/wallet/rpc/spend.cpp | 13 ++++++-- test/functional/wallet_fundrawtransaction.py | 2 ++ test/functional/wallet_taproot.py | 35 ++++++++++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index 08d4c9f6df06..4275a3433013 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -279,6 +279,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "send", 4, "replaceable"}, { "send", 4, "solving_data"}, { "send", 4, "max_tx_weight"}, + { "send", 4, "keypath_only"}, { "send", 5, "version"}, { "sendall", 0, "recipients" }, { "sendall", 1, "conf_target" }, @@ -294,6 +295,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "sendall", 4, "send_max"}, { "sendall", 4, "minconf"}, { "sendall", 4, "maxconf"}, + { "sendall", 4, "keypath_only"}, { "sendall", 4, "conf_target"}, { "sendall", 4, "replaceable"}, { "sendall", 4, "solving_data"}, diff --git a/src/wallet/rpc/spend.cpp b/src/wallet/rpc/spend.cpp index 9bd93d5d36d7..d6b7ceaf7cf4 100644 --- a/src/wallet/rpc/spend.cpp +++ b/src/wallet/rpc/spend.cpp @@ -115,11 +115,13 @@ static UniValue FinishTransaction(const std::shared_ptr pwallet, const // Make a blank psbt PartiallySignedTransaction psbtx(rawTx, /*version=*/2); + bool keypath_only{options.exists("keypath_only") ? options["keypath_only"].get_bool() : DEFAULT_SIGN_TAPROOT_KEYPATH_ONLY}; + // First fill transaction with our data without signing, // so external signers are not asked to sign more than once. bool complete; - pwallet->FillPSBT(psbtx, {.sign = false, .bip32_derivs = true}, complete); - const auto err{pwallet->FillPSBT(psbtx, {.sign = true, .bip32_derivs = false}, complete)}; + pwallet->FillPSBT(psbtx, {.sign = false, .bip32_derivs = true, .taproot_keypath_only = keypath_only}, complete); + const auto err{pwallet->FillPSBT(psbtx, {.sign = true, .bip32_derivs = false, .taproot_keypath_only = keypath_only}, complete)}; if (err) { throw JSONRPCPSBTError(*err); } @@ -517,6 +519,7 @@ CreatedTransactionResult FundTransaction(CWallet& wallet, const CMutableTransact {"includeWatching", UniValueType(UniValue::VBOOL)}, {"include_watching", UniValueType(UniValue::VBOOL)}, {"inputs", UniValueType(UniValue::VARR)}, + {"keypath_only", UniValueType(UniValue::VBOOL)}, {"lockUnspents", UniValueType(UniValue::VBOOL)}, {"lock_unspents", UniValueType(UniValue::VBOOL)}, {"locktime", UniValueType(UniValue::VNUM)}, @@ -829,6 +832,9 @@ RPCMethod fundrawtransaction() throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed"); } UniValue options = request.params[1]; + if (options.exists("keypath_only")) { + throw JSONRPCError(RPC_TYPE_ERROR, "Unexpected key keypath_only"); + } std::vector> destinations; for (const auto& tx_out : tx.vout) { CTxDestination dest; @@ -1241,6 +1247,7 @@ RPCMethod send() }}, }, }, + {"keypath_only", RPCArg::Type::BOOL, RPCArg::Default{DEFAULT_SIGN_TAPROOT_KEYPATH_ONLY}, "Only add Taproot key-path data, and only sign and finalize Taproot inputs using the key path. Existing script-path data is not signed or finalized."}, {"locktime", RPCArg::Type::NUM, RPCArg::DefaultHint{"locktime close to block height to prevent fee sniping"}, "Raw locktime. Non-0 value also locktime-activates inputs"}, {"lock_unspents", RPCArg::Type::BOOL, RPCArg::Default{false}, "Lock selected unspent outputs"}, {"psbt", RPCArg::Type::BOOL, RPCArg::DefaultHint{"automatic"}, "Always return a PSBT, implies add_to_wallet=false."}, @@ -1365,6 +1372,7 @@ RPCMethod sendall() {"send_max", RPCArg::Type::BOOL, RPCArg::Default{false}, "When true, only use UTXOs that can pay for their own fees to maximize the output amount. When 'false' (default), no UTXO is left behind. send_max is incompatible with providing specific inputs."}, {"minconf", RPCArg::Type::NUM, RPCArg::Default{0}, "Require inputs with at least this many confirmations."}, {"maxconf", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "Require inputs with at most this many confirmations."}, + {"keypath_only", RPCArg::Type::BOOL, RPCArg::Default{DEFAULT_SIGN_TAPROOT_KEYPATH_ONLY}, "Only add Taproot key-path data, and only sign and finalize Taproot inputs using the key path. Existing script-path data is not signed or finalized."}, {"version", RPCArg::Type::NUM, RPCArg::Default{DEFAULT_WALLET_TX_VERSION}, "Transaction version"}, }, FundTxDoc() @@ -1408,6 +1416,7 @@ RPCMethod sendall() {"fee_rate", UniValueType()}, {"include_watching", UniValueType(UniValue::VBOOL)}, {"inputs", UniValueType(UniValue::VARR)}, + {"keypath_only", UniValueType(UniValue::VBOOL)}, {"locktime", UniValueType(UniValue::VNUM)}, {"lock_unspents", UniValueType(UniValue::VBOOL)}, {"psbt", UniValueType(UniValue::VBOOL)}, diff --git a/test/functional/wallet_fundrawtransaction.py b/test/functional/wallet_fundrawtransaction.py index 01c3cb7b6bdb..14f6c9164ef1 100755 --- a/test/functional/wallet_fundrawtransaction.py +++ b/test/functional/wallet_fundrawtransaction.py @@ -298,8 +298,10 @@ def test_invalid_option(self): dec_tx = self.nodes[2].decoderawtransaction(rawtx) assert_equal(utx['txid'], dec_tx['vin'][0]['txid']) + assert "keypath_only" not in self.nodes[2].help("fundrawtransaction") assert_raises_rpc_error(-8, "Unknown named parameter foo", self.nodes[2].fundrawtransaction, rawtx, foo='bar') assert_raises_rpc_error(-3, "JSON value of type bool is not of expected type object", self.nodes[2].fundrawtransaction, rawtx, True) + assert_raises_rpc_error(-3, "Unexpected key keypath_only", self.nodes[2].fundrawtransaction, rawtx, {"keypath_only": True}) # reserveChangeKey was deprecated and is now removed assert_raises_rpc_error(-8, "Unknown named parameter reserveChangeKey", lambda: self.nodes[2].fundrawtransaction(hexstring=rawtx, reserveChangeKey=True)) diff --git a/test/functional/wallet_taproot.py b/test/functional/wallet_taproot.py index b82e2ff4f4cd..61573c5e440b 100755 --- a/test/functional/wallet_taproot.py +++ b/test/functional/wallet_taproot.py @@ -253,6 +253,41 @@ def do_test_psbt(self, comment, pattern, privmap, treefn, keys_pay, keys_change, self.generatetoaddress(self.nodes[0], 1, self.boring.getnewaddress(), sync_fun=self.no_op) assert psbt_online.gettransaction(txid)['confirmations'] > 0 + if keypath_only: + self.log.info("Testing send with keypath_only") + + def assert_no_script_path(psbt): + decoded = psbt_online.decodepsbt(psbt) + for psbtin in decoded["inputs"]: + assert "taproot_script_path_sigs" not in psbtin + + test_balance = int(psbt_online.getbalance() * 100000000) + outputs = {self.boring.getnewaddress(): Decimal(test_balance // 2) / 100000000} + res = psbt_online.send( + outputs=outputs, + options={ + "keypath_only": True, + "psbt": True, + "add_to_wallet": False, + "fee_rate": 1, + "subtract_fee_from_outputs": [0], + }, + ) + assert_equal(res["complete"], False) + assert_no_script_path(res["psbt"]) + + self.log.info("Testing sendall with keypath_only") + res = psbt_online.sendall( + recipients=[self.boring.getnewaddress()], + keypath_only=True, + psbt=True, + add_to_wallet=False, + fee_rate=1, + ) + assert_equal(res["complete"], False) + assert_no_script_path(res["psbt"]) + + # Cleanup # Match the framework fallbackfee; otherwise the underestimated taproot # script-path spend size can produce an effective feerate below min relay. psbt = psbt_online.sendall(recipients=[self.boring.getnewaddress()], psbt=True, fee_rate=20)["psbt"] From 0949b3c0fba9430d30e37998c713fccc42ae8fc7 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Fri, 6 Nov 2026 05:12:29 +0000 Subject: [PATCH 05/72] rpc: add keypath_only to walletcreatefundedpsbt --- src/rpc/client.cpp | 1 + src/wallet/rpc/spend.cpp | 4 +++- test/functional/wallet_taproot.py | 13 ++++++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index 4275a3433013..031bc5471348 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -218,6 +218,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "walletcreatefundedpsbt", 3, "replaceable"}, { "walletcreatefundedpsbt", 3, "solving_data"}, { "walletcreatefundedpsbt", 3, "max_tx_weight"}, + { "walletcreatefundedpsbt", 3, "keypath_only"}, { "walletcreatefundedpsbt", 4, "bip32derivs" }, { "walletcreatefundedpsbt", 5, "version" }, { "walletcreatefundedpsbt", 6, "psbt_version" }, diff --git a/src/wallet/rpc/spend.cpp b/src/wallet/rpc/spend.cpp index d6b7ceaf7cf4..1728a4525149 100644 --- a/src/wallet/rpc/spend.cpp +++ b/src/wallet/rpc/spend.cpp @@ -1775,6 +1775,7 @@ RPCMethod walletcreatefundedpsbt() {"change_position|changePosition", RPCArg::Type::NUM, RPCArg::DefaultHint{"random"}, "The index of the change output"}, {"change_type", RPCArg::Type::STR, RPCArg::DefaultHint{"set by -changetype"}, "The output type to use. Only valid if changeAddress is not specified. Options are " + FormatAllOutputTypes() + "."}, {"includeWatching", RPCArg::Type::BOOL, RPCArg::Default{false}, "(DEPRECATED) No longer used"}, + {"keypath_only", RPCArg::Type::BOOL, RPCArg::Default{DEFAULT_SIGN_TAPROOT_KEYPATH_ONLY}, "Only add Taproot key-path data to the PSBT. Script-path data is not added."}, {"lock_unspents|lockUnspents", RPCArg::Type::BOOL, RPCArg::Default{false}, "Lock selected unspent outputs"}, {"fee_rate", RPCArg::Type::AMOUNT, RPCArg::DefaultHint{"not set, fall back to wallet fee estimation"}, "Specify a fee rate in " + CURRENCY_ATOM + "/vB."}, {"feeRate", RPCArg::Type::AMOUNT, RPCArg::DefaultHint{"not set, fall back to wallet fee estimation"}, "Specify a fee rate in " + CURRENCY_UNIT + "/kvB."}, @@ -1853,8 +1854,9 @@ RPCMethod walletcreatefundedpsbt() // Fill transaction with our data but don't sign const bool bip32derivs{self.Arg("bip32derivs")}; + bool keypath_only{options.exists("keypath_only") ? options["keypath_only"].get_bool() : DEFAULT_SIGN_TAPROOT_KEYPATH_ONLY}; bool complete = true; - const auto err{wallet.FillPSBT(psbtx, {.sign = false, .bip32_derivs = bip32derivs}, complete)}; + const auto err{wallet.FillPSBT(psbtx, {.sign = false, .bip32_derivs = bip32derivs, .taproot_keypath_only = keypath_only}, complete)}; if (err) { throw JSONRPCPSBTError(*err); } diff --git a/test/functional/wallet_taproot.py b/test/functional/wallet_taproot.py index 61573c5e440b..7da4bcb01c4a 100755 --- a/test/functional/wallet_taproot.py +++ b/test/functional/wallet_taproot.py @@ -181,6 +181,8 @@ def do_test_psbt(self, comment, pattern, privmap, treefn, keys_pay, keys_change, psbt_online = self.nodes[0].get_wallet_rpc(f"psbt_online_{wallet_uuid}") psbt_offline = self.nodes[1].get_wallet_rpc(f"psbt_offline_{wallet_uuid}") key_only_wallet = self.nodes[1].get_wallet_rpc(f"key_only_wallet_{wallet_uuid}") + if keypath_only: + assert "keypath_only" in psbt_online.help("walletcreatefundedpsbt") desc_pay = self.make_desc(pattern, privmap, keys_pay, False) desc_change = self.make_desc(pattern, privmap, keys_change, False) @@ -215,7 +217,16 @@ def do_test_psbt(self, comment, pattern, privmap, treefn, keys_pay, keys_change, if not keypath_only: # Increase fee_rate to compensate for the wallet's inability to estimate fees for script path spends. fee_rate = 200 - psbt = psbt_online.walletcreatefundedpsbt([], [{self.boring.getnewaddress(): Decimal(ret_amnt) / 100000000}], None, {"subtractFeeFromOutputs":[0], "fee_rate": fee_rate, "change_type": address_type})['psbt'] + options = { + "subtractFeeFromOutputs": [0], + "fee_rate": fee_rate, + "change_type": address_type, + "keypath_only": keypath_only, + } + psbt = psbt_online.walletcreatefundedpsbt([], [{self.boring.getnewaddress(): Decimal(ret_amnt) / 100000000}], None, options)['psbt'] + if keypath_only: + for psbtin in psbt_online.decodepsbt(psbt)["inputs"]: + assert "taproot_scripts" not in psbtin res = psbt_offline.walletprocesspsbt(psbt=psbt, finalize=False, keypath_only=keypath_only) for wallet in [psbt_offline, key_only_wallet]: res = wallet.walletprocesspsbt(psbt=psbt, finalize=False, keypath_only=keypath_only) From f978eb21e369db33028d67933ad94186f3dcc966 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Fri, 6 Nov 2026 05:13:31 +0000 Subject: [PATCH 06/72] rpc: add keypath_only to descriptorprocesspsbt --- src/rpc/client.cpp | 1 + src/rpc/rawtransaction.cpp | 21 ++++++++--- test/functional/rpc_psbt.py | 69 +++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index 031bc5471348..2c0882fc7e93 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -233,6 +233,7 @@ static const CRPCConvertParam vRPCConvertParams[] = { "descriptorprocesspsbt", 2, "sighashtype", ParamFormat::STRING }, { "descriptorprocesspsbt", 3, "bip32derivs" }, { "descriptorprocesspsbt", 4, "finalize" }, + { "descriptorprocesspsbt", 5, "keypath_only"}, { "createpsbt", 0, "inputs" }, { "createpsbt", 1, "outputs" }, { "createpsbt", 2, "locktime" }, diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index 031ec21c6aa3..c52351449a0b 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -152,7 +152,13 @@ static std::vector CreateTxDoc() // Update PSBT with information from the mempool, the UTXO set, the txindex, and the provided descriptors. // Optionally, sign the inputs that we can using information from the descriptors. -PartiallySignedTransaction ProcessPSBT(const std::string& psbt_string, const std::any& context, const HidingSigningProvider& provider, std::optional sighash_type, bool finalize) +PartiallySignedTransaction ProcessPSBT( + const std::string& psbt_string, + const std::any& context, + const HidingSigningProvider& provider, + std::optional sighash_type, + bool finalize, + bool taproot_keypath_only) { // Unserialize the transactions util::Result psbt_res = DecodeBase64PSBT(psbt_string); @@ -214,7 +220,10 @@ PartiallySignedTransaction ProcessPSBT(const std::string& psbt_string, const std // We only actually care about those if our signing provider doesn't hide private // information, as is the case with `descriptorprocesspsbt` // Only error for mismatching sighash types as it is critical that the sighash to sign with matches the PSBT's - const auto sign_result = SignPSBTInput(provider, psbtx, /*index=*/i, &txdata, {.sighash_type = sighash_type, .finalize = finalize}, /*out_sigdata=*/nullptr); + const auto sign_result = SignPSBTInput(provider, psbtx, /*index=*/i, &txdata, { + .sighash_type = sighash_type, + .finalize = finalize, + .taproot_keypath_only = taproot_keypath_only}, /*out_sigdata=*/nullptr); if (!sign_result.has_value() && sign_result.error() == common::PSBTError::SIGHASH_MISMATCH) { throw JSONRPCPSBTError(common::PSBTError::SIGHASH_MISMATCH); } @@ -1866,7 +1875,8 @@ static RPCMethod utxoupdatepsbt() request.context, HidingSigningProvider(&provider, /*hide_secret=*/true, /*hide_origin=*/false), /*sighash_type=*/std::nullopt, - /*finalize=*/false); + /*finalize=*/false, + /*taproot_keypath_only=*/false); DataStream ssTx{}; ssTx << psbtx; @@ -2093,6 +2103,7 @@ RPCMethod descriptorprocesspsbt() " \"SINGLE|ANYONECANPAY\""}, {"bip32derivs", RPCArg::Type::BOOL, RPCArg::Default{true}, "Include BIP 32 derivation paths for public keys if we know them"}, {"finalize", RPCArg::Type::BOOL, RPCArg::Default{true}, "Also finalize inputs if possible"}, + {"keypath_only", RPCArg::Type::BOOL, RPCArg::Default{false}, "Only add Taproot key-path data, and only sign and finalize Taproot inputs using the key path. Existing script-path data is not signed or finalized."}, }, RPCResult{ RPCResult::Type::OBJ, "", "", @@ -2119,13 +2130,15 @@ RPCMethod descriptorprocesspsbt() std::optional sighash_type = ParseSighashString(request.params[2]); const bool bip32derivs{self.Arg("bip32derivs")}; const bool finalize{self.Arg("finalize")}; + const bool keypath_only{self.Arg("keypath_only")}; const PartiallySignedTransaction& psbtx = ProcessPSBT( request.params[0].get_str(), request.context, HidingSigningProvider(&provider, /*hide_secret=*/false, !bip32derivs), sighash_type, - finalize); + finalize, + keypath_only); // Check whether or not all of the inputs are now correctly signed bool complete = true; diff --git a/test/functional/rpc_psbt.py b/test/functional/rpc_psbt.py index 7c362ed99069..49ecdf9348fc 100755 --- a/test/functional/rpc_psbt.py +++ b/test/functional/rpc_psbt.py @@ -1808,6 +1808,75 @@ def global_xpub_key(extended_pubkey): # Broadcast transaction self.nodes[2].sendrawtransaction(processed_psbt['hex']) + self.log.info("Test descriptorprocesspsbt keypath_only skips taproot script path signing") + taproot_key = get_generate_key() + taproot_descriptor = descsum_create(f"tr({H_POINT},pk({taproot_key.privkey}))") + taproot_public_descriptor = descsum_create(f"tr({H_POINT},pk({taproot_key.pubkey}))") + taproot_address = self.nodes[2].deriveaddresses(taproot_descriptor)[0] + taproot_utxo = self.create_outpoints(self.nodes[0], outputs=[{taproot_address: 1}])[0] + self.sync_all() + + taproot_psbt = self.nodes[2].createpsbt([taproot_utxo], {self.nodes[0].getnewaddress(): 0.99999}) + keypath_only_psbt = self.nodes[2].descriptorprocesspsbt( + psbt=taproot_psbt, + descriptors=[taproot_descriptor], + finalize=False, + keypath_only=True, + ) + decoded = self.nodes[2].decodepsbt(keypath_only_psbt["psbt"]) + assert "taproot_scripts" not in decoded["inputs"][0] + assert "taproot_key_path_sig" not in decoded["inputs"][0] + assert "taproot_script_path_sigs" not in decoded["inputs"][0] + + prepopulated_psbt = self.nodes[2].descriptorprocesspsbt( + psbt=taproot_psbt, + descriptors=[taproot_public_descriptor], + finalize=False, + ) + decoded = self.nodes[2].decodepsbt(prepopulated_psbt["psbt"]) + assert "taproot_scripts" in decoded["inputs"][0] + assert "taproot_script_path_sigs" not in decoded["inputs"][0] + + keypath_only_prepopulated_psbt = self.nodes[2].descriptorprocesspsbt( + psbt=prepopulated_psbt["psbt"], + descriptors=[taproot_descriptor], + finalize=False, + keypath_only=True, + ) + decoded = self.nodes[2].decodepsbt(keypath_only_prepopulated_psbt["psbt"]) + assert "taproot_scripts" in decoded["inputs"][0] + assert "taproot_key_path_sig" not in decoded["inputs"][0] + assert "taproot_script_path_sigs" not in decoded["inputs"][0] + + signed_psbt = self.nodes[2].descriptorprocesspsbt( + psbt=taproot_psbt, + descriptors=[taproot_descriptor], + finalize=False, + keypath_only=False, + ) + decoded = self.nodes[2].decodepsbt(signed_psbt["psbt"]) + assert "taproot_script_path_sigs" in decoded["inputs"][0] + + # Do not finalize an existing script-path signature when keypath_only is + # requested, since that would produce a broadcastable script-path spend. + keypath_only_signed_psbt = self.nodes[2].descriptorprocesspsbt( + psbt=signed_psbt["psbt"], + descriptors=[taproot_descriptor], + finalize=True, + keypath_only=True, + ) + assert_equal(keypath_only_signed_psbt["complete"], False) + decoded = self.nodes[2].decodepsbt(keypath_only_signed_psbt["psbt"]) + assert "taproot_script_path_sigs" in decoded["inputs"][0] + assert "final_scriptwitness" not in decoded["inputs"][0] + + # The standalone finalizer has no key-path-only policy and can explicitly + # finalize the otherwise unchanged script-path signature. + finalized = self.nodes[2].finalizepsbt(keypath_only_signed_psbt["psbt"]) + assert_equal(finalized["complete"], True) + rawtx = finalized["hex"] + assert self.nodes[2].testmempoolaccept([rawtx])[0]["allowed"] + self.log.info("Test descriptorprocesspsbt raises if an invalid sighashtype is passed") assert_raises_rpc_error(-8, "'all' is not a valid sighash parameter.", self.nodes[2].descriptorprocesspsbt, psbt=psbt, descriptors=[descriptor], sighashtype="all") From 8ea7ca460e0d922bf8d82ca618ef2c994dcc2035 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Tue, 9 Jun 2026 15:42:09 +0000 Subject: [PATCH 07/72] test: cover keypath_only in wallet_musig.py Extract the descriptor with both MuSig key and script paths into key_and_script_path_musigs. Reuse it for the existing cases and give those cases descriptions. Expand the same scenario with keypath_only=true. The new case asks walletprocesspsbt to avoid script-path signing. Co-authored-by: rkrux --- test/functional/wallet_musig.py | 37 ++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/test/functional/wallet_musig.py b/test/functional/wallet_musig.py index bc12a32afff2..658beb7d3882 100755 --- a/test/functional/wallet_musig.py +++ b/test/functional/wallet_musig.py @@ -194,7 +194,16 @@ def test_failure_case_3(self, comment, pat): assert "musig2_pubnonces" in dec["inputs"][0] assert "musig2_partial_sigs" not in dec["inputs"][0] - def test_success_case(self, comment, pattern, sighash_type=None, scriptpath=False, nosign_wallets=None, only_one_musig_wallet=False): + def test_success_case( + self, + comment, + pattern, + sighash_type=None, + scriptpath=False, + keypath_only=False, + nosign_wallets=None, + only_one_musig_wallet=False, + ): self.log.info(f"Testing {comment}") has_internal = MULTIPATH_TWO_RE.search(pattern) is not None @@ -221,6 +230,9 @@ def test_success_case(self, comment, pattern, sighash_type=None, scriptpath=Fals continue if musig_partial_sigs is not None: expected_partial_sigs += musig_partial_sigs + if keypath_only: + # The first MuSig aggregate is the key path, so do not count script-path MuSigs. + break # Check that the wallets agree on the same musig address addr = None @@ -289,7 +301,7 @@ def test_success_case(self, comment, pattern, sighash_type=None, scriptpath=Fals if nosign_wallets and i in nosign_wallets: continue for psbt_list in [nonce_psbts, nonce_psbts2]: - proc = wallet.walletprocesspsbt(psbt=psbt, sighashtype=sighash_type) + proc = wallet.walletprocesspsbt(psbt=psbt, sighashtype=sighash_type, keypath_only=keypath_only) assert_equal(proc["complete"], False) psbt_list.append(proc["psbt"]) @@ -310,7 +322,7 @@ def test_success_case(self, comment, pattern, sighash_type=None, scriptpath=Fals if nosign_wallets and i in nosign_wallets: continue for psbt, psbt_list in [(comb_nonce_psbt, psig_psbts), (comb_nonce_psbt2, psig_psbts2)]: - proc = wallet.walletprocesspsbt(psbt=psbt, sighashtype=sighash_type) + proc = wallet.walletprocesspsbt(psbt=psbt, sighashtype=sighash_type, keypath_only=keypath_only) assert_equal(proc["complete"], False) psbt_list.append(proc["psbt"]) @@ -355,8 +367,23 @@ def run_test(self): self.test_success_case("tr(H,{pk(musig/*), pk(musig/*)})", "tr($H,{pk(musig($0,$1,$2)/<0;1>/*),pk(musig($3,$4,$5)/0/*)})", scriptpath=True) self.test_success_case("tr(H,{pk(musig/*), pk(same keys different musig/*)})", "tr($H,{pk(musig($0,$1,$2)/<0;1>/*),pk(musig($1,$2)/0/*)})", scriptpath=True) self.test_success_case("tr(H,and(pk(musig/*),pk(same musig, other derivation/*)))", "tr($H,and_v(v:pk(musig($0,$1,$2)/<0;1>/*),pk(musig($0,$1,$2)/<2;3>/*)))", scriptpath=True) - self.test_success_case("tr(musig/*,{pk(partial keys diff musig-1/*),pk(partial keys diff musig-2/*)})}", "tr(musig($0,$1,$2)/<3;4>/*,{pk(musig($0,$1)/<5;6>/*),pk(musig($1,$2)/7/*)})") - self.test_success_case("tr(musig/*,{pk(partial keys diff musig-1/*),pk(partial keys diff musig-2/*)})} script-path", "tr(musig($0,$1,$2)/<3;4>/*,{pk(musig($0,$1)/<5;6>/*),pk(musig($1,$2)/7/*)})", scriptpath=True, nosign_wallets=[0]) + # Descriptor with one MuSig key path and two MuSig script paths. + key_and_script_path_musigs = "tr(musig($0,$1,$2)/<3;4>/*,{pk(musig($0,$1)/<5;6>/*),pk(musig($1,$2)/7/*)})" + self.test_success_case( + "tr() with MuSig key path and different MuSig script paths", + key_and_script_path_musigs, + ) + self.test_success_case( + "tr() with MuSig script path when key path cannot sign", + key_and_script_path_musigs, + scriptpath=True, + nosign_wallets=[0], + ) + self.test_success_case( + "tr() with MuSig key path and keypath_only", + key_and_script_path_musigs, + keypath_only=True, + ) self.test_success_case("tr(H,and(pk(musig/*),after(1)))", "tr($H,and_v(v:pk(musig($0,$1,$2)/<0;1>/*),after(1)))", scriptpath=True) self.test_success_case("tr(H,and(pk_k(musig/*),after(1)))", "tr($H,and_v(vc:pk_k(musig($0,$1,$2)/<0;1>/*),after(1)))", scriptpath=True) self.test_success_case("tr(H,and(pkh(musig/*),after(1)))", "tr($H,and_v(v:pkh(musig($0,$1,$2)/<0;1>/*),after(1)))", scriptpath=True) From 317b8f259a6e1e3c41cb2d2681384d29044fb4f9 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Tue, 16 Jun 2026 15:56:18 +0000 Subject: [PATCH 08/72] doc: add release note for keypath_only --- doc/release-notes-32857.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 doc/release-notes-32857.md diff --git a/doc/release-notes-32857.md b/doc/release-notes-32857.md new file mode 100644 index 000000000000..24cdb811e794 --- /dev/null +++ b/doc/release-notes-32857.md @@ -0,0 +1,8 @@ +Updated RPCs +------------ + +- The `send`, `sendall`, `walletprocesspsbt`, `walletcreatefundedpsbt`, and + `descriptorprocesspsbt` RPCs now accept a `keypath_only` option. When enabled, + they do not add new Taproot script-path data, sign script paths, or finalize + script-path spends. Existing script-path data and signatures remain in a + supplied PSBT. (#32857) From 5d0e71dc7f87eec111e9314c0cb0f938f7f719d8 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Thu, 14 May 2026 13:30:39 +0200 Subject: [PATCH 09/72] test: check unused xprv descriptor pubkeys Co-authored-by: adyshimony <6388409+adyshimony@users.noreply.github.com> --- src/test/descriptor_tests.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/descriptor_tests.cpp b/src/test/descriptor_tests.cpp index 349695ac6bb8..cac63bc5b27c 100644 --- a/src/test/descriptor_tests.cpp +++ b/src/test/descriptor_tests.cpp @@ -1401,7 +1401,7 @@ void CheckUnused(const std::string& prv, const std::string& pub) // Check both only have one pubkey std::set prv_pubkeys; std::set prv_extpubs; - parse_pub->GetPubKeys(prv_pubkeys, prv_extpubs); + parse_priv->GetPubKeys(prv_pubkeys, prv_extpubs); BOOST_CHECK_EQUAL(prv_pubkeys.size() + prv_extpubs.size(), 1); std::set pub_pubkeys; std::set pub_extpubs; From 6d668f0ba22b8b0f97efcb5e9e3975219bbb03db Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Thu, 14 May 2026 13:30:44 +0200 Subject: [PATCH 10/72] wallet: reject duplicate addhdkey xprvs Co-authored-by: adyshimony <6388409+adyshimony@users.noreply.github.com> --- src/wallet/wallet.cpp | 7 +++++++ test/functional/wallet_hd.py | 2 ++ 2 files changed, 9 insertions(+) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 8e3079370f6e..8c1a45b454bc 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3669,6 +3669,13 @@ util::Expected CWallet::AddHDKey(const std::optional Date: Thu, 3 Jul 2025 10:29:58 +0200 Subject: [PATCH 11/72] rpc: make createwalletdescriptor smarter When a wallet contains only an unused(KEY) descriptor, use it. Previously the user would have to call listdescriptors and manually specify it. --- src/wallet/rpc/wallet.cpp | 19 ++++++++++++--- .../wallet_createwalletdescriptor.py | 23 ++++++++++++++++++- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/wallet/rpc/wallet.cpp b/src/wallet/rpc/wallet.cpp index 6c799a853613..ff733cc1b945 100644 --- a/src/wallet/rpc/wallet.cpp +++ b/src/wallet/rpc/wallet.cpp @@ -749,7 +749,7 @@ static RPCMethod createwalletdescriptor() {"type", RPCArg::Type::STR, RPCArg::Optional::NO, "The address type the descriptor will produce. Options are " + FormatAllOutputTypes() + "."}, {"options", RPCArg::Type::OBJ_NAMED_PARAMS, RPCArg::Optional::OMITTED, "", { {"internal", RPCArg::Type::BOOL, RPCArg::DefaultHint{"Both external and internal will be generated unless this parameter is specified"}, "Whether to only make one descriptor that is internal (if parameter is true) or external (if parameter is false)"}, - {"hdkey", RPCArg::Type::STR, RPCArg::DefaultHint{"The HD key used by all other active descriptors"}, "The HD key that the wallet knows the private key of, listed using 'gethdkeys', to use for this descriptor's key"}, + {"hdkey", RPCArg::Type::STR, RPCArg::DefaultHint{"The HD key used by all other active descriptors, or, if there are none, the HD key of an unused(KEY) descriptor."}, "The HD key that the wallet knows the private key of, listed using 'gethdkeys', to use for this descriptor's key"}, }}, }, RPCResult{ @@ -791,11 +791,24 @@ static RPCMethod createwalletdescriptor() CExtPubKey xpub; if (hdkey.isNull()) { + // First consider the HD key from active descriptors HDPubKeyMap active_xpubs = pwallet->GetHDPubKeys(HDKeyFilter::Active); - if (active_xpubs.size() != 1) { + if (active_xpubs.size() == 1) { + xpub = active_xpubs.begin()->first; + } else if (active_xpubs.size() > 1) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unable to determine which HD key to use from active descriptors. Please specify with 'hdkey'"); + } else { + // Look for an unused(KEY) descriptor + HDPubKeyMap wallet_xpubs{pwallet->GetHDPubKeys(HDKeyFilter::UnusedKey)}; + + if (wallet_xpubs.empty()) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No HD key found. Please generate one with 'addhdkey' or import an active descriptor."); + } else if (wallet_xpubs.size() > 1) { + throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Unable to determine which HD key to use. Please specify with 'hdkey'"); + } + + xpub = wallet_xpubs.begin()->first; } - xpub = active_xpubs.begin()->first; } else { xpub = DecodeExtPubKey(hdkey.get_str()); if (!xpub.pubkey.IsValid()) { diff --git a/test/functional/wallet_createwalletdescriptor.py b/test/functional/wallet_createwalletdescriptor.py index b1d411322812..73a75f3f2d3a 100755 --- a/test/functional/wallet_createwalletdescriptor.py +++ b/test/functional/wallet_createwalletdescriptor.py @@ -25,6 +25,7 @@ def run_test(self): self.test_basic() self.test_imported_other_keys() self.test_encrypted() + self.test_from_unused_desc() def test_basic(self): def_wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name) @@ -39,7 +40,7 @@ def test_basic(self): if desc["desc"].startswith("wpkh("): expected_descs.append(desc["desc"]) - assert_raises_rpc_error(-5, "Unable to determine which HD key to use from active descriptors. Please specify with 'hdkey'", wallet.createwalletdescriptor, "bech32") + assert_raises_rpc_error(-5, "No HD key found. Please generate one with 'addhdkey' or import an active descriptor.", wallet.createwalletdescriptor, "bech32") assert_raises_rpc_error(-5, f"Private key for {xpub} is not known", wallet.createwalletdescriptor, type="bech32", hdkey=xpub) self.log.info("Test createwalletdescriptor after importing active descriptor to blank wallet") @@ -114,6 +115,26 @@ def test_encrypted(self): with WalletUnlock(wallet, "pass"): wallet.createwalletdescriptor(type="bech32m") + def test_from_unused_desc(self): + self.log.info("Test createwalletdescriptor from only an unused(KEY) descriptor") + self.nodes[0].createwallet("w1", blank=True) + w1 = self.nodes[0].get_wallet_rpc("w1") + + # Wallet can't be completely empty + assert_raises_rpc_error(-5, "No HD key found. Please generate one with 'addhdkey' or import an active descriptor.", w1.createwalletdescriptor, "bech32") + + # Create unused(KEY) descriptor and try again + w1.addhdkey() + w1.createwalletdescriptor(type="bech32") + + self.nodes[0].createwallet("w2", blank=True) + w2 = self.nodes[0].get_wallet_rpc("w2") + + # Multiple unused(KEY) descriptors require user to choose + w2.addhdkey() + w2.addhdkey() + + assert_raises_rpc_error(-5, "Unable to determine which HD key to use. Please specify with 'hdkey'", w2.createwalletdescriptor, "bech32") if __name__ == '__main__': From 2a567f8b55505c54e94945b57167ffb70e8eeab9 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Tue, 25 Aug 2026 09:39:21 +0200 Subject: [PATCH 12/72] doc: use a blank wallet in the multisig tutorial The participant wallets are only used to derive an xpub and sign PSBTs for the multisig wallet. Creating them blank and adding just the legacy descriptor avoids handing the reader singlesig addresses they are not supposed to use. The legacy descriptor can't be dropped entirely because the signing code only considers descriptors whose derivation paths cover the input. To further discourage use of the singlesig descriptors, the PSBT example now pays a separate recipient wallet rather than participant_1. --- doc/multisig-tutorial.md | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/doc/multisig-tutorial.md b/doc/multisig-tutorial.md index fd74e62ed28f..0a9379d9caf7 100644 --- a/doc/multisig-tutorial.md +++ b/doc/multisig-tutorial.md @@ -20,15 +20,27 @@ This tutorial also uses the default PKH derivation path to get the xpubs and doe For a 2-of-3 multisig, create 3 wallets. These wallets contain HD seed and private keys, which will be used to sign the PSBTs and derive the xpub. -These three wallets should not be used directly for privacy reasons (public key reuse). They should only be used to sign transactions for the (watch-only) multisig wallet. +These three wallets should not be used directly for privacy reasons (public key reuse). They should only be used to sign transactions for the (watch-only) multisig wallet. To make that less likely to happen by accident, participant wallets only have singlesig legacy addresses, and no singlesig Bech32 addresses. + +`addhdkey` adds an HD key to the blank wallet, which `createwalletdescriptor` then uses: ```bash for ((n=1;n<=3;n++)) do - ./build/bin/bitcoin rpc -signet createwallet "participant_${n}" + ./build/bin/bitcoin rpc -signet -named createwallet wallet_name="participant_${n}" blank=true + ./build/bin/bitcoin rpc -signet -rpcwallet="participant_${n}" addhdkey + ./build/bin/bitcoin rpc -signet -rpcwallet="participant_${n}" createwalletdescriptor legacy done ``` +The `legacy` descriptor is only there to hold the private keys: it derives from `m/44h/1h/0h/<0;1>/*`, the same paths as the multisig descriptor defined below, and a wallet can only sign for a derivation path that one of its descriptors uses. + +A later step spends from the multisig wallet, so create one more wallet to receive that payment. This one is an ordinary singlesig wallet, standing in for whoever is being paid: + +```bash +./build/bin/bitcoin rpc -signet createwallet "recipient" +``` + Extract the xpub of each wallet. To do this, the `derivehdkey` RPC is used. Note that previously at least two descriptors were usually used, one for external derivation paths and one for internal ones. Since https://github.com/bitcoin/bitcoin/pull/22838 this redundancy has been eliminated by a multipath descriptor with <0;1> at the [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki#change) change level expanding to external and internal descriptors when imported. @@ -100,6 +112,7 @@ Once the wallets have already been created and this tutorial needs to be repeate ```bash for ((n=1;n<=3;n++)); do ./build/bin/bitcoin rpc -signet loadwallet "participant_${n}"; done ./build/bin/bitcoin rpc -signet loadwallet "multisig_wallet_01" +./build/bin/bitcoin rpc -signet loadwallet "recipient" ``` ### 1.4 Fund the wallet @@ -138,7 +151,7 @@ PSBT is a data format that allows wallets and other tools to exchange informatio The current PSBT version (v0) is defined in [BIP 174](https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki). -For simplicity, the destination address is taken from the `participant_1` wallet in the code above, but it can be any valid bitcoin address. +The destination address is taken from the `recipient` wallet. The `walletcreatefundedpsbt` RPC is used to create and fund a transaction in the PSBT format. It is the first step in creating the PSBT. @@ -147,7 +160,7 @@ balance=$(./build/bin/bitcoin rpc -signet -rpcwallet="multisig_wallet_01" getbal amount=$(echo "$balance * 0.8" | bc -l | sed -e 's/^\./0./' -e 's/^-\./-0./') -destination_addr=$(./build/bin/bitcoin rpc -signet -rpcwallet="participant_1" getnewaddress) +destination_addr=$(./build/bin/bitcoin rpc -signet -rpcwallet="recipient" getnewaddress "" bech32m) funded_psbt=$(./build/bin/bitcoin rpc -signet -rpcwallet="multisig_wallet_01" walletcreatefundedpsbt outputs="{\"$destination_addr\": $amount}" | jq -r '.psbt') ``` From bf0af8ae296afec325de542fd5d84a41bba028f6 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Tue, 25 Aug 2026 09:43:02 +0200 Subject: [PATCH 13/72] doc: add release note for #32861 --- doc/release-notes-32861.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 doc/release-notes-32861.md diff --git a/doc/release-notes-32861.md b/doc/release-notes-32861.md new file mode 100644 index 000000000000..9030ee57f9f9 --- /dev/null +++ b/doc/release-notes-32861.md @@ -0,0 +1,9 @@ +Updated RPCs +------------ + +- `createwalletdescriptor` no longer requires the `hdkey` argument when the + wallet has no active descriptors but does have exactly one HD key added with + `addhdkey`. Previously the user had to look the key up with `listdescriptors` + and pass it in. This makes it easier to set up a blank wallet that only has + the descriptors it actually needs, which `doc/multisig-tutorial.md` now + demonstrates. (#32861) From 48628a9bf6fbcdca2d2fc7dc91b667bacf99ca74 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Fri, 1 Aug 2025 09:06:54 +0000 Subject: [PATCH 14/72] wallet: don't import external keys at creation if blank There's no need to treat external signer wallets different in this regard. When the user sets the 'blank' flag, don't generate or import keys. For multisig setups that involve an external signer, it may be useful to start from a blank wallet and manually import descriptors. --- src/wallet/wallet.cpp | 8 ++++---- test/functional/wallet_signer.py | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index d6cc7ff72875..3a90cae6779e 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3536,10 +3536,10 @@ void CWallet::SetupDescriptorScriptPubKeyMans() void CWallet::SetupWalletGeneration() { AssertLockHeld(cs_wallet); - // Skip setup for non-external-signer wallets that are either blank - // or have private keys disabled (not having private keys implies blank). - if (!IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER) && - (IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET) || IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS))) { + // Skip setup for blank wallets. Non-external-signer wallets with disabled + // private keys also skip setup (not having private keys implies blank). + if (IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET) || + (!IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER) && IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS))) { return; } SetupDescriptorScriptPubKeyMans(); diff --git a/test/functional/wallet_signer.py b/test/functional/wallet_signer.py index 20a5a3f3fcb4..3890fc059850 100755 --- a/test/functional/wallet_signer.py +++ b/test/functional/wallet_signer.py @@ -73,6 +73,13 @@ def test_valid_signer(self): hww = self.nodes[1].get_wallet_rpc('hww') assert_equal(hww.getwalletinfo()["external_signer"], True) + # A blank external signer wallet does not auto-import any keys. + self.nodes[1].createwallet(wallet_name='hww_blank', disable_private_keys=True, external_signer=True, blank=True) + hww_blank = self.nodes[1].get_wallet_rpc('hww_blank') + assert_equal(hww_blank.getwalletinfo()["keypoolsize"], 0) + assert_equal(hww_blank.listdescriptors()["descriptors"], []) + self.nodes[1].unloadwallet('hww_blank') + # Flag can't be set afterwards (could be added later for non-blank descriptor based watch-only wallets) self.nodes[1].createwallet(wallet_name='not_hww', disable_private_keys=True, external_signer=False) not_hww = self.nodes[1].get_wallet_rpc('not_hww') From 762a32ef46b33b9ac7adcefe676dbcad67d6e96f Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Fri, 1 Aug 2025 12:51:53 +0000 Subject: [PATCH 15/72] wallet: avoid signing via createTransaction() with external signer External signer enabled wallets should always use the process PSBT flow. Avoid going through CreateTransaction. This has no effect until a later commit where WALLET_FLAG_EXTERNAL_SIGNER no longer implies WALLET_FLAG_DISABLE_PRIVATE_KEYS. Without this change signing with the GUI would break for external signers with private keys enabled. --- src/qt/walletmodel.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/qt/walletmodel.cpp b/src/qt/walletmodel.cpp index 2422b8857975..7fd2c5028a34 100644 --- a/src/qt/walletmodel.cpp +++ b/src/qt/walletmodel.cpp @@ -203,7 +203,7 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact try { auto& newTx = transaction.getWtx(); - const auto& res = m_wallet->createTransaction(vecSend, coinControl, /*sign=*/!wallet().privateKeysDisabled(), /*change_pos=*/std::nullopt); + const auto& res = m_wallet->createTransaction(vecSend, coinControl, /*sign=*/!wallet().privateKeysDisabled() && !wallet().hasExternalSigner(), /*change_pos=*/std::nullopt); if (!res) { Q_EMIT message(tr("Send Coins"), QString::fromStdString(util::ErrorString(res).translated), CClientUIInterface::MSG_ERROR); From 91c0e4445d158a74d79967452db297ef656b3b20 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Fri, 1 Aug 2025 08:45:49 +0000 Subject: [PATCH 16/72] wallet: make watch-only optional for external signer Before this change the external_signer flag required the wallet to be watch-only. This precludes multisig setups in which we hold a hot key. Remove this as a requirement, but disable private keys by default. This leaves the typical (and only documented) use case of a single external signer unaffected. --- src/qt/createwalletdialog.cpp | 28 ++++++++-------------------- src/wallet/rpc/wallet.cpp | 19 ++++++++++++++----- src/wallet/wallet.cpp | 7 ------- test/functional/wallet_signer.py | 29 +++++++++++++++++++---------- 4 files changed, 41 insertions(+), 42 deletions(-) diff --git a/src/qt/createwalletdialog.cpp b/src/qt/createwalletdialog.cpp index 59c6f51a27ec..b73c9728240e 100644 --- a/src/qt/createwalletdialog.cpp +++ b/src/qt/createwalletdialog.cpp @@ -26,34 +26,25 @@ CreateWalletDialog::CreateWalletDialog(QWidget* parent) : }); connect(ui->encrypt_wallet_checkbox, &QCheckBox::toggled, [this](bool checked) { - // Disable the disable_privkeys_checkbox and external_signer_checkbox when isEncryptWalletChecked is + // Disable the disable_privkeys_checkbox when isEncryptWalletChecked is // set to true, enable it when isEncryptWalletChecked is false. ui->disable_privkeys_checkbox->setEnabled(!checked); -#ifdef ENABLE_EXTERNAL_SIGNER - ui->external_signer_checkbox->setEnabled(m_has_signers && !checked); -#endif + // When the disable_privkeys_checkbox is disabled, uncheck it. if (!ui->disable_privkeys_checkbox->isEnabled()) { ui->disable_privkeys_checkbox->setChecked(false); } - - // When the external_signer_checkbox box is disabled, uncheck it. - if (!ui->external_signer_checkbox->isEnabled()) { - ui->external_signer_checkbox->setChecked(false); - } - }); connect(ui->external_signer_checkbox, &QCheckBox::toggled, [this](bool checked) { - ui->encrypt_wallet_checkbox->setEnabled(!checked); - ui->blank_wallet_checkbox->setEnabled(!checked); - ui->disable_privkeys_checkbox->setEnabled(!checked); + // In the basic use case all keys will be on the external signer + // device and the wallet should be watch-only. Makes this the + // default suggestion. + ui->disable_privkeys_checkbox->setChecked(checked); - // The external signer checkbox is only enabled when a device is detected. - // In that case it is checked by default. Toggling it restores the other - // options to their default. + // The external signer box is checked by default when a device is + // detected. Toggling it restores the other options to their default. ui->encrypt_wallet_checkbox->setChecked(false); - ui->disable_privkeys_checkbox->setChecked(checked); ui->blank_wallet_checkbox->setChecked(false); }); @@ -103,12 +94,9 @@ void CreateWalletDialog::setSigners(const std::vectorexternal_signer_checkbox->setEnabled(true); ui->external_signer_checkbox->setChecked(true); - ui->encrypt_wallet_checkbox->setEnabled(false); ui->encrypt_wallet_checkbox->setChecked(false); // The order matters, because connect() is called when toggling a checkbox: - ui->blank_wallet_checkbox->setEnabled(false); ui->blank_wallet_checkbox->setChecked(false); - ui->disable_privkeys_checkbox->setEnabled(false); ui->disable_privkeys_checkbox->setChecked(true); const std::string label = signers[0]->getName(); ui->wallet_name_line_edit->setText(QString::fromStdString(label)); diff --git a/src/wallet/rpc/wallet.cpp b/src/wallet/rpc/wallet.cpp index 3983776b5a71..af5a43514655 100644 --- a/src/wallet/rpc/wallet.cpp +++ b/src/wallet/rpc/wallet.cpp @@ -360,13 +360,13 @@ static RPCMethod createwallet() "Creates and loads a new wallet.\n", { {"wallet_name", RPCArg::Type::STR, RPCArg::Optional::NO, "The name for the new wallet. If this is a path, the wallet will be created at the path location."}, - {"disable_private_keys", RPCArg::Type::BOOL, RPCArg::Default{false}, "Disable the possibility of private keys (only watchonlys are possible in this mode)."}, + {"disable_private_keys", RPCArg::Type::BOOL, RPCArg::DefaultHint{"false unless external_signer is set"}, "Disable the possibility of private keys (only watchonlys are possible in this mode)."}, {"blank", RPCArg::Type::BOOL, RPCArg::Default{false}, "Create a blank wallet. A blank wallet has no keys."}, {"passphrase", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "Encrypt the wallet with this passphrase."}, {"avoid_reuse", RPCArg::Type::BOOL, RPCArg::Default{false}, "Keep track of coin reuse, and treat dirty and clean coins differently with privacy considerations in mind."}, {"descriptors", RPCArg::Type::BOOL, RPCArg::Default{true}, "If set, must be \"true\""}, {"load_on_startup", RPCArg::Type::BOOL, RPCArg::Optional::OMITTED, "Save wallet name to persistent settings and load on startup. True to add wallet to startup list, false to remove, null to leave unchanged."}, - {"external_signer", RPCArg::Type::BOOL, RPCArg::Default{false}, "Use an external signer such as a hardware wallet. Requires -signer to be configured. Wallet creation will fail if keys cannot be fetched. Requires disable_private_keys and descriptors set to true."}, + {"external_signer", RPCArg::Type::BOOL, RPCArg::Default{false}, "Use an external signer such as a hardware wallet. Requires -signer to be configured. Wallet creation will fail if keys cannot be fetched."}, }, RPCResult{ RPCResult::Type::OBJ, "", "", @@ -388,9 +388,8 @@ static RPCMethod createwallet() { WalletContext& context = EnsureWalletContext(request.context); uint64_t flags = 0; - if (self.Arg("disable_private_keys")) { - flags |= WALLET_FLAG_DISABLE_PRIVATE_KEYS; - } + + std::optional disable_private_keys{self.MaybeArg("disable_private_keys")}; if (self.Arg("blank")) { flags |= WALLET_FLAG_BLANK_WALLET; @@ -416,11 +415,21 @@ static RPCMethod createwallet() if (self.Arg("external_signer")) { #ifdef ENABLE_EXTERNAL_SIGNER flags |= WALLET_FLAG_EXTERNAL_SIGNER; + if (!disable_private_keys.has_value()) { + // In the basic use case all keys will be on the external signer + // device and the wallet should be watch-only. Makes this the + // default. + disable_private_keys = true; + } #else throw JSONRPCError(RPC_WALLET_ERROR, "Compiled without external signing support (required for external signing)"); #endif } + if (disable_private_keys.value_or(false)) { + flags |= WALLET_FLAG_DISABLE_PRIVATE_KEYS; + } + DatabaseOptions options; DatabaseStatus status; ReadDatabaseArgs(*context.args, options); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 3a90cae6779e..02b03ff7f764 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -349,13 +349,6 @@ std::shared_ptr CreateWallet(WalletContext& context, const std::string& options.require_format = DatabaseFormat::SQLITE; - // Private keys must be disabled for an external signer wallet - if ((wallet_creation_flags & WALLET_FLAG_EXTERNAL_SIGNER) && !(wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { - error = Untranslated("Private keys must be disabled when using an external signer"); - status = DatabaseStatus::FAILED_CREATE; - return nullptr; - } - // Do not allow a passphrase when private keys are disabled if (born_encrypted && (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { error = Untranslated("Passphrase provided but private keys are disabled. A passphrase is only used to encrypt private keys, so cannot be used for wallets with private keys disabled."); diff --git a/test/functional/wallet_signer.py b/test/functional/wallet_signer.py index 3890fc059850..23d6f1893806 100755 --- a/test/functional/wallet_signer.py +++ b/test/functional/wallet_signer.py @@ -67,29 +67,38 @@ def test_valid_signer(self): self.log.debug(f"-signer={self.mock_signer_path()}") # Create new wallets for an external signer. - # disable_private_keys and descriptors must be true: - assert_raises_rpc_error(-4, "Private keys must be disabled when using an external signer", self.nodes[1].createwallet, wallet_name='not_hww', disable_private_keys=False, external_signer=True) - self.nodes[1].createwallet(wallet_name='hww', disable_private_keys=True, external_signer=True) + self.nodes[1].createwallet(wallet_name='hww', external_signer=True) hww = self.nodes[1].get_wallet_rpc('hww') assert_equal(hww.getwalletinfo()["external_signer"], True) + # Private keys are disabled by default + assert_equal(hww.getwalletinfo()["private_keys_enabled"], False) + + # Private keys can be explicitly enabled for external signer wallets + self.nodes[1].createwallet(wallet_name='hww_hot', external_signer=True, disable_private_keys=False) + hww_hot = self.nodes[1].get_wallet_rpc('hww_hot') + assert_equal(hww_hot.getwalletinfo()["external_signer"], True) + assert_equal(hww_hot.getwalletinfo()["private_keys_enabled"], True) + self.nodes[1].unloadwallet('hww_hot') + # A blank external signer wallet does not auto-import any keys. - self.nodes[1].createwallet(wallet_name='hww_blank', disable_private_keys=True, external_signer=True, blank=True) + self.nodes[1].createwallet(wallet_name='hww_blank', external_signer=True, blank=True) hww_blank = self.nodes[1].get_wallet_rpc('hww_blank') assert_equal(hww_blank.getwalletinfo()["keypoolsize"], 0) assert_equal(hww_blank.listdescriptors()["descriptors"], []) self.nodes[1].unloadwallet('hww_blank') # Flag can't be set afterwards (could be added later for non-blank descriptor based watch-only wallets) - self.nodes[1].createwallet(wallet_name='not_hww', disable_private_keys=True, external_signer=False) + self.nodes[1].createwallet(wallet_name='not_hww', external_signer=False) not_hww = self.nodes[1].get_wallet_rpc('not_hww') assert_equal(not_hww.getwalletinfo()["external_signer"], False) + # Without external_signer, private keys are enabled by default + assert_equal(not_hww.getwalletinfo()["private_keys_enabled"], True) assert_raises_rpc_error(-8, "Wallet flag is immutable: external_signer", not_hww.setwalletflag, "external_signer", True) - self.set_mock_result(self.nodes[1], '0 {"invalid json"}') assert_raises_rpc_error(-1, 'Unable to parse JSON', - self.nodes[1].createwallet, wallet_name='hww2', disable_private_keys=True, external_signer=True + self.nodes[1].createwallet, wallet_name='hww2', external_signer=True ) self.clear_mock_result(self.nodes[1]) @@ -233,7 +242,7 @@ def test_disconnected_signer(self): self.log.info('Test disconnected external signer') # First create a wallet with the signer connected - self.nodes[1].createwallet(wallet_name='hww_disconnect', disable_private_keys=True, external_signer=True) + self.nodes[1].createwallet(wallet_name='hww_disconnect', external_signer=True) hww = self.nodes[1].get_wallet_rpc('hww_disconnect') assert_equal(hww.getwalletinfo()["external_signer"], True) @@ -254,13 +263,13 @@ def test_disconnected_signer(self): def test_invalid_signer(self): self.log.debug(f"-signer={self.mock_invalid_signer_path()}") self.log.info('Test invalid external signer') - assert_raises_rpc_error(-1, "Invalid descriptor", self.nodes[1].createwallet, wallet_name='hww_invalid', disable_private_keys=True, external_signer=True) + assert_raises_rpc_error(-1, "Invalid descriptor", self.nodes[1].createwallet, wallet_name='hww_invalid', external_signer=True) def test_multiple_signers(self): self.log.debug(f"-signer={self.mock_multi_signers_path()}") self.log.info('Test multiple external signers') - assert_raises_rpc_error(-1, "More than one external signer found", self.nodes[1].createwallet, wallet_name='multi_hww', disable_private_keys=True, external_signer=True) + assert_raises_rpc_error(-1, "More than one external signer found", self.nodes[1].createwallet, wallet_name='multi_hww', external_signer=True) if __name__ == '__main__': WalletSignerTest(__file__).main() From fd0f9f1feb8ab7a5d992960d9423bc611cbb922e Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Fri, 1 Aug 2025 08:46:50 +0000 Subject: [PATCH 17/72] wallet: make external_signer flag mutable With the removal of legacy wallets and the relaxing of restrictions in the previous commit, it's no longer a problem to toggle this flag. --- src/wallet/rpc/wallet.cpp | 6 ++++++ src/wallet/wallet.h | 3 ++- test/functional/wallet_avoidreuse.py | 3 +++ test/functional/wallet_signer.py | 13 +++++++------ 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/wallet/rpc/wallet.cpp b/src/wallet/rpc/wallet.cpp index af5a43514655..a53c1251c25a 100644 --- a/src/wallet/rpc/wallet.cpp +++ b/src/wallet/rpc/wallet.cpp @@ -329,6 +329,12 @@ static RPCMethod setwalletflag() throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Wallet flag is immutable: %s", flag_str)); } +#ifndef ENABLE_EXTERNAL_SIGNER + if (flag == WALLET_FLAG_EXTERNAL_SIGNER && value) { + throw JSONRPCError(RPC_WALLET_ERROR, "Compiled without external signing support (required for external signing)"); + } +#endif + UniValue res(UniValue::VOBJ); if (pwallet->IsWalletFlagSet(flag) == value) { diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 350d7e4db109..18618a0ea219 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -165,7 +165,8 @@ inline constexpr uint64_t KNOWN_WALLET_FLAGS = | WALLET_FLAG_EXTERNAL_SIGNER; inline constexpr uint64_t MUTABLE_WALLET_FLAGS = - WALLET_FLAG_AVOID_REUSE; + WALLET_FLAG_AVOID_REUSE + | WALLET_FLAG_EXTERNAL_SIGNER; static const std::map WALLET_FLAG_TO_STRING{ {WALLET_FLAG_AVOID_REUSE, "avoid_reuse"}, diff --git a/test/functional/wallet_avoidreuse.py b/test/functional/wallet_avoidreuse.py index 3e00d14c69ac..4f5cd3b10e86 100755 --- a/test/functional/wallet_avoidreuse.py +++ b/test/functional/wallet_avoidreuse.py @@ -138,6 +138,9 @@ def test_immutable(self): # Attempt to set the disable_private_keys flag; this should not work assert_raises_rpc_error(-8, "Wallet flag is immutable", self.nodes[1].setwalletflag, 'disable_private_keys') + if not self.is_external_signer_compiled(): + assert_raises_rpc_error(-4, "Compiled without external signing support", self.nodes[1].setwalletflag, "external_signer") + tempwallet = ".wallet_avoidreuse.py_test_immutable_wallet.dat" # Create a wallet with disable_private_keys set; this should work diff --git a/test/functional/wallet_signer.py b/test/functional/wallet_signer.py index 23d6f1893806..1a1232719fe9 100755 --- a/test/functional/wallet_signer.py +++ b/test/functional/wallet_signer.py @@ -88,13 +88,14 @@ def test_valid_signer(self): assert_equal(hww_blank.listdescriptors()["descriptors"], []) self.nodes[1].unloadwallet('hww_blank') - # Flag can't be set afterwards (could be added later for non-blank descriptor based watch-only wallets) - self.nodes[1].createwallet(wallet_name='not_hww', external_signer=False) - not_hww = self.nodes[1].get_wallet_rpc('not_hww') - assert_equal(not_hww.getwalletinfo()["external_signer"], False) + # Flag can be set afterwards + self.nodes[1].createwallet(wallet_name='not_hww_initially', external_signer=False) + not_hww_initially = self.nodes[1].get_wallet_rpc('not_hww_initially') + assert_equal(not_hww_initially.getwalletinfo()["external_signer"], False) # Without external_signer, private keys are enabled by default - assert_equal(not_hww.getwalletinfo()["private_keys_enabled"], True) - assert_raises_rpc_error(-8, "Wallet flag is immutable: external_signer", not_hww.setwalletflag, "external_signer", True) + assert_equal(not_hww_initially.getwalletinfo()["private_keys_enabled"], True) + not_hww_initially.setwalletflag("external_signer", True) + assert_equal(not_hww_initially.getwalletinfo()["external_signer"], True) self.set_mock_result(self.nodes[1], '0 {"invalid json"}') assert_raises_rpc_error(-1, 'Unable to parse JSON', From e66095d29fc08cf4e362cf157efc86b1eaa05f78 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Wed, 26 Aug 2026 08:27:50 +0000 Subject: [PATCH 18/72] wallet: extract load and unload wallet RPC helpers A later commit reuses these helpers to reload a wallet. This does not change behavior. --- src/wallet/rpc/wallet.cpp | 92 ++++++++++++++++++++++++--------------- 1 file changed, 56 insertions(+), 36 deletions(-) diff --git a/src/wallet/rpc/wallet.cpp b/src/wallet/rpc/wallet.cpp index a53c1251c25a..bae9da9c445b 100644 --- a/src/wallet/rpc/wallet.cpp +++ b/src/wallet/rpc/wallet.cpp @@ -25,6 +25,7 @@ #include #include +#include #include #include @@ -220,6 +221,53 @@ static RPCMethod listwallets() }; } +struct WalletLoadResult { + std::shared_ptr wallet; + std::vector warnings; +}; + +static WalletLoadResult LoadWalletForRPC(WalletContext& context, const std::string& name, std::optional load_on_start) +{ + DatabaseOptions options; + DatabaseStatus status; + ReadDatabaseArgs(*context.args, options); + options.require_existing = true; + bilingual_str error; + std::vector warnings; + + { + LOCK(context.wallets_mutex); + if (std::any_of(context.wallets.begin(), context.wallets.end(), [&name](const auto& wallet) { return wallet->GetName() == name; })) { + throw JSONRPCError(RPC_WALLET_ALREADY_LOADED, "Wallet \"" + name + "\" is already loaded."); + } + } + + std::shared_ptr wallet{LoadWallet(context, name, load_on_start, options, status, error, warnings)}; + HandleWalletError(wallet, status, error); + return {std::move(wallet), std::move(warnings)}; +} + +static std::vector UnloadWallet(WalletContext& context, std::shared_ptr wallet, std::optional load_on_start, std::unique_ptr reserver = nullptr) +{ + if (!reserver) { + reserver = std::make_unique(*wallet); + if (!reserver->reserve()) { + throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait."); + } + } else { + CHECK_NONFATAL(reserver->isReserved()); + } + + std::vector warnings; + if (!RemoveWallet(context, wallet, load_on_start, warnings)) { + throw JSONRPCError(RPC_MISC_ERROR, "Requested wallet already unloaded"); + } + + reserver.reset(); + WaitForDeleteWallet(std::move(wallet)); + return warnings; +} + static RPCMethod loadwallet() { return RPCMethod{ @@ -257,28 +305,12 @@ static RPCMethod loadwallet() WalletContext& context = EnsureWalletContext(request.context); const std::string name(request.params[0].get_str()); - DatabaseOptions options; - DatabaseStatus status; - ReadDatabaseArgs(*context.args, options); - options.require_existing = true; - bilingual_str error; - std::vector warnings; std::optional load_on_start = request.params[1].isNull() ? std::nullopt : std::optional(request.params[1].get_bool()); - - { - LOCK(context.wallets_mutex); - if (std::any_of(context.wallets.begin(), context.wallets.end(), [&name](const auto& wallet) { return wallet->GetName() == name; })) { - throw JSONRPCError(RPC_WALLET_ALREADY_LOADED, "Wallet \"" + name + "\" is already loaded."); - } - } - - std::shared_ptr const wallet = LoadWallet(context, name, load_on_start, options, status, error, warnings); - - HandleWalletError(wallet, status, error); + WalletLoadResult load_result{LoadWalletForRPC(context, name, load_on_start)}; UniValue obj(UniValue::VOBJ); - obj.pushKV("name", wallet->GetName()); - PushWarnings(warnings, obj); + obj.pushKV("name", load_result.wallet->GetName()); + PushWarnings(load_result.warnings, obj); return obj; }, @@ -485,23 +517,11 @@ static RPCMethod unloadwallet() throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded"); } - std::vector warnings; - { - WalletRescanReserver reserver(*wallet); - if (!reserver.reserve()) { - throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait."); - } - - // Release the "main" shared pointer and prevent further notifications. - // Note that any attempt to load the same wallet would fail until the wallet - // is destroyed (see CheckUniqueFileid). - std::optional load_on_start{self.MaybeArg("load_on_startup")}; - if (!RemoveWallet(context, wallet, load_on_start, warnings)) { - throw JSONRPCError(RPC_MISC_ERROR, "Requested wallet already unloaded"); - } - } - - WaitForDeleteWallet(std::move(wallet)); + // Release the "main" shared pointer and prevent further notifications. + // Note that any attempt to load the same wallet would fail until the wallet + // is destroyed (see CheckUniqueFileid). + std::optional load_on_start{self.MaybeArg("load_on_startup")}; + std::vector warnings{UnloadWallet(context, std::move(wallet), load_on_start)}; UniValue result(UniValue::VOBJ); PushWarnings(warnings, result); From aa632dd0cfec161ba11a30783789df15525368e8 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Wed, 26 Aug 2026 08:47:33 +0000 Subject: [PATCH 19/72] wallet: report whether flag changes require reload A later commit uses this signal to reload the wallet after changing flags that affect in-memory state. Existing callers ignore the return value, so this does not change behavior. --- src/wallet/wallet.cpp | 14 +++++++++----- src/wallet/wallet.h | 15 +++++++++------ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 02b03ff7f764..22922abf1600 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -1742,32 +1742,36 @@ bool CWallet::CanGetAddresses(bool internal) const return false; } -void CWallet::SetWalletFlag(uint64_t flags) +bool CWallet::SetWalletFlag(uint64_t flags) { WalletBatch batch(GetDatabase()); return SetWalletFlagWithDB(batch, flags); } -void CWallet::SetWalletFlagWithDB(WalletBatch& batch, uint64_t flags) +bool CWallet::SetWalletFlagWithDB(WalletBatch& batch, uint64_t flags) { LOCK(cs_wallet); + const uint64_t flags_before{m_wallet_flags}; m_wallet_flags |= flags; if (!batch.WriteWalletFlags(m_wallet_flags)) throw std::runtime_error(std::string(__func__) + ": writing wallet flags failed"); + return ((flags_before ^ m_wallet_flags) & WALLET_FLAGS_REQUIRING_RELOAD) != 0; } -void CWallet::UnsetWalletFlag(uint64_t flag) +bool CWallet::UnsetWalletFlag(uint64_t flag) { WalletBatch batch(GetDatabase()); - UnsetWalletFlagWithDB(batch, flag); + return UnsetWalletFlagWithDB(batch, flag); } -void CWallet::UnsetWalletFlagWithDB(WalletBatch& batch, uint64_t flag) +bool CWallet::UnsetWalletFlagWithDB(WalletBatch& batch, uint64_t flag) { LOCK(cs_wallet); + const uint64_t flags_before{m_wallet_flags}; m_wallet_flags &= ~flag; if (!batch.WriteWalletFlags(m_wallet_flags)) throw std::runtime_error(std::string(__func__) + ": writing wallet flags failed"); + return ((flags_before ^ m_wallet_flags) & WALLET_FLAGS_REQUIRING_RELOAD) != 0; } void CWallet::UnsetBlankWalletFlag(WalletBatch& batch) diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 18618a0ea219..1d775d6013b4 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -168,6 +168,9 @@ inline constexpr uint64_t MUTABLE_WALLET_FLAGS = WALLET_FLAG_AVOID_REUSE | WALLET_FLAG_EXTERNAL_SIGNER; +inline constexpr uint64_t WALLET_FLAGS_REQUIRING_RELOAD = + WALLET_FLAG_EXTERNAL_SIGNER; + static const std::map WALLET_FLAG_TO_STRING{ {WALLET_FLAG_AVOID_REUSE, "avoid_reuse"}, {WALLET_FLAG_BLANK_WALLET, "blank"}, @@ -374,7 +377,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati bool SetAddressBookWithDB(WalletBatch& batch, const CTxDestination& address, const std::string& strName, const std::optional& strPurpose); //! Unsets a wallet flag and saves it to disk - void UnsetWalletFlagWithDB(WalletBatch& batch, uint64_t flag); + bool UnsetWalletFlagWithDB(WalletBatch& batch, uint64_t flag); //! Unset the blank wallet flag and saves it to disk void UnsetBlankWalletFlag(WalletBatch& batch) override; @@ -421,7 +424,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati void AddActiveScriptPubKeyManWithDb(WalletBatch& batch, uint256 id, OutputType type, bool internal); /** Store wallet flags */ - void SetWalletFlagWithDB(WalletBatch& batch, uint64_t flags); + bool SetWalletFlagWithDB(WalletBatch& batch, uint64_t flags); //! Cache of descriptor ScriptPubKeys used for IsMine. Maps ScriptPubKey to set of spkms std::unordered_map, SaltedSipHasher> m_cached_spks; @@ -890,11 +893,11 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati */ void BlockUntilSyncedToCurrentChain() const LOCKS_EXCLUDED(::cs_main) EXCLUSIVE_LOCKS_REQUIRED(!cs_wallet); - /** set a single wallet flag */ - void SetWalletFlag(uint64_t flags); + /** Set wallet flags. Returns whether the wallet needs to be reloaded. */ + bool SetWalletFlag(uint64_t flags); - /** Unsets a single wallet flag */ - void UnsetWalletFlag(uint64_t flag); + /** Unset wallet flags. Returns whether the wallet needs to be reloaded. */ + bool UnsetWalletFlag(uint64_t flag); /** check if a certain wallet flag is set */ bool IsWalletFlagSet(uint64_t flag) const override; From 9e441bb2cf54f7792031588f38da1e1a8318ef3f Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Wed, 26 Aug 2026 08:47:45 +0000 Subject: [PATCH 20/72] wallet: reload wallet when external signer flag changes Have setwalletflag unload and reload the wallet when a flag setter reports this is needed. Reuse the normal wallet loading path to recreate descriptor ScriptPubKeyMans. Document which flags trigger a reload and warn users to avoid concurrent wallet RPC clients while changing them. --- src/wallet/rpc/wallet.cpp | 44 ++++++++++++++++++++++++++++---- test/functional/wallet_signer.py | 4 +++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/wallet/rpc/wallet.cpp b/src/wallet/rpc/wallet.cpp index bae9da9c445b..4eb4db5f5373 100644 --- a/src/wallet/rpc/wallet.cpp +++ b/src/wallet/rpc/wallet.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -268,6 +269,15 @@ static std::vector UnloadWallet(WalletContext& context, std::shar return warnings; } +static std::vector ReloadWallet(WalletContext& context, std::shared_ptr wallet, std::unique_ptr reserver) +{ + const std::string wallet_name{wallet->GetName()}; + std::vector warnings{UnloadWallet(context, std::move(wallet), /*load_on_start=*/std::nullopt, std::move(reserver))}; + WalletLoadResult load_result{LoadWalletForRPC(context, wallet_name, /*load_on_start=*/std::nullopt)}; + warnings.insert(warnings.end(), load_result.warnings.begin(), load_result.warnings.end()); + return warnings; +} + static RPCMethod loadwallet() { return RPCMethod{ @@ -324,9 +334,16 @@ static RPCMethod setwalletflag() if (it.second & MUTABLE_WALLET_FLAGS) flags += (flags == "" ? "" : ", ") + it.first; + std::string reload_flags; + for (auto& it : STRING_TO_WALLET_FLAG) + if (it.second & WALLET_FLAGS_REQUIRING_RELOAD) + reload_flags += (reload_flags == "" ? "" : ", ") + it.first; + return RPCMethod{ "setwalletflag", - "Change the state of the given wallet flag for a wallet.\n", + "Change the state of the given wallet flag for a wallet.\n" + "The following flags trigger a wallet reload: " + reload_flags + ".\n" + "Make sure no other RPC clients are using the wallet when changing these flags.\n", { {"flag", RPCArg::Type::STR, RPCArg::Optional::NO, "The name of the flag to change. Current available flags: " + flags}, {"value", RPCArg::Type::BOOL, RPCArg::Default{true}, "The new state."}, @@ -345,7 +362,7 @@ static RPCMethod setwalletflag() }, [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue { - std::shared_ptr const pwallet = GetWalletForJSONRPCRequest(request); + std::shared_ptr pwallet = GetWalletForJSONRPCRequest(request); if (!pwallet) return UniValue::VNULL; std::string flag_str = request.params[0].get_str(); @@ -373,18 +390,35 @@ static RPCMethod setwalletflag() throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Wallet flag is already set to %s: %s", value ? "true" : "false", flag_str)); } + std::unique_ptr rescan_reserver; + if (flag & WALLET_FLAGS_REQUIRING_RELOAD) { + rescan_reserver = std::make_unique(*pwallet); + if (!rescan_reserver->reserve()) { + throw JSONRPCError(RPC_WALLET_ERROR, "Wallet is currently rescanning. Abort existing rescan or wait."); + } + } + res.pushKV("flag_name", flag_str); res.pushKV("flag_state", value); + bool reload_wallet; if (value) { - pwallet->SetWalletFlag(flag); + reload_wallet = pwallet->SetWalletFlag(flag); } else { - pwallet->UnsetWalletFlag(flag); + reload_wallet = pwallet->UnsetWalletFlag(flag); } + std::vector warnings; if (flag && value && WALLET_FLAG_CAVEATS.contains(flag)) { - res.pushKV("warnings", WALLET_FLAG_CAVEATS.at(flag)); + warnings.push_back(WALLET_FLAG_CAVEATS.at(flag)); + } + if (reload_wallet) { + WalletContext& context{EnsureWalletContext(request.context)}; + for (const bilingual_str& reload_warning : ReloadWallet(context, std::move(pwallet), std::move(rescan_reserver))) { + warnings.push_back(reload_warning.original); + } } + if (!warnings.empty()) res.pushKV("warnings", util::Join(warnings, "\n")); return res; }, diff --git a/test/functional/wallet_signer.py b/test/functional/wallet_signer.py index 1a1232719fe9..2db28ee4b6bd 100755 --- a/test/functional/wallet_signer.py +++ b/test/functional/wallet_signer.py @@ -133,6 +133,10 @@ def test_valid_signer(self): assert_equal(address_info['ismine'], True) assert_equal(address_info['hdkeypath'], "m/86h/1h/0h/0/0") + hww.setwalletflag("external_signer", False) + assert_raises_rpc_error(-1, "There is no ScriptPubKeyManager for this address", hww.walletdisplayaddress, address1) + hww.setwalletflag("external_signer", True) + self.log.info('Test walletdisplayaddress') for address in [address1, address2, address3]: result = hww.walletdisplayaddress(address) From 398b1f27ffbc73314fa34afcc90901503cf19f3b Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Wed, 6 May 2026 09:03:37 +0000 Subject: [PATCH 21/72] test: move mock signer path helper to the test framework Both rpc_signer.py and wallet_signer.py defined identical mock_signer_path() helpers; the upcoming wallet_signer_musig2.py test needs the same helper. Move it to BitcoinTestFramework. --- test/functional/rpc_signer.py | 5 ---- .../test_framework/test_framework.py | 5 ++++ test/functional/wallet_signer.py | 29 ++++--------------- 3 files changed, 11 insertions(+), 28 deletions(-) diff --git a/test/functional/rpc_signer.py b/test/functional/rpc_signer.py index 6d51182c6bf2..fbec9597f183 100755 --- a/test/functional/rpc_signer.py +++ b/test/functional/rpc_signer.py @@ -9,7 +9,6 @@ """ import os import platform -import sys from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( @@ -19,10 +18,6 @@ class RPCSignerTest(BitcoinTestFramework): - def mock_signer_path(self): - path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'mocks', 'signer.py') - return sys.executable + " " + path - def set_test_params(self): self.num_nodes = 4 diff --git a/test/functional/test_framework/test_framework.py b/test/functional/test_framework/test_framework.py index 3cf901e27be1..54c148b52e9e 100755 --- a/test/functional/test_framework/test_framework.py +++ b/test/functional/test_framework/test_framework.py @@ -1130,6 +1130,11 @@ def skip_if_no_external_signer(self): if not self.is_external_signer_compiled(): raise SkipTest("external signer support has not been compiled.") + def mock_signer_path(self, name='signer.py'): + """Return a command that invokes a mock external signer script under test/functional/mocks/.""" + path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'mocks', name) + return sys.executable + " " + os.path.realpath(path) + def skip_if_running_under_valgrind(self): """Skip the running test if Valgrind is being used.""" if self.options.valgrind: diff --git a/test/functional/wallet_signer.py b/test/functional/wallet_signer.py index 2db28ee4b6bd..0fe384925a70 100755 --- a/test/functional/wallet_signer.py +++ b/test/functional/wallet_signer.py @@ -8,7 +8,6 @@ See also rpc_signer.py for tests without wallet context. """ import os -import sys from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( @@ -19,22 +18,6 @@ class WalletSignerTest(BitcoinTestFramework): - def mock_signer_path(self): - path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'mocks', 'signer.py') - return sys.executable + " " + path - - def mock_no_connected_signer_path(self): - path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'mocks', 'no_signer.py') - return sys.executable + " " + path - - def mock_invalid_signer_path(self): - path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'mocks', 'invalid_signer.py') - return sys.executable + " " + path - - def mock_multi_signers_path(self): - path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'mocks', 'multi_signers.py') - return sys.executable + " " + path - def set_test_params(self): self.num_nodes = 2 self.noban_tx_relay = True # faster tx relay @@ -58,9 +41,9 @@ def clear_mock_result(self, node): def run_test(self): self.test_valid_signer() self.test_disconnected_signer() - self.restart_node(1, [f"-signer={self.mock_invalid_signer_path()}", "-keypool=10"]) + self.restart_node(1, [f"-signer={self.mock_signer_path('invalid_signer.py')}", "-keypool=10"]) self.test_invalid_signer() - self.restart_node(1, [f"-signer={self.mock_multi_signers_path()}", "-keypool=10"]) + self.restart_node(1, [f"-signer={self.mock_signer_path('multi_signers.py')}", "-keypool=10"]) self.test_multiple_signers() def test_valid_signer(self): @@ -256,8 +239,8 @@ def test_disconnected_signer(self): self.generate(self.nodes[0], 1) # Restart node with no signer connected - self.log.debug(f"-signer={self.mock_no_connected_signer_path()}") - self.restart_node(1, [f"-signer={self.mock_no_connected_signer_path()}", "-keypool=10"]) + self.log.debug(f"-signer={self.mock_signer_path('no_signer.py')}") + self.restart_node(1, [f"-signer={self.mock_signer_path('no_signer.py')}", "-keypool=10"]) self.nodes[1].loadwallet('hww_disconnect') hww = self.nodes[1].get_wallet_rpc('hww_disconnect') @@ -266,12 +249,12 @@ def test_disconnected_signer(self): assert_raises_rpc_error(-25, "External signer not found", hww.send, outputs=[{dest:0.5}]) def test_invalid_signer(self): - self.log.debug(f"-signer={self.mock_invalid_signer_path()}") + self.log.debug(f"-signer={self.mock_signer_path('invalid_signer.py')}") self.log.info('Test invalid external signer') assert_raises_rpc_error(-1, "Invalid descriptor", self.nodes[1].createwallet, wallet_name='hww_invalid', external_signer=True) def test_multiple_signers(self): - self.log.debug(f"-signer={self.mock_multi_signers_path()}") + self.log.debug(f"-signer={self.mock_signer_path('multi_signers.py')}") self.log.info('Test multiple external signers') assert_raises_rpc_error(-1, "More than one external signer found", self.nodes[1].createwallet, wallet_name='multi_hww', external_signer=True) From 89185febb07f58ae9261cd2ce9e829fffcfb2df9 Mon Sep 17 00:00:00 2001 From: Sjors Date: Wed, 29 Apr 2026 10:03:11 +0000 Subject: [PATCH 22/72] wallet: upgrade to ExternalSignerScriptPubKeyMan in AddWalletDescriptor CWallet::AddWalletDescriptor created a plain DescriptorScriptPubKeyMan even when WALLET_FLAG_EXTERNAL_SIGNER was set. Create the external-signer variant immediately so newly imported descriptors can use address display and signing without requiring an unload/reload cycle. --- src/wallet/external_signer_scriptpubkeyman.cpp | 9 +++++++++ src/wallet/external_signer_scriptpubkeyman.h | 1 + src/wallet/wallet.cpp | 7 ++++++- test/functional/wallet_signer.py | 18 ++++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/wallet/external_signer_scriptpubkeyman.cpp b/src/wallet/external_signer_scriptpubkeyman.cpp index 96f1ee6cfcc0..5195c303219c 100644 --- a/src/wallet/external_signer_scriptpubkeyman.cpp +++ b/src/wallet/external_signer_scriptpubkeyman.cpp @@ -26,6 +26,15 @@ std::unique_ptr ExternalSignerScriptPubKeyMan::Lo return std::unique_ptr(new ExternalSignerScriptPubKeyMan(storage, id, descriptor, keypool_size, keys, ckeys)); } +std::unique_ptr ExternalSignerScriptPubKeyMan::CreateFromImport(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size, const FlatSigningProvider& provider) +{ + auto spkm = std::unique_ptr(new ExternalSignerScriptPubKeyMan(storage, descriptor, keypool_size)); + if (auto res = spkm->UpdateWalletDescriptor(descriptor, provider); !res) { + throw std::runtime_error(util::ErrorString(res).original); + } + return spkm; +} + std::unique_ptr ExternalSignerScriptPubKeyMan::CreateNew(WalletStorage& storage, WalletBatch& batch, int64_t keypool_size, std::unique_ptr desc) { int64_t creation_time = GetTime(); diff --git a/src/wallet/external_signer_scriptpubkeyman.h b/src/wallet/external_signer_scriptpubkeyman.h index 51c7362d983e..5a6cd98f6ed2 100644 --- a/src/wallet/external_signer_scriptpubkeyman.h +++ b/src/wallet/external_signer_scriptpubkeyman.h @@ -20,6 +20,7 @@ class ExternalSignerScriptPubKeyMan : public DescriptorScriptPubKeyMan public: static std::unique_ptr LoadFromStorage(WalletStorage& storage, const uint256& id, WalletDescriptor& descriptor, int64_t keypool_size, const KeyMap& keys, const CryptedKeyMap& ckeys); + static std::unique_ptr CreateFromImport(WalletStorage& storage, WalletDescriptor& descriptor, int64_t keypool_size, const FlatSigningProvider& provider); static std::unique_ptr CreateNew(WalletStorage& storage, WalletBatch& batch, int64_t keypool_size, std::unique_ptr desc); static util::Result GetExternalSigner(); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 22922abf1600..ff593db52138 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3639,7 +3639,12 @@ util::Result> CWallet::AddWall return util::Error{util::ErrorString(spkm_res)}; } } else { - auto new_spk_man = DescriptorScriptPubKeyMan::CreateFromImport(*this, desc, m_keypool_size, signing_provider); + std::unique_ptr new_spk_man; + if (IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER)) { + new_spk_man = ExternalSignerScriptPubKeyMan::CreateFromImport(*this, desc, m_keypool_size, signing_provider); + } else { + new_spk_man = DescriptorScriptPubKeyMan::CreateFromImport(*this, desc, m_keypool_size, signing_provider); + } spk_man = new_spk_man.get(); // Save the descriptor to memory diff --git a/test/functional/wallet_signer.py b/test/functional/wallet_signer.py index 0fe384925a70..9da82a5a9380 100755 --- a/test/functional/wallet_signer.py +++ b/test/functional/wallet_signer.py @@ -9,6 +9,7 @@ """ import os +from test_framework.descriptors import descsum_create from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, @@ -40,6 +41,7 @@ def clear_mock_result(self, node): def run_test(self): self.test_valid_signer() + self.test_import_descriptor() self.test_disconnected_signer() self.restart_node(1, [f"-signer={self.mock_signer_path('invalid_signer.py')}", "-keypool=10"]) self.test_invalid_signer() @@ -225,6 +227,22 @@ def test_valid_signer(self): assert_greater_than(res["fee"], res["origfee"]) assert_equal(res["errors"], []) + def test_import_descriptor(self): + self.log.info('Test using the signer for an imported descriptor, without reloading') + + self.nodes[1].createwallet(wallet_name='hww_import', external_signer=True, blank=True) + hww_import = self.nodes[1].get_wallet_rpc('hww_import') + xpub = "tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B" + result = hww_import.importdescriptors([{ + "desc": descsum_create(f"wpkh([00000001/84h/1h/0h]{xpub}/0/*)"), + "active": True, + "timestamp": "now", + }]) + assert_equal(result[0]["success"], True) + + address = hww_import.getnewaddress(address_type="bech32") + assert_equal(hww_import.walletdisplayaddress(address), {"address": address}) + self.nodes[1].unloadwallet('hww_import') def test_disconnected_signer(self): self.log.info('Test disconnected external signer') From df99c3a3801338b462fe1573389b1ac5eb6471db Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Fri, 28 Aug 2026 07:19:20 +0000 Subject: [PATCH 23/72] wallet: sign with own keys before using the external signer ExternalSignerScriptPubKeyMan::FillPSBT went straight to the external signer whenever sign is set. Now that a signer wallet can hold private keys, a descriptor with a hot key gets an ExternalSignerScriptPubKeyMan as well, and its signature was never made. Let the base class fill and sign first, and only involve the signer if an input that belongs to this descriptor is still unsigned. --- .../external_signer_scriptpubkeyman.cpp | 19 +++++++++++--- test/functional/wallet_signer.py | 25 +++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/wallet/external_signer_scriptpubkeyman.cpp b/src/wallet/external_signer_scriptpubkeyman.cpp index 5195c303219c..febf12b26120 100644 --- a/src/wallet/external_signer_scriptpubkeyman.cpp +++ b/src/wallet/external_signer_scriptpubkeyman.cpp @@ -96,13 +96,24 @@ util::Result ExternalSignerScriptPubKeyMan::DisplayAddress(const CTxDestin // If sign is true, transaction must previously have been filled std::optional ExternalSignerScriptPubKeyMan::FillPSBT(PartiallySignedTransaction& psbt, const PrecomputedTransactionData& txdata, const common::PSBTFillOptions& options, int* n_signed) const { - if (!options.sign) { - return DescriptorScriptPubKeyMan::FillPSBT(psbt, txdata, options, n_signed); - } + // Fill in metadata. The base class only signs if options.sign is set, and + // only with keys we hold ourselves. + if (auto err = DescriptorScriptPubKeyMan::FillPSBT(psbt, txdata, options, n_signed)) return err; + if (!options.sign) return {}; - // Already complete if every input is now signed + // No need for an external signer roundtrip if we already have all the + // signatures we need. bool complete = true; for (const auto& input : psbt.inputs) { + CTxOut utxo; + // An externally created PSBT may not include the UTXO for inputs we + // don't know, in which case we can't tell whether it's ours. Leave it + // to the signer. + if (input.GetUTXO(utxo)) { + // Only consider inputs that belong to this descriptor; the wallet + // may hold the private key for the other inputs. + if (!IsMine(utxo.scriptPubKey)) continue; + } complete &= PSBTInputSigned(input); } if (complete) return {}; diff --git a/test/functional/wallet_signer.py b/test/functional/wallet_signer.py index 9da82a5a9380..220e6f581872 100755 --- a/test/functional/wallet_signer.py +++ b/test/functional/wallet_signer.py @@ -10,6 +10,7 @@ import os from test_framework.descriptors import descsum_create +from test_framework.extendedkey import ExtendedPrivateKey from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_equal, @@ -42,6 +43,7 @@ def clear_mock_result(self, node): def run_test(self): self.test_valid_signer() self.test_import_descriptor() + self.test_hot_key() self.test_disconnected_signer() self.restart_node(1, [f"-signer={self.mock_signer_path('invalid_signer.py')}", "-keypool=10"]) self.test_invalid_signer() @@ -244,6 +246,29 @@ def test_import_descriptor(self): assert_equal(hww_import.walletdisplayaddress(address), {"address": address}) self.nodes[1].unloadwallet('hww_import') + def test_hot_key(self): + self.log.info('Test spending a hot key coin in an external signer wallet') + + self.nodes[1].createwallet(wallet_name='hww_hot_key', external_signer=True, disable_private_keys=False) + hww_hot_key = self.nodes[1].get_wallet_rpc('hww_hot_key') + hot_desc = descsum_create(f"wpkh({ExtendedPrivateKey.generate().to_string()}/<0;1>/*)") + result = hww_hot_key.importdescriptors([{ + "desc": hot_desc, + "active": True, + "timestamp": "now", + }]) + assert_equal(result[0]["success"], True) + + hot_address = hww_hot_key.getnewaddress(address_type="bech32") + self.nodes[0].sendtoaddress(hot_address, 1) + self.generate(self.nodes[0], 1) + hot_utxo = hww_hot_key.listunspent(addresses=[hot_address])[0] + + dest = self.nodes[0].getnewaddress() + res = hww_hot_key.send(outputs={dest: 0.5}, inputs=[hot_utxo], add_inputs=False, add_to_wallet=False) + assert res["complete"] + assert self.nodes[1].testmempoolaccept([res["hex"]])[0]["allowed"] + def test_disconnected_signer(self): self.log.info('Test disconnected external signer') From 92cd45b6e48d4847a515274321958a7c7cfc4ccf Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Mon, 28 Sep 2026 07:57:04 +0000 Subject: [PATCH 24/72] test: reuse create_outpoints in wallet_signer --- test/functional/wallet_signer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/functional/wallet_signer.py b/test/functional/wallet_signer.py index 20a5a3f3fcb4..073b4e51eb93 100755 --- a/test/functional/wallet_signer.py +++ b/test/functional/wallet_signer.py @@ -151,7 +151,7 @@ def test_valid_signer(self): ) self.log.info('Prepare mock PSBT') - self.nodes[0].sendtoaddress(address4, 1) + self.create_outpoints(self.nodes[0], outputs=[{address4: 1}]) self.generate(self.nodes[0], 1) # Load private key into wallet to generate a signed PSBT for the mock @@ -231,7 +231,7 @@ def test_disconnected_signer(self): assert_equal(hww.getwalletinfo()["external_signer"], True) # Fund wallet - self.nodes[0].sendtoaddress(hww.getnewaddress(address_type="bech32m"), 1) + self.create_outpoints(self.nodes[0], outputs=[{hww.getnewaddress(address_type="bech32m"): 1}]) self.generate(self.nodes[0], 1) # Restart node with no signer connected From b94d560e1d82779cee48bdf3ae97d9e2823e5dee Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Fri, 28 Aug 2026 08:36:36 +0000 Subject: [PATCH 25/72] test: have external signer mock use a wallet The external signer mock previously replayed a PSBT that the test prepared in advance. This makes it difficult to test more complicated scenarios like a misbehaving wallet and (MuSig2) multisig. Instead, give the mock its own descriptor wallet. The test provides a dedicated node for this wallet and keeps it offline, so the mock can't cheat by e.g. inspecting the UTXO set. The mock creates the wallet on first use and signs with walletprocesspsbt. wallet_signer.py now funds all four descriptor types and spends them in a single transaction, exercising every signing code path. --- test/functional/mocks/signer.py | 50 ++++++++++++++---- test/functional/wallet_signer.py | 89 +++++++++++++------------------- 2 files changed, 76 insertions(+), 63 deletions(-) diff --git a/test/functional/mocks/signer.py b/test/functional/mocks/signer.py index a13c97d1216f..d668b0da65ed 100755 --- a/test/functional/mocks/signer.py +++ b/test/functional/mocks/signer.py @@ -8,6 +8,17 @@ import argparse import json +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.realpath(__file__)))) + +from test_framework.authproxy import AuthServiceProxy, JSONRPCException + +# Master private key for the tpub in getdescriptors below. Used by signtx, +# which imports the keys into a wallet on the offline node provided by the +# test and lets it do the actual signing. +tprv = "tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK" + +MOCK_WALLET = "mock" + def perform_pre_checks(): mock_result_path = os.path.join(os.getcwd(), "mock_result") if os.path.isfile(mock_result_path): @@ -56,20 +67,41 @@ def displayaddress(args): return sys.stdout.write(json.dumps({"address": expected_desc[args.desc]})) +def get_mock_wallet(): + """RPC connection to the wallet holding our private keys, created on + first use. The test provides a dedicated offline node for it and passes + the node's RPC URL via a file in our working directory.""" + with open(os.path.join(os.getcwd(), "mock_rpc_url"), "r", encoding="utf8") as f: + node_url = f.read().strip() + node = AuthServiceProxy(node_url) + wallet = AuthServiceProxy(f"{node_url}/wallet/{MOCK_WALLET}") + try: + node.loadwallet(filename=MOCK_WALLET) + return wallet + except JSONRPCException as e: + if e.error["code"] == -35: # RPC_WALLET_ALREADY_LOADED + return wallet + if e.error["code"] != -18: # RPC_WALLET_NOT_FOUND + raise + node.createwallet(wallet_name=MOCK_WALLET, blank=True) + requests = [] + for desc in [f"pkh({tprv}/<0;1>/*)", f"sh(wpkh({tprv}/<0;1>/*))", f"wpkh({tprv}/<0;1>/*)", f"tr({tprv}/<0;1>/*)"]: + checksum = node.getdescriptorinfo(descriptor=desc)["checksum"] + requests.append({"desc": f"{desc}#{checksum}", "timestamp": "now", "range": [0, 99]}) + result = wallet.importdescriptors(requests=requests) + assert all(r["success"] for r in result) + return wallet + def signtx(args): if args.fingerprint != "00000001": return sys.stdout.write(json.dumps({"error": "Unexpected fingerprint", "fingerprint": args.fingerprint})) - with open(os.path.join(os.getcwd(), "mock_psbt"), "r") as f: - mock_psbt = f.read() + psbt = args.psbt + wallet = get_mock_wallet() + result = wallet.walletprocesspsbt(psbt=psbt, sign=True, bip32derivs=False, finalize=False) + reply = result["psbt"] - if args.fingerprint == "00000001" : - sys.stdout.write(json.dumps({ - "psbt": mock_psbt, - "complete": True - })) - else: - sys.stdout.write(json.dumps({"psbt": args.psbt})) + sys.stdout.write(json.dumps({"psbt": reply})) parser = argparse.ArgumentParser(prog='./signer.py', description='External signer mock') parser.add_argument('--fingerprint') diff --git a/test/functional/wallet_signer.py b/test/functional/wallet_signer.py index 073b4e51eb93..88fe8e9c7193 100755 --- a/test/functional/wallet_signer.py +++ b/test/functional/wallet_signer.py @@ -36,14 +36,26 @@ def mock_multi_signers_path(self): return sys.executable + " " + path def set_test_params(self): - self.num_nodes = 2 + self.num_nodes = 3 self.noban_tx_relay = True # faster tx relay self.extra_args = [ [], [f"-signer={self.mock_signer_path()}", '-keypool=10'], + # Node for the signer mock's wallet, kept offline so the mock + # can't cheat by e.g. inspecting the UTXO set + ["-maxconnections=0"], ] + def setup_network(self): + self.setup_nodes() + # Leave the signer mock's node disconnected + self.connect_nodes(0, 1) + + def sync_except_mock(self): + """Sync all nodes except the signer mock's, which never receives blocks.""" + self.sync_all(self.nodes[0:2]) + def skip_test_if_missing_module(self): self.skip_if_no_external_signer() self.skip_if_no_wallet() @@ -55,7 +67,16 @@ def set_mock_result(self, node, res): def clear_mock_result(self, node): os.remove(os.path.join(node.cwd, "mock_result")) + def init_mock_node(self): + """Hand the signer mock its dedicated offline node, on which it + creates the wallet it signs with.""" + signer_node = self.nodes[2] + assert_equal(signer_node.getconnectioncount(), 0) + with open(os.path.join(self.nodes[1].cwd, "mock_rpc_url"), "w") as f: + f.write(signer_node.url) + def run_test(self): + self.init_mock_node() self.test_valid_signer() self.test_disconnected_signer() self.restart_node(1, [f"-signer={self.mock_invalid_signer_path()}", "-keypool=10"]) @@ -150,73 +171,33 @@ def test_valid_signer(self): hww.walletdisplayaddress, address_fail ) - self.log.info('Prepare mock PSBT') - self.create_outpoints(self.nodes[0], outputs=[{address4: 1}]) - self.generate(self.nodes[0], 1) - - # Load private key into wallet to generate a signed PSBT for the mock - self.nodes[1].createwallet(wallet_name="mock", disable_private_keys=False, blank=True) - mock_wallet = self.nodes[1].get_wallet_rpc("mock") - assert mock_wallet.getwalletinfo()['private_keys_enabled'] - - result = mock_wallet.importdescriptors([{ - "desc": "tr([00000001/86h/1h/0']tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK/0/*)#7ew68cn8", - "timestamp": 0, - "range": [0,1], - "internal": False, - "active": True - }, - { - "desc": "tr([00000001/86h/1h/0']tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK/1/*)#0dtm6drl", - "timestamp": 0, - "range": [0, 0], - "internal": True, - "active": True - }]) - assert_equal(result[0], {'success': True}) - assert_equal(result[1], {'success': True}) - assert_equal(mock_wallet.getwalletinfo()["txcount"], 1) - dest = self.nodes[0].getnewaddress(address_type='bech32') - mock_psbt = mock_wallet.walletcreatefundedpsbt([], {dest:0.5}, 0, {'replaceable': True}, True)['psbt'] - mock_psbt_signed = mock_wallet.walletprocesspsbt(psbt=mock_psbt, sign=True, sighashtype="ALL", bip32derivs=True) - mock_tx = mock_psbt_signed["hex"] - assert mock_wallet.testmempoolaccept([mock_tx])[0]["allowed"] - + self.log.info('Fund hww wallet') + self.create_outpoints(self.nodes[0], outputs=[{address: 1} for address in [address1, address2, address3, address4]]) + self.generate(self.nodes[0], 1, sync_fun=self.sync_except_mock) assert_equal(hww.getwalletinfo()["txcount"], 1) - assert hww.testmempoolaccept([mock_tx])[0]["allowed"] - - with open(os.path.join(self.nodes[1].cwd, "mock_psbt"), "w") as f: - f.write(mock_psbt_signed["psbt"]) + dest = self.nodes[0].getnewaddress(address_type='bech32') self.log.info('Test send using hww1') - # Don't broadcast transaction yet so the RPC returns the raw hex - res = hww.send(outputs={dest:0.5},add_to_wallet=False) + # Spend all four address types at once. Don't broadcast the transaction + # yet so the RPC returns the raw hex. + res = hww.send(outputs={dest:3.5}, add_to_wallet=False) assert res["complete"] - assert_equal(res["hex"], mock_tx) + assert_equal(len(hww.decoderawtransaction(res["hex"])["vin"]), 4) + assert hww.testmempoolaccept([res["hex"]])[0]["allowed"] self.log.info('Test sendall using hww1') - res = hww.sendall(recipients=[{dest:0.5}, hww.getrawchangeaddress()], add_to_wallet=False) + res = hww.sendall(recipients=[{dest:3.5}, hww.getrawchangeaddress()], add_to_wallet=False) assert res["complete"] - assert_equal(res["hex"], mock_tx) + assert hww.testmempoolaccept([res["hex"]])[0]["allowed"] # Broadcast transaction so we can bump the fee hww.sendrawtransaction(res["hex"]) - self.log.info('Prepare fee bumped mock PSBT') - - # Now that the transaction is broadcast, bump fee in mock wallet: - orig_tx_id = res["txid"] - mock_psbt_bumped = mock_wallet.psbtbumpfee(orig_tx_id)["psbt"] - mock_psbt_bumped_signed = mock_wallet.walletprocesspsbt(psbt=mock_psbt_bumped, sign=True, sighashtype="ALL", bip32derivs=True) - - with open(os.path.join(self.nodes[1].cwd, "mock_psbt"), "w") as f: - f.write(mock_psbt_bumped_signed["psbt"]) - self.log.info('Test bumpfee using hww1') - # Bump fee + orig_tx_id = res["txid"] res = hww.bumpfee(orig_tx_id) assert_greater_than(res["fee"], res["origfee"]) assert_equal(res["errors"], []) @@ -232,7 +213,7 @@ def test_disconnected_signer(self): # Fund wallet self.create_outpoints(self.nodes[0], outputs=[{hww.getnewaddress(address_type="bech32m"): 1}]) - self.generate(self.nodes[0], 1) + self.generate(self.nodes[0], 1, sync_fun=self.sync_except_mock) # Restart node with no signer connected self.log.debug(f"-signer={self.mock_no_connected_signer_path()}") From d95f9f42e86934e1121249e0d5e236474b75312c Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Mon, 28 Sep 2026 08:12:30 +0000 Subject: [PATCH 26/72] test: add external signer mock sign modes Co-authored-by: brunoerg --- test/functional/mocks/signer.py | 8 ++++++++ test/functional/wallet_signer.py | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/test/functional/mocks/signer.py b/test/functional/mocks/signer.py index d668b0da65ed..722b7c109a9a 100755 --- a/test/functional/mocks/signer.py +++ b/test/functional/mocks/signer.py @@ -92,6 +92,14 @@ def get_mock_wallet(): assert all(r["success"] for r in result) return wallet +def get_mock_sign_mode(): + # The test can instruct us to sign in a specific, possibly misbehaving, way + sign_mode_path = os.path.join(os.getcwd(), "mock_sign_mode") + if os.path.isfile(sign_mode_path): + with open(sign_mode_path, "r", encoding="utf8") as f: + return f.read().strip() + return None + def signtx(args): if args.fingerprint != "00000001": return sys.stdout.write(json.dumps({"error": "Unexpected fingerprint", "fingerprint": args.fingerprint})) diff --git a/test/functional/wallet_signer.py b/test/functional/wallet_signer.py index 88fe8e9c7193..aeb916c97786 100755 --- a/test/functional/wallet_signer.py +++ b/test/functional/wallet_signer.py @@ -67,6 +67,13 @@ def set_mock_result(self, node, res): def clear_mock_result(self, node): os.remove(os.path.join(node.cwd, "mock_result")) + def set_mock_sign_mode(self, node, mode): + with open(os.path.join(node.cwd, "mock_sign_mode"), "w") as f: + f.write(mode) + + def clear_mock_sign_mode(self, node): + os.remove(os.path.join(node.cwd, "mock_sign_mode")) + def init_mock_node(self): """Hand the signer mock its dedicated offline node, on which it creates the wallet it signs with.""" From 12fa81f66c0ba4401ba8b4382c4f4b7529661fd1 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Mon, 28 Sep 2026 07:53:30 +0000 Subject: [PATCH 27/72] external_signer: require matching PSBT versions Reject a signer response with a different PSBT version and report the expected and returned versions. Document the requirement and test a v0 response to a v2 request. --- doc/external-signer.md | 2 ++ src/external_signer.cpp | 5 +++++ test/functional/mocks/signer.py | 7 +++++++ test/functional/wallet_signer.py | 14 ++++++++++++++ 4 files changed, 28 insertions(+) diff --git a/doc/external-signer.md b/doc/external-signer.md index 8b02edd6b8ab..1e6ffb528bda 100644 --- a/doc/external-signer.md +++ b/doc/external-signer.md @@ -148,6 +148,8 @@ A future extension could add an optional return field `reachable`, in case `` and MUST return a JSON object. On success, it MUST contain `{"psbt": ""}` updated to include any new signatures. On failure, it SHOULD contain `{"error": ""}`. Presence of a key `error` with value `null` is not considered an error. +The returned PSBT MUST use the same PSBT version as the request. + PSBT SHOULD include BIP32 derivations. The command SHOULD fail if none of the BIP32 derivations match a key owned by the device. The command SHOULD fail if the user cancels. diff --git a/src/external_signer.cpp b/src/external_signer.cpp index cb49cedc69c2..1a2bfcdb3da4 100644 --- a/src/external_signer.cpp +++ b/src/external_signer.cpp @@ -121,6 +121,11 @@ bool ExternalSigner::SignTransaction(PartiallySignedTransaction& psbtx, std::str return false; } + if (signer_psbtx->GetVersion() != psbtx.GetVersion()) { + error = strprintf("Signer returned PSBT version %u, expected %u", signer_psbtx->GetVersion(), psbtx.GetVersion()); + return false; + } + psbtx = *signer_psbtx; return true; diff --git a/test/functional/mocks/signer.py b/test/functional/mocks/signer.py index 722b7c109a9a..a4aa46005adf 100755 --- a/test/functional/mocks/signer.py +++ b/test/functional/mocks/signer.py @@ -104,11 +104,18 @@ def signtx(args): if args.fingerprint != "00000001": return sys.stdout.write(json.dumps({"error": "Unexpected fingerprint", "fingerprint": args.fingerprint})) + mode = get_mock_sign_mode() psbt = args.psbt wallet = get_mock_wallet() result = wallet.walletprocesspsbt(psbt=psbt, sign=True, bip32derivs=False, finalize=False) reply = result["psbt"] + if mode == "psbt_v0": + # Return the same transaction as a v0 PSBT. + tx = wallet.finalizepsbt(reply)["hex"] + # The conversion discards signatures; the version check runs first. + reply = wallet.converttopsbt(tx, permitsigdata=True, psbt_version=0) + sys.stdout.write(json.dumps({"psbt": reply})) parser = argparse.ArgumentParser(prog='./signer.py', description='External signer mock') diff --git a/test/functional/wallet_signer.py b/test/functional/wallet_signer.py index aeb916c97786..66526e0e07bf 100755 --- a/test/functional/wallet_signer.py +++ b/test/functional/wallet_signer.py @@ -85,6 +85,7 @@ def init_mock_node(self): def run_test(self): self.init_mock_node() self.test_valid_signer() + self.test_signer_psbt_version() self.test_disconnected_signer() self.restart_node(1, [f"-signer={self.mock_invalid_signer_path()}", "-keypool=10"]) self.test_invalid_signer() @@ -210,6 +211,19 @@ def test_valid_signer(self): assert_equal(res["errors"], []) + def test_signer_psbt_version(self): + hww = self.nodes[1].get_wallet_rpc('hww') + inputs = self.create_outpoints(self.nodes[0], outputs=[{hww.getnewaddress(): 2}]) + self.generate(self.nodes[0], 1, sync_fun=self.sync_except_mock) + dest = self.nodes[0].getnewaddress() + + self.log.info('The signer must preserve the PSBT version') + self.set_mock_sign_mode(self.nodes[1], "psbt_v0") + with self.nodes[1].assert_debug_log(["Signer returned PSBT version 0, expected 2"]): + assert_raises_rpc_error(-25, "External signer failed to sign", hww.send, outputs={dest: 1.5}, inputs=inputs, add_inputs=False) + + self.clear_mock_sign_mode(self.nodes[1]) + def test_disconnected_signer(self): self.log.info('Test disconnected external signer') From 2b801fc38452662f10c6ee998e86a1f90f97f482 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Sun, 25 Oct 2026 02:06:36 +0000 Subject: [PATCH 28/72] external_signer: merge PSBT response instead of replacing Previously the PSBT returned by the external signer replaced the original wholesale, trusting the signer not to modify the transaction. Merge it instead. This rejects a response that describes a different transaction. Merging also supports signers that strip fields they don't need from their response. New tests cover both scenarios. Also test a signer that changes a PSBT v2 input sequence before signing. The transaction identifier used for merging ignores sequences, but the merge preserves the original sequence, leaving the signatures invalid for the wallet's transaction. This and a corrupted signature must leave the original transaction unchanged and return an incomplete PSBT. Test ECDSA and Schnorr signatures independently. Suggested in: https://github.com/bitcoin/bitcoin/pull/36114#issuecomment-6390058038 Co-authored-by: brunoerg --- src/external_signer.cpp | 5 ++- test/functional/mocks/signer.py | 50 +++++++++++++++++++++ test/functional/wallet_signer.py | 76 ++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 1 deletion(-) diff --git a/src/external_signer.cpp b/src/external_signer.cpp index 1a2bfcdb3da4..b4646e43e4df 100644 --- a/src/external_signer.cpp +++ b/src/external_signer.cpp @@ -126,7 +126,10 @@ bool ExternalSigner::SignTransaction(PartiallySignedTransaction& psbtx, std::str return false; } - psbtx = *signer_psbtx; + if (!psbtx.Merge(*signer_psbtx)) { + error = "Signer returned a PSBT for a different transaction"; + return false; + } return true; } diff --git a/test/functional/mocks/signer.py b/test/functional/mocks/signer.py index a4aa46005adf..e4f53f168f25 100755 --- a/test/functional/mocks/signer.py +++ b/test/functional/mocks/signer.py @@ -11,6 +11,14 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.realpath(__file__)))) from test_framework.authproxy import AuthServiceProxy, JSONRPCException +from test_framework.psbt import ( + PSBT, + PSBT_IN_PARTIAL_SIG, + PSBT_IN_SEQUENCE, + PSBT_IN_TAP_KEY_SIG, + PSBT_OUT_AMOUNT, + PSBT_OUT_SCRIPT, +) # Master private key for the tpub in getdescriptors below. Used by signtx, # which imports the keys into a wallet on the offline node provided by the @@ -100,12 +108,32 @@ def get_mock_sign_mode(): return f.read().strip() return None +def tamper(psbt_b64, mode): + """Alter the transaction described by the (version 2) PSBT before signing + it, like a rogue or broken signer might.""" + psbt = PSBT.from_base64(psbt_b64) + if mode == "change_amount": + # Steal from the output by redirecting the value to fees + amount = int.from_bytes(psbt.o[0].map[PSBT_OUT_AMOUNT], "little", signed=True) + psbt.o[0].map[PSBT_OUT_AMOUNT] = (amount - 1).to_bytes(8, "little", signed=True) + elif mode == "change_script": + psbt.o[0].map[PSBT_OUT_SCRIPT] = bytes([0x51]) # OP_TRUE + elif mode == "remove_output": + psbt.o.pop() + elif mode == "change_sequence": + sequence = int.from_bytes(psbt.i[0].map[PSBT_IN_SEQUENCE], "little") + psbt.i[0].map[PSBT_IN_SEQUENCE] = (sequence ^ 1).to_bytes(4, "little") + return psbt.to_base64() + def signtx(args): if args.fingerprint != "00000001": return sys.stdout.write(json.dumps({"error": "Unexpected fingerprint", "fingerprint": args.fingerprint})) mode = get_mock_sign_mode() psbt = args.psbt + if mode in ("change_amount", "change_script", "remove_output", "change_sequence"): + psbt = tamper(psbt, mode) + wallet = get_mock_wallet() result = wallet.walletprocesspsbt(psbt=psbt, sign=True, bip32derivs=False, finalize=False) reply = result["psbt"] @@ -115,6 +143,28 @@ def signtx(args): tx = wallet.finalizepsbt(reply)["hex"] # The conversion discards signatures; the version check runs first. reply = wallet.converttopsbt(tx, permitsigdata=True, psbt_version=0) + elif mode == "strip": + # Return only the signatures, plus the fields required to describe + # the same transaction + signed = PSBT.from_base64(reply) + stripped = PSBT.from_base64(reply) + stripped.make_blank() + for signed_in, stripped_in in zip(signed.i, stripped.i): + for key, value in signed_in.map.items(): + if key == PSBT_IN_TAP_KEY_SIG or (isinstance(key, bytes) and key[0] == PSBT_IN_PARTIAL_SIG): + stripped_in.map[key] = value + reply = stripped.to_base64() + elif mode == "corrupt_signature": + signed = PSBT.from_base64(reply) + for psbt_in in signed.i: + for key, value in psbt_in.map.items(): + if key == PSBT_IN_TAP_KEY_SIG or (isinstance(key, bytes) and key[0] == PSBT_IN_PARTIAL_SIG): + # Change a signature byte while preserving the encoding + # and any trailing sighash byte. + signature = bytearray(value) + signature[-2] ^= 1 + psbt_in.map[key] = bytes(signature) + reply = signed.to_base64() sys.stdout.write(json.dumps({"psbt": reply})) diff --git a/test/functional/wallet_signer.py b/test/functional/wallet_signer.py index 66526e0e07bf..6b8982df47af 100755 --- a/test/functional/wallet_signer.py +++ b/test/functional/wallet_signer.py @@ -86,6 +86,9 @@ def run_test(self): self.init_mock_node() self.test_valid_signer() self.test_signer_psbt_version() + self.test_signer_stripped_psbt() + self.test_misbehaving_signer() + self.test_invalid_signer_signatures() self.test_disconnected_signer() self.restart_node(1, [f"-signer={self.mock_invalid_signer_path()}", "-keypool=10"]) self.test_invalid_signer() @@ -224,6 +227,79 @@ def test_signer_psbt_version(self): self.clear_mock_sign_mode(self.nodes[1]) + def test_signer_stripped_psbt(self): + self.log.info('Test external signer returning a stripped PSBT') + hww = self.nodes[1].get_wallet_rpc('hww') + + # Spend a segwit and a taproot UTXO, to cover both ECDSA and schnorr + # signatures + addresses = [hww.getnewaddress(address_type="bech32"), hww.getnewaddress(address_type="bech32m")] + inputs = self.create_outpoints(self.nodes[0], outputs=[{address: 1} for address in addresses]) + self.generate(self.nodes[0], 1, sync_fun=self.sync_except_mock) + dest = self.nodes[0].getnewaddress() + + self.log.info('The signer may strip fields it does not need') + self.set_mock_sign_mode(self.nodes[1], "strip") + res = hww.send(outputs={dest: 1.5}, inputs=inputs, add_inputs=False, add_to_wallet=False) + assert res["complete"] + assert hww.testmempoolaccept([res["hex"]])[0]["allowed"] + + self.clear_mock_sign_mode(self.nodes[1]) + + def test_misbehaving_signer(self): + self.log.info('Test misbehaving external signer') + hww = self.nodes[1].get_wallet_rpc('hww') + + # Spend a segwit and a taproot UTXO, to cover both ECDSA and schnorr + # signatures + addresses = [hww.getnewaddress(address_type="bech32"), hww.getnewaddress(address_type="bech32m")] + inputs = self.create_outpoints(self.nodes[0], outputs=[{address: 1} for address in addresses]) + self.generate(self.nodes[0], 1, sync_fun=self.sync_except_mock) + dest = self.nodes[0].getnewaddress() + + self.log.info('The signer must not tamper with the transaction') + for mode in ["change_amount", "change_script", "remove_output"]: + self.set_mock_sign_mode(self.nodes[1], mode) + with self.nodes[1].assert_debug_log(["Signer returned a PSBT for a different transaction"]): + assert_raises_rpc_error(-25, "External signer failed to sign", hww.send, outputs={dest: 1.5}, inputs=inputs, add_inputs=False) + + self.clear_mock_sign_mode(self.nodes[1]) + + # The same transaction is accepted from a well-behaved signer + res = hww.send(outputs={dest: 1.5}, inputs=inputs, add_inputs=False, add_to_wallet=False) + assert res["complete"] + + def test_invalid_signer_signatures(self): + hww = self.nodes[1].get_wallet_rpc('hww') + # Exercise ECDSA and schnorr verification independently. + for address_type in ["bech32", "bech32m"]: + self.log.info(f'Test invalid signer signatures for {address_type}') + inputs = self.create_outpoints(self.nodes[0], outputs=[{hww.getnewaddress(address_type=address_type): 1}]) + self.generate(self.nodes[0], 1, sync_fun=self.sync_except_mock) + dest = self.nodes[0].getnewaddress() + psbt = hww.walletcreatefundedpsbt(inputs=inputs, outputs={dest: 0.5}, options={"add_inputs": False}, psbt_version=2)["psbt"] + original = hww.decodepsbt(psbt) + assert_equal(original["input_count"], 1) + + # PSBT v2 merging ignores sequence differences when identifying + # the transaction, but must preserve the original sequence. A + # signature for the changed transaction cannot complete it. + for mode in ["change_sequence", "corrupt_signature"]: + self.set_mock_sign_mode(self.nodes[1], mode) + res = hww.walletprocesspsbt(psbt) + assert_equal(res["complete"], False) + assert "hex" not in res + decoded = hww.decodepsbt(res["psbt"]) + for key in ["tx_version", "fallback_locktime", "input_count", "output_count", "outputs"]: + assert_equal(decoded[key], original[key]) + for key in ["previous_txid", "previous_vout", "sequence"]: + assert_equal(decoded["inputs"][0][key], original["inputs"][0][key]) + + self.clear_mock_sign_mode(self.nodes[1]) + res = hww.walletprocesspsbt(psbt) + assert res["complete"] + assert hww.testmempoolaccept([res["hex"]])[0]["allowed"] + def test_disconnected_signer(self): self.log.info('Test disconnected external signer') From 27961808424345b112a2bca59457c9de54782fba Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Mon, 28 Sep 2026 07:48:39 +0000 Subject: [PATCH 29/72] external_signer: reject finalized PSBT inputs Require signer responses to keep signatures in non-final fields and leave finalization to Bitcoin Core. Document the requirement in the signer API. Test final scriptSig and witness fields independently, including responses that finalize only one input. --- doc/external-signer.md | 2 ++ src/external_signer.cpp | 5 +++++ test/functional/mocks/signer.py | 11 +++++++++++ test/functional/wallet_signer.py | 21 +++++++++++++++++++++ 4 files changed, 39 insertions(+) diff --git a/doc/external-signer.md b/doc/external-signer.md index 1e6ffb528bda..d125fba87b85 100644 --- a/doc/external-signer.md +++ b/doc/external-signer.md @@ -150,6 +150,8 @@ This command reads request `` and MUST return a JSON object The returned PSBT MUST use the same PSBT version as the request. +The returned PSBT MUST NOT contain finalized inputs (`PSBT_IN_FINAL_SCRIPTSIG` or `PSBT_IN_FINAL_SCRIPTWITNESS`). Signatures MUST remain in the partial signature or Taproot signature fields. + PSBT SHOULD include BIP32 derivations. The command SHOULD fail if none of the BIP32 derivations match a key owned by the device. The command SHOULD fail if the user cancels. diff --git a/src/external_signer.cpp b/src/external_signer.cpp index b4646e43e4df..d4d373a26273 100644 --- a/src/external_signer.cpp +++ b/src/external_signer.cpp @@ -126,6 +126,11 @@ bool ExternalSigner::SignTransaction(PartiallySignedTransaction& psbtx, std::str return false; } + if (std::ranges::any_of(signer_psbtx->inputs, PSBTInputSigned)) { + error = "Signer returned a PSBT with finalized inputs"; + return false; + } + if (!psbtx.Merge(*signer_psbtx)) { error = "Signer returned a PSBT for a different transaction"; return false; diff --git a/test/functional/mocks/signer.py b/test/functional/mocks/signer.py index e4f53f168f25..90c121d6f683 100755 --- a/test/functional/mocks/signer.py +++ b/test/functional/mocks/signer.py @@ -154,6 +154,17 @@ def signtx(args): if key == PSBT_IN_TAP_KEY_SIG or (isinstance(key, bytes) and key[0] == PSBT_IN_PARTIAL_SIG): stripped_in.map[key] = value reply = stripped.to_base64() + elif mode in ("finalized", "partially_finalized"): + finalized = wallet.finalizepsbt(reply, extract=False) + assert finalized["complete"] + if mode == "finalized": + reply = finalized["psbt"] + else: + # Finalize only one input, leaving the rest unfinalized. + signed = PSBT.from_base64(reply) + assert len(signed.i) > 1 + signed.i[0] = PSBT.from_base64(finalized["psbt"]).i[0] + reply = signed.to_base64() elif mode == "corrupt_signature": signed = PSBT.from_base64(reply) for psbt_in in signed.i: diff --git a/test/functional/wallet_signer.py b/test/functional/wallet_signer.py index 6b8982df47af..ae6488797456 100755 --- a/test/functional/wallet_signer.py +++ b/test/functional/wallet_signer.py @@ -89,6 +89,7 @@ def run_test(self): self.test_signer_stripped_psbt() self.test_misbehaving_signer() self.test_invalid_signer_signatures() + self.test_finalized_signer() self.test_disconnected_signer() self.restart_node(1, [f"-signer={self.mock_invalid_signer_path()}", "-keypool=10"]) self.test_invalid_signer() @@ -300,6 +301,26 @@ def test_invalid_signer_signatures(self): assert res["complete"] assert hww.testmempoolaccept([res["hex"]])[0]["allowed"] + def test_finalized_signer(self): + hww = self.nodes[1].get_wallet_rpc('hww') + # Exercise final_script_sig and final_script_witness independently. + for address_type in ["legacy", "bech32m"]: + self.log.info(f'Test finalized signer inputs for {address_type}') + addresses = [hww.getnewaddress(address_type=address_type) for _ in range(2)] + inputs = self.create_outpoints(self.nodes[0], outputs=[{address: 1} for address in addresses]) + self.generate(self.nodes[0], 1, sync_fun=self.sync_except_mock) + dest = self.nodes[0].getnewaddress() + + for mode in ["finalized", "partially_finalized"]: + self.set_mock_sign_mode(self.nodes[1], mode) + with self.nodes[1].assert_debug_log(["Signer returned a PSBT with finalized inputs"]): + assert_raises_rpc_error(-25, "External signer failed to sign", hww.send, outputs={dest: 1.5}, inputs=inputs, add_inputs=False) + + self.clear_mock_sign_mode(self.nodes[1]) + res = hww.send(outputs={dest: 1.5}, inputs=inputs, add_inputs=False, add_to_wallet=False) + assert res["complete"] + assert hww.testmempoolaccept([res["hex"]])[0]["allowed"] + def test_disconnected_signer(self): self.log.info('Test disconnected external signer') From 4110fbd0570711ba7cb780eb4d2b6088364e266a Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Sat, 24 Oct 2026 05:58:11 +0000 Subject: [PATCH 30/72] external_signer: reject unsafe sighash types A signature with SIGHASH_NONE or SIGHASH_SINGLE doesn't commit to all outputs, letting anyone alter them after signing. Reject a PSBT from an external signer that declares such a sighash type or contains signatures made with one. SIGHASH_ANYONECANPAY is still accepted: it only permits adding inputs, which does not affect us. The mock signer produces real signatures for these scenarios by letting its wallet sign with the requested sighash type, optionally hiding the declared sighash type field so that only the signatures themselves reveal it. Co-authored-by: brunoerg --- doc/external-signer.md | 2 +- src/external_signer.cpp | 37 ++++++++++++++++++++++++++++++++ test/functional/mocks/signer.py | 26 +++++++++++++++++++++- test/functional/wallet_signer.py | 31 ++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 2 deletions(-) diff --git a/doc/external-signer.md b/doc/external-signer.md index d125fba87b85..0a2158eece78 100644 --- a/doc/external-signer.md +++ b/doc/external-signer.md @@ -150,7 +150,7 @@ This command reads request `` and MUST return a JSON object The returned PSBT MUST use the same PSBT version as the request. -The returned PSBT MUST NOT contain finalized inputs (`PSBT_IN_FINAL_SCRIPTSIG` or `PSBT_IN_FINAL_SCRIPTWITNESS`). Signatures MUST remain in the partial signature or Taproot signature fields. +The returned PSBT MUST NOT contain finalized inputs (`PSBT_IN_FINAL_SCRIPTSIG` or `PSBT_IN_FINAL_SCRIPTWITNESS`). Signatures MUST remain in the partial signature or Taproot signature fields so Bitcoin Core can check their sighash types. PSBT SHOULD include BIP32 derivations. The command SHOULD fail if none of the BIP32 derivations match a key owned by the device. diff --git a/src/external_signer.cpp b/src/external_signer.cpp index d4d373a26273..4ce2505c41be 100644 --- a/src/external_signer.cpp +++ b/src/external_signer.cpp @@ -8,10 +8,13 @@ #include #include #include +#include