MCPcopy
hub / github.com/petertodd/python-bitcoinlib / VerifyScript

Function VerifyScript

bitcoin/core/scripteval.py:737–787  ·  view source on GitHub ↗

Verify a scriptSig satisfies a scriptPubKey scriptSig - Signature scriptPubKey - PubKey txTo - Spending transaction inIdx - Index of the transaction input containing scriptSig Raises a ValidationError subclass if the validation fails.

(scriptSig, scriptPubKey, txTo, inIdx, flags=())

Source from the content-addressed store, hash-verified

735 pass
736
737def VerifyScript(scriptSig, scriptPubKey, txTo, inIdx, flags=()):
738 """Verify a scriptSig satisfies a scriptPubKey
739
740 scriptSig - Signature
741
742 scriptPubKey - PubKey
743
744 txTo - Spending transaction
745
746 inIdx - Index of the transaction input containing scriptSig
747
748 Raises a ValidationError subclass if the validation fails.
749 """
750 stack = []
751 EvalScript(stack, scriptSig, txTo, inIdx, flags=flags)
752 if SCRIPT_VERIFY_P2SH in flags:
753 stackCopy = list(stack)
754 EvalScript(stack, scriptPubKey, txTo, inIdx, flags=flags)
755 if len(stack) == 0:
756 raise VerifyScriptError("scriptPubKey left an empty stack")
757 if not _CastToBool(stack[-1]):
758 raise VerifyScriptError("scriptPubKey returned false")
759
760 # Additional validation for spend-to-script-hash transactions
761 if SCRIPT_VERIFY_P2SH in flags and scriptPubKey.is_p2sh():
762 if not scriptSig.is_push_only():
763 raise VerifyScriptError("P2SH scriptSig not is_push_only()")
764
765 # restore stack
766 stack = stackCopy
767
768 # stack cannot be empty here, because if it was the
769 # P2SH HASH <> EQUAL scriptPubKey would be evaluated with
770 # an empty stack and the EvalScript above would return false.
771 assert len(stack)
772
773 pubKey2 = CScript(stack.pop())
774
775 EvalScript(stack, pubKey2, txTo, inIdx, flags=flags)
776
777 if not len(stack):
778 raise VerifyScriptError("P2SH inner scriptPubKey left an empty stack")
779
780 if not _CastToBool(stack[-1]):
781 raise VerifyScriptError("P2SH inner scriptPubKey returned false")
782
783 if SCRIPT_VERIFY_CLEANSTACK in flags:
784 assert SCRIPT_VERIFY_P2SH in flags
785
786 if len(stack) != 1:
787 raise VerifyScriptError("scriptPubKey left extra items on stack")
788
789
790class VerifySignatureError(bitcoin.core.ValidationError):

Callers 7

test_tx_validMethod · 0.90
test_tx_invalidMethod · 0.90
VerifySignatureFunction · 0.85
test_script_validMethod · 0.85
test_script_invalidMethod · 0.85

Calls 6

EvalScriptFunction · 0.85
VerifyScriptErrorClass · 0.85
_CastToBoolFunction · 0.85
CScriptClass · 0.85
is_p2shMethod · 0.80
is_push_onlyMethod · 0.80

Tested by 4

test_tx_validMethod · 0.72
test_tx_invalidMethod · 0.72
test_script_validMethod · 0.68
test_script_invalidMethod · 0.68