Blockchain Fundamentals — Web3

A blockchain is a distributed database: data is stored in blocks, every block stores the previous block's hash, and many computers (nodes) hold a copy. A hash…

At Raka's cash desk there is one book of his customers' points (PTS), and only Raka holds it. Imagine the book is copied to hundreds of people, and every new page carries the "fingerprint" of the page before it: a short code computed from that page. If someone secretly changes one number on an old page, that page's fingerprint changes and no longer matches the one written on the next page. Everyone else's copy stays as it was, so the change is noticed. That is a blockchain: a record book copied onto thousands of computers, with every page locked to the one before it. A page is called a block, and its fingerprint is called a hash.

In this Web3 track (apps that use a blockchain) we follow Raka, who owns Raka's Shirt Shop: Coffee Print Shirt $95, Plain Black Shirt $75, Raka Cap $40, Coffee Print Mug $65. We will build two things:

Everything runs only on a local chain (a practice blockchain on your laptop) and on Sepolia, a testnet (a trial network) of Ethereum. Ethereum is a public blockchain whose currency is ETH, and we use test ETH that is worth nothing. No real money. To be clear from the start: for one shop, an ordinary database (a place to store data) fits better. The section "When a blockchain is the wrong tool" explains why.

Hashes: a fingerprint for data

A hash function turns data of any length into a number of fixed size. That number is called a hash. The hash functions used here produce 256 bits (each a 0 or a 1), written as 64 hex characters (digits and the letters a to f). Three properties:

Ethereum uses the hash function keccak256. Bitcoin, another public blockchain, uses SHA-256 (twice in a row). These are real results from both:

Input keccak256 SHA-256
Dinda +95 PTS 0x06aaf677...d910 0xad8590d2...e24d
Dinda +950 PTS 0x192b238a...4fae 0x67187bb8...7032

Only one extra 0 in the input, but the hashes look nothing alike. The 0x at the start marks a hex number, and the dots mean the hash is shortened.

This is how to compute the hashes with viem, a JavaScript library for Ethereum. The results are in the comments:

import { keccak256, sha256, toHex } from 'viem'

console.log(keccak256(toHex('Dinda +95 PTS')))
// 0x06aaf677ed2861f982be7e803ad4e383a15e099c2bd1d99886cec5b507e8d910
console.log(sha256(toHex('Dinda +95 PTS')))
// 0xad8590d26bec1871aa5c11e75fa5bd2f1234af7e6b096bb206d417079cd5e24d

The chain of blocks

A block is one page of the blockchain's record book. In our example it holds a number, the data, prevHash (the previous block's hash), a nonce (a trial number, explained below) and the block's own hash. The block's hash is computed from the other four: number|prevHash|data|nonce.

Raka's PTS book has one rule: a block's hash only counts if it starts with 0x00. To get such a hash, the computer tries nonce 0, 1, 2 and so on until the hash is valid. This search for a nonce is called mining. These are real results, computed with keccak256:

Block Data prevHash nonce hash
#0 Genesis: Raka's Shirt Shop PTS book 0x0000...0000 245 0x00be8036...3258
#1 Dinda +95 PTS (Coffee Print Shirt) 0x00be8036...3258 524 0x00d25d00...3f03
#2 Yoga +115 PTS (Plain Black Shirt, Raka Cap) 0x00d25d00...3f03 249 0x009e7f5e...7264
#3 Dinda +65 PTS (Coffee Print Mug) 0x009e7f5e...7264 309 0x00a2fb6c...28b7

Genesis is the first block. Notice that every block's prevHash equals the hash of the block in the row above.

Now change "Dinda +95 PTS" in the data of block #1 to "Dinda +950 PTS". The hash of block #1 becomes 0x01029e59...3f3b, which no longer starts with 0x00. The prevHash in block #2 still holds the old hash of #1, 0x00d25d00...3f03, so it doesn't match the new hash. The chain breaks at block #1.

To make the chain valid again:

  1. You re-mine #1. Its new nonce is 67.
  2. The new hash of #1 goes into the prevHash of #2, so #2's hash changes too and #2 has to be re-mined.
  3. The same happens to #3.

So 3 blocks have to be re-mined: #1, #2 and #3, 719 hash computations in all. Changing one block means recomputing every block after it.

Here is the code. hashBlock computes a block's hash:

const hashBlock = (b) => keccak256(toHex(`${b.number}|${b.prevHash}|${b.data}|${b.nonce}`))

function mine(b) {
  b.nonce = 0
  while (!hashBlock(b).startsWith('0x00')) b.nonce++ // try the next nonce
  b.hash = hashBlock(b)
  return b
}

function isValidChain(chain) {
  return chain.every((b, i) =>
    hashBlock(b) === b.hash && b.hash.startsWith('0x00') &&
    (i === 0 || b.prevHash === chain[i - 1].hash))
}

Digital signatures: who may write

A hash makes any change to the content visible. To prove who wrote it, a blockchain uses digital signatures:

Every transaction (a request to write to the blockchain, such as moving PTS) is signed with the private key. Anyone can check the signature with just the writer's address, without ever seeing the private key.

The signature algorithm is called ECDSA, and it uses the secp256k1 curve (a curve is the mathematical formula behind it). WebCrypto in the browser has ECDSA, but only for the P-256, P-384 and P-521 curves, not secp256k1, so viem and ethers bring their own JavaScript implementation.

Throughout this track we use the built-in test accounts of Hardhat and Anvil, two tools that run a local chain. Account #0 0xf39F...2266 is Raka, #1 0x7099...79C8 is Dinda, and #2 0x3C44...93BC is Yoga. These accounts' private keys are public on purpose: the command npx hardhat node shows the same private keys on every computer. Use them for local chains only, and never send real ETH to those addresses, because anyone can take it.

Consensus: PoW and PoS

Thousands of computers, called nodes, hold the same copy of the blockchain. Consensus is the rule that makes all nodes agree on which block comes next. The two best-known rules:

Proof of Work (PoW) Proof of Stake (PoS)
Used by Bitcoin Ethereum since The Merge, 15 September 2022
Who writes the block the miner who finds a nonce first a randomly picked validator who has locked 32 ETH
If they cheat their electricity and time are wasted their stake is cut (slashing)
Energy very large about 99.95% less than Ethereum's old PoW

A stake is the ETH that a validator (a node that writes blocks in PoS) locks as a deposit. With PoW, rewriting history (changing an old block and all blocks after it) takes more computing power than all the honest miners together. On Ethereum, every 12 seconds there is a slot (a turn) for one block. Other validators approve that block, and after about 13 minutes the block counts as final.

On-chain and off-chain

On-chain means stored on the blockchain: balances, smart contract code (a program stored and run on the blockchain) with its data, and the transaction history. All of it is public and permanent, and every write costs something. Off-chain is everything else: product photos, names, phone numbers, delivery addresses. Put on-chain only what really needs shared trust, such as PTS balances and who owns Coffee Edition Shirt number 7. Dinda's account address doesn't contain her name. But once someone knows the address is hers, anyone can read her whole transaction history.

When a blockchain is the wrong tool

For one shop's PTS, a points table in a Laravel database (Laravel is a web framework) wins at almost everything:

Raka's shop database Public blockchain
Cost per record practically free gas for every transaction
Cashier types it wrong just UPDATE it can't be deleted, needs a new transaction
Privacy only Raka sees it anyone can read it
Who is trusted Raka no single party needs to be

Gas is the transaction fee, and UPDATE is the database command that changes data. The last row is the blockchain's only advantage, but Raka's customers already trust Raka. A blockchain starts to make sense when several parties who don't trust each other must share one record that nobody owns. For example, five different shops accept the same PTS, and customers may move their PTS without anyone's permission. We still study blockchains because you want to be able to build one. An honest developer also knows when not to use one.

One more thing: blockchain transactions can't be undone. If you send ETH to the wrong address, no customer service can get it back. Many scams use this. Fake sites, for example, ask for your seed phrase (the 12 or 24 backup words of a wallet, the app that holds your private key). Others ask you to sign something you don't understand.

📌 Crypto rules differ by country (as of November 2026)

Each country has its own rules for crypto: who may run a crypto exchange, which authority regulates and supervises crypto-asset trading, and whether crypto may be used to pay. In most countries crypto is not legal tender, the money that must be accepted to settle a debt. Before a token you build is traded or used for payment, check the current rules where you live, on the website of your country's financial regulator. This note is not legal advice.

⚠️ The most common misconceptions about blockchains

TL;DR: A blockchain is a distributed database: data is stored in blocks, every block stores the previous block's hash, and many computers (nodes) hold a copy. A hash is a fingerprint of data: a short code computed from the data by a hash function (keccak256 on the Ethereum blockchain, SHA-256 on Bitcoin). A hash changes completely when the data changes slightly, so changing one block breaks the chain and forces every later block to be recomputed. Digital signatures (ECDSA with the secp256k1 curve) prove who wrote something without revealing the private key, the account owner's secret number. Consensus is the rule for agreeing on the next block. PoW (Bitcoin) does it by mining (searching for a valid hash), and PoS (Ethereum since 15 September 2022) with validators, nodes that lock a stake (a deposit in ETH, Ethereum's currency). On-chain (stored on the blockchain) means public and permanent, so personal data stays off-chain (outside the blockchain). For one shop, an ordinary database is cheaper, faster, correctable and private. A blockchain is useful when many parties who don't trust each other share one record. We only use a local chain (a practice blockchain on a laptop) and the Sepolia trial network, with test accounts whose private keys are public.

⚡ Hashes: a fingerprint for data

Write:

console.log(keccak256(toHex('Dinda +95 PTS')))
// 0x06aaf677ed2861f982be7e803ad4e383a15e099c2bd1d99886cec5b507e8d910
Guess first: keccak256 computes a hash, which is the fingerprint of a piece of data: a short code computed from it. The comment above shows the hash of Dinda +95 PTS. If the input becomes Dinda +950 PTS, how much of the hash changes?

All of it: it becomes 0x192b238a...4fae (shortened). Only one extra 0, but the hashes look nothing alike. This is called the avalanche effect.

Meaning: The same input always gives the same hash, and from a hash you can't find the input. toHex turns the text into bytes (units of data). The Ethereum blockchain uses keccak256, and Bitcoin uses SHA-256.

⚡ Every block is locked to the one before

Write:

const hashBlock = (b) => keccak256(toHex(`${b.number}|${b.prevHash}|${b.data}|${b.nonce}`))

Meaning: A blockchain is a distributed database: data is stored in blocks, every block stores prevHash, the previous block's hash, and many nodes (computers) hold a copy. hashBlock computes a block's hash from its number, prevHash, data and nonce (a trial number). Because prevHash is part of the computation, every block is locked to the block before it: any change there becomes visible.

⚡ Change one block, re-mine everything after it

Guess first: a block's hash only counts if it starts with 0x00. Mining means trying one nonce (a number) after another until the hash is valid. In the chain of blocks #0 to #3, "Dinda +95 PTS" in the data of block #1 is changed to "Dinda +950 PTS". How many blocks have to be re-mined?

3 blocks: #1, #2 and #3, 719 hash computations in all. The new hash of #1 goes into the prevHash of #2, so #2's hash changes too, and so on.

Meaning: The function that checks the chain, isValidChain, checks three things in every block: its hash matches a fresh computation, its hash starts with 0x00, and its prevHash equals the previous block's hash.

⚡ When a blockchain is the wrong tool

Meaning: For recording one shop's PTS (customer points), a table in an ordinary database is cheaper, correctable and private. A blockchain only makes sense when many parties who don't trust each other must share one record.

What you will learn