ERC-4337 · Final(最终) · ERC · 2021-09-29
ERC-4337: Account Abstraction Using Alt Mempool
不改协议的账户抽象:智能合约钱包通过独立内存池发送 UserOperation,Bundler 经由 EntryPoint 合约将其上链,Paymaster 可以代付 gas。
它解决什么问题?
普通以太坊账户(EOA)非常僵硬:只有一把 secp256k1 私钥,丢了无法找回,没有消费限额,一笔交易只能做一次调用,而且必须持有 ETH 支付 gas。智能合约钱包可以做到这一切,但合约无法自己发起交易,必须有人用 EOA 触发并付费。
早期提案希望在协议内部解决,这需要硬分叉和共识。ERC-4337 仅用合约和链下基础设施实现了"账户抽象",因此今天就能在任何 EVM 链上运行。
它如何运作
- UserOperation。用户不再签交易,而是签一个
UserOperation对象:sender(智能账户)、nonce、callData、gas 上限、费用字段、可选的paymaster和任意格式的signature。 - 独立内存池与 Bundler。UserOperation 进入专用内存池。Bundler 取出它们、模拟执行、把多个打包进一笔普通交易并调用
EntryPoint.handleOps(...),由 Bundler 支付 gas。 - EntryPoint。一个经过审计的单例合约。它对每个操作调用发送账户的
validateUserOp(验证签名并向 EntryPoint 支付 gas),然后执行callData,最后补偿 Bundler。 - Paymaster(可选)。同意为操作支付 gas 的合约:赞助新用户、接受 ERC-20 代币代替 ETH 等。
- Factory(可选)。如果账户尚不存在,
initCode/factory会在同一操作中部署它,用户的第一个动作就可以从一个反事实地址发出。
结果是:支持 passkey、多签、社交恢复、会话密钥、批量调用和无 gas 入门的钱包,而无需任何协议改动。
示例代码
每个 ERC-4337 账户实现的接口(v0.7):
struct PackedUserOperation {
address sender;
uint256 nonce;
bytes initCode; // factory 地址 ++ factoryData,或为空
bytes callData;
bytes32 accountGasLimits; // verificationGasLimit (16 bytes) ++ callGasLimit (16 bytes)
uint256 preVerificationGas;
bytes32 gasFees; // maxPriorityFeePerGas (16 bytes) ++ maxFeePerGas (16 bytes)
bytes paymasterAndData; // paymaster ++ gas 上限 ++ paymasterData,或为空
bytes signature;
}
interface IAccount {
/// 签名有效返回 0,否则返回 1 (SIG_VALIDATION_FAILED)。
/// missingAccountFunds 非零时必须把该金额转给 EntryPoint。
function validateUserOp(
PackedUserOperation calldata userOp,
bytes32 userOpHash,
uint256 missingAccountFunds
) external returns (uint256 validationData);
}
一个刻意精简、由单个 EOA 密钥控制的账户:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import {MessageHashUtils} from "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
contract SimpleAccount is IAccount {
address public immutable entryPoint;
address public owner;
constructor(address _entryPoint, address _owner) { entryPoint = _entryPoint; owner = _owner; }
function validateUserOp(PackedUserOperation calldata op, bytes32 opHash, uint256 missing)
external returns (uint256)
{
require(msg.sender == entryPoint, "only EntryPoint");
address signer = ECDSA.recover(MessageHashUtils.toEthSignedMessageHash(opHash), op.signature);
if (missing != 0) payable(entryPoint).call{value: missing}("");
return signer == owner ? 0 : 1;
}
function execute(address to, uint256 value, bytes calldata data) external {
require(msg.sender == entryPoint, "only EntryPoint");
(bool ok, bytes memory ret) = to.call{value: value}(data);
require(ok, string(ret));
}
}
从客户端发送操作(viem 的 account-abstraction 模块):
import { createBundlerClient } from "viem/account-abstraction";
import { http } from "viem";
import { sepolia } from "viem/chains";
const bundler = createBundlerClient({
account, // SmartAccount 对象(如 toCoinbaseSmartAccount / toSimpleSmartAccount)
chain: sepolia,
transport: http("https://your-bundler.example/rpc"),
});
const hash = await bundler.sendUserOperation({
calls: [{ to: "0x...", value: 0n, data: "0x..." }],
// paymaster: true, // 让 Paymaster 代付 gas
});
const receipt = await bundler.waitForUserOperationReceipt({ hash });
底层调用的是 Bundler 的 JSON-RPC 方法 eth_sendUserOperation(userOp, entryPoint)。
需要注意
- EntryPoint 是共享合约,每个版本有固定地址(v0.6
0x5FF1…2789,v0.70x0000000071727De22E5E9d8BAf0edAc6f37da032)。账户和 Bundler 必须使用同一版本。 - 验证代码受到限制:不能以会让模拟与实际执行不一致的方式使用
TIMESTAMP等操作码,存储访问也受限(见 ERC-7562)。这正是内存池抗垃圾操作的关键。 - 你需要一个 Bundler 服务(Alchemy、Pimlico、Biconomy、Candide、自建……),通常还需要 Paymaster 服务;它们是基础设施,不是链的一部分。
- EIP-7702 让已有 EOA 表现得像智能账户;ERC-4337 原生支持 7702 账户,二者是互补而非竞争关系。