wallet_listdescriptors.py
1 #!/usr/bin/env python3 2 # Copyright (c) 2014-2022 The Bitcoin Core developers 3 # Distributed under the MIT software license, see the accompanying 4 # file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 """Test the listdescriptors RPC.""" 6 7 from test_framework.blocktools import ( 8 TIME_GENESIS_BLOCK, 9 ) 10 from test_framework.descriptors import ( 11 descsum_create, 12 ) 13 from test_framework.test_framework import BitcoinTestFramework 14 from test_framework.util import ( 15 assert_not_equal, 16 assert_equal, 17 assert_raises_rpc_error, 18 ) 19 20 21 class ListDescriptorsTest(BitcoinTestFramework): 22 def set_test_params(self): 23 self.num_nodes = 1 24 25 def skip_test_if_missing_module(self): 26 self.skip_if_no_wallet() 27 28 # do not create any wallet by default 29 def init_wallet(self, *, node): 30 return 31 32 def run_test(self): 33 node = self.nodes[0] 34 assert_raises_rpc_error(-18, 'No wallet is loaded.', node.listdescriptors) 35 36 self.log.info('Test the command for empty descriptors wallet.') 37 node.createwallet(wallet_name='w2', blank=True, descriptors=True) 38 assert_equal(0, len(node.get_wallet_rpc('w2').listdescriptors()['descriptors'])) 39 40 self.log.info('Test the command for a default descriptors wallet.') 41 node.createwallet(wallet_name='w3', descriptors=True) 42 result = node.get_wallet_rpc('w3').listdescriptors() 43 assert_equal("w3", result['wallet_name']) 44 assert_equal(8, len(result['descriptors'])) 45 assert_equal(8, len([d for d in result['descriptors'] if d['active']])) 46 assert_equal(4, len([d for d in result['descriptors'] if d['internal']])) 47 for item in result['descriptors']: 48 assert_not_equal(item['desc'], '') 49 assert item['next_index'] == 0 50 assert item['range'] == [0, 0] 51 assert item['timestamp'] is not None 52 53 self.log.info('Test that descriptor strings are returned in lexicographically sorted order.') 54 descriptor_strings = [descriptor['desc'] for descriptor in result['descriptors']] 55 assert_equal(descriptor_strings, sorted(descriptor_strings)) 56 57 self.log.info('Test descriptors with hardened derivations are listed in importable form.') 58 xprv = 'tprv8ZgxMBicQKsPeuVhWwi6wuMQGfPKi9Li5GtX35jVNknACgqe3CY4g5xgkfDDJcmtF7o1QnxWDRYw4H5P26PXq7sbcUkEqeR4fg3Kxp2tigg' 59 xpub_acc = 'tpubDCMVLhErorrAGfApiJSJzEKwqeaf2z3NrkVMxgYQjZLzMjXMBeRw2muGNYbvaekAE8rUFLftyEar4LdrG2wXyyTJQZ26zptmeTEjPTaATts' 60 hardened_path = '/84h/1h/0h' 61 wallet = node.get_wallet_rpc('w2') 62 wallet.importdescriptors([{ 63 'desc': descsum_create('wpkh(' + xprv + hardened_path + '/0/*)'), 64 'timestamp': TIME_GENESIS_BLOCK, 65 }]) 66 expected = { 67 'wallet_name': 'w2', 68 'descriptors': [ 69 {'desc': descsum_create('wpkh([80002067' + hardened_path + ']' + xpub_acc + '/0/*)'), 70 'timestamp': TIME_GENESIS_BLOCK, 71 'active': False, 72 'range': [0, 0], 73 'next': 0, 74 'next_index': 0}, 75 ], 76 } 77 assert_equal(expected, wallet.listdescriptors()) 78 assert_equal(expected, wallet.listdescriptors(False)) 79 80 self.log.info('Test list private descriptors') 81 expected_private = { 82 'wallet_name': 'w2', 83 'descriptors': [ 84 {'desc': descsum_create('wpkh(' + xprv + hardened_path + '/0/*)'), 85 'timestamp': TIME_GENESIS_BLOCK, 86 'active': False, 87 'range': [0, 0], 88 'next': 0, 89 'next_index': 0}, 90 ], 91 } 92 assert_equal(expected_private, wallet.listdescriptors(True)) 93 94 self.log.info("Test listdescriptors with encrypted wallet") 95 wallet.encryptwallet("pass") 96 assert_equal(expected, wallet.listdescriptors()) 97 98 self.log.info('Test list private descriptors with encrypted wallet') 99 assert_raises_rpc_error(-13, 'Please enter the wallet passphrase with walletpassphrase first.', wallet.listdescriptors, True) 100 wallet.walletpassphrase(passphrase="pass", timeout=1000000) 101 assert_equal(expected_private, wallet.listdescriptors(True)) 102 103 self.log.info('Test list private descriptors with watch-only wallet') 104 node.createwallet(wallet_name='watch-only', descriptors=True, disable_private_keys=True) 105 watch_only_wallet = node.get_wallet_rpc('watch-only') 106 watch_only_wallet.importdescriptors([{ 107 'desc': descsum_create('wpkh(' + xpub_acc + ')'), 108 'timestamp': TIME_GENESIS_BLOCK, 109 }]) 110 assert_raises_rpc_error(-4, 'Can\'t get descriptor string', watch_only_wallet.listdescriptors, True) 111 112 self.log.info('Test non-active non-range combo descriptor') 113 node.createwallet(wallet_name='w4', blank=True, descriptors=True) 114 wallet = node.get_wallet_rpc('w4') 115 wallet.importdescriptors([{ 116 'desc': descsum_create('combo(' + node.get_deterministic_priv_key().key + ')'), 117 'timestamp': TIME_GENESIS_BLOCK, 118 }]) 119 expected = { 120 'wallet_name': 'w4', 121 'descriptors': [ 122 {'active': False, 123 'desc': 'combo(0227d85ba011276cf25b51df6a188b75e604b38770a462b2d0e9fb2fc839ef5d3f)#np574htj', 124 'timestamp': TIME_GENESIS_BLOCK}, 125 ] 126 } 127 assert_equal(expected, wallet.listdescriptors()) 128 129 130 if __name__ == '__main__': 131 ListDescriptorsTest(__file__).main()