Skip to content

Commit 1999dcf

Browse files
committed
test: add helpers for creating P2TR scripts/addresses from output key
1 parent 9e4fbeb commit 1999dcf

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

test/functional/test_framework/address.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ def create_deterministic_address_bcrt1_p2tr_op_true():
4747
Returns a tuple with the generated address and the internal key.
4848
"""
4949
internal_key = (1).to_bytes(32, 'big')
50-
scriptPubKey = taproot_construct(internal_key, [(None, CScript([OP_TRUE]))]).scriptPubKey
51-
address = encode_segwit_address("bcrt", 1, scriptPubKey[2:])
50+
address = output_key_to_p2tr(taproot_construct(internal_key, [(None, CScript([OP_TRUE]))]).output_pubkey)
5251
assert_equal(address, 'bcrt1p9yfmy5h72durp7zrhlw9lf7jpwjgvwdg0jr0lqmmjtgg83266lqsekaqka')
5352
return (address, internal_key)
5453

@@ -141,6 +140,10 @@ def script_to_p2sh_p2wsh(script, main=False):
141140
p2shscript = CScript([OP_0, sha256(script)])
142141
return script_to_p2sh(p2shscript, main)
143142

143+
def output_key_to_p2tr(key, main=False):
144+
assert len(key) == 32
145+
return program_to_witness(1, key, main)
146+
144147
def check_key(key):
145148
if (type(key) is str):
146149
key = bytes.fromhex(key) # Assuming this is hex string

test/functional/test_framework/script_util.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ def script_to_p2sh_p2wsh_script(script):
105105
return script_to_p2sh_script(p2shscript)
106106

107107

108+
def output_key_to_p2tr_script(key):
109+
assert len(key) == 32
110+
return program_to_witness_script(1, key)
111+
112+
108113
def check_key(key):
109114
if isinstance(key, str):
110115
key = bytes.fromhex(key) # Assuming this is hex string

test/functional/wallet_taproot.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,18 @@
77
import random
88

99
from decimal import Decimal
10+
from test_framework.address import output_key_to_p2tr
1011
from test_framework.test_framework import BitcoinTestFramework
1112
from test_framework.util import assert_equal
1213
from test_framework.descriptors import descsum_create
1314
from test_framework.script import (
1415
CScript,
1516
MAX_PUBKEYS_PER_MULTI_A,
16-
OP_1,
1717
OP_CHECKSIG,
1818
OP_CHECKSIGADD,
1919
OP_NUMEQUAL,
2020
taproot_construct,
2121
)
22-
from test_framework.segwit_addr import encode_segwit_address
2322

2423
# xprvs/xpubs, and m/* derived x-only pubkeys (created using independent implementation)
2524
KEYS = [
@@ -183,10 +182,7 @@ def multi_a(k, hex_keys, sort=False):
183182

184183
def compute_taproot_address(pubkey, scripts):
185184
"""Compute the address for a taproot output with given inner key and scripts."""
186-
tap = taproot_construct(pubkey, scripts)
187-
assert tap.scriptPubKey[0] == OP_1
188-
assert tap.scriptPubKey[1] == 0x20
189-
return encode_segwit_address("bcrt", 1, tap.scriptPubKey[2:])
185+
return output_key_to_p2tr(taproot_construct(pubkey, scripts).output_pubkey)
190186

191187
class WalletTaprootTest(BitcoinTestFramework):
192188
"""Test generation and spending of P2TR address outputs."""

0 commit comments

Comments
 (0)