Understanding Bitcoin Blockchain for Beginners

Understanding Bitcoin Blockchain for Beginners

A Web-2 dev's guide to other Web-2 devs on becoming the cool dev who knows Web3 šŸ˜‰šŸ‘ŠšŸ»

Ā·

9 min read

Foreword

Iā€™m a Web-2 dev (React-Native) and recently joined a Web-3 startup (which Iā€™m happy for) where I was tasked to create a Bitcoin wallet in React-Native which was in itself a task which I had no idea about since I did not know Blockchain itself, let alone Bitcoin, for me it was just a crypto-currency. So once I had put in the extra hours and got some clarity on the topic, I thought to create this blog series and explain people these critical concepts in the simplest way possiblešŸ˜Š

In this blog, Iā€™ll try to complete a complete summary of how the Bitcoin blockchain works and in the upcoming blogs, weā€™ll dive deep into each topic. So hold on tight and I wish you the best of luck in becoming the Cool Guy who knows Web-3 šŸ»


What the hell is this ā€œBlock Chainā€?

Well, itā€™s exactly what it sounds like, a chain of Blocks. What we need to understand is what are these blocks and why are they chained. Simply understand that these blocks store some information and every other block is pointing to the block previous to it, much like a linked list!

Now what information is stored in these blocks is determined by the type of blockchain weā€™re referring to. In Bitcoin these blocks store a list of the transactions that were done on this blockchain in a certain amount of time and some extra data (Weā€™ll discuss later). In Ethereum, they also contain something called smart contracts (Which are like chunks of code that could be executed on the Ethereum Virtual Machine)


The Bitcoin Blockchain: A Chain of Vaults

As I told you before, the Blocks in the Bitcoin chain contain a list of transactions which typically involve who sent to whom, whatā€™s the amount sent and what was the fees involved. A simple JSON representation of it would look like this

{
  "inputs": [ 
    {
      "txid": "abc123",
      "index": 0,
      "scriptSig": {
        "signature": "AliceSignature",
        "publicKey": "AlicePublicKey"
      }
    }
  ],
  "outputs": [
    {
      "value": 0.8,
      "scriptPubKey": "BobPublicKeyHash"
    },
    {
      "value": 0.19,
      "scriptPubKey": "AlicePublicKeyHash"
    }
  ],
  "fee": 0.01
}

Imagine Bitcoin as a chain of vaults, each containing locked assets called bitcoins. These locks signify ownershipā€”only the rightful owner can unlock and spend these bitcoins. When you send Bitcoin to someone, you unlock some vaults, create a new lock for the recipientā€™s public key, and broadcast the transaction to the network. Miners then verify that you have the right to unlock the vault and add your transaction to the blockchain.

Now letā€™s understand the same explanation in technical terms

The vaults weā€™re speaking to are called UTXO which stands for Unspent Transaction Outputs and again, they are exactly what they sound likešŸ˜‰. See in the Bitcoin blockchain, whenever you send someone an amount, we canā€™t directly send the exact amount as we do in the bank rather we first look at all the Unspent changes left with the sender, take some of them, and send all of them to the recipient, and the amount thatā€™s extra after that is returned to us as yet another UTXO which we can use in our future transactions

Understanding the JSON

Inputs

These are the vaults or the UTXO that youā€™re using to send the transaction. Each object in the input array signifies a UTXO of a given past transaction.

Transaction Id (txid): The txid is in little-endian format (The original ID is reversed to keep the least significant bye first) and refers to the hash of the transaction which yielded this change or UTXO.

index: The index comes into play when there are multiple UTXOs created in a transaction (arranged as an array) as change, this index helps us to select which UTXO we are using in the transaction.

Script Signature: The scriptSig helps us verify that weā€™re the owner of this UTXO, signature which is created by the sender using their private key (will be explained later promise šŸ¤žšŸ») along with their public key (Their public identity on the Bitcoin blockchain) is enough to verify that the person owning the signature is indeed the one in control of this UTXO

Outputs

This is very simple, this field specifies to whom are the bitcoins from the UTXO being sent. Usually, there are 2 objects in that array, the first one conveys the recipient through their publicKey and the amount that will be sent to them. the second object is the ā€œChangeā€ or the leftover amount that is returned back to the recipient in the form of UTXO

Fees

For every transaction that we do, we have to pay a fee. This fee goes to the person who verifies our transaction and adds it to the Block (called a miner ā›ļøšŸ‘·šŸ»)

Once the block is verified and added to the chain of blocks, our transaction is successful!

Now letā€™s understand every component in sufficient detail so that you can prepare your next web3 startup pitch šŸ˜Ž


Components in Bitcoin Blockchain

Now weā€™ll dive into all the components that help us to interact with the Bitcoin blockchain. In upcoming blogs, Iā€™ll deep dive into each of them but Iā€™ll try to keep their explanation as concise as possible here.

Bitcoin Node

The entire Bitcoin Blockchain is just a collection of computers collectively running a software called Bitcoin. These nodes are responsible for always being in sync with the Blockchain and can verify the transactions that are being done on the blockchain network. Think of it like every node has a linked list of Excel sheets which contain the transactions that were done on the blockchain. As soon as a new block or sheet is added, all the nodes sync up and update their local blockchain with the latest change.

Bitcoin Wallet

To interact with the Bitcoin blockchain, you need two keys:

  1. Public Key: Similar to a bank account number, itā€™s shared publicly to receive bitcoins.

  2. Private Key: Like your ATM PIN, itā€™s used to unlock and spend your bitcoins.

Together, these keys form your Bitcoin wallet, managing all bitcoins associated with your account.

Now letā€™s understand how Wallets Are Created

Step 1: Generating a Seed

The process begins by generating a pseudo-random 128-bit number called the seed. A checksum is derived using SHA-256 and appended to the seed, forming a 132-bit value.

Step 2: Mnemonic Generation

This 132-bit data is divided into 12 parts, each 11 bits long, and mapped to words from the BIP-39 word list. These 12 words are your mnemonicā€”the key to your wallet. Keep it safe, as anyone with access to it can control your bitcoins.

āš ļøWhat not to do with this Mnemonic Phrase

  • Paste it on your room (why? just why?)

  • Save it on your PC as a text file (Really?)

  • Tell your pet about it (They might take control over your crypto and join Illuminati)

  • Summon a demon with it (*negotiable*)

āœ…What to do with your Mnemonic Phrase

  • Invoke the great spirit of deception and ask it how you can securely store itšŸ§ššŸ»(*Optional*)

  • Jokes aside, be sure to take multiple hard copies and store them in different places so that even if one or more copies are destroyed youā€™re still having a backup. (Donā€™t tell anyone about these places, especially if youā€™re a married guy IYKYK)

Step 3: Creating Master Keys

The seed is passed through the HMAC-512 algorithm, producing a 512-bit output. The left 256 bits form your Master Private Key, and the right 256 bits form the Master Chain Code. Using Elliptical Curve Cryptography (secp256k), your Master Public Key is derived as:

P=kƗG

Where:

  • k is your private key.

  • G is a fixed starting point on the curve.

This process ensures security, making it computationally infeasible to derive the private key from the public key.

Using your Master Private Key and Master Chain Code, you can generate multiple child wallets in a tree-like structure (Hierarchical Deterministic Wallets). These wallets are secure because revealing a childā€™s private key doesnā€™t compromise the parentā€™s private key.

Wallet Address Types - There are a few different types of address that we can create which process transactions differently. Weā€™ll discuss them in a dedicated blog

  1. Legacy (P2PKH): Oldest format; addresses start with 1.

  2. SegWit (P2SH): Optimized version; addresses start with 3.

  3. Native SegWit (Bech32): Widely used; addresses start with bc1.

  4. Taproot: Uses advanced technology like Schnorr Signatures and Merkle Trees; addresses start with bc1p.

Transactions and UTXO

Bitcoin uses a system called UTXO (Unspent Transaction Output) to track balances. Think of UTXOs as leftover change after a transaction. Hereā€™s how it works:

  1. Fetch all UTXOs associated with your wallet.

  2. Define which UTXOs to unlock using your private key.

  3. Specify outputs:

    • Amount sent to the recipient.

    • Change returned to yourself.

Caution:

If you donā€™t specify the change, itā€™s given to miners as a fee!

Mining: The Backbone of Bitcoin

Miners validate transactions and add them to the blockchain. Hereā€™s the process:

  1. Verification: Miners ensure transactions are valid (e.g., you have enough balance).

  2. Merkle Root Calculation: The transactions in the block are in the form of a Hash, Miners Combine transaction hashes into a single hash, the Merkle Root which behaves as a check-sum for that can be used to trace if a particular transaction hash is there in a block or not.

  3. Block Hashing: Create a hash of the block data (transactions + metadata).

  4. Proof of Work: Miners solve a complex puzzle to generate a hash with a specified number of leading zeroes.

This computational effort ensures security and prevents tampering. Once verified, the block is added to the blockchain, and miners receive rewards (block reward + transaction fees).

Key Features of Bitcoin

  1. Decentralization: Operates without central authority.

  2. Security: Uses cryptographic algorithms to ensure tamper-proof transactions.

  3. Transparency: All transactions are public and verifiable.

  4. Scarcity: Only 21 million bitcoins will ever exist.


Limitations of Bitcoin

  1. Scalability: Limited transaction speed (7 Transactions Per Sec).

  2. Energy Consumption: High energy usage due to Proof of Work.

  3. Limited Use Cases: Primarily for transferring value; lacks programmability like Ethereum.


Bitcoin vs. Ethereum

FeatureBitcoinEthereum
PurposeDigital GoldProgrammable Money
Smart ContractsNoYes
Transaction Speed~10 minutes/block~12-15 seconds/block
Consensus MechanismProof of WorkProof of Stake (from ETH 2.0)

Emerging Technology: Lightning Network

Bitcoinā€™s Lightning Network addresses scalability by enabling off-chain, near-instant transactions. Itā€™s a layer-2 solution for microtransactions and faster payments.


Conclusion

Bitcoin is a groundbreaking technology that redefined how we perceive money and transactions. Understanding its core concepts like keys, wallets, UTXO, and mining can help you appreciate its power and potential. While it has limitations, ongoing developments like the Lightning Network continue to improve its functionality.

Stay tuned for more blogs if youā€™re excited to dive deeper into Bitcoin or blockchain technology!

Feel free to comment with any doubts or improvements. Iā€™ll be happy if you reach out on my socials and Iā€™d love a follow on Twitter šŸ¦… (Didnā€™t find this emoji šŸ¦)

Ā