@@ -619,16 +619,15 @@ def FindAndDelete(script, sig):
619
619
r += script [last_sop_idx :]
620
620
return CScript (r )
621
621
622
- def LegacySignatureHash (script , txTo , inIdx , hashtype ):
623
- """Consensus-correct SignatureHash
622
+ def LegacySignatureMsg (script , txTo , inIdx , hashtype ):
623
+ """Preimage of the signature hash, if it exists.
624
624
625
- Returns (hash , err) to precisely match the consensus-critical behavior of
626
- the SIGHASH_SINGLE bug. (inIdx is *not* checked for validity)
625
+ Returns either (None , err) to indicate error (which translates to sighash 1),
626
+ or (msg, None).
627
627
"""
628
- HASH_ONE = b'\x01 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 '
629
628
630
629
if inIdx >= len (txTo .vin ):
631
- return (HASH_ONE , "inIdx %d out of range (%d)" % (inIdx , len (txTo .vin )))
630
+ return (None , "inIdx %d out of range (%d)" % (inIdx , len (txTo .vin )))
632
631
txtmp = CTransaction (txTo )
633
632
634
633
for txin in txtmp .vin :
@@ -645,7 +644,7 @@ def LegacySignatureHash(script, txTo, inIdx, hashtype):
645
644
elif (hashtype & 0x1f ) == SIGHASH_SINGLE :
646
645
outIdx = inIdx
647
646
if outIdx >= len (txtmp .vout ):
648
- return (HASH_ONE , "outIdx %d out of range (%d)" % (outIdx , len (txtmp .vout )))
647
+ return (None , "outIdx %d out of range (%d)" % (outIdx , len (txtmp .vout )))
649
648
650
649
tmp = txtmp .vout [outIdx ]
651
650
txtmp .vout = []
@@ -665,15 +664,27 @@ def LegacySignatureHash(script, txTo, inIdx, hashtype):
665
664
s = txtmp .serialize_without_witness ()
666
665
s += struct .pack (b"<I" , hashtype )
667
666
668
- hash = hash256 ( s )
667
+ return ( s , None )
669
668
670
- return (hash , None )
669
+ def LegacySignatureHash (* args , ** kwargs ):
670
+ """Consensus-correct SignatureHash
671
+
672
+ Returns (hash, err) to precisely match the consensus-critical behavior of
673
+ the SIGHASH_SINGLE bug. (inIdx is *not* checked for validity)
674
+ """
675
+
676
+ HASH_ONE = b'\x01 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 '
677
+ msg , err = LegacySignatureMsg (* args , ** kwargs )
678
+ if msg is None :
679
+ return (HASH_ONE , err )
680
+ else :
681
+ return (hash256 (msg ), err )
671
682
672
683
# TODO: Allow cached hashPrevouts/hashSequence/hashOutputs to be provided.
673
684
# Performance optimization probably not necessary for python tests, however.
674
685
# Note that this corresponds to sigversion == 1 in EvalScript, which is used
675
686
# for version 0 witnesses.
676
- def SegwitV0SignatureHash (script , txTo , inIdx , hashtype , amount ):
687
+ def SegwitV0SignatureMsg (script , txTo , inIdx , hashtype , amount ):
677
688
678
689
hashPrevouts = 0
679
690
hashSequence = 0
@@ -711,8 +722,10 @@ def SegwitV0SignatureHash(script, txTo, inIdx, hashtype, amount):
711
722
ss += ser_uint256 (hashOutputs )
712
723
ss += struct .pack ("<i" , txTo .nLockTime )
713
724
ss += struct .pack ("<I" , hashtype )
725
+ return ss
714
726
715
- return hash256 (ss )
727
+ def SegwitV0SignatureHash (* args , ** kwargs ):
728
+ return hash256 (SegwitV0SignatureMsg (* args , ** kwargs ))
716
729
717
730
class TestFrameworkScript (unittest .TestCase ):
718
731
def test_bn2vch (self ):
@@ -742,7 +755,7 @@ def test_cscriptnum_encoding(self):
742
755
for value in values :
743
756
self .assertEqual (CScriptNum .decode (CScriptNum .encode (CScriptNum (value ))), value )
744
757
745
- def TaprootSignatureHash (txTo , spent_utxos , hash_type , input_index = 0 , scriptpath = False , script = CScript (), codeseparator_pos = - 1 , annex = None , leaf_ver = LEAF_VERSION_TAPSCRIPT ):
758
+ def TaprootSignatureMsg (txTo , spent_utxos , hash_type , input_index = 0 , scriptpath = False , script = CScript (), codeseparator_pos = - 1 , annex = None , leaf_ver = LEAF_VERSION_TAPSCRIPT ):
746
759
assert (len (txTo .vin ) == len (spent_utxos ))
747
760
assert (input_index < len (txTo .vin ))
748
761
out_type = SIGHASH_ALL if hash_type == 0 else hash_type & 3
@@ -783,7 +796,10 @@ def TaprootSignatureHash(txTo, spent_utxos, hash_type, input_index = 0, scriptpa
783
796
ss += bytes ([0 ])
784
797
ss += struct .pack ("<i" , codeseparator_pos )
785
798
assert len (ss ) == 175 - (in_type == SIGHASH_ANYONECANPAY ) * 49 - (out_type != SIGHASH_ALL and out_type != SIGHASH_SINGLE ) * 32 + (annex is not None ) * 32 + scriptpath * 37
786
- return TaggedHash ("TapSighash" , ss )
799
+ return ss
800
+
801
+ def TaprootSignatureHash (* args , ** kwargs ):
802
+ return TaggedHash ("TapSighash" , TaprootSignatureMsg (* args , ** kwargs ))
787
803
788
804
def taproot_tree_helper (scripts ):
789
805
if len (scripts ) == 0 :
0 commit comments