mirror of https://github.com/bitcoin/bitcoin
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.
81 lines
3.4 KiB
81 lines
3.4 KiB
8 years ago
|
#!/usr/bin/env python3
|
||
7 years ago
|
# Copyright (c) 2016-2017 The Bitcoin Core developers
|
||
8 years ago
|
# Distributed under the MIT software license, see the accompanying
|
||
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||
|
"""Test Wallet encryption"""
|
||
|
|
||
|
import time
|
||
|
|
||
7 years ago
|
from test_framework.test_framework import BitcoinTestFramework
|
||
8 years ago
|
from test_framework.util import (
|
||
|
assert_equal,
|
||
7 years ago
|
assert_raises_rpc_error,
|
||
7 years ago
|
assert_greater_than,
|
||
|
assert_greater_than_or_equal,
|
||
8 years ago
|
)
|
||
|
|
||
|
class WalletEncryptionTest(BitcoinTestFramework):
|
||
8 years ago
|
def set_test_params(self):
|
||
8 years ago
|
self.setup_clean_chain = True
|
||
|
self.num_nodes = 1
|
||
|
|
||
|
def run_test(self):
|
||
|
passphrase = "WalletPassphrase"
|
||
|
passphrase2 = "SecondWalletPassphrase"
|
||
|
|
||
|
# Make sure the wallet isn't encrypted first
|
||
|
address = self.nodes[0].getnewaddress()
|
||
|
privkey = self.nodes[0].dumpprivkey(address)
|
||
|
assert_equal(privkey[:1], "c")
|
||
|
assert_equal(len(privkey), 52)
|
||
|
|
||
|
# Encrypt the wallet
|
||
8 years ago
|
self.nodes[0].node_encrypt_wallet(passphrase)
|
||
8 years ago
|
self.start_node(0)
|
||
8 years ago
|
|
||
|
# Test that the wallet is encrypted
|
||
7 years ago
|
assert_raises_rpc_error(-13, "Please enter the wallet passphrase with walletpassphrase first", self.nodes[0].dumpprivkey, address)
|
||
8 years ago
|
|
||
|
# Check that walletpassphrase works
|
||
|
self.nodes[0].walletpassphrase(passphrase, 2)
|
||
|
assert_equal(privkey, self.nodes[0].dumpprivkey(address))
|
||
|
|
||
|
# Check that the timeout is right
|
||
|
time.sleep(2)
|
||
7 years ago
|
assert_raises_rpc_error(-13, "Please enter the wallet passphrase with walletpassphrase first", self.nodes[0].dumpprivkey, address)
|
||
8 years ago
|
|
||
|
# Test wrong passphrase
|
||
7 years ago
|
assert_raises_rpc_error(-14, "wallet passphrase entered was incorrect", self.nodes[0].walletpassphrase, passphrase + "wrong", 10)
|
||
8 years ago
|
|
||
|
# Test walletlock
|
||
|
self.nodes[0].walletpassphrase(passphrase, 84600)
|
||
|
assert_equal(privkey, self.nodes[0].dumpprivkey(address))
|
||
|
self.nodes[0].walletlock()
|
||
7 years ago
|
assert_raises_rpc_error(-13, "Please enter the wallet passphrase with walletpassphrase first", self.nodes[0].dumpprivkey, address)
|
||
8 years ago
|
|
||
|
# Test passphrase changes
|
||
|
self.nodes[0].walletpassphrasechange(passphrase, passphrase2)
|
||
7 years ago
|
assert_raises_rpc_error(-14, "wallet passphrase entered was incorrect", self.nodes[0].walletpassphrase, passphrase, 10)
|
||
8 years ago
|
self.nodes[0].walletpassphrase(passphrase2, 10)
|
||
|
assert_equal(privkey, self.nodes[0].dumpprivkey(address))
|
||
7 years ago
|
self.nodes[0].walletlock()
|
||
|
|
||
|
# Test timeout bounds
|
||
|
assert_raises_rpc_error(-8, "Timeout cannot be negative.", self.nodes[0].walletpassphrase, passphrase2, -10)
|
||
|
# Check the timeout
|
||
|
# Check a time less than the limit
|
||
|
expected_time = int(time.time()) + (1 << 30) - 600
|
||
|
self.nodes[0].walletpassphrase(passphrase2, (1 << 30) - 600)
|
||
|
actual_time = self.nodes[0].getwalletinfo()['unlocked_until']
|
||
|
assert_greater_than_or_equal(actual_time, expected_time)
|
||
|
assert_greater_than(expected_time + 5, actual_time) # 5 second buffer
|
||
|
# Check a time greater than the limit
|
||
|
expected_time = int(time.time()) + (1 << 30) - 1
|
||
|
self.nodes[0].walletpassphrase(passphrase2, (1 << 33))
|
||
|
actual_time = self.nodes[0].getwalletinfo()['unlocked_until']
|
||
|
assert_greater_than_or_equal(actual_time, expected_time)
|
||
|
assert_greater_than(expected_time + 5, actual_time) # 5 second buffer
|
||
8 years ago
|
|
||
|
if __name__ == '__main__':
|
||
|
WalletEncryptionTest().main()
|