MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / VerifyScript

Function VerifyScript

src/script/interpreter.cpp:2012–2131  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2010}
2011
2012bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CScriptWitness* witness, script_verify_flags flags, const BaseSignatureChecker& checker, ScriptError* serror)
2013{
2014 static const CScriptWitness emptyWitness;
2015 if (witness == nullptr) {
2016 witness = &emptyWitness;
2017 }
2018 bool hadWitness = false;
2019
2020 set_error(serror, SCRIPT_ERR_UNKNOWN_ERROR);
2021
2022 if ((flags & SCRIPT_VERIFY_SIGPUSHONLY) != 0 && !scriptSig.IsPushOnly()) {
2023 return set_error(serror, SCRIPT_ERR_SIG_PUSHONLY);
2024 }
2025
2026 // scriptSig and scriptPubKey must be evaluated sequentially on the same stack
2027 // rather than being simply concatenated (see CVE-2010-5141)
2028 std::vector<std::vector<unsigned char> > stack, stackCopy;
2029 if (!EvalScript(stack, scriptSig, flags, checker, SigVersion::BASE, serror))
2030 // serror is set
2031 return false;
2032 if (flags & SCRIPT_VERIFY_P2SH)
2033 stackCopy = stack;
2034 if (!EvalScript(stack, scriptPubKey, flags, checker, SigVersion::BASE, serror))
2035 // serror is set
2036 return false;
2037 if (stack.empty())
2038 return set_error(serror, SCRIPT_ERR_EVAL_FALSE);
2039 if (CastToBool(stack.back()) == false)
2040 return set_error(serror, SCRIPT_ERR_EVAL_FALSE);
2041
2042 // Bare witness programs
2043 int witnessversion;
2044 std::vector<unsigned char> witnessprogram;
2045 if (flags & SCRIPT_VERIFY_WITNESS) {
2046 if (scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) {
2047 hadWitness = true;
2048 if (scriptSig.size() != 0) {
2049 // The scriptSig must be _exactly_ CScript(), otherwise we reintroduce malleability.
2050 return set_error(serror, SCRIPT_ERR_WITNESS_MALLEATED);
2051 }
2052 if (!VerifyWitnessProgram(*witness, witnessversion, witnessprogram, flags, checker, serror, /*is_p2sh=*/false)) {
2053 return false;
2054 }
2055 // Bypass the cleanstack check at the end. The actual stack is obviously not clean
2056 // for witness programs.
2057 stack.resize(1);
2058 }
2059 }
2060
2061 // Additional validation for spend-to-script-hash transactions:
2062 if ((flags & SCRIPT_VERIFY_P2SH) && scriptPubKey.IsPayToScriptHash())
2063 {
2064 // scriptSig must be literals-only or validation fails
2065 if (!scriptSig.IsPushOnly())
2066 return set_error(serror, SCRIPT_ERR_SIG_PUSHONLY);
2067
2068 // Restore stack.
2069 swap(stack, stackCopy);

Callers 15

CheckSignetBlockSolutionFunction · 0.85
operator()Method · 0.85
ProduceSignatureFunction · 0.85
DataFromTransactionFunction · 0.85
SignTransactionFunction · 0.85
AssetTestFunction · 0.85
VerifyWithFlagFunction · 0.85
VerifyFunction · 0.85
CheckTxScriptsFunction · 0.85

Calls 15

set_errorFunction · 0.85
EvalScriptFunction · 0.85
CastToBoolFunction · 0.85
VerifyWitnessProgramFunction · 0.85
popstackFunction · 0.85
set_successFunction · 0.85
IsPushOnlyMethod · 0.80
IsPayToScriptHashMethod · 0.80
CScriptClass · 0.70
emptyMethod · 0.45
backMethod · 0.45
IsWitnessProgramMethod · 0.45

Tested by 14

AssetTestFunction · 0.68
VerifyWithFlagFunction · 0.68
VerifyFunction · 0.68
CheckTxScriptsFunction · 0.68
CheckWithFlagFunction · 0.68
DoTestMethod · 0.68
BOOST_AUTO_TEST_CASEFunction · 0.68
BOOST_AUTO_TEST_CASEFunction · 0.68
TestSatisfyMethod · 0.68
FUZZ_TARGETFunction · 0.68
TestNodeFunction · 0.68