You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wallet-dump.py 4.7KB

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