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