UNPKG

@zerodev/sdk

Version:

A utility library for working with ERC-4337

166 lines 6.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSenderAddress = exports.InvalidEntryPointError = void 0; const viem_1 = require("viem"); const actions_1 = require("viem/actions"); const utils_1 = require("viem/utils"); class InvalidEntryPointError extends viem_1.BaseError { constructor({ cause, entryPointAddress } = {}) { super(`The entry point address (\`entryPoint\`${entryPointAddress ? ` = ${entryPointAddress}` : ""}) is not a valid entry point. getSenderAddress did not revert with a SenderAddressResult error.`, { cause }); Object.defineProperty(this, "name", { enumerable: true, configurable: true, writable: true, value: "InvalidEntryPointError" }); } } exports.InvalidEntryPointError = InvalidEntryPointError; const getSenderAddress = async (client, args) => { const { initCode, entryPointAddress, factory, factoryData } = args; if (!initCode && !factory && !factoryData) { throw new Error("Either `initCode` or `factory` and `factoryData` must be provided"); } try { await (0, utils_1.getAction)(client, actions_1.simulateContract, "simulateContract")({ address: entryPointAddress, abi: [ { inputs: [ { internalType: "address", name: "sender", type: "address" } ], name: "SenderAddressResult", type: "error" }, { inputs: [ { internalType: "bytes", name: "initCode", type: "bytes" } ], name: "getSenderAddress", outputs: [], stateMutability: "nonpayable", type: "function" } ], functionName: "getSenderAddress", args: [initCode || (0, viem_1.concat)([factory, factoryData])] }); } catch (e) { const revertError = e.walk((err) => err instanceof viem_1.ContractFunctionRevertedError || err instanceof viem_1.RpcRequestError || err instanceof viem_1.InvalidInputRpcError || err instanceof viem_1.UnknownRpcError); if (!revertError) { const cause = e.cause; const errorName = cause?.data?.errorName ?? ""; if (errorName === "SenderAddressResult" && cause?.data?.args && cause?.data?.args[0]) { return cause.data?.args[0]; } } if (revertError instanceof viem_1.ContractFunctionRevertedError) { const errorName = revertError.data?.errorName ?? ""; if (errorName === "SenderAddressResult" && revertError.data?.args && revertError.data?.args[0]) { return revertError.data?.args[0]; } } if (revertError instanceof viem_1.RpcRequestError) { const hexStringRegex = /0x[a-fA-F0-9]+/; const match = revertError.cause.data.match(hexStringRegex); if (!match) { throw new Error("Failed to parse revert bytes from RPC response"); } const data = match[0]; const error = (0, viem_1.decodeErrorResult)({ abi: [ { inputs: [ { internalType: "address", name: "sender", type: "address" } ], name: "SenderAddressResult", type: "error" } ], data }); return error.args[0]; } if (revertError instanceof viem_1.InvalidInputRpcError) { const { data: data_ } = (e instanceof viem_1.RawContractError ? e : e instanceof viem_1.BaseError ? e.walk((err) => "data" in err) || e.walk() : {}); const data = typeof data_ === "string" ? data_ : data_?.data; if (data === undefined) { throw new Error("Failed to parse revert bytes from RPC response"); } const error = (0, viem_1.decodeErrorResult)({ abi: [ { inputs: [ { internalType: "address", name: "sender", type: "address" } ], name: "SenderAddressResult", type: "error" } ], data }); return error.args[0]; } if (revertError instanceof viem_1.UnknownRpcError) { const parsedBody = JSON.parse(revertError.cause.body); const revertData = parsedBody.error.data; const hexStringRegex = /0x[a-fA-F0-9]+/; const match = revertData.match(hexStringRegex); if (!match) { throw new Error("Failed to parse revert bytes from RPC response"); } const data = match[0]; const error = (0, viem_1.decodeErrorResult)({ abi: [ { inputs: [ { internalType: "address", name: "sender", type: "address" } ], name: "SenderAddressResult", type: "error" } ], data }); return error.args[0]; } throw e; } throw new InvalidEntryPointError({ entryPointAddress }); }; exports.getSenderAddress = getSenderAddress; //# sourceMappingURL=getSenderAddress.js.map