mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 20:42:59 +01:00
ad25d54300
e6cf0ed92de31a5ac35a271b0da8f0a8364d1175 wallet, rpc: listdescriptors does not need unlocked (Andrew Chow) 3280704886b60644d103a5eb310691c003a39328 Pass in DescriptorCache to ToNormalizedString (Andrew Chow) 7a26ff10c2f2e139fbc63e2f37fb33ea4efae088 Change DescriptorImpl::ToStringHelper to use an enum (Andrew Chow) 75530c93a83f3e94bcb78b6aa463c5570c1e737e Remove priv option for ToNormalizedString (Andrew Chow) 74fede3b8ba69e2cc82c617cdf406ab79df58825 wallet: Upgrade existing descriptor caches (Andrew Chow) 432ba9e5434da90d2cf680f23e8c7b7164c9f945 wallet: Store last hardened xpub cache (Andrew Chow) d87b544b834077f102724415e0fada6ee8b2def2 descriptors: Cache last hardened xpub (Andrew Chow) cacc3910989c4f3d7afa530dbab042461426abce Move DescriptorCache writing to WalletBatch (Andrew Chow) 0b4c8ef75cd03c8f0a8cfadb47e0fbcabe3c5e59 Refactor Cache merging and writing (Andrew Chow) 976b53b085d681645fd3a008fe382de85647e29f Revert "Cache parent xpub inside of BIP32PubkeyProvider" (Andrew Chow) Pull request description: Currently fetching a normalized descriptor requires the wallet to be unlocked as it needs the private keys to derive the last hardened xpub. This is not very user friendly as normalized descriptors shouldn't require and don't involve the private keys except for derivation. We solve this problem by caching the last hardened xpub (which has to be derived at some point when generating the address pool). However the last hardened xpub was not already being cached. We only cached the immediate parent xpub and derived child keys. For example, with a descriptor derivation path of `/84'/0'/0'/0/*`, the parent xpub that is cached is `m/84'/0'/0'/0`, and the child keys of `m/84'/0'/0'/0/i` (note that child keys would not be cached in this case). This parent xpub is not suitable for the normalized descriptor form as we want the key at `m/84'/0'/0'`. So this PR adds another field to `DescriptorCache` to cache the last hardened xpub so that we can use them for normalized descriptors. Since `DescriptorCache` is changing, existing descriptor wallets need to be upgraded to use this new cache. The upgrade will occur in the background either at loading time (if the wallet is not encrypted) or at unlocking time in the same manner that `UpgradeKeyMetadata` operates. It will use a new wallet flag `WALLET_FLAG_LAST_HARDENED_XPUB_CACHED` to indicate whether the descriptor wallet has the last hardened xpub cache. Lastly `listdescriptors` will not require the wallet to be locked and `getaddressinfo`'s `parent_desc` will always be output (assuming the upgrade has occurred). ACKs for top commit: fjahr: tACK e6cf0ed92de31a5ac35a271b0da8f0a8364d1175 S3RK: reACK e6cf0ed jonatack: Semi ACK e6cf0ed92de31a5ac35a271b0da8f0a8364d1175 reviewed, debug-built and ran unit tests and some of the descriptor functional tests at each commit. I'm not very familiar with this code and it could be clearer to the uninitiated IMHO, so I'm not confident enough to give a full ACK. Various minor suggestions follow, most of them for readability, feel free to pick and choose. meshcollider: Code review + functional test run ACK e6cf0ed92de31a5ac35a271b0da8f0a8364d1175 Tree-SHA512: ac27aade8644525cd65bfcaf27ff32afb974085b1451faf4ff68c6671a690bd6a41d4f39a33cbf461ae0fbe85995c0a4c08dbd36171da1c1d2a1d00053ad298d
100 lines
4.0 KiB
Python
Executable File
100 lines
4.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# Copyright (c) 2014-2021 The Bitcoin Core developers
|
|
# Distributed under the MIT software license, see the accompanying
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
"""Test the listdescriptors RPC."""
|
|
|
|
from test_framework.descriptors import (
|
|
descsum_create
|
|
)
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
from test_framework.util import (
|
|
assert_equal,
|
|
assert_raises_rpc_error,
|
|
)
|
|
|
|
|
|
class ListDescriptorsTest(BitcoinTestFramework):
|
|
def set_test_params(self):
|
|
self.num_nodes = 1
|
|
|
|
def skip_test_if_missing_module(self):
|
|
self.skip_if_no_wallet()
|
|
self.skip_if_no_sqlite()
|
|
|
|
# do not create any wallet by default
|
|
def init_wallet(self, i):
|
|
return
|
|
|
|
def run_test(self):
|
|
node = self.nodes[0]
|
|
assert_raises_rpc_error(-18, 'No wallet is loaded.', node.listdescriptors)
|
|
|
|
if self.is_bdb_compiled():
|
|
self.log.info('Test that the command is not available for legacy wallets.')
|
|
node.createwallet(wallet_name='w1', descriptors=False)
|
|
assert_raises_rpc_error(-4, 'listdescriptors is not available for non-descriptor wallets', node.listdescriptors)
|
|
|
|
self.log.info('Test the command for empty descriptors wallet.')
|
|
node.createwallet(wallet_name='w2', blank=True, descriptors=True)
|
|
assert_equal(0, len(node.get_wallet_rpc('w2').listdescriptors()['descriptors']))
|
|
|
|
self.log.info('Test the command for a default descriptors wallet.')
|
|
node.createwallet(wallet_name='w3', descriptors=True)
|
|
result = node.get_wallet_rpc('w3').listdescriptors()
|
|
assert_equal("w3", result['wallet_name'])
|
|
assert_equal(2, len(result['descriptors']))
|
|
assert_equal(2, len([d for d in result['descriptors'] if d['active']]))
|
|
assert_equal(1, len([d for d in result['descriptors'] if d['internal']]))
|
|
for item in result['descriptors']:
|
|
assert item['desc'] != ''
|
|
assert item['next'] == 0
|
|
assert item['range'] == [0, 0]
|
|
assert item['timestamp'] is not None
|
|
|
|
self.log.info('Test descriptors with hardened derivations are listed in importable form.')
|
|
xprv = 'tprv8ZgxMBicQKsPeuVhWwi6wuMQGfPKi9Li5GtX35jVNknACgqe3CY4g5xgkfDDJcmtF7o1QnxWDRYw4H5P26PXq7sbcUkEqeR4fg3Kxp2tigg'
|
|
xpub_acc = 'tpubDCMVLhErorrAGfApiJSJzEKwqeaf2z3NrkVMxgYQjZLzMjXMBeRw2muGNYbvaekAE8rUFLftyEar4LdrG2wXyyTJQZ26zptmeTEjPTaATts'
|
|
hardened_path = '/84\'/1\'/0\''
|
|
wallet = node.get_wallet_rpc('w2')
|
|
wallet.importdescriptors([{
|
|
'desc': descsum_create('pkh(' + xprv + hardened_path + '/0/*)'),
|
|
'timestamp': 1296688602,
|
|
}])
|
|
expected = {
|
|
'wallet_name': 'w2',
|
|
'descriptors': [
|
|
{'desc': descsum_create('pkh([80002067' + hardened_path + ']' + xpub_acc + '/0/*)'),
|
|
'timestamp': 1296688602,
|
|
'active': False,
|
|
'range': [0, 0],
|
|
'next': 0},
|
|
],
|
|
}
|
|
assert_equal(expected, wallet.listdescriptors())
|
|
|
|
self.log.info("Test listdescriptors with encrypted wallet")
|
|
wallet.encryptwallet("pass")
|
|
assert_equal(expected, wallet.listdescriptors())
|
|
|
|
self.log.info('Test non-active non-range combo descriptor')
|
|
node.createwallet(wallet_name='w4', blank=True, descriptors=True)
|
|
wallet = node.get_wallet_rpc('w4')
|
|
wallet.importdescriptors([{
|
|
'desc': descsum_create('combo(' + node.get_deterministic_priv_key().key + ')'),
|
|
'timestamp': 1296688602,
|
|
}])
|
|
expected = {
|
|
'wallet_name': 'w4',
|
|
'descriptors': [
|
|
{'active': False,
|
|
'desc': 'combo(038af19f35924e37ad7c3c8045d1e19b9b90b7310e08b892e620c253a102fe49f0)#2j2j0825',
|
|
'timestamp': 1296688602},
|
|
]
|
|
}
|
|
assert_equal(expected, wallet.listdescriptors())
|
|
|
|
|
|
if __name__ == '__main__':
|
|
ListDescriptorsTest().main()
|