Skip to content

Bitcoin Primer

Bitcoin is regarded as the origin blockchain or cryptocurrency. While the last core upgrade to the Bitcoin network was in 2010 several smaller upgrades have been made adjacent to the core protocol, the most significant of these are Segwit and Taproot.

Bitcoin Blocks

A Bitcoin block can be thought of as a container attesting to a validity of several transactions. It starts with 0xD9B4BEF9 and then continues describing the new state of the network, including listing all newly included transaction. The Bitcoin block also describes a block header. The block header is a self-contained description of the newly added state. If you only cared about a subset of all transaction in a block, the block header is a more efficient description of the block itself.

For the purpose of validating transactions outside the noise of the core network, block headers are perfect. Satoshi Nakamoto designed block headers to be self describing, that is, if you have a list of block headers it is possible to verify if a new block header belongs to the list. A block header is 80 bytes and consist of:

Version(4B) | PrevBlock(32B) | MerkleRoot(32B) | Time(4B) | Bits(4B) | Nonce(4B)

https://en.bitcoin.it/wiki/Block_hashing_algorithm

By checking if the hash of the bitcoin hash is sufficiently low compared to the specified Bits, the header can be authenticated to be correctly mined. By checking if PrevBlock is the same hash as the leading transaction in your list, it can be verified to extend your list. Lastly, Bits has to be checked if it follows the difficulty rules.

You will have noticed that these checks do not assert any validity whether the included transactions within are valid. The performed checks can be viewed as the least amount of work required to authenticate a Bitcoin block. This technique is very fittingly called Simplified Payment Validation.

Bitcoin Transaction

This section has not been written yet.

Transaction Outputs

Transaction Outputs contains the spending conditions written in Bitcoin Script. Legacy transaction contains the entirety of the spending condition within the output itself while Segwit transaction places the spending condition in the witness and only stores the hash of it in the output. The Bitcoin blockchain itself has no concept of addresses, instead output scripts have been standardized into 7 defined transaction types with 5 still in general use today. The 2 that are generally not used anymore are P2PK and P2MS.

While non-standard scripts may be spendable by a user’s privatekey they are unlikely to be recognised by their wallet wallet. Additionally, most custom scripts are implemented through P2SH to allow wallets to pay into it.

Each standardised transaction type describes what the output looks like. The script below is a legacy P2PKH output script:

OP_DUB | OP_HASH160 | PUSH_20 | {publicKeyHash} | OP_EQUALVERIFY | OP_CHECKSIG

If you need to pay to a P2PKH address, the output script needs to have the above format. Additionally, publicKeyHash defines who the spender is. To fully generate an output script you thus need the target publicKeyHash. This is what the address is. A P2PKH address is 00 + publicKeyHash encoded with Base58Check. A Bitcoin address has 2 purposes:

  1. Identify which output script needs to be used.
  2. Identify which variable elements needs to be filled
UTXO Type Table

The below table have enumerated the 5 transaction types from 1 to 5.

VersionNameEncoding SchemePrefixHash Length
0UnknownIgnore
1P2PKHBase58Check(00+PKH)1*20
2P2SHBase58Check(05+SH)3*20
3P2WPKHBech32bc1q**20
4P2WSHBech32bc1q**32
5P2TRBech32mbc1p**32

* Prefix is determined by the encoding scheme.
** Part of the prefix – 1q/1p – is determined by the encoding scheme.

Transaction Inputs

Transaction inputs link to other transactions’ outputs along with the unlock conditions fulfilled. For a P2PKH transaction this is the publickey & signature of the transaction.

Importantly, the sum of all inputs must be greater than the outputs. The difference between the 2 is the fee and will be claimed by the miner.

Proving Bitcoin Transactions

This section has not been written yet.