Coverage Report

Created: 2026-09-14 20:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/signet.cpp
Line
Count
Source
1
// Copyright (c) 2019-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <signet.h>
6
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>
18
19
#include <algorithm>
20
#include <cstddef>
21
#include <cstdint>
22
#include <exception>
23
#include <span>
24
#include <utility>
25
#include <vector>
26
27
static constexpr uint8_t SIGNET_HEADER[4] = {0xec, 0xc7, 0xda, 0xa2};
28
29
static constexpr script_verify_flags BLOCK_SCRIPT_VERIFY_FLAGS = SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_NULLDUMMY;
30
31
static bool FetchAndClearCommitmentSection(const std::span<const uint8_t> header, CScript& witness_commitment, std::vector<uint8_t>& result)
32
27
{
33
27
    CScript replacement;
34
27
    bool found_header = false;
35
27
    result.clear();
36
37
27
    opcodetype opcode;
38
27
    CScript::const_iterator pc = witness_commitment.begin();
39
27
    std::vector<uint8_t> pushdata;
40
102
    while (witness_commitment.GetOp(pc, opcode, pushdata)) {
41
75
        if (pushdata.size() > 0) {
42
48
            if (!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 data
44
19
                result.insert(result.end(), pushdata.begin() + header.size(), pushdata.end());
45
19
                pushdata.erase(pushdata.begin() + header.size(), pushdata.end());
46
19
                found_header = true;
47
19
            }
48
48
            replacement << pushdata;
49
48
        } else {
50
27
            replacement << opcode;
51
27
        }
52
75
    }
53
54
27
    if (found_header) witness_commitment = replacement;
55
27
    return found_header;
56
27
}
57
58
static uint256 ComputeModifiedMerkleRoot(const CMutableTransaction& cb, const CBlock& block)
59
23
{
60
23
    std::vector<uint256> leaves;
61
23
    leaves.reserve((block.vtx.size() + 1) & ~1ULL); // capacity rounded up to even
62
23
    leaves.push_back(cb.GetHash().ToUint256());
63
29
    for (size_t s = 1; s < block.vtx.size(); ++s) {
64
6
        leaves.push_back(block.vtx[s]->GetHash().ToUint256());
65
6
    }
66
23
    return ComputeMerkleRoot(std::move(leaves));
67
23
}
68
69
std::optional<SignetTxs> SignetTxs::Create(const CBlock& block, const CScript& challenge)
70
31
{
71
31
    CMutableTransaction tx_to_spend;
72
31
    tx_to_spend.version = 0;
73
31
    tx_to_spend.nLockTime = 0;
74
31
    tx_to_spend.vin.emplace_back(COutPoint(), CScript(OP_0), 0);
75
31
    tx_to_spend.vout.emplace_back(0, challenge);
76
77
31
    CMutableTransaction tx_spending;
78
31
    tx_spending.version = 0;
79
31
    tx_spending.nLockTime = 0;
80
31
    tx_spending.vin.emplace_back(COutPoint(), CScript(), 0);
81
31
    tx_spending.vout.emplace_back(0, CScript(OP_RETURN));
82
83
    // can't fill any other fields before extracting signet
84
    // responses from block coinbase tx
85
86
    // find and delete signet signature
87
31
    if (block.vtx.empty()) return std::nullopt; // no coinbase tx in block; invalid
88
29
    CMutableTransaction modified_cb(*block.vtx.at(0));
89
90
29
    const int cidx = GetWitnessCommitmentIndex(block);
91
29
    if (cidx == NO_WITNESS_COMMITMENT) {
92
2
        return std::nullopt; // require a witness commitment
93
2
    }
94
95
27
    CScript& witness_commitment = modified_cb.vout.at(cidx).scriptPubKey;
96
97
27
    std::vector<uint8_t> signet_solution;
98
27
    if (!FetchAndClearCommitmentSection(SIGNET_HEADER, witness_commitment, signet_solution)) {
99
        // no signet solution -- allow this to support OP_TRUE as trivial block challenge
100
19
    } else {
101
19
        try {
102
19
            SpanReader v{signet_solution};
103
19
            v >> tx_spending.vin[0].scriptSig;
104
19
            v >> tx_spending.vin[0].scriptWitness.stack;
105
19
            if (!v.empty()) return std::nullopt; // extraneous data encountered
106
19
        } catch (const std::exception&) {
107
2
            return std::nullopt; // parsing error
108
2
        }
109
19
    }
110
23
    uint256 signet_merkle = ComputeModifiedMerkleRoot(modified_cb, block);
111
112
23
    std::vector<uint8_t> block_data;
113
23
    VectorWriter writer{block_data, 0};
114
23
    writer << block.nVersion;
115
23
    writer << block.hashPrevBlock;
116
23
    writer << signet_merkle;
117
23
    writer << block.nTime;
118
23
    tx_to_spend.vin[0].scriptSig << block_data;
119
23
    tx_spending.vin[0].prevout = COutPoint(tx_to_spend.GetHash(), 0);
120
121
23
    return SignetTxs{tx_to_spend, tx_spending};
122
27
}
123
124
// Signet block solution checker
125
bool CheckSignetBlockSolution(const CBlock& block, const Consensus::Params& consensusParams)
126
51
{
127
51
    if (block.GetHash() == consensusParams.hashGenesisBlock) {
128
        // genesis block solution is always valid
129
27
        return true;
130
27
    }
131
132
24
    const CScript challenge(consensusParams.signet_challenge.begin(), consensusParams.signet_challenge.end());
133
24
    const std::optional<SignetTxs> signet_txs = SignetTxs::Create(block, challenge);
134
135
24
    if (!signet_txs) {
136
4
        LogDebug(BCLog::VALIDATION, "CheckSignetBlockSolution: Errors in block (block solution parse failure)\n");
137
4
        return false;
138
4
    }
139
140
20
    const CScript& scriptSig = signet_txs->m_to_sign.vin[0].scriptSig;
141
20
    const CScriptWitness& witness = signet_txs->m_to_sign.vin[0].scriptWitness;
142
143
20
    PrecomputedTransactionData txdata;
144
20
    txdata.Init(signet_txs->m_to_sign, {signet_txs->m_to_spend.vout[0]});
145
20
    TransactionSignatureChecker sigcheck(&signet_txs->m_to_sign, /* nInIn= */ 0, /* amountIn= */ signet_txs->m_to_spend.vout[0].nValue, txdata, MissingDataBehavior::ASSERT_FAIL);
146
147
20
    if (!VerifyScript(scriptSig, signet_txs->m_to_spend.vout[0].scriptPubKey, &witness, BLOCK_SCRIPT_VERIFY_FLAGS, sigcheck)) {
148
1
        LogDebug(BCLog::VALIDATION, "CheckSignetBlockSolution: Errors in block (block solution invalid)\n");
149
1
        return false;
150
1
    }
151
19
    return true;
152
20
}