Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1251bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, script_verify_flags flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptError* serror)1252{1253ScriptExecutionData execdata;1254return EvalScript(stack, script, flags, checker, sigversion, execdata, serror);1255}1257namespace {1259/**1260* Wrapper that serializes like CTransaction, but with the modifications1261* required for the signature hash done in-place1262*/1263template <class T>1264class CTransactionSignatureSerializer1265{1266private:1267const T& txTo; //!< reference to the spending transaction (the one being serialized)1268const CScript& scriptCode; //!< output script being consumed1269const unsigned int nIn; //!< input index of txTo being signed1270const bool fAnyoneCanPay; //!< whether the hashtype has the SIGHASH_ANYONECANPAY flag set1271const bool fHashSingle; //!< whether the hashtype is SIGHASH_SINGLE1272const bool fHashNone; //!< whether the hashtype is SIGHASH_NONE1274public:1275CTransactionSignatureSerializer(const T& txToIn, const CScript& scriptCodeIn, unsigned int nInIn, int nHashTypeIn) :1276txTo(txToIn), scriptCode(scriptCodeIn), nIn(nInIn),1277fAnyoneCanPay(!!(nHashTypeIn & SIGHASH_ANYONECANPAY)),1278fHashSingle((nHashTypeIn & 0x1f) == SIGHASH_SINGLE),1279fHashNone((nHashTypeIn & 0x1f) == SIGHASH_NONE) {}1281/** Serialize the passed scriptCode, skipping OP_CODESEPARATORs */1282template<typename S>1283void SerializeScriptCode(S &s) const {1284CScript::const_iterator it = scriptCode.begin();1285CScript::const_iterator itBegin = it;1286opcodetype opcode;1287unsigned int nCodeSeparators = 0;1288while (scriptCode.GetOp(it, opcode)) {1289if (opcode == OP_CODESEPARATOR)1290nCodeSeparators++;1291}1292::WriteCompactSize(s, scriptCode.size() - nCodeSeparators);1293it = itBegin;1294while (scriptCode.GetOp(it, opcode)) {1295if (opcode == OP_CODESEPARATOR) {1296s.write(std::as_bytes(std::span{&itBegin[0], size_t(it - itBegin - 1)}));1297itBegin = it;1298}1299}1300if (itBegin != scriptCode.end())1301s.write(std::as_bytes(std::span{&itBegin[0], size_t(it - itBegin)}));1302}1304/** Serialize an input of txTo */1305template<typename S>1306void SerializeInput(S &s, unsigned int nInput) const {1307// In case of SIGHASH_ANYONECANPAY, only the input being signed is serialized1308if (fAnyoneCanPay)1309nInput = nIn;1310// Serialize the prevout1311::Serialize(s, txTo.vin[nInput].prevout);1312// Serialize the script1313if (nInput != nIn)1314// Blank out other inputs' signatures1315::Serialize(s, CScript());1316else1317SerializeScriptCode(s);1318// Serialize the nSequence1319if (nInput != nIn && (fHashSingle || fHashNone))1320// let the others update at will1321::Serialize(s, int32_t{0});1322else1323::Serialize(s, txTo.vin[nInput].nSequence);1324}1326/** Serialize an output of txTo */1327template<typename S>1328void SerializeOutput(S &s, unsigned int nOutput) const {1329if (fHashSingle && nOutput != nIn)1330// Do not lock-in the txout payee at other indices as txin1331::Serialize(s, CTxOut());1332else1333::Serialize(s, txTo.vout[nOutput]);1334}1336/** Serialize txTo */1337template<typename S>1338void Serialize(S &s) const {1339// Serialize version1340::Serialize(s, txTo.version);1341// Serialize vin1342unsigned int nInputs = fAnyoneCanPay ? 1 : txTo.vin.size();1343::WriteCompactSize(s, nInputs);1344for (unsigned int nInput = 0; nInput < nInputs; nInput++)1345SerializeInput(s, nInput);1346// Serialize vout1347unsigned int nOutputs = fHashNone ? 0 : (fHashSingle ? nIn+1 : txTo.vout.size());1348::WriteCompactSize(s, nOutputs);1349for (unsigned int nOutput = 0; nOutput < nOutputs; nOutput++)1350SerializeOutput(s, nOutput);1351// Serialize nLockTime1352::Serialize(s, txTo.nLockTime);1353}1354};1356/** Compute the (single) SHA256 of the concatenation of all prevouts of a tx. */1357template <class T>1358uint256 GetPrevoutsSHA256(const T& txTo)1359{1360HashWriter ss{};1361for (const auto& txin : txTo.vin) {1362ss << txin.prevout;1363}1364return ss.GetSHA256();1365}1367/** Compute the (single) SHA256 of the concatenation of all nSequences of a tx. */1368template <class T>1369uint256 GetSequencesSHA256(const T& txTo)1370{1371HashWriter ss{};1372for (const auto& txin : txTo.vin) {1373ss << txin.nSequence;1374}1375return ss.GetSHA256();1376}1378/** Compute the (single) SHA256 of the concatenation of all txouts of a tx. */1379template <class T>1380uint256 GetOutputsSHA256(const T& txTo)1381{1382HashWriter ss{};1383for (const auto& txout : txTo.vout) {1384ss << txout;1385}1386return ss.GetSHA256();1387}1389/** Compute the (single) SHA256 of the concatenation of all amounts spent by a tx. */1390uint256 GetSpentAmountsSHA256(const std::vector<CTxOut>& outputs_spent)1391{1392HashWriter ss{};1393for (const auto& txout : outputs_spent) {1394ss << txout.nValue;1395}1396return ss.GetSHA256();1397}1399/** Compute the (single) SHA256 of the concatenation of all scriptPubKeys spent by a tx. */1400uint256 GetSpentScriptsSHA256(const std::vector<CTxOut>& outputs_spent)1401{1402HashWriter ss{};1403for (const auto& txout : outputs_spent) {1404ss << txout.scriptPubKey;1405}1406return ss.GetSHA256();1407}1410} // namespace1412template <class T>1413void PrecomputedTransactionData::Init(const T& txTo, std::vector<CTxOut>&& spent_outputs, bool force)1414{1415assert(!m_spent_outputs_ready);1417m_spent_outputs = std::move(spent_outputs);1418if (!m_spent_outputs.empty()) {1419assert(m_spent_outputs.size() == txTo.vin.size());1420m_spent_outputs_ready = true;1421}1423// Determine which precomputation-impacting features this transaction uses.1424bool uses_bip143_segwit = force;1425bool uses_bip341_taproot = force;1426for (size_t inpos = 0; inpos < txTo.vin.size() && !(uses_bip143_segwit && uses_bip341_taproot); ++inpos) {1427if (!txTo.vin[inpos].scriptWitness.IsNull()) {1428if (m_spent_outputs_ready && m_spent_outputs[inpos].scriptPubKey.size() == 2 + WITNESS_V1_TAPROOT_SIZE &&1429m_spent_outputs[inpos].scriptPubKey[0] == OP_1) {1430// Treat every witness-bearing spend with 34-byte scriptPubKey that starts with OP_1 as a Taproot1431// spend. This only works if spent_outputs was provided as well, but if it wasn't, actual validation1432// will fail anyway. Note that this branch may trigger for scriptPubKeys that aren't actually segwit1433// but in that case validation will fail as SCRIPT_ERR_WITNESS_UNEXPECTED anyway.1434uses_bip341_taproot = true;1435} else {1436// Treat every spend that's not known to native witness v1 as a Witness v0 spend. This branch may1437// also be taken for unknown witness versions, but it is harmless, and being precise would require1438// P2SH evaluation to find the redeemScript.1439uses_bip143_segwit = true;1440}1441}1442if (uses_bip341_taproot && uses_bip143_segwit) break; // No need to scan further if we already need all.1443}1445if (uses_bip143_segwit || uses_bip341_taproot) {1446// Computations shared between both sighash schemes.1447m_prevouts_single_hash = GetPrevoutsSHA256(txTo);1448m_sequences_single_hash = GetSequencesSHA256(txTo);1449m_outputs_single_hash = GetOutputsSHA256(txTo);1450}1451if (uses_bip143_segwit) {1452hashPrevouts = SHA256Uint256(m_prevouts_single_hash);1453hashSequence = SHA256Uint256(m_sequences_single_hash);1454hashOutputs = SHA256Uint256(m_outputs_single_hash);1455m_bip143_segwit_ready = true;1456}1457if (uses_bip341_taproot && m_spent_outputs_ready) {1458m_spent_amounts_single_hash = GetSpentAmountsSHA256(m_spent_outputs);1459m_spent_scripts_single_hash = GetSpentScriptsSHA256(m_spent_outputs);1460m_bip341_taproot_ready = true;1461}1462}1464template <class T>1465PrecomputedTransactionData::PrecomputedTransactionData(const T& txTo)1466{1467Init(txTo, {});1468}1470// explicit instantiation1471template void PrecomputedTransactionData::Init(const CTransaction& txTo, std::vector<CTxOut>&& spent_outputs, bool force);1472template void PrecomputedTransactionData::Init(const CMutableTransaction& txTo, std::vector<CTxOut>&& spent_outputs, bool force);1473template PrecomputedTransactionData::PrecomputedTransactionData(const CTransaction& txTo);1474template PrecomputedTransactionData::PrecomputedTransactionData(const CMutableTransaction& txTo);1476const HashWriter HASHER_TAPSIGHASH{TaggedHash("TapSighash")};1477const HashWriter HASHER_TAPLEAF{TaggedHash("TapLeaf")};1478const HashWriter HASHER_TAPBRANCH{TaggedHash("TapBranch")};1480static bool HandleMissingData(MissingDataBehavior mdb)1481{1482switch (mdb) {1483case MissingDataBehavior::ASSERT_FAIL:1484assert(!"Missing data");1485break;1486case MissingDataBehavior::FAIL:1487return false;1488}1489assert(!"Unknown MissingDataBehavior value");1490}1492template<typename T>1493bool SignatureHashSchnorr(uint256& hash_out, ScriptExecutionData& execdata, const T& tx_to, uint32_t in_pos, uint8_t hash_type, SigVersion sigversion, const PrecomputedTransactionData& cache, MissingDataBehavior mdb)1494{1495uint8_t ext_flag, key_version;1496switch (sigversion) {1497case SigVersion::TAPROOT:1498ext_flag = 0;1499// key_version is not used and left uninitialized.1500break;