ERC-6900 · Draft(草案) · ERC · 2023-04-18
ERC-6900: Modular Smart Contract Accounts
模块化智能账户的草案标准:验证逻辑、执行函数和 hook 都放在可安装的模块中,因此同一个模块可以在任何兼容的 ERC-4337 账户上运行。
它解决什么问题?
ERC-4337 允许智能合约账户定义自己的验证和执行逻辑。会话密钥、支出限额、订阅或基于角色的访问控制等功能,要么内置在某个特定的账户实现中,要么是某个钱包平台的专有插件。用户被锁定在拥有所需功能的账户上,模块开发者则必须选择支持哪些平台。
ERC-6900 定义了账户与其模块之间如何交互,使任何兼容的模块都能安装到任何兼容的账户上,并对每个模块被允许做什么给出明确规则。
它如何运作
模块化账户把逻辑拆分为三类模块化函数,全部托管在外部模块合约中:
- 验证函数决定某个操作是否被授权。有三种流程:用户操作验证(通过 EntryPoint 的
validateUserOp)、运行时验证(通过executeWithRuntimeValidation直接调用)以及签名验证(ERC-1271 的isValidSignature)。 - 执行函数为账户添加新的可调用函数(选择器)。
- Hook:验证 hook 在验证函数之前运行(例如用于强制执行限额),执行 hook 在执行之前和/或之后运行(pre-hook 可以向 post-hook 传递数据)。
账户必须实现 ERC-4337 的 IAccount 和 IAccountExecute、ERC-1271 以及 IERC6900Account:
execute、executeBatch、executeWithRuntimeValidationinstallExecution/uninstallExecution,接收模块的执行清单(execution manifest)(其选择器、hook 和接口 ID)installValidation/uninstallValidation,接收带有isGlobal、isSignatureValidation和isUserOpValidation标志的ValidationConfig,以及它可验证的选择器和它的 hookaccountId()
模块实现 IERC6900Module(onInstall、onUninstall、返回 "vendor.module.semver" 的 moduleId()),以及验证、验证 hook、执行或执行 hook 模块接口中的任意一种。
关键规则:除非被标记为全局,否则验证函数只能授权它安装时对应的选择器。从 execute 回调账户自身的调用会受到额外检查,因此权限狭窄的模块无法提升自己的权限。
示例代码
一个最小模块的形态(基于规范中的接口):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;
import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
interface IERC6900Module is IERC165 {
function onInstall(bytes calldata data) external;
function onUninstall(bytes calldata data) external;
function moduleId() external view returns (string memory);
}
contract DailyLimitModule is IERC6900Module {
mapping(address account => uint256) public limit;
function onInstall(bytes calldata data) external {
limit[msg.sender] = abi.decode(data, (uint256)); // msg.sender is the account
}
function onUninstall(bytes calldata) external {
delete limit[msg.sender]; // clean up, or stale state may be reused on reinstall
}
function moduleId() external pure returns (string memory) {
return "example.daily-limit.1.0.0";
}
function supportsInterface(bytes4 id) external pure returns (bool) {
return id == type(IERC6900Module).interfaceId || id == type(IERC165).interfaceId;
}
// A real module would also implement a validation-hook or execution-hook interface.
}
需要注意
- 状态:Draft。接口经历过重大修订;请确认你的账户和模块所针对的规范版本。
- 模块是受信任的代码。恶意模块可以添加会 revert 的 hook 使账户无法使用,或添加执行函数把账户资产转走。请只安装经过审计的模块。
- 要谨慎对待全局验证:全局启用的验证函数可以授权暴露给它的任何原生函数,这实际上相当于 root 权限。
- 规范允许忽略
onUninstall的失败,因此模块可能留下过期状态。请彻底清理,尤其是在使用相同实体 ID 重新安装之前。 - ERC-7579(Draft)是另一种更精简的模块化账户标准。两者不可互换。