EIPs Explained

ERC-7769 · Draft(草案) · ERC · 2024-08-23

ERC-7769: JSON-RPC API for ERC-4337

标准化钱包与 ERC-4337 bundler 通信所用的 JSON-RPC 方法(发送、估算、查询 UserOperation),以及用于测试 bundler 的 debug 方法。

它解决什么问题?

在 ERC-4337 账户抽象中,用户并不发送普通交易。他们签署 UserOperation 对象,这些对象进入由 bundler 运行的独立 mempool,bundler 再把许多个打包进对 EntryPoint 合约的一次 handleOps 调用。eth_sendRawTransaction 和 eth_estimateGas 这类标准方法不适用于这一流程。

ERC-7769 定义了每个 bundler 都应提供的 RPC 接口,这样钱包或 SDK 无需修改代码即可切换 bundler 服务商。

它如何运作

eth 命名空间(公开):

方法 用途
eth_sendUserOperation 验证、模拟并将 UserOperation 加入 mempool;返回 userOpHash
eth_estimateUserOperationGas 返回 preVerificationGas、verificationGasLimit、callGasLimit(以及 paymaster 验证 gas);签名可以是占位值;接受 state override 集合
eth_getUserOperationByHash 返回 UserOperation 以及 blockNumber、transactionHash 等
eth_getUserOperationReceipt 返回 success、actualGasCost、actualGasUsed、该 op 自身的日志,以及该 bundle 的交易回执
eth_supportedEntryPoints bundler 接受的 EntryPoint 地址,首选的排在前面
eth_chainId EIP-155 链 ID

错误使用特定的代码,例如 -32602 字段无效,-32500 被 EntryPoint 验证拒绝(消息中带有 AAxx 代码),-32501 被 paymaster 拒绝,-32502 违反 ERC-7562 的 opcode 规则,-32503 超出时间范围,-32507 签名错误。

在支持 EIP-7702 的链上,UserOperation 可以携带 eip7702Auth 元组,将 factory 设为标记值 0x7702 即告诉 EntryPoint 该 sender 是经 7702 委托的 EOA;此时 factoryData 会被传给 sender 用于初始化。

debug 命名空间(仅用于测试): debug_bundler_clearState、dumpMempool、sendBundleNow、setBundlingMode、setReputation、dumpReputation、addUserOps。这些方法的存在是为了让共享的兼容性测试套件可以驱动任何 bundler。

示例代码

提交 UserOperation 并轮询其回执(TypeScript,直接使用 fetch):

async function rpc(method: string, params: unknown[]) {
  const res = await fetch(BUNDLER_URL, {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }),
  });
  const json = await res.json();
  if (json.error) throw new Error(`${json.error.code}: ${json.error.message}`);
  return json.result;
}

const [entryPoint] = await rpc("eth_supportedEntryPoints", []);
const gas = await rpc("eth_estimateUserOperationGas", [userOp, entryPoint]);
Object.assign(userOp, gas);
userOp.signature = await signUserOp(userOp, entryPoint); // wallet-specific

const userOpHash = await rpc("eth_sendUserOperation", [userOp, entryPoint]);

let receipt = null;
while (!receipt) {
  await new Promise((r) => setTimeout(r, 2000));
  receipt = await rpc("eth_getUserOperationReceipt", [userOpHash]);
}
console.log(receipt.success, receipt.actualGasCost);

需要注意

  • 状态:Draft。 字段名和错误码仍可能变化。
  • 所有数值字段都是十六进制字符串,空字节必须写成 "0x"。
  • eth_getUserOperationReceipt 中的 receipt 涵盖整个 bundle;请使用顶层的 logs 查看你自己 operation 的日志。
  • paymaster 的 postOpGasLimit 无法可靠估算;应由 paymaster 提供。
  • debug_* 方法绝不能暴露在生产端点上。

#account-abstraction #json-rpc #bundler #erc-4337 #wallet

详细信息

作者
Vitalik Buterin (@vbuterin), Yoav Weiss (@yoavw), Dror Tirosh (@drortirosh), Shahaf Nacson (@shahafn), Alex Forshtat (@forshtat)
状态
Draft(草案)
类型
Standards Track / ERC
创建日期
2024-08-23
依赖的提案

机器可读数据: Markdown · JSON