2 # Copyright (c) 2016-2017 The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 """Test the dumpwallet RPC."""
9 from test_framework
.test_framework
import BitcoinTestFramework
10 from test_framework
.util
import (assert_equal
, assert_raises_rpc_error
)
13 def read_dump(file_name
, addrs
, script_addrs
, hd_master_addr_old
):
15 Read the given dump, count the addrs that match, count change and reserve.
16 Also check that the old hd_master is inactive
18 with
open(file_name
, encoding
='utf8') as inputfile
:
23 hd_master_addr_ret
= None
24 for line
in inputfile
:
25 # only read non comment lines
26 if line
[0] != "#" and len(line
) > 10:
28 key_label
, comment
= line
.split("#")
29 # key = key_label.split(" ")[0]
30 keytype
= key_label
.split(" ")[2]
32 addr_keypath
= comment
.split(" addr=")[1]
33 addr
= addr_keypath
.split(" ")[0]
35 if keytype
== "inactivehdmaster=1":
36 # ensure the old master is still available
37 assert(hd_master_addr_old
== addr
)
38 elif keytype
== "hdmaster=1":
39 # ensure we have generated a new hd master key
40 assert(hd_master_addr_old
!= addr
)
41 hd_master_addr_ret
= addr
42 elif keytype
== "script=1":
43 # scripts don't have keypaths
46 keypath
= addr_keypath
.rstrip().split("hdkeypath=")[1]
50 if addrObj
['address'] == addr
and addrObj
['hdkeypath'] == keypath
and keytype
== "label=":
53 elif keytype
== "change=1":
56 elif keytype
== "reserve=1":
61 for script_addr
in script_addrs
:
62 if script_addr
== addr
.rstrip() and keytype
== "script=1":
63 found_script_addr
+= 1
66 return found_addr
, found_script_addr
, found_addr_chg
, found_addr_rsv
, hd_master_addr_ret
69 class WalletDumpTest(BitcoinTestFramework
):
70 def set_test_params(self
):
72 self
.extra_args
= [["-keypool=90"]]
74 def setup_network(self
, split
=False):
75 # Use 1 minute timeout because the initial getnewaddress RPC can take
76 # longer than the default 30 seconds due to an expensive
77 # CWallet::TopUpKeyPool call, and the encryptwallet RPC made later in
78 # the test often takes even longer.
79 self
.add_nodes(self
.num_nodes
, self
.extra_args
, timewait
=60)
83 tmpdir
= self
.options
.tmpdir
85 # generate 20 addresses to compare against the dump
88 for i
in range(0,test_addr_count
):
89 addr
= self
.nodes
[0].getnewaddress()
90 vaddr
= self
.nodes
[0].validateaddress(addr
) #required to get hd keypath
93 self
.nodes
[0].keypoolrefill()
95 # Test scripts dump by adding a P2SH witness and a 1-of-1 multisig address
96 witness_addr
= self
.nodes
[0].addwitnessaddress(addrs
[0]["address"], True)
97 multisig_addr
= self
.nodes
[0].addmultisigaddress(1, [addrs
[1]["address"]])
98 script_addrs
= [witness_addr
, multisig_addr
]
100 # dump unencrypted wallet
101 result
= self
.nodes
[0].dumpwallet(tmpdir
+ "/node0/wallet.unencrypted.dump")
102 assert_equal(result
['filename'], os
.path
.abspath(tmpdir
+ "/node0/wallet.unencrypted.dump"))
104 found_addr
, found_script_addr
, found_addr_chg
, found_addr_rsv
, hd_master_addr_unenc
= \
105 read_dump(tmpdir
+ "/node0/wallet.unencrypted.dump", addrs
, script_addrs
, None)
106 assert_equal(found_addr
, test_addr_count
) # all keys must be in the dump
107 assert_equal(found_script_addr
, 2) # all scripts must be in the dump
108 assert_equal(found_addr_chg
, 50) # 50 blocks where mined
109 assert_equal(found_addr_rsv
, 90*2) # 90 keys plus 100% internal keys
111 #encrypt wallet, restart, unlock and dump
112 self
.nodes
[0].node_encrypt_wallet('test')
114 self
.nodes
[0].walletpassphrase('test', 10)
116 self
.nodes
[0].keypoolrefill()
117 self
.nodes
[0].dumpwallet(tmpdir
+ "/node0/wallet.encrypted.dump")
119 found_addr
, found_script_addr
, found_addr_chg
, found_addr_rsv
, _
= \
120 read_dump(tmpdir
+ "/node0/wallet.encrypted.dump", addrs
, script_addrs
, hd_master_addr_unenc
)
121 assert_equal(found_addr
, test_addr_count
)
122 assert_equal(found_script_addr
, 2)
123 assert_equal(found_addr_chg
, 90*2 + 50) # old reserve keys are marked as change now
124 assert_equal(found_addr_rsv
, 90*2)
126 # Overwriting should fail
127 assert_raises_rpc_error(-8, "already exists", self
.nodes
[0].dumpwallet
, tmpdir
+ "/node0/wallet.unencrypted.dump")
129 # Restart node with new wallet, and test importwallet
131 self
.start_node(0, ['-wallet=w2'])
133 # Make sure the address is not IsMine before import
134 result
= self
.nodes
[0].validateaddress(multisig_addr
)
135 assert(result
['ismine'] == False)
137 self
.nodes
[0].importwallet(os
.path
.abspath(tmpdir
+ "/node0/wallet.unencrypted.dump"))
139 # Now check IsMine is true
140 result
= self
.nodes
[0].validateaddress(multisig_addr
)
141 assert(result
['ismine'] == True)
143 if __name__
== '__main__':
144 WalletDumpTest().main ()