1
1
#!/usr/bin/env python3
2
- # Copyright (c) 2016 The Bitcoin Core developers
2
+ # Copyright (c) 2016-2025 The Dash Core developers
3
3
# Distributed under the MIT software license, see the accompanying
4
4
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
"""
15
15
from test_framework .util import (
16
16
assert_equal ,
17
17
assert_raises_rpc_error ,
18
+ get_mnemonic ,
18
19
)
19
20
20
21
@@ -29,15 +30,17 @@ def skip_test_if_missing_module(self):
29
30
def setup_network (self ):
30
31
self .add_nodes (self .num_nodes , self .extra_args )
31
32
self .start_nodes ()
32
- self .import_deterministic_coinbase_privkeys ()
33
+ self .nodes [0 ].createwallet (self .default_wallet_name , blank = True , load_on_startup = True )
34
+ self .nodes [0 ].importprivkey (privkey = self .nodes [0 ].get_deterministic_priv_key ().key , label = 'coinbase' , rescan = True )
33
35
34
36
def recover_non_hd (self ):
35
37
self .log .info ("Recover non-HD wallet to check different upgrade paths" )
36
38
node = self .nodes [0 ]
37
39
self .stop_node (0 )
38
40
shutil .copyfile (os .path .join (node .datadir , "non_hd.bak" ), os .path .join (node .datadir , self .chain , self .default_wallet_name , self .wallet_data_filename ))
39
41
self .start_node (0 )
40
- assert 'hdchainid' not in node .getwalletinfo ()
42
+ if not self .options .descriptors :
43
+ assert 'hdchainid' not in node .getwalletinfo ()
41
44
42
45
def run_test (self ):
43
46
node = self .nodes [0 ]
@@ -47,9 +50,10 @@ def run_test(self):
47
50
assert 'hdchainid' not in node .getwalletinfo ()
48
51
balance_before = node .getbalance ()
49
52
assert node .upgradetohd ()
50
- mnemonic = node .dumphdinfo ()['mnemonic' ]
51
- chainid = node .getwalletinfo ()['hdchainid' ]
52
- assert_equal (len (chainid ), 64 )
53
+ mnemonic = get_mnemonic (node )
54
+ if not self .options .descriptors :
55
+ chainid = node .getwalletinfo ()['hdchainid' ]
56
+ assert_equal (len (chainid ), 64 )
53
57
assert_equal (balance_before , node .getbalance ())
54
58
55
59
self .log .info ("Should be spendable and should use correct paths" )
@@ -82,8 +86,9 @@ def run_test(self):
82
86
83
87
self .log .info ("No mnemonic, no mnemonic passphrase, no wallet passphrase, should result in completely different keys" )
84
88
assert node .upgradetohd ()
85
- assert mnemonic != node .dumphdinfo ()['mnemonic' ]
86
- assert chainid != node .getwalletinfo ()['hdchainid' ]
89
+ assert mnemonic != get_mnemonic (node )
90
+ if not self .options .descriptors :
91
+ assert chainid != node .getwalletinfo ()['hdchainid' ]
87
92
assert_equal (balance_non_HD , node .getbalance ())
88
93
node .keypoolrefill (5 )
89
94
node .rescanblockchain ()
@@ -96,18 +101,20 @@ def run_test(self):
96
101
self .restart_node (0 , extra_args = ['-keypool=10' ])
97
102
assert node .upgradetohd ("" , "" , "" , True )
98
103
# Completely different keys, no HD coins should be recovered
99
- assert mnemonic != node .dumphdinfo ()['mnemonic' ]
100
- assert chainid != node .getwalletinfo ()['hdchainid' ]
104
+ assert mnemonic != get_mnemonic (node )
105
+ if not self .options .descriptors :
106
+ assert chainid != node .getwalletinfo ()['hdchainid' ]
101
107
assert_equal (balance_non_HD , node .getbalance ())
102
108
103
109
self .recover_non_hd ()
104
110
105
111
self .log .info ("Same mnemonic, another mnemonic passphrase, no wallet passphrase, should result in a different set of keys" )
106
112
new_mnemonic_passphrase = "somewords"
107
113
assert node .upgradetohd (mnemonic , new_mnemonic_passphrase )
108
- assert_equal (mnemonic , node .dumphdinfo ()['mnemonic' ])
109
- new_chainid = node .getwalletinfo ()['hdchainid' ]
110
- assert chainid != new_chainid
114
+ assert_equal (mnemonic , get_mnemonic (node ))
115
+ if not self .options .descriptors :
116
+ new_chainid = node .getwalletinfo ()['hdchainid' ]
117
+ assert chainid != new_chainid
111
118
assert_equal (balance_non_HD , node .getbalance ())
112
119
node .keypoolrefill (5 )
113
120
node .rescanblockchain ()
@@ -119,8 +126,9 @@ def run_test(self):
119
126
120
127
self .log .info ("Same mnemonic, another mnemonic passphrase, no wallet passphrase, should result in a different set of keys (again)" )
121
128
assert node .upgradetohd (mnemonic , new_mnemonic_passphrase )
122
- assert_equal (mnemonic , node .dumphdinfo ()['mnemonic' ])
123
- assert_equal (new_chainid , node .getwalletinfo ()['hdchainid' ])
129
+ assert_equal (mnemonic , get_mnemonic (node ))
130
+ if not self .options .descriptors :
131
+ assert_equal (new_chainid , node .getwalletinfo ()['hdchainid' ])
124
132
assert_equal (balance_non_HD , node .getbalance ())
125
133
node .keypoolrefill (5 )
126
134
node .rescanblockchain ()
@@ -132,8 +140,9 @@ def run_test(self):
132
140
133
141
self .log .info ("Same mnemonic, no mnemonic passphrase, no wallet passphrase, should recover all coins after rescan" )
134
142
assert node .upgradetohd (mnemonic )
135
- assert_equal (mnemonic , node .dumphdinfo ()['mnemonic' ])
136
- assert_equal (chainid , node .getwalletinfo ()['hdchainid' ])
143
+ assert_equal (mnemonic , get_mnemonic (node ))
144
+ if not self .options .descriptors :
145
+ assert_equal (chainid , node .getwalletinfo ()['hdchainid' ])
137
146
node .keypoolrefill (5 )
138
147
assert balance_after != node .getbalance ()
139
148
node .rescanblockchain ()
@@ -144,8 +153,9 @@ def run_test(self):
144
153
self .log .info ("Same mnemonic, no mnemonic passphrase, no wallet passphrase, large enough keepool, should recover all coins with no extra rescan" )
145
154
self .restart_node (0 , extra_args = ['-keypool=10' ])
146
155
assert node .upgradetohd (mnemonic )
147
- assert_equal (mnemonic , node .dumphdinfo ()['mnemonic' ])
148
- assert_equal (chainid , node .getwalletinfo ()['hdchainid' ])
156
+ assert_equal (mnemonic , get_mnemonic (node ))
157
+ if not self .options .descriptors :
158
+ assert_equal (chainid , node .getwalletinfo ()['hdchainid' ])
149
159
# All coins should be recovered
150
160
assert_equal (balance_after , node .getbalance ())
151
161
@@ -154,8 +164,9 @@ def run_test(self):
154
164
self .log .info ("Same mnemonic, no mnemonic passphrase, no wallet passphrase, large enough keepool, rescan is skipped initially, should recover all coins after rescanblockchain" )
155
165
self .restart_node (0 , extra_args = ['-keypool=10' ])
156
166
assert node .upgradetohd (mnemonic , "" , "" , False )
157
- assert_equal (mnemonic , node .dumphdinfo ()['mnemonic' ])
158
- assert_equal (chainid , node .getwalletinfo ()['hdchainid' ])
167
+ assert_equal (mnemonic , get_mnemonic (node ))
168
+ if not self .options .descriptors :
169
+ assert_equal (chainid , node .getwalletinfo ()['hdchainid' ])
159
170
assert balance_after != node .getbalance ()
160
171
node .rescanblockchain ()
161
172
# All coins should be recovered
@@ -171,8 +182,9 @@ def run_test(self):
171
182
self .start_node (0 , extra_args = ['-rescan' ])
172
183
assert_raises_rpc_error (- 13 , "Error: Please enter the wallet passphrase with walletpassphrase first." , node .dumphdinfo )
173
184
node .walletpassphrase (walletpass , 100 )
174
- assert_equal (mnemonic , node .dumphdinfo ()['mnemonic' ])
175
- assert_equal (chainid , node .getwalletinfo ()['hdchainid' ])
185
+ assert_equal (mnemonic , get_mnemonic (node ))
186
+ if not self .options .descriptors :
187
+ assert_equal (chainid , node .getwalletinfo ()['hdchainid' ])
176
188
# Note: wallet encryption results in additional keypool topup,
177
189
# so we can't compare new balance to balance_non_HD here,
178
190
# assert_equal(balance_non_HD, node.getbalance()) # won't work
@@ -191,12 +203,25 @@ def run_test(self):
191
203
node .wait_until_stopped ()
192
204
self .start_node (0 , extra_args = ['-rescan' ])
193
205
assert_raises_rpc_error (- 13 , "Error: Wallet encrypted but passphrase not supplied to RPC." , node .upgradetohd , mnemonic )
194
- assert_raises_rpc_error (- 1 , "Error: The wallet passphrase entered was incorrect" , node .upgradetohd , mnemonic , "" , "wrongpass" )
206
+ if not self .options .descriptors :
207
+ assert_raises_rpc_error (- 1 , "Error: The wallet passphrase entered was incorrect" , node .upgradetohd , mnemonic , "" , "wrongpass" )
208
+ else :
209
+ assert_raises_rpc_error (- 1 , "SetupDescriptorScriptPubKeyMans: Wallet is locked, cannot setup new descriptors" , node .upgradetohd , mnemonic , "" , "wrongpass" )
210
+ if self .options .descriptors :
211
+ # TODO - implement auto-unlock descriptor wallet
212
+ node .walletpassphrase (walletpass , 100 )
195
213
assert node .upgradetohd (mnemonic , "" , walletpass )
196
- assert_raises_rpc_error (- 13 , "Error: Please enter the wallet passphrase with walletpassphrase first." , node .dumphdinfo )
214
+ # TODO - drop it too!
215
+ if self .options .descriptors :
216
+ node .walletlock ()
217
+ if not self .options .descriptors :
218
+ assert_raises_rpc_error (- 13 , "Error: Please enter the wallet passphrase with walletpassphrase first." , node .dumphdinfo )
219
+ else :
220
+ assert_raises_rpc_error (- 13 , "Error: Please enter the wallet passphrase with walletpassphrase first." , node .listdescriptors , True )
197
221
node .walletpassphrase (walletpass , 100 )
198
- assert_equal (mnemonic , node .dumphdinfo ()['mnemonic' ])
199
- assert_equal (chainid , node .getwalletinfo ()['hdchainid' ])
222
+ assert_equal (mnemonic , get_mnemonic (node ))
223
+ if not self .options .descriptors :
224
+ assert_equal (chainid , node .getwalletinfo ()['hdchainid' ])
200
225
# Note: wallet encryption results in additional keypool topup,
201
226
# so we can't compare new balance to balance_non_HD here,
202
227
# assert_equal(balance_non_HD, node.getbalance()) # won't work
@@ -206,6 +231,28 @@ def run_test(self):
206
231
# All coins should be recovered
207
232
assert_equal (balance_after , node .getbalance ())
208
233
234
+ self .log .info ("Test upgradetohd with user defined mnemonic" )
235
+ custom_mnemonic = "similar behave slot swim scissors throw planet view ghost laugh drift calm"
236
+ # this address belongs to custom mnemonic with no passphrase
237
+ custom_address_1 = "yLpq97zZUsFQ2rdMqhcPKkYT36MoPK4Hob"
238
+ # this address belongs to custom mnemonic with passphrase "custom-passphrase"
239
+ custom_address_2 = "yYBPeZQcqgQHu9dxA5pKBWtYbK2hwfFHxf"
240
+ node .sendtoaddress (custom_address_1 , 11 )
241
+ node .sendtoaddress (custom_address_2 , 12 )
242
+ self .generate (node , 1 )
243
+
244
+ node .createwallet ("wallet-11" , blank = True )
245
+ w11 = node .get_wallet_rpc ("wallet-11" )
246
+ w11 .upgradetohd (custom_mnemonic )
247
+ assert_equal (11 , w11 .getbalance ())
248
+ w11 .unloadwallet ()
249
+
250
+ node .createwallet ("wallet-12" , blank = True )
251
+ w12 = node .get_wallet_rpc ("wallet-12" )
252
+ w12 .upgradetohd (custom_mnemonic , "custom-passphrase" )
253
+ assert_equal (12 , w12 .getbalance ())
254
+ w12 .unloadwallet ()
255
+
209
256
210
257
if __name__ == '__main__' :
211
258
WalletUpgradeToHDTest ().main ()
0 commit comments