Overview about Bitcoin Transaction
Following description shows a deep inside into how Bitcoin blocks and transactions are looking on a byte-level.
The level of detail is something around six to seven on a scale from 0–10 (10 = Bitcoin hardcore developer). The target audience is a person who wants to understand the technical aspects of a block and transactions.
You should be familiar with Inputs, Outputs and Script language. Maybe this helps: https://medium.com/@marc_steiner/technical-summary-about-a-bitcoin-transaction-in-one-picture-1455e1812227
There are similar descriptions out there, but I have tried to reduce all the noise and come straight to the point.
I use following Block as example 0000000000000000000bd1dcc67100bddc69e85426a2d9b298f15f6fddc2f4d7, with this transaction 69DF7FDCD5AB9BA1CB3FF573731ACF6ED37CC9A1CB92C6ED7FFCABA92605E49B inside the block.
First, we have to understand what is inside of a Bitcoin block. There are two main parts; The block header and the transactions list.
The block header contains all of the relevant information to verify a block. The hash of the header represents the information of a block without transactions. Simplified Payment Verification devices (SPV) just save this 80 byte because of limited storage capacity. (Detailed description: Link, Link)
Now, we will have a closer look into the structure of one transaction. There are a lot of “byte-information” inside a single transaction. The same concept here; Just the SHA-256 hash of the full transaction information is stored in the Bitcoin block as the following pictures shows.
It is now interesting to understand how the byte structure of this transaction look like. A Bitcoin transaction based on the concept of inputs (“crawling” the Bitcoin to spend from a previous transaction) and outputs (how many Bitcoin will be sent to the next address). On byte level it looks like this:
Description of Bitcoin Inputs
Transaction version (fix 4 byte):
Identifies the protocol version being used by the node, currently “1”. (Detailed description: Link)
Input Count / Number of inputs (1–9 byte):
Indicates the upcoming number of inputs in this transaction.(Detailed description: Link)
Previous transaction hash (fix 32 byte):
Link to an existing transaction in the previous parent block (last block of Blockchain) so that the node knows to which block the new block must be linked. (Detailed description: Link)
Output index of previous transaction outputs (fix 4 byte):
Every output has a index number. The first output is index “0”. The index number shows which output (UTXO) from a previous transaction will be spent in this new transaction. (Detailed description: Link, Link)
Input script length (1–9 bytes):
Defines how long (number of bytes) the following input script (unlocking code) is.
ScriptSig data (many bytes):
Unlocking script, that unlocks your Bitcoin which will be spent (is part of each input script). The script checks if the conditions are full-filled to spend the unspent transaction output (UTXO). Or in more easy words: Unlocking code checks, if your are allowed to spend the Bitcoins you want to send.(Detailed description: Link, Link)
Input sequence number (fix 4 byte):
Normally 0xFFFFFFFF. Irrelevant unless transaction’s locktime is >0. A number intended to allow unconfirmed time-locked transactions to be updated before being finalized; currently not used except to disable locktime in a transaction. (Detailed description: Link)
Description of Bitcoin Outputs
Number of outputs (1–9 bytes):
How many outputs are following and will be spent (upcoming number of outputs).
Output value (fix 8 byte):
Specifies how many Bitcoin in Satoshi (BTC/ 10⁸) will be transfered to the next address.(Detailed description: Link)
Output Script length (1–9 bytes):
Defines how long (number of bytes) the following output script (locking code) is: ScriptPubKey Operation+ ScriptSig data.(Detailed description: Link)
Defines the steps, what the node has to do next (script).
ScriptPubKey data (many bytes):
Locking script, that defines the conditions when the outputs (Bitcoins) could be spent in a next transaction or more technical: It is a short script that explains what conditions must be met to claim ownership of bitcoins. The name “ScriptPubKey” comes from the fact, that this script needs a publicKey .(Detailed description: Link, Link, Link, Link)
Transaction lock time (4 byte):
Defines the earliest time that a transaction is valid (or minimum block height) and can be relayed on the network or added to the blockchain. (Detailed description: Link)
Note: bytes are little endian
Here you find a similar article on Medium with beautiful graphics.