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) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#ifndef BITCOIN_PRIMITIVES_TRANSACTION_H7#define BITCOIN_PRIMITIVES_TRANSACTION_H9#include <attributes.h>10#include <consensus/amount.h>11#include <primitives/transaction_identifier.h> // IWYU pragma: export12#include <script/script.h>13#include <serialize.h>15#include <algorithm>16#include <compare>17#include <cstddef>18#include <cstdint>19#include <ios>20#include <limits>21#include <memory>22#include <numeric>23#include <string>24#include <tuple>25#include <utility>26#include <vector>28/** An outpoint - a combination of a transaction hash and an index n into its vout */29class COutPoint30{31public:32Txid hash;33uint32_t n;35static constexpr uint32_t NULL_INDEX = std::numeric_limits<uint32_t>::max();37COutPoint(): n(NULL_INDEX) { }38COutPoint(const Txid& hashIn, uint32_t nIn): hash(hashIn), n(nIn) { }40SERIALIZE_METHODS(COutPoint, obj) { READWRITE(obj.hash, obj.n); }42void SetNull() { hash.SetNull(); n = NULL_INDEX; }43bool IsNull() const { return (hash.IsNull() && n == NULL_INDEX); }45friend bool operator<(const COutPoint& a, const COutPoint& b)46{47return std::tie(a.hash, a.n) < std::tie(b.hash, b.n);48}50friend bool operator==(const COutPoint& a, const COutPoint& b)51{52return (a.hash == b.hash && a.n == b.n);53}55std::string ToString() const;56};58/** An input of a transaction. It contains the location of the previous59* transaction's output that it claims and a signature that matches the60* output's public key.61*/62class CTxIn63{64public:65COutPoint prevout;66CScript scriptSig;67uint32_t nSequence;68CScriptWitness scriptWitness; //!< Only serialized through CTransaction70/**71* Setting nSequence to this value for every input in a transaction72* disables nLockTime/IsFinalTx().73* It fails OP_CHECKLOCKTIMEVERIFY/CheckLockTime() for any input that has74* it set (BIP 65).75* It has SEQUENCE_LOCKTIME_DISABLE_FLAG set (BIP 68/112).76*/77static constexpr uint32_t SEQUENCE_FINAL{0xffffffff};78/**79* This is the maximum sequence number that enables both nLockTime and80* OP_CHECKLOCKTIMEVERIFY (BIP 65).81* It has SEQUENCE_LOCKTIME_DISABLE_FLAG set (BIP 68/112).82*/83static constexpr uint32_t MAX_SEQUENCE_NONFINAL{SEQUENCE_FINAL - 1};85// Below flags apply in the context of BIP 68. BIP 68 requires the tx86// version to be set to 2, or higher.87/**88* If this flag is set, CTxIn::nSequence is NOT interpreted as a89* relative lock-time.90* It skips SequenceLocks() for any input that has it set (BIP 68).91* It fails OP_CHECKSEQUENCEVERIFY/CheckSequence() for any input that has92* it set (BIP 112).93*/94static constexpr uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG{1U << 31};96/**97* If CTxIn::nSequence encodes a relative lock-time and this flag98* is set, the relative lock-time has units of 512 seconds,99* otherwise it specifies blocks with a granularity of 1. */100static constexpr uint32_t SEQUENCE_LOCKTIME_TYPE_FLAG{1 << 22};102/**103* If CTxIn::nSequence encodes a relative lock-time, this mask is104* applied to extract that lock-time from the sequence field. */105static constexpr uint32_t SEQUENCE_LOCKTIME_MASK{0x0000ffff};107/**108* In order to use the same number of bits to encode roughly the109* same wall-clock duration, and because blocks are naturally110* limited to occur every 600s on average, the minimum granularity111* for time-based relative lock-time is fixed at 512 seconds.112* Converting from CTxIn::nSequence to seconds is performed by113* multiplying by 512 = 2^9, or equivalently shifting up by114* 9 bits. */115static constexpr int SEQUENCE_LOCKTIME_GRANULARITY{9};117CTxIn()118{119nSequence = SEQUENCE_FINAL;120}122explicit CTxIn(COutPoint prevoutIn, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);123CTxIn(Txid hashPrevTx, uint32_t nOut, CScript scriptSigIn=CScript(), uint32_t nSequenceIn=SEQUENCE_FINAL);125SERIALIZE_METHODS(CTxIn, obj) { READWRITE(obj.prevout, obj.scriptSig, obj.nSequence); }127friend bool operator==(const CTxIn& a, const CTxIn& b)128{129return (a.prevout == b.prevout &&130a.scriptSig == b.scriptSig &&131a.nSequence == b.nSequence);132}134std::string ToString() const;135};137/** An output of a transaction. It contains the public key that the next input138* must be able to sign with to claim it.139*/140class CTxOut141{142public:143CAmount nValue;144CScript scriptPubKey;146CTxOut()147{148SetNull();149}151CTxOut(const CAmount& nValueIn, CScript scriptPubKeyIn);153SERIALIZE_METHODS(CTxOut, obj) { READWRITE(obj.nValue, obj.scriptPubKey); }155void SetNull()156{157nValue = -1;158scriptPubKey.clear();159}161bool IsNull() const162{163return (nValue == -1);164}166friend bool operator==(const CTxOut& a, const CTxOut& b)167{168return (a.nValue == b.nValue &&169a.scriptPubKey == b.scriptPubKey);170}172std::string ToString() const;173};175struct CMutableTransaction;177struct TransactionSerParams {178const bool allow_witness;179SER_PARAMS_OPFUNC180};181inline constexpr TransactionSerParams TX_WITH_WITNESS{.allow_witness = true};182inline constexpr TransactionSerParams TX_NO_WITNESS{.allow_witness = false};184/**185* Basic transaction serialization format:186* - uint32_t version187* - std::vector<CTxIn> vin188* - std::vector<CTxOut> vout189* - uint32_t nLockTime190*191* Extended transaction serialization format:192* - uint32_t version193* - unsigned char dummy = 0x00194* - unsigned char flags (!= 0)195* - std::vector<CTxIn> vin196* - std::vector<CTxOut> vout197* - if (flags & 1):198* - CScriptWitness scriptWitness; (deserialized into CTxIn)199* - uint32_t nLockTime200*/201template<typename Stream, typename TxType>202void UnserializeTransaction(TxType& tx, Stream& s, const TransactionSerParams& params)203{204const bool fAllowWitness = params.allow_witness;206s >> tx.version;207unsigned char flags = 0;208tx.vin.clear();209tx.vout.clear();210/* Try to read the vin. In case the dummy is there, this will be read as an empty vector. */211s >> tx.vin;212if (tx.vin.size() == 0 && fAllowWitness) {213/* We read a dummy or an empty vin. */214s >> flags;215if (flags != 0) {216s >> tx.vin;217s >> tx.vout;218}219} else {220/* We read a non-empty vin. Assume a normal vout follows. */221s >> tx.vout;222}223if ((flags & 1) && fAllowWitness) {224/* The witness flag is present, and we support witnesses. */225flags ^= 1;226for (size_t i = 0; i < tx.vin.size(); i++) {227s >> tx.vin[i].scriptWitness.stack;228}229if (!tx.HasWitness()) {230/* It's illegal to encode witnesses when all witness stacks are empty. */231throw std::ios_base::failure("Superfluous witness record");232}233}234if (flags) {235/* Unknown flag in the serialization */236throw std::ios_base::failure("Unknown transaction optional data");237}238s >> tx.nLockTime;239}241template<typename Stream, typename TxType>242void SerializeTransaction(const TxType& tx, Stream& s, const TransactionSerParams& params)243{244const bool fAllowWitness = params.allow_witness;246s << tx.version;247unsigned char flags = 0;248// Consistency check249if (fAllowWitness) {250/* Check whether witnesses need to be serialized. */