The Anatomy of Bitcoin Transactions

About Me

Information Security Consultant @ Blit-Tech

Software Developer @ Shelter Insurance

Email: nabil@blit.tech

Blockchains a Quick Refresher

What is a Bitcoin Transaction

A Bitcoin Transaction is a record assigning the outputs of a previous transaction to new outputs based on the Transaction Script

Glossary

Bitcoin Address
The hash of a public key
TxId
Transaction ID, hash of the transaction
UTxO
Unspent Transaction Outputs

Glossary Continued

Public Key Script
Transaction output script that allows the holder of the corresponding private key to spend the output
Signature Script
Script proving the creator is the owner of the corresponding private key
Coinbase Transaction
Special transaction that creates a new Bitcoin

The Transaction Structure

SizeNameTypeDescription
4versionuint32Version Number
0 or 2flagsuint8[2]Optional and always 0001
1 to 9tx_in countvar_intNumber of Input Transactions
Variestx_inInput TransactionsSpecifies prev. output and signature script
1 to 9tx_out countvar_intNumber of Outputs
Variestx_outOutput TransactionThe Output Transactions
4lock_timeuint32Currently always 0.

The tx_in Structure

SizeNameTypeDescription
36prev_outputoutpoint32 byte ID of the previous transaction + 4 byte index of specific output
1 to 9script lengthvar_intThe length of the Signature Script
Variessignature scriptuchar[]Script that confirms the specified output's script
4sequenceuint32Currently not used

The tx_out Structure

SizeNameTypeDescription
8valueint64The output's value
1 to 9script lengthvar_intThe length of the pk_script
Variespk_scriptuchar[]Script setting up the conditions that allow the coin to spent

Implications of the Structure

  • The transaction must consume (spend) the full value of all it's inputs
  • A transaction might have an output that is spent and another that isn't, thus UTxO
  • Two transactions might attempt to spend the same output of a transaction.

Scripts

  • Where the magic happens
  • Non Turing Complete
  • Stack Based
  • Stateless
  • Accepted Scripts are Standardized
  • Signature Script is prepended to PubKey Script

The Standard Scripts

  • P2PKH: Pay to public key hash
  • P2SH: Pay to script hash
  • Multisig: m-of-n signatures

"Other" Standard Scripts

  • Pubkey: Simplified version of P2PKH. Not used in new transactions
  • Null Data: Standard script for storing arbitrary data on the blockchain

P2PKH

Signature Script

<Sig> <PubKey>

PubKey Script

OP_DUP OP_HASH160 <PubkeyHash> OP_EQUALVERIFY OP_CHECKSIG

P2PKH Evaluation

<Sig> <PubKey> OP_DUP OP_HASH160 <PubkeyHash> OP_EQUALVERIFY OP_CHECKSIG

P2PK Evaluation

<Sig> <PubKey> <PubKey> OP_HASH160 <PubkeyHash> OP_EQUALVERIFY OP_CHECKSIG

P2PK Evaluation

<Sig> <PubKey> <PubkeyHash> <PubkeyHash> OP_EQUALVERIFY OP_CHECKSIG

P2PK Evaluation

<Sig> <PubKey> OP_CHECKSIG

P2PK Evaluation

TRUE

P2SH

Signaure Script

<sig> <redeemScript>

PubKey Script

OP_HASH160 <Hash160(redeemScript)> OP_EQAL

Any standard script can be used as the redeemScript

MultiSig

Signature Script

OP_0 <sig A> [sig B] [sig C...]

PubKey Script

<m> <A pk> [B pk] [C pk..] <n> OP_CHECKMULTISIG

Pubkey

Signature Script

<sig>

PubKey Script

<pubkey> OP_CHECKSIG

Null Data

No Signature Script since the transaction is not spendable

PubKey Script

OP_RETURN <0 to 40 bytes of data>

Coinbase Transaction

  • The first transaction in a new block
  • Has no input transactions
  • Outputs define how the block reward (fees + subsidy) can be spend

The True Nature of Bitcois

A wallet's Bitcoins are just the UTxO in the block chain that the wallet is allowed to spend