Skip to content

Commit e5b9127

Browse files
committed
test: Add signs P2TR and RAWSCRIPT to MiniWallet sign_tx
1 parent b4fb0a3 commit e5b9127

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

test/functional/test_framework/wallet.py

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -146,20 +146,28 @@ def scan_txs(self, txs):
146146
self.scan_tx(tx)
147147

148148
def sign_tx(self, tx, fixed_length=True):
149-
"""Sign tx that has been created by MiniWallet in P2PK mode"""
150-
assert_equal(self._mode, MiniWalletMode.RAW_P2PK)
151-
(sighash, err) = LegacySignatureHash(CScript(self._scriptPubKey), tx, 0, SIGHASH_ALL)
152-
assert err is None
153-
# for exact fee calculation, create only signatures with fixed size by default (>49.89% probability):
154-
# 65 bytes: high-R val (33 bytes) + low-S val (32 bytes)
155-
# with the DER header/skeleton data of 6 bytes added, this leads to a target size of 71 bytes
156-
der_sig = b''
157-
while not len(der_sig) == 71:
158-
der_sig = self._priv_key.sign_ecdsa(sighash)
159-
if not fixed_length:
160-
break
161-
tx.vin[0].scriptSig = CScript([der_sig + bytes(bytearray([SIGHASH_ALL]))])
162-
tx.rehash()
149+
if self._mode == MiniWalletMode.RAW_P2PK:
150+
(sighash, err) = LegacySignatureHash(CScript(self._scriptPubKey), tx, 0, SIGHASH_ALL)
151+
assert err is None
152+
# for exact fee calculation, create only signatures with fixed size by default (>49.89% probability):
153+
# 65 bytes: high-R val (33 bytes) + low-S val (32 bytes)
154+
# with the DER header/skeleton data of 6 bytes added, this leads to a target size of 71 bytes
155+
der_sig = b''
156+
while not len(der_sig) == 71:
157+
der_sig = self._priv_key.sign_ecdsa(sighash)
158+
if not fixed_length:
159+
break
160+
tx.vin[0].scriptSig = CScript([der_sig + bytes(bytearray([SIGHASH_ALL]))])
161+
tx.rehash()
162+
elif self._mode == MiniWalletMode.RAW_OP_TRUE:
163+
for i in range(len(tx.vin)):
164+
tx.vin[i].scriptSig = CScript([OP_NOP] * 43) # pad to identical size
165+
elif self._mode == MiniWalletMode.ADDRESS_OP_TRUE:
166+
tx.wit.vtxinwit = [CTxInWitness()] * len(tx.vin)
167+
for i in range(len(tx.vin)):
168+
tx.wit.vtxinwit[i].scriptWitness.stack = [CScript([OP_TRUE]), bytes([LEAF_VERSION_TAPSCRIPT]) + self._internal_key]
169+
else:
170+
assert False
163171

164172
def generate(self, num_blocks, **kwargs):
165173
"""Generate blocks with coinbase outputs to the internal address, and call rescan_utxos"""
@@ -273,17 +281,7 @@ def create_self_transfer_multi(
273281
tx.vout = [CTxOut(amount_per_output, bytearray(self._scriptPubKey)) for _ in range(num_outputs)]
274282
tx.nLockTime = locktime
275283

276-
if self._mode == MiniWalletMode.RAW_P2PK:
277-
self.sign_tx(tx)
278-
elif self._mode == MiniWalletMode.RAW_OP_TRUE:
279-
for i in range(len(utxos_to_spend)):
280-
tx.vin[i].scriptSig = CScript([OP_NOP] * 43) # pad to identical size
281-
elif self._mode == MiniWalletMode.ADDRESS_OP_TRUE:
282-
tx.wit.vtxinwit = [CTxInWitness()] * len(utxos_to_spend)
283-
for i in range(len(utxos_to_spend)):
284-
tx.wit.vtxinwit[i].scriptWitness.stack = [CScript([OP_TRUE]), bytes([LEAF_VERSION_TAPSCRIPT]) + self._internal_key]
285-
else:
286-
assert False
284+
self.sign_tx(tx)
287285

288286
if target_weight:
289287
self._bulk_tx(tx, target_weight)

0 commit comments

Comments
 (0)