Repository: bitcoin/bitcoin
File: test/functional/wallet_descriptor.py (Python)

Code before:
class WalletDescriptorTest(BitcoinTestFramework):
    def set_test_params(self):
        self.setup_clean_chain = True
        self.num_nodes = 1
        self.extra_args = [['-keypool=100']]
        self.wallet_names = []

    def skip_test_if_missing_module(self):
        self.skip_if_no_wallet()
        self.skip_if_no_py_sqlite3()

    def run_test(self):
        # Make a descriptor wallet
        self.log.info("Making a descriptor wallet")
        self.nodes[0].createwallet(wallet_name="desc1")

        # A descriptor wallet should have 100 addresses * 4 types = 400 keys
        self.log.info("Checking wallet info")
        wallet_info = self.nodes[0].getwalletinfo()
        assert_equal(wallet_info['format'], 'sqlite')
        assert_equal(wallet_info['keypoolsize'], 400)
        assert_equal(wallet_info['keypoolsize_hd_internal'], 400)
        assert 'keypoololdest' not in wallet_info

        # Check that getnewaddress works
        self.log.info("Test that getnewaddress and getrawchangeaddress work")
        addr = self.nodes[0].getnewaddress("", "legacy")
        addr_info = self.nodes[0].getaddressinfo(addr)
        assert addr_info['desc'].startswith('pkh(')
        assert_equal(addr_info['hdkeypath'], 'm/44h/1h/0h/0/0')

        addr = self.nodes[0].getnewaddress("", "p2sh-segwit")
        addr_info = self.nodes[0].getaddressinfo(addr)
        assert addr_info['desc'].startswith('sh(wpkh(')
        assert_equal(addr_info['hdkeypath'], 'm/49h/1h/0h/0/0')

        addr = self.nodes[0].getnewaddress("", "bech32")
        addr_info = self.nodes[0].getaddressinfo(addr)
        assert addr_info['desc'].startswith('wpkh(')
        assert_equal(addr_info['hdkeypath'], 'm/84h/1h/0h/0/0')

        addr = self.nodes[0].getnewaddress("", "bech32m")
        addr_info = self.nodes[0].getaddressinfo(addr)
        assert addr_info['desc'].startswith('tr(')
        assert_equal(addr_info['hdkeypath'], 'm/86h/1h/0h/0/0')

        # Check that getrawchangeaddress works
        addr = self.nodes[0].getrawchangeaddress("legacy")
        addr_info = self.nodes[0].getaddressinfo(addr)
        assert addr_info['desc'].startswith('pkh(')
        assert_equal(addr_info['hdkeypath'], 'm/44h/1h/0h/1/0')

Review comment (suggestion):
`r"tpub\w+?(?=/)"`

The `\w` will accept an xpub with underscore as well that is not allowed, but ok.

Code after:


class WalletDescriptorTest(BitcoinTestFramework):
    def set_test_params(self):
        self.setup_clean_chain = True
        self.num_nodes = 1
        self.extra_args = [['-keypool=100']]

    def skip_test_if_missing_module(self):
        self.skip_if_no_wallet()
        self.skip_if_no_py_sqlite3()

    def test_parent_descriptors(self):
        self.log.info("Check that parent_descs is the same for all RPCs and is normalized")
        self.nodes[0].createwallet(wallet_name="parent_descs")
        wallet = self.nodes[0].get_wallet_rpc("parent_descs")
        default_wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name)

        addr = wallet.getnewaddress()
        parent_desc = wallet.getaddressinfo(addr)["parent_desc"]

        # Verify that the parent descriptor is normalized
        # First remove the checksum
        desc_verify = parent_desc.split("#")[0]
        # Next extract the xpub
        desc_verify = re.sub(r"tpub\w+?(?=/)", "", desc_verify)
        # Extract origin info
        origin_match = re.search(r'\[([\da-fh/]+)\]', desc_verify)
        origin_part = origin_match.group(1) if origin_match else ""
        # Split on "]" for everything after the origin info
        after_origin = desc_verify.split("]", maxsplit=1)[-1]
        # Look for the hardened markers “h” inside each piece
        # We don't need to check for aspostrophe as normalization will not output aspostrophe
        found_hardened_in_origin = "h" in origin_part
        found_hardened_after_origin = "h" in after_origin
        assert_equal(found_hardened_in_origin, True)
        assert_equal(found_hardened_after_origin, False)

        # Send some coins so we can check listunspent, listtransactions, listunspent, and gettransaction
        since_block = self.nodes[0].getbestblockhash()
        txid = default_wallet.sendtoaddress(addr, 1)
        self.generate(self.nodes[0], 1)

        unspent = wallet.listunspent()
        assert_equal(len(unspent), 1)
        assert_equal(unspent[0]["parent_descs"], [parent_desc])

        txs = wallet.listtransactions()
        assert_equal(len(txs), 1)
        assert_equal(txs[0]["parent_descs"], [parent_desc])