UNPKG

@alchemy/aa-core

Version:

viem based SDK that enables interactions with ERC-4337 Smart Accounts. ABIs are based off the definitions generated in @account-abstraction/contracts

82 lines 2.92 kB
import { concat, encodeAbiParameters, hexToBigInt, isAddress, keccak256, pad, } from "viem"; import { EntryPointAbi_v7 } from "../abis/EntryPointAbi_v7.js"; const packUserOperation = (request) => { const initCode = request.factory && request.factoryData ? concat([request.factory, request.factoryData]) : "0x"; const accountGasLimits = packAccountGasLimits((({ verificationGasLimit, callGasLimit }) => ({ verificationGasLimit, callGasLimit, }))(request)); const gasFees = packAccountGasLimits((({ maxPriorityFeePerGas, maxFeePerGas }) => ({ maxPriorityFeePerGas, maxFeePerGas, }))(request)); const paymasterAndData = request.paymaster && isAddress(request.paymaster) ? packPaymasterData((({ paymaster, paymasterVerificationGasLimit, paymasterPostOpGasLimit, paymasterData, }) => ({ paymaster, paymasterVerificationGasLimit, paymasterPostOpGasLimit, paymasterData, }))(request)) : "0x"; return encodeAbiParameters([ { type: "address" }, { type: "uint256" }, { type: "bytes32" }, { type: "bytes32" }, { type: "bytes32" }, { type: "uint256" }, { type: "bytes32" }, { type: "bytes32" }, ], [ request.sender, hexToBigInt(request.nonce), keccak256(initCode), keccak256(request.callData), accountGasLimits, hexToBigInt(request.preVerificationGas), gasFees, keccak256(paymasterAndData), ]); }; export default { version: "0.7.0", address: { default: "0x0000000071727De22E5E9d8BAf0edAc6f37da032", }, abi: EntryPointAbi_v7, getUserOperationHash: (request, entryPointAddress, chainId) => { const encoded = encodeAbiParameters([{ type: "bytes32" }, { type: "address" }, { type: "uint256" }], [ keccak256(packUserOperation(request)), entryPointAddress, BigInt(chainId), ]); return keccak256(encoded); }, packUserOperation, }; export function packAccountGasLimits(data) { return concat(Object.values(data).map((v) => pad(v, { size: 16 }))); } export function packPaymasterData({ paymaster, paymasterVerificationGasLimit, paymasterPostOpGasLimit, paymasterData, }) { if (!paymaster || !paymasterVerificationGasLimit || !paymasterPostOpGasLimit || !paymasterData) { return "0x"; } return concat([ paymaster, pad(paymasterVerificationGasLimit, { size: 16 }), pad(paymasterPostOpGasLimit, { size: 16 }), paymasterData, ]); } export function unpackAccountGasLimits(accountGasLimits) { return { verificationGasLimit: parseInt(accountGasLimits.slice(2, 34), 16), callGasLimit: parseInt(accountGasLimits.slice(34), 16), }; } //# sourceMappingURL=0.7.js.map