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-encryption.py 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 Wallet encryption"""
  6. import time
  7. from test_framework.test_framework import BitcoinTestFramework
  8. from test_framework.util import (
  9. assert_equal,
  10. assert_raises_rpc_error,
  11. )
  12. class WalletEncryptionTest(BitcoinTestFramework):
  13. def set_test_params(self):
  14. self.setup_clean_chain = True
  15. self.num_nodes = 1
  16. def run_test(self):
  17. passphrase = "WalletPassphrase"
  18. passphrase2 = "SecondWalletPassphrase"
  19. # Make sure the wallet isn't encrypted first
  20. address = self.nodes[0].getnewaddress()
  21. privkey = self.nodes[0].dumpprivkey(address)
  22. assert_equal(privkey[:1], "c")
  23. assert_equal(len(privkey), 52)
  24. # Encrypt the wallet
  25. self.nodes[0].node_encrypt_wallet(passphrase)
  26. self.start_node(0)
  27. # Test that the wallet is encrypted
  28. assert_raises_rpc_error(-13, "Please enter the wallet passphrase with walletpassphrase first", self.nodes[0].dumpprivkey, address)
  29. # Check that walletpassphrase works
  30. self.nodes[0].walletpassphrase(passphrase, 2)
  31. assert_equal(privkey, self.nodes[0].dumpprivkey(address))
  32. # Check that the timeout is right
  33. time.sleep(2)
  34. assert_raises_rpc_error(-13, "Please enter the wallet passphrase with walletpassphrase first", self.nodes[0].dumpprivkey, address)
  35. # Test wrong passphrase
  36. assert_raises_rpc_error(-14, "wallet passphrase entered was incorrect", self.nodes[0].walletpassphrase, passphrase + "wrong", 10)
  37. # Test walletlock
  38. self.nodes[0].walletpassphrase(passphrase, 84600)
  39. assert_equal(privkey, self.nodes[0].dumpprivkey(address))
  40. self.nodes[0].walletlock()
  41. assert_raises_rpc_error(-13, "Please enter the wallet passphrase with walletpassphrase first", self.nodes[0].dumpprivkey, address)
  42. # Test passphrase changes
  43. self.nodes[0].walletpassphrasechange(passphrase, passphrase2)
  44. assert_raises_rpc_error(-14, "wallet passphrase entered was incorrect", self.nodes[0].walletpassphrase, passphrase, 10)
  45. self.nodes[0].walletpassphrase(passphrase2, 10)
  46. assert_equal(privkey, self.nodes[0].dumpprivkey(address))
  47. if __name__ == '__main__':
  48. WalletEncryptionTest().main()