|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2022 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or https://www.opensource.org/licenses/mit-license.php. |
| 5 | + |
| 6 | +import os |
| 7 | + |
| 8 | +from test_framework.test_framework import BitcoinTestFramework |
| 9 | +from test_framework.address import ( |
| 10 | + ADDRESS_BCRT1_UNSPENDABLE, |
| 11 | + ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR, |
| 12 | +) |
| 13 | +from test_framework.key import ECKey |
| 14 | +from test_framework.util import ( |
| 15 | + assert_equal, |
| 16 | +) |
| 17 | +from test_framework.wallet_util import bytes_to_wif |
| 18 | + |
| 19 | + |
| 20 | +class WalletBlankTest(BitcoinTestFramework): |
| 21 | + def set_test_params(self): |
| 22 | + self.num_nodes = 1 |
| 23 | + |
| 24 | + def skip_test_if_missing_module(self): |
| 25 | + self.skip_if_no_wallet() |
| 26 | + |
| 27 | + def add_options(self, options): |
| 28 | + self.add_wallet_options(options) |
| 29 | + |
| 30 | + def test_importaddress(self): |
| 31 | + if self.options.descriptors: |
| 32 | + return |
| 33 | + self.log.info("Test that importaddress unsets the blank flag") |
| 34 | + self.nodes[0].createwallet(wallet_name="iaddr", disable_private_keys=True, blank=True) |
| 35 | + wallet = self.nodes[0].get_wallet_rpc("iaddr") |
| 36 | + info = wallet.getwalletinfo() |
| 37 | + assert_equal(info["descriptors"], False) |
| 38 | + assert_equal(info["blank"], True) |
| 39 | + wallet.importaddress(ADDRESS_BCRT1_UNSPENDABLE) |
| 40 | + assert_equal(wallet.getwalletinfo()["blank"], False) |
| 41 | + |
| 42 | + def test_importpubkey(self): |
| 43 | + if self.options.descriptors: |
| 44 | + return |
| 45 | + self.log.info("Test that importpubkey unsets the blank flag") |
| 46 | + for i, comp in enumerate([True, False]): |
| 47 | + self.nodes[0].createwallet(wallet_name=f"ipub{i}", disable_private_keys=True, blank=True) |
| 48 | + wallet = self.nodes[0].get_wallet_rpc(f"ipub{i}") |
| 49 | + info = wallet.getwalletinfo() |
| 50 | + assert_equal(info["descriptors"], False) |
| 51 | + assert_equal(info["blank"], True) |
| 52 | + |
| 53 | + eckey = ECKey() |
| 54 | + eckey.generate(compressed=comp) |
| 55 | + |
| 56 | + wallet.importpubkey(eckey.get_pubkey().get_bytes().hex()) |
| 57 | + assert_equal(wallet.getwalletinfo()["blank"], False) |
| 58 | + |
| 59 | + def test_importprivkey(self): |
| 60 | + if self.options.descriptors: |
| 61 | + return |
| 62 | + self.log.info("Test that importprivkey unsets the blank flag") |
| 63 | + for i, comp in enumerate([True, False]): |
| 64 | + self.nodes[0].createwallet(wallet_name=f"ipriv{i}", blank=True) |
| 65 | + wallet = self.nodes[0].get_wallet_rpc(f"ipriv{i}") |
| 66 | + info = wallet.getwalletinfo() |
| 67 | + assert_equal(info["descriptors"], False) |
| 68 | + assert_equal(info["blank"], True) |
| 69 | + |
| 70 | + eckey = ECKey() |
| 71 | + eckey.generate(compressed=comp) |
| 72 | + wif = bytes_to_wif(eckey.get_bytes(), eckey.is_compressed) |
| 73 | + |
| 74 | + wallet.importprivkey(wif) |
| 75 | + assert_equal(wallet.getwalletinfo()["blank"], False) |
| 76 | + |
| 77 | + def test_importmulti(self): |
| 78 | + if self.options.descriptors: |
| 79 | + return |
| 80 | + self.log.info("Test that importmulti unsets the blank flag") |
| 81 | + self.nodes[0].createwallet(wallet_name="imulti", disable_private_keys=True, blank=True) |
| 82 | + wallet = self.nodes[0].get_wallet_rpc("imulti") |
| 83 | + info = wallet.getwalletinfo() |
| 84 | + assert_equal(info["descriptors"], False) |
| 85 | + assert_equal(info["blank"], True) |
| 86 | + wallet.importmulti([{ |
| 87 | + "desc": ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR, |
| 88 | + "timestamp": "now", |
| 89 | + }]) |
| 90 | + assert_equal(wallet.getwalletinfo()["blank"], False) |
| 91 | + |
| 92 | + def test_importdescriptors(self): |
| 93 | + if not self.options.descriptors: |
| 94 | + return |
| 95 | + self.log.info("Test that importdescriptors preserves the blank flag") |
| 96 | + self.nodes[0].createwallet(wallet_name="idesc", disable_private_keys=True, blank=True) |
| 97 | + wallet = self.nodes[0].get_wallet_rpc("idesc") |
| 98 | + info = wallet.getwalletinfo() |
| 99 | + assert_equal(info["descriptors"], True) |
| 100 | + assert_equal(info["blank"], True) |
| 101 | + wallet.importdescriptors([{ |
| 102 | + "desc": ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR, |
| 103 | + "timestamp": "now", |
| 104 | + }]) |
| 105 | + assert_equal(wallet.getwalletinfo()["blank"], True) |
| 106 | + |
| 107 | + def test_importwallet(self): |
| 108 | + if self.options.descriptors: |
| 109 | + return |
| 110 | + self.log.info("Test that importwallet unsets the blank flag") |
| 111 | + def_wallet = self.nodes[0].get_wallet_rpc(self.default_wallet_name) |
| 112 | + |
| 113 | + self.nodes[0].createwallet(wallet_name="iwallet", blank=True) |
| 114 | + wallet = self.nodes[0].get_wallet_rpc("iwallet") |
| 115 | + info = wallet.getwalletinfo() |
| 116 | + assert_equal(info["descriptors"], False) |
| 117 | + assert_equal(info["blank"], True) |
| 118 | + |
| 119 | + wallet_dump_path = os.path.join(self.nodes[0].datadir, "wallet.dump") |
| 120 | + def_wallet.dumpwallet(wallet_dump_path) |
| 121 | + |
| 122 | + wallet.importwallet(wallet_dump_path) |
| 123 | + assert_equal(wallet.getwalletinfo()["blank"], False) |
| 124 | + |
| 125 | + def test_encrypt_legacy(self): |
| 126 | + if self.options.descriptors: |
| 127 | + return |
| 128 | + self.log.info("Test that encrypting a blank legacy wallet preserves the blank flag and does not generate a seed") |
| 129 | + self.nodes[0].createwallet(wallet_name="encblanklegacy", blank=True) |
| 130 | + wallet = self.nodes[0].get_wallet_rpc("encblanklegacy") |
| 131 | + |
| 132 | + info = wallet.getwalletinfo() |
| 133 | + assert_equal(info["descriptors"], False) |
| 134 | + assert_equal(info["blank"], True) |
| 135 | + assert "hdseedid" not in info |
| 136 | + |
| 137 | + wallet.encryptwallet("pass") |
| 138 | + info = wallet.getwalletinfo() |
| 139 | + assert_equal(info["blank"], True) |
| 140 | + assert "hdseedid" not in info |
| 141 | + |
| 142 | + def test_encrypt_descriptors(self): |
| 143 | + if not self.options.descriptors: |
| 144 | + return |
| 145 | + self.log.info("Test that encrypting a blank descriptor wallet preserves the blank flag and descriptors remain the same") |
| 146 | + self.nodes[0].createwallet(wallet_name="encblankdesc", blank=True) |
| 147 | + wallet = self.nodes[0].get_wallet_rpc("encblankdesc") |
| 148 | + |
| 149 | + info = wallet.getwalletinfo() |
| 150 | + assert_equal(info["descriptors"], True) |
| 151 | + assert_equal(info["blank"], True) |
| 152 | + descs = wallet.listdescriptors() |
| 153 | + |
| 154 | + wallet.encryptwallet("pass") |
| 155 | + assert_equal(wallet.getwalletinfo()["blank"], True) |
| 156 | + assert_equal(descs, wallet.listdescriptors()) |
| 157 | + |
| 158 | + def run_test(self): |
| 159 | + self.test_importaddress() |
| 160 | + self.test_importpubkey() |
| 161 | + self.test_importprivkey() |
| 162 | + self.test_importmulti() |
| 163 | + self.test_importdescriptors() |
| 164 | + self.test_importwallet() |
| 165 | + self.test_encrypt_legacy() |
| 166 | + self.test_encrypt_descriptors() |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == '__main__': |
| 170 | + WalletBlankTest().main() |
0 commit comments