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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. import os
  7. from test_framework.test_framework import BitcoinTestFramework
  8. from test_framework.util import (assert_equal, assert_raises_rpc_error)
  9. def read_dump(file_name, addrs, hd_master_addr_old):
  10. """
  11. Read the given dump, count the addrs that match, count change and reserve.
  12. Also check that the old hd_master is inactive
  13. """
  14. with open(file_name, encoding='utf8') as inputfile:
  15. found_addr = 0
  16. found_addr_chg = 0
  17. found_addr_rsv = 0
  18. hd_master_addr_ret = None
  19. for line in inputfile:
  20. # only read non comment lines
  21. if line[0] != "#" and len(line) > 10:
  22. # split out some data
  23. key_label, comment = line.split("#")
  24. # key = key_label.split(" ")[0]
  25. keytype = key_label.split(" ")[2]
  26. if len(comment) > 1:
  27. addr_keypath = comment.split(" addr=")[1]
  28. addr = addr_keypath.split(" ")[0]
  29. keypath = None
  30. if keytype == "inactivehdmaster=1":
  31. # ensure the old master is still available
  32. assert(hd_master_addr_old == addr)
  33. elif keytype == "hdmaster=1":
  34. # ensure we have generated a new hd master key
  35. assert(hd_master_addr_old != addr)
  36. hd_master_addr_ret = addr
  37. else:
  38. keypath = addr_keypath.rstrip().split("hdkeypath=")[1]
  39. # count key types
  40. for addrObj in addrs:
  41. if addrObj['address'] == addr and addrObj['hdkeypath'] == keypath and keytype == "label=":
  42. found_addr += 1
  43. break
  44. elif keytype == "change=1":
  45. found_addr_chg += 1
  46. break
  47. elif keytype == "reserve=1":
  48. found_addr_rsv += 1
  49. break
  50. return found_addr, found_addr_chg, found_addr_rsv, hd_master_addr_ret
  51. class WalletDumpTest(BitcoinTestFramework):
  52. def set_test_params(self):
  53. self.num_nodes = 1
  54. self.extra_args = [["-keypool=90"]]
  55. def setup_network(self, split=False):
  56. # Use 1 minute timeout because the initial getnewaddress RPC can take
  57. # longer than the default 30 seconds due to an expensive
  58. # CWallet::TopUpKeyPool call, and the encryptwallet RPC made later in
  59. # the test often takes even longer.
  60. self.add_nodes(self.num_nodes, self.extra_args, timewait=60)
  61. self.start_nodes()
  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. result = self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.unencrypted.dump")
  75. assert_equal(result['filename'], os.path.abspath(tmpdir + "/node0/wallet.unencrypted.dump"))
  76. found_addr, found_addr_chg, found_addr_rsv, hd_master_addr_unenc = \
  77. read_dump(tmpdir + "/node0/wallet.unencrypted.dump", addrs, None)
  78. assert_equal(found_addr, test_addr_count) # all keys must be in the dump
  79. assert_equal(found_addr_chg, 50) # 50 blocks where mined
  80. assert_equal(found_addr_rsv, 90*2) # 90 keys plus 100% internal keys
  81. #encrypt wallet, restart, unlock and dump
  82. self.nodes[0].node_encrypt_wallet('test')
  83. self.start_node(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*2 + 50) # old reserve keys are marked as change now
  92. assert_equal(found_addr_rsv, 90*2)
  93. # Overwriting should fail
  94. assert_raises_rpc_error(-8, "already exists", self.nodes[0].dumpwallet, tmpdir + "/node0/wallet.unencrypted.dump")
  95. if __name__ == '__main__':
  96. WalletDumpTest().main ()