An account that checks its own rules
An ordinary Ethereum account is one key: lose it and the account is gone, leak it and anybody can empty it. A smart account is a contract, so it can check rules before it acts, and somebody else can even pay its gas. This lab shows both on a local test chain, with no real money.
By the end of this lesson
Note
I can describe how a smart account carries out a UserOperation.
Key words
- EOA: an ordinary account, controlled by one private key.
- Smart account: an account that is a contract with its own rules.
- UserOperation: a signed request for what a smart account should do.
- Bundler: a server that sends UserOperations to the chain.
- EntryPoint: the contract that checks and runs UserOperations.
- Paymaster: a contract that pays gas for other accounts.
Starter: what you already know
Note
Answer in your head. 1. What does a digital signature prove? 2. What does a transaction pay gas for? 3. Why is a lost password worse when nobody can reset it? Answers: who approved a message; the work it causes; nothing can get you back in.
Before you start
Note
Press Start the lab on the bar at the top of this lesson and wait until it says it is running. There is one container, chain, a local test chain called anvil with the cast tool, and the terminal reaches it with docker exec chain, which runs one command inside it. Each Run button below types its command into your lab's terminal, one after another in the same session. To watch them, or to type your own, press Open terminal on the bar.
An ordinary account and a smart account
Alex's ordinary account has no code at all, just 5 ETH and a nonce of 0. Alex's smart account has 2950 bytes of contract code and names Alex's key as its owner. An app's key with no ETH tries to send a transaction and is refused: an ordinary account must pay its own gas.
docker exec chain ./steps/accounts.shShown here, not run. Press Start lab to type it into your own lab’s terminal.
One UserOperation, start to finish
Alex signs a UserOperation asking the smart account to add one to a counter. The bundler sends it to the EntryPoint, which checks the signature and then runs it. The event says success true with gas 114672, and the count goes from 0 to 1. The bundler was paid back from the account's deposit.
docker exec chain ./steps/userop.shShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Tip
Who sent the transaction to the chain, Alex or the bundler? Answer: the bundler. Alex only signed the UserOperation.
A paymaster pays for somebody else
Nia's smart account holds no ETH at all. A paymaster agrees to pay, so the operation runs and the paymaster's deposit falls from 1 to 0.999901392 ETH. Alex is not on the paymaster's list and is refused with AA34 paymaster refused, and so is Nia when she asks for more gas than its cap.
docker exec chain ./steps/paymaster.shShown here, not run. Press Start lab to type it into your own lab’s terminal.
A session key with a spending limit
Alex gives an app a key that may pay one shop, at most 0.01 ETH in all. Two payments of 0.004 ETH go through. The third would pass the limit and is refused, and so is a payment to anybody else and a key whose time is up. The shop ends with 0.008 ETH.
docker exec chain ./steps/session.shShown here, not run. Press Start lab to type it into your own lab’s terminal.
An ordinary account borrows code with EIP-7702
Sam signs an EIP-7702 delegation to BatchDelegate. His code changes from 0x to 0xef0100 followed by BatchDelegate's address, and in the same transaction he pays two people. Mallory tries to use his account and is refused, because the code checks who is calling. Delegating to the zero address undoes it.
docker exec chain ./steps/delegate.shShown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think a smart account is safe just because it is code. It is only as safe as its rules. A careless check, or a delegation signed to code that never asks who is calling, can let anybody move the money.
Exit check
Tip
Answer without looking back. 1. What must an ordinary account hold to send anything? 2. What does the EntryPoint do before it runs an operation? 3. Who paid for Nia's operation? 4. Why was the third session payment refused? Answers: ETH for gas; checks it; the paymaster; it would pass the limit.
For teachers
Tip
This lesson is a small example of a lesson worked in a lab: the steps and the Run buttons are the lesson, and the lab is each student's own copy. The lab is one service, a local test chain with the contracts deployed, and nothing here marks itself: the student hands in a few lines on a script they changed and ran. The EntryPoint is a simplified teaching model and the bundler a shell function; EIP-7702 is real. Key learning points: 1. An ordinary account is one key and pays its own gas. 2. A UserOperation is checked by the account, then run, through the EntryPoint. 3. A paymaster or a session key changes who pays and who may spend. Common mistake: thinking code alone makes an account safe. Equipment: the lab and a browser. Tip: act out user, bundler and EntryPoint with three students first. Copy it into your own module and change it, or build one from scratch with the Lab menu in the lesson builder.