|
| 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 | + |
| 7 | +import time |
| 8 | + |
| 9 | +from test_framework.test_framework import BitcoinTestFramework |
| 10 | +from test_framework.util import ( |
| 11 | + assert_equal, |
| 12 | + assert_raises_jsonrpc, |
| 13 | + bitcoind_processes, |
| 14 | + BITCOIND_PROC_WAIT_TIMEOUT, |
| 15 | +) |
| 16 | + |
| 17 | +class WalletEncryptionTest(BitcoinTestFramework): |
| 18 | + |
| 19 | + def __init__(self): |
| 20 | + super().__init__() |
| 21 | + self.setup_clean_chain = True |
| 22 | + self.num_nodes = 1 |
| 23 | + |
| 24 | + def run_test(self): |
| 25 | + passphrase = "WalletPassphrase" |
| 26 | + passphrase2 = "SecondWalletPassphrase" |
| 27 | + |
| 28 | + # Make sure the wallet isn't encrypted first |
| 29 | + address = self.nodes[0].getnewaddress() |
| 30 | + privkey = self.nodes[0].dumpprivkey(address) |
| 31 | + assert_equal(privkey[:1], "c") |
| 32 | + assert_equal(len(privkey), 52) |
| 33 | + |
| 34 | + # Encrypt the wallet |
| 35 | + self.nodes[0].encryptwallet(passphrase) |
| 36 | + bitcoind_processes[0].wait(timeout=BITCOIND_PROC_WAIT_TIMEOUT) |
| 37 | + self.nodes[0] = self.start_node(0, self.options.tmpdir) |
| 38 | + |
| 39 | + # Test that the wallet is encrypted |
| 40 | + assert_raises_jsonrpc(-13, "Please enter the wallet passphrase with walletpassphrase first", self.nodes[0].dumpprivkey, address) |
| 41 | + |
| 42 | + # Check that walletpassphrase works |
| 43 | + self.nodes[0].walletpassphrase(passphrase, 2) |
| 44 | + assert_equal(privkey, self.nodes[0].dumpprivkey(address)) |
| 45 | + |
| 46 | + # Check that the timeout is right |
| 47 | + time.sleep(2) |
| 48 | + assert_raises_jsonrpc(-13, "Please enter the wallet passphrase with walletpassphrase first", self.nodes[0].dumpprivkey, address) |
| 49 | + |
| 50 | + # Test wrong passphrase |
| 51 | + assert_raises_jsonrpc(-14, "wallet passphrase entered was incorrect", self.nodes[0].walletpassphrase, passphrase + "wrong", 10) |
| 52 | + |
| 53 | + # Test walletlock |
| 54 | + self.nodes[0].walletpassphrase(passphrase, 84600) |
| 55 | + assert_equal(privkey, self.nodes[0].dumpprivkey(address)) |
| 56 | + self.nodes[0].walletlock() |
| 57 | + assert_raises_jsonrpc(-13, "Please enter the wallet passphrase with walletpassphrase first", self.nodes[0].dumpprivkey, address) |
| 58 | + |
| 59 | + # Test passphrase changes |
| 60 | + self.nodes[0].walletpassphrasechange(passphrase, passphrase2) |
| 61 | + assert_raises_jsonrpc(-14, "wallet passphrase entered was incorrect", self.nodes[0].walletpassphrase, passphrase, 10) |
| 62 | + self.nodes[0].walletpassphrase(passphrase2, 10) |
| 63 | + assert_equal(privkey, self.nodes[0].dumpprivkey(address)) |
| 64 | + |
| 65 | +if __name__ == '__main__': |
| 66 | + WalletEncryptionTest().main() |
0 commit comments