Merge bitcoin/bitcoin#32594: wallet, rpc: Return normalized descriptor in parent_descs
0def84d407test: Verify parent_desc in RPCs (Ava Chow)2554cee988test: Enable default wallet for wallet_descriptor.py (Ava Chow)3fc9d9f241wallet, rpc: Push the normalized parent descriptor (Ava Chow) Pull request description: Instead of prividing the descriptor string as stored in the db, use the normalized descriptor as is done for getaddressinfo's parent_desc field. Split from #32489 ACKs for top commit: Sjors: re-utACK0def84d407rkrux: ACK0def84d407w0xlt: reACK https://github.com/bitcoin/bitcoin/pull/32594/commits/0def84d407facd319b52826d013cad0d5fc8dbf5 Tree-SHA512: 575c5b545d6f0aa7e135696b7a55c004e754fca4dd35dd9cf71b0b45b49a2e86e7b20570e768534d587005953bb893645379ec1ba4f98cfd26811f9c2f17de2d
This commit is contained in:
2 files changed
+75
-19
No files matched your search
@@ -109,7 +109,10 @@ void PushParentDescriptors(const CWallet& wallet, const CScript& script_pubkey,
|
|||||||
{
|
{
|
||||||
UniValue parent_descs(UniValue::VARR);
|
UniValue parent_descs(UniValue::VARR);
|
||||||
for (const auto& desc: wallet.GetWalletDescriptors(script_pubkey)) {
|
for (const auto& desc: wallet.GetWalletDescriptors(script_pubkey)) {
|
||||||
parent_descs.push_back(desc.descriptor->ToString());
|
std::string desc_str;
|
||||||
|
FlatSigningProvider dummy_provider;
|
||||||
|
if (!CHECK_NONFATAL(desc.descriptor->ToNormalizedString(dummy_provider, desc_str, &desc.cache))) continue;
|
||||||
|
parent_descs.push_back(desc_str);
|
||||||
}
|
}
|
||||||
entry.pushKV("parent_descs", std::move(parent_descs));
|
entry.pushKV("parent_descs", std::move(parent_descs));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
from test_framework.blocktools import COINBASE_MATURITY
|
from test_framework.blocktools import COINBASE_MATURITY
|
||||||
from test_framework.test_framework import BitcoinTestFramework
|
from test_framework.test_framework import BitcoinTestFramework
|
||||||
from test_framework.util import (
|
from test_framework.util import (
|
||||||
@@ -24,20 +26,70 @@ class WalletDescriptorTest(BitcoinTestFramework):
|
|||||||
self.setup_clean_chain = True
|
self.setup_clean_chain = True
|
||||||
self.num_nodes = 1
|
self.num_nodes = 1
|
||||||
self.extra_args = [['-keypool=100']]
|
self.extra_args = [['-keypool=100']]
|
||||||
self.wallet_names = []
|
|
||||||
|
|
||||||
def skip_test_if_missing_module(self):
|
def skip_test_if_missing_module(self):
|
||||||
self.skip_if_no_wallet()
|
self.skip_if_no_wallet()
|
||||||
self.skip_if_no_py_sqlite3()
|
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])
|
||||||
|
|
||||||
|
txs = wallet.listsinceblock(since_block)["transactions"]
|
||||||
|
assert_equal(len(txs), 1)
|
||||||
|
assert_equal(txs[0]["parent_descs"], [parent_desc])
|
||||||
|
|
||||||
|
tx = wallet.gettransaction(txid=txid, verbose=True)
|
||||||
|
assert_equal(tx["details"][0]["parent_descs"], [parent_desc])
|
||||||
|
|
||||||
|
wallet.unloadwallet()
|
||||||
|
|
||||||
def run_test(self):
|
def run_test(self):
|
||||||
|
self.generate(self.nodes[0], COINBASE_MATURITY + 1)
|
||||||
|
|
||||||
# Make a descriptor wallet
|
# Make a descriptor wallet
|
||||||
self.log.info("Making a descriptor wallet")
|
self.log.info("Making a descriptor wallet")
|
||||||
self.nodes[0].createwallet(wallet_name="desc1")
|
self.nodes[0].createwallet(wallet_name="desc1")
|
||||||
|
wallet = self.nodes[0].get_wallet_rpc("desc1")
|
||||||
|
|
||||||
# A descriptor wallet should have 100 addresses * 4 types = 400 keys
|
# A descriptor wallet should have 100 addresses * 4 types = 400 keys
|
||||||
self.log.info("Checking wallet info")
|
self.log.info("Checking wallet info")
|
||||||
wallet_info = self.nodes[0].getwalletinfo()
|
wallet_info = wallet.getwalletinfo()
|
||||||
assert_equal(wallet_info['format'], 'sqlite')
|
assert_equal(wallet_info['format'], 'sqlite')
|
||||||
assert_equal(wallet_info['keypoolsize'], 400)
|
assert_equal(wallet_info['keypoolsize'], 400)
|
||||||
assert_equal(wallet_info['keypoolsize_hd_internal'], 400)
|
assert_equal(wallet_info['keypoolsize_hd_internal'], 400)
|
||||||
@@ -45,44 +97,44 @@ class WalletDescriptorTest(BitcoinTestFramework):
|
|||||||
|
|
||||||
# Check that getnewaddress works
|
# Check that getnewaddress works
|
||||||
self.log.info("Test that getnewaddress and getrawchangeaddress work")
|
self.log.info("Test that getnewaddress and getrawchangeaddress work")
|
||||||
addr = self.nodes[0].getnewaddress("", "legacy")
|
addr = wallet.getnewaddress("", "legacy")
|
||||||
addr_info = self.nodes[0].getaddressinfo(addr)
|
addr_info = wallet.getaddressinfo(addr)
|
||||||
assert addr_info['desc'].startswith('pkh(')
|
assert addr_info['desc'].startswith('pkh(')
|
||||||
assert_equal(addr_info['hdkeypath'], 'm/44h/1h/0h/0/0')
|
assert_equal(addr_info['hdkeypath'], 'm/44h/1h/0h/0/0')
|
||||||
|
|
||||||
addr = self.nodes[0].getnewaddress("", "p2sh-segwit")
|
addr = wallet.getnewaddress("", "p2sh-segwit")
|
||||||
addr_info = self.nodes[0].getaddressinfo(addr)
|
addr_info = wallet.getaddressinfo(addr)
|
||||||
assert addr_info['desc'].startswith('sh(wpkh(')
|
assert addr_info['desc'].startswith('sh(wpkh(')
|
||||||
assert_equal(addr_info['hdkeypath'], 'm/49h/1h/0h/0/0')
|
assert_equal(addr_info['hdkeypath'], 'm/49h/1h/0h/0/0')
|
||||||
|
|
||||||
addr = self.nodes[0].getnewaddress("", "bech32")
|
addr = wallet.getnewaddress("", "bech32")
|
||||||
addr_info = self.nodes[0].getaddressinfo(addr)
|
addr_info = wallet.getaddressinfo(addr)
|
||||||
assert addr_info['desc'].startswith('wpkh(')
|
assert addr_info['desc'].startswith('wpkh(')
|
||||||
assert_equal(addr_info['hdkeypath'], 'm/84h/1h/0h/0/0')
|
assert_equal(addr_info['hdkeypath'], 'm/84h/1h/0h/0/0')
|
||||||
|
|
||||||
addr = self.nodes[0].getnewaddress("", "bech32m")
|
addr = wallet.getnewaddress("", "bech32m")
|
||||||
addr_info = self.nodes[0].getaddressinfo(addr)
|
addr_info = wallet.getaddressinfo(addr)
|
||||||
assert addr_info['desc'].startswith('tr(')
|
assert addr_info['desc'].startswith('tr(')
|
||||||
assert_equal(addr_info['hdkeypath'], 'm/86h/1h/0h/0/0')
|
assert_equal(addr_info['hdkeypath'], 'm/86h/1h/0h/0/0')
|
||||||
|
|
||||||
# Check that getrawchangeaddress works
|
# Check that getrawchangeaddress works
|
||||||
addr = self.nodes[0].getrawchangeaddress("legacy")
|
addr = wallet.getrawchangeaddress("legacy")
|
||||||
addr_info = self.nodes[0].getaddressinfo(addr)
|
addr_info = wallet.getaddressinfo(addr)
|
||||||
assert addr_info['desc'].startswith('pkh(')
|
assert addr_info['desc'].startswith('pkh(')
|
||||||
assert_equal(addr_info['hdkeypath'], 'm/44h/1h/0h/1/0')
|
assert_equal(addr_info['hdkeypath'], 'm/44h/1h/0h/1/0')
|
||||||
|
|
||||||
addr = self.nodes[0].getrawchangeaddress("p2sh-segwit")
|
addr = wallet.getrawchangeaddress("p2sh-segwit")
|
||||||
addr_info = self.nodes[0].getaddressinfo(addr)
|
addr_info = wallet.getaddressinfo(addr)
|
||||||
assert addr_info['desc'].startswith('sh(wpkh(')
|
assert addr_info['desc'].startswith('sh(wpkh(')
|
||||||
assert_equal(addr_info['hdkeypath'], 'm/49h/1h/0h/1/0')
|
assert_equal(addr_info['hdkeypath'], 'm/49h/1h/0h/1/0')
|
||||||
|
|
||||||
addr = self.nodes[0].getrawchangeaddress("bech32")
|
addr = wallet.getrawchangeaddress("bech32")
|
||||||
addr_info = self.nodes[0].getaddressinfo(addr)
|
addr_info = wallet.getaddressinfo(addr)
|
||||||
assert addr_info['desc'].startswith('wpkh(')
|
assert addr_info['desc'].startswith('wpkh(')
|
||||||
assert_equal(addr_info['hdkeypath'], 'm/84h/1h/0h/1/0')
|
assert_equal(addr_info['hdkeypath'], 'm/84h/1h/0h/1/0')
|
||||||
|
|
||||||
addr = self.nodes[0].getrawchangeaddress("bech32m")
|
addr = wallet.getrawchangeaddress("bech32m")
|
||||||
addr_info = self.nodes[0].getaddressinfo(addr)
|
addr_info = wallet.getaddressinfo(addr)
|
||||||
assert addr_info['desc'].startswith('tr(')
|
assert addr_info['desc'].startswith('tr(')
|
||||||
assert_equal(addr_info['hdkeypath'], 'm/86h/1h/0h/1/0')
|
assert_equal(addr_info['hdkeypath'], 'm/86h/1h/0h/1/0')
|
||||||
|
|
||||||
@@ -216,6 +268,7 @@ class WalletDescriptorTest(BitcoinTestFramework):
|
|||||||
conn.close()
|
conn.close()
|
||||||
assert_raises_rpc_error(-4, "Unexpected legacy entry in descriptor wallet found.", self.nodes[0].loadwallet, "crashme")
|
assert_raises_rpc_error(-4, "Unexpected legacy entry in descriptor wallet found.", self.nodes[0].loadwallet, "crashme")
|
||||||
|
|
||||||
|
self.test_parent_descriptors()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
WalletDescriptorTest(__file__).main()
|
WalletDescriptorTest(__file__).main()
|
||||||
Reference in new issue
Block a user