5
5
"""Test createwallet arguments.
6
6
"""
7
7
8
+ from test_framework .address import key_to_p2wpkh
9
+ from test_framework .descriptors import descsum_create
10
+ from test_framework .key import ECKey
8
11
from test_framework .test_framework import BitcoinTestFramework
9
12
from test_framework .util import (
10
13
assert_equal ,
11
14
assert_raises_rpc_error ,
12
15
)
16
+ from test_framework .wallet_util import bytes_to_wif , generate_wif_key
13
17
14
18
class CreateWalletTest (BitcoinTestFramework ):
15
19
def set_test_params (self ):
@@ -35,10 +39,14 @@ def run_test(self):
35
39
w1 .importpubkey (w0 .getaddressinfo (address1 )['pubkey' ])
36
40
37
41
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 ())
40
45
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 ]}])
42
50
assert not result [0 ]['success' ]
43
51
assert 'warning' not in result [0 ]
44
52
assert_equal (result [0 ]['error' ]['code' ], - 4 )
@@ -58,12 +66,25 @@ def run_test(self):
58
66
assert_raises_rpc_error (- 4 , "Error: This wallet has no available keys" , w3 .getnewaddress )
59
67
assert_raises_rpc_error (- 4 , "Error: This wallet has no available keys" , w3 .getrawchangeaddress )
60
68
# Import private key
61
- w3 .importprivkey (w0 . dumpprivkey ( address1 ))
69
+ w3 .importprivkey (generate_wif_key ( ))
62
70
# Imported private keys are currently ignored by the keypool
63
71
assert_equal (w3 .getwalletinfo ()['keypoolsize' ], 0 )
64
72
assert_raises_rpc_error (- 4 , "Error: This wallet has no available keys" , w3 .getnewaddress )
65
73
# 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 ()
67
88
assert_equal (w3 .getwalletinfo ()['keypoolsize' ], 1 )
68
89
w3 .getnewaddress ()
69
90
w3 .getrawchangeaddress ()
@@ -80,7 +101,20 @@ def run_test(self):
80
101
assert_raises_rpc_error (- 4 , "Error: This wallet has no available keys" , w4 .getrawchangeaddress )
81
102
# Now set a seed and it should work. Wallet should also be encrypted
82
103
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 ()
84
118
w4 .getnewaddress ()
85
119
w4 .getrawchangeaddress ()
86
120
@@ -111,13 +145,14 @@ def run_test(self):
111
145
w6 .walletpassphrase ('thisisapassphrase' , 60 )
112
146
w6 .signmessage (w6 .getnewaddress ('' , 'legacy' ), "test" )
113
147
w6 .keypoolrefill (1 )
114
- # There should only be 1 key
148
+ # There should only be 1 key for legacy, 3 for descriptors
115
149
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 )
118
153
# Allow empty passphrase, but there should be a warning
119
154
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' ]
121
156
w7 = node .get_wallet_rpc ('w7' )
122
157
assert_raises_rpc_error (- 15 , 'Error: running with an unencrypted wallet, but walletpassphrase was called.' , w7 .walletpassphrase , '' , 60 )
123
158
0 commit comments