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-hd.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 Hierarchical Deterministic wallet function."""
  6. from test_framework.test_framework import BitcoinTestFramework
  7. from test_framework.util import (
  8. assert_equal,
  9. connect_nodes_bi,
  10. )
  11. import shutil
  12. class WalletHDTest(BitcoinTestFramework):
  13. def set_test_params(self):
  14. self.setup_clean_chain = True
  15. self.num_nodes = 2
  16. self.extra_args = [['-usehd=0'], ['-usehd=1', '-keypool=0']]
  17. def run_test (self):
  18. tmpdir = self.options.tmpdir
  19. # Make sure can't switch off usehd after wallet creation
  20. self.stop_node(1)
  21. self.assert_start_raises_init_error(1, ['-usehd=0'], 'already existing HD wallet')
  22. self.start_node(1)
  23. connect_nodes_bi(self.nodes, 0, 1)
  24. # Make sure we use hd, keep masterkeyid
  25. masterkeyid = self.nodes[1].getwalletinfo()['hdmasterkeyid']
  26. assert_equal(len(masterkeyid), 40)
  27. # create an internal key
  28. change_addr = self.nodes[1].getrawchangeaddress()
  29. change_addrV= self.nodes[1].validateaddress(change_addr)
  30. assert_equal(change_addrV["hdkeypath"], "m/0'/1'/0'") #first internal child key
  31. # Import a non-HD private key in the HD wallet
  32. non_hd_add = self.nodes[0].getnewaddress()
  33. self.nodes[1].importprivkey(self.nodes[0].dumpprivkey(non_hd_add))
  34. # This should be enough to keep the master key and the non-HD key
  35. self.nodes[1].backupwallet(tmpdir + "/hd.bak")
  36. #self.nodes[1].dumpwallet(tmpdir + "/hd.dump")
  37. # Derive some HD addresses and remember the last
  38. # Also send funds to each add
  39. self.nodes[0].generate(101)
  40. hd_add = None
  41. num_hd_adds = 300
  42. for i in range(num_hd_adds):
  43. hd_add = self.nodes[1].getnewaddress()
  44. hd_info = self.nodes[1].validateaddress(hd_add)
  45. assert_equal(hd_info["hdkeypath"], "m/0'/0'/"+str(i+1)+"'")
  46. assert_equal(hd_info["hdmasterkeyid"], masterkeyid)
  47. self.nodes[0].sendtoaddress(hd_add, 1)
  48. self.nodes[0].generate(1)
  49. self.nodes[0].sendtoaddress(non_hd_add, 1)
  50. self.nodes[0].generate(1)
  51. # create an internal key (again)
  52. change_addr = self.nodes[1].getrawchangeaddress()
  53. change_addrV= self.nodes[1].validateaddress(change_addr)
  54. assert_equal(change_addrV["hdkeypath"], "m/0'/1'/1'") #second internal child key
  55. self.sync_all()
  56. assert_equal(self.nodes[1].getbalance(), num_hd_adds + 1)
  57. self.log.info("Restore backup ...")
  58. self.stop_node(1)
  59. # we need to delete the complete regtest directory
  60. # otherwise node1 would auto-recover all funds in flag the keypool keys as used
  61. shutil.rmtree(tmpdir + "/node1/regtest/blocks")
  62. shutil.rmtree(tmpdir + "/node1/regtest/chainstate")
  63. shutil.copyfile(tmpdir + "/hd.bak", tmpdir + "/node1/regtest/wallet.dat")
  64. self.start_node(1)
  65. # Assert that derivation is deterministic
  66. hd_add_2 = None
  67. for _ in range(num_hd_adds):
  68. hd_add_2 = self.nodes[1].getnewaddress()
  69. hd_info_2 = self.nodes[1].validateaddress(hd_add_2)
  70. assert_equal(hd_info_2["hdkeypath"], "m/0'/0'/"+str(_+1)+"'")
  71. assert_equal(hd_info_2["hdmasterkeyid"], masterkeyid)
  72. assert_equal(hd_add, hd_add_2)
  73. connect_nodes_bi(self.nodes, 0, 1)
  74. self.sync_all()
  75. # Needs rescan
  76. self.stop_node(1)
  77. self.start_node(1, extra_args=self.extra_args[1] + ['-rescan'])
  78. assert_equal(self.nodes[1].getbalance(), num_hd_adds + 1)
  79. # send a tx and make sure its using the internal chain for the changeoutput
  80. txid = self.nodes[1].sendtoaddress(self.nodes[0].getnewaddress(), 1)
  81. outs = self.nodes[1].decoderawtransaction(self.nodes[1].gettransaction(txid)['hex'])['vout']
  82. keypath = ""
  83. for out in outs:
  84. if out['value'] != 1:
  85. keypath = self.nodes[1].validateaddress(out['scriptPubKey']['addresses'][0])['hdkeypath']
  86. assert_equal(keypath[0:7], "m/0'/1'")
  87. if __name__ == '__main__':
  88. WalletHDTest().main ()