
Lesson 1: Rethinking Web3 · Part 2/6 — Accounts and Cryptography
Topic: Accounts and cryptography (Keys & Signatures). Overview: Web2 vs Web3 account models / public-key cryptography / a Viem signing demo
2. Crypto wallets and the account system
1. Web2 vs Web3 account models
First, let’s look at how accounts differ in Web2 and Web3.
In Web2, if you want to use a website, you usually need to register an account and set a password first. The account and password are stored on the site’s servers. If you forget the password, you can still recover it through email or a phone number; in a pinch, you might even be able to go straight to a site administrator and get the account back.
And in Web3? An account is just a public key and a private key; no server stores our accounts. The blockchain only cares whether someone has ownership of an account, that is, whether they know the account’s private key. So in Web3, whoever holds the private key can control the account, but that also means that if the private key is lost, the account is lost for good.
2. How public-key cryptography (public / private keys) works
The Web3 account system is based on strict mathematical principles. It relies on proven mathematical theorems to keep the account system running, not on a centralized administrator.
That mathematical principle is public-key cryptography (asymmetric encryption).
A Web3 account is a private key. From the private key you can derive a public key, and from the public key you can generate a wallet address.
The wallet address is used to identify an account and to interact with the outside world.
The public key can be used to encrypt a piece of information, which is then decrypted with the private key.
The private key can also be used to sign a piece of information, after which the public key is used to verify whether the signature is valid.
Signing is an important operation. In Web2, we confirm ownership of an account by verifying the password the user enters. In Web3, we verify it with a signature.
And because signing and signature verification are mathematically guaranteed to be correct, in the world of Web3 an account represented by a public-private key pair can be used to log in to and use all Web3 apps.
2.1 Hands-on demo: generating an account
Generate an account with bit address.
Below, we’ll try it in practice and generate an account through bigaddress.org.

Keep moving your mouse around to create truly random numbers, until the progress reaches 100%.

A private key has now been generated.
You will each need to find a way to store the private key securely, and not let anyone else learn it. Otherwise your account is mine.
Next we’ll write some code and learn how to sign with a private key, and how to verify a signature with a public key.
The hands-on screen recording starts here.
Use Viem to write code that shows how to sign and verify a signature.
Series navigation (part 2 of 6)
Lesson 1: Rethinking Web3 is split into 6 X Articles.
← Previous: 01a — Overview
→ Next: 01c — Installing MetaMask
📚 Full table of contents: see README.md
🔗 Original long article: see ../01-Web3 认知重启.md
This part covers: accounts and cryptography
Code snippets from the article
The following code comes from code blocks in the original X Article, arranged in the order they appeared.
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'
import { recoverMessageAddress } from 'viem'
// 1. Generate a random private key
const privateKey = generatePrivateKey()
console.log("Private key:", privateKey)
// 2. Generate an account from the private key
const account = privateKeyToAccount(privateKey)
console.log("Address:", account.address)
console.log("Public key:", account.publicKey)
// 3. Sign a message
const message = "Hello Web3"
const signature = await account.signMessage({ message })
console.log("Message:", message)
console.log("Signature:", signature)
// 4. Verify the signature and recover the address
const recoveredAddress = await recoverMessageAddress({
message,
signature
})
console.log("Recovered address:", recoveredAddress)
console.log("Verification succeeded:", recoveredAddress === account.address)
This article was imported from an X Article. Original: https://x.com/i/status/2110700625659076774