@@ -84,24 +84,21 @@ def _FindAndDelete(script, sig):
84
84
return CScript (script_bytes )
85
85
86
86
87
- def _CheckSig (sig , pubkey , script , txTo , inIdx , hashtype ):
87
+ def _CheckSig (sig , pubkey , script , txTo , inIdx ):
88
88
key = bitcoin .core .key .CKey ()
89
89
key .set_pubkey (pubkey )
90
90
91
91
if len (sig ) == 0 :
92
92
return False
93
- if hashtype == 0 :
94
- hashtype = bord (sig [- 1 ])
95
- elif hashtype != bord (sig [- 1 ]):
96
- return False
93
+ hashtype = bord (sig [- 1 ])
97
94
sig = sig [:- 1 ]
98
95
99
96
# Raw signature hash due to the SIGHASH_SINGLE bug
100
97
(h , err ) = RawSignatureHash (script , txTo , inIdx , hashtype )
101
98
return key .verify (h , sig )
102
99
103
100
104
- def _CheckMultiSig (opcode , script , stack , txTo , inIdx , hashtype ):
101
+ def _CheckMultiSig (opcode , script , stack , txTo , inIdx ):
105
102
i = 1
106
103
if len (stack ) < i :
107
104
raise _MissingOpArgumentsError (opcode , stack , i )
@@ -138,7 +135,7 @@ def _CheckMultiSig(opcode, script, stack, txTo, inIdx, hashtype):
138
135
sig = stack [- isig ]
139
136
pubkey = stack [- ikey ]
140
137
141
- if _CheckSig (sig , pubkey , script , txTo , inIdx , hashtype ):
138
+ if _CheckSig (sig , pubkey , script , txTo , inIdx ):
142
139
isig += 1
143
140
sigs_count -= 1
144
141
@@ -296,7 +293,15 @@ def _CheckExec(vfExec):
296
293
return True
297
294
298
295
299
- def _EvalScript (stack , scriptIn , txTo , inIdx , hashtype , flags = ()):
296
+ def _EvalScript (stack , scriptIn , txTo , inIdx , flags = ()):
297
+ """Evaluate a script
298
+
299
+ stack - Initial stack
300
+ scriptIn - Script
301
+ txTo - Transaction the script is a part of
302
+ inIdx - txin index of the scriptSig
303
+ flags - SCRIPT_VERIFY_* flags to apply
304
+ """
300
305
if len (scriptIn ) > MAX_SCRIPT_SIZE :
301
306
raise EvalScriptError ('script too large; got %d bytes; maximum %d bytes' %
302
307
(len (scriptIn ), MAX_SCRIPT_SIZE ))
@@ -391,8 +396,7 @@ def check_args(n):
391
396
392
397
elif fExec and sop == OP_CHECKMULTISIG or sop == OP_CHECKMULTISIGVERIFY :
393
398
tmpScript = CScript (scriptIn [pbegincodehash :])
394
- ok = _CheckMultiSig (sop , tmpScript , stack , txTo ,
395
- inIdx , hashtype )
399
+ ok = _CheckMultiSig (sop , tmpScript , stack , txTo , inIdx )
396
400
if not ok :
397
401
return False
398
402
@@ -409,7 +413,7 @@ def check_args(n):
409
413
tmpScript = _FindAndDelete (tmpScript , vchSig )
410
414
411
415
ok = _CheckSig (vchSig , vchPubKey , tmpScript ,
412
- txTo , inIdx , hashtype )
416
+ txTo , inIdx )
413
417
if ok :
414
418
if sop != OP_CHECKSIGVERIFY :
415
419
stack .append (b"\x01 " )
@@ -598,22 +602,29 @@ def check_args(n):
598
602
599
603
return True
600
604
601
- def EvalScript (stack , scriptIn , txTo , inIdx , hashtype , flags = ()):
605
+ def EvalScript (stack , scriptIn , txTo , inIdx , flags = ()):
602
606
try :
603
- return _EvalScript (stack , scriptIn , txTo , inIdx , hashtype , flags = flags )
607
+ return _EvalScript (stack , scriptIn , txTo , inIdx , flags = flags )
604
608
except CScriptInvalidError :
605
609
return False
606
610
except EvalScriptError :
607
611
return False
608
612
609
613
610
- def VerifyScript (scriptSig , scriptPubKey , txTo , inIdx , hashtype , flags = ()):
614
+ def VerifyScript (scriptSig , scriptPubKey , txTo , inIdx , flags = ()):
615
+ """Verify a scriptSig satisfies a scriptPubKey
616
+
617
+ scriptSig - Signature
618
+ scriptPubKey - PubKey
619
+ txTo - Spending transaction
620
+ inIdx - Index of the transaction input containing scriptSig
621
+ """
611
622
stack = []
612
- if not EvalScript (stack , scriptSig , txTo , inIdx , hashtype , flags = flags ):
623
+ if not EvalScript (stack , scriptSig , txTo , inIdx , flags = flags ):
613
624
return False
614
625
if SCRIPT_VERIFY_P2SH in flags :
615
626
stackCopy = list (stack )
616
- if not EvalScript (stack , scriptPubKey , txTo , inIdx , hashtype , flags = flags ):
627
+ if not EvalScript (stack , scriptPubKey , txTo , inIdx , flags = flags ):
617
628
return False
618
629
if len (stack ) == 0 :
619
630
return False
@@ -632,7 +643,7 @@ def VerifyScript(scriptSig, scriptPubKey, txTo, inIdx, hashtype, flags=()):
632
643
633
644
pubKey2 = CScript (stackCopy .pop ())
634
645
635
- if not EvalScript (stackCopy , pubKey2 , txTo , inIdx , hashtype , flags = flags ):
646
+ if not EvalScript (stackCopy , pubKey2 , txTo , inIdx , flags = flags ):
636
647
return False
637
648
638
649
if not len (stackCopy ):
@@ -642,7 +653,12 @@ def VerifyScript(scriptSig, scriptPubKey, txTo, inIdx, hashtype, flags=()):
642
653
643
654
return True
644
655
645
- def VerifySignature (txFrom , txTo , inIdx , hashtype ):
656
+ def VerifySignature (txFrom , txTo , inIdx ):
657
+ """Verify a scriptSig signature
658
+
659
+ Verifies that the scriptSig in txTo.vin[inIdx] is a valid scriptSig for the
660
+ corresponding COutPoint in transaction txFrom.
661
+ """
646
662
if inIdx >= len (txTo .vin ):
647
663
return False
648
664
txin = txTo .vin [inIdx ]
@@ -656,8 +672,7 @@ def VerifySignature(txFrom, txTo, inIdx, hashtype):
656
672
if txin .prevout .hash != txFrom .sha256 :
657
673
return False
658
674
659
- if not VerifyScript (txin .scriptSig , txout .scriptPubKey , txTo , inIdx ,
660
- hashtype ):
675
+ if not VerifyScript (txin .scriptSig , txout .scriptPubKey , txTo , inIdx ):
661
676
return False
662
677
663
678
return True
0 commit comments