Skip to content

Commit e47ce42

Browse files
committed
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.
1 parent 4142d19 commit e47ce42

File tree

8 files changed

+16
-8
lines changed

8 files changed

+16
-8
lines changed

test/functional/feature_nulldummy.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"""
1515
import time
1616

17+
from test_framework.address import address_to_scriptpubkey
1718
from test_framework.blocktools import (
1819
COINBASE_MATURITY,
1920
NORMAL_GBT_REQUEST_PARAMS,
@@ -77,7 +78,7 @@ def run_test(self):
7778
cms = self.nodes[0].createmultisig(1, [self.pubkey])
7879
wms = self.nodes[0].createmultisig(1, [self.pubkey], 'p2sh-segwit')
7980
self.ms_address = cms["address"]
80-
ms_unlock_details = {"scriptPubKey": self.nodes[0].validateaddress(self.ms_address)["scriptPubKey"],
81+
ms_unlock_details = {"scriptPubKey": address_to_scriptpubkey(self.ms_address).hex(),
8182
"redeemScript": cms["redeemScript"]}
8283
self.wit_ms_address = wms['address']
8384

test/functional/rpc_createmultisig.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import json
99
import os
1010

11+
from test_framework.address import address_to_scriptpubkey
1112
from test_framework.blocktools import COINBASE_MATURITY
1213
from test_framework.authproxy import JSONRPCException
1314
from test_framework.descriptors import descsum_create, drop_origins
@@ -193,7 +194,7 @@ def do_multisig(self):
193194
assert mredeemw == mredeem
194195
wmulti.unloadwallet()
195196

196-
spk = bytes.fromhex(node0.validateaddress(madd)["scriptPubKey"])
197+
spk = address_to_scriptpubkey(madd)
197198
txid, _ = self.wallet.send_to(from_node=self.nodes[0], scriptPubKey=spk, amount=1300)
198199
tx = node0.getrawtransaction(txid, True)
199200
vout = [v["n"] for v in tx["vout"] if madd == v["scriptPubKey"]["address"]]

test/functional/rpc_scanblocks.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test the scanblocks RPC call."""
6+
from test_framework.address import address_to_scriptpubkey
67
from test_framework.blockfilter import (
78
bip158_basic_element_hash,
89
bip158_relevant_scriptpubkeys,
@@ -36,7 +37,7 @@ def run_test(self):
3637
# send 1.0, mempool only
3738
# childkey 5 of `parent_key`
3839
wallet.send_to(from_node=node,
39-
scriptPubKey=bytes.fromhex(node.validateaddress("mkS4HXoTYWRTescLGaUTGbtTTYX5EjJyEE")['scriptPubKey']),
40+
scriptPubKey=address_to_scriptpubkey("mkS4HXoTYWRTescLGaUTGbtTTYX5EjJyEE"),
4041
amount=1 * COIN)
4142

4243
# mine a block and assure that the mined blockhash is in the filterresult

test/functional/rpc_signrawtransactionwithkey.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
COINBASE_MATURITY,
99
)
1010
from test_framework.address import (
11+
address_to_scriptpubkey,
1112
script_to_p2sh,
1213
)
1314
from test_framework.key import ECKey
@@ -118,7 +119,7 @@ def verify_txn_with_witness_script(self, tx_type):
118119
}.get(tx_type, "Invalid tx_type")
119120
redeem_script = script_to_p2wsh_script(witness_script).hex()
120121
addr = script_to_p2sh(redeem_script)
121-
script_pub_key = self.nodes[1].validateaddress(addr)['scriptPubKey']
122+
script_pub_key = address_to_scriptpubkey(addr).hex()
122123
# Fund that address
123124
txid = self.send_to_address(addr, 10)
124125
vout = find_vout_for_address(self.nodes[0], txid, addr)

test/functional/test_framework/blocktools.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import unittest
1010

1111
from .address import (
12+
address_to_scriptpubkey,
1213
key_to_p2sh_p2wpkh,
1314
key_to_p2wpkh,
1415
script_to_p2sh_p2wsh,
@@ -205,7 +206,7 @@ def create_witness_tx(node, use_p2wsh, utxo, pubkey, encode_p2sh, amount):
205206
else:
206207
addr = key_to_p2sh_p2wpkh(pubkey) if encode_p2sh else key_to_p2wpkh(pubkey)
207208
if not encode_p2sh:
208-
assert_equal(node.getaddressinfo(addr)['scriptPubKey'], witness_script(use_p2wsh, pubkey))
209+
assert_equal(address_to_scriptpubkey(addr).hex(), witness_script(use_p2wsh, pubkey))
209210
return node.createrawtransaction([utxo], {addr: amount})
210211

211212
def send_to_witness(use_p2wsh, node, utxo, pubkey, encode_p2sh, amount, sign=True, insert_redeem_script=""):

test/functional/test_framework/wallet.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
Optional,
1414
)
1515
from test_framework.address import (
16+
address_to_scriptpubkey,
1617
create_deterministic_address_bcrt1_p2tr_op_true,
1718
key_to_p2pkh,
1819
key_to_p2sh_p2wpkh,
@@ -96,7 +97,7 @@ def __init__(self, test_node, *, mode=MiniWalletMode.ADDRESS_OP_TRUE):
9697
self._scriptPubKey = key_to_p2pk_script(pub_key.get_bytes())
9798
elif mode == MiniWalletMode.ADDRESS_OP_TRUE:
9899
self._address, self._internal_key = create_deterministic_address_bcrt1_p2tr_op_true()
99-
self._scriptPubKey = bytes.fromhex(self._test_node.validateaddress(self._address)['scriptPubKey'])
100+
self._scriptPubKey = address_to_scriptpubkey(self._address)
100101

101102
# When the pre-mined test framework chain is used, it contains coinbase
102103
# outputs to the MiniWallet's default address in blocks 76-100

test/functional/wallet_avoidreuse.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test the avoid_reuse and setwalletflag features."""
66

7+
from test_framework.address import address_to_scriptpubkey
78
from test_framework.test_framework import BitcoinTestFramework
89
from test_framework.util import (
910
assert_approx,
@@ -257,7 +258,7 @@ def test_sending_from_reused_address_fails(self, second_addr_type):
257258
if not self.options.descriptors:
258259
# For the second send, we transmute it to a related single-key address
259260
# to make sure it's also detected as re-use
260-
fund_spk = self.nodes[0].getaddressinfo(fundaddr)["scriptPubKey"]
261+
fund_spk = address_to_scriptpubkey(fundaddr).hex()
261262
fund_decoded = self.nodes[0].decodescript(fund_spk)
262263
if second_addr_type == "p2sh-segwit":
263264
new_fundaddr = fund_decoded["segwit"]["p2sh-segwit"]

test/functional/wallet_fast_rescan.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
from typing import List
99

10+
from test_framework.address import address_to_scriptpubkey
1011
from test_framework.descriptors import descsum_create
1112
from test_framework.test_framework import BitcoinTestFramework
1213
from test_framework.test_node import TestNode
@@ -58,7 +59,7 @@ def run_test(self):
5859
if 'range' in desc_info:
5960
start_range, end_range = desc_info['range']
6061
addr = w.deriveaddresses(desc_info['desc'], [end_range, end_range])[0]
61-
spk = bytes.fromhex(w.getaddressinfo(addr)['scriptPubKey'])
62+
spk = address_to_scriptpubkey(addr)
6263
self.log.info(f"-> range [{start_range},{end_range}], last address {addr}")
6364
else:
6465
spk = bytes.fromhex(fixed_key.p2wpkh_script)

0 commit comments

Comments
 (0)