Dinda's stamp and signature always stay in her own bag. Raka's Shirt Shop's web page may hand her a document and say "please stamp this". Dinda reads it and decides, but the stamp never passes into the shop's hands. A wallet such as MetaMask works exactly like that: an app that keeps private keys and signs transactions when you approve them. A wallet doesn't "hold coins" either. Dinda's PTS balance is recorded on the blockchain, and the wallet only holds the key that proves the balance is hers. In this lesson we build a shop page that can connect to Dinda's wallet.
EIP-1193: one door called request
A browser wallet puts a provider on the page, usually at window.ethereum. Its shape is set by the EIP-1193 standard: one function, request({ method, params }), that returns a Promise, plus on(event, handler) to listen for changes. Every conversation with the wallet goes through that door, using Ethereum's JSON-RPC method names.
const ethereum = window.ethereum
if (!ethereum) show('Install a wallet first, MetaMask for example, then reload the page.')
else console.log(await ethereum.request({ method: 'eth_chainId' })) // '0xaa36a7'
window.ethereumdoesn't exist when no wallet is installed. Check first; don't use it straight away.eth_chainIdreturns a hex string:'0x7a69'is 31337 (the local chain),'0xaa36a7'is 11155111 (Sepolia),'0x1'is mainnet.
MetaMask can't be installed in this page's Lab, so the Lab uses a mock wallet that implements the same EIP-1193 interface: request, on, the same error codes. The signatures inside it were made beforehand by a real Hardhat node with test accounts.
Connect: eth_requestAccounts
async function connect() {
try {
const accounts = await ethereum.request({ method: 'eth_requestAccounts' })
return accounts[0] // '0x70997970c51812dc3a010c7d01b50e0d17dc79c8'
} catch (error) {
if (error.code === 4001) show('Connection rejected. Try again when you are ready.')
else throw error
}
}
eth_requestAccountsopens the wallet's popup. If approved, the result is an array of the allowed addresses, with the active one at index 0.- If Dinda presses Reject, the Promise fails with an error whose
codeis 4001 ("User Rejected Request" in EIP-1193), not an empty array. Catch it withtry/catchand show a friendly message. eth_accountsreturns the addresses already allowed without a popup (an empty array if none yet). Use it when the page loads. Calleth_requestAccountsonly when the user presses a "Connect" button.ethereum.enable()is the old, deprecated way. Don't use it.
Networks: chainId and wallet_switchEthereumChain
Dinda's wallet might be on mainnet, while our shop only runs on the local chain and Sepolia. Ask to switch with wallet_switchEthereumChain. If the wallet doesn't know the network yet, it refuses with code 4902, and you offer to add it:
async function switchToLocalChain() {
try {
await ethereum.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: '0x7a69' }] })
} catch (error) {
if (error.code !== 4902) throw error // 4902: the wallet doesn't know this chain yet
await ethereum.request({
method: 'wallet_addEthereumChain',
params: [{
chainId: '0x7a69',
chainName: 'Local Hardhat',
nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 },
rpcUrls: ['http://127.0.0.1:8545'],
}],
})
}
}
chainIdis always written in hex: 31337 becomes'0x7a69'.- Sepolia comes built into MetaMask (in the test networks list), so
wallet_switchEthereumChainwith'0xaa36a7'is enough. The local chain doesn't, so it has to be added first. - Every switch and every new network still needs the user's approval in the wallet.
Events: accountsChanged and chainChanged
Dinda can change account or network straight from her wallet at any time. The page is told through events:
ethereum.on('accountsChanged', (accounts) => {
if (accounts.length === 0) signOut() // the wallet disconnected
else switchUser(accounts[0])
})
ethereum.on('chainChanged', (chainId) => {
window.location.reload() // MetaMask's advice: reload and start from a clean state
})
accountsChangedcarries the new array. An empty array means the page may no longer see any account.- Don't store the address once and assume it stays. A login session must end too when the account changes.
Many wallets at once: EIP-6963
If Dinda installs two wallet extensions, both fight over window.ethereum, and only one wins. EIP-6963 (status Final) fixes that: every wallet announces itself through an event, and the page shows a choice.
const wallets = []
window.addEventListener('eip6963:announceProvider', (event) => {
wallets.push(event.detail) // { info: { uuid, name, icon, rdns }, provider }
})
window.dispatchEvent(new Event('eip6963:requestProvider'))
info.rdns is the wallet's unique name in reverse-domain form (for example io.metamask), and provider is the same EIP-1193 provider as before. Libraries such as wagmi (later in this track) already handle this.
Signing in with a wallet: personal_sign and SIWE
To sign in, the shop doesn't need a transaction. The server hands out a message with a random nonce, Dinda signs it with personal_sign, and the server checks the signature. The signature is free (no gas) and changes nothing on the blockchain. The message format is standardised by Sign-In with Ethereum (SIWE, ERC-4361):
raka-shirt-shop.test wants you to sign in with your Ethereum account:
0x70997970C51812dc3A010C7d01b50e0d17dc79C8
Sign in to Raka's Shirt Shop to see your PTS.
URI: https://raka-shirt-shop.test/login
Version: 1
Chain ID: 11155111
Nonce: k8Rq2vX7mPz4
Issued At: 2026-10-11T09:00:00Z
const utf8ToHex = (text) => '0x' + Array.from(new TextEncoder().encode(text), (b) => b.toString(16).padStart(2, '0')).join('')
const signature = await ethereum.request({
method: 'personal_sign',
params: [utf8ToHex(message), address], // the message first, then the address
})
// 0xd486f947a149...1ccf334c1c (65 bytes)
The signature above is real, made by a Hardhat node for Dinda's account. On the server, viem's verifyMessage({ address, message, signature }) returns true, and false for any other address. The server must also check the domain, the chain ID, the time, and that the nonce is used only once, so an old signature can't be replayed.
Nobody honest asks for a seed phrase
A seed phrase (12 or 24 words) is the backup of every private key in a wallet. An honest dApp never needs a seed phrase or a private key: all its work goes through request, and the wallet does the signing. A site, a form, or an "admin" in a chat that asks for a seed phrase is a scammer, every time. The same goes for a signature request whose content you can't understand.
Since EIP-7702 (the Pectra upgrade, May 2025) there is one more kind of request: "upgrade to smart account", or an authorization that makes your account run a contract's code. That code can take near complete control of your account. So approve it only when it is your own wallet's feature, never when it comes from a dApp you don't trust.
⚠️ What usually breaks the Connect button
- Comparing addresses with a plain
===. Some wallets return an all-lowercase address (0x7099...79c8), others the checksummed version (0x7099...79C8). Normalise first withgetAddress()from viem or ethers, ortoLowerCase(). - Not catching error 4001. Rejection is normal. Without a
catch, your page crashes just because the user said "not now". - Calling
eth_requestAccountswhen the page loads. A popup appears that nobody asked for. Check witheth_accounts, and ask to connect when the button is pressed. - Assuming the network is right. Check
eth_chainIdand listen tochainChangedbefore sending anything.
TL;DR: The wallet keeps the private key and signs; a dApp only talks through the EIP-1193 provider: request({ method, params }) and on(event, handler). eth_requestAccounts asks to connect through a popup and fails with error 4001 when rejected; eth_accounts checks without a popup. eth_chainId is hex (31337 = 0x7a69, Sepolia = 0xaa36a7); switch with wallet_switchEthereumChain, and on error 4902 add the chain first with wallet_addEthereumChain. Listen to accountsChanged (an empty array = disconnected) and chainChanged. EIP-6963 finds every installed wallet. Sign in with personal_sign over a SIWE message carrying a nonce, then the server verifies the signature. No honest dApp asks for a seed phrase.
⚡ A wallet keeps keys, not coins
Meaning: A wallet such as MetaMask keeps private keys and signs transactions when you approve them. Dinda's PTS balance is recorded on the blockchain; the wallet only holds the key that proves the balance is hers.
⚡ Connect with eth_requestAccounts
Write:
const accounts = await ethereum.request({ method: 'eth_requestAccounts' })
Guess first: if Dinda presses Reject in the wallet popup, what's in accounts?
Not an empty array. The Promise fails with an error whose code is 4001, so catch it with try/catch and show a friendly message.
Meaning: eth_requestAccounts opens a popup, so call it when the Connect button is pressed. When the page loads, check with eth_accounts, without a popup.
⚡ Chain IDs are written in hex
Write:
await ethereum.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: '0x7a69' }] })
Meaning: 31337 becomes '0x7a69'. If the wallet doesn't know the chain yet, it refuses with error 4902, and you add it first with wallet_addEthereumChain.
⚡ Nobody honest asks for a seed phrase
Meaning: An honest dApp never needs a seed phrase or a private key: all its work goes through request, and the wallet does the signing.