wallet_listdescriptors.py
1 #!/usr/bin/env python3 2 # Copyright (c) 2014-present 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) 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') 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', 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 private descriptor string for watch-only wallets', 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) 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 self.log.info('Test taproot descriptor do not have mixed hardened derivation marker') 130 node.createwallet(wallet_name='w5', descriptors=True, disable_private_keys=True) 131 wallet = node.get_wallet_rpc('w5') 132 wallet.importdescriptors([{ 133 'desc': "tr([1dce71b2/48'/1'/0'/2']tpubDEeP3GefjqbaDTTaVAF5JkXWhoFxFDXQ9KuhVrMBViFXXNR2B3Lvme2d2AoyiKfzRFZChq2AGMNbU1qTbkBMfNv7WGVXLt2pnYXY87gXqcs/0/*,and_v(v:pk([c658b283/48'/1'/0'/2']tpubDFL5wzgPBYK5pZ2Kh1T8qrxnp43kjE5CXfguZHHBrZSWpkfASy5rVfj7prh11XdqkC1P3kRwUPBeX7AHN8XBNx8UwiprnFnEm5jyswiRD4p/0/*),older(65535)))#xl20m6md", 134 'timestamp': TIME_GENESIS_BLOCK, 135 }]) 136 expected = { 137 'wallet_name': 'w5', 138 'descriptors': [ 139 {'active': False, 140 'desc': 'tr([1dce71b2/48h/1h/0h/2h]tpubDEeP3GefjqbaDTTaVAF5JkXWhoFxFDXQ9KuhVrMBViFXXNR2B3Lvme2d2AoyiKfzRFZChq2AGMNbU1qTbkBMfNv7WGVXLt2pnYXY87gXqcs/0/*,and_v(v:pk([c658b283/48h/1h/0h/2h]tpubDFL5wzgPBYK5pZ2Kh1T8qrxnp43kjE5CXfguZHHBrZSWpkfASy5rVfj7prh11XdqkC1P3kRwUPBeX7AHN8XBNx8UwiprnFnEm5jyswiRD4p/0/*),older(65535)))#m4uznndk', 141 'timestamp': TIME_GENESIS_BLOCK, 142 'range': [0, 0], 143 'next': 0, 144 'next_index': 0}, 145 ] 146 } 147 assert_equal(expected, wallet.listdescriptors()) 148 149 self.log.info('Test descriptor with missing private keys') 150 node.createwallet(wallet_name='w6', blank=True) 151 wallet = node.get_wallet_rpc('w6') 152 153 expected_descs = { 154 descsum_create('tr(' + node.get_deterministic_priv_key().key + 155 ',{pk(03cdabb7f2dce7bfbd8a0b9570c6fd1e712e5d64045e9d6b517b3d5072251dc204)' + 156 ',pk([d34db33f/44h/0h/0h]tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B/0)})'), 157 descsum_create('wsh(and_v(v:ripemd160(095ff41131e5946f3c85f79e44adbcf8e27e080e),multi(1,' + node.get_deterministic_priv_key().key + 158 ',tpubD6NzVbkrYhZ4WaWSyoBvQwbpLkojyoTZPRsgXELWz3Popb3qkjcJyJUGLnL4qHHoQvao8ESaAstxYSnhyswJ76uZPStJRJCTKvosUCJZL5B/0)))'), 159 descsum_create('tr(03dff1d77f2a671c5f36183726db2341be58feae1da2deced843240f7b502ba659,pk(musig(tprv8ZgxMBicQKsPeNLUGrbv3b7qhUk1LQJZAGMuk9gVuKh9sd4BWGp1eMsehUni6qGb8bjkdwBxCbgNGdh2bYGACK5C5dRTaif9KBKGVnSezxV,tpubD6NzVbkrYhZ4XcACN3PEwNjRpR1g4tZjBVk5pdMR2B6dbd3HYhdGVZNKofAiFZd9okBserZvv58A6tBX4pE64UpXGNTSesfUW7PpW36HuKz)/7/8/*))'), 160 descsum_create('tr(03dff1d77f2a671c5f36183726db2341be58feae1da2deced843240f7b502ba659,pk(musig(tprv8ZgxMBicQKsPeNLUGrbv3b7qhUk1LQJZAGMuk9gVuKh9sd4BWGp1eMsehUni6qGb8bjkdwBxCbgNGdh2bYGACK5C5dRTaif9KBKGVnSezxV/10,tpubD6NzVbkrYhZ4XcACN3PEwNjRpR1g4tZjBVk5pdMR2B6dbd3HYhdGVZNKofAiFZd9okBserZvv58A6tBX4pE64UpXGNTSesfUW7PpW36HuKz/11)/*))'), 161 descsum_create('tr(03dff1d77f2a671c5f36183726db2341be58feae1da2deced843240f7b502ba659,{pk(musig(tpubD6NzVbkrYhZ4Wo2WcFSgSqRD9QWkGxddo6WSqsVBx7uQ8QEtM7WncKDRjhFEexK119NigyCsFygA4b7sAPQxqebyFGAZ9XVV1BtcgNzbCRR,tprv8ZgxMBicQKsPen4PGtDwURYnCtVMDejyE8vVwMGhQWfVqB2FBPdekhTacDW4vmsKTsgC1wsncVqXiZdX2YFGAnKoLXYf42M78fQJFzuDYFN)/12/*),pk(musig(tprv8ZgxMBicQKsPeNLUGrbv3b7qhUk1LQJZAGMuk9gVuKh9sd4BWGp1eMsehUni6qGb8bjkdwBxCbgNGdh2bYGACK5C5dRTaif9KBKGVnSezxV,tpubD6NzVbkrYhZ4XWb6fGPjyhgLxapUhXszv7ehQYrQWDgDX4nYWcNcbgWcM2RhYo9s2mbZcfZJ8t5LzYcr24FK79zVybsw5Qj3Rtqug8jpJMy)/13/*)})'), 162 descsum_create('tr(03dff1d77f2a671c5f36183726db2341be58feae1da2deced843240f7b502ba659,{pk(musig(tpubD6NzVbkrYhZ4Wo2WcFSgSqRD9QWkGxddo6WSqsVBx7uQ8QEtM7WncKDRjhFEexK119NigyCsFygA4b7sAPQxqebyFGAZ9XVV1BtcgNzbCRR,tpubD6NzVbkrYhZ4Wc3i6L6N1Pp7cyVeyMcdLrFGXGDGzCfdCa5F4Zs3EY46N72Ws8QDEUYBVwXfDfda2UKSseSdU1fsBegJBhGCZyxkf28bkQ6)/12/*),pk(musig(tprv8ZgxMBicQKsPeNLUGrbv3b7qhUk1LQJZAGMuk9gVuKh9sd4BWGp1eMsehUni6qGb8bjkdwBxCbgNGdh2bYGACK5C5dRTaif9KBKGVnSezxV,tpubD6NzVbkrYhZ4XWb6fGPjyhgLxapUhXszv7ehQYrQWDgDX4nYWcNcbgWcM2RhYo9s2mbZcfZJ8t5LzYcr24FK79zVybsw5Qj3Rtqug8jpJMy)/13/*)})') 163 } 164 165 descs_to_import = [] 166 for desc in expected_descs: 167 descs_to_import.append({'desc': desc, 'timestamp': TIME_GENESIS_BLOCK}) 168 169 wallet.importdescriptors(descs_to_import) 170 result = wallet.listdescriptors(True) 171 actual_descs = [d['desc'] for d in result['descriptors']] 172 173 assert_equal(len(actual_descs), len(expected_descs)) 174 for desc in actual_descs: 175 if desc not in expected_descs: 176 raise AssertionError(f"{desc} missing") 177 178 179 180 if __name__ == '__main__': 181 ListDescriptorsTest(__file__).main()