@@ -755,6 +755,21 @@ def test_cscriptnum_encoding(self):
755
755
for value in values :
756
756
self .assertEqual (CScriptNum .decode (CScriptNum .encode (CScriptNum (value ))), value )
757
757
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
+
758
773
def TaprootSignatureMsg (txTo , spent_utxos , hash_type , input_index = 0 , scriptpath = False , script = CScript (), codeseparator_pos = - 1 , annex = None , leaf_ver = LEAF_VERSION_TAPSCRIPT ):
759
774
assert (len (txTo .vin ) == len (spent_utxos ))
760
775
assert (input_index < len (txTo .vin ))
@@ -765,12 +780,12 @@ def TaprootSignatureMsg(txTo, spent_utxos, hash_type, input_index = 0, scriptpat
765
780
ss += struct .pack ("<i" , txTo .nVersion )
766
781
ss += struct .pack ("<I" , txTo .nLockTime )
767
782
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 )
772
787
if out_type == SIGHASH_ALL :
773
- ss += sha256 ( b"" . join ( o . serialize () for o in txTo . vout ) )
788
+ ss += BIP341_sha_outputs ( txTo )
774
789
spend_type = 0
775
790
if annex is not None :
776
791
spend_type |= 1
0 commit comments