|
| 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 | + |
| 6 | +from test_framework.test_framework import BitcoinTestFramework |
| 7 | +from test_framework.util import * |
| 8 | +import os |
| 9 | +import shutil |
| 10 | + |
| 11 | + |
| 12 | +class WalletDumpTest(BitcoinTestFramework): |
| 13 | + |
| 14 | + def __init__(self): |
| 15 | + super().__init__() |
| 16 | + self.setup_clean_chain = False |
| 17 | + self.num_nodes = 1 |
| 18 | + |
| 19 | + def setup_network(self, split=False): |
| 20 | + extra_args = [["-keypool=100"]] |
| 21 | + self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, extra_args) |
| 22 | + |
| 23 | + def run_test (self): |
| 24 | + tmpdir = self.options.tmpdir |
| 25 | + |
| 26 | + #generate 20 addresses to compare against the dump |
| 27 | + test_addr_count = 20 |
| 28 | + addrs = [] |
| 29 | + for i in range(0,test_addr_count): |
| 30 | + addr = self.nodes[0].getnewaddress() |
| 31 | + vaddr= self.nodes[0].validateaddress(addr) #required to get hd keypath |
| 32 | + addrs.append(vaddr) |
| 33 | + |
| 34 | + # dump unencrypted wallet |
| 35 | + self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.unencrypted.dump") |
| 36 | + |
| 37 | + #open file |
| 38 | + inputfile = open(tmpdir + "/node0/wallet.unencrypted.dump") |
| 39 | + found_addr = 0 |
| 40 | + found_addr_chg = 0 |
| 41 | + found_addr_rsv = 0 |
| 42 | + hdmasteraddr = "" |
| 43 | + for line in inputfile: |
| 44 | + #only read non comment lines |
| 45 | + if line[0] != "#" and len(line) > 10: |
| 46 | + #split out some data |
| 47 | + keyLabel, comment = line.split("#") |
| 48 | + key = keyLabel.split(" ")[0] |
| 49 | + keytype = keyLabel.split(" ")[2] |
| 50 | + if len(comment) > 1: |
| 51 | + addrKeypath = comment.split(" addr=")[1] |
| 52 | + addr = addrKeypath.split(" ")[0] |
| 53 | + keypath = "" |
| 54 | + if keytype != "hdmaster=1": |
| 55 | + keypath = addrKeypath.rstrip().split("hdkeypath=")[1] |
| 56 | + else: |
| 57 | + #keep hd master for later comp. |
| 58 | + hdmasteraddr = addr |
| 59 | + |
| 60 | + #count key types |
| 61 | + for addrObj in addrs: |
| 62 | + if (addrObj['address'] == addr and addrObj['hdkeypath'] == keypath and keytype == "label="): |
| 63 | + found_addr+=1 |
| 64 | + break |
| 65 | + elif (keytype == "change=1"): |
| 66 | + found_addr_chg+=1 |
| 67 | + break |
| 68 | + elif (keytype == "reserve=1"): |
| 69 | + found_addr_rsv+=1 |
| 70 | + break |
| 71 | + assert(found_addr == test_addr_count) #all keys must be in the dump |
| 72 | + assert(found_addr_chg == 50) #50 blocks where mined |
| 73 | + assert(found_addr_rsv == 100) #100 reserve keys (keypool) |
| 74 | + |
| 75 | + #encrypt wallet, restart, unlock and dump |
| 76 | + self.nodes[0].encryptwallet('test') |
| 77 | + bitcoind_processes[0].wait() |
| 78 | + self.nodes[0] = start_node(0, self.options.tmpdir) |
| 79 | + self.nodes[0].walletpassphrase('test', 10) |
| 80 | + self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.encrypted.dump") |
| 81 | + |
| 82 | + #open dump done with an encrypted wallet |
| 83 | + inputfile = open(tmpdir + "/node0/wallet.encrypted.dump") |
| 84 | + found_addr = 0 |
| 85 | + found_addr_chg = 0 |
| 86 | + found_addr_rsv = 0 |
| 87 | + for line in inputfile: |
| 88 | + if line[0] != "#" and len(line) > 10: |
| 89 | + keyLabel, comment = line.split("#") |
| 90 | + key = keyLabel.split(" ")[0] |
| 91 | + keytype = keyLabel.split(" ")[2] |
| 92 | + if len(comment) > 1: |
| 93 | + addrKeypath = comment.split(" addr=")[1] |
| 94 | + addr = addrKeypath.split(" ")[0] |
| 95 | + keypath = "" |
| 96 | + if keytype != "hdmaster=1": |
| 97 | + keypath = addrKeypath.rstrip().split("hdkeypath=")[1] |
| 98 | + else: |
| 99 | + #ensure we have generated a new hd master key |
| 100 | + assert(hdmasteraddr != addr) |
| 101 | + if keytype == "inactivehdmaster=1": |
| 102 | + #ensure the old master is still available |
| 103 | + assert(hdmasteraddr == addr) |
| 104 | + for addrObj in addrs: |
| 105 | + if (addrObj['address'] == addr and addrObj['hdkeypath'] == keypath and keytype == "label="): |
| 106 | + found_addr+=1 |
| 107 | + break |
| 108 | + elif (keytype == "change=1"): |
| 109 | + found_addr_chg+=1 |
| 110 | + break |
| 111 | + elif (keytype == "reserve=1"): |
| 112 | + found_addr_rsv+=1 |
| 113 | + break |
| 114 | + |
| 115 | + assert(found_addr == test_addr_count) |
| 116 | + assert(found_addr_chg == 150) #old reserve keys are marked as change now |
| 117 | + assert(found_addr_rsv == 100) #keypool size |
| 118 | + |
| 119 | +if __name__ == '__main__': |
| 120 | + WalletDumpTest().main () |
0 commit comments