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