Skip to content

Commit af4b618

Browse files
committed
test: make wallet_upgradetohd works for descriptor wallets too
1 parent 0267fb7 commit af4b618

File tree

1 file changed

+69
-26
lines changed

1 file changed

+69
-26
lines changed

test/functional/wallet_upgradetohd.py

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,36 @@ def skip_test_if_missing_module(self):
2929
def setup_network(self):
3030
self.add_nodes(self.num_nodes, self.extra_args)
3131
self.start_nodes()
32-
self.import_deterministic_coinbase_privkeys()
32+
self.nodes[0].createwallet(self.default_wallet_name, blank=True, load_on_startup=True)
33+
self.nodes[0].importprivkey(privkey=self.nodes[0].get_deterministic_priv_key().key, label='coinbase', rescan=True)
34+
35+
# TODO: remove duplicated code with wallet_mnemonicbits.py
36+
def get_mnemonic(self, node):
37+
if not self.options.descriptors:
38+
return node.dumphdinfo()["mnemonic"]
39+
40+
mnemonic = None
41+
descriptors = node.listdescriptors(True)['descriptors']
42+
for desc in descriptors:
43+
if desc['desc'][:4] == 'pkh(':
44+
if mnemonic is None:
45+
mnemonic = desc['mnemonic']
46+
else:
47+
assert_equal(mnemonic, desc['mnemonic'])
48+
elif desc['desc'][:6] == 'combo(':
49+
assert 'mnemonic' not in desc
50+
else:
51+
raise AssertionError(f"Unknown descriptor type: {desc['desc']}")
52+
return mnemonic
3353

3454
def recover_non_hd(self):
3555
self.log.info("Recover non-HD wallet to check different upgrade paths")
3656
node = self.nodes[0]
3757
self.stop_node(0)
3858
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))
3959
self.start_node(0)
40-
assert 'hdchainid' not in node.getwalletinfo()
60+
if not self.options.descriptors:
61+
assert 'hdchainid' not in node.getwalletinfo()
4162

4263
def run_test(self):
4364
node = self.nodes[0]
@@ -47,9 +68,10 @@ def run_test(self):
4768
assert 'hdchainid' not in node.getwalletinfo()
4869
balance_before = node.getbalance()
4970
assert node.upgradetohd()
50-
mnemonic = node.dumphdinfo()['mnemonic']
51-
chainid = node.getwalletinfo()['hdchainid']
52-
assert_equal(len(chainid), 64)
71+
mnemonic = self.get_mnemonic(node)
72+
if not self.options.descriptors:
73+
chainid = node.getwalletinfo()['hdchainid']
74+
assert_equal(len(chainid), 64)
5375
assert_equal(balance_before, node.getbalance())
5476

5577
self.log.info("Should be spendable and should use correct paths")
@@ -82,8 +104,9 @@ def run_test(self):
82104

83105
self.log.info("No mnemonic, no mnemonic passphrase, no wallet passphrase, should result in completely different keys")
84106
assert node.upgradetohd()
85-
assert mnemonic != node.dumphdinfo()['mnemonic']
86-
assert chainid != node.getwalletinfo()['hdchainid']
107+
assert mnemonic != self.get_mnemonic(node)
108+
if not self.options.descriptors:
109+
assert chainid != node.getwalletinfo()['hdchainid']
87110
assert_equal(balance_non_HD, node.getbalance())
88111
node.keypoolrefill(5)
89112
node.rescanblockchain()
@@ -96,18 +119,20 @@ def run_test(self):
96119
self.restart_node(0, extra_args=['-keypool=10'])
97120
assert node.upgradetohd("", "", "", True)
98121
# Completely different keys, no HD coins should be recovered
99-
assert mnemonic != node.dumphdinfo()['mnemonic']
100-
assert chainid != node.getwalletinfo()['hdchainid']
122+
assert mnemonic != self.get_mnemonic(node)
123+
if not self.options.descriptors:
124+
assert chainid != node.getwalletinfo()['hdchainid']
101125
assert_equal(balance_non_HD, node.getbalance())
102126

103127
self.recover_non_hd()
104128

105129
self.log.info("Same mnemonic, another mnemonic passphrase, no wallet passphrase, should result in a different set of keys")
106130
new_mnemonic_passphrase = "somewords"
107131
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
132+
assert_equal(mnemonic, self.get_mnemonic(node))
133+
if not self.options.descriptors:
134+
new_chainid = node.getwalletinfo()['hdchainid']
135+
assert chainid != new_chainid
111136
assert_equal(balance_non_HD, node.getbalance())
112137
node.keypoolrefill(5)
113138
node.rescanblockchain()
@@ -119,8 +144,9 @@ def run_test(self):
119144

120145
self.log.info("Same mnemonic, another mnemonic passphrase, no wallet passphrase, should result in a different set of keys (again)")
121146
assert node.upgradetohd(mnemonic, new_mnemonic_passphrase)
122-
assert_equal(mnemonic, node.dumphdinfo()['mnemonic'])
123-
assert_equal(new_chainid, node.getwalletinfo()['hdchainid'])
147+
assert_equal(mnemonic, self.get_mnemonic(node))
148+
if not self.options.descriptors:
149+
assert_equal(new_chainid, node.getwalletinfo()['hdchainid'])
124150
assert_equal(balance_non_HD, node.getbalance())
125151
node.keypoolrefill(5)
126152
node.rescanblockchain()
@@ -132,8 +158,9 @@ def run_test(self):
132158

133159
self.log.info("Same mnemonic, no mnemonic passphrase, no wallet passphrase, should recover all coins after rescan")
134160
assert node.upgradetohd(mnemonic)
135-
assert_equal(mnemonic, node.dumphdinfo()['mnemonic'])
136-
assert_equal(chainid, node.getwalletinfo()['hdchainid'])
161+
assert_equal(mnemonic, self.get_mnemonic(node))
162+
if not self.options.descriptors:
163+
assert_equal(chainid, node.getwalletinfo()['hdchainid'])
137164
node.keypoolrefill(5)
138165
assert balance_after != node.getbalance()
139166
node.rescanblockchain()
@@ -144,8 +171,9 @@ def run_test(self):
144171
self.log.info("Same mnemonic, no mnemonic passphrase, no wallet passphrase, large enough keepool, should recover all coins with no extra rescan")
145172
self.restart_node(0, extra_args=['-keypool=10'])
146173
assert node.upgradetohd(mnemonic)
147-
assert_equal(mnemonic, node.dumphdinfo()['mnemonic'])
148-
assert_equal(chainid, node.getwalletinfo()['hdchainid'])
174+
assert_equal(mnemonic, self.get_mnemonic(node))
175+
if not self.options.descriptors:
176+
assert_equal(chainid, node.getwalletinfo()['hdchainid'])
149177
# All coins should be recovered
150178
assert_equal(balance_after, node.getbalance())
151179

@@ -154,8 +182,9 @@ def run_test(self):
154182
self.log.info("Same mnemonic, no mnemonic passphrase, no wallet passphrase, large enough keepool, rescan is skipped initially, should recover all coins after rescanblockchain")
155183
self.restart_node(0, extra_args=['-keypool=10'])
156184
assert node.upgradetohd(mnemonic, "", "", False)
157-
assert_equal(mnemonic, node.dumphdinfo()['mnemonic'])
158-
assert_equal(chainid, node.getwalletinfo()['hdchainid'])
185+
assert_equal(mnemonic, self.get_mnemonic(node))
186+
if not self.options.descriptors:
187+
assert_equal(chainid, node.getwalletinfo()['hdchainid'])
159188
assert balance_after != node.getbalance()
160189
node.rescanblockchain()
161190
# All coins should be recovered
@@ -171,8 +200,9 @@ def run_test(self):
171200
self.start_node(0, extra_args=['-rescan'])
172201
assert_raises_rpc_error(-13, "Error: Please enter the wallet passphrase with walletpassphrase first.", node.dumphdinfo)
173202
node.walletpassphrase(walletpass, 100)
174-
assert_equal(mnemonic, node.dumphdinfo()['mnemonic'])
175-
assert_equal(chainid, node.getwalletinfo()['hdchainid'])
203+
assert_equal(mnemonic, self.get_mnemonic(node))
204+
if not self.options.descriptors:
205+
assert_equal(chainid, node.getwalletinfo()['hdchainid'])
176206
# Note: wallet encryption results in additional keypool topup,
177207
# so we can't compare new balance to balance_non_HD here,
178208
# assert_equal(balance_non_HD, node.getbalance()) # won't work
@@ -191,12 +221,25 @@ def run_test(self):
191221
node.wait_until_stopped()
192222
self.start_node(0, extra_args=['-rescan'])
193223
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")
224+
if not self.options.descriptors:
225+
assert_raises_rpc_error(-1, "Error: The wallet passphrase entered was incorrect", node.upgradetohd, mnemonic, "", "wrongpass")
226+
else:
227+
assert_raises_rpc_error(-1, "SetupDescriptorScriptPubKeyMans: Wallet is locked, cannot setup new descriptors", node.upgradetohd, mnemonic, "", "wrongpass")
228+
if self.options.descriptors:
229+
# TODO - implement auto-unlock descriptor wallet
230+
node.walletpassphrase(walletpass, 100)
195231
assert node.upgradetohd(mnemonic, "", walletpass)
196-
assert_raises_rpc_error(-13, "Error: Please enter the wallet passphrase with walletpassphrase first.", node.dumphdinfo)
232+
# TODO - drop it too!
233+
if self.options.descriptors:
234+
node.walletlock()
235+
if not self.options.descriptors:
236+
assert_raises_rpc_error(-13, "Error: Please enter the wallet passphrase with walletpassphrase first.", node.dumphdinfo)
237+
else:
238+
assert_raises_rpc_error(-13, "Error: Please enter the wallet passphrase with walletpassphrase first.", node.listdescriptors, True)
197239
node.walletpassphrase(walletpass, 100)
198-
assert_equal(mnemonic, node.dumphdinfo()['mnemonic'])
199-
assert_equal(chainid, node.getwalletinfo()['hdchainid'])
240+
assert_equal(mnemonic, self.get_mnemonic(node))
241+
if not self.options.descriptors:
242+
assert_equal(chainid, node.getwalletinfo()['hdchainid'])
200243
# Note: wallet encryption results in additional keypool topup,
201244
# so we can't compare new balance to balance_non_HD here,
202245
# assert_equal(balance_non_HD, node.getbalance()) # won't work

0 commit comments

Comments
 (0)