Calculating the TXID
Calculating the TXID
The Transaction ID (TXID) is the primary key of the Bitcoin ecosystem. Just like a Block Hash, the TXID is a deterministic cryptographic result of the transaction data.
1. The Algorithm
A TXID is calculated by performing a Double-SHA256 hash on the serialized transaction data. $$TXID = SHA256(SHA256(Raw\ Transaction))$$
2. What is Hashed?
For a standard (non-SegWit) transaction, the hash includes:
-
Version
-
Inputs (TXID, VOUT, ScriptSig, Sequence)
-
Outputs (Value, ScriptPubKey)
-
Locktime
If you change even a single bit in the signature (ScriptSig) or the recipient's address, the resulting TXID will be completely different.
3. Transaction Malleability
Because the signature is part of the data being hashed for the TXID, a problem called Transaction Malleability once existed.
-
An attacker could slightly modify a signature without making it invalid.
-
This would result in a different TXID.
-
This made Layer 2 protocols (like Lightning) difficult to build because they relied on fixed TXIDs.
-
SegWit solved this by moving the signatures (Witness) to a separate area that is not included in the primary TXID.
4. Big-Endian vs. Little-Endian
Similar to block hashes, TXIDs are displayed in Big-Endian by block explorers but processed in Little-Endian by the protocol.
-
Explorer TXID:
4a5e1e... -
Protocol TXID:
...1e5e4a
| Step | Action |
|---|---|
| 1. Serialize | Convert the TX fields into a raw byte stream. |
| 2. Hash | Apply SHA256 twice. |
| 3. Finalize | The resulting 32 bytes is the internal TXID. |
In the next section, we will discuss how nodes use the TXID to reference spendability.
TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation: