Skip to content

Commit 983ca04

Browse files
committed
test: introduce address_to_scriptpubkey helper
Works only with legacy addresses (Base58Check) right now.
1 parent e704d4d commit 983ca04

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

test/functional/test_framework/wallet.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from random import choice
1111
from typing import Optional
1212
from test_framework.address import (
13+
base58_to_byte,
1314
create_deterministic_address_bcrt1_p2tr_op_true,
1415
key_to_p2pkh,
1516
key_to_p2sh_p2wpkh,
@@ -39,6 +40,8 @@
3940
key_to_p2pkh_script,
4041
key_to_p2sh_p2wpkh_script,
4142
key_to_p2wpkh_script,
43+
keyhash_to_p2pkh_script,
44+
scripthash_to_p2sh_script,
4245
)
4346
from test_framework.util import (
4447
assert_equal,
@@ -240,6 +243,18 @@ def getnewdestination(address_type='bech32'):
240243
return pubkey, scriptpubkey, address
241244

242245

246+
def address_to_scriptpubkey(address):
247+
"""Converts a given address to the corresponding output script (scriptPubKey)."""
248+
payload, version = base58_to_byte(address)
249+
if version == 111: # testnet pubkey hash
250+
return keyhash_to_p2pkh_script(payload)
251+
elif version == 196: # testnet script hash
252+
return scripthash_to_p2sh_script(payload)
253+
# TODO: also support other address formats
254+
else:
255+
assert False
256+
257+
243258
def make_chain(node, address, privkeys, parent_txid, parent_value, n=0, parent_locking_script=None, fee=DEFAULT_FEE):
244259
"""Build a transaction that spends parent_txid.vout[n] and produces one output with
245260
amount = parent_value with a fee deducted.

0 commit comments

Comments
 (0)