Skip to content

Commit 4142d19

Browse files
committed
refactor: move address_to_scriptpubkey to address.py
The COINBASE_MATURITY constant in blocktools.py is imported in wallet.py. However, importing address_to_scriptpubkey to blocktools.py will generate a circular import error. Since the method is related to addresses, it is best to move it to address.py, which will also fix the circular import error. Update imports of address_to_scriptpubkey accordingly.
1 parent b759cef commit 4142d19

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

test/functional/rpc_scantxoutset.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
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 scantxoutset rpc call."""
6+
from test_framework.address import address_to_scriptpubkey
67
from test_framework.messages import COIN
78
from test_framework.test_framework import BitcoinTestFramework
89
from test_framework.util import assert_equal, assert_raises_rpc_error
910
from test_framework.wallet import (
1011
MiniWallet,
11-
address_to_scriptpubkey,
1212
getnewdestination,
1313
)
1414

test/functional/test_framework/address.py

+21
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@
2121
taproot_construct,
2222
)
2323
from .util import assert_equal
24+
from test_framework.script_util import (
25+
keyhash_to_p2pkh_script,
26+
program_to_witness_script,
27+
scripthash_to_p2sh_script,
28+
)
2429
from test_framework.segwit_addr import (
2530
decode_segwit_address,
2631
encode_segwit_address,
2732
)
2833

34+
2935
ADDRESS_BCRT1_UNSPENDABLE = 'bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj'
3036
ADDRESS_BCRT1_UNSPENDABLE_DESCRIPTOR = 'addr(bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj)#juyq9d97'
3137
# Coins sent to this address can be spent with a witness stack of just OP_TRUE
@@ -172,6 +178,21 @@ def bech32_to_bytes(address):
172178
return version, bytearray(payload)
173179

174180

181+
def address_to_scriptpubkey(address):
182+
"""Converts a given address to the corresponding output script (scriptPubKey)."""
183+
version, payload = bech32_to_bytes(address)
184+
if version is not None:
185+
return program_to_witness_script(version, payload) # testnet segwit scriptpubkey
186+
payload, version = base58_to_byte(address)
187+
if version == 111: # testnet pubkey hash
188+
return keyhash_to_p2pkh_script(payload)
189+
elif version == 196: # testnet script hash
190+
return scripthash_to_p2sh_script(payload)
191+
# TODO: also support other address formats
192+
else:
193+
assert False
194+
195+
175196
class TestFrameworkScript(unittest.TestCase):
176197
def test_base58encodedecode(self):
177198
def check_base58(data, version):

test/functional/test_framework/wallet.py

-20
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
Optional,
1414
)
1515
from test_framework.address import (
16-
base58_to_byte,
17-
bech32_to_bytes,
1816
create_deterministic_address_bcrt1_p2tr_op_true,
1917
key_to_p2pkh,
2018
key_to_p2sh_p2wpkh,
@@ -49,9 +47,6 @@
4947
key_to_p2pkh_script,
5048
key_to_p2sh_p2wpkh_script,
5149
key_to_p2wpkh_script,
52-
keyhash_to_p2pkh_script,
53-
program_to_witness_script,
54-
scripthash_to_p2sh_script,
5550
)
5651
from test_framework.util import (
5752
assert_equal,
@@ -412,18 +407,3 @@ def getnewdestination(address_type='bech32m'):
412407
else:
413408
assert False
414409
return pubkey, scriptpubkey, address
415-
416-
417-
def address_to_scriptpubkey(address):
418-
"""Converts a given address to the corresponding output script (scriptPubKey)."""
419-
version, payload = bech32_to_bytes(address)
420-
if version is not None:
421-
return program_to_witness_script(version, payload) # testnet segwit scriptpubkey
422-
payload, version = base58_to_byte(address)
423-
if version == 111: # testnet pubkey hash
424-
return keyhash_to_p2pkh_script(payload)
425-
elif version == 196: # testnet script hash
426-
return scripthash_to_p2sh_script(payload)
427-
# TODO: also support other address formats
428-
else:
429-
assert False

0 commit comments

Comments
 (0)