Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32# Copyright (c) 2023-present The Bitcoin Core developers3# Distributed under the MIT software license, see the accompanying4# file COPYING or http://www.opensource.org/licenses/mit-license.php.5"""Test wallet gethdkeys RPC."""7from test_framework.descriptors import descsum_create8from test_framework.test_framework import BitcoinTestFramework9from test_framework.util import (10assert_equal,11assert_raises_rpc_error,12assert_not_equal,13assert_greater_than,14)15from test_framework.wallet_util import WalletUnlock18class WalletGetHDKeyTest(BitcoinTestFramework):19def set_test_params(self):20self.setup_clean_chain = True21self.num_nodes = 123def skip_test_if_missing_module(self):24self.skip_if_no_wallet()26def run_test(self):27self.test_basic_gethdkeys()28self.test_ranged_imports()29self.test_lone_key_imports()30self.test_ranged_multisig()31self.test_mixed_multisig()33def test_basic_gethdkeys(self):34self.log.info("Test gethdkeys basics")35self.nodes[0].createwallet("basic")36wallet = self.nodes[0].get_wallet_rpc("basic")37xpub_info = wallet.gethdkeys()38assert_equal(len(xpub_info), 1)39assert_equal(xpub_info[0]["has_private"], True)41assert "xprv" not in xpub_info[0]42xpub = xpub_info[0]["xpub"]44xpub_info = wallet.gethdkeys(private=True)45xprv = xpub_info[0]["xprv"]46assert_equal(xpub_info[0]["xpub"], xpub)47assert_equal(xpub_info[0]["has_private"], True)49descs = wallet.listdescriptors(True)50for desc in descs["descriptors"]:51assert xprv in desc["desc"]53self.log.info("HD pubkey can be retrieved from encrypted wallets")54prev_xprv = xprv55wallet.encryptwallet("pass")56# HD key is rotated on encryption, there should now be 2 HD keys57assert_equal(len(wallet.gethdkeys()), 2)58# New key is active, should be able to get only that one and its descriptors59xpub_info = wallet.gethdkeys(active_only=True)60assert_equal(len(xpub_info), 1)61assert_not_equal(xpub_info[0]["xpub"], xpub)62assert "xprv" not in xpub_info[0]63assert_equal(xpub_info[0]["has_private"], True)65self.log.info("HD privkey can be retrieved from encrypted wallets")66assert_raises_rpc_error(-13, "Error: Please enter the wallet passphrase with walletpassphrase first", wallet.gethdkeys, private=True)67with WalletUnlock(wallet, "pass"):68xpub_info = wallet.gethdkeys(active_only=True, private=True)[0]69assert_not_equal(xpub_info["xprv"], xprv)70for desc in wallet.listdescriptors(True)["descriptors"]:71if desc["active"]:72# After encrypting, HD key was rotated and should appear in all active descriptors73assert xpub_info["xprv"] in desc["desc"]74else:75# Inactive descriptors should have the previous HD key76assert prev_xprv in desc["desc"]78def test_ranged_imports(self):79self.log.info("Keys of imported ranged descriptors appear in gethdkeys")80def_wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name)81self.nodes[0].createwallet("imports")82wallet = self.nodes[0].get_wallet_rpc("imports")84xpub_info = wallet.gethdkeys()85assert_equal(len(xpub_info), 1)86active_xpub = xpub_info[0]["xpub"]88import_xpub = def_wallet.gethdkeys(active_only=True)[0]["xpub"]89desc_import = def_wallet.listdescriptors(True)["descriptors"]90for desc in desc_import:91desc["active"] = False92wallet.importdescriptors(desc_import)93assert_equal(wallet.gethdkeys(active_only=True), xpub_info)95xpub_info = wallet.gethdkeys()96assert_equal(len(xpub_info), 2)97for x in xpub_info:98if x["xpub"] == active_xpub:99for desc in x["descriptors"]:100assert_equal(desc["active"], True)101elif x["xpub"] == import_xpub:102for desc in x["descriptors"]:103assert_equal(desc["active"], False)104else:105assert False108def test_lone_key_imports(self):109self.log.info("Non-HD keys do not appear in gethdkeys")110self.nodes[0].createwallet("lonekey", blank=True)111wallet = self.nodes[0].get_wallet_rpc("lonekey")113assert_equal(wallet.gethdkeys(), [])114wallet.importdescriptors([{"desc": descsum_create("wpkh(cTe1f5rdT8A8DFgVWTjyPwACsDPJM9ff4QngFxUixCSvvbg1x6sh)"), "timestamp": "now"}])115assert_equal(wallet.gethdkeys(), [])117self.log.info("HD keys of non-ranged descriptors should appear in gethdkeys")118def_wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name)119xpub_info = def_wallet.gethdkeys(private=True)120xpub = xpub_info[0]["xpub"]121xprv = xpub_info[0]["xprv"]122prv_desc = descsum_create(f"wpkh({xprv})")123pub_desc = descsum_create(f"wpkh({xpub})")124assert_equal(wallet.importdescriptors([{"desc": prv_desc, "timestamp": "now"}])[0]["success"], True)125xpub_info = wallet.gethdkeys()126assert_equal(len(xpub_info), 1)127assert_equal(xpub_info[0]["xpub"], xpub)128assert_equal(len(xpub_info[0]["descriptors"]), 1)129assert_equal(xpub_info[0]["descriptors"][0]["desc"], pub_desc)130assert_equal(xpub_info[0]["descriptors"][0]["active"], False)132def test_ranged_multisig(self):133self.log.info("HD keys of a multisig appear in gethdkeys")135def_wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name)136outside_wallet_xpub = def_wallet.gethdkeys()[0]["xpub"]138self.nodes[0].createwallet("ranged_multisig")139wallet = self.nodes[0].get_wallet_rpc("ranged_multisig")141hdkeys_info = wallet.gethdkeys(private=True)142assert_equal(len(hdkeys_info), 1)143within_wallet_xprv = hdkeys_info[0]["xprv"]144within_wallet_xpub = hdkeys_info[0]["xpub"]146prv_multi_desc = descsum_create(f"wsh(multi(2,{within_wallet_xprv}/*,{outside_wallet_xpub}/*))")147pub_multi_desc = descsum_create(f"wsh(multi(2,{within_wallet_xpub}/*,{outside_wallet_xpub}/*))")148assert_equal(wallet.importdescriptors([{"desc": prv_multi_desc, "timestamp": "now"}])[0]["success"], True)150rpcs_req_resp = [[False, wallet.gethdkeys()], [True, wallet.gethdkeys(private=True)]]151for rpc_req_resp in rpcs_req_resp:152requested_private, hdkeys_response = rpc_req_resp153assert_equal(len(hdkeys_response), 2)155for hdkeys_info in hdkeys_response:156if hdkeys_info["xpub"] == within_wallet_xpub:157assert_equal(hdkeys_info["has_private"], True)158if requested_private:159assert_equal(hdkeys_info["xprv"], within_wallet_xprv)160else:161assert_equal("xprv" not in hdkeys_info, True)162assert_greater_than(len(hdkeys_info["descriptors"]), 1) # within wallet xpub by default is part of multiple descriptors163found_desc = next((d for d in hdkeys_info["descriptors"] if d["desc"] == pub_multi_desc), None)164elif hdkeys_info["xpub"] == outside_wallet_xpub:165assert_equal(hdkeys_info["has_private"], False)166assert_equal("xprv" not in hdkeys_info, True)167assert_equal(len(hdkeys_info["descriptors"]), 1) # outside wallet xpub is part of only the imported descriptor168found_desc = hdkeys_info["descriptors"][0]169else:170assert False172assert_equal(found_desc["desc"], pub_multi_desc)173assert_equal(found_desc["active"], False)175def test_mixed_multisig(self):176self.log.info("Non-HD keys of a multisig do not appear in gethdkeys")177def_wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name)178self.nodes[0].createwallet("single_multisig")179wallet = self.nodes[0].get_wallet_rpc("single_multisig")181xpub = wallet.gethdkeys()[0]["xpub"]182xprv = wallet.gethdkeys(private=True)[0]["xprv"]183pub = def_wallet.getaddressinfo(def_wallet.getnewaddress())["pubkey"]185prv_multi_desc = descsum_create(f"wsh(multi(2,{xprv},{pub}))")186pub_multi_desc = descsum_create(f"wsh(multi(2,{xpub},{pub}))")187import_res = wallet.importdescriptors([{"desc": prv_multi_desc, "timestamp": "now"}])188assert_equal(import_res[0]["success"], True)190xpub_info = wallet.gethdkeys()191assert_equal(len(xpub_info), 1)192assert_equal(xpub_info[0]["xpub"], xpub)193found_desc = next((d for d in xpub_info[0]["descriptors"] if d["desc"] == pub_multi_desc), None)194assert found_desc is not None195assert_equal(found_desc["active"], False)198if __name__ == '__main__':199WalletGetHDKeyTest(__file__).main()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.