You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于ERC-4337账户抽象(Account Abstraction)智能合约及ethers.js 6版本的技术咨询

ERC-4337账户抽象(Account Abstraction)智能合约及ethers.js 6版本技术咨询

嘿,这是Alchemy提供的一份ERC-4337账户抽象相关的智能合约代码,包含基础账户合约和配套的工厂合约:

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;

import "@account-abstraction/contracts/core/EntryPoint.sol";
import "@account-abstraction/contracts/interfaces/IAccount.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/utils/Create2.sol";

contract Account is IAccount{
    uint public count;
    address public owner;
    constructor (address _owner) {
       owner = _owner;
    }

    function validateUserOp(UserOperation calldata userOp , bytes32 userOpHash , uint256 )
    external view returns (uint256 validationData) 
    {
      // 当前示例直接返回0,表示验证通过,实际项目需补充签名验证等逻辑
      return 0; 
    }
    
    function execute() external{
        count++;
    }
}

contract AccountFactory{
    function createAccount(address owner) external returns (address){
      Account acc = new Account(owner);
      return address(acc);
    }
}

合约核心说明

  • Account合约:实现了ERC-4337标准的IAccount接口,包含一个用于测试的状态变量count和账户所有者地址owner。validateUserOp是账户抽象的核心验证方法,当前仅做基础示例返回0,实际开发中需要补充签名校验、权限控制等逻辑;execute方法用于简单递增count值,模拟业务操作。
  • AccountFactory合约:提供批量创建Account实例的便捷方法,传入所有者地址即可生成对应的账户合约。

下面是对应这个合约的ethers.js 6版本交互示例,覆盖部署、创建账户、调用业务方法等常见操作:

import { ethers } from "ethers";

// 1. 初始化提供者与签名钱包
const provider = new ethers.JsonRpcProvider("你的RPC节点地址");
const wallet = new ethers.Wallet("你的私钥", provider);

// 2. 合约ABI(简化版,实际开发需使用完整ABI)
const accountFactoryABI = [
  "function createAccount(address owner) external returns (address)"
];
const accountABI = [
  "function count() external view returns (uint)",
  "function execute() external"
];

// 3. 部署AccountFactory合约
const AccountFactory = new ethers.ContractFactory(
  accountFactoryABI,
  // 替换为AccountFactory合约的字节码
  "0x...",
  wallet
);
const factory = await AccountFactory.deploy();
await factory.waitForDeployment();
console.log("AccountFactory部署地址:", await factory.getAddress());

// 4. 创建新的账户合约
const ownerAddress = wallet.address;
const createTx = await factory.createAccount(ownerAddress);
await createTx.wait();
// 获取创建的账户地址(通过静态调用获取返回值)
const accountAddress = await factory.createAccount.staticCall(ownerAddress);
console.log("生成的账户地址:", accountAddress);

// 5. 调用账户的execute方法
const accountContract = new ethers.Contract(accountAddress, accountABI, wallet);
const executeTx = await accountContract.execute();
await executeTx.wait();

// 6. 查询账户的count状态
const currentCount = await accountContract.count();
console.log("当前count值:", currentCount.toString());

开发注意点

  • 务必完善validateUserOp的验证逻辑,否则账户会存在严重的安全风险,比如任意地址都能触发执行操作。
  • ethers.js 6的API和旧版本有不少差异,比如用JsonRpcProvider替代了旧版的providers.JsonRpcProvider,用waitForDeployment替代了deployed()方法,开发时要注意适配。

备注:内容来源于stack exchange,提问作者Sree Krrish

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.16 11:18:10