Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Transactions

A Bitcoin transaction consumes previously created outputs and creates new outputs. Consensus validation distinguishes the serialized transaction bytes, the identifiers derived from those bytes, and the contextual checks that depend on the UTXO set and block height.

Identifier and amount primitives

TypeEncodingMeaning
Txiduint256$txid(tx)$, as defined in Section Notation.
Wtxiduint256$wtxid(tx)$, as defined in Section Notation.
OutPoint`Txid
Amountint64Satoshis. Consensus-valid output amounts satisfy $0 <= value <=$ MAX_MONEY.

Transaction records

FieldTypeEncodingMeaning
valueAmountint64Output value in satoshis.
scriptPubKeyScriptvarbytesLocking script.
FieldTypeEncodingMeaning
previousOutputOutPoint36 bytesOutput spent by this input, or the null outpoint for coinbase.
scriptSigScriptvarbytesUnlocking script or coinbase data.
sequenceuint324 little-endian bytesFinality, replacement, and relative-lock field.
FieldTypeEncodingMeaning
versionint324 little-endian bytesTransaction version. Versions 1 and 2 are standard by default relay policy; consensus accepts the integer subject to contextual rules.
inputsvector<TxIn>CompactSize count, then inputsSpent outputs or coinbase input.
outputsvector<TxOut>CompactSize count, then outputsCreated outputs.
witnessesvector<Witness>witness serialization onlyOne witness stack per input when witness serialization is used.
lockTimeuint324 little-endian bytesAbsolute height or time lock.

Witness is vector<varbytes>. It is present only in witness serialization and has exactly one stack per input.

Legacy serialization

$E_0(tx)$, the legacy transaction serialization, is:

$$ version || inputs || outputs || lockTime. $$

PartEncodingMeaning
versionint32Transaction version.
inputCountcompactSizeNumber of inputs.
inputsTxIn[inputCount]Serialized inputs in order.
outputCountcompactSizeNumber of outputs.
outputsTxOut[outputCount]Serialized outputs in order.
lockTimeuint32Absolute lock field.

Witness serialization

$E_w(tx)$, the witness transaction serialization, is:

$$ version || marker || flag || inputs || outputs || witnesses || lockTime. $$

PartEncodingMeaning
marker0x00Distinguishes witness serialization from legacy serialization.
flag0x01Indicates witness data follows. Other nonzero flag bits are invalid.
witnessesWitness[inputCount]One witness stack per input.

The witness form is valid only when at least one witness stack is nonempty. A transaction with no witness data uses legacy serialization for both Txid and Wtxid (sources: bip-0141,bip-0144).

Transaction identifiers

IdentifierDefinition
txid(tx)Defined in Section Notation.
wtxid(tx)Defined in Section Notation.
GenTxidPair of identifier kind and hash used by relay to distinguish txid and wtxid announcements.

Context-free validity

A transaction is context-free valid only if:

  1. it has at least one input and at least one output;

  2. its stripped size satisfies $strippedSize(tx) * WITNESS_SCALE_FACTOR <= MAX_BLOCK_WEIGHT$;

  3. every output value is in range;

  4. the sum of output values is in range;

  5. no two inputs reference the same previous output;

  6. if it is coinbase, it has exactly one input whose previous output is the null outpoint and whose scriptSig length is between COINBASE_SCRIPTSIG_MIN and COINBASE_SCRIPTSIG_MAX bytes; and

  7. if it is not coinbase, no input uses the null outpoint.

Context-free validity does not prove the inputs exist or signatures are valid; those checks require the UTXO set and rule context (sources: bitcoin-core-v31).

Coinbase transaction

A coinbase transaction has exactly one input whose previous output is the null outpoint defined by NULL_TXID and NULL_INDEX. Its input does not spend a UTXO. Its outputs create the block subsidy and collect fees, subject to the coinbase amount rule during block connection.

Coinbase-created outputs require COINBASE_MATURITY confirmations before they may be spent.

Value conservation

For a non-coinbase transaction:

$$ fee(tx) = \sum value(spentInputs(tx)) - \sum value(outputs(tx)). $$

The transaction is invalid if any referenced input is absent, any spent coin is immature coinbase, any output is outside the money range, the output sum is outside the money range, or fee(tx) is negative (sources: bitcoin-core-v31).

Locktime and sequence

lockTime values below LOCKTIME_THRESHOLD are block heights; values at or above that threshold are Unix times. For a candidate block at height $h$ with rule context $R$ and locktime cutoff $t$, define the selected lock boundary:

$$ \ell(tx,h,t)= \begin{cases} h, & tx.\text{\code{lockTime}} < \text{\code{LOCKTIME_THRESHOLD}},\ t, & tx.\text{\code{lockTime}} \ge \text{\code{LOCKTIME_THRESHOLD}}, \end{cases} $$

$$ \begin{aligned} \operatorname{AbsFinal}(tx,h,t) \Longleftrightarrow;& tx.\text{\code{lockTime}}=0 \vee tx.\text{\code{lockTime}} < \ell(tx,h,t) \vee {}\ &\forall i:\ tx.\text{\code{inputs}}[i].\text{\code{sequence}} = \text{\code{SEQUENCE_FINAL}}. \end{aligned} $$

If $R$ includes BIP113, $t=MTP(C,h-1)$; otherwise $t$ is the candidate block timestamp.

If $R$ includes BIP68, relative locks apply only to version-2-or-higher transactions and only to inputs whose sequence does not set SEQUENCE_LOCKTIME_DISABLE_FLAG. For validation view $V$, define $SeqFinal(tx,C,V,h)$ as follows. If $tx.version < 2$, it is true. Otherwise, for each applicable input $i$, let $s_i$ be its sequence value, $v_i=s_i \mathbin& SEQUENCE_LOCKTIME_MASK$, and $c_i$ be the height of the coin spent by input $i$ in $V$. Height locks require:

$$ h > c_i + v_i - 1. $$

When $s_i$ sets SEQUENCE_LOCKTIME_TYPE_FLAG, the input is time locked instead. Let $\tau_i=MTP(C,c_i-1)$. Time locks require:

$$ \operatorname{MTP}(C,h-1) > \tau_i + v_i \cdot \text{\code{SEQUENCE_LOCKTIME_GRANULARITY}} - 1. $$

The transaction satisfies relative locks iff every applicable input satisfies its selected height or time constraint; disabled inputs impose no constraint. If $R$ does not include BIP68, $SeqFinal$ is true (sources: bip-0068,bip-0112,bip-0113,bitcoin-core-v31).

Signature hashes

Signature validation signs a digest derived from the transaction, input index, spent output, hash type, and active script version. Legacy, SegWit v0, and Taproot/Tapscript spend paths use different digest algorithms, specified in Section Script. These digests are consensus-critical because the signature checks validate exactly those bytes.