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"""
9 from test_framework
.test_framework
import BitcoinTestFramework
, BITCOIND_PROC_WAIT_TIMEOUT
10 from test_framework
.util
import (
12 assert_raises_jsonrpc
,
15 class WalletEncryptionTest(BitcoinTestFramework
):
19 self
.setup_clean_chain
= True
23 passphrase
= "WalletPassphrase"
24 passphrase2
= "SecondWalletPassphrase"
26 # Make sure the wallet isn't encrypted first
27 address
= self
.nodes
[0].getnewaddress()
28 privkey
= self
.nodes
[0].dumpprivkey(address
)
29 assert_equal(privkey
[:1], "c")
30 assert_equal(len(privkey
), 52)
33 self
.nodes
[0].encryptwallet(passphrase
)
34 self
.bitcoind_processes
[0].wait(timeout
=BITCOIND_PROC_WAIT_TIMEOUT
)
35 self
.nodes
[0] = self
.start_node(0, self
.options
.tmpdir
)
37 # Test that the wallet is encrypted
38 assert_raises_jsonrpc(-13, "Please enter the wallet passphrase with walletpassphrase first", self
.nodes
[0].dumpprivkey
, address
)
40 # Check that walletpassphrase works
41 self
.nodes
[0].walletpassphrase(passphrase
, 2)
42 assert_equal(privkey
, self
.nodes
[0].dumpprivkey(address
))
44 # Check that the timeout is right
46 assert_raises_jsonrpc(-13, "Please enter the wallet passphrase with walletpassphrase first", self
.nodes
[0].dumpprivkey
, address
)
48 # Test wrong passphrase
49 assert_raises_jsonrpc(-14, "wallet passphrase entered was incorrect", self
.nodes
[0].walletpassphrase
, passphrase
+ "wrong", 10)
52 self
.nodes
[0].walletpassphrase(passphrase
, 84600)
53 assert_equal(privkey
, self
.nodes
[0].dumpprivkey(address
))
54 self
.nodes
[0].walletlock()
55 assert_raises_jsonrpc(-13, "Please enter the wallet passphrase with walletpassphrase first", self
.nodes
[0].dumpprivkey
, address
)
57 # Test passphrase changes
58 self
.nodes
[0].walletpassphrasechange(passphrase
, passphrase2
)
59 assert_raises_jsonrpc(-14, "wallet passphrase entered was incorrect", self
.nodes
[0].walletpassphrase
, passphrase
, 10)
60 self
.nodes
[0].walletpassphrase(passphrase2
, 10)
61 assert_equal(privkey
, self
.nodes
[0].dumpprivkey(address
))
63 if __name__
== '__main__':
64 WalletEncryptionTest().main()