Skip to content

Commit b2037ad

Browse files
committed
test: add MiniWallet tagging support to avoid UTXO mixing
Note that this commit doesn't change behaviour yet, as tagging isn't used in any MiniWallet instance.
1 parent c8e6d08 commit b2037ad

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

test/functional/test_framework/address.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,18 @@ class AddressType(enum.Enum):
4747
b58chars = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
4848

4949

50-
def create_deterministic_address_bcrt1_p2tr_op_true():
50+
def create_deterministic_address_bcrt1_p2tr_op_true(explicit_internal_key=None):
5151
"""
5252
Generates a deterministic bech32m address (segwit v1 output) that
5353
can be spent with a witness stack of OP_TRUE and the control block
5454
with internal public key (script-path spending).
5555
5656
Returns a tuple with the generated address and the internal key.
5757
"""
58-
internal_key = (1).to_bytes(32, 'big')
58+
internal_key = explicit_internal_key or (1).to_bytes(32, 'big')
5959
address = output_key_to_p2tr(taproot_construct(internal_key, [(None, CScript([OP_TRUE]))]).output_pubkey)
60-
assert_equal(address, 'bcrt1p9yfmy5h72durp7zrhlw9lf7jpwjgvwdg0jr0lqmmjtgg83266lqsekaqka')
60+
if explicit_internal_key is None:
61+
assert_equal(address, 'bcrt1p9yfmy5h72durp7zrhlw9lf7jpwjgvwdg0jr0lqmmjtgg83266lqsekaqka')
6162
return (address, internal_key)
6263

6364

test/functional/test_framework/wallet.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
CTxIn,
3333
CTxInWitness,
3434
CTxOut,
35+
hash256,
3536
)
3637
from test_framework.script import (
3738
CScript,
@@ -65,7 +66,10 @@ class MiniWalletMode(Enum):
6566
However, if the transactions need to be modified by the user (e.g. prepending
6667
scriptSig for testing opcodes that are activated by a soft-fork), or the txs
6768
should contain an actual signature, the raw modes RAW_OP_TRUE and RAW_P2PK
68-
can be useful. Summary of modes:
69+
can be useful. In order to avoid mixing of UTXOs between different MiniWallet
70+
instances, a tag name can be passed to the default mode, to create different
71+
output scripts. Note that the UTXOs from the pre-generated test chain can
72+
only be spent if no tag is passed. Summary of modes:
6973
7074
| output | | tx is | can modify | needs
7175
mode | description | address | standard | scriptSig | signing
@@ -80,22 +84,25 @@ class MiniWalletMode(Enum):
8084

8185

8286
class MiniWallet:
83-
def __init__(self, test_node, *, mode=MiniWalletMode.ADDRESS_OP_TRUE):
87+
def __init__(self, test_node, *, mode=MiniWalletMode.ADDRESS_OP_TRUE, tag_name=None):
8488
self._test_node = test_node
8589
self._utxos = []
8690
self._mode = mode
8791

8892
assert isinstance(mode, MiniWalletMode)
8993
if mode == MiniWalletMode.RAW_OP_TRUE:
94+
assert tag_name is None
9095
self._scriptPubKey = bytes(CScript([OP_TRUE]))
9196
elif mode == MiniWalletMode.RAW_P2PK:
9297
# use simple deterministic private key (k=1)
98+
assert tag_name is None
9399
self._priv_key = ECKey()
94100
self._priv_key.set((1).to_bytes(32, 'big'), True)
95101
pub_key = self._priv_key.get_pubkey()
96102
self._scriptPubKey = key_to_p2pk_script(pub_key.get_bytes())
97103
elif mode == MiniWalletMode.ADDRESS_OP_TRUE:
98-
self._address, self._internal_key = create_deterministic_address_bcrt1_p2tr_op_true()
104+
internal_key = None if tag_name is None else hash256(tag_name.encode())
105+
self._address, self._internal_key = create_deterministic_address_bcrt1_p2tr_op_true(internal_key)
99106
self._scriptPubKey = address_to_scriptpubkey(self._address)
100107

101108
# When the pre-mined test framework chain is used, it contains coinbase

0 commit comments

Comments
 (0)