paint-brush
An Essential Introduction to Bitcoin for Developersby@sonnyalvesdias
466 reads
466 reads

An Essential Introduction to Bitcoin for Developers

by Sonny Alves DiasFebruary 18th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

If you are going to work on a 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.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail

Coins Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - An Essential Introduction to Bitcoin for Developers
Sonny Alves Dias HackerNoon profile picture

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 structure

Source: Wikipedia


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


  • 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

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 right of appending a block in the blockchain is 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 blocks (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.


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 public and private keys.
  • 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 the 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 WalletsJustin, 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: https://www.cnn.com/2019/02/05/tech/quadriga-gerald-cotten-cryptocurrency/index.html

  • 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 an example from 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.


Also published on: https://sonny.alvesdi.as/introduction-to-bitcoin-for-developers