Check gpg version before setting --weak-digest
[bitcoinplatinum.git] / qa / rpc-tests / wallet-dump.py
blobb819b72b75f5bd0938c533c57ff9cf17ab55a896
1 #!/usr/bin/env python3
2 # Copyright (c) 2016 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."""
7 from test_framework.test_framework import BitcoinTestFramework
8 from test_framework.util import (start_nodes, start_node, assert_equal, bitcoind_processes)
11 def read_dump(file_name, addrs, hd_master_addr_old):
12 """
13 Read the given dump, count the addrs that match, count change and reserve.
14 Also check that the old hd_master is inactive
15 """
16 with open(file_name, encoding='utf8') as inputfile:
17 found_addr = 0
18 found_addr_chg = 0
19 found_addr_rsv = 0
20 hd_master_addr_ret = None
21 for line in inputfile:
22 # only read non comment lines
23 if line[0] != "#" and len(line) > 10:
24 # split out some data
25 key_label, comment = line.split("#")
26 # key = key_label.split(" ")[0]
27 keytype = key_label.split(" ")[2]
28 if len(comment) > 1:
29 addr_keypath = comment.split(" addr=")[1]
30 addr = addr_keypath.split(" ")[0]
31 keypath = None
32 if keytype == "inactivehdmaster=1":
33 # ensure the old master is still available
34 assert(hd_master_addr_old == addr)
35 elif keytype == "hdmaster=1":
36 # ensure we have generated a new hd master key
37 assert(hd_master_addr_old != addr)
38 hd_master_addr_ret = addr
39 else:
40 keypath = addr_keypath.rstrip().split("hdkeypath=")[1]
42 # count key types
43 for addrObj in addrs:
44 if addrObj['address'] == addr and addrObj['hdkeypath'] == keypath and keytype == "label=":
45 found_addr += 1
46 break
47 elif keytype == "change=1":
48 found_addr_chg += 1
49 break
50 elif keytype == "reserve=1":
51 found_addr_rsv += 1
52 break
53 return found_addr, found_addr_chg, found_addr_rsv, hd_master_addr_ret
56 class WalletDumpTest(BitcoinTestFramework):
58 def __init__(self):
59 super().__init__()
60 self.setup_clean_chain = False
61 self.num_nodes = 1
62 self.extra_args = [["-keypool=90"]]
64 def setup_network(self, split=False):
65 # Use 1 minute timeout because the initial getnewaddress RPC can take
66 # longer than the default 30 seconds due to an expensive
67 # CWallet::TopUpKeyPool call, and the encryptwallet RPC made later in
68 # the test often takes even longer.
69 self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, self.extra_args, timewait=60)
71 def run_test (self):
72 tmpdir = self.options.tmpdir
74 # generate 20 addresses to compare against the dump
75 test_addr_count = 20
76 addrs = []
77 for i in range(0,test_addr_count):
78 addr = self.nodes[0].getnewaddress()
79 vaddr= self.nodes[0].validateaddress(addr) #required to get hd keypath
80 addrs.append(vaddr)
81 # Should be a no-op:
82 self.nodes[0].keypoolrefill()
84 # dump unencrypted wallet
85 self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.unencrypted.dump")
87 found_addr, found_addr_chg, found_addr_rsv, hd_master_addr_unenc = \
88 read_dump(tmpdir + "/node0/wallet.unencrypted.dump", addrs, None)
89 assert_equal(found_addr, test_addr_count) # all keys must be in the dump
90 assert_equal(found_addr_chg, 50) # 50 blocks where mined
91 assert_equal(found_addr_rsv, 90 + 1) # keypool size (TODO: fix off-by-one)
93 #encrypt wallet, restart, unlock and dump
94 self.nodes[0].encryptwallet('test')
95 bitcoind_processes[0].wait()
96 self.nodes[0] = start_node(0, self.options.tmpdir, self.extra_args[0])
97 self.nodes[0].walletpassphrase('test', 10)
98 # Should be a no-op:
99 self.nodes[0].keypoolrefill()
100 self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.encrypted.dump")
102 found_addr, found_addr_chg, found_addr_rsv, hd_master_addr_enc = \
103 read_dump(tmpdir + "/node0/wallet.encrypted.dump", addrs, hd_master_addr_unenc)
104 assert_equal(found_addr, test_addr_count)
105 assert_equal(found_addr_chg, 90 + 1 + 50) # old reserve keys are marked as change now
106 assert_equal(found_addr_rsv, 90 + 1) # keypool size (TODO: fix off-by-one)
108 if __name__ == '__main__':
109 WalletDumpTest().main ()