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.
1// Copyright (c) 2019-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <signet.h>7#include <consensus/merkle.h>8#include <consensus/params.h>9#include <consensus/validation.h>10#include <primitives/block.h>11#include <primitives/transaction.h>12#include <script/interpreter.h>13#include <script/script.h>14#include <script/verify_flags.h>15#include <streams.h>16#include <uint256.h>17#include <util/log.h>19#include <algorithm>20#include <cstddef>21#include <cstdint>22#include <exception>23#include <span>24#include <utility>25#include <vector>27static constexpr uint8_t SIGNET_HEADER[4] = {0xec, 0xc7, 0xda, 0xa2};29static constexpr script_verify_flags BLOCK_SCRIPT_VERIFY_FLAGS = SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_NULLDUMMY;31static bool FetchAndClearCommitmentSection(const std::span<const uint8_t> header, CScript& witness_commitment, std::vector<uint8_t>& result)32{33CScript replacement;34bool found_header = false;35result.clear();37opcodetype opcode;38CScript::const_iterator pc = witness_commitment.begin();39std::vector<uint8_t> pushdata;40while (witness_commitment.GetOp(pc, opcode, pushdata)) {41if (pushdata.size() > 0) {42if (!found_header && pushdata.size() > header.size() && std::ranges::equal(std::span{pushdata}.first(header.size()), header)) {43// pushdata only counts if it has the header _and_ some data44result.insert(result.end(), pushdata.begin() + header.size(), pushdata.end());45pushdata.erase(pushdata.begin() + header.size(), pushdata.end());46found_header = true;47}48replacement << pushdata;49} else {50replacement << opcode;51}52}54if (found_header) witness_commitment = replacement;55return found_header;56}58static uint256 ComputeModifiedMerkleRoot(const CMutableTransaction& cb, const CBlock& block)59{60std::vector<uint256> leaves;61leaves.reserve((block.vtx.size() + 1) & ~1ULL); // capacity rounded up to even62leaves.push_back(cb.GetHash().ToUint256());63for (size_t s = 1; s < block.vtx.size(); ++s) {64leaves.push_back(block.vtx[s]->GetHash().ToUint256());65}66return ComputeMerkleRoot(std::move(leaves));67}69std::optional<SignetTxs> SignetTxs::Create(const CBlock& block, const CScript& challenge)70{71CMutableTransaction tx_to_spend;72tx_to_spend.version = 0;73tx_to_spend.nLockTime = 0;74tx_to_spend.vin.emplace_back(COutPoint(), CScript(OP_0), 0);75tx_to_spend.vout.emplace_back(0, challenge);77CMutableTransaction tx_spending;78tx_spending.version = 0;79tx_spending.nLockTime = 0;80tx_spending.vin.emplace_back(COutPoint(), CScript(), 0);81tx_spending.vout.emplace_back(0, CScript(OP_RETURN));83// can't fill any other fields before extracting signet84// responses from block coinbase tx86// find and delete signet signature87if (block.vtx.empty()) return std::nullopt; // no coinbase tx in block; invalid88CMutableTransaction modified_cb(*block.vtx.at(0));90const int cidx = GetWitnessCommitmentIndex(block);91if (cidx == NO_WITNESS_COMMITMENT) {92return std::nullopt; // require a witness commitment93}95CScript& witness_commitment = modified_cb.vout.at(cidx).scriptPubKey;97std::vector<uint8_t> signet_solution;98if (!FetchAndClearCommitmentSection(SIGNET_HEADER, witness_commitment, signet_solution)) {99// no signet solution -- allow this to support OP_TRUE as trivial block challenge100} else {101try {102SpanReader v{signet_solution};103v >> tx_spending.vin[0].scriptSig;104v >> tx_spending.vin[0].scriptWitness.stack;105if (!v.empty()) return std::nullopt; // extraneous data encountered106} catch (const std::exception&) {107return std::nullopt; // parsing error108}109}110uint256 signet_merkle = ComputeModifiedMerkleRoot(modified_cb, block);112std::vector<uint8_t> block_data;113VectorWriter writer{block_data, 0};114writer << block.nVersion;115writer << block.hashPrevBlock;116writer << signet_merkle;117writer << block.nTime;118tx_to_spend.vin[0].scriptSig << block_data;119tx_spending.vin[0].prevout = COutPoint(tx_to_spend.GetHash(), 0);121return SignetTxs{tx_to_spend, tx_spending};122}124// Signet block solution checker125bool CheckSignetBlockSolution(const CBlock& block, const Consensus::Params& consensusParams)126{127if (block.GetHash() == consensusParams.hashGenesisBlock) {128// genesis block solution is always valid129return true;130}132const CScript challenge(consensusParams.signet_challenge.begin(), consensusParams.signet_challenge.end());133const std::optional<SignetTxs> signet_txs = SignetTxs::Create(block, challenge);135if (!signet_txs) {136LogDebug(BCLog::VALIDATION, "CheckSignetBlockSolution: Errors in block (block solution parse failure)\n");137return false;138}140const CScript& scriptSig = signet_txs->m_to_sign.vin[0].scriptSig;141const CScriptWitness& witness = signet_txs->m_to_sign.vin[0].scriptWitness;143PrecomputedTransactionData txdata;144txdata.Init(signet_txs->m_to_sign, {signet_txs->m_to_spend.vout[0]});145TransactionSignatureChecker sigcheck(&signet_txs->m_to_sign, /* nInIn= */ 0, /* amountIn= */ signet_txs->m_to_spend.vout[0].nValue, txdata, MissingDataBehavior::ASSERT_FAIL);147if (!VerifyScript(scriptSig, signet_txs->m_to_spend.vout[0].scriptPubKey, &witness, BLOCK_SCRIPT_VERIFY_FLAGS, sigcheck)) {148LogDebug(BCLog::VALIDATION, "CheckSignetBlockSolution: Errors in block (block solution invalid)\n");149return false;150}151return true;152}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.