Introduction to Bitcoin for developers

ยท

9 min read

Play this article

If you are going to work on software using Bitcoin or Liquid, there are several concepts you need to be familiar with. This article aims at developers and is based on a presentation I held for the Pixelmatic team.

Disclaimer: this is an introduction and it does not cover everything obviously. And I simplified some explanations on purpose.

Summary

Bitcoin comes in the form of a daemon, a background process. Which is composed essentially of these components:

  • ๐Ÿ”— Blockchain - a distributed ledger of transactions
  • ๐Ÿค๐Ÿผ Consensus protocol - a peer to peer network with PoW consensus
  • ๐Ÿ‘› Wallet(s) - a tool to manage your funds
  • ๐Ÿค– Script - a programming language to state the behavior of the transactions
  • ๐Ÿ“ƒ API - an RPC interface to interact with the bitcoin node

We will go through these concepts and I will add some other important notions on top of these subjects.

๐Ÿ”— Blockchain

In essence, the Bitcoin blockchain is a linked list where a node has a link to its parent.

Bitcoin Blockchain Bitcoin blockchain structure

Source: Wikipedia

A node, or called block too, contains also information:

  • A Merkle tree indexing and containing the transactions
  • A timestamp to state at what time the block was added to the blockchain
  • A nonce which is manipulated in the step of mining

    There is a bit more information in the Bitcoin blocks, but these are the essentials.


๐Ÿ’ฑ UTXOs

Before looking at the consensus protocol, I think it's important to understand the transaction model used in Bitcoin and the concept of UTXO.

Example of transactions Example of transactions

Source: Medium

  • In a transaction, the sum of the inputs is always equal to the sum of the outputs, like in the example above.
  • UTXO stands for Unspent Transaction Output. This is the actual money you can spend and are deemed to become inputs.
  • In the example, Alice sends 0.5 BTC to Bob and sends the rest back to her. To maximize privacy, the rest of the input's owner is conventionally sent back to another address different from the original UTXO, called change address.
  • You can use multiple UTXO as inputs to fund a transaction.

๐Ÿค๐Ÿผ Consensus protocol

The consensus protocol is made for securing the funds on Bitcoin, and validate what is the source of truth.

In order to do that, Bitcoin nodes are part of a peer-to-peer network and the rights of appending a block in the blockchain are somehow randomized.

The consensus happens on two steps:

  • When signaling a transaction to the network

    The direct peers of the bitcoin node broadcasting a transaction will evaluate the transaction and if it is valid will add it into their mempool and continue the propagation of the information.

  • When submitting a new block to the network

    The direct peers of the bitcoin node submitting the block will evaluate the block and if it is valid will append it into their blockchain and broadcast it to their other peers.

The mempool

The mempool is a buffer of a fixed size where pending transactions are recorded. And every bitcoin node hosts its own instance of mempool that is synchronized through the peer-to-peer network. Note that if the mempool is full, the transactions with the lowest fees will start to be canceled.

Since it is a limited size buffer, you are in competition with the other people doing transactions. Hence transactions include a fee. You can modulate that fee up to increase your chance to get recorded in a block quickly. The higher the fees you pay, the more interest the miners will have to include in your transaction to maximize their gains.

Mining and Proof-of-work

To earn the right to append a block to the blockchain, the bitcoin nodes use the concept of proof of work.

On the bitcoin network, there is a variable synchronized on every node called the difficulty. That variable corresponds to the mining difficulty. And to earn the right to append a block, the goal is to try different nonces in a block template provided by the bitcoin daemon until the value of the hash is lower than a certain target hash extrapolated from the difficulty.

It is visible by looking at the hashes of the blocks, for example:

  • at block 2, the difficulty was 1, and the block hash: 000000006a625f06636b8bb6ac7b960a8d03705d1ace08b1a19da3fdcc99ddbd,
  • at block 718023, the difficulty was 24371874614345, and the block hash: 000000000000000000024d24499352253749af74e93c4c50553c1ec747375953.

The higher the difficulty, the lesser chance to find a block, the smaller the target number will be, thus the higher number of leading zeroes there will be.

In the first blocks, the difficulty was 1. And every 2016 block (about two weeks), the difficulty is adjusted up or down in order to make sure we get blocks every 10 minutes on average. Because this operation of trying nonce and hashing is actually predictable (on average).

Network splits

One last interesting note is how the network reacts to network splits. Imagine a malicious country cutting the internet at its borders. Also stopped all their Bitcoin miners (or almost). At first, the difficulty would be too high for them to find blocks every 10 minutes or even any blocks at all. Then the network would adjust the difficulty to a low value that would match the last two weeks' hashrate. At that moment, maybe the difficulty is back to 1. If they turned back on all their Bitcoin miners then they would validate blocks not only every 10 minutes but potentially every second or less. Soon enough their domestic Bitcoin blockchain could surpass the height of the blockchain of the rest of the world. What will happen if they open back their internet borders? Well, all their effort to erase the blockchain will be crushed. Because Bitcoin will agree that the one true blockchain is the longest chain with the most work. So the domestic with X amount of blocks in advance but Y amount of less work would be quickly replaced by the rest of the world chain having fewer blocks, but a lot more work committed.


๐Ÿ‘› Wallets

Bitcoin uses something called Hierarchical Deterministic Wallets.

HD Wallets BIP32 Hierarchical Deterministic Wallet

Source: Medium

  • The goal of the wallet is to simplify the management of your funds and UTXOs. Instead of manipulating granularly UTXOs, the Bitcoin node will fund your transactions automatically by prioritizing the UTXOs with the highest amount.
  • From an initial seed, we create a key pair of a public and private key.
  • Those keys can be derived to obtain child keys.
  • Initially, the derivation was thought to possibly manage multiple accounts and for multiple coins. What happens in practice is often you don't mix coins in your wallets, but it's possible, for instance, Ethereum also uses HD Wallets.
  • The first use case of having multiple accounts is for change addresses. That way you make sure that the addresses you generate on the fly to receive payments do not overlap the change addresses. Maximizing privacy.
  • Having multiple accounts can be helpful to split balance for managing different budgets.

๐Ÿง‘โ€๐Ÿคโ€๐Ÿง‘ Multisig Wallets

Wallets can be also shared among multiple owners, and we call those multisignature wallets.

Multisignature Wallets Justin, Vittie, and Craig each hold one of the three keys needed to unlock the multisig wallet.

Source: Coindesk

There are two major use cases for the multisignature wallets:

  • Share funds across multiple people. Imagine a company owning a bunch of Bitcoins like a crypto exchange, having a multisignature wallet will make sure that if someone is not available other people on the team can access the funds.

    Real-life example:

  • Have a backup of your wallet. Imagine you could have a signature on your computer, on your phone, and the third one on an old device hidden. If you lose any one of your devices, then you still have a chance to access your funds and move them to a new wallet.

๐Ÿค– Script

Bitcoin offers a simple stack-based programming language, non-Turing complete (on purpose). Below find the page about it from the Bitcoin Wiki.

Source: Bitcoin Wiki

  • Bitcoin is sometimes called programmable money because of this language.
  • It describes how UTXOs should be validated.
  • I would invite you to read some Script examples from the Bitcoin wiki to quickly understand the nature of the language
  • FYI The opcode OP_RETURN can be used to leave a message on the blockchain.

๐Ÿ“ƒ API

To interact with the bitcoin daemon, it exposes an RPC interface. This is what the bitcoin CLI tool uses, but you could also use curl or any HTTP client to communicate with a bitcoin node.

Source: Bitcoin.org

  • You can do about everything with the interface, so it's not safe to expose it on the internet.
  • If really you need to expose it, you can define RPC users with credentials.
  • Some examples:
    • To send 1 BTC to a friend: bitcoin-cli sendtoaddress 1 ${YOUR_FRIEND_ADDRESS}
    • To get your balance: bitcoin-cli getbalance
    • To submit a block to the network: bitcoin-cli submitblock ${HEX_BLOCK}
    • To check if your local blockchain is fully synchronized: bitcon-cli getblockchaininfo

๐Ÿ—ฃ๏ธ Bitcoin Improvement Proposal (BIP)

Major updates and changes in Bitcoin are always first discussed and properly specified in a document labeled BIP. All the BIPs and work-in-progress BIP are stored there:

github.com/bitcoin/bips


๐ŸŽ‡ Conclusion

As of 2022 and several years from now, if you understand all the concepts cited in that article, you are good enough to start with your development using Bitcoin.

Depending on what part of Bitcoin you will be working with or using, here are some hints on where you should start digging:

  • Regarding mining, blocks contain more information than I quoted,
  • Regarding transactions and Script, there are different types of transactions using Script: p2sh, p2pkh, p2wpkh, but also an interesting thing called Hashed TimeLock Contract (HTLC),
  • Regarding wallets, you probably want to look at the latest updates with Taproot that allows for more privacy with multisignature wallets,
  • More generally there are Lightning as layer 2 of Bitcoin solution for payments settlement, and Liquid a sidechain of Bitcoin with confidential transactions and assets issuance.

SPOILER In a future article, I will introduce Liquid, so stay tuned.

Did you find this article valuable?

Support Sonny Alves Dias by becoming a sponsor. Any amount is appreciated!

ย