test: Add signs P2TR and RAWSCRIPT to MiniWallet sign_tx

This commit is contained in:
Miles Liu
2022-12-12 18:22:28 +08:00
parent b4fb0a3255
commit e5b9127d9e

View File

@@ -146,8 +146,7 @@ class MiniWallet:
self.scan_tx(tx) self.scan_tx(tx)
def sign_tx(self, tx, fixed_length=True): def sign_tx(self, tx, fixed_length=True):
"""Sign tx that has been created by MiniWallet in P2PK mode""" if self._mode == MiniWalletMode.RAW_P2PK:
assert_equal(self._mode, MiniWalletMode.RAW_P2PK)
(sighash, err) = LegacySignatureHash(CScript(self._scriptPubKey), tx, 0, SIGHASH_ALL) (sighash, err) = LegacySignatureHash(CScript(self._scriptPubKey), tx, 0, SIGHASH_ALL)
assert err is None assert err is None
# for exact fee calculation, create only signatures with fixed size by default (>49.89% probability): # for exact fee calculation, create only signatures with fixed size by default (>49.89% probability):
@@ -160,6 +159,15 @@ class MiniWallet:
break break
tx.vin[0].scriptSig = CScript([der_sig + bytes(bytearray([SIGHASH_ALL]))]) tx.vin[0].scriptSig = CScript([der_sig + bytes(bytearray([SIGHASH_ALL]))])
tx.rehash() tx.rehash()
elif self._mode == MiniWalletMode.RAW_OP_TRUE:
for i in range(len(tx.vin)):
tx.vin[i].scriptSig = CScript([OP_NOP] * 43) # pad to identical size
elif self._mode == MiniWalletMode.ADDRESS_OP_TRUE:
tx.wit.vtxinwit = [CTxInWitness()] * len(tx.vin)
for i in range(len(tx.vin)):
tx.wit.vtxinwit[i].scriptWitness.stack = [CScript([OP_TRUE]), bytes([LEAF_VERSION_TAPSCRIPT]) + self._internal_key]
else:
assert False
def generate(self, num_blocks, **kwargs): def generate(self, num_blocks, **kwargs):
"""Generate blocks with coinbase outputs to the internal address, and call rescan_utxos""" """Generate blocks with coinbase outputs to the internal address, and call rescan_utxos"""
@@ -273,17 +281,7 @@ class MiniWallet:
tx.vout = [CTxOut(amount_per_output, bytearray(self._scriptPubKey)) for _ in range(num_outputs)] tx.vout = [CTxOut(amount_per_output, bytearray(self._scriptPubKey)) for _ in range(num_outputs)]
tx.nLockTime = locktime tx.nLockTime = locktime
if self._mode == MiniWalletMode.RAW_P2PK:
self.sign_tx(tx) self.sign_tx(tx)
elif self._mode == MiniWalletMode.RAW_OP_TRUE:
for i in range(len(utxos_to_spend)):
tx.vin[i].scriptSig = CScript([OP_NOP] * 43) # pad to identical size
elif self._mode == MiniWalletMode.ADDRESS_OP_TRUE:
tx.wit.vtxinwit = [CTxInWitness()] * len(utxos_to_spend)
for i in range(len(utxos_to_spend)):
tx.wit.vtxinwit[i].scriptWitness.stack = [CScript([OP_TRUE]), bytes([LEAF_VERSION_TAPSCRIPT]) + self._internal_key]
else:
assert False
if target_weight: if target_weight:
self._bulk_tx(tx, target_weight) self._bulk_tx(tx, target_weight)