/ test / functional / wallet_import_with_label.py
wallet_import_with_label.py
  1  #!/usr/bin/env python3
  2  # Copyright (c) 2018-2022 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 the behavior of RPC importprivkey on set and unset labels of
  6  addresses.
  7  
  8  It tests different cases in which an address is imported with importaddress
  9  with or without a label and then its private key is imported with importprivkey
 10  with and without a label.
 11  """
 12  
 13  from test_framework.test_framework import BitcoinTestFramework
 14  from test_framework.wallet_util import test_address
 15  
 16  
 17  class ImportWithLabel(BitcoinTestFramework):
 18      def add_options(self, parser):
 19          self.add_wallet_options(parser, descriptors=False)
 20  
 21      def set_test_params(self):
 22          self.num_nodes = 2
 23          self.setup_clean_chain = True
 24  
 25      def skip_test_if_missing_module(self):
 26          self.skip_if_no_wallet()
 27  
 28      def run_test(self):
 29          """Main test logic"""
 30  
 31          self.log.info(
 32              "Test importaddress with label and importprivkey without label."
 33          )
 34          self.log.info("Import a watch-only address with a label.")
 35          address = self.nodes[0].getnewaddress()
 36          label = "Test Label"
 37          self.nodes[1].importaddress(address, label)
 38          test_address(self.nodes[1],
 39                       address,
 40                       iswatchonly=True,
 41                       ismine=False,
 42                       labels=[label])
 43  
 44          self.log.info(
 45              "Import the watch-only address's private key without a "
 46              "label and the address should keep its label."
 47          )
 48          priv_key = self.nodes[0].dumpprivkey(address)
 49          self.nodes[1].importprivkey(priv_key)
 50          test_address(self.nodes[1], address, labels=[label])
 51  
 52          self.log.info(
 53              "Test importaddress without label and importprivkey with label."
 54          )
 55          self.log.info("Import a watch-only address without a label.")
 56          address2 = self.nodes[0].getnewaddress()
 57          self.nodes[1].importaddress(address2)
 58          test_address(self.nodes[1],
 59                       address2,
 60                       iswatchonly=True,
 61                       ismine=False,
 62                       labels=[""])
 63  
 64          self.log.info(
 65              "Import the watch-only address's private key with a "
 66              "label and the address should have its label updated."
 67          )
 68          priv_key2 = self.nodes[0].dumpprivkey(address2)
 69          label2 = "Test Label 2"
 70          self.nodes[1].importprivkey(priv_key2, label2)
 71  
 72          test_address(self.nodes[1], address2, labels=[label2])
 73  
 74          self.log.info("Test importaddress with label and importprivkey with label.")
 75          self.log.info("Import a watch-only address with a label.")
 76          address3 = self.nodes[0].getnewaddress()
 77          label3_addr = "Test Label 3 for importaddress"
 78          self.nodes[1].importaddress(address3, label3_addr)
 79          test_address(self.nodes[1],
 80                       address3,
 81                       iswatchonly=True,
 82                       ismine=False,
 83                       labels=[label3_addr])
 84  
 85          self.log.info(
 86              "Import the watch-only address's private key with a "
 87              "label and the address should have its label updated."
 88          )
 89          priv_key3 = self.nodes[0].dumpprivkey(address3)
 90          label3_priv = "Test Label 3 for importprivkey"
 91          self.nodes[1].importprivkey(priv_key3, label3_priv)
 92  
 93          test_address(self.nodes[1], address3, labels=[label3_priv])
 94  
 95          self.log.info(
 96              "Test importprivkey won't label new dests with the same "
 97              "label as others labeled dests for the same key."
 98          )
 99          self.log.info("Import a watch-only p2sh-segwit address with a label.")
100          address4 = self.nodes[0].getnewaddress("", "p2sh-segwit")
101          label4_addr = "Test Label 4 for importaddress"
102          self.nodes[1].importaddress(address4, label4_addr)
103          test_address(self.nodes[1],
104                       address4,
105                       iswatchonly=True,
106                       ismine=False,
107                       labels=[label4_addr],
108                       embedded=None)
109  
110          self.log.info(
111              "Import the watch-only address's private key without a "
112              "label and new destinations for the key should have an "
113              "empty label while the 'old' destination should keep "
114              "its label."
115          )
116          priv_key4 = self.nodes[0].dumpprivkey(address4)
117          self.nodes[1].importprivkey(priv_key4)
118          embedded_addr = self.nodes[1].getaddressinfo(address4)['embedded']['address']
119  
120          test_address(self.nodes[1], embedded_addr, labels=[""])
121  
122          test_address(self.nodes[1], address4, labels=[label4_addr])
123  
124          self.stop_nodes()
125  
126  
127  if __name__ == "__main__":
128      ImportWithLabel().main()