@@ -18,11 +18,12 @@ def read_dump(file_name, addrs, script_addrs, hd_master_addr_old):
18
18
Also check that the old hd_master is inactive
19
19
"""
20
20
with open (file_name , encoding = 'utf8' ) as inputfile :
21
- found_addr = 0
21
+ found_legacy_addr = 0
22
+ found_p2sh_segwit_addr = 0
23
+ found_bech32_addr = 0
22
24
found_script_addr = 0
23
25
found_addr_chg = 0
24
26
found_addr_rsv = 0
25
- witness_addr_ret = None
26
27
hd_master_addr_ret = None
27
28
for line in inputfile :
28
29
# only read non comment lines
@@ -59,14 +60,14 @@ def read_dump(file_name, addrs, script_addrs, hd_master_addr_old):
59
60
# count key types
60
61
for addrObj in addrs :
61
62
if addrObj ['address' ] == addr .split ("," )[0 ] and addrObj ['hdkeypath' ] == keypath and keytype == "label=" :
62
- # a labeled entry in the wallet should contain both a native address
63
- # and the p2sh-p2wpkh address that was added at wallet setup
64
- if len ( addr . split ( "," )) == 2 :
65
- addr_list = addr .split ( "," )
66
- # the entry should be of the first key in the wallet
67
- assert_equal ( addrs [ 0 ][ 'address' ], addr_list [ 0 ])
68
- witness_addr_ret = addr_list [ 1 ]
69
- found_addr += 1
63
+ if addr . startswith ( 'm' ) or addr . startswith ( 'n' ):
64
+ # P2PKH address
65
+ found_legacy_addr += 1
66
+ elif addr .startswith ( '2' ):
67
+ # P2SH-segwit address
68
+ found_p2sh_segwit_addr += 1
69
+ elif addr . startswith ( 'bcrt1' ):
70
+ found_bech32_addr += 1
70
71
break
71
72
elif keytype == "change=1" :
72
73
found_addr_chg += 1
@@ -81,13 +82,13 @@ def read_dump(file_name, addrs, script_addrs, hd_master_addr_old):
81
82
found_script_addr += 1
82
83
break
83
84
84
- return found_addr , found_script_addr , found_addr_chg , found_addr_rsv , hd_master_addr_ret , witness_addr_ret
85
+ return found_legacy_addr , found_p2sh_segwit_addr , found_bech32_addr , found_script_addr , found_addr_chg , found_addr_rsv , hd_master_addr_ret
85
86
86
87
87
88
class WalletDumpTest (BitcoinTestFramework ):
88
89
def set_test_params (self ):
89
90
self .num_nodes = 1
90
- self .extra_args = [["-keypool=90" , "-addresstype=legacy" , "-deprecatedrpc=addwitnessaddress" ]]
91
+ self .extra_args = [["-keypool=90" , "-addresstype=legacy" ]]
91
92
self .rpc_timeout = 120
92
93
93
94
def skip_test_if_missing_module (self ):
@@ -101,34 +102,38 @@ def run_test(self):
101
102
wallet_unenc_dump = os .path .join (self .nodes [0 ].datadir , "wallet.unencrypted.dump" )
102
103
wallet_enc_dump = os .path .join (self .nodes [0 ].datadir , "wallet.encrypted.dump" )
103
104
104
- # generate 20 addresses to compare against the dump
105
- # but since we add a p2sh-p2wpkh address for the first pubkey in the
106
- # wallet, we will expect 21 addresses in the dump
107
- test_addr_count = 20
105
+ # generate 30 addresses to compare against the dump
106
+ # - 10 legacy P2PKH
107
+ # - 10 P2SH-segwit
108
+ # - 10 bech32
109
+ test_addr_count = 10
108
110
addrs = []
109
- for i in range (0 , test_addr_count ):
110
- addr = self .nodes [0 ].getnewaddress ()
111
- vaddr = self .nodes [0 ].getaddressinfo (addr ) # required to get hd keypath
112
- addrs .append (vaddr )
113
- # Should be a no-op:
114
- self .nodes [0 ].keypoolrefill ()
111
+ for address_type in ['legacy' , 'p2sh-segwit' , 'bech32' ]:
112
+ for i in range (0 , test_addr_count ):
113
+ addr = self .nodes [0 ].getnewaddress (address_type = address_type )
114
+ vaddr = self .nodes [0 ].getaddressinfo (addr ) # required to get hd keypath
115
+ addrs .append (vaddr )
115
116
116
- # Test scripts dump by adding a P2SH witness and a 1-of-1 multisig address
117
- witness_addr = self .nodes [0 ].addwitnessaddress (addrs [0 ]["address" ], True )
117
+ # Test scripts dump by adding a 1-of-1 multisig address
118
118
multisig_addr = self .nodes [0 ].addmultisigaddress (1 , [addrs [1 ]["address" ]])["address" ]
119
- script_addrs = [witness_addr , multisig_addr ]
119
+
120
+ # Refill the keypool. getnewaddress() refills the keypool *before* taking a key from
121
+ # the keypool, so the final call to getnewaddress leaves the keypool with one key below
122
+ # its capacity
123
+ self .nodes [0 ].keypoolrefill ()
120
124
121
125
# dump unencrypted wallet
122
126
result = self .nodes [0 ].dumpwallet (wallet_unenc_dump )
123
127
assert_equal (result ['filename' ], wallet_unenc_dump )
124
128
125
- found_addr , found_script_addr , found_addr_chg , found_addr_rsv , hd_master_addr_unenc , witness_addr_ret = \
126
- read_dump (wallet_unenc_dump , addrs , script_addrs , None )
127
- assert_equal (found_addr , test_addr_count ) # all keys must be in the dump
128
- assert_equal (found_script_addr , 2 ) # all scripts must be in the dump
129
+ found_legacy_addr , found_p2sh_segwit_addr , found_bech32_addr , found_script_addr , found_addr_chg , found_addr_rsv , hd_master_addr_unenc = \
130
+ read_dump (wallet_unenc_dump , addrs , [multisig_addr ], None )
131
+ assert_equal (found_legacy_addr , test_addr_count ) # all keys must be in the dump
132
+ assert_equal (found_p2sh_segwit_addr , test_addr_count ) # all keys must be in the dump
133
+ assert_equal (found_bech32_addr , test_addr_count ) # all keys must be in the dump
134
+ assert_equal (found_script_addr , 1 ) # all scripts must be in the dump
129
135
assert_equal (found_addr_chg , 0 ) # 0 blocks where mined
130
136
assert_equal (found_addr_rsv , 90 * 2 ) # 90 keys plus 100% internal keys
131
- assert_equal (witness_addr_ret , witness_addr ) # p2sh-p2wsh address added to the first key
132
137
133
138
# encrypt wallet, restart, unlock and dump
134
139
self .nodes [0 ].encryptwallet ('test' )
@@ -137,13 +142,14 @@ def run_test(self):
137
142
self .nodes [0 ].keypoolrefill ()
138
143
self .nodes [0 ].dumpwallet (wallet_enc_dump )
139
144
140
- found_addr , found_script_addr , found_addr_chg , found_addr_rsv , _ , witness_addr_ret = \
141
- read_dump (wallet_enc_dump , addrs , script_addrs , hd_master_addr_unenc )
142
- assert_equal (found_addr , test_addr_count )
143
- assert_equal (found_script_addr , 2 )
145
+ found_legacy_addr , found_p2sh_segwit_addr , found_bech32_addr , found_script_addr , found_addr_chg , found_addr_rsv , _ = \
146
+ read_dump (wallet_enc_dump , addrs , [multisig_addr ], hd_master_addr_unenc )
147
+ assert_equal (found_legacy_addr , test_addr_count ) # all keys must be in the dump
148
+ assert_equal (found_p2sh_segwit_addr , test_addr_count ) # all keys must be in the dump
149
+ assert_equal (found_bech32_addr , test_addr_count ) # all keys must be in the dump
150
+ assert_equal (found_script_addr , 1 )
144
151
assert_equal (found_addr_chg , 90 * 2 ) # old reserve keys are marked as change now
145
152
assert_equal (found_addr_rsv , 90 * 2 )
146
- assert_equal (witness_addr_ret , witness_addr )
147
153
148
154
# Overwriting should fail
149
155
assert_raises_rpc_error (- 8 , "already exists" , lambda : self .nodes [0 ].dumpwallet (wallet_enc_dump ))
0 commit comments