Skip to content

Commit 25bc5dc

Browse files
committed
Use importdescriptors when in descriptor wallet mode in wallet_createwallet.py
sethdseed and importmulti are not available for descriptor wallets, so when doing descriptor wallet tests, use importdescriptors instead. Also changes some output to match what descriptor wallets will return.
1 parent 0bd1860 commit 25bc5dc

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

test/functional/wallet_createwallet.py

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
"""Test createwallet arguments.
66
"""
77

8+
from test_framework.address import key_to_p2wpkh
9+
from test_framework.descriptors import descsum_create
10+
from test_framework.key import ECKey
811
from test_framework.test_framework import BitcoinTestFramework
912
from test_framework.util import (
1013
assert_equal,
1114
assert_raises_rpc_error,
1215
)
16+
from test_framework.wallet_util import bytes_to_wif, generate_wif_key
1317

1418
class CreateWalletTest(BitcoinTestFramework):
1519
def set_test_params(self):
@@ -35,10 +39,14 @@ def run_test(self):
3539
w1.importpubkey(w0.getaddressinfo(address1)['pubkey'])
3640

3741
self.log.info('Test that private keys cannot be imported')
38-
addr = w0.getnewaddress('', 'legacy')
39-
privkey = w0.dumpprivkey(addr)
42+
eckey = ECKey()
43+
eckey.generate()
44+
privkey = bytes_to_wif(eckey.get_bytes())
4045
assert_raises_rpc_error(-4, 'Cannot import private keys to a wallet with private keys disabled', w1.importprivkey, privkey)
41-
result = w1.importmulti([{'scriptPubKey': {'address': addr}, 'timestamp': 'now', 'keys': [privkey]}])
46+
if self.options.descriptors:
47+
result = w1.importdescriptors([{'desc': descsum_create('wpkh(' + privkey + ')'), 'timestamp': 'now'}])
48+
else:
49+
result = w1.importmulti([{'scriptPubKey': {'address': key_to_p2wpkh(eckey.get_pubkey().get_bytes())}, 'timestamp': 'now', 'keys': [privkey]}])
4250
assert not result[0]['success']
4351
assert 'warning' not in result[0]
4452
assert_equal(result[0]['error']['code'], -4)
@@ -58,12 +66,25 @@ def run_test(self):
5866
assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w3.getnewaddress)
5967
assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w3.getrawchangeaddress)
6068
# Import private key
61-
w3.importprivkey(w0.dumpprivkey(address1))
69+
w3.importprivkey(generate_wif_key())
6270
# Imported private keys are currently ignored by the keypool
6371
assert_equal(w3.getwalletinfo()['keypoolsize'], 0)
6472
assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w3.getnewaddress)
6573
# Set the seed
66-
w3.sethdseed()
74+
if self.options.descriptors:
75+
w3.importdescriptors([{
76+
'desc': descsum_create('wpkh(tprv8ZgxMBicQKsPcwuZGKp8TeWppSuLMiLe2d9PupB14QpPeQsqoj3LneJLhGHH13xESfvASyd4EFLJvLrG8b7DrLxEuV7hpF9uUc6XruKA1Wq/0h/*)'),
77+
'timestamp': 'now',
78+
'active': True
79+
},
80+
{
81+
'desc': descsum_create('wpkh(tprv8ZgxMBicQKsPcwuZGKp8TeWppSuLMiLe2d9PupB14QpPeQsqoj3LneJLhGHH13xESfvASyd4EFLJvLrG8b7DrLxEuV7hpF9uUc6XruKA1Wq/1h/*)'),
82+
'timestamp': 'now',
83+
'active': True,
84+
'internal': True
85+
}])
86+
else:
87+
w3.sethdseed()
6788
assert_equal(w3.getwalletinfo()['keypoolsize'], 1)
6889
w3.getnewaddress()
6990
w3.getrawchangeaddress()
@@ -80,7 +101,20 @@ def run_test(self):
80101
assert_raises_rpc_error(-4, "Error: This wallet has no available keys", w4.getrawchangeaddress)
81102
# Now set a seed and it should work. Wallet should also be encrypted
82103
w4.walletpassphrase('pass', 60)
83-
w4.sethdseed()
104+
if self.options.descriptors:
105+
w4.importdescriptors([{
106+
'desc': descsum_create('wpkh(tprv8ZgxMBicQKsPcwuZGKp8TeWppSuLMiLe2d9PupB14QpPeQsqoj3LneJLhGHH13xESfvASyd4EFLJvLrG8b7DrLxEuV7hpF9uUc6XruKA1Wq/0h/*)'),
107+
'timestamp': 'now',
108+
'active': True
109+
},
110+
{
111+
'desc': descsum_create('wpkh(tprv8ZgxMBicQKsPcwuZGKp8TeWppSuLMiLe2d9PupB14QpPeQsqoj3LneJLhGHH13xESfvASyd4EFLJvLrG8b7DrLxEuV7hpF9uUc6XruKA1Wq/1h/*)'),
112+
'timestamp': 'now',
113+
'active': True,
114+
'internal': True
115+
}])
116+
else:
117+
w4.sethdseed()
84118
w4.getnewaddress()
85119
w4.getrawchangeaddress()
86120

@@ -111,13 +145,14 @@ def run_test(self):
111145
w6.walletpassphrase('thisisapassphrase', 60)
112146
w6.signmessage(w6.getnewaddress('', 'legacy'), "test")
113147
w6.keypoolrefill(1)
114-
# There should only be 1 key
148+
# There should only be 1 key for legacy, 3 for descriptors
115149
walletinfo = w6.getwalletinfo()
116-
assert_equal(walletinfo['keypoolsize'], 1)
117-
assert_equal(walletinfo['keypoolsize_hd_internal'], 1)
150+
keys = 3 if self.options.descriptors else 1
151+
assert_equal(walletinfo['keypoolsize'], keys)
152+
assert_equal(walletinfo['keypoolsize_hd_internal'], keys)
118153
# Allow empty passphrase, but there should be a warning
119154
resp = self.nodes[0].createwallet(wallet_name='w7', disable_private_keys=False, blank=False, passphrase='')
120-
assert_equal(resp['warning'], 'Empty string given as passphrase, wallet will not be encrypted.')
155+
assert 'Empty string given as passphrase, wallet will not be encrypted.' in resp['warning']
121156
w7 = node.get_wallet_rpc('w7')
122157
assert_raises_rpc_error(-15, 'Error: running with an unencrypted wallet, but walletpassphrase was called.', w7.walletpassphrase, '', 60)
123158

0 commit comments

Comments
 (0)