UNPKG

@biconomy/abstractjs

Version:

SDK for Biconomy integration with support for account abstraction, smart accounts, ERC-4337.

92 lines 3.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toValidator = void 0; const viem_1 = require("viem"); const __1 = require("../index.js"); const ERC7739_DETECTION_HASH = "0x7739773977397739773977397739773977397739773977397739773977397739"; const ERC7739_MAGIC_PREFIX = "0x77390"; const toValidator = (parameters) => { const { deInitData = "0x", type = "validator", signer, walletClient, data = "0x", module, erc7739VersionSupported_, ...rest } = parameters; if (walletClient && !walletClient.account) { throw new Error("Account should be defined in the wallet client provided to the `toValidator`"); } let _erc7739VersionSupported = erc7739VersionSupported_; const erc7739VersionSupported = async () => { if (_erc7739VersionSupported === undefined) { if (walletClient) { _erc7739VersionSupported = await detectErc7739Version(walletClient, module); } else { _erc7739VersionSupported = 0; } } return _erc7739VersionSupported; }; const signMessage = async (message) => { if (signer) { return await signer.signMessage({ message }); } return await walletClient.signMessage({ account: walletClient.account, message }); }; const signUserOperationHash = async (hash) => { return await signMessage({ raw: hash }); }; const signTypedData = async (typedData) => { if (signer) { return await signer.signTypedData(typedData); } return await walletClient.signTypedData({ account: walletClient.account, ...typedData }); }; const signMessageErc7739 = (_message, _verifierDomain) => { throw new Error("Erc7739 PersonalSign flow is not supported by this module"); }; const signTypedDataErc7739 = (_typedData, _verifierDomain) => { throw new Error("Erc7739 TypedDataSign flow is not supported by this module"); }; return { deInitData, data, module, address: module, ...(signer ? { signer } : { walletClient: walletClient }), type, getStubSignature: async () => __1.DUMMY_SIGNATURE, signMessage, signUserOperationHash, signTypedData, erc7739VersionSupported, signMessageErc7739, signTypedDataErc7739, ...rest }; }; exports.toValidator = toValidator; const detectErc7739Version = async (walletClient, moduleAddress) => { try { const client = walletClient.extend(viem_1.publicActions); const result = await client.readContract({ address: moduleAddress, abi: (0, viem_1.parseAbi)([ "function isValidSignature(bytes32 hash, bytes calldata signature) external view returns (bytes4)" ]), functionName: "isValidSignature", args: [ERC7739_DETECTION_HASH, "0x"] }); if (typeof result === "string" && result.toLowerCase().startsWith(ERC7739_MAGIC_PREFIX)) { const versionHex = result.slice(-1); return Number.parseInt(versionHex, 16); } return 0; } catch { return 0; } }; //# sourceMappingURL=toValidator.js.map