| 1452 | } |
| 1453 | |
| 1454 | bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CScriptWitness* witness, unsigned int flags, const BaseSignatureChecker& checker, ScriptError* serror) |
| 1455 | { |
| 1456 | static const CScriptWitness emptyWitness; |
| 1457 | if (witness == nullptr) { |
| 1458 | witness = &emptyWitness; |
| 1459 | } |
| 1460 | bool hadWitness = false; |
| 1461 | |
| 1462 | set_error(serror, SCRIPT_ERR_UNKNOWN_ERROR); |
| 1463 | |
| 1464 | if ((flags & SCRIPT_VERIFY_SIGPUSHONLY) != 0 && !scriptSig.IsPushOnly()) { |
| 1465 | return set_error(serror, SCRIPT_ERR_SIG_PUSHONLY); |
| 1466 | } |
| 1467 | |
| 1468 | vector<vector<unsigned char> > stack, stackCopy; |
| 1469 | if (!EvalScript(stack, scriptSig, flags, checker, SIGVERSION_BASE, serror)) |
| 1470 | // serror is set |
| 1471 | return false; |
| 1472 | if (flags & SCRIPT_VERIFY_P2SH) |
| 1473 | stackCopy = stack; |
| 1474 | if (!EvalScript(stack, scriptPubKey, flags, checker, SIGVERSION_BASE, serror)) |
| 1475 | // serror is set |
| 1476 | return false; |
| 1477 | if (stack.empty()) { |
| 1478 | return set_error(serror, SCRIPT_ERR_EVAL_FALSE); |
| 1479 | } |
| 1480 | |
| 1481 | if (CastToBool(stack.back()) == false) { |
| 1482 | return set_error(serror, SCRIPT_ERR_EVAL_FALSE); |
| 1483 | } |
| 1484 | |
| 1485 | // Bare witness programs |
| 1486 | int witnessversion; |
| 1487 | std::vector<unsigned char> witnessprogram; |
| 1488 | if (flags & SCRIPT_VERIFY_WITNESS) { |
| 1489 | if (scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) { |
| 1490 | hadWitness = true; |
| 1491 | if (scriptSig.size() != 0) { |
| 1492 | // The scriptSig must be _exactly_ CScript(), otherwise we reintroduce malleability. |
| 1493 | return set_error(serror, SCRIPT_ERR_WITNESS_MALLEATED); |
| 1494 | } |
| 1495 | if (!VerifyWitnessProgram(*witness, witnessversion, witnessprogram, flags, checker, serror)) { |
| 1496 | return false; |
| 1497 | } |
| 1498 | // Bypass the cleanstack check at the end. The actual stack is obviously not clean |
| 1499 | // for witness programs. |
| 1500 | stack.resize(1); |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | // Additional validation for spend-to-script-hash transactions: |
| 1505 | if ((flags & SCRIPT_VERIFY_P2SH) && scriptPubKey.IsPayToScriptHash()) |
| 1506 | { |
| 1507 | // scriptSig must be literals-only or validation fails |
| 1508 | if (!scriptSig.IsPushOnly()) |
| 1509 | return set_error(serror, SCRIPT_ERR_SIG_PUSHONLY); |
| 1510 | |
| 1511 | // Restore stack. |