Inputs: Previous txid

TransactionPrevious TXID
Structure
Version
4 bytes
Segwit (Optional)
Marker
1 byte
Flag
1 byte
Structure
Input Count
variable
Input Structure
Previous TXID
32 bytes
Output Index
4 bytes
ScriptSig Size
variable
ScriptSig
variable
Sequence
4 bytes
Structure
Output Count
variable
Output Structure
Amount
8 bytes
ScriptPubKey Size
variable
ScriptPubKey
variable
Structure
Witness
variable
Structure
Locktime
4 bytes

Size

32 bytes

Format

Natural Byte Order

Description

The TXID of the transaction containing the output you want to spend.

Example

7967a5185e907a25225574544c31f7b059c1a191d65b53dcc1554d339c4f9efc

Byte Visualization

79
Byte 1
67
Byte 2
a5
Byte 3
18
Byte 4
5e
Byte 5
90
Byte 6
7a
Byte 7
25
Byte 8
22
Byte 9
55
Byte 10
74
Byte 11
54
Byte 12
4c
Byte 13
31
Byte 14
f7
Byte 15
b0
Byte 16
59
Byte 17
c1
Byte 18
a1
Byte 19
91
Byte 20
d6
Byte 21
5b
Byte 22
53
Byte 23
dc
Byte 24
c1
Byte 25
55
Byte 26
4d
Byte 27
33
Byte 28
9c
Byte 29
4f
Byte 30
9e
Byte 31
fc
Byte 32

The previous transaction ID (txid) is a 32-byte hash that uniquely identifies the transaction containing the output we want to spend.

Let's examine our transaction to understand this better:

SVG Image

1- Byte Order

The txid uses Bitcoin's internal byte order (little-endian). Let's examine this transaction's previous txid:

python
# Display order (what you see on block explorers)
display_txid = "5e6e1a9b4ce3f9f39467d7129e3ecfbe6c81c08dd377aac666fedc9a758fe614"

# Internal order (how it appears in raw transaction)
internal_txid = "14e68f759adcfe66c6aa77d38dc0816cbecf3e9e12d76794f3f9e34c9b1a6e5e"

You can view this transaction in a block explorer using the display order:

-> https://mempool.space/tx/5e6e1a9b4ce3f9f39467d7129e3ecfbe...

Converting between byte orders

Here's a Python function to convert between display and internal byte orders:

python
def reverse_bytes(hex_string):
    """Convert between internal and display byte order"""
    return bytes.fromhex(hex_string)[::-1].hex()

2- Implementation Example

Here's how you might parse a transaction input's txid:

python
def parse_txid(raw_tx: bytes, offset: int = 0) -> tuple[str, int]:
    """
    Parse a transaction ID from raw transaction bytes
    
    Args:
        raw_tx: Raw transaction bytes
        offset: Starting position in bytes
        
    Returns:
        (txid, new_offset)
    """
    # Read 32 bytes for txid
    txid_bytes = raw_tx[offset:offset + 32]
    
    # Convert to hex string (internal byte order)
    txid = txid_bytes.hex()
    
    return txid, offset + 32

3- Inspecting with Bitcoin Core RPC Commands

You can use Bitcoin Core's RPC commands to look up and inspect transactions by their txid. Use getrawtransaction <txid> true to view the full transaction details:

bash
$ bitcoin-cli getrawtransaction \
    5e6e1a9b4ce3f9f39467d7129e3ecfbe6c81c08dd377aac666fedc9a758fe614 true

{
"txid": "5e6e1a9b4ce3f9f39467d7129e3ecfbe6c81c08dd377aac666fedc9a758fe614",
"hash": "5e6e1a9b4ce3f9f39467d7129e3ecfbe6c81c08dd377aac666fedc9a758fe614",
"version": 1,
"size": 225,
"vsize": 225,
"weight": 900,
"locktime": 0,
"vin": [
{
"txid": "14e68f759adcfe66c6aa77d38dc0816cbecf3e9e12d76794f3f9e34c9b1a6e5e",
"vout": 0,
"scriptSig": {
"asm": "3045022100b18dd5124cf2fcd31af3747a55ed941867f781c340007d9d86dae9e04fa8933f02200bdf817b2e6122ba97b8de40541728ead3988df7462241d6ce70e0f6788e126c[ALL] 02cd3dfbe89ebca9ee2cfeb5d36234c7d33bce1c329f8a0a318271a48f",
"hex": "483045022100b18dd5124cf2fcd31af3747a55ed941867f781c340007d9d86dae9e04fa8933f02200bdf817b2e6122ba97b8de40541728ead3988df7462241d6ce70e0f6788e126c012102cd3dfbe89ebca9ee2cfeb5d36234c7d33bce1c329f8a0a318271a48f"
},
"sequence": 4294967295
}
],
"vout": [...]
}

Note

Remember to use the display order of the txid when querying through RPC commands. The internal byte order used in raw transactions must be reversed.

Creating a TXID

The TXID is created differently depending on the transaction type:

  • Legacy transactions: HASH256 (double SHA-256) of all transaction data
  • Segwit transactions: HASH256 of transaction data, excluding the marker, flag, and witness fields

This difference in segwit transactions was intentionally designed to prevent transaction malleability, as signatures are no longer part of the TXID calculation.

Historical Note

Duplicate TXID: e3bf3d07d4b0375638d5f1db5255fe07ba2c4cb067cd81b84ee974b6585fb468

There was a unique situation in Bitcoin's history where the same TXID (above) occurred in multiple blocks: 91,722 and 91,880. As a result, BIP 30 was implemented to prevent blocks from containing duplicate TXIDs, and BIP 34 was introduced to require coinbase transactions to include block height in their data.

Implementation Details

Here's a detailed look at how transaction IDs are calculated in code:

python
class Tx:
    def id(self):
        """Human-readable hexadecimal of the transaction hash"""
        return self.hash().hex()

    def hash(self):
        """Binary hash of the legacy serialization"""
        return hash256(self.serialize())[::-1]

The transaction ID is created by:

  1. Serializing the entire transaction
  2. Performing a double SHA256 hash
  3. Reversing the byte order
  4. Converting to hexadecimal (for display)

Note

The transaction ID calculation differs between legacy and segwit transactions. For segwit, the witness data is excluded from the ID calculation to prevent transaction malleability.

Suggest Edits
View our public visitor count
Built with 🧡 by the Bitcoin Dev Project
We'd love to hear your feedback on this project?Give Feedback