From e47ce42f670fc43859c157766b342509ab5916f9 Mon Sep 17 00:00:00 2001 From: ismaelsadeeq Date: Tue, 28 Mar 2023 16:58:16 +0100 Subject: refactor: use address_to_scriptpubkey to retrieve addresses scriptpubkey This commit updates the code by replacing the RPC call used to decode an address and retrieve its corresponding scriptpubkey with the address_to_scriptpubkey function. address_to_scriptpubkey function can now decode all addresses formats, which makes it more efficient to use. --- test/functional/feature_nulldummy.py | 3 ++- test/functional/rpc_createmultisig.py | 3 ++- test/functional/rpc_scanblocks.py | 3 ++- test/functional/rpc_signrawtransactionwithkey.py | 3 ++- test/functional/test_framework/blocktools.py | 3 ++- test/functional/test_framework/wallet.py | 3 ++- test/functional/wallet_avoidreuse.py | 3 ++- test/functional/wallet_fast_rescan.py | 3 ++- 8 files changed, 16 insertions(+), 8 deletions(-) (limited to 'test') diff --git a/test/functional/feature_nulldummy.py b/test/functional/feature_nulldummy.py index d7558c830e..c95657dbbb 100755 --- a/test/functional/feature_nulldummy.py +++ b/test/functional/feature_nulldummy.py @@ -14,6 +14,7 @@ Generate COINBASE_MATURITY (CB) more blocks to ensure the coinbases are mature. """ import time +from test_framework.address import address_to_scriptpubkey from test_framework.blocktools import ( COINBASE_MATURITY, NORMAL_GBT_REQUEST_PARAMS, @@ -77,7 +78,7 @@ class NULLDUMMYTest(BitcoinTestFramework): cms = self.nodes[0].createmultisig(1, [self.pubkey]) wms = self.nodes[0].createmultisig(1, [self.pubkey], 'p2sh-segwit') self.ms_address = cms["address"] - ms_unlock_details = {"scriptPubKey": self.nodes[0].validateaddress(self.ms_address)["scriptPubKey"], + ms_unlock_details = {"scriptPubKey": address_to_scriptpubkey(self.ms_address).hex(), "redeemScript": cms["redeemScript"]} self.wit_ms_address = wms['address'] diff --git a/test/functional/rpc_createmultisig.py b/test/functional/rpc_createmultisig.py index 7d03ed2951..bec499107f 100755 --- a/test/functional/rpc_createmultisig.py +++ b/test/functional/rpc_createmultisig.py @@ -8,6 +8,7 @@ import itertools import json import os +from test_framework.address import address_to_scriptpubkey from test_framework.blocktools import COINBASE_MATURITY from test_framework.authproxy import JSONRPCException from test_framework.descriptors import descsum_create, drop_origins @@ -193,7 +194,7 @@ class RpcCreateMultiSigTest(BitcoinTestFramework): assert mredeemw == mredeem wmulti.unloadwallet() - spk = bytes.fromhex(node0.validateaddress(madd)["scriptPubKey"]) + spk = address_to_scriptpubkey(madd) txid, _ = self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=spk, amount=1300) tx = node0.getrawtransaction(txid, True) vout = [v["n"] for v in tx["vout"] if madd == v["scriptPubKey"]["address"]] diff --git a/test/functional/rpc_scanblocks.py b/test/functional/rpc_scanblocks.py index 126e95362b..9354709dfb 100755 --- a/test/functional/rpc_scanblocks.py +++ b/test/functional/rpc_scanblocks.py @@ -3,6 +3,7 @@ # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Test the scanblocks RPC call.""" +from test_framework.address import address_to_scriptpubkey from test_framework.blockfilter import ( bip158_basic_element_hash, bip158_relevant_scriptpubkeys, @@ -36,7 +37,7 @@ class ScanblocksTest(BitcoinTestFramework): # send 1.0, mempool only # childkey 5 of `parent_key` wallet.send_to(from_node=node, - scriptPubKey=bytes.fromhex(node.validateaddress("mkS4HXoTYWRTescLGaUTGbtTTYX5EjJyEE")['scriptPubKey']), + scriptPubKey=address_to_scriptpubkey("mkS4HXoTYWRTescLGaUTGbtTTYX5EjJyEE"), amount=1 * COIN) # mine a block and assure that the mined blockhash is in the filterresult diff --git a/test/functional/rpc_signrawtransactionwithkey.py b/test/functional/rpc_signrawtransactionwithkey.py index 0da5a99fdb..580f63063d 100755 --- a/test/functional/rpc_signrawtransactionwithkey.py +++ b/test/functional/rpc_signrawtransactionwithkey.py @@ -8,6 +8,7 @@ from test_framework.blocktools import ( COINBASE_MATURITY, ) from test_framework.address import ( + address_to_scriptpubkey, script_to_p2sh, ) from test_framework.key import ECKey @@ -118,7 +119,7 @@ class SignRawTransactionWithKeyTest(BitcoinTestFramework): }.get(tx_type, "Invalid tx_type") redeem_script = script_to_p2wsh_script(witness_script).hex() addr = script_to_p2sh(redeem_script) - script_pub_key = self.nodes[1].validateaddress(addr)['scriptPubKey'] + script_pub_key = address_to_scriptpubkey(addr).hex() # Fund that address txid = self.send_to_address(addr, 10) vout = find_vout_for_address(self.nodes[0], txid, addr) diff --git a/test/functional/test_framework/blocktools.py b/test/functional/test_framework/blocktools.py index b08cc6a3f9..cfd923bab3 100644 --- a/test/functional/test_framework/blocktools.py +++ b/test/functional/test_framework/blocktools.py @@ -9,6 +9,7 @@ import time import unittest from .address import ( + address_to_scriptpubkey, key_to_p2sh_p2wpkh, key_to_p2wpkh, script_to_p2sh_p2wsh, @@ -205,7 +206,7 @@ def create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount): else: addr = key_to_p2sh_p2wpkh(pubkey) if encode_p2sh else key_to_p2wpkh(pubkey) if not encode_p2sh: - assert_equal(node.getaddressinfo(addr)['scriptPubKey'], witness_script(use_p2wsh, pubkey)) + assert_equal(address_to_scriptpubkey(addr).hex(), witness_script(use_p2wsh, pubkey)) return node.createrawtransaction([utxo], {addr: amount}) def send_to_witness(use_p2wsh, node, utxo, pubkey, encode_p2sh, amount, sign=True, insert_redeem_script=""): diff --git a/test/functional/test_framework/wallet.py b/test/functional/test_framework/wallet.py index 616acfe119..fcd396c700 100644 --- a/test/functional/test_framework/wallet.py +++ b/test/functional/test_framework/wallet.py @@ -13,6 +13,7 @@ from typing import ( Optional, ) from test_framework.address import ( + address_to_scriptpubkey, create_deterministic_address_bcrt1_p2tr_op_true, key_to_p2pkh, key_to_p2sh_p2wpkh, @@ -96,7 +97,7 @@ class MiniWallet: self._scriptPubKey = key_to_p2pk_script(pub_key.get_bytes()) elif mode == MiniWalletMode.ADDRESS_OP_TRUE: self._address, self._internal_key = create_deterministic_address_bcrt1_p2tr_op_true() - self._scriptPubKey = bytes.fromhex(self._test_node.validateaddress(self._address)['scriptPubKey']) + self._scriptPubKey = address_to_scriptpubkey(self._address) # When the pre-mined test framework chain is used, it contains coinbase # outputs to the MiniWallet's default address in blocks 76-100 diff --git a/test/functional/wallet_avoidreuse.py b/test/functional/wallet_avoidreuse.py index 5601d81227..c257bda452 100755 --- a/test/functional/wallet_avoidreuse.py +++ b/test/functional/wallet_avoidreuse.py @@ -4,6 +4,7 @@ # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Test the avoid_reuse and setwalletflag features.""" +from test_framework.address import address_to_scriptpubkey from test_framework.test_framework import BitcoinTestFramework from test_framework.util import ( assert_approx, @@ -257,7 +258,7 @@ class AvoidReuseTest(BitcoinTestFramework): if not self.options.descriptors: # For the second send, we transmute it to a related single-key address # to make sure it's also detected as re-use - fund_spk = self.nodes[0].getaddressinfo(fundaddr)["scriptPubKey"] + fund_spk = address_to_scriptpubkey(fundaddr).hex() fund_decoded = self.nodes[0].decodescript(fund_spk) if second_addr_type == "p2sh-segwit": new_fundaddr = fund_decoded["segwit"]["p2sh-segwit"] diff --git a/test/functional/wallet_fast_rescan.py b/test/functional/wallet_fast_rescan.py index 1ab24f1a96..112aa25e86 100755 --- a/test/functional/wallet_fast_rescan.py +++ b/test/functional/wallet_fast_rescan.py @@ -7,6 +7,7 @@ import os from typing import List +from test_framework.address import address_to_scriptpubkey from test_framework.descriptors import descsum_create from test_framework.test_framework import BitcoinTestFramework from test_framework.test_node import TestNode @@ -58,7 +59,7 @@ class WalletFastRescanTest(BitcoinTestFramework): if 'range' in desc_info: start_range, end_range = desc_info['range'] addr = w.deriveaddresses(desc_info['desc'], [end_range, end_range])[0] - spk = bytes.fromhex(w.getaddressinfo(addr)['scriptPubKey']) + spk = address_to_scriptpubkey(addr) self.log.info(f"-> range [{start_range},{end_range}], last address {addr}") else: spk = bytes.fromhex(fixed_key.p2wpkh_script) -- cgit v1.2.3