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 createwalletdescriptor RPC."""7from test_framework.descriptors import descsum_create8from test_framework.test_framework import BitcoinTestFramework9from test_framework.util import (10assert_equal,11assert_raises_rpc_error,12)13from test_framework.wallet_util import WalletUnlock16class WalletCreateDescriptorTest(BitcoinTestFramework):17def set_test_params(self):18self.setup_clean_chain = True19self.num_nodes = 121def skip_test_if_missing_module(self):22self.skip_if_no_wallet()24def run_test(self):25self.test_basic()26self.test_imported_other_keys()27self.test_encrypted()29def test_basic(self):30def_wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name)31self.nodes[0].createwallet("blank", blank=True)32wallet = self.nodes[0].get_wallet_rpc("blank")34xpub_info = def_wallet.gethdkeys(private=True)35xpub = xpub_info[0]["xpub"]36xprv = xpub_info[0]["xprv"]37expected_descs = []38for desc in def_wallet.listdescriptors()["descriptors"]:39if desc["desc"].startswith("wpkh("):40expected_descs.append(desc["desc"])42assert_raises_rpc_error(-5, "Unable to determine which HD key to use from active descriptors. Please specify with 'hdkey'", wallet.createwalletdescriptor, "bech32")43assert_raises_rpc_error(-5, f"Private key for {xpub} is not known", wallet.createwalletdescriptor, type="bech32", hdkey=xpub)45self.log.info("Test createwalletdescriptor after importing active descriptor to blank wallet")46# Import one active descriptor47assert_equal(wallet.importdescriptors([{"desc": descsum_create(f"pkh({xprv}/44h/2h/0h/0/0/*)"), "timestamp": "now", "active": True}])[0]["success"], True)48assert_equal(len(wallet.listdescriptors()["descriptors"]), 1)49assert_equal(len(wallet.gethdkeys()), 1)51new_descs = wallet.createwalletdescriptor("bech32")["descs"]52assert_equal(len(new_descs), 2)53assert_equal(len(wallet.gethdkeys()), 1)54assert_equal(new_descs, expected_descs)56self.log.info("Test descriptor creation options")57old_descs = set([(d["desc"], d["active"], d["internal"]) for d in wallet.listdescriptors(private=True)["descriptors"]])58wallet.createwalletdescriptor(type="bech32m", internal=False)59curr_descs = set([(d["desc"], d["active"], d["internal"]) for d in wallet.listdescriptors(private=True)["descriptors"]])60new_descs = list(curr_descs - old_descs)61assert_equal(len(new_descs), 1)62assert_equal(len(wallet.gethdkeys()), 1)63assert_equal(new_descs[0][0], descsum_create(f"tr({xprv}/86h/1h/0h/0/*)"))64assert_equal(new_descs[0][1], True)65assert_equal(new_descs[0][2], False)67old_descs = curr_descs68wallet.createwalletdescriptor(type="bech32m", internal=True)69curr_descs = set([(d["desc"], d["active"], d["internal"]) for d in wallet.listdescriptors(private=True)["descriptors"]])70new_descs = list(curr_descs - old_descs)71assert_equal(len(new_descs), 1)72assert_equal(len(wallet.gethdkeys()), 1)73assert_equal(new_descs[0][0], descsum_create(f"tr({xprv}/86h/1h/0h/1/*)"))74assert_equal(new_descs[0][1], True)75assert_equal(new_descs[0][2], True)77def test_imported_other_keys(self):78self.log.info("Test createwalletdescriptor with multiple keys in active descriptors")79def_wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name)80self.nodes[0].createwallet("multiple_keys")81wallet = self.nodes[0].get_wallet_rpc("multiple_keys")83wallet_xpub = wallet.gethdkeys()[0]["xpub"]85xpub_info = def_wallet.gethdkeys(private=True)86xpub = xpub_info[0]["xpub"]87xprv = xpub_info[0]["xprv"]89assert_equal(wallet.importdescriptors([{"desc": descsum_create(f"wpkh({xprv}/0/0/*)"), "timestamp": "now", "active": True}])[0]["success"], True)90assert_equal(len(wallet.gethdkeys()), 2)92assert_raises_rpc_error(-5, "Unable to determine which HD key to use from active descriptors. Please specify with 'hdkey'", wallet.createwalletdescriptor, "bech32")93assert_raises_rpc_error(-4, "Descriptor already exists", wallet.createwalletdescriptor, type="bech32m", hdkey=wallet_xpub)94assert_raises_rpc_error(-5, "Unable to parse HD key. Please provide a valid xpub", wallet.createwalletdescriptor, type="bech32m", hdkey=xprv)96# Able to replace tr() descriptor with other hd key97wallet.createwalletdescriptor(type="bech32m", hdkey=xpub)99def test_encrypted(self):100self.log.info("Test createwalletdescriptor with encrypted wallets")101def_wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name)102self.nodes[0].createwallet("encrypted", blank=True, passphrase="pass")103wallet = self.nodes[0].get_wallet_rpc("encrypted")105xpub_info = def_wallet.gethdkeys(private=True)106xprv = xpub_info[0]["xprv"]108with WalletUnlock(wallet, "pass"):109assert_equal(wallet.importdescriptors([{"desc": descsum_create(f"wpkh({xprv}/0/0/*)"), "timestamp": "now", "active": True}])[0]["success"], True)110assert_equal(len(wallet.gethdkeys()), 1)112assert_raises_rpc_error(-13, "Error: Please enter the wallet passphrase with walletpassphrase first.", wallet.createwalletdescriptor, type="bech32m")114with WalletUnlock(wallet, "pass"):115wallet.createwalletdescriptor(type="bech32m")119if __name__ == '__main__':120WalletCreateDescriptorTest(__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.