UNPKG

@owlprotocol/contracts-account-abstraction

Version:

ERC4337 Account Abstraction support for deploying relevant smart contracts and interacting with UserOps.

258 lines (257 loc) 9.47 kB
import { describe, test, beforeEach, expect, beforeAll } from "vitest"; import { createPublicClient, createWalletClient, http, encodeAbiParameters, encodeFunctionData, parseEther, hexToBytes, concatHex, zeroAddress, nonceManager } from "viem"; import { localhost } from "viem/chains"; import { getLocalAccount } from "@owlprotocol/viem-utils"; import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; import { entryPoint07Address, getUserOperationHash } from "viem/account-abstraction"; import { port } from "./test/constants.js"; import { VerifyingPaymaster } from "./artifacts/VerifyingPaymaster.js"; import { IEntryPoint } from "./artifacts/IEntryPoint.js"; import { getSimpleAccountAddress } from "./SimpleAccount.js"; import { ERC1967Proxy } from "./artifacts/ERC1967Proxy.js"; import { SimpleAccountFactory } from "./artifacts/SimpleAccountFactory.js"; import { dummySignature, encodeUserOp } from "./models/UserOperation.js"; import { erc4337Contracts, setupVerifyingPaymaster } from "./setupERC4337Contracts.js"; import { toPackedUserOperation } from "./models/PackedUserOperation.js"; import { getVerifyingPaymasterHash } from "./VerifyingPaymaster.js"; import { estimateUserOperationGas } from "./actions/bundler/estimateUserOperationGas.js"; describe("VerifyingPaymaster.test.ts", function() { const chain = { ...localhost, rpcUrls: { default: { http: [`http://127.0.0.1:${port}`] } } }; const transport = http(chain.rpcUrls.default.http[0]); const publicClient = createPublicClient({ chain, transport }); const walletClient = createWalletClient({ account: getLocalAccount(0, { nonceManager }), chain, transport }); let simpleAccountFactory; let verifyingPaymaster; beforeAll(async () => { simpleAccountFactory = erc4337Contracts.simpleAccountFactory; verifyingPaymaster = (await setupVerifyingPaymaster(walletClient, { verifyingSignerAddress: walletClient.account.address })).address; }); test("getVerifyingPaymasterHash", async () => { const validUntil = Date.now() + 3600; const validAfter = 0; const paymasterDataUnsigned = encodeAbiParameters( [ { name: "validUntil", type: "uint48" }, { name: "validAfter", type: "uint48" } ], [validUntil, validAfter] ); const userOperation = { sender: zeroAddress, nonce: 0n, factory: zeroAddress, factoryData: "0x", callData: "0x", preVerificationGas: 0n, verificationGasLimit: 0n, callGasLimit: 0n, maxFeePerGas: 0n, maxPriorityFeePerGas: 0n, signature: dummySignature, paymaster: verifyingPaymaster, paymasterVerificationGasLimit: 0n, paymasterPostOpGasLimit: 0n, paymasterData: paymasterDataUnsigned }; const userOperationPacked = toPackedUserOperation(encodeUserOp(userOperation)); const hashExpected = await publicClient.readContract({ address: verifyingPaymaster, abi: VerifyingPaymaster.abi, functionName: "getHash", args: [userOperationPacked, validUntil, validAfter] }); const hash = getVerifyingPaymasterHash({ chainId: publicClient.chain.id, userOperation: userOperationPacked }); expect(hash).toBe(hashExpected); }); describe("Exec existing Simple Account", () => { let account; let simpleAccount; beforeEach(async () => { const privateKey = generatePrivateKey(); account = privateKeyToAccount(privateKey); const simpleAccountAddress = getSimpleAccountAddress( { owner: account.address, salt: 0n }, { factoryAddress: simpleAccountFactory, proxyBytecode: ERC1967Proxy.bytecode } ); const simpleAccountFactoryData = encodeFunctionData({ abi: SimpleAccountFactory.abi, functionName: "createAccount", args: [account.address, 0n] }); simpleAccount = { address: simpleAccountAddress, factoryData: simpleAccountFactoryData, factoryAddress: simpleAccountFactory }; const { request: createAccountRequest } = await publicClient.simulateContract({ account: walletClient.account, address: simpleAccountFactory, abi: SimpleAccountFactory.abi, functionName: "createAccount", args: [account.address, 0n] }); const createAccountHash = await walletClient.writeContract(createAccountRequest); await publicClient.waitForTransactionReceipt({ hash: createAccountHash }); const fundSimpleAccountHash = await walletClient.sendTransaction({ to: simpleAccount.address, value: 1n }); await publicClient.waitForTransactionReceipt({ hash: fundSimpleAccountHash }); }); test("submitUserOp - manual", async () => { const to = privateKeyToAccount(generatePrivateKey()).address; const value = 1n; const data = "0x"; const callData = encodeFunctionData({ abi: [ { inputs: [ { name: "dest", type: "address" }, { name: "value", type: "uint256" }, { name: "func", type: "bytes" } ], name: "execute", outputs: [], stateMutability: "nonpayable", type: "function" } ], args: [to, value, data] }); const validUntil = Date.now() + 3600; const validAfter = 0; const paymasterDataUnsigned = encodeAbiParameters( [ { name: "validUntil", type: "uint48" }, { name: "validAfter", type: "uint48" } ], [validUntil, validAfter] ); const paymasterDataDummySignature = concatHex([paymasterDataUnsigned, dummySignature]); const gasPrice = await publicClient.estimateFeesPerGas(); const userOpData = { sender: simpleAccount.address, nonce: 0n, callData, paymaster: verifyingPaymaster, //Empty, will be replaced with signature paymasterData: paymasterDataDummySignature, maxFeePerGas: gasPrice.maxFeePerGas }; const { preVerificationGas, verificationGasLimit, callGasLimit, paymasterVerificationGasLimit, paymasterPostOpGasLimit } = await estimateUserOperationGas( { ...publicClient, entryPointSimulationsAddress: erc4337Contracts.pimlicoEntrypointSimulations }, userOpData ); const userOp = { sender: simpleAccount.address, nonce: 0n, signature: "0xfffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c", callData, callGasLimit, verificationGasLimit, preVerificationGas, maxFeePerGas: gasPrice.maxFeePerGas, maxPriorityFeePerGas: gasPrice.maxPriorityFeePerGas, paymaster: verifyingPaymaster, //Empty, will be replaced with signature paymasterData: paymasterDataDummySignature, paymasterVerificationGasLimit, paymasterPostOpGasLimit }; const userOpPaymasterPacked = toPackedUserOperation(encodeUserOp(userOp)); const userOpPaymasterHash = await publicClient.readContract({ address: verifyingPaymaster, abi: VerifyingPaymaster.abi, functionName: "getHash", args: [userOpPaymasterPacked, validUntil, validAfter] }); const paymasterSignature = await walletClient.account.signMessage({ message: { raw: userOpPaymasterHash } }); const paymasterSignatureBytes = hexToBytes(paymasterSignature); expect(paymasterSignatureBytes.length).toBeGreaterThanOrEqual(64); expect(paymasterSignatureBytes.length).toBeLessThanOrEqual(65); const paymasterDataSigned = concatHex([paymasterDataUnsigned, paymasterSignature]); userOp.paymasterData = paymasterDataSigned; const userOpHash = getUserOperationHash({ userOperation: userOp, entryPointAddress: entryPoint07Address, entryPointVersion: "0.7", chainId: localhost.id }); const signature = await account.signMessage({ message: { raw: userOpHash } }); userOp.signature = signature; const userOpPacked = toPackedUserOperation(encodeUserOp(userOp)); const handleOpsArgs = [[userOpPacked], walletClient.account.address]; const paymasterDeposit = await publicClient.simulateContract({ account: walletClient.account, address: verifyingPaymaster, abi: VerifyingPaymaster.abi, functionName: "deposit", value: parseEther("10"), args: [] }); const paymasterDepositHash = await walletClient.writeContract(paymasterDeposit.request); await publicClient.waitForTransactionReceipt({ hash: paymasterDepositHash }); const { request } = await publicClient.simulateContract({ account: walletClient.account, address: entryPoint07Address, abi: IEntryPoint.abi, functionName: "handleOps", args: handleOpsArgs }); const handleOpsHash = await walletClient.writeContract(request); await publicClient.waitForTransactionReceipt({ hash: handleOpsHash }); const balance = await publicClient.getBalance({ address: to }); expect(balance).toBe(value); }); }); });