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