test: refactor: return TaprootInfo from P2TR address creation routine

Rather than only returning the internal key from the P2TR anyone-can-spend
address creation routine, provide the whole TaprootInfo object, which in turn
contains a dictionary of TaprootLeafInfo object for named leaves.

This data is used in MiniWallet for the default ADDRESS_OP_TRUE mode, in order
to deduplicate the witness script and leaf version of the control block.
This commit is contained in:
Sebastian Falbesoner 2024-05-09 18:52:27 +02:00
parent e6e4c18a9b
commit 7774c314fb
2 changed files with 11 additions and 6 deletions

View file

@ -53,13 +53,14 @@ def create_deterministic_address_bcrt1_p2tr_op_true(explicit_internal_key=None):
can be spent with a witness stack of OP_TRUE and the control block can be spent with a witness stack of OP_TRUE and the control block
with internal public key (script-path spending). with internal public key (script-path spending).
Returns a tuple with the generated address and the internal key. Returns a tuple with the generated address and the TaprootInfo object.
""" """
internal_key = explicit_internal_key or (1).to_bytes(32, 'big') internal_key = explicit_internal_key or (1).to_bytes(32, 'big')
address = output_key_to_p2tr(taproot_construct(internal_key, [(None, CScript([OP_TRUE]))]).output_pubkey) taproot_info = taproot_construct(internal_key, [("only-path", CScript([OP_TRUE]))])
address = output_key_to_p2tr(taproot_info.output_pubkey)
if explicit_internal_key is None: if explicit_internal_key is None:
assert_equal(address, 'bcrt1p9yfmy5h72durp7zrhlw9lf7jpwjgvwdg0jr0lqmmjtgg83266lqsekaqka') assert_equal(address, 'bcrt1p9yfmy5h72durp7zrhlw9lf7jpwjgvwdg0jr0lqmmjtgg83266lqsekaqka')
return (address, internal_key) return (address, taproot_info)
def byte_to_base58(b, version): def byte_to_base58(b, version):

View file

@ -39,7 +39,6 @@ from test_framework.messages import (
) )
from test_framework.script import ( from test_framework.script import (
CScript, CScript,
LEAF_VERSION_TAPSCRIPT,
OP_1, OP_1,
OP_NOP, OP_NOP,
OP_RETURN, OP_RETURN,
@ -107,7 +106,7 @@ class MiniWallet:
self._scriptPubKey = key_to_p2pk_script(pub_key.get_bytes()) self._scriptPubKey = key_to_p2pk_script(pub_key.get_bytes())
elif mode == MiniWalletMode.ADDRESS_OP_TRUE: elif mode == MiniWalletMode.ADDRESS_OP_TRUE:
internal_key = None if tag_name is None else hash256(tag_name.encode()) internal_key = None if tag_name is None else hash256(tag_name.encode())
self._address, self._internal_key = create_deterministic_address_bcrt1_p2tr_op_true(internal_key) self._address, self._taproot_info = create_deterministic_address_bcrt1_p2tr_op_true(internal_key)
self._scriptPubKey = address_to_scriptpubkey(self._address) self._scriptPubKey = address_to_scriptpubkey(self._address)
# When the pre-mined test framework chain is used, it contains coinbase # When the pre-mined test framework chain is used, it contains coinbase
@ -195,7 +194,12 @@ class MiniWallet:
elif self._mode == MiniWalletMode.ADDRESS_OP_TRUE: elif self._mode == MiniWalletMode.ADDRESS_OP_TRUE:
tx.wit.vtxinwit = [CTxInWitness()] * len(tx.vin) tx.wit.vtxinwit = [CTxInWitness()] * len(tx.vin)
for i in tx.wit.vtxinwit: for i in tx.wit.vtxinwit:
i.scriptWitness.stack = [CScript([OP_TRUE]), bytes([LEAF_VERSION_TAPSCRIPT]) + self._internal_key] assert_equal(len(self._taproot_info.leaves), 1)
leaf_info = list(self._taproot_info.leaves.values())[0]
i.scriptWitness.stack = [
leaf_info.script,
bytes([leaf_info.version]) + self._taproot_info.internal_pubkey,
]
else: else:
assert False assert False