Skip to content

Commit c98c53f

Browse files
committed
tests: abstract out precomputed BIP341 signature hash elements
1 parent a5bde01 commit c98c53f

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

test/functional/test_framework/script.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,21 @@ def test_cscriptnum_encoding(self):
755755
for value in values:
756756
self.assertEqual(CScriptNum.decode(CScriptNum.encode(CScriptNum(value))), value)
757757

758+
def BIP341_sha_prevouts(txTo):
759+
return sha256(b"".join(i.prevout.serialize() for i in txTo.vin))
760+
761+
def BIP341_sha_amounts(spent_utxos):
762+
return sha256(b"".join(struct.pack("<q", u.nValue) for u in spent_utxos))
763+
764+
def BIP341_sha_scriptpubkeys(spent_utxos):
765+
return sha256(b"".join(ser_string(u.scriptPubKey) for u in spent_utxos))
766+
767+
def BIP341_sha_sequences(txTo):
768+
return sha256(b"".join(struct.pack("<I", i.nSequence) for i in txTo.vin))
769+
770+
def BIP341_sha_outputs(txTo):
771+
return sha256(b"".join(o.serialize() for o in txTo.vout))
772+
758773
def TaprootSignatureMsg(txTo, spent_utxos, hash_type, input_index = 0, scriptpath = False, script = CScript(), codeseparator_pos = -1, annex = None, leaf_ver = LEAF_VERSION_TAPSCRIPT):
759774
assert (len(txTo.vin) == len(spent_utxos))
760775
assert (input_index < len(txTo.vin))
@@ -765,12 +780,12 @@ def TaprootSignatureMsg(txTo, spent_utxos, hash_type, input_index = 0, scriptpat
765780
ss += struct.pack("<i", txTo.nVersion)
766781
ss += struct.pack("<I", txTo.nLockTime)
767782
if in_type != SIGHASH_ANYONECANPAY:
768-
ss += sha256(b"".join(i.prevout.serialize() for i in txTo.vin))
769-
ss += sha256(b"".join(struct.pack("<q", u.nValue) for u in spent_utxos))
770-
ss += sha256(b"".join(ser_string(u.scriptPubKey) for u in spent_utxos))
771-
ss += sha256(b"".join(struct.pack("<I", i.nSequence) for i in txTo.vin))
783+
ss += BIP341_sha_prevouts(txTo)
784+
ss += BIP341_sha_amounts(spent_utxos)
785+
ss += BIP341_sha_scriptpubkeys(spent_utxos)
786+
ss += BIP341_sha_sequences(txTo)
772787
if out_type == SIGHASH_ALL:
773-
ss += sha256(b"".join(o.serialize() for o in txTo.vout))
788+
ss += BIP341_sha_outputs(txTo)
774789
spend_type = 0
775790
if annex is not None:
776791
spend_type |= 1

0 commit comments

Comments
 (0)