"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } async function _asyncNullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return await rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); // src/customChains.ts var _viem = require('viem'); var muster = _viem.defineChain.call(void 0, { id: 4078, name: "Muster", network: "muster", nativeCurrency: { decimals: 18, name: "Ether", symbol: "ETH" }, rpcUrls: { default: { http: [ process.env.RPC_URL_MUSTER_TESTNET || "https://muster.alt.technology/" ] }, public: { http: [ process.env.RPC_URL_MUSTER_TESTNET || "https://muster.alt.technology/" ] } }, contracts: { multicall3: { address: "0xcA11bde05977b3631167028862bE2a173976CA11" } } }); // src/constants.ts var API_URL = "https://api.4337.cometh.io"; var ENTRYPOINT_ADDRESS_V07 = "0x0000000071727De22E5E9d8BAf0edAc6f37da032"; var SENTINEL_MODULES = "0x0000000000000000000000000000000000000001"; var customChains = [muster]; // src/core/accounts/safe/abi/safe4337SessionKeyModuleAbi.ts var safe4337SessionKeyModuleAbi = [ { inputs: [ { internalType: "address", name: "entryPoint", type: "address" } ], stateMutability: "nonpayable", type: "constructor" }, { inputs: [], name: "ExecutionFailed", type: "error" }, { inputs: [], name: "InvalidCaller", type: "error" }, { inputs: [], name: "InvalidEntryPoint", type: "error" }, { inputs: [ { internalType: "address", name: "sessionKey", type: "address" } ], name: "InvalidSessionInterval", type: "error" }, { inputs: [ { internalType: "address", name: "sessionKey", type: "address" } ], name: "InvalidSessionKey", type: "error" }, { inputs: [], name: "InvalidSessionKeyCaller", type: "error" }, { inputs: [ { internalType: "address", name: "sessionKey", type: "address" } ], name: "InvalidSignature", type: "error" }, { inputs: [ { internalType: "address", name: "sessionKey", type: "address" } ], name: "RevokedSession", type: "error" }, { inputs: [], name: "SessionKeyExecutionFailed", type: "error" }, { inputs: [], name: "UnsupportedEntryPoint", type: "error" }, { inputs: [ { internalType: "bytes4", name: "selector", type: "bytes4" } ], name: "UnsupportedExecutionFunction", type: "error" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "sessionKey", type: "address" }, { indexed: true, internalType: "address", name: "account", type: "address" } ], name: "SessionKeyAdded", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "sessionKey", type: "address" }, { indexed: true, internalType: "address", name: "account", type: "address" } ], name: "SessionKeyRevoked", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "sessionKey", type: "address" }, { indexed: true, internalType: "address", name: "destination", type: "address" } ], name: "WhitelistedDestinationAdded", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "sessionKey", type: "address" }, { indexed: true, internalType: "address", name: "destination", type: "address" } ], name: "WhitelistedDestinationRemoved", type: "event" }, { inputs: [], name: "SUPPORTED_ENTRYPOINT", outputs: [ { internalType: "address", name: "", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "sessionKey", type: "address" }, { internalType: "uint48", name: "validAfter", type: "uint48" }, { internalType: "uint48", name: "validUntil", type: "uint48" }, { internalType: "address[]", name: "destinations", type: "address[]" } ], name: "addSessionKey", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "sessionKey", type: "address" }, { internalType: "address", name: "destination", type: "address" } ], name: "addWhitelistDestination", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [], name: "domainSeparator", outputs: [ { internalType: "bytes32", name: "domainSeparatorHash", type: "bytes32" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "contract Safe", name: "safe", type: "address" }, { internalType: "bytes", name: "message", type: "bytes" } ], name: "encodeMessageDataForSafe", outputs: [ { internalType: "bytes", name: "", type: "bytes" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "to", type: "address" }, { internalType: "uint256", name: "value", type: "uint256" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "uint8", name: "operation", type: "uint8" } ], name: "executeUserOp", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "to", type: "address" }, { internalType: "uint256", name: "value", type: "uint256" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "uint8", name: "operation", type: "uint8" } ], name: "executeUserOpWithErrorString", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { components: [ { internalType: "address", name: "target", type: "address" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "bool", name: "allowAllDestinations", type: "bool" } ], internalType: "struct SessionKeys4337.Call", name: "call", type: "tuple" }, { internalType: "address", name: "sessionKey", type: "address" } ], name: "executeWithSessionKey", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "bytes", name: "message", type: "bytes" } ], name: "getMessageHash", outputs: [ { internalType: "bytes32", name: "", type: "bytes32" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "contract Safe", name: "safe", type: "address" }, { internalType: "bytes", name: "message", type: "bytes" } ], name: "getMessageHashForSafe", outputs: [ { internalType: "bytes32", name: "", type: "bytes32" } ], stateMutability: "view", type: "function" }, { inputs: [], name: "getModules", outputs: [ { internalType: "address[]", name: "", type: "address[]" } ], stateMutability: "view", type: "function" }, { inputs: [ { components: [ { internalType: "address", name: "sender", type: "address" }, { internalType: "uint256", name: "nonce", type: "uint256" }, { internalType: "bytes", name: "initCode", type: "bytes" }, { internalType: "bytes", name: "callData", type: "bytes" }, { internalType: "bytes32", name: "accountGasLimits", type: "bytes32" }, { internalType: "uint256", name: "preVerificationGas", type: "uint256" }, { internalType: "bytes32", name: "gasFees", type: "bytes32" }, { internalType: "bytes", name: "paymasterAndData", type: "bytes" }, { internalType: "bytes", name: "signature", type: "bytes" } ], internalType: "struct PackedUserOperation", name: "userOp", type: "tuple" } ], name: "getOperationHash", outputs: [ { internalType: "bytes32", name: "operationHash", type: "bytes32" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "bytes32", name: "_dataHash", type: "bytes32" }, { internalType: "bytes", name: "_signature", type: "bytes" } ], name: "isValidSignature", outputs: [ { internalType: "bytes4", name: "", type: "bytes4" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "bytes", name: "_data", type: "bytes" }, { internalType: "bytes", name: "_signature", type: "bytes" } ], name: "isValidSignature", outputs: [ { internalType: "bytes4", name: "", type: "bytes4" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "", type: "address" }, { internalType: "address", name: "", type: "address" }, { internalType: "uint256[]", name: "", type: "uint256[]" }, { internalType: "uint256[]", name: "", type: "uint256[]" }, { internalType: "bytes", name: "", type: "bytes" } ], name: "onERC1155BatchReceived", outputs: [ { internalType: "bytes4", name: "", type: "bytes4" } ], stateMutability: "pure", type: "function" }, { inputs: [ { internalType: "address", name: "", type: "address" }, { internalType: "address", name: "", type: "address" }, { internalType: "uint256", name: "", type: "uint256" }, { internalType: "uint256", name: "", type: "uint256" }, { internalType: "bytes", name: "", type: "bytes" } ], name: "onERC1155Received", outputs: [ { internalType: "bytes4", name: "", type: "bytes4" } ], stateMutability: "pure", type: "function" }, { inputs: [ { internalType: "address", name: "", type: "address" }, { internalType: "address", name: "", type: "address" }, { internalType: "uint256", name: "", type: "uint256" }, { internalType: "bytes", name: "", type: "bytes" } ], name: "onERC721Received", outputs: [ { internalType: "bytes4", name: "", type: "bytes4" } ], stateMutability: "pure", type: "function" }, { inputs: [ { internalType: "address", name: "sessionKey", type: "address" }, { internalType: "address", name: "destination", type: "address" } ], name: "removeWhitelistDestination", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "sessionKey", type: "address" } ], name: "revokeSession", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "", type: "address" } ], name: "sessionKeys", outputs: [ { internalType: "address", name: "account", type: "address" }, { internalType: "uint48", name: "validAfter", type: "uint48" }, { internalType: "uint48", name: "validUntil", type: "uint48" }, { internalType: "bool", name: "revoked", type: "bool" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "targetContract", type: "address" }, { internalType: "bytes", name: "calldataPayload", type: "bytes" } ], name: "simulate", outputs: [ { internalType: "bytes", name: "response", type: "bytes" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "bytes4", name: "interfaceId", type: "bytes4" } ], name: "supportsInterface", outputs: [ { internalType: "bool", name: "", type: "bool" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "", type: "address" }, { internalType: "address", name: "", type: "address" }, { internalType: "address", name: "", type: "address" }, { internalType: "uint256", name: "", type: "uint256" }, { internalType: "bytes", name: "", type: "bytes" }, { internalType: "bytes", name: "", type: "bytes" } ], name: "tokensReceived", outputs: [], stateMutability: "pure", type: "function" }, { inputs: [ { components: [ { internalType: "address", name: "sender", type: "address" }, { internalType: "uint256", name: "nonce", type: "uint256" }, { internalType: "bytes", name: "initCode", type: "bytes" }, { internalType: "bytes", name: "callData", type: "bytes" }, { internalType: "bytes32", name: "accountGasLimits", type: "bytes32" }, { internalType: "uint256", name: "preVerificationGas", type: "uint256" }, { internalType: "bytes32", name: "gasFees", type: "bytes32" }, { internalType: "bytes", name: "paymasterAndData", type: "bytes" }, { internalType: "bytes", name: "signature", type: "bytes" } ], internalType: "struct PackedUserOperation", name: "userOp", type: "tuple" }, { internalType: "bytes32", name: "userOpHash", type: "bytes32" }, { internalType: "uint256", name: "missingAccountFunds", type: "uint256" } ], name: "validateUserOp", outputs: [ { internalType: "uint256", name: "validationData", type: "uint256" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "", type: "address" }, { internalType: "address", name: "", type: "address" } ], name: "whitelistDestinations", outputs: [ { internalType: "bool", name: "", type: "bool" } ], stateMutability: "view", type: "function" } ]; // src/core/services/utils.ts var decodeUTF8 = (b) => { return new TextDecoder().decode(b); }; var encodeUTF8 = (s) => { return new TextEncoder().encode(s); }; var arrayBufferToBase64 = (buffer) => { let binary = ""; const bytes = new Uint8Array(buffer); const len = bytes.byteLength; for (let i = 0; i < len; i++) { binary += String.fromCharCode(bytes[i]); } return window.btoa(binary); }; var uint8ArrayToBase64 = (bytes) => { let binary = ""; const len = bytes.byteLength; for (let i = 0; i < len; i++) { binary += String.fromCharCode(bytes[i]); } return window.btoa(binary); }; var base64toUint8Array = (base64) => { const binary_string = window.atob(base64); const len = binary_string.length; const bytes = new Uint8Array(len); for (let i = 0; i < len; i++) { bytes[i] = binary_string.charCodeAt(i); } return bytes; }; var base64ToArrayBuffer = (base64) => { const binary_string = window.atob(base64); const len = binary_string.length; const bytes = new Uint8Array(len); for (let i = 0; i < len; i++) { bytes[i] = binary_string.charCodeAt(i); } return bytes.buffer; }; // src/core/signers/ecdsa/services/ecdsaService.ts var _accounts = require('viem/accounts'); // src/core/signers/ecdsa/services/cryptoService.ts var pbkdf2 = async (password, salt, iterations) => { const key = await window.crypto.subtle.importKey( "raw", password, { name: "PBKDF2" }, false, ["deriveBits"] ); return await window.crypto.subtle.deriveBits( { name: "PBKDF2", salt, iterations, hash: { name: "sha-256" } }, key, 256 ); }; var encryptAESCBC = async (privateKey, iv, data) => { const key = await window.crypto.subtle.importKey( "raw", new Uint8Array(privateKey), { name: "AES-CBC" }, false, ["encrypt"] ); return await window.crypto.subtle.encrypt( { name: "AES-CBC", iv }, key, data ); }; var decryptAESCBC = async (privateKey, iv, data) => { const key = await window.crypto.subtle.importKey( "raw", privateKey, { name: "AES-CBC" }, false, ["decrypt"] ); return await window.crypto.subtle.decrypt( { name: "AES-CBC", iv }, key, data ); }; var getRandomValues = (arr) => { return window.crypto.getRandomValues(arr); }; // src/core/signers/ecdsa/services/randomIvService.ts var getRandomIV = () => { const array = new Uint8Array(16); getRandomValues(array); return array; }; // src/core/signers/ecdsa/services/ecdsaService.ts var defaultEncryptionSalt = "COMETH-CONNECT"; var Pbkdf2Iterations = 1e6; var encryptSigner = async (smartAccountAddress, privateKey, salt) => { const { encryptedPrivateKey, iv } = await encryptEoaFallback( smartAccountAddress, privateKey, salt || defaultEncryptionSalt ); const signer = _accounts.privateKeyToAccount.call(void 0, privateKey); const storageValue = formatStorageValue( encryptedPrivateKey, iv, signer.address ); return storageValue; }; var encryptSignerInStorage = async (smartAccountAddress, privateKey, salt) => { const storageValue = await encryptSigner( smartAccountAddress, privateKey, salt ); window.localStorage.setItem( `cometh-connect-fallback-${smartAccountAddress}`, storageValue ); }; var getSignerLocalStorage = async (smartAccountAddress, salt) => { const localStorage2 = window.localStorage.getItem( `cometh-connect-fallback-${smartAccountAddress}` ); if (localStorage2) { const { encryptedPrivateKey, iv } = unFormatStorageValue(localStorage2); const privateKey = await decryptEoaFallback( smartAccountAddress, base64ToArrayBuffer(encryptedPrivateKey), base64toUint8Array(iv), salt || defaultEncryptionSalt ); return privateKey; } return null; }; var encryptEoaFallback = async (smartAccountAddress, privateKey, salt) => { const encodedSmartAccountAddress = encodeUTF8(smartAccountAddress); const encodedSalt = encodeUTF8(salt); const encryptionKey = await pbkdf2( encodedSmartAccountAddress, encodedSalt, Pbkdf2Iterations ); const encodedPrivateKey = encodeUTF8(privateKey); const iv = getRandomIV(); const encryptedPrivateKey = await encryptAESCBC( encryptionKey, iv, encodedPrivateKey ); return { encryptedPrivateKey: arrayBufferToBase64(encryptedPrivateKey), iv: uint8ArrayToBase64(iv) }; }; var decryptEoaFallback = async (smartAccountAddress, encryptedPrivateKey, iv, salt) => { const encodedsmartAccountAddress = encodeUTF8(smartAccountAddress); const encodedSalt = encodeUTF8(salt); const encryptionKey = await pbkdf2( encodedsmartAccountAddress, encodedSalt, Pbkdf2Iterations ); const privateKey = await decryptAESCBC( encryptionKey, iv, encryptedPrivateKey ); return decodeUTF8(privateKey); }; var formatStorageValue = (encryptedPrivateKey, iv, signerAddress) => { return JSON.stringify({ encryptedPrivateKey, iv, signerAddress }); }; var unFormatStorageValue = (storageValue) => { return JSON.parse(storageValue); }; // src/core/signers/ecdsa/sessionKeyEoa/sessionKeyEoaService.ts var encryptSessionKeyInStorage = async (smartAccountAddress, privateKey, salt) => { const storageValue = await encryptSigner( smartAccountAddress, privateKey, salt ); window.localStorage.setItem( `cometh-connect-sessionKey-${smartAccountAddress}`, storageValue ); }; var getSessionKeyPKFromLocalStorage = async (smartAccountAddress, salt) => { const localStorage2 = window.localStorage.getItem( `cometh-connect-sessionKey-${smartAccountAddress}` ); if (localStorage2) { const { encryptedPrivateKey, iv } = unFormatStorageValue(localStorage2); return await decryptEoaFallback( smartAccountAddress, base64ToArrayBuffer(encryptedPrivateKey), base64toUint8Array(iv), salt || defaultEncryptionSalt ); } return null; }; var getSessionKeySignerFromLocalStorage = async (smartAccountAddress) => { const localStorage2 = window.localStorage.getItem( `cometh-connect-sessionKey-${smartAccountAddress}` ); if (localStorage2) { const { signerAddress } = unFormatStorageValue(localStorage2); return signerAddress; } return null; }; var deleteSessionKeyInStorage = (smartAccountAddress) => { window.localStorage.removeItem( `cometh-connect-sessionKey-${smartAccountAddress}` ); }; // src/core/actions/accounts/safe/sessionKeys/utils.ts var _permissionless = require('permissionless'); // src/core/signers/ecdsa/fallbackEoa/fallbackEoaSigner.ts var getFallbackEoaSigner = async ({ smartAccountAddress, encryptionSalt }) => { const privateKey = await getSignerLocalStorage( smartAccountAddress, encryptionSalt ); if (!privateKey) throw new Error("no account found"); return { privateKey, signer: _accounts.privateKeyToAccount.call(void 0, privateKey) }; }; var createFallbackEoaSigner = async () => { const privateKey = _accounts.generatePrivateKey.call(void 0, ); const signer = _accounts.privateKeyToAccount.call(void 0, privateKey); return { signer, privateKey }; }; // src/core/signers/passkeys/passkeyService.ts var _helpers = require('@simplewebauthn/server/helpers'); var _cborjs = require('cbor-js'); var _cborjs2 = _interopRequireDefault(_cborjs); var _elliptic = require('elliptic'); var _elliptic2 = _interopRequireDefault(_elliptic); var _psl = require('psl'); var psl = _interopRequireWildcard(_psl); // src/errors.ts var NoFallbackSignerError = class extends Error { constructor() { super("No fallback signer found"); } }; var NoPasskeySignerFoundInDBError = class extends Error { constructor() { super("No passkey signer found in db for this walletAddress"); } }; var NoPasskeySignerFoundForGivenChain = class extends Error { constructor() { super("No passkey signer found in db for this walletAddress and chain"); } }; var NoPasskeySignerFoundInDeviceError = class extends Error { constructor() { super( "No signer was found on your device. You might need to add that domain as signer" ); } }; var SignerNotOwnerError = class extends Error { constructor() { super("Signer found is not owner of the wallet"); } }; var RetrieveWalletFromPasskeyError = class extends Error { constructor() { super("Unable to retrieve wallet address from passkeys"); } }; var NoRecoveryRequestFoundError = class extends Error { constructor() { super("No recovery request found"); } }; var RelayedTransactionError = class extends Error { constructor() { super("Error during the relay of the transaction"); } }; // src/core/signers/passkeys/utils.ts var DEFAULT_WEBAUTHN_OPTIONS = { // authenticatorSelection documentation can be found here: https://www.w3.org/TR/webauthn-2/#dictdef-authenticatorselectioncriteria authenticatorSelection: { authenticatorAttachment: "platform", residentKey: "preferred", userVerification: "preferred" } }; var hexArrayStr = (array) => new Uint8Array(array).reduce( (acc, v) => acc + v.toString(16).padStart(2, "0"), "0x" ); var parseHex = (str) => { const matches = str.match(/[\da-f]{2}/gi); if (matches === null) { return new Uint8Array(); } return new Uint8Array(matches.map((h) => parseInt(h, 16))); }; var isWebAuthnCompatible = async (webAuthnOptions) => { try { if (typeof window === "undefined" || !window.PublicKeyCredential) return false; if (_optionalChain([webAuthnOptions, 'access', _2 => _2.authenticatorSelection, 'optionalAccess', _3 => _3.authenticatorAttachment]) === "platform") { const isUserVerifyingPlatformAuthenticatorAvailable = await PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable(); if (!isUserVerifyingPlatformAuthenticatorAvailable) return false; } return true; } catch (e2) { return false; } }; function extractClientDataFields(response) { const clientDataJSON = new TextDecoder("utf-8").decode( response.clientDataJSON ); const match = clientDataJSON.match( /^\{"type":"webauthn.get","challenge":"[A-Za-z0-9\-_]{43}",(.*)\}$/ ); if (!match) { throw new Error("challenge not found in client data JSON"); } const [, fields] = match; return _viem.toHex.call(void 0, _viem.toBytes.call(void 0, fields)); } function extractSignature(response) { const check = (x) => { if (!x) { throw new Error("invalid signature encoding"); } }; const view = new DataView(response.signature); check(view.getUint8(0) === 48); check(view.getUint8(1) === view.byteLength - 2); const readInt = (offset) => { check(view.getUint8(offset) === 2); const len = view.getUint8(offset + 1); const start = offset + 2; const end = start + len; const n = BigInt(_viem.toHex.call(void 0, new Uint8Array(view.buffer.slice(start, end)))); check(n < _viem.maxUint256); return [n, end]; }; const [r, sOffset] = readInt(2); const [s] = readInt(sOffset); return [r, s]; } // src/core/signers/passkeys/passkeyService.ts var EC = _elliptic2.default.ec; var _formatCreatingRpId = (fullDomainSelected) => { const rootDomain = psl.default.parse(window.location.host).domain; if (!rootDomain) return { name: "localhost" }; return fullDomainSelected ? { name: window.location.host, id: window.location.host } : { name: rootDomain, id: rootDomain }; }; var _formatSigningRpId = (fullDomainSelected) => { const rootDomain = psl.default.parse(window.location.host).domain; if (!rootDomain) return void 0; return fullDomainSelected ? window.location.host : rootDomain; }; var createPasskeySigner = async ({ api, webAuthnOptions, passKeyName, fullDomainSelected, safeWebAuthnSharedSignerAddress }) => { try { const name = passKeyName || "Cometh Connect"; const authenticatorSelection = _optionalChain([webAuthnOptions, 'optionalAccess', _4 => _4.authenticatorSelection]); const extensions = _optionalChain([webAuthnOptions, 'optionalAccess', _5 => _5.extensions]); const passkeyCredential = await navigator.credentials.create({ publicKey: { rp: _formatCreatingRpId(fullDomainSelected), user: { id: crypto.getRandomValues(new Uint8Array(32)), name, displayName: name }, attestation: "none", authenticatorSelection, timeout: 6e4, challenge: crypto.getRandomValues(new Uint8Array(32)), pubKeyCredParams: [ { alg: -7, type: "public-key" }, { alg: -257, type: "public-key" } ], extensions } }); if (!passkeyCredential) { throw new Error( "Failed to generate passkey. Received null as a credential" ); } const publicKeyAlgorithm = passkeyCredential.response.getPublicKeyAlgorithm(); const attestationObject = _optionalChain([passkeyCredential, 'optionalAccess', _6 => _6.response, 'optionalAccess', _7 => _7.attestationObject]); let attestation; if (attestationObject instanceof ArrayBuffer) { attestation = _cborjs2.default.decode(attestationObject); } else { attestation = _cborjs2.default.decode(attestationObject.buffer); } const authData = _helpers.parseAuthenticatorData.call(void 0, attestation.authData); const credentialPublicKeyBuffer = _optionalChain([authData, 'optionalAccess', _8 => _8.credentialPublicKey, 'optionalAccess', _9 => _9.buffer]); const publicKey = _cborjs2.default.decode(credentialPublicKeyBuffer); const x = publicKey[-2]; const y = publicKey[-3]; const curve = new EC("p256"); const point = curve.curve.point(x, y); const publicKeyX = `0x${point.getX().toString(16)}`; const publicKeyY = `0x${point.getY().toString(16)}`; const publicKeyId = hexArrayStr(passkeyCredential.rawId); const signerAddress = await _asyncNullishCoalesce(safeWebAuthnSharedSignerAddress, async () => ( await api.predictWebAuthnSignerAddress({ publicKeyX, publicKeyY }))); const passkeyWithCoordinates = { id: publicKeyId, pubkeyCoordinates: { x: publicKeyX, y: publicKeyY }, publicKeyAlgorithm, signerAddress }; return passkeyWithCoordinates; } catch (e) { console.log({ e }); throw new Error("Error in the passkey creation"); } }; var sign = async ({ challenge, fullDomainSelected, publicKeyCredential }) => { const assertion = await navigator.credentials.get({ publicKey: { challenge: _viem.toBytes.call(void 0, challenge), rpId: _formatSigningRpId(fullDomainSelected), allowCredentials: publicKeyCredential || [], userVerification: "required", timeout: 6e4 } }); if (!assertion) throw new Error("Passkey signature failed"); const signature = _viem.encodeAbiParameters.call(void 0, [ { type: "bytes", name: "authenticatorData" }, { type: "bytes", name: "clientDataFields" }, { type: "uint256[2]", name: "signature" } ], [ _viem.toHex.call(void 0, new Uint8Array(assertion.response.authenticatorData)), extractClientDataFields(assertion.response), extractSignature(assertion.response) ] ); const publicKeyId = hexArrayStr(assertion.rawId); return { signature, publicKeyId }; }; var signWithPasskey = async ({ challenge, webAuthnSigners, fullDomainSelected }) => { let publicKeyCredentials; if (webAuthnSigners) { publicKeyCredentials = webAuthnSigners.map((webAuthnSigner) => { return { id: parseHex(webAuthnSigner.publicKeyId), type: "public-key" }; }); } const webAuthnSignature = await sign({ challenge: _viem.keccak256.call(void 0, _viem.hashMessage.call(void 0, challenge)), publicKeyCredential: publicKeyCredentials, fullDomainSelected }); return webAuthnSignature; }; var setPasskeyInStorage = (smartAccountAddress, publicKeyId, publicKeyX, publicKeyY, signerAddress) => { const storageKey = `cometh-connect-${smartAccountAddress}`; const passkeyWithCoordinates = { id: publicKeyId, pubkeyCoordinates: { x: publicKeyX, y: publicKeyY }, signerAddress }; window.localStorage.setItem( storageKey, JSON.stringify(passkeyWithCoordinates) ); }; var getPasskeyInStorage = (smartAccountAddress) => { const storageKey = `cometh-connect-${smartAccountAddress}`; const storedData = window.localStorage.getItem(storageKey); if (!storedData) return null; return JSON.parse(storedData); }; var getPasskeySigner = async ({ api, smartAccountAddress, chain, rpcUrl, safeProxyFactoryAddress, safeSingletonAddress, safeModuleSetUpAddress, safeWebAuthnSharedSignerAddress, fallbackHandler, p256Verifier, multisendAddress: multisendAddress2, fullDomainSelected }) => { const localStoragePasskey = getPasskeyInStorage(smartAccountAddress); if (localStoragePasskey) { const passkey = localStoragePasskey; const signer = { type: "passkey", passkey: { id: passkey.id, pubkeyCoordinates: { x: passkey.pubkeyCoordinates.x, y: passkey.pubkeyCoordinates.y }, signerAddress: passkey.signerAddress } }; const isOwner = await isSafeOwner({ safeAddress: smartAccountAddress, accountSigner: signer, chain, rpcUrl, safeProxyFactoryAddress, safeSingletonAddress, safeModuleSetUpAddress, sharedWebAuthnSignerContractAddress: safeWebAuthnSharedSignerAddress, modules: [fallbackHandler], fallbackHandler, p256Verifier, multisendAddress: multisendAddress2 }); if (!isOwner) throw new SignerNotOwnerError(); return passkey; } const dbPasskeySigners = await api.getPasskeySignersByWalletAddress(smartAccountAddress); if (dbPasskeySigners.length === 0) throw new NoPasskeySignerFoundInDBError(); let webAuthnSignature; try { webAuthnSignature = await signWithPasskey({ challenge: "SDK Connection", webAuthnSigners: dbPasskeySigners, fullDomainSelected }); } catch (e3) { throw new NoPasskeySignerFoundInDeviceError(); } const signingWebAuthnSigners = await api.getPasskeySignerByPublicKeyId( webAuthnSignature.publicKeyId ); const webAuthnSignerForGivenChain = signingWebAuthnSigners.find( (signer) => +signer.chainId === chain.id ); if (!webAuthnSignerForGivenChain) throw new NoPasskeySignerFoundForGivenChain(); const passkeyWithCoordinates = { id: webAuthnSignerForGivenChain.publicKeyId, pubkeyCoordinates: { x: webAuthnSignerForGivenChain.publicKeyX, y: webAuthnSignerForGivenChain.publicKeyY }, signerAddress: webAuthnSignerForGivenChain.signerAddress }; setPasskeyInStorage( smartAccountAddress, passkeyWithCoordinates.id, passkeyWithCoordinates.pubkeyCoordinates.x, passkeyWithCoordinates.pubkeyCoordinates.y, passkeyWithCoordinates.signerAddress ); return passkeyWithCoordinates; }; var retrieveSmartAccountAddressFromPasskey = async (API2, chain, fullDomainSelected) => { let publicKeyId; try { publicKeyId = (await signWithPasskey({ challenge: "Retrieve user wallet", fullDomainSelected })).publicKeyId; } catch (e4) { throw new RetrieveWalletFromPasskeyError(); } const signingPasskeySigners = await API2.getPasskeySignerByPublicKeyId(publicKeyId); if (signingPasskeySigners.length === 0) throw new NoPasskeySignerFoundInDBError(); const webAuthnSignerForGivenChain = signingPasskeySigners.find( (signer) => +signer.chainId === chain.id ); if (!webAuthnSignerForGivenChain) throw new NoPasskeySignerFoundForGivenChain(); const { smartAccountAddress, publicKeyX, publicKeyY, signerAddress } = webAuthnSignerForGivenChain; setPasskeyInStorage( smartAccountAddress, publicKeyId, publicKeyX, publicKeyY, signerAddress ); return smartAccountAddress; }; var retrieveSmartAccountAddressFromPasskeyId = async ({ API: API2, id, chain, fullDomainSelected }) => { const publicKeyCredentials = [ { id: parseHex(id), type: "public-key" } ]; let publicKeyId; try { publicKeyId = (await sign({ challenge: _viem.keccak256.call(void 0, _viem.hashMessage.call(void 0, "Retrieve user wallet")), publicKeyCredential: publicKeyCredentials, fullDomainSelected })).publicKeyId; } catch (e5) { throw new RetrieveWalletFromPasskeyError(); } const signingPasskeySigners = await API2.getPasskeySignerByPublicKeyId(publicKeyId); if (signingPasskeySigners.length === 0) throw new NoPasskeySignerFoundInDBError(); const webAuthnSignerForGivenChain = signingPasskeySigners.find( (signer) => +signer.chainId === chain.id ); if (!webAuthnSignerForGivenChain) throw new NoPasskeySignerFoundForGivenChain(); const { smartAccountAddress, publicKeyX, publicKeyY, signerAddress } = webAuthnSignerForGivenChain; setPasskeyInStorage( smartAccountAddress, publicKeyId, publicKeyX, publicKeyY, signerAddress ); return smartAccountAddress; }; // src/core/services/API.ts var _axios = require('axios'); var _axios2 = _interopRequireDefault(_axios); var API = class { constructor(apiKey, baseUrl) { __publicField(this, "api"); __publicField(this, "_apiKey"); this._apiKey = apiKey; this.api = _axios2.default.create({ baseURL: baseUrl || API_URL }); this.api.defaults.headers.common.apiKey = apiKey; } get apiUrl() { return this.api.defaults.baseURL || ""; } get apiKey() { return this._apiKey; } async getProjectParams(chainId) { const response = await this.api.get( `/project/params?chainId=${chainId}` ); return response.data.projectParams; } async getWalletByNetworks(walletAddress) { const response = await this.api.get(`/wallet/${walletAddress}`); return response.data.wallets; } async createWallet({ chainId, smartAccountAddress, initiatorAddress }) { const body = { chainId: chainId.toString(), walletAddress: smartAccountAddress, initiatorAddress }; await this.api.post("/wallet", body); } async initWallet({ chainId, smartAccountAddress, initiatorAddress, publicKeyId, publicKeyX, publicKeyY, deviceData }) { const body = { chainId: chainId.toString(), walletAddress: smartAccountAddress, initiatorAddress, publicKeyId, publicKeyX, publicKeyY, deviceData }; const res = await this.api.post("/wallet/init", body); return res.data.isNewWallet; } async importExternalSafe({ smartAccountAddress, publicKeyId, publicKeyX, publicKeyY, deviceData, signerAddress, chainId }) { const body = { walletAddress: smartAccountAddress, signerAddress, publicKeyId, publicKeyX, publicKeyY, deviceData, chainId }; await this.api.post("/wallet/import", body); } /** * WebAuthn Section */ async createWebAuthnSigner({ chainId, walletAddress, publicKeyId, publicKeyX, publicKeyY, deviceData, signerAddress, isSharedWebAuthnSigner }) { const body = { chainId: chainId.toString(), walletAddress, publicKeyId, publicKeyX, publicKeyY, deviceData, signerAddress, isSharedWebAuthnSigner }; await this.api.post("/webauthn-signer/create", body); } async getPasskeySignerByPublicKeyId(publicKeyId) { const response = await this.api.get( `/webauthn-signer/public-key-id/${publicKeyId}` ); return response.data.webAuthnSigners; } async getPasskeySignersByWalletAddress(walletAddress) { const response = await this.api.get( `/webauthn-signer/${walletAddress}` ); return response.data.webAuthnSigners; } async predictWebAuthnSignerAddress({ publicKeyX, publicKeyY }) { const body = { publicKeyX, publicKeyY }; const response = await this.api.post( "/webauthn-signer/predict-address", body ); return response.data.signerAddress; } async getWebAuthnSignersByWalletAddressAndChain(walletAddress, chainId) { const response = await this.api.get( `/webauthn-signer/${walletAddress}/${chainId}` ); return _optionalChain([response, 'optionalAccess', _10 => _10.data, 'optionalAccess', _11 => _11.webAuthnSigners]); } async isValidSignature(walletAddress, message, signature, chainId) { const body = { chainId: chainId.toString(), message, signature }; const response = await this.api.post( `/wallet/is-valid-signature/${walletAddress}`, body ); return _optionalChain([response, 'optionalAccess', _12 => _12.data, 'optionalAccess', _13 => _13.result]); } }; // src/core/signers/createSigner.ts var isComethSigner = (signer) => { return "type" in signer && (signer.type === "localWallet" || signer.type === "passkey"); }; var getSignerAddress = (customSigner) => { if (isComethSigner(customSigner)) { return customSigner.type === "localWallet" ? customSigner.eoaFallback.signer.address : customSigner.passkey.signerAddress; } return customSigner.address; }; var getSigner = (customSigner) => { if (isComethSigner(customSigner)) { if (customSigner.type === "passkey") throw Error("passkey signer not valid"); return customSigner.eoaFallback.signer; } return customSigner; }; var saveSigner = async (signer, smartAccountAddress) => { if (isComethSigner(signer)) { if (signer.type === "localWallet") { const storedEncryptedPK = await getSignerLocalStorage( smartAccountAddress, signer.eoaFallback.encryptionSalt ); if (!storedEncryptedPK) await encryptSignerInStorage( smartAccountAddress, signer.eoaFallback.privateKey, signer.eoaFallback.encryptionSalt ); } else { setPasskeyInStorage( smartAccountAddress, signer.passkey.id, signer.passkey.pubkeyCoordinates.x, signer.passkey.pubkeyCoordinates.y, signer.passkey.signerAddress ); } } }; var throwErrorWhenEoaFallbackDisabled = (disableEoaFallback) => { if (disableEoaFallback) throw new Error("Passkeys are not compatible with your device"); }; var isFallbackSigner = () => { const fallbackSigner = Object.keys(localStorage).find( (key) => key.startsWith("cometh-connect-fallback-") ); return !!fallbackSigner; }; var isDeviceCompatibleWithPasskeys = async (options) => { const webAuthnCompatible = await isWebAuthnCompatible( options.webAuthnOptions ); return webAuthnCompatible && !isFallbackSigner(); }; async function createSigner({ apiKey, chain, rpcUrl, smartAccountAddress, baseUrl, disableEoaFallback = false, encryptionSalt, webAuthnOptions = DEFAULT_WEBAUTHN_OPTIONS, passKeyName, fullDomainSelected = false, safeContractParams }) { const api = new API(apiKey, baseUrl); const passkeyCompatible = await isDeviceCompatibleWithPasskeys({ webAuthnOptions }); if (passkeyCompatible) { let passkey; if (!smartAccountAddress) { passkey = await createPasskeySigner({ api, webAuthnOptions, passKeyName, fullDomainSelected, safeWebAuthnSharedSignerAddress: safeContractParams.safeWebAuthnSharedSignerContractAddress }); if (passkey.publicKeyAlgorithm !== -7) { console.warn("ECC passkey are not supported by your device"); throwErrorWhenEoaFallbackDisabled(disableEoaFallback); return { type: "localWallet", eoaFallback: await createFallbackEoaSigner() }; } } else { passkey = await getPasskeySigner({ api, smartAccountAddress, chain, rpcUrl, safeModuleSetUpAddress: safeContractParams.setUpContractAddress, safeProxyFactoryAddress: safeContractParams.safeProxyFactoryAddress, safeSingletonAddress: safeContractParams.safeSingletonAddress, fallbackHandler: safeContractParams.fallbackHandler, p256Verifier: safeContractParams.p256Verifier, multisendAddress: safeContractParams.multisendAddress, safeWebAuthnSharedSignerAddress: safeContractParams.safeWebAuthnSharedSignerContractAddress, fullDomainSelected }); } return { type: "passkey", passkey }; } console.warn("ECC passkey are not supported by your device"); throwErrorWhenEoaFallbackDisabled(disableEoaFallback); let privateKey; let signer; if (!smartAccountAddress) { ({ privateKey, signer } = await createFallbackEoaSigner()); } else { ({ privateKey, signer } = await getFallbackEoaSigner({ smartAccountAddress, encryptionSalt })); } return { type: "localWallet", eoaFallback: { privateKey, signer, encryptionSalt } }; } // src/core/accounts/safe/services/safe.ts // src/core/accounts/safe/abi/Multisend.ts var MultiSendContractABI = [ { inputs: [], stateMutability: "nonpayable", type: "constructor" }, { inputs: [ { internalType: "bytes", name: "transactions", type: "bytes" } ], name: "multiSend", outputs: [], stateMutability: "payable", type: "function" } ]; // src/core/accounts/safe/abi/enableModule.ts var EnableModuleAbi = [ { inputs: [ { internalType: "address[]", name: "modules", type: "address[]" } ], name: "enableModules", outputs: [], stateMutability: "nonpayable", type: "function" } ]; // src/core/accounts/safe/abi/safe.ts var SafeAbi = [ { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "owner", type: "address" } ], name: "AddedOwner", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "bytes32", name: "approvedHash", type: "bytes32" }, { indexed: true, internalType: "address", name: "owner", type: "address" } ], name: "ApproveHash", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "handler", type: "address" } ], name: "ChangedFallbackHandler", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "guard", type: "address" } ], name: "ChangedGuard", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "uint256", name: "threshold", type: "uint256" } ], name: "ChangedThreshold", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "module", type: "address" } ], name: "DisabledModule", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "module", type: "address" } ], name: "EnabledModule", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "bytes32", name: "txHash", type: "bytes32" }, { indexed: false, internalType: "uint256", name: "payment", type: "uint256" } ], name: "ExecutionFailure", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "module", type: "address" } ], name: "ExecutionFromModuleFailure", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "module", type: "address" } ], name: "ExecutionFromModuleSuccess", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "bytes32", name: "txHash", type: "bytes32" }, { indexed: false, internalType: "uint256", name: "payment", type: "uint256" } ], name: "ExecutionSuccess", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "owner", type: "address" } ], name: "RemovedOwner", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "address", name: "module", type: "address" }, { indexed: false, internalType: "address", name: "to", type: "address" }, { indexed: false, internalType: "uint256", name: "value", type: "uint256" }, { indexed: false, internalType: "bytes", name: "data", type: "bytes" }, { indexed: false, internalType: "enum Enum.Operation", name: "operation", type: "uint8" } ], name: "SafeModuleTransaction", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "address", name: "to", type: "address" }, { indexed: false, internalType: "uint256", name: "value", type: "uint256" }, { indexed: false, internalType: "bytes", name: "data", type: "bytes" }, { indexed: false, internalType: "enum Enum.Operation", name: "operation", type: "uint8" }, { indexed: false, internalType: "uint256", name: "safeTxGas", type: "uint256" }, { indexed: false, internalType: "uint256", name: "baseGas", type: "uint256" }, { indexed: false, internalType: "uint256", name: "gasPrice", type: "uint256" }, { indexed: false, internalType: "address", name: "gasToken", type: "address" }, { indexed: false, internalType: "address payable", name: "refundReceiver", type: "address" }, { indexed: false, internalType: "bytes", name: "signatures", type: "bytes" }, { indexed: false, internalType: "bytes", name: "additionalInfo", type: "bytes" } ], name: "SafeMultiSigTransaction", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "sender", type: "address" }, { indexed: false, internalType: "uint256", name: "value", type: "uint256" } ], name: "SafeReceived", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "initiator", type: "address" }, { indexed: false, internalType: "address[]", name: "owners", type: "address[]" }, { indexed: false, internalType: "uint256", name: "threshold", type: "uint256" }, { indexed: false, internalType: "address", name: "initializer", type: "address" }, { indexed: false, internalType: "address", name: "fallbackHandler", type: "address" } ], name: "SafeSetup", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "bytes32", name: "msgHash", type: "bytes32" } ], name: "SignMsg", type: "event" }, { stateMutability: "nonpayable", type: "fallback" }, { inputs: [], name: "VERSION", outputs: [ { internalType: "string", name: "", type: "string" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "owner", type: "address" }, { internalType: "uint256", name: "_threshold", type: "uint256" } ], name: "addOwnerWithThreshold", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "bytes32", name: "hashToApprove", type: "bytes32" } ], name: "approveHash", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "", type: "address" }, { internalType: "bytes32", name: "", type: "bytes32" } ], name: "approvedHashes", outputs: [ { internalType: "uint256", name: "", type: "uint256" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "uint256", name: "_threshold", type: "uint256" } ], name: "changeThreshold", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "bytes32", name: "dataHash", type: "bytes32" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "bytes", name: "signatures", type: "bytes" }, { internalType: "uint256", name: "requiredSignatures", type: "uint256" } ], name: "checkNSignatures", outputs: [], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "bytes32", name: "dataHash", type: "bytes32" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "bytes", name: "signatures", type: "bytes" } ], name: "checkSignatures", outputs: [], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "prevModule", type: "address" }, { internalType: "address", name: "module", type: "address" } ], name: "disableModule", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [], name: "domainSeparator", outputs: [ { internalType: "bytes32", name: "", type: "bytes32" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "module", type: "address" } ], name: "enableModule", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "to", type: "address" }, { internalType: "uint256", name: "value", type: "uint256" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "enum Enum.Operation", name: "operation", type: "uint8" }, { internalType: "uint256", name: "safeTxGas", type: "uint256" }, { internalType: "uint256", name: "baseGas", type: "uint256" }, { internalType: "uint256", name: "gasPrice", type: "uint256" }, { internalType: "address", name: "gasToken", type: "address" }, { internalType: "address", name: "refundReceiver", type: "address" }, { internalType: "uint256", name: "_nonce", type: "uint256" } ], name: "encodeTransactionData", outputs: [ { internalType: "bytes", name: "", type: "bytes" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "to", type: "address" }, { internalType: "uint256", name: "value", type: "uint256" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "enum Enum.Operation", name: "operation", type: "uint8" }, { internalType: "uint256", name: "safeTxGas", type: "uint256" }, { internalType: "uint256", name: "baseGas", type: "uint256" }, { internalType: "uint256", name: "gasPrice", type: "uint256" }, { internalType: "address", name: "gasToken", type: "address" }, { internalType: "address payable", name: "refundReceiver", type: "address" }, { internalType: "bytes", name: "signatures", type: "bytes" } ], name: "execTransaction", outputs: [ { internalType: "bool", name: "", type: "bool" } ], stateMutability: "payable", type: "function" }, { inputs: [ { internalType: "address", name: "to", type: "address" }, { internalType: "uint256", name: "value", type: "uint256" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "enum Enum.Operation", name: "operation", type: "uint8" } ], name: "execTransactionFromModule", outputs: [ { internalType: "bool", name: "success", type: "bool" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "to", type: "address" }, { internalType: "uint256", name: "value", type: "uint256" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "enum Enum.Operation", name: "operation", type: "uint8" } ], name: "execTransactionFromModuleReturnData", outputs: [ { internalType: "bool", name: "success", type: "bool" }, { internalType: "bytes", name: "returnData", type: "bytes" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [], name: "getChainId", outputs: [ { internalType: "uint256", name: "", type: "uint256" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "start", type: "address" }, { internalType: "uint256", name: "pageSize", type: "uint256" } ], name: "getModulesPaginated", outputs: [ { internalType: "address[]", name: "array", type: "address[]" }, { internalType: "address", name: "next", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [], name: "getOwners", outputs: [ { internalType: "address[]", name: "", type: "address[]" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "uint256", name: "offset", type: "uint256" }, { internalType: "uint256", name: "length", type: "uint256" } ], name: "getStorageAt", outputs: [ { internalType: "bytes", name: "", type: "bytes" } ], stateMutability: "view", type: "function" }, { inputs: [], name: "getThreshold", outputs: [ { internalType: "uint256", name: "", type: "uint256" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "to", type: "address" }, { internalType: "uint256", name: "value", type: "uint256" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "enum Enum.Operation", name: "operation", type: "uint8" }, { internalType: "uint256", name: "safeTxGas", type: "uint256" }, { internalType: "uint256", name: "baseGas", type: "uint256" }, { internalType: "uint256", name: "gasPrice", type: "uint256" }, { internalType: "address", name: "gasToken", type: "address" }, { internalType: "address", name: "refundReceiver", type: "address" }, { internalType: "uint256", name: "_nonce", type: "uint256" } ], name: "getTransactionHash", outputs: [ { internalType: "bytes32", name: "", type: "bytes32" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "module", type: "address" } ], name: "isModuleEnabled", outputs: [ { internalType: "bool", name: "", type: "bool" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "owner", type: "address" } ], name: "isOwner", outputs: [ { internalType: "bool", name: "", type: "bool" } ], stateMutability: "view", type: "function" }, { inputs: [], name: "nonce", outputs: [ { internalType: "uint256", name: "", type: "uint256" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "prevOwner", type: "address" }, { internalType: "address", name: "owner", type: "address" }, { internalType: "uint256", name: "_threshold", type: "uint256" } ], name: "removeOwner", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "handler", type: "address" } ], name: "setFallbackHandler", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "guard", type: "address" } ], name: "setGuard", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address[]", name: "_owners", type: "address[]" }, { internalType: "uint256", name: "_threshold", type: "uint256" }, { internalType: "address", name: "to", type: "address" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "address", name: "fallbackHandler", type: "address" }, { internalType: "address", name: "paymentToken", type: "address" }, { internalType: "uint256", name: "payment", type: "uint256" }, { internalType: "address payable", name: "paymentReceiver", type: "address" } ], name: "setup", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "bytes32", name: "", type: "bytes32" } ], name: "signedMessages", outputs: [ { internalType: "uint256", name: "", type: "uint256" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "targetContract", type: "address" }, { internalType: "bytes", name: "calldataPayload", type: "bytes" } ], name: "simulateAndRevert", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "prevOwner", type: "address" }, { internalType: "address", name: "oldOwner", type: "address" }, { internalType: "address", name: "newOwner", type: "address" } ], name: "swapOwner", outputs: [], stateMutability: "nonpayable", type: "function" }, { stateMutability: "payable", type: "receive" } ]; // src/core/accounts/safe/abi/safeProxyFactory.ts var SafeProxyContractFactoryABI = [ { anonymous: false, inputs: [ { indexed: true, internalType: "contract SafeProxy", name: "proxy", type: "address" }, { indexed: false, internalType: "address", name: "singleton", type: "address" } ], name: "ProxyCreation", type: "event" }, { inputs: [ { internalType: "address", name: "_singleton", type: "address" }, { internalType: "bytes", name: "initializer", type: "bytes" }, { internalType: "uint256", name: "saltNonce", type: "uint256" } ], name: "createChainSpecificProxyWithNonce", outputs: [ { internalType: "contract SafeProxy", name: "proxy", type: "address" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "_singleton", type: "address" }, { internalType: "bytes", name: "initializer", type: "bytes" }, { internalType: "uint256", name: "saltNonce", type: "uint256" }, { internalType: "contract IProxyCreationCallback", name: "callback", type: "address" } ], name: "createProxyWithCallback", outputs: [ { internalType: "contract SafeProxy", name: "proxy", type: "address" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "_singleton", type: "address" }, { internalType: "bytes", name: "initializer", type: "bytes" }, { internalType: "uint256", name: "saltNonce", type: "uint256" } ], name: "createProxyWithNonce", outputs: [ { internalType: "contract SafeProxy", name: "proxy", type: "address" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [], name: "getChainId", outputs: [ { internalType: "uint256", name: "", type: "uint256" } ], stateMutability: "view", type: "function" }, { inputs: [], name: "proxyCreationCode", outputs: [ { internalType: "bytes", name: "", type: "bytes" } ], stateMutability: "pure", type: "function" } ]; // src/core/accounts/safe/abi/sharedWebAuthnSigner.ts var SafeWebAuthnSharedSignerAbi = [ { type: "constructor", inputs: [], stateMutability: "nonpayable" }, { type: "error", name: "NotDelegateCalled", inputs: [] }, { type: "function", name: "SIGNER_SLOT", inputs: [], outputs: [ { name: "", type: "uint256", internalType: "uint256" } ], stateMutability: "view" }, { type: "function", name: "configure", inputs: [ { name: "signer", type: "tuple", internalType: "struct SafeWebAuthnSharedSigner.Signer", components: [ { name: "x", type: "uint256", internalType: "uint256" }, { name: "y", type: "uint256", internalType: "uint256" }, { name: "verifiers", type: "uint176", internalType: "P256.Verifiers" } ] } ], outputs: [], stateMutability: "nonpayable" }, { type: "function", name: "getConfiguration", inputs: [ { name: "account", type: "address", internalType: "address" } ], outputs: [ { name: "signer", type: "tuple", internalType: "struct SafeWebAuthnSharedSigner.Signer", components: [ { name: "x", type: "uint256", internalType: "uint256" }, { name: "y", type: "uint256", internalType: "uint256" }, { name: "verifiers", type: "uint176", internalType: "P256.Verifiers" } ] } ], stateMutability: "view" }, { type: "function", name: "isValidSignature", inputs: [ { name: "message", type: "bytes32", internalType: "bytes32" }, { name: "signature", type: "bytes", internalType: "bytes" } ], outputs: [ { name: "magicValue", type: "bytes4", internalType: "bytes4" } ], stateMutability: "view" }, { type: "function", name: "isValidSignature", inputs: [ { name: "data", type: "bytes", internalType: "bytes" }, { name: "signature", type: "bytes", internalType: "bytes" } ], outputs: [ { name: "magicValue", type: "bytes4", internalType: "bytes4" } ], stateMutability: "view" } ]; // src/core/accounts/safe/services/safe.ts var encodeMultiSendTransactions = (transactions) => { return _viem.concat.call(void 0, transactions.map( ({ op, to, value, data }) => _viem.encodePacked.call(void 0, ["uint8", "address", "uint256", "uint256", "bytes"], [op, to, _nullishCoalesce(value, () => ( 0n)), BigInt(_viem.size.call(void 0, data)), data] ) ) ); }; var decodeUserOp = ({ userOperation, multisend }) => { const { args } = _viem.decodeFunctionData.call(void 0, { abi: safe4337SessionKeyModuleAbi, data: userOperation.callData }); if (!args) throw new Error("Invalid callData for Safe Account"); const txs = []; if (args[0] === multisend) { const decodedData = _viem.decodeFunctionData.call(void 0, { abi: MultiSendContractABI, data: args[2] }); const multisendArgs = decodedData.args[0]; let index = 2; while (index < multisendArgs.length) { const operation = `0x${multisendArgs.slice(index, index + 2)}`; index += 2; const to = `0x${multisendArgs.slice(index, index + 40)}`; index += 40; const value = `0x${multisendArgs.slice(index, index + 64)}`; index += 64; const dataLength = parseInt(multisendArgs.slice(index, index + 64), 16) * 2; index += 64; const data = `0x${multisendArgs.slice( index, index + dataLength )}`; index += dataLength; txs.push({ op: Number(operation), to: _viem.getAddress.call(void 0, to), value: BigInt(value), data }); } } else { txs.push({ to: args[0], value: args[1], data: args[2], op: args[3] }); } return txs; }; var getSetUpCallData = ({ modules, accountSigner, setUpContractAddress, safeWebAuthnSharedSignerContractAddress, safeP256VerifierAddress }) => { const enableModuleCallData = _viem.encodeFunctionData.call(void 0, { abi: EnableModuleAbi, functionName: "enableModules", args: [modules] }); if (isComethSigner(accountSigner) && accountSigner.type === "passkey") { const sharedSignerConfigCallData = _viem.encodeFunctionData.call(void 0, { abi: SafeWebAuthnSharedSignerAbi, functionName: "configure", args: [ { x: _viem.hexToBigInt.call(void 0, accountSigner.passkey.pubkeyCoordinates.x), y: _viem.hexToBigInt.call(void 0, accountSigner.passkey.pubkeyCoordinates.y), verifiers: _viem.hexToBigInt.call(void 0, safeP256VerifierAddress) } ] }); return _viem.encodeFunctionData.call(void 0, { abi: MultiSendContractABI, functionName: "multiSend", args: [ encodeMultiSendTransactions([ { op: 1, to: setUpContractAddress, data: enableModuleCallData }, { op: 1, to: safeWebAuthnSharedSignerContractAddress, data: sharedSignerConfigCallData } ]) ] }); } return enableModuleCallData; }; var getSafeInitializer = ({ accountSigner, threshold, fallbackHandler, modules, setUpContractAddress, safeWebAuthnSharedSignerContractAddress, p256Verifier, multisendAddress: multisendAddress2 }) => { const signerAddress = getSignerAddress(accountSigner); const setUpCallData = getSetUpCallData({ modules, accountSigner, setUpContractAddress, safeWebAuthnSharedSignerContractAddress, safeP256VerifierAddress: p256Verifier }); if (isComethSigner(accountSigner) && accountSigner.type === "passkey") { return getSafeSetUpData({ owner: safeWebAuthnSharedSignerContractAddress, threshold, setUpContractAddress: multisendAddress2, setUpData: setUpCallData, fallbackHandler }); } return getSafeSetUpData({ owner: signerAddress, threshold, setUpContractAddress, setUpData: setUpCallData, fallbackHandler }); }; var isSafeOwner = async ({ safeAddress, accountSigner, chain, safeProxyFactoryAddress, safeSingletonAddress, safeModuleSetUpAddress, fallbackHandler, modules, sharedWebAuthnSignerContractAddress, p256Verifier, multisendAddress: multisendAddress2, rpcUrl }) => { const signerAddress = getSignerAddress(accountSigner); try { const publicClient = _viem.createPublicClient.call(void 0, { chain, transport: _viem.http.call(void 0, rpcUrl) }); const safe = _viem.getContract.call(void 0, { address: safeAddress, abi: SafeAbi, client: publicClient }); const isDeployed2 = await _permissionless.isSmartAccountDeployed.call(void 0, publicClient, safeAddress ); if (!isDeployed2) throw new Error("Safe not deployed"); return await safe.read.isOwner([signerAddress]); } catch (e6) { const predictedWalletAddress = await predictSafeAddress({ saltNonce: 0n, chain, accountSigner, safeProxyFactoryAddress, safeSingletonAddress, setUpContractAddress: safeModuleSetUpAddress, fallbackHandler, p256Verifier, modules, multisendAddress: multisendAddress2, threshold: 1, sharedWebAuthnSignerContractAddress }); if (predictedWalletAddress !== safeAddress) return false; } return true; }; var predictSafeAddress = async ({ saltNonce, chain, accountSigner, fallbackHandler, modules, setUpContractAddress, safeSingletonAddress, safeProxyFactoryAddress, sharedWebAuthnSignerContractAddress, p256Verifier, multisendAddress: multisendAddress2, threshold = 1, rpcUrl }) => { const initializer = getSafeInitializer({ accountSigner, threshold, fallbackHandler, modules, setUpContractAddress, safeWebAuthnSharedSignerContractAddress: sharedWebAuthnSignerContractAddress, p256Verifier, multisendAddress: multisendAddress2 }); return getSafeAddressFromInitializer({ chain, rpcUrl, safeProxyFactoryAddress, safeSingletonAddress, initializer, saltNonce }); }; var getSafeAddressFromInitializer = async ({ chain, rpcUrl, safeProxyFactoryAddress, safeSingletonAddress, initializer, saltNonce }) => { const publicClient = _viem.createPublicClient.call(void 0, { chain, transport: _viem.http.call(void 0, rpcUrl) }); const proxyCreationCode = await publicClient.readContract({ address: safeProxyFactoryAddress, abi: SafeProxyContractFactoryABI, functionName: "proxyCreationCode" }); const deploymentCode = _viem.encodePacked.call(void 0, ["bytes", "uint256"], [proxyCreationCode, _viem.hexToBigInt.call(void 0, safeSingletonAddress)] ); const salt = _viem.keccak256.call(void 0, _viem.encodePacked.call(void 0, ["bytes32", "uint256"], [_viem.keccak256.call(void 0, _viem.encodePacked.call(void 0, ["bytes"], [initializer])), saltNonce] ) ); return _viem.getContractAddress.call(void 0, { bytecode: deploymentCode, from: safeProxyFactoryAddress, opcode: "CREATE2", salt }); }; var getSafeSetUpData = ({ owner, threshold, setUpContractAddress, setUpData, fallbackHandler }) => { return _viem.encodeFunctionData.call(void 0, { abi: SafeAbi, functionName: "setup", args: [ [owner], threshold, setUpContractAddress, setUpData, fallbackHandler, _viem.zeroAddress, 0, _viem.zeroAddress ] }); }; // src/core/actions/accounts/safe/sessionKeys/utils.ts var isUserOpWhitelisted = async ({ chain, userOperation, safe4337SessionKeysModule, multisend, smartAccountAddress, sessionKey, rpcUrl }) => { const txs = await decodeUserOp({ userOperation, multisend }); for (const tx of txs) { const isWhitelisted = await queryIsWhitelistFrom4337ModuleAddress({ chain, smartAccountAddress, safe4337SessionKeysModule, sessionKey, rpcUrl, targetAddress: tx.to }); if (!isWhitelisted) return false; } return true; }; var querySessionFrom4337ModuleAddress = async (args) => { const publicClient = _viem.createPublicClient.call(void 0, { chain: args.chain, transport: _viem.http.call(void 0, _optionalChain([args, 'optionalAccess', _14 => _14.rpcUrl])), cacheTime: 6e4, batch: { multicall: { wait: 50 } } }); const isDeployed2 = await _permissionless.isSmartAccountDeployed.call(void 0, publicClient, args.smartAccountAddress ); if (!isDeployed2) throw new Error("Smart account is not deployed."); const safe4337SessionKeyModuleContract = _viem.getContract.call(void 0, { address: args.safe4337SessionKeysModule, abi: safe4337SessionKeyModuleAbi, client: publicClient }); return await safe4337SessionKeyModuleContract.read.sessionKeys([ args.sessionKey ]); }; var queryIsWhitelistFrom4337ModuleAddress = async (args) => { const publicClient = _viem.createPublicClient.call(void 0, { chain: args.chain, transport: _viem.http.call(void 0, _optionalChain([args, 'optionalAccess', _15 => _15.rpcUrl])), cacheTime: 6e4, batch: { multicall: { wait: 50 } } }); const isDeployed2 = await _permissionless.isSmartAccountDeployed.call(void 0, publicClient, args.smartAccountAddress ); if (!isDeployed2) throw new Error("Smart account is not deployed."); const safe4337SessionKeyModuleContract = _viem.getContract.call(void 0, { address: args.safe4337SessionKeysModule, abi: safe4337SessionKeyModuleAbi, client: publicClient }); return await safe4337SessionKeyModuleContract.read.whitelistDestinations([ args.sessionKey, args.targetAddress ]); }; var getSessionKeySigner = async ({ chain, safe4337SessionKeysModule, rpcUrl, smartAccountAddress }) => { if (!smartAccountAddress) return void 0; const privateKey = await getSessionKeyPKFromLocalStorage(smartAccountAddress); if (!privateKey) return void 0; const signer = _accounts.privateKeyToAccount.call(void 0, privateKey); const session = await querySessionFrom4337ModuleAddress({ chain, smartAccountAddress, safe4337SessionKeysModule, sessionKey: signer.address, rpcUrl }); try { const now = /* @__PURE__ */ new Date(); const validAfter = new Date(session.validAfter); const validUntil = new Date(session.validUntil); if (session.revoked) throw new Error("Session key has been revoked"); if (validAfter > now) throw new Error("Session key is not yet valid"); if (validUntil < now) throw new Error("Session key is expired"); } catch (err) { console.info(err); deleteSessionKeyInStorage(smartAccountAddress); return void 0; } return { type: "localWallet", eoaFallback: { privateKey, signer: _accounts.privateKeyToAccount.call(void 0, privateKey), encryptionSalt: defaultEncryptionSalt } }; }; // src/core/services/deviceService.ts var _bowser = require('bowser'); var _bowser2 = _interopRequireDefault(_bowser); var getDeviceData = () => { const parser = _bowser2.default.getParser(window.navigator.userAgent); const result = parser.getResult(); const browser = result.browser.name; const os = result.os.name; const platform = result.platform.type; return { browser, os, platform }; }; // src/core/services/comethService.ts var createNewWalletInDb = async ({ chain, api, smartAccountAddress, signer }) => { const initiatorAddress = getSignerAddress(signer); if (signer.type === "passkey") { return await api.initWallet({ chainId: chain.id, smartAccountAddress, initiatorAddress, publicKeyId: signer.passkey.id, publicKeyX: signer.passkey.pubkeyCoordinates.x, publicKeyY: signer.passkey.pubkeyCoordinates.y, deviceData: getDeviceData() }); } return await api.initWallet({ chainId: chain.id, smartAccountAddress, initiatorAddress }); }; var getProjectParamsByChain = async ({ api, chain }) => { return await api.getProjectParams(chain.id); }; // src/core/accounts/safe/createSafeSmartAccount.ts var _accounts3 = require('permissionless/accounts'); // src/core/accounts/utils.ts var getViemClient = (chain, rpcUrl) => { const rpcTransport = _viem.http.call(void 0, rpcUrl, { batch: { wait: 50 }, retryCount: 5, retryDelay: 200, timeout: 2e4 }); return _viem.createClient.call(void 0, { chain, transport: rpcTransport, cacheTime: 6e4, batch: { multicall: { wait: 50 } } }); }; // src/core/accounts/safe/abi/safe4337ModuleAbi.ts var safe4337ModuleAbi = [ { inputs: [ { internalType: "address", name: "entryPoint", type: "address" } ], stateMutability: "nonpayable", type: "constructor" }, { inputs: [], name: "ExecutionFailed", type: "error" }, { inputs: [], name: "InvalidCaller", type: "error" }, { inputs: [], name: "InvalidEntryPoint", type: "error" }, { inputs: [], name: "UnsupportedEntryPoint", type: "error" }, { inputs: [{ internalType: "bytes4", name: "selector", type: "bytes4" }], name: "UnsupportedExecutionFunction", type: "error" }, { inputs: [], name: "SUPPORTED_ENTRYPOINT", outputs: [{ internalType: "address", name: "", type: "address" }], stateMutability: "view", type: "function" }, { inputs: [], name: "domainSeparator", outputs: [ { internalType: "bytes32", name: "domainSeparatorHash", type: "bytes32" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "contract Safe", name: "safe", type: "address" }, { internalType: "bytes", name: "message", type: "bytes" } ], name: "encodeMessageDataForSafe", outputs: [{ internalType: "bytes", name: "", type: "bytes" }], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "to", type: "address" }, { internalType: "uint256", name: "value", type: "uint256" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "uint8", name: "operation", type: "uint8" } ], name: "executeUserOp", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "to", type: "address" }, { internalType: "uint256", name: "value", type: "uint256" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "uint8", name: "operation", type: "uint8" } ], name: "executeUserOpWithErrorString", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [{ internalType: "bytes", name: "message", type: "bytes" }], name: "getMessageHash", outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "contract Safe", name: "safe", type: "address" }, { internalType: "bytes", name: "message", type: "bytes" } ], name: "getMessageHashForSafe", outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }], stateMutability: "view", type: "function" }, { inputs: [], name: "getModules", outputs: [{ internalType: "address[]", name: "", type: "address[]" }], stateMutability: "view", type: "function" }, { inputs: [ { components: [ { internalType: "address", name: "sender", type: "address" }, { internalType: "uint256", name: "nonce", type: "uint256" }, { internalType: "bytes", name: "initCode", type: "bytes" }, { internalType: "bytes", name: "callData", type: "bytes" }, { internalType: "bytes32", name: "accountGasLimits", type: "bytes32" }, { internalType: "uint256", name: "preVerificationGas", type: "uint256" }, { internalType: "bytes32", name: "gasFees", type: "bytes32" }, { internalType: "bytes", name: "paymasterAndData", type: "bytes" }, { internalType: "bytes", name: "signature", type: "bytes" } ], internalType: "struct PackedUserOperation", name: "userOp", type: "tuple" } ], name: "getOperationHash", outputs: [ { internalType: "bytes32", name: "operationHash", type: "bytes32" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "bytes32", name: "_dataHash", type: "bytes32" }, { internalType: "bytes", name: "_signature", type: "bytes" } ], name: "isValidSignature", outputs: [{ internalType: "bytes4", name: "", type: "bytes4" }], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "bytes", name: "_data", type: "bytes" }, { internalType: "bytes", name: "_signature", type: "bytes" } ], name: "isValidSignature", outputs: [{ internalType: "bytes4", name: "", type: "bytes4" }], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "", type: "address" }, { internalType: "address", name: "", type: "address" }, { internalType: "uint256[]", name: "", type: "uint256[]" }, { internalType: "uint256[]", name: "", type: "uint256[]" }, { internalType: "bytes", name: "", type: "bytes" } ], name: "onERC1155BatchReceived", outputs: [{ internalType: "bytes4", name: "", type: "bytes4" }], stateMutability: "pure", type: "function" }, { inputs: [ { internalType: "address", name: "", type: "address" }, { internalType: "address", name: "", type: "address" }, { internalType: "uint256", name: "", type: "uint256" }, { internalType: "uint256", name: "", type: "uint256" }, { internalType: "bytes", name: "", type: "bytes" } ], name: "onERC1155Received", outputs: [{ internalType: "bytes4", name: "", type: "bytes4" }], stateMutability: "pure", type: "function" }, { inputs: [ { internalType: "address", name: "", type: "address" }, { internalType: "address", name: "", type: "address" }, { internalType: "uint256", name: "", type: "uint256" }, { internalType: "bytes", name: "", type: "bytes" } ], name: "onERC721Received", outputs: [{ internalType: "bytes4", name: "", type: "bytes4" }], stateMutability: "pure", type: "function" }, { inputs: [ { internalType: "address", name: "targetContract", type: "address" }, { internalType: "bytes", name: "calldataPayload", type: "bytes" } ], name: "simulate", outputs: [{ internalType: "bytes", name: "response", type: "bytes" }], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "bytes4", name: "interfaceId", type: "bytes4" } ], name: "supportsInterface", outputs: [{ internalType: "bool", name: "", type: "bool" }], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "", type: "address" }, { internalType: "address", name: "", type: "address" }, { internalType: "address", name: "", type: "address" }, { internalType: "uint256", name: "", type: "uint256" }, { internalType: "bytes", name: "", type: "bytes" }, { internalType: "bytes", name: "", type: "bytes" } ], name: "tokensReceived", outputs: [], stateMutability: "pure", type: "function" }, { inputs: [ { components: [ { internalType: "address", name: "sender", type: "address" }, { internalType: "uint256", name: "nonce", type: "uint256" }, { internalType: "bytes", name: "initCode", type: "bytes" }, { internalType: "bytes", name: "callData", type: "bytes" }, { internalType: "bytes32", name: "accountGasLimits", type: "bytes32" }, { internalType: "uint256", name: "preVerificationGas", type: "uint256" }, { internalType: "bytes32", name: "gasFees", type: "bytes32" }, { internalType: "bytes", name: "paymasterAndData", type: "bytes" }, { internalType: "bytes", name: "signature", type: "bytes" } ], internalType: "struct PackedUserOperation", name: "userOp", type: "tuple" }, { internalType: "bytes32", name: "", type: "bytes32" }, { internalType: "uint256", name: "missingAccountFunds", type: "uint256" } ], name: "validateUserOp", outputs: [ { internalType: "uint256", name: "validationData", type: "uint256" } ], stateMutability: "nonpayable", type: "function" } ]; // src/core/accounts/safe/safeSigner/ecdsa/ecdsa.ts var _actions = require('viem/actions'); // src/core/accounts/safe/services/utils.ts var ECDSA_DUMMY_SIGNATURE = "0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"; var DUMMY_AUTHENTICATOR_DATA = new Uint8Array(37); DUMMY_AUTHENTICATOR_DATA.fill(254); DUMMY_AUTHENTICATOR_DATA[32] = 4; var DUMMY_CLIENT_DATA_FIELDS = [ `"origin":"http://safe.global"`, `"padding":"This pads the clientDataJSON so that we can leave room for additional implementation specific fields for a more accurate 'preVerificationGas' estimate."` ].join(","); function getSignatureBytes({ authenticatorData, clientDataFields, r, s }) { const encodeUint256 = (x) => x.toString(16).padStart(64, "0"); const byteSize = (data) => 32 * (Math.ceil(data.length / 32) + 1); const encodeBytes = (data) => `${encodeUint256(data.length)}${_viem.toHex.call(void 0, data).slice(2)}`.padEnd( byteSize(data) * 2, "0" ); const authenticatorDataOffset = 32 * 4; const clientDataFieldsOffset = authenticatorDataOffset + byteSize(authenticatorData); return `0x${encodeUint256(authenticatorDataOffset)}${encodeUint256( clientDataFieldsOffset )}${encodeUint256(r)}${encodeUint256(s)}${encodeBytes( authenticatorData )}${encodeBytes(new TextEncoder().encode(clientDataFields))}`; } var buildSignatureBytes = (signatures) => { const SIGNATURE_LENGTH_BYTES = 65; signatures.sort( (left, right) => left.signer.toLowerCase().localeCompare(right.signer.toLowerCase()) ); let signatureBytes = "0x"; let dynamicBytes = ""; for (const sig of signatures) { if (sig.dynamic) { const dynamicPartPosition = (signatures.length * SIGNATURE_LENGTH_BYTES + dynamicBytes.length / 2).toString(16).padStart(64, "0"); const dynamicPartLength = (sig.data.slice(2).length / 2).toString(16).padStart(64, "0"); const staticSignature = `${sig.signer.slice(2).padStart(64, "0")}${dynamicPartPosition}00`; const dynamicPartWithLength = `${dynamicPartLength}${sig.data.slice( 2 )}`; signatureBytes += staticSignature; dynamicBytes += dynamicPartWithLength; } else { signatureBytes += sig.data.slice(2); } } return signatureBytes + dynamicBytes; }; function packPaymasterData({ paymaster, paymasterVerificationGasLimit, paymasterPostOpGasLimit, paymasterData }) { if (!paymasterData) return "0x"; return _viem.encodePacked.call(void 0, ["address", "uint128", "uint128", "bytes"], [ paymaster, paymasterVerificationGasLimit, paymasterPostOpGasLimit, paymasterData ] ); } var packInitCode = ({ factory, factoryData }) => { if (!(factoryData && factory)) return "0x"; const factoryBytes = _viem.toBytes.call(void 0, factory); const factoryDataBytes = _viem.toBytes.call(void 0, factoryData); return _viem.toHex.call(void 0, _viem.concat.call(void 0, [factoryBytes, factoryDataBytes])); }; // src/core/accounts/safe/types.ts var EIP712_SAFE_OPERATION_TYPE = { SafeOp: [ { type: "address", name: "safe" }, { type: "uint256", name: "nonce" }, { type: "bytes", name: "initCode" }, { type: "bytes", name: "callData" }, { type: "uint128", name: "verificationGasLimit" }, { type: "uint128", name: "callGasLimit" }, { type: "uint256", name: "preVerificationGas" }, { type: "uint128", name: "maxPriorityFeePerGas" }, { type: "uint128", name: "maxFeePerGas" }, { type: "bytes", name: "paymasterAndData" }, { type: "uint48", name: "validAfter" }, { type: "uint48", name: "validUntil" }, { type: "address", name: "entryPoint" } ] }; var EIP712_SAFE_MESSAGE_TYPE = { // "SafeMessage(bytes message)" SafeMessage: [{ type: "bytes", name: "message" }] }; var SAFE_SENTINEL_OWNERS = "0x1"; // src/core/accounts/safe/safeSigner/ecdsa/ecdsa.ts async function safeECDSASigner(client, { signer, safe4337Module, smartAccountAddress }) { const viemSigner = { ...signer, signTransaction: (_, __) => { throw new (0, _accounts3.SignTransactionNotSupportedBySmartAccount)(); } }; const account = _accounts.toAccount.call(void 0, { address: smartAccountAddress, async signMessage({ message }) { return _actions.signTypedData.call(void 0, client, { account: viemSigner, domain: { chainId: _optionalChain([client, 'access', _16 => _16.chain, 'optionalAccess', _17 => _17.id]), verifyingContract: smartAccountAddress }, types: EIP712_SAFE_MESSAGE_TYPE, primaryType: "SafeMessage", message: { message } }); }, async signTransaction(_, __) { throw new (0, _accounts3.SignTransactionNotSupportedBySmartAccount)(); }, async signTypedData(typedData) { return _actions.signTypedData.call(void 0, client, { account: viemSigner, ...typedData } ); } }); return { ...account, address: smartAccountAddress, source: "safeECDSASigner", // Sign a user operation async signUserOperation(userOperation) { const payload = { domain: { chainId: _optionalChain([client, 'access', _18 => _18.chain, 'optionalAccess', _19 => _19.id]), verifyingContract: safe4337Module }, types: EIP712_SAFE_OPERATION_TYPE, primaryType: "SafeOp", message: { callData: userOperation.callData, nonce: userOperation.nonce, initCode: packInitCode({ factory: userOperation.factory, factoryData: userOperation.factoryData }), paymasterAndData: packPaymasterData({ paymaster: userOperation.paymaster, paymasterVerificationGasLimit: userOperation.paymasterVerificationGasLimit, paymasterPostOpGasLimit: userOperation.paymasterPostOpGasLimit, paymasterData: userOperation.paymasterData }), preVerificationGas: userOperation.preVerificationGas, entryPoint: ENTRYPOINT_ADDRESS_V07, validAfter: 0, validUntil: 0, safe: userOperation.sender, verificationGasLimit: userOperation.verificationGasLimit, callGasLimit: userOperation.callGasLimit, maxPriorityFeePerGas: userOperation.maxPriorityFeePerGas, maxFeePerGas: userOperation.maxFeePerGas } }; return _viem.encodePacked.call(void 0, ["uint48", "uint48", "bytes"], [ 0, 0, buildSignatureBytes([ { signer: signer.address, data: await signer.signTypedData(payload) } ]) ] ); }, /** * Get a dummy signature for this smart account */ async getDummySignature() { return ECDSA_DUMMY_SIGNATURE; } }; } // src/core/accounts/safe/safeSigner/webauthn/webAuthn.ts async function safeWebAuthnSigner(client, { passkey, passkeySignerAddress, safe4337Module, smartAccountAddress, fullDomainSelected }) { const publicKeyCredential = { id: parseHex(passkey.id), type: "public-key" }; const account = _accounts.toAccount.call(void 0, { address: smartAccountAddress, async signMessage({ message }) { if (typeof message === "string") message = _viem.toHex.call(void 0, message); const hash = _viem.hashTypedData.call(void 0, { domain: { chainId: _optionalChain([client, 'access', _20 => _20.chain, 'optionalAccess', _21 => _21.id]), verifyingContract: smartAccountAddress }, types: EIP712_SAFE_MESSAGE_TYPE, primaryType: "SafeMessage", message: { message } }); const passkeySignature = await sign({ challenge: hash, publicKeyCredential: [publicKeyCredential], fullDomainSelected }); return buildSignatureBytes([ { signer: passkeySignerAddress, data: passkeySignature.signature, dynamic: true } ]); }, async signTransaction(_, __) { throw new (0, _accounts3.SignTransactionNotSupportedBySmartAccount)(); }, async signTypedData() { throw new (0, _accounts3.SignTransactionNotSupportedBySmartAccount)(); } }); return { ...account, address: smartAccountAddress, source: "safeWebAuthnSigner", // Sign a user operation async signUserOperation(userOperation) { const hash = _viem.hashTypedData.call(void 0, { domain: { chainId: _optionalChain([client, 'access', _22 => _22.chain, 'optionalAccess', _23 => _23.id]), verifyingContract: safe4337Module }, types: EIP712_SAFE_OPERATION_TYPE, primaryType: "SafeOp", message: { callData: userOperation.callData, nonce: userOperation.nonce, initCode: packInitCode({ factory: userOperation.factory, factoryData: userOperation.factoryData }), paymasterAndData: packPaymasterData({ paymaster: userOperation.paymaster, paymasterVerificationGasLimit: userOperation.paymasterVerificationGasLimit, paymasterPostOpGasLimit: userOperation.paymasterPostOpGasLimit, paymasterData: userOperation.paymasterData }), preVerificationGas: userOperation.preVerificationGas, entryPoint: ENTRYPOINT_ADDRESS_V07, validAfter: 0, validUntil: 0, safe: userOperation.sender, verificationGasLimit: userOperation.verificationGasLimit, callGasLimit: userOperation.callGasLimit, maxPriorityFeePerGas: userOperation.maxPriorityFeePerGas, maxFeePerGas: userOperation.maxFeePerGas } }); const passkeySignature = await sign({ challenge: hash, publicKeyCredential: [publicKeyCredential], fullDomainSelected }); return _viem.encodePacked.call(void 0, ["uint48", "uint48", "bytes"], [ 0, 0, buildSignatureBytes([ { signer: passkeySignerAddress, data: passkeySignature.signature, dynamic: true } ]) ] ); }, /** * Get a dummy signature for this smart account */ async getDummySignature() { return _viem.encodePacked.call(void 0, ["uint48", "uint48", "bytes"], [ 0, 0, buildSignatureBytes([ { signer: passkeySignerAddress, data: getSignatureBytes({ authenticatorData: DUMMY_AUTHENTICATOR_DATA, clientDataFields: DUMMY_CLIENT_DATA_FIELDS, r: BigInt(`0x${"ec".repeat(32)}`), s: BigInt(`0x${"d5a".repeat(21)}f`) }), dynamic: true } ]) ] ); } }; } // src/core/accounts/safe/safeSigner/comethSignerToSafeSigner.ts async function comethSignerToSafeSigner(client, { accountSigner, safe4337Module, smartAccountAddress, fullDomainSelected }) { if (isComethSigner(accountSigner) && accountSigner.type === "passkey") { return { ...await safeWebAuthnSigner(client, { passkey: accountSigner.passkey, passkeySignerAddress: accountSigner.passkey.signerAddress, safe4337Module, smartAccountAddress, fullDomainSelected }) }; } return { ...await safeECDSASigner(client, { signer: getSigner(accountSigner), safe4337Module, smartAccountAddress }) }; } // src/core/accounts/safe/createSafeSmartAccount.ts var getAccountInitCode = async ({ initializer, singletonAddress, safeFactoryAddress, saltNonce = _viem.zeroHash }) => { return _viem.encodePacked.call(void 0, ["address", "bytes"], [ safeFactoryAddress, _viem.encodeFunctionData.call(void 0, { abi: SafeProxyContractFactoryABI, functionName: "createProxyWithNonce", args: [singletonAddress, initializer, _viem.hexToBigInt.call(void 0, saltNonce)] }) ] ); }; var storeWalletInComethApi = async ({ chain, singletonAddress, safeProxyFactoryAddress, saltNonce, initializer, signer, api }) => { const smartAccountAddress = await getAccountAddress({ chain, singletonAddress, safeProxyFactoryAddress, saltNonce, initializer }); const isNewWallet = await createNewWalletInDb({ chain, api, smartAccountAddress, signer }); return { smartAccountAddress, isNewWallet }; }; var getAccountAddress = async ({ chain, singletonAddress, safeProxyFactoryAddress, saltNonce = _viem.zeroHash, initializer }) => { return getSafeAddressFromInitializer({ chain, initializer, saltNonce: _viem.hexToBigInt.call(void 0, saltNonce), safeProxyFactoryAddress, safeSingletonAddress: singletonAddress }); }; async function createSafeSmartAccount({ apiKey, chain, rpcUrl, baseUrl, smartAccountAddress, entryPoint: entryPointAddress, comethSignerConfig, safeContractConfig, sessionKeysEnabled = false, signer }) { const api = new API(apiKey, baseUrl); const [client, contractParams] = await Promise.all([ getViemClient(chain, rpcUrl), getProjectParamsByChain({ api, chain }) ]); const { safeWebAuthnSharedSignerContractAddress, setUpContractAddress, p256Verifier, safeProxyFactoryAddress, safeSingletonAddress, multisendAddress: multisendAddress2, safe4337ModuleAddress, safe4337SessionKeysModule, safeWebAuthnSignerFactory } = _nullishCoalesce(safeContractConfig, () => ( contractParams.safeContractParams)); if (!safe4337ModuleAddress) { throw new Error("Network is not supported"); } if (sessionKeysEnabled && !safe4337SessionKeysModule) { throw new Error("Session keys not enabled for this network"); } const safe4337Module = sessionKeysEnabled ? safe4337SessionKeysModule : safe4337ModuleAddress; const accountSigner = await (_nullishCoalesce(signer, () => ( createSigner({ apiKey, chain, smartAccountAddress, ...comethSignerConfig, rpcUrl, baseUrl, safeContractParams: { safeWebAuthnSharedSignerContractAddress, setUpContractAddress, p256Verifier, safeProxyFactoryAddress, safeSingletonAddress, multisendAddress: multisendAddress2, fallbackHandler: safe4337Module, safeWebAuthnSignerFactory } })))); const signerAddress = getSignerAddress(accountSigner); const initializer = getSafeInitializer({ accountSigner, threshold: 1, fallbackHandler: safe4337Module, modules: [safe4337Module], setUpContractAddress, safeWebAuthnSharedSignerContractAddress, p256Verifier, multisendAddress: multisendAddress2 }); if (!smartAccountAddress) { smartAccountAddress = await getAccountAddress({ chain, singletonAddress: safeSingletonAddress, safeProxyFactoryAddress, saltNonce: _viem.zeroHash, initializer }); } const smartAccountDeployed = await _permissionless.isSmartAccountDeployed.call(void 0, client, smartAccountAddress ); const generateInitCode = () => getAccountInitCode({ initializer, singletonAddress: safeSingletonAddress, safeFactoryAddress: safeProxyFactoryAddress }); const res = await storeWalletInComethApi({ chain, singletonAddress: safeSingletonAddress, safeProxyFactoryAddress, saltNonce: _viem.zeroHash, initializer, signer: accountSigner, api }); if (res.isNewWallet) { await saveSigner(accountSigner, smartAccountAddress); } const safeSigner = await comethSignerToSafeSigner( client, { accountSigner, safe4337Module, smartAccountAddress, fullDomainSelected: _nullishCoalesce(_optionalChain([comethSignerConfig, 'optionalAccess', _24 => _24.fullDomainSelected]), () => ( false)) } ); const smartAccount = _accounts3.toSmartAccount.call(void 0, { address: smartAccountAddress, async signMessage({ message }) { return safeSigner.signMessage({ message }); }, async signTransaction(_, __) { throw new (0, _accounts3.SignTransactionNotSupportedBySmartAccount)(); }, async signTypedData() { throw new (0, _accounts3.SignTransactionNotSupportedBySmartAccount)(); }, client, entryPoint: entryPointAddress, source: "safeSmartAccount", async getNonce() { return _permissionless.getAccountNonce.call(void 0, client, { sender: smartAccountAddress, entryPoint: entryPointAddress }); }, async signUserOperation(userOp) { return safeSigner.signUserOperation(userOp); }, async getInitCode() { if (smartAccountDeployed) return "0x"; return await generateInitCode(); }, async getFactory() { if (smartAccountDeployed) return void 0; return safeProxyFactoryAddress; }, async getFactoryData() { if (smartAccountDeployed) return void 0; const initCode = await generateInitCode(); return `0x${initCode.slice(safeProxyFactoryAddress.length)}`; }, async encodeDeployCallData(_) { throw new Error("Safe account doesn't support account deployment"); }, async encodeCallData(_tx) { if (Array.isArray(_tx)) { const userOpCalldata = _viem.encodeFunctionData.call(void 0, { abi: MultiSendContractABI, functionName: "multiSend", args: [ encodeMultiSendTransactions( _tx.map((tx) => ({ op: 0, to: tx.to, data: tx.data, value: _nullishCoalesce(tx.value, () => ( BigInt(0))) })) ) ] }); return _viem.encodeFunctionData.call(void 0, { abi: safe4337ModuleAbi, functionName: "executeUserOpWithErrorString", args: [multisendAddress2, BigInt(0), userOpCalldata, 1] }); } return _viem.encodeFunctionData.call(void 0, { abi: safe4337ModuleAbi, functionName: "executeUserOpWithErrorString", args: [_tx.to, _tx.value, _tx.data, 0] }); }, async getDummySignature(userOp) { return safeSigner.getDummySignature(userOp); } }); return { ...smartAccount, signerAddress, safe4337SessionKeysModule, sessionKeysEnabled, async buildUserOperation(_txs) { const sender = smartAccountAddress; const nonce = await smartAccount.getNonce(); const callData = await smartAccount.encodeCallData(_txs); const factory = await smartAccount.getFactory(); const factoryData = await smartAccount.getFactoryData(); const userOperation = { sender, nonce, factory, factoryData, callData, callGasLimit: 1n, verificationGasLimit: 1n, preVerificationGas: 1n, maxFeePerGas: 1n, maxPriorityFeePerGas: 1n, signature: "0x" }; userOperation.signature = await smartAccount.getDummySignature( userOperation ); return userOperation; }, async signUserOperationWithSessionKey(userOp) { const sessionKeySigner = await getSessionKeySigner({ chain: _optionalChain([client, 'optionalAccess', _25 => _25.chain]), smartAccountAddress, rpcUrl, safe4337SessionKeysModule }); if (!sessionKeySigner) throw new Error("Session key not found"); const signer2 = _optionalChain([sessionKeySigner, 'optionalAccess', _26 => _26.eoaFallback, 'access', _27 => _27.signer]); const isWhitelisted = await isUserOpWhitelisted({ chain: _optionalChain([client, 'optionalAccess', _28 => _28.chain]), safe4337SessionKeysModule, sessionKey: _optionalChain([signer2, 'optionalAccess', _29 => _29.address]), userOperation: userOp, multisend: multisendAddress2, smartAccountAddress }); if (!isWhitelisted) throw new Error("Transactions are not whitelisted"); const payload = { domain: { chainId: _optionalChain([client, 'access', _30 => _30.chain, 'optionalAccess', _31 => _31.id]), verifyingContract: safe4337SessionKeysModule }, types: EIP712_SAFE_OPERATION_TYPE, primaryType: "SafeOp", message: { callData: userOp.callData, nonce: userOp.nonce, initCode: packInitCode({ factory: userOp.factory, factoryData: userOp.factoryData }), paymasterAndData: packPaymasterData({ paymaster: userOp.paymaster, paymasterVerificationGasLimit: userOp.paymasterVerificationGasLimit, paymasterPostOpGasLimit: userOp.paymasterPostOpGasLimit, paymasterData: userOp.paymasterData }), preVerificationGas: userOp.preVerificationGas, entryPoint: ENTRYPOINT_ADDRESS_V07, validAfter: 0, validUntil: 0, safe: userOp.sender, verificationGasLimit: userOp.verificationGasLimit, callGasLimit: userOp.callGasLimit, maxPriorityFeePerGas: userOp.maxPriorityFeePerGas, maxFeePerGas: userOp.maxFeePerGas } }; return _viem.encodePacked.call(void 0, ["uint48", "uint48", "bytes"], [ 0, 0, buildSignatureBytes([ { signer: _optionalChain([sessionKeySigner, 'optionalAccess', _32 => _32.eoaFallback, 'access', _33 => _33.signer, 'access', _34 => _34.address]), data: await _optionalChain([signer2, 'optionalAccess', _35 => _35.signTypedData, 'call', _36 => _36(payload)]) } ]) ] ); }, getConnectApi() { return api; } }; } // src/core/actions/accounts/addNewDevice.ts var _qrcode = require('qrcode'); var QRCode = _interopRequireWildcard(_qrcode); var _flattenPayload = (signerPayload) => { const optimizedPayload = { os: signerPayload.deviceData.os, b: signerPayload.deviceData.browser, p: signerPayload.deviceData.platform, x: signerPayload.publicKeyX, y: signerPayload.publicKeyY, id: signerPayload.publicKeyId, ad: signerPayload.signerAddress }; const flattened = {}; function flattenObject(obj, parentKey = "") { for (const [key, value] of Object.entries(obj)) { const fullKey = parentKey ? `${parentKey}_${key}` : key; if (value && typeof value === "object" && !Array.isArray(value)) { flattenObject(value, fullKey); } else { flattened[fullKey] = value.toString(); } } } flattenObject(optimizedPayload); return flattened; }; var createNewSignerWithAccountAddress = async ({ apiKey, baseUrl, smartAccountAddress, params = {} }) => { const api = new API(apiKey, baseUrl); const { signer, localPrivateKey } = await _createNewSigner(api, { passKeyName: params.passKeyName, webAuthnOptions: params.webAuthnOptions, fullDomainSelected: _nullishCoalesce(params.fullDomainSelected, () => ( false)) }); if (signer.publicKeyId) { const { publicKeyId, publicKeyX, publicKeyY, signerAddress } = signer; if (!(publicKeyId && publicKeyX && publicKeyY && signerAddress)) throw new Error("Invalid signer data"); setPasskeyInStorage( smartAccountAddress, publicKeyId, publicKeyX, publicKeyY, signerAddress ); } else { if (!localPrivateKey) throw new NoFallbackSignerError(); encryptSignerInStorage( smartAccountAddress, localPrivateKey, params.encryptionSalt ); } return signer; }; var createNewSigner = async ({ apiKey, baseUrl, params = {} }) => { const api = new API(apiKey, baseUrl); const { signer } = await _createNewPasskeySigner(api, { webAuthnOptions: params.webAuthnOptions, passKeyName: params.passKeyName, fullDomainSelected: _nullishCoalesce(params.fullDomainSelected, () => ( false)) }); return signer; }; var serializeUrlWithSignerPayload = async (validationPageUrl, signerPayload) => { try { const url = new URL(validationPageUrl); const params = _flattenPayload(signerPayload); for (const [key, value] of Object.entries(params)) { url.searchParams.set(key, value); } return url; } catch (error) { throw new Error(`Failed to serialize url: ${error}`); } }; var generateQRCodeUrl = async (validationPageUrl, signerPayload, options) => { try { const serializedUrl = await serializeUrlWithSignerPayload( validationPageUrl, signerPayload ); const qrCodeImageUrl = await QRCode.toDataURL( serializedUrl.toString(), { width: _optionalChain([options, 'optionalAccess', _37 => _37.width]) || 200, margin: _optionalChain([options, 'optionalAccess', _38 => _38.margin]) || 4, color: { dark: _optionalChain([options, 'optionalAccess', _39 => _39.color, 'optionalAccess', _40 => _40.dark]) || "#000000ff", light: _optionalChain([options, 'optionalAccess', _41 => _41.color, 'optionalAccess', _42 => _42.light]) || "#ffffffff" } } ); return qrCodeImageUrl; } catch (error) { throw new Error(`Failed to generate QR Code: ${error}`); } }; var _createNewPasskeySigner = async (api, { passKeyName, webAuthnOptions = DEFAULT_WEBAUTHN_OPTIONS, fullDomainSelected = false }) => { const webAuthnCompatible = await isWebAuthnCompatible(webAuthnOptions); if (!webAuthnCompatible || isFallbackSigner()) throw new Error("Device not compatible with passkeys"); const passkeyWithCoordinates = await createPasskeySigner({ api, webAuthnOptions, passKeyName, fullDomainSelected }); if (passkeyWithCoordinates.publicKeyAlgorithm !== -7) throw new Error("Device not compatible with SECKP256r1 passkeys"); return { signer: { signerAddress: passkeyWithCoordinates.signerAddress, deviceData: getDeviceData(), publicKeyId: passkeyWithCoordinates.id, publicKeyX: passkeyWithCoordinates.pubkeyCoordinates.x, publicKeyY: passkeyWithCoordinates.pubkeyCoordinates.y } }; }; var _createNewSigner = async (api, { passKeyName, webAuthnOptions = DEFAULT_WEBAUTHN_OPTIONS, fullDomainSelected = false }) => { const webAuthnCompatible = await isWebAuthnCompatible(webAuthnOptions); if (webAuthnCompatible && !isFallbackSigner()) { const passkeyWithCoordinates = await createPasskeySigner({ api, webAuthnOptions, passKeyName, fullDomainSelected }); if (passkeyWithCoordinates.publicKeyAlgorithm === -7) { return { signer: { signerAddress: passkeyWithCoordinates.signerAddress, deviceData: getDeviceData(), publicKeyId: passkeyWithCoordinates.id, publicKeyX: passkeyWithCoordinates.pubkeyCoordinates.x, publicKeyY: passkeyWithCoordinates.pubkeyCoordinates.y } }; } } const privateKey = _accounts.generatePrivateKey.call(void 0, ); const signer = _accounts.privateKeyToAccount.call(void 0, privateKey); return { signer: { signerAddress: signer.address, deviceData: getDeviceData() }, localPrivateKey: privateKey }; }; // src/core/actions/accounts/retrieveAccountAddressFromPasskey.ts var retrieveAccountAddressFromPasskeys = async (apiKey, chain, fullDomainSelected = false, baseUrl) => { const api = new API(apiKey, baseUrl); return await retrieveSmartAccountAddressFromPasskey( api, chain, fullDomainSelected ); }; var retrieveAccountAddressFromPasskeyId = async ({ apiKey, id, chain, fullDomainSelected = false, baseUrl }) => { const api = new API(apiKey, baseUrl); return await retrieveSmartAccountAddressFromPasskeyId({ API: api, id, chain, fullDomainSelected }); }; // src/core/actions/accounts/safe/owners/safeOwnerActions.ts var safeOwnerPluginActions = (rpcUrl) => (client) => ({ async addOwner(args) { return await client.sendTransaction({ to: _optionalChain([client, 'access', _43 => _43.account, 'optionalAccess', _44 => _44.address]), data: _viem.encodeFunctionData.call(void 0, { abi: SafeAbi, functionName: "addOwnerWithThreshold", args: [args.ownerToAdd, 1] }), maxFeePerBlobGas: 0n, blobs: [] }); }, async removeOwner(args) { const publicClient = _viem.createPublicClient.call(void 0, { chain: client.chain, transport: _viem.http.call(void 0, rpcUrl), cacheTime: 6e4, batch: { multicall: { wait: 50 } } }); const isDeployed2 = await _permissionless.isSmartAccountDeployed.call(void 0, publicClient, _optionalChain([client, 'access', _45 => _45.account, 'optionalAccess', _46 => _46.address]) ); if (!isDeployed2) throw new Error("Can't remove owner on an undeployed safe"); const owners = await publicClient.readContract({ address: _optionalChain([client, 'access', _47 => _47.account, 'optionalAccess', _48 => _48.address]), abi: SafeAbi, functionName: "getOwners" }); const index = owners.findIndex( (ownerToFind) => ownerToFind === args.ownerToRemove ); if (index === -1) throw new Error(`${args.ownerToRemove} is not a safe owner`); let prevOwner; if (index !== 0) { prevOwner = _viem.getAddress.call(void 0, owners[index - 1]); } else { prevOwner = _viem.getAddress.call(void 0, _viem.pad.call(void 0, SAFE_SENTINEL_OWNERS, { size: 20 })); } return await client.sendTransaction({ to: _optionalChain([client, 'access', _49 => _49.account, 'optionalAccess', _50 => _50.address]), data: _viem.encodeFunctionData.call(void 0, { abi: SafeAbi, functionName: "removeOwner", args: [prevOwner, args.ownerToRemove, 1] }), maxFeePerBlobGas: 0n, blobs: [] }); }, async getOwners() { const publicClient = _viem.createPublicClient.call(void 0, { chain: client.chain, transport: _viem.http.call(void 0, rpcUrl), cacheTime: 6e4, batch: { multicall: { wait: 50 } } }); const isDeployed2 = await _permissionless.isSmartAccountDeployed.call(void 0, publicClient, _optionalChain([client, 'access', _51 => _51.account, 'optionalAccess', _52 => _52.address]) ); if (!isDeployed2) return [_optionalChain([client, 'access', _53 => _53.account, 'optionalAccess', _54 => _54.signerAddress])]; return await publicClient.readContract({ address: _optionalChain([client, 'access', _55 => _55.account, 'optionalAccess', _56 => _56.address]), abi: SafeAbi, functionName: "getOwners" }); }, async getEnrichedOwners() { const publicClient = _viem.createPublicClient.call(void 0, { chain: client.chain, transport: _viem.http.call(void 0, rpcUrl), cacheTime: 6e4, batch: { multicall: { wait: 50 } } }); const isDeployed2 = await _permissionless.isSmartAccountDeployed.call(void 0, publicClient, _optionalChain([client, 'access', _57 => _57.account, 'optionalAccess', _58 => _58.address]) ); const api = _optionalChain([client, 'optionalAccess', _59 => _59.account, 'optionalAccess', _60 => _60.getConnectApi, 'call', _61 => _61()]); const webAuthnSigners = await _optionalChain([api, 'optionalAccess', _62 => _62.getWebAuthnSignersByWalletAddressAndChain, 'call', _63 => _63( _optionalChain([client, 'access', _64 => _64.account, 'optionalAccess', _65 => _65.address]), _optionalChain([publicClient, 'access', _66 => _66.chain, 'optionalAccess', _67 => _67.id]) )]); if (!isDeployed2) { if (webAuthnSigners.length === 0) { return [ { address: _optionalChain([client, 'access', _68 => _68.account, 'optionalAccess', _69 => _69.signerAddress]) } ]; } return [ { address: webAuthnSigners[0].signerAddress, deviceData: webAuthnSigners[0].deviceData, creationDate: webAuthnSigners[0].creationDate } ]; } const owners = await publicClient.readContract({ address: _optionalChain([client, 'access', _70 => _70.account, 'optionalAccess', _71 => _71.address]), abi: SafeAbi, functionName: "getOwners" }); const enrichedOwners = owners.map((owner) => { const webauthSigner = webAuthnSigners.find( (webauthnSigner) => webauthnSigner.signerAddress === owner ); if (webauthSigner) { return { address: owner, deviceData: webauthSigner.deviceData, creationDate: webauthSigner.creationDate }; } return { address: owner }; }); return enrichedOwners; } }); // src/core/actions/accounts/safe/sessionKeys/sessionKeyActions.ts var defaultValidAfter = /* @__PURE__ */ new Date(); var defaultValidUntil = new Date( defaultValidAfter.getTime() + 1e3 * 60 * 60 * 1 ); var safeSessionKeyActions = (rpcUrl) => (client) => ({ async addSessionKey({ validAfter = defaultValidAfter, validUntil = defaultValidUntil, destinations }) { if (destinations.length === 0) throw new Error("destinations cannot be empty"); if (!_optionalChain([client, 'access', _72 => _72.account, 'optionalAccess', _73 => _73.sessionKeysEnabled])) throw new Error("Session Keys are not enabled"); const fallbackEoaSigner = await createFallbackEoaSigner(); await encryptSessionKeyInStorage( _optionalChain([client, 'access', _74 => _74.account, 'optionalAccess', _75 => _75.address]), fallbackEoaSigner.privateKey ); return await client.sendTransaction({ to: _optionalChain([client, 'access', _76 => _76.account, 'optionalAccess', _77 => _77.address]), data: _viem.encodeFunctionData.call(void 0, { abi: safe4337SessionKeyModuleAbi, functionName: "addSessionKey", args: [ fallbackEoaSigner.signer.address, validAfter, validUntil, destinations ] }), maxFeePerBlobGas: 0n, blobs: [] }); }, async revokeSessionKey(args) { if (!_optionalChain([client, 'access', _78 => _78.account, 'optionalAccess', _79 => _79.sessionKeysEnabled])) throw new Error("Session Keys are not enabled"); const txHash = await client.sendTransaction({ to: _optionalChain([client, 'access', _80 => _80.account, 'optionalAccess', _81 => _81.address]), data: _viem.encodeFunctionData.call(void 0, { abi: safe4337SessionKeyModuleAbi, functionName: "revokeSessionKey", args: [args.sessionKey] }), maxFeePerBlobGas: 0n, blobs: [] }); deleteSessionKeyInStorage(_optionalChain([client, 'access', _82 => _82.account, 'optionalAccess', _83 => _83.address])); return txHash; }, async getSessionFromAddress(args) { return await querySessionFrom4337ModuleAddress({ chain: client.chain, smartAccountAddress: _optionalChain([client, 'access', _84 => _84.account, 'optionalAccess', _85 => _85.address]), safe4337SessionKeysModule: _optionalChain([client, 'optionalAccess', _86 => _86.account, 'optionalAccess', _87 => _87.safe4337SessionKeysModule]), sessionKey: args.sessionKey, rpcUrl }); }, async getCurrentSessionSignerAddress(args) { return getSessionKeySignerFromLocalStorage( args.smartAccountAddress ); }, async addWhitelistDestination(args) { if (!_optionalChain([client, 'access', _88 => _88.account, 'optionalAccess', _89 => _89.sessionKeysEnabled])) throw new Error("Session Keys are not enabled"); return await client.sendTransaction({ to: _optionalChain([client, 'access', _90 => _90.account, 'optionalAccess', _91 => _91.address]), data: _viem.encodeFunctionData.call(void 0, { abi: safe4337SessionKeyModuleAbi, functionName: "addWhitelistDestination", args: [args.sessionKey, args.destination] }), maxFeePerBlobGas: 0n, blobs: [] }); }, async removeWhitelistDestination(args) { return await client.sendTransaction({ to: _optionalChain([client, 'access', _92 => _92.account, 'optionalAccess', _93 => _93.address]), data: _viem.encodeFunctionData.call(void 0, { abi: safe4337SessionKeyModuleAbi, functionName: "removeWhitelistDestination", args: [args.sessionKey, args.destination] }), maxFeePerBlobGas: 0n, blobs: [] }); }, async isAddressWhitelistDestination(args) { return await queryIsWhitelistFrom4337ModuleAddress({ chain: client.chain, smartAccountAddress: _optionalChain([client, 'access', _94 => _94.account, 'optionalAccess', _95 => _95.address]), safe4337SessionKeysModule: _optionalChain([client, 'optionalAccess', _96 => _96.account, 'optionalAccess', _97 => _97.safe4337SessionKeysModule]), sessionKey: args.sessionKey, targetAddress: args.targetAddress, rpcUrl }); } }); // src/core/clients/accounts/safe/createClient.ts // src/core/clients/decorators/cometh.ts // src/core/actions/accounts/estimateGas.ts var _pimlico = require('permissionless/actions/pimlico'); var _utils = require('viem/utils'); var estimateGas = async (client, args) => { const { userOperation } = args; const estimateGas2 = await _utils.getAction.call(void 0, client, _permissionless.estimateUserOperationGas, "estimateUserOperationGas" )({ userOperation, entryPoint: _permissionless.ENTRYPOINT_ADDRESS_V07 }); const maxGasPriceResult = await _utils.getAction.call(void 0, client, _pimlico.getUserOperationGasPrice, "getUserOperationGasPrice" )({}); return { callGasLimit: estimateGas2.callGasLimit, verificationGasLimit: estimateGas2.verificationGasLimit, preVerificationGas: estimateGas2.preVerificationGas, paymasterVerificationGasLimit: estimateGas2.paymasterVerificationGasLimit, paymasterPostOpGasLimit: estimateGas2.paymasterPostOpGasLimit, maxFeePerGas: maxGasPriceResult.fast.maxFeePerGas, maxPriorityFeePerGas: maxGasPriceResult.fast.maxPriorityFeePerGas }; }; // src/core/accounts/safe/abi/safeWebauthnSignerFactory.ts var safeWebauthnSignerFactory = [ { inputs: [], stateMutability: "nonpayable", type: "constructor" }, { anonymous: false, inputs: [ { indexed: false, internalType: "address", name: "signer", type: "address" }, { indexed: false, internalType: "uint256", name: "x", type: "uint256" }, { indexed: false, internalType: "uint256", name: "y", type: "uint256" }, { indexed: false, internalType: "P256.Verifiers", name: "verifiers", type: "uint176" } ], name: "Created", type: "event" }, { inputs: [], name: "SINGLETON", outputs: [ { internalType: "contract SafeWebAuthnSignerSingleton", name: "", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "uint256", name: "x", type: "uint256" }, { internalType: "uint256", name: "y", type: "uint256" }, { internalType: "P256.Verifiers", name: "verifiers", type: "uint176" } ], name: "createSigner", outputs: [ { internalType: "address", name: "signer", type: "address" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "uint256", name: "x", type: "uint256" }, { internalType: "uint256", name: "y", type: "uint256" }, { internalType: "P256.Verifiers", name: "verifiers", type: "uint176" } ], name: "getSigner", outputs: [ { internalType: "address", name: "signer", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "bytes32", name: "message", type: "bytes32" }, { internalType: "bytes", name: "signature", type: "bytes" }, { internalType: "uint256", name: "x", type: "uint256" }, { internalType: "uint256", name: "y", type: "uint256" }, { internalType: "P256.Verifiers", name: "verifiers", type: "uint176" } ], name: "isValidSignatureForSigner", outputs: [ { internalType: "bytes4", name: "magicValue", type: "bytes4" } ], stateMutability: "view", type: "function" } ]; // src/core/actions/accounts/safe/owners/addDeviceActions.ts var _smartAccount = require('permissionless/actions/smartAccount'); async function validateAddDevice(client, args) { const { signer, middleware } = args; const api = _optionalChain([client, 'optionalAccess', _98 => _98.account, 'optionalAccess', _99 => _99.getConnectApi, 'call', _100 => _100()]); if (!api) throw new Error("No api found"); const addOwnerCalldata = _utils.encodeFunctionData.call(void 0, { abi: SafeAbi, functionName: "addOwnerWithThreshold", args: [signer.signerAddress, 1] }); const smartAccountAddress = _optionalChain([client, 'access', _101 => _101.account, 'optionalAccess', _102 => _102.address]); if (!smartAccountAddress) throw new Error("No smart account address found"); const txs = [ { to: smartAccountAddress, value: BigInt(0), data: addOwnerCalldata } ]; if (signer.publicKeyX && signer.publicKeyY) { const { p256Verifier: safeP256VerifierAddress, safeWebAuthnSignerFactory } = (await getProjectParamsByChain({ api, chain: client.chain })).safeContractParams; const deployWebAuthnSignerCalldata = _utils.encodeFunctionData.call(void 0, { abi: safeWebauthnSignerFactory, functionName: "createSigner", args: [ signer.publicKeyX, signer.publicKeyY, safeP256VerifierAddress ] }); txs.unshift({ to: safeWebAuthnSignerFactory, value: BigInt(0), data: deployWebAuthnSignerCalldata }); } const hash = await _utils.getAction.call(void 0, client, _smartAccount.sendTransactions, "sendTransactions" )({ transactions: txs, middleware }); if (signer.publicKeyX && signer.publicKeyY && signer.publicKeyId) { await api.createWebAuthnSigner({ chainId: _optionalChain([client, 'access', _103 => _103.chain, 'optionalAccess', _104 => _104.id]), walletAddress: smartAccountAddress, publicKeyId: signer.publicKeyId, publicKeyX: signer.publicKeyX, publicKeyY: signer.publicKeyY, deviceData: signer.deviceData, signerAddress: signer.signerAddress, isSharedWebAuthnSigner: false }); } return hash; } // src/core/services/delayModuleService.ts // src/core/accounts/safe/abi/delayModule.ts var delayModuleABI = [ { inputs: [ { internalType: "address", name: "_owner", type: "address" }, { internalType: "address", name: "_avatar", type: "address" }, { internalType: "address", name: "_target", type: "address" }, { internalType: "uint256", name: "_cooldown", type: "uint256" }, { internalType: "uint256", name: "_expiration", type: "uint256" } ], stateMutability: "nonpayable", type: "constructor" }, { inputs: [ { internalType: "address", name: "module", type: "address" } ], name: "AlreadyDisabledModule", type: "error" }, { inputs: [ { internalType: "address", name: "module", type: "address" } ], name: "AlreadyEnabledModule", type: "error" }, { inputs: [ { internalType: "bytes32", name: "", type: "bytes32" } ], name: "HashAlreadyConsumed", type: "error" }, { inputs: [], name: "InvalidInitialization", type: "error" }, { inputs: [ { internalType: "address", name: "module", type: "address" } ], name: "InvalidModule", type: "error" }, { inputs: [], name: "InvalidPageSize", type: "error" }, { inputs: [ { internalType: "address", name: "sender", type: "address" } ], name: "NotAuthorized", type: "error" }, { inputs: [], name: "NotInitializing", type: "error" }, { inputs: [ { internalType: "address", name: "owner", type: "address" } ], name: "OwnableInvalidOwner", type: "error" }, { inputs: [ { internalType: "address", name: "account", type: "address" } ], name: "OwnableUnauthorizedAccount", type: "error" }, { inputs: [], name: "SetupModulesAlreadyCalled", type: "error" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "previousAvatar", type: "address" }, { indexed: true, internalType: "address", name: "newAvatar", type: "address" } ], name: "AvatarSet", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "initiator", type: "address" }, { indexed: true, internalType: "address", name: "owner", type: "address" }, { indexed: true, internalType: "address", name: "avatar", type: "address" }, { indexed: false, internalType: "address", name: "target", type: "address" } ], name: "DelaySetup", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "address", name: "module", type: "address" } ], name: "DisabledModule", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "address", name: "module", type: "address" } ], name: "EnabledModule", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "module", type: "address" } ], name: "ExecutionFromModuleFailure", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "module", type: "address" } ], name: "ExecutionFromModuleSuccess", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "bytes32", name: "", type: "bytes32" } ], name: "HashExecuted", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "bytes32", name: "", type: "bytes32" } ], name: "HashInvalidated", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "uint64", name: "version", type: "uint64" } ], name: "Initialized", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "previousOwner", type: "address" }, { indexed: true, internalType: "address", name: "newOwner", type: "address" } ], name: "OwnershipTransferred", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "previousTarget", type: "address" }, { indexed: true, internalType: "address", name: "newTarget", type: "address" } ], name: "TargetSet", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "uint256", name: "queueNonce", type: "uint256" }, { indexed: true, internalType: "bytes32", name: "txHash", type: "bytes32" }, { indexed: false, internalType: "address", name: "to", type: "address" }, { indexed: false, internalType: "uint256", name: "value", type: "uint256" }, { indexed: false, internalType: "bytes", name: "data", type: "bytes" }, { indexed: false, internalType: "enum Enum.Operation", name: "operation", type: "uint8" } ], name: "TransactionAdded", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "uint256", name: "cooldown", type: "uint256" } ], name: "TxCooldownSet", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "uint256", name: "expiration", type: "uint256" } ], name: "TxExpirationSet", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "uint256", name: "nonce", type: "uint256" } ], name: "TxNonceSet", type: "event" }, { inputs: [], name: "avatar", outputs: [ { internalType: "address", name: "", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "", type: "address" }, { internalType: "bytes32", name: "", type: "bytes32" } ], name: "consumed", outputs: [ { internalType: "bool", name: "", type: "bool" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "prevModule", type: "address" }, { internalType: "address", name: "module", type: "address" } ], name: "disableModule", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "module", type: "address" } ], name: "enableModule", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "to", type: "address" }, { internalType: "uint256", name: "value", type: "uint256" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "enum Enum.Operation", name: "operation", type: "uint8" } ], name: "execTransactionFromModule", outputs: [ { internalType: "bool", name: "success", type: "bool" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "to", type: "address" }, { internalType: "uint256", name: "value", type: "uint256" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "enum Enum.Operation", name: "operation", type: "uint8" } ], name: "execTransactionFromModuleReturnData", outputs: [ { internalType: "bool", name: "success", type: "bool" }, { internalType: "bytes", name: "returnData", type: "bytes" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "to", type: "address" }, { internalType: "uint256", name: "value", type: "uint256" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "enum Enum.Operation", name: "operation", type: "uint8" } ], name: "executeNextTx", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "start", type: "address" }, { internalType: "uint256", name: "pageSize", type: "uint256" } ], name: "getModulesPaginated", outputs: [ { internalType: "address[]", name: "array", type: "address[]" }, { internalType: "address", name: "next", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "to", type: "address" }, { internalType: "uint256", name: "value", type: "uint256" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "enum Enum.Operation", name: "operation", type: "uint8" } ], name: "getTransactionHash", outputs: [ { internalType: "bytes32", name: "", type: "bytes32" } ], stateMutability: "pure", type: "function" }, { inputs: [ { internalType: "uint256", name: "_nonce", type: "uint256" } ], name: "getTxCreatedAt", outputs: [ { internalType: "uint256", name: "", type: "uint256" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "uint256", name: "_nonce", type: "uint256" } ], name: "getTxHash", outputs: [ { internalType: "bytes32", name: "", type: "bytes32" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "bytes32", name: "hash", type: "bytes32" } ], name: "invalidate", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "_module", type: "address" } ], name: "isModuleEnabled", outputs: [ { internalType: "bool", name: "", type: "bool" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "bytes32", name: "salt", type: "bytes32" } ], name: "moduleTxHash", outputs: [ { internalType: "bytes32", name: "", type: "bytes32" } ], stateMutability: "view", type: "function" }, { inputs: [], name: "owner", outputs: [ { internalType: "address", name: "", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [], name: "queueNonce", outputs: [ { internalType: "uint256", name: "", type: "uint256" } ], stateMutability: "view", type: "function" }, { inputs: [], name: "renounceOwnership", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "_avatar", type: "address" } ], name: "setAvatar", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "_target", type: "address" } ], name: "setTarget", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "uint256", name: "_txCooldown", type: "uint256" } ], name: "setTxCooldown", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "uint256", name: "_txExpiration", type: "uint256" } ], name: "setTxExpiration", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "uint256", name: "_txNonce", type: "uint256" } ], name: "setTxNonce", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "bytes", name: "initParams", type: "bytes" } ], name: "setUp", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [], name: "skipExpired", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [], name: "target", outputs: [ { internalType: "address", name: "", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "newOwner", type: "address" } ], name: "transferOwnership", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [], name: "txCooldown", outputs: [ { internalType: "uint256", name: "", type: "uint256" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "uint256", name: "", type: "uint256" } ], name: "txCreatedAt", outputs: [ { internalType: "uint256", name: "", type: "uint256" } ], stateMutability: "view", type: "function" }, { inputs: [], name: "txExpiration", outputs: [ { internalType: "uint256", name: "", type: "uint256" } ], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "uint256", name: "", type: "uint256" } ], name: "txHash", outputs: [ { internalType: "bytes32", name: "", type: "bytes32" } ], stateMutability: "view", type: "function" }, { inputs: [], name: "txNonce", outputs: [ { internalType: "uint256", name: "", type: "uint256" } ], stateMutability: "view", type: "function" } ]; // src/core/accounts/safe/abi/delayModuleFactory.ts var delayModuleFactoryABI = [ { inputs: [], name: "FailedInitialization", type: "error" }, { inputs: [ { internalType: "address", name: "address_", type: "address" } ], name: "TakenAddress", type: "error" }, { inputs: [{ internalType: "address", name: "target", type: "address" }], name: "TargetHasNoCode", type: "error" }, { inputs: [{ internalType: "address", name: "target", type: "address" }], name: "ZeroAddress", type: "error" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "proxy", type: "address" }, { indexed: true, internalType: "address", name: "masterCopy", type: "address" } ], name: "ModuleProxyCreation", type: "event" }, { inputs: [ { internalType: "address", name: "masterCopy", type: "address" }, { internalType: "bytes", name: "initializer", type: "bytes" }, { internalType: "uint256", name: "saltNonce", type: "uint256" } ], name: "deployModule", outputs: [{ internalType: "address", name: "proxy", type: "address" }], stateMutability: "nonpayable", type: "function" } ]; // src/core/services/delayModuleService.ts var isDeployed = async ({ delayAddress, client }) => { try { const bytecode = await _actions.getCode.call(void 0, client, { address: delayAddress }); if (!bytecode) return false; return true; } catch (e7) { return false; } }; var getDelayAddress = (safe, context) => { const cooldown = context.recoveryCooldown; const expiration = context.recoveryExpiration; const moduleAddress = context.delayModuleAddress; const factoryAddress = context.moduleFactoryAddress; const args = _viem.encodeFunctionData.call(void 0, { abi: delayModuleABI, functionName: "setUp", args: [ _viem.encodeAbiParameters.call(void 0, _viem.parseAbiParameters.call(void 0, "address, address, address, uint256, uint256" ), [safe, safe, safe, BigInt(cooldown), BigInt(expiration)] ) ] }); const initializer = args; const code = _viem.concat.call(void 0, [ "0x602d8060093d393df3363d3d373d3d3d363d73", moduleAddress.slice(2), "5af43d82803e903d91602b57fd5bf3" ]); const salt = _viem.keccak256.call(void 0, _viem.concat.call(void 0, [_viem.keccak256.call(void 0, initializer), _viem.pad.call(void 0, safe, { size: 32 })]) ); return _viem.getContractAddress.call(void 0, { bytecode: code, from: factoryAddress, salt, opcode: "CREATE2" }); }; var createSetTxNonceFunction = async (proxyDelayAddress, client) => { const txNonce = await client.readContract({ address: proxyDelayAddress, abi: delayModuleABI, functionName: "txNonce" }); const newNonce = txNonce + 1n; return { to: proxyDelayAddress, value: "0x0", data: _viem.encodeFunctionData.call(void 0, { abi: delayModuleABI, functionName: "setTxNonce", args: [newNonce] }), operation: 0 }; }; var getCurrentRecoveryParams = async (delayModuleAddress, chain, rpcUrl) => { const client = _viem.createPublicClient.call(void 0, { chain, transport: _viem.http.call(void 0, rpcUrl), cacheTime: 6e4, batch: { multicall: { wait: 50 } } }); const txNonce = await client.readContract({ address: delayModuleAddress, abi: delayModuleABI, functionName: "txNonce" }); const [txCreatedAt, txHash] = await Promise.all([ client.readContract({ address: delayModuleAddress, abi: delayModuleABI, functionName: "getTxCreatedAt", args: [txNonce] }), client.readContract({ address: delayModuleAddress, abi: delayModuleABI, functionName: "getTxHash", args: [txNonce] }) ]); return { txCreatedAt, txHash }; }; var isQueueEmpty = async (moduleAddress, chain, rpcUrl) => { const publicClient = _viem.createPublicClient.call(void 0, { chain, transport: _viem.http.call(void 0, rpcUrl), cacheTime: 6e4, batch: { multicall: { wait: 50 } } }); const [txNonce, queueNonce] = await Promise.all([ publicClient.readContract({ address: moduleAddress, abi: delayModuleABI, functionName: "txNonce" }), publicClient.readContract({ address: moduleAddress, abi: delayModuleABI, functionName: "queueNonce" }) ]); return txNonce === queueNonce; }; var setUpDelayModule = async ({ safe, cooldown, expiration }) => { const setUpArgs = _viem.encodeAbiParameters.call(void 0, _viem.parseAbiParameters.call(void 0, [ "address", "address", "address", "uint256", "uint256" ]), [safe, safe, safe, BigInt(cooldown), BigInt(expiration)] ); return _viem.encodeFunctionData.call(void 0, { abi: delayModuleABI, functionName: "setUp", args: [setUpArgs] }); }; var encodeDeployDelayModule = ({ singletonDelayModule, initializer, safe }) => { return _viem.encodeFunctionData.call(void 0, { abi: delayModuleFactoryABI, functionName: "deployModule", args: [singletonDelayModule, initializer, BigInt(safe)] }); }; var getGuardianAddress = async ({ delayAddress, chain, rpcUrl }) => { const client = _viem.createPublicClient.call(void 0, { chain, transport: _viem.http.call(void 0, rpcUrl) }); const modulesPaginated = await client.readContract({ address: delayAddress, abi: delayModuleABI, functionName: "getModulesPaginated", args: [SENTINEL_MODULES, 1n] }); return modulesPaginated[0][0]; }; var delayModuleService_default = { getDelayAddress, isDeployed, createSetTxNonceFunction, getCurrentRecoveryParams, isQueueEmpty, setUpDelayModule, encodeDeployDelayModule, getGuardianAddress }; // src/core/actions/accounts/safe/recovery/cancelRecoveryRequest.ts async function cancelRecoveryRequest(client, args) { const { rpcUrl, middleware } = args; const smartAccounAddress = _optionalChain([client, 'access', _105 => _105.account, 'optionalAccess', _106 => _106.address]); const publicClient = _viem.createPublicClient.call(void 0, { chain: client.chain, transport: _viem.http.call(void 0, rpcUrl), cacheTime: 6e4, batch: { multicall: { wait: 50 } } }); const api = _optionalChain([client, 'optionalAccess', _107 => _107.account, 'optionalAccess', _108 => _108.getConnectApi, 'call', _109 => _109()]); if (!api) throw new Error("No api found"); const projectParams = await getProjectParamsByChain({ api, chain: client.chain }); if (!projectParams) throw Error("Error fetching project params"); const { moduleFactoryAddress, delayModuleAddress, recoveryCooldown, recoveryExpiration } = projectParams.recoveryParams; const delayAddress = await delayModuleService_default.getDelayAddress( smartAccounAddress, { moduleFactoryAddress, delayModuleAddress, recoveryCooldown, recoveryExpiration } ); const isDelayModuleDeployed = await delayModuleService_default.isDeployed({ delayAddress, client: publicClient }); if (!isDelayModuleDeployed) throw Error("Recovery not active"); const recoveryRequest = await delayModuleService_default.getCurrentRecoveryParams( delayAddress, client.chain, rpcUrl ); if (!recoveryRequest) throw new NoRecoveryRequestFoundError(); if (recoveryRequest.txHash === _viem.zeroHash) throw new NoRecoveryRequestFoundError(); const updateNonceTx = await delayModuleService_default.createSetTxNonceFunction( delayAddress, publicClient ); const hash = await _utils.getAction.call(void 0, client, _smartAccount.sendTransactions, "sendTransactions" )({ transactions: updateNonceTx, middleware }); return hash; } // src/core/actions/accounts/safe/recovery/getRecoveryRequest.ts async function getRecoveryRequest(client, args = {}) { const { rpcUrl } = args; const smartAccounAddress = _optionalChain([client, 'access', _110 => _110.account, 'optionalAccess', _111 => _111.address]); const publicClient = _viem.createPublicClient.call(void 0, { chain: client.chain, transport: _viem.http.call(void 0, rpcUrl), cacheTime: 6e4, batch: { multicall: { wait: 50 } } }); const api = _optionalChain([client, 'optionalAccess', _112 => _112.account, 'optionalAccess', _113 => _113.getConnectApi, 'call', _114 => _114()]); if (!api) throw new Error("No api found"); const projectParams = await getProjectParamsByChain({ api, chain: client.chain }); if (!projectParams) throw Error("Error fetching project params"); const { moduleFactoryAddress, delayModuleAddress, recoveryCooldown, recoveryExpiration } = projectParams.recoveryParams; const delayAddress = await delayModuleService_default.getDelayAddress( smartAccounAddress, { moduleFactoryAddress, delayModuleAddress, recoveryCooldown, recoveryExpiration } ); const isDelayModuleDeployed = await delayModuleService_default.isDeployed({ delayAddress, client: publicClient }); if (!isDelayModuleDeployed) throw new Error("Recovery has not been setup"); const isRecoveryQueueEmpty = await delayModuleService_default.isQueueEmpty( delayAddress, client.chain, rpcUrl ); if (isRecoveryQueueEmpty) return void 0; return await delayModuleService_default.getCurrentRecoveryParams( delayAddress, client.chain, rpcUrl ); } // src/core/actions/accounts/safe/recovery/isRecoveryActive.ts async function isRecoveryActive(client, args = {}) { const { rpcUrl } = args; const smartAccounAddress = _optionalChain([client, 'access', _115 => _115.account, 'optionalAccess', _116 => _116.address]); const publicClient = _viem.createPublicClient.call(void 0, { chain: client.chain, transport: _viem.http.call(void 0, rpcUrl), cacheTime: 6e4, batch: { multicall: { wait: 50 } } }); const api = _optionalChain([client, 'optionalAccess', _117 => _117.account, 'optionalAccess', _118 => _118.getConnectApi, 'call', _119 => _119()]); if (!api) throw new Error("No api found"); const projectParams = await getProjectParamsByChain({ api, chain: client.chain }); if (!projectParams) throw Error("Error fetching project params"); const { moduleFactoryAddress, delayModuleAddress, recoveryCooldown, recoveryExpiration } = projectParams.recoveryParams; const delayAddress = await delayModuleService_default.getDelayAddress( smartAccounAddress, { moduleFactoryAddress, delayModuleAddress, recoveryCooldown, recoveryExpiration } ); let contractGuardian = null; const isDelayModuleDeployed = await delayModuleService_default.isDeployed({ delayAddress, client: publicClient }); if (isDelayModuleDeployed) { contractGuardian = await delayModuleService_default.getGuardianAddress({ delayAddress, chain: client.chain, rpcUrl }); } return { isDelayModuleDeployed, guardianAddress: contractGuardian }; } // src/core/actions/accounts/safe/recovery/setUpRecoveryModule.ts async function setUpRecoveryModule(client, args) { const { rpcUrl, middleware } = args; const smartAccounAddress = _optionalChain([client, 'access', _120 => _120.account, 'optionalAccess', _121 => _121.address]); const publicClient = _viem.createPublicClient.call(void 0, { chain: client.chain, transport: _viem.http.call(void 0, rpcUrl), cacheTime: 6e4, batch: { multicall: { wait: 50 } } }); const api = _optionalChain([client, 'optionalAccess', _122 => _122.account, 'optionalAccess', _123 => _123.getConnectApi, 'call', _124 => _124()]); if (!api) throw new Error("No api found"); const projectParams = await getProjectParamsByChain({ api, chain: client.chain }); if (!projectParams) throw Error("Error fetching project params"); const { moduleFactoryAddress, delayModuleAddress, recoveryCooldown, recoveryExpiration, guardianAddress } = projectParams.recoveryParams; const delayAddress = await delayModuleService_default.getDelayAddress( smartAccounAddress, { moduleFactoryAddress, delayModuleAddress, recoveryCooldown, recoveryExpiration } ); const isDelayModuleDeployed = await delayModuleService_default.isDeployed({ delayAddress, client: publicClient }); if (isDelayModuleDeployed) throw Error("Recovery already setup"); const delayModuleInitializer = await delayModuleService_default.setUpDelayModule({ safe: smartAccounAddress, cooldown: recoveryCooldown, expiration: recoveryExpiration }); const setUpDelayTx = [ { to: moduleFactoryAddress, value: BigInt(0), data: await delayModuleService_default.encodeDeployDelayModule({ singletonDelayModule: delayModuleAddress, initializer: delayModuleInitializer, safe: smartAccounAddress }) }, { to: smartAccounAddress, value: BigInt(0), data: _viem.encodeFunctionData.call(void 0, { abi: _viem.parseAbi.call(void 0, ["function enableModule(address module) public"]), functionName: "enableModule", args: [delayAddress] }) }, { to: delayAddress, value: BigInt(0), data: _viem.encodeFunctionData.call(void 0, { abi: _viem.parseAbi.call(void 0, ["function enableModule(address module) public"]), functionName: "enableModule", args: [guardianAddress] }) } ]; const hash = await _utils.getAction.call(void 0, client, _smartAccount.sendTransactions, "sendTransactions" )({ transactions: setUpDelayTx, middleware }); return hash; } // src/core/actions/accounts/safe/sessionKeys/sendTransactionWithSessionKey.ts async function sendTransactionWithSessionKey(client, args) { const { data, maxFeePerGas, maxPriorityFeePerGas, to, value, nonce, middleware } = args; const account = client.account; const smartAccountAddress = _optionalChain([client, 'access', _125 => _125.account, 'optionalAccess', _126 => _126.address]); if (!smartAccountAddress) throw new Error("No smart account address found"); const callData = await account.encodeCallData({ to, value: value || BigInt(0), data: data || "0x" }); const userOperation = await _utils.getAction.call(void 0, client, _smartAccount.prepareUserOperationRequest, "prepareUserOperationRequest" )({ userOperation: { sender: account.address, maxFeePerGas: maxFeePerGas || BigInt(0), maxPriorityFeePerGas: maxPriorityFeePerGas || BigInt(0), callData, nonce }, account, middleware }); userOperation.signature = await account.signUserOperationWithSessionKey( userOperation ); try { const userOperationHash = await client.request({ method: "eth_sendUserOperation", params: [ _permissionless.deepHexlify.call(void 0, userOperation), ENTRYPOINT_ADDRESS_V07 ] }); const userOperationReceipt = await _utils.getAction.call(void 0, client, _permissionless.waitForUserOperationReceipt, "waitForUserOperationReceipt" )({ hash: userOperationHash }); return _optionalChain([userOperationReceipt, 'optionalAccess', _127 => _127.receipt, 'access', _128 => _128.transactionHash]); } catch (err) { throw new Error(`Error sending user operation: ${err}`); } } // src/core/actions/accounts/safe/sessionKeys/sendTransactionsWithSessionKey.ts async function sendTransactionsWithSessionKey(client, args) { const { transactions, middleware, maxFeePerGas, maxPriorityFeePerGas, nonce } = args; const account = client.account; const smartAccountAddress = _optionalChain([client, 'access', _129 => _129.account, 'optionalAccess', _130 => _130.address]); if (!smartAccountAddress) throw new Error("No smart account address found"); const callData = await account.encodeCallData( transactions.map( ({ to, value, data }) => { if (!to) throw new Error("Missing to address"); return { to, value: value || BigInt(0), data: data || "0x" }; } ) ); const userOperation = await _utils.getAction.call(void 0, client, _smartAccount.prepareUserOperationRequest, "prepareUserOperationRequest" )({ userOperation: { sender: account.address, maxFeePerGas: maxFeePerGas || BigInt(0), maxPriorityFeePerGas: maxPriorityFeePerGas || BigInt(0), callData, nonce }, account, middleware }); userOperation.signature = await account.signUserOperationWithSessionKey( userOperation ); try { const userOperationHash = await client.request({ method: "eth_sendUserOperation", params: [ _permissionless.deepHexlify.call(void 0, userOperation), ENTRYPOINT_ADDRESS_V07 ] }); const userOperationReceipt = await _utils.getAction.call(void 0, client, _permissionless.waitForUserOperationReceipt, "waitForUserOperationReceipt" )({ hash: userOperationHash }); return _optionalChain([userOperationReceipt, 'optionalAccess', _131 => _131.receipt, 'access', _132 => _132.transactionHash]); } catch (err) { throw new Error(`Error sending user operation: ${err}`); } } // src/core/actions/accounts/safe/verifySignature.ts async function verifySignature(client, args) { const { message, signature } = args; const api = _optionalChain([client, 'optionalAccess', _133 => _133.account, 'optionalAccess', _134 => _134.getConnectApi, 'call', _135 => _135()]); const smartAccountAddress = _optionalChain([client, 'access', _136 => _136.account, 'optionalAccess', _137 => _137.address]); if (!api) throw new Error("No api found"); const isValidSignature = await api.isValidSignature( smartAccountAddress, message, signature, _optionalChain([client, 'optionalAccess', _138 => _138.chain, 'optionalAccess', _139 => _139.id]) ); return isValidSignature; } // src/core/clients/decorators/cometh.ts function comethAccountClientActions({ middleware }) { return (client) => ({ ..._permissionless.smartAccountActions.call(void 0, { middleware })(client), estimateUserOperationGas: (args, stateOverrides) => _permissionless.estimateUserOperationGas.call(void 0, client, { ...args, entryPoint: _permissionless.ENTRYPOINT_ADDRESS_V07 }, stateOverrides ), validateAddDevice: (args) => validateAddDevice( client, { ...args, middleware } ), setUpRecoveryModule: (args) => setUpRecoveryModule( client, { ...args, middleware } ), isRecoveryActive: (args) => isRecoveryActive(client, { ...args, middleware }), getRecoveryRequest: (args) => getRecoveryRequest( client, { ...args, middleware } ), cancelRecoveryRequest: (args) => cancelRecoveryRequest( client, { ...args, middleware } ), verifySignature: (args) => verifySignature(client, { ...args }), sendTransactionWithSessionKey: (args) => sendTransactionWithSessionKey(client, { ...args, middleware }), sendTransactionsWithSessionKey: (args) => sendTransactionsWithSessionKey(client, { ...args, middleware }), estimateGas: async (args) => estimateGas(client, args) }); } // src/core/clients/accounts/safe/createClient.ts function createSmartAccountClient(parameters) { const { key = "Account", name = "Cometh Smart Account Client", bundlerTransport } = parameters; const client = _viem.createClient.call(void 0, { ...parameters, key, name, transport: bundlerTransport, type: "smartAccountClient" }).extend( comethAccountClientActions({ middleware: parameters.middleware // biome-ignore lint/suspicious/noExplicitAny: TODO: remove any }) ); return client.extend(safeSessionKeyActions(parameters.rpcUrl)).extend( safeOwnerPluginActions(parameters.rpcUrl) ); } // src/core/actions/paymaster/gasPrice.ts var gasPrice = async (client, rpcUrl) => { const publicClient = _viem.createPublicClient.call(void 0, { chain: client.chain, transport: _viem.http.call(void 0, rpcUrl) }); const { maxFeePerGas } = await publicClient.estimateFeesPerGas(); return { maxFeePerGas: maxFeePerGas * 2n, maxPriorityFeePerGas: maxFeePerGas * 2n }; }; // src/core/actions/paymaster/sponsorUserOperation.ts var sponsorUserOperation = async (client, args) => { const response = await client.request({ method: "pm_sponsorUserOperation", params: [_permissionless.deepHexlify.call(void 0, args.userOperation), args.entryPoint] }); return { callGasLimit: BigInt(response.callGasLimit), verificationGasLimit: BigInt(response.verificationGasLimit), preVerificationGas: BigInt(response.preVerificationGas), paymaster: response.paymaster, paymasterVerificationGasLimit: BigInt( response.paymasterVerificationGasLimit ), paymasterPostOpGasLimit: BigInt( response.paymasterPostOpGasLimit ), paymasterData: response.paymasterData }; }; // src/core/clients/paymaster/createPaymasterClient.ts var comethPaymasterActions = (entryPointAddress, rpcUrl) => (client) => ({ sponsorUserOperation: async (args) => sponsorUserOperation(client, { ...args, entryPoint: entryPointAddress }), gasPrice: async () => gasPrice(client, rpcUrl) }); var createComethPaymasterClient = (parameters) => { const { key = "public", name = "Cometh Paymaster Client" } = parameters; const client = _viem.createClient.call(void 0, { ...parameters, key, name, type: "comethPaymasterClient" }); return client.extend( comethPaymasterActions(parameters.entryPoint, parameters.rpcUrl) ); }; // src/migrationKit/createLegacySafeSmartAccount.ts // src/migrationKit/abi/migration.ts var MigrationAbi = [ { inputs: [ { internalType: "address", name: "safeSingleton", type: "address" }, { internalType: "address", name: "safeL2Singleton", type: "address" }, { internalType: "address", name: "fallbackHandler", type: "address" } ], stateMutability: "nonpayable", type: "constructor" }, { anonymous: false, inputs: [ { indexed: false, internalType: "address", name: "singleton", type: "address" } ], name: "ChangedMasterCopy", type: "event" }, { inputs: [], name: "MIGRATION_SINGLETON", outputs: [ { internalType: "address", name: "", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [], name: "SAFE_FALLBACK_HANDLER", outputs: [ { internalType: "address", name: "", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [], name: "SAFE_L2_SINGLETON", outputs: [ { internalType: "address", name: "", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [], name: "SAFE_SINGLETON", outputs: [ { internalType: "address", name: "", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [], name: "migrateL2Singleton", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [], name: "migrateL2WithFallbackHandler", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [], name: "migrateSingleton", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [], name: "migrateWithFallbackHandler", outputs: [], stateMutability: "nonpayable", type: "function" } ]; // src/migrationKit/createLegacySafeSmartAccount.ts // src/migrationKit/safeLegacySigner/ecdsa/ecdsa.ts // src/migrationKit/types.ts var EIP712_SAFE_TX_TYPES = { SafeTx: [ { type: "address", name: "to" }, { type: "uint256", name: "value" }, { type: "bytes", name: "data" }, { type: "uint8", name: "operation" }, { type: "uint256", name: "safeTxGas" }, { type: "uint256", name: "baseGas" }, { type: "uint256", name: "gasPrice" }, { type: "address", name: "gasToken" }, { type: "address", name: "refundReceiver" }, { type: "uint256", name: "nonce" } ] }; // src/migrationKit/safeLegacySigner/ecdsa/ecdsa.ts async function safeLegacyECDSASigner(client, { signer, smartAccountAddress }) { const viemSigner = { ...signer, signTransaction: (_, __) => { throw new (0, _accounts3.SignTransactionNotSupportedBySmartAccount)(); } }; const account = _accounts.toAccount.call(void 0, { address: smartAccountAddress, async signMessage({ message }) { if (typeof message === "string") message = _viem.toHex.call(void 0, message); return _actions.signTypedData.call(void 0, client, { account: viemSigner, domain: { chainId: _optionalChain([client, 'access', _140 => _140.chain, 'optionalAccess', _141 => _141.id]), verifyingContract: smartAccountAddress }, types: EIP712_SAFE_MESSAGE_TYPE, primaryType: "SafeMessage", message: { message } }); }, // biome-ignore lint/suspicious/noExplicitAny: TODO async signTransaction(tx) { return _actions.signTypedData.call(void 0, client, { account: viemSigner, domain: { chainId: _optionalChain([client, 'access', _142 => _142.chain, 'optionalAccess', _143 => _143.id]), verifyingContract: smartAccountAddress }, types: EIP712_SAFE_TX_TYPES, primaryType: "SafeTx", message: { to: tx.to, value: _viem.hexToBigInt.call(void 0, tx.value).toString(), data: tx.data, operation: tx.operation, safeTxGas: _viem.hexToBigInt.call(void 0, tx.safeTxGas).toString(), baseGas: _viem.hexToBigInt.call(void 0, tx.baseGas).toString(), gasPrice: _viem.hexToBigInt.call(void 0, tx.gasPrice).toString(), gasToken: _nullishCoalesce(tx.gasToken, () => ( _viem.zeroAddress)), refundReceiver: _viem.zeroAddress, nonce: _viem.hexToBigInt.call(void 0, tx.nonce).toString() } }); }, async signTypedData(typedData) { return _actions.signTypedData.call(void 0, client, { account: viemSigner, ...typedData } ); } }); return { ...account, address: smartAccountAddress, source: "safeLegacyECDSASigner" }; } // src/migrationKit/safeLegacySigner/webauthn/webAuthn.ts // src/migrationKit/signers/passkeyService.ts // src/migrationKit/services/safe.ts // src/migrationKit/abi/safeLegacy.ts var SafeLegacyAbi = [ { anonymous: false, inputs: [ { indexed: false, internalType: "address", name: "owner", type: "address" } ], name: "AddedOwner", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "bytes32", name: "approvedHash", type: "bytes32" }, { indexed: true, internalType: "address", name: "owner", type: "address" } ], name: "ApproveHash", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "address", name: "handler", type: "address" } ], name: "ChangedFallbackHandler", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "address", name: "guard", type: "address" } ], name: "ChangedGuard", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "uint256", name: "threshold", type: "uint256" } ], name: "ChangedThreshold", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "address", name: "module", type: "address" } ], name: "DisabledModule", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "address", name: "module", type: "address" } ], name: "EnabledModule", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "bytes32", name: "txHash", type: "bytes32" }, { indexed: false, internalType: "uint256", name: "payment", type: "uint256" } ], name: "ExecutionFailure", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "module", type: "address" } ], name: "ExecutionFromModuleFailure", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "module", type: "address" } ], name: "ExecutionFromModuleSuccess", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "bytes32", name: "txHash", type: "bytes32" }, { indexed: false, internalType: "uint256", name: "payment", type: "uint256" } ], name: "ExecutionSuccess", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "address", name: "owner", type: "address" } ], name: "RemovedOwner", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "address", name: "module", type: "address" }, { indexed: false, internalType: "address", name: "to", type: "address" }, { indexed: false, internalType: "uint256", name: "value", type: "uint256" }, { indexed: false, internalType: "bytes", name: "data", type: "bytes" }, { indexed: false, internalType: "enum Enum.Operation", name: "operation", type: "uint8" } ], name: "SafeModuleTransaction", type: "event" }, { anonymous: false, inputs: [ { indexed: false, internalType: "address", name: "to", type: "address" }, { indexed: false, internalType: "uint256", name: "value", type: "uint256" }, { indexed: false, internalType: "bytes", name: "data", type: "bytes" }, { indexed: false, internalType: "enum Enum.Operation", name: "operation", type: "uint8" }, { indexed: false, internalType: "uint256", name: "safeTxGas", type: "uint256" }, { indexed: false, internalType: "uint256", name: "baseGas", type: "uint256" }, { indexed: false, internalType: "uint256", name: "gasPrice", type: "uint256" }, { indexed: false, internalType: "address", name: "gasToken", type: "address" }, { indexed: false, internalType: "address payable", name: "refundReceiver", type: "address" }, { indexed: false, internalType: "bytes", name: "signatures", type: "bytes" }, { indexed: false, internalType: "bytes", name: "additionalInfo", type: "bytes" } ], name: "SafeMultiSigTransaction", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "sender", type: "address" }, { indexed: false, internalType: "uint256", name: "value", type: "uint256" } ], name: "SafeReceived", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "address", name: "initiator", type: "address" }, { indexed: false, internalType: "address[]", name: "owners", type: "address[]" }, { indexed: false, internalType: "uint256", name: "threshold", type: "uint256" }, { indexed: false, internalType: "address", name: "initializer", type: "address" }, { indexed: false, internalType: "address", name: "fallbackHandler", type: "address" } ], name: "SafeSetup", type: "event" }, { anonymous: false, inputs: [ { indexed: true, internalType: "bytes32", name: "msgHash", type: "bytes32" } ], name: "SignMsg", type: "event" }, { stateMutability: "nonpayable", type: "fallback" }, { inputs: [], name: "VERSION", outputs: [{ internalType: "string", name: "", type: "string" }], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "owner", type: "address" }, { internalType: "uint256", name: "_threshold", type: "uint256" } ], name: "addOwnerWithThreshold", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "bytes32", name: "hashToApprove", type: "bytes32" } ], name: "approveHash", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "", type: "address" }, { internalType: "bytes32", name: "", type: "bytes32" } ], name: "approvedHashes", outputs: [{ internalType: "uint256", name: "", type: "uint256" }], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "uint256", name: "_threshold", type: "uint256" } ], name: "changeThreshold", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "bytes32", name: "dataHash", type: "bytes32" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "bytes", name: "signatures", type: "bytes" }, { internalType: "uint256", name: "requiredSignatures", type: "uint256" } ], name: "checkNSignatures", outputs: [], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "bytes32", name: "dataHash", type: "bytes32" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "bytes", name: "signatures", type: "bytes" } ], name: "checkSignatures", outputs: [], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "prevModule", type: "address" }, { internalType: "address", name: "module", type: "address" } ], name: "disableModule", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [], name: "domainSeparator", outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }], stateMutability: "view", type: "function" }, { inputs: [{ internalType: "address", name: "module", type: "address" }], name: "enableModule", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "to", type: "address" }, { internalType: "uint256", name: "value", type: "uint256" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "enum Enum.Operation", name: "operation", type: "uint8" }, { internalType: "uint256", name: "safeTxGas", type: "uint256" }, { internalType: "uint256", name: "baseGas", type: "uint256" }, { internalType: "uint256", name: "gasPrice", type: "uint256" }, { internalType: "address", name: "gasToken", type: "address" }, { internalType: "address", name: "refundReceiver", type: "address" }, { internalType: "uint256", name: "_nonce", type: "uint256" } ], name: "encodeTransactionData", outputs: [{ internalType: "bytes", name: "", type: "bytes" }], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "to", type: "address" }, { internalType: "uint256", name: "value", type: "uint256" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "enum Enum.Operation", name: "operation", type: "uint8" }, { internalType: "uint256", name: "safeTxGas", type: "uint256" }, { internalType: "uint256", name: "baseGas", type: "uint256" }, { internalType: "uint256", name: "gasPrice", type: "uint256" }, { internalType: "address", name: "gasToken", type: "address" }, { internalType: "address payable", name: "refundReceiver", type: "address" }, { internalType: "bytes", name: "signatures", type: "bytes" } ], name: "execTransaction", outputs: [{ internalType: "bool", name: "", type: "bool" }], stateMutability: "payable", type: "function" }, { inputs: [ { internalType: "address", name: "to", type: "address" }, { internalType: "uint256", name: "value", type: "uint256" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "enum Enum.Operation", name: "operation", type: "uint8" } ], name: "execTransactionFromModule", outputs: [{ internalType: "bool", name: "success", type: "bool" }], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "to", type: "address" }, { internalType: "uint256", name: "value", type: "uint256" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "enum Enum.Operation", name: "operation", type: "uint8" } ], name: "execTransactionFromModuleReturnData", outputs: [ { internalType: "bool", name: "success", type: "bool" }, { internalType: "bytes", name: "returnData", type: "bytes" } ], stateMutability: "nonpayable", type: "function" }, { inputs: [], name: "getChainId", outputs: [{ internalType: "uint256", name: "", type: "uint256" }], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "start", type: "address" }, { internalType: "uint256", name: "pageSize", type: "uint256" } ], name: "getModulesPaginated", outputs: [ { internalType: "address[]", name: "array", type: "address[]" }, { internalType: "address", name: "next", type: "address" } ], stateMutability: "view", type: "function" }, { inputs: [], name: "getOwners", outputs: [{ internalType: "address[]", name: "", type: "address[]" }], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "uint256", name: "offset", type: "uint256" }, { internalType: "uint256", name: "length", type: "uint256" } ], name: "getStorageAt", outputs: [{ internalType: "bytes", name: "", type: "bytes" }], stateMutability: "view", type: "function" }, { inputs: [], name: "getThreshold", outputs: [{ internalType: "uint256", name: "", type: "uint256" }], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "to", type: "address" }, { internalType: "uint256", name: "value", type: "uint256" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "enum Enum.Operation", name: "operation", type: "uint8" }, { internalType: "uint256", name: "safeTxGas", type: "uint256" }, { internalType: "uint256", name: "baseGas", type: "uint256" }, { internalType: "uint256", name: "gasPrice", type: "uint256" }, { internalType: "address", name: "gasToken", type: "address" }, { internalType: "address", name: "refundReceiver", type: "address" }, { internalType: "uint256", name: "_nonce", type: "uint256" } ], name: "getTransactionHash", outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }], stateMutability: "view", type: "function" }, { inputs: [{ internalType: "address", name: "module", type: "address" }], name: "isModuleEnabled", outputs: [{ internalType: "bool", name: "", type: "bool" }], stateMutability: "view", type: "function" }, { inputs: [{ internalType: "address", name: "owner", type: "address" }], name: "isOwner", outputs: [{ internalType: "bool", name: "", type: "bool" }], stateMutability: "view", type: "function" }, { inputs: [], name: "nonce", outputs: [{ internalType: "uint256", name: "", type: "uint256" }], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "prevOwner", type: "address" }, { internalType: "address", name: "owner", type: "address" }, { internalType: "uint256", name: "_threshold", type: "uint256" } ], name: "removeOwner", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "to", type: "address" }, { internalType: "uint256", name: "value", type: "uint256" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "enum Enum.Operation", name: "operation", type: "uint8" } ], name: "requiredTxGas", outputs: [{ internalType: "uint256", name: "", type: "uint256" }], stateMutability: "nonpayable", type: "function" }, { inputs: [{ internalType: "address", name: "handler", type: "address" }], name: "setFallbackHandler", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [{ internalType: "address", name: "guard", type: "address" }], name: "setGuard", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address[]", name: "_owners", type: "address[]" }, { internalType: "uint256", name: "_threshold", type: "uint256" }, { internalType: "address", name: "to", type: "address" }, { internalType: "bytes", name: "data", type: "bytes" }, { internalType: "address", name: "fallbackHandler", type: "address" }, { internalType: "address", name: "paymentToken", type: "address" }, { internalType: "uint256", name: "payment", type: "uint256" }, { internalType: "address payable", name: "paymentReceiver", type: "address" } ], name: "setup", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [{ internalType: "bytes32", name: "", type: "bytes32" }], name: "signedMessages", outputs: [{ internalType: "uint256", name: "", type: "uint256" }], stateMutability: "view", type: "function" }, { inputs: [ { internalType: "address", name: "targetContract", type: "address" }, { internalType: "bytes", name: "calldataPayload", type: "bytes" } ], name: "simulateAndRevert", outputs: [], stateMutability: "nonpayable", type: "function" }, { inputs: [ { internalType: "address", name: "prevOwner", type: "address" }, { internalType: "address", name: "oldOwner", type: "address" }, { internalType: "address", name: "newOwner", type: "address" } ], name: "swapOwner", outputs: [], stateMutability: "nonpayable", type: "function" }, { stateMutability: "payable", type: "receive" } ]; // src/migrationKit/services/safe.ts var isSafeOwner2 = async ({ safeAddress, signerAddress, chain, rpcUrl }) => { const publicClient = _viem.createPublicClient.call(void 0, { chain, transport: _viem.http.call(void 0, rpcUrl) }); const safe = _viem.getContract.call(void 0, { address: safeAddress, abi: SafeLegacyAbi, client: publicClient }); const isDeployed2 = await _permissionless.isSmartAccountDeployed.call(void 0, publicClient, safeAddress); if (!isDeployed2) throw new Error("Safe not deployed"); return await safe.read.isOwner([signerAddress]); }; var isSigner = async (signerAddress, walletAddress, chain, API2) => { try { const owner = await isSafeOwner2({ safeAddress: walletAddress, chain, signerAddress }); if (!owner) return false; } catch (e8) { const predictedWalletAddress = await API2.getWalletAddress(signerAddress); if (predictedWalletAddress !== walletAddress) return false; } return true; }; // src/migrationKit/signers/utils.ts var hexArrayStr2 = (array) => new Uint8Array(array).reduce( (acc, v) => acc + v.toString(16).padStart(2, "0"), "0x" ); var parseHex2 = (str) => { const matches = str.match(/[\da-f]{2}/gi); if (matches === null) { return new Uint8Array(); } return new Uint8Array(matches.map((h) => parseInt(h, 16))); }; var derToRS = (der) => { let offset = 3; let dataOffset; if (der[offset] === 33) { dataOffset = offset + 2; } else { dataOffset = offset + 1; } const r = der.slice(dataOffset, dataOffset + 32); offset = offset + der[offset] + 1 + 1; if (der[offset] === 33) { dataOffset = offset + 2; } else { dataOffset = offset + 1; } const s = der.slice(dataOffset, dataOffset + 32); return [r, s]; }; var findSequence = (arr, seq) => { for (let i = 0; i < arr.length; ++i) { for (let j = 0; j < seq.length; j++) { if (arr[i + j] !== seq[j]) { break; } if (j === seq.length - 1) { return i; } } } return -1; }; var getChallengeOffset = (clientData, challengePrefix2) => { return BigInt( findSequence(new Uint8Array(clientData), parseHex2(challengePrefix2)) + 12 + 1 ); }; // src/migrationKit/signers/passkeyService.ts var challengePrefix = "226368616c6c656e6765223a"; var _formatSigningRpId2 = () => { const rootDomain = psl.default.parse(window.location.host).domain; if (!rootDomain) return void 0; return rootDomain; }; var signWithCredential = async (challenge, publicKeyCredential) => { const assertionPayload = await navigator.credentials.get({ publicKey: { challenge, rpId: _formatSigningRpId2(), allowCredentials: publicKeyCredential || [], userVerification: "required", timeout: 3e4 } }); return assertionPayload; }; var getWebAuthnSignature = async (hash, publicKeyCredential) => { const challenge = parseHex2(hash.slice(2)); const assertionPayload = await signWithCredential( challenge, publicKeyCredential ); const publicKeyId = hexArrayStr2(assertionPayload.rawId); const { signature, authenticatorData, clientDataJSON: clientData } = assertionPayload.response; const rs = derToRS(new Uint8Array(signature)); const challengeOffset = getChallengeOffset( clientData, challengePrefix ); const encodedSignature = _viem.encodeAbiParameters.call(void 0, _viem.parseAbiParameters.call(void 0, "bytes, bytes, uint256, uint256[2]"), [ hexArrayStr2(authenticatorData), hexArrayStr2(clientData), challengeOffset, [ BigInt(hexArrayStr2(rs[0])), BigInt(hexArrayStr2(rs[1])) ] ] ); return { encodedSignature, publicKeyId }; }; var formatToSafeContractSignature = (signerAddress, signature) => { const dataOffsetInBytes = 65n; const verifierAndDataPosition = _viem.encodeAbiParameters.call(void 0, _viem.parseAbiParameters.call(void 0, "uint256, uint256"), [_viem.hexToBigInt.call(void 0, signerAddress), dataOffsetInBytes] ); const signatureType = "00"; const signatureBytes = _viem.hexToBytes.call(void 0, signature); const signatureLength = _viem.padHex.call(void 0, _viem.toHex.call(void 0, signatureBytes.length), { size: 32 }).slice(2); const data = signature.slice(2); return `${verifierAndDataPosition}${signatureType}${signatureLength}${data}`; }; var getSigner2 = async ({ API: API2, walletAddress, chain }) => { const webAuthnSigners = await API2.getWebAuthnSignersByWalletAddress(walletAddress); if (webAuthnSigners.length === 0) throw new NoPasskeySignerFoundInDBError(); const localStorageWebauthnCredentials = getWebauthnCredentialsInStorage(walletAddress); if (!localStorageWebauthnCredentials) throw new NoPasskeySignerFoundInDBError(); const registeredWebauthnSigner = await API2.getWebAuthnSignerByPublicKeyId( JSON.parse(localStorageWebauthnCredentials).publicKeyId ); const isOwner = await isSigner( registeredWebauthnSigner.signerAddress, walletAddress, chain, API2 ); if (!isOwner) throw new SignerNotOwnerError(); return registeredWebauthnSigner; }; var getWebauthnCredentialsInStorage = (walletAddress) => { return window.localStorage.getItem(`cometh-connect-${walletAddress}`); }; // src/migrationKit/safeLegacySigner/webauthn/webAuthn.ts async function safeLegacyWebAuthnSigner(client, { smartAccountAddress, publicKeyId, signerAddress }) { const account = _accounts.toAccount.call(void 0, { address: smartAccountAddress, async signMessage({ message }) { if (typeof message === "string") message = _viem.toHex.call(void 0, message); const hash = _viem.hashTypedData.call(void 0, { domain: { chainId: _optionalChain([client, 'access', _144 => _144.chain, 'optionalAccess', _145 => _145.id]), verifyingContract: smartAccountAddress }, types: EIP712_SAFE_MESSAGE_TYPE, primaryType: "SafeMessage", message: { message } }); const publicKeyCredential = [ { id: parseHex2(publicKeyId), type: "public-key" } ]; const { encodedSignature } = await getWebAuthnSignature( hash, publicKeyCredential ); return formatToSafeContractSignature( signerAddress, encodedSignature ); }, // biome-ignore lint/suspicious/noExplicitAny: TODO async signTransaction(tx) { const hash = _viem.hashTypedData.call(void 0, { domain: { chainId: _optionalChain([client, 'access', _146 => _146.chain, 'optionalAccess', _147 => _147.id]), verifyingContract: smartAccountAddress }, types: EIP712_SAFE_TX_TYPES, primaryType: "SafeTx", message: { to: tx.to, value: _viem.hexToBigInt.call(void 0, tx.value).toString(), data: tx.data, operation: tx.operation, safeTxGas: _viem.hexToBigInt.call(void 0, tx.safeTxGas).toString(), baseGas: _viem.hexToBigInt.call(void 0, tx.baseGas).toString(), gasPrice: _viem.hexToBigInt.call(void 0, tx.gasPrice).toString(), gasToken: _viem.zeroAddress, refundReceiver: _viem.zeroAddress, nonce: _viem.hexToBigInt.call(void 0, tx.nonce).toString() } }); const publicKeyCredential = [ { id: parseHex2(publicKeyId), type: "public-key" } ]; const { encodedSignature } = await getWebAuthnSignature( hash, publicKeyCredential ); return formatToSafeContractSignature( signerAddress, encodedSignature ); }, async signTypedData() { throw new Error("not supported"); } }); return { ...account, address: smartAccountAddress, source: "safeLegacyWebAuthnSigner" }; } // src/migrationKit/safeLegacySigner/comethSignerToSafeSigner.ts async function comethSignerToSafeSigner2(client, { smartAccountAddress, eoaSigner, passkeySigner }) { if (passkeySigner) { return { ...await safeLegacyWebAuthnSigner(client, { signerAddress: passkeySigner.signerAddress, smartAccountAddress, publicKeyId: passkeySigner.publicKeyId }) }; } if (!eoaSigner) throw new Error("eoaSigner is required"); return { ...await safeLegacyECDSASigner(client, { signer: eoaSigner, smartAccountAddress }) }; } // src/migrationKit/services/LEGACY_API.ts var LEGACY_API = class { constructor(apiKey, baseUrl) { __publicField(this, "api"); this.api = _axios2.default.create({ baseURL: baseUrl || "https://api.connect.cometh.io" }); this.api.defaults.headers.common.apikey = apiKey; } async relayTransaction({ walletAddress, safeTxData, signatures }) { const body = { ...safeTxData, nonce: _optionalChain([safeTxData, 'optionalAccess', _148 => _148.nonce, 'optionalAccess', _149 => _149.toString, 'call', _150 => _150()]), baseGas: _optionalChain([safeTxData, 'optionalAccess', _151 => _151.baseGas, 'optionalAccess', _152 => _152.toString, 'call', _153 => _153()]), gasPrice: _optionalChain([safeTxData, 'optionalAccess', _154 => _154.gasPrice, 'optionalAccess', _155 => _155.toString, 'call', _156 => _156()]), safeTxGas: _optionalChain([safeTxData, 'optionalAccess', _157 => _157.safeTxGas, 'optionalAccess', _158 => _158.toString, 'call', _159 => _159()]), signatures }; const response = await this.api.post( `/wallets/${walletAddress}/relay`, body ); return { safeTxHash: response.data.safeTxHash, relayId: response.data.relayId }; } async getWalletAddress(ownerAddress) { const response = await this.api.get( `/wallets/${ownerAddress}/wallet-address` ); return _optionalChain([response, 'optionalAccess', _160 => _160.data, 'optionalAccess', _161 => _161.walletAddress]); } async getRelayedTransaction(relayId) { const response = await this.api.get(`/relayed-transactions/${relayId}`); return response.data.relayedTransaction; } async getWebAuthnSignerByPublicKeyId(publicKeyId) { const response = await this.api.get( `/webauthn-signer/public-key-id/${publicKeyId}` ); return _optionalChain([response, 'optionalAccess', _162 => _162.data, 'optionalAccess', _163 => _163.webAuthnSigner]); } async getWebAuthnSignersByWalletAddress(walletAddress) { const response = await this.api.get( `/webauthn-signer/${walletAddress}` ); return _optionalChain([response, 'optionalAccess', _164 => _164.data, 'optionalAccess', _165 => _165.webAuthnSigners]); } }; // src/migrationKit/utils.ts function toLegacySmartAccount({ address, client, source, signMessage, signTypedData: signTypedData3, signTransaction, migrate, hasMigrated }) { const account = _accounts.toAccount.call(void 0, { address, signMessage: async ({ message }) => { const signature = await signMessage({ message }); return signature; }, signTypedData: async (typedData) => { const signature = await signTypedData3( typedData ); return signature; }, async signTransaction(tx) { const signature = await signTransaction(tx); return signature; } }); return { ...account, source, client, type: "local", publicKey: address, migrate, hasMigrated }; } // src/migrationKit/createLegacySafeSmartAccount.ts var DEFAULT_CONFIRMATION_TIME = 60 * 1e3; var multisendAddress = "0xA238CBeb142c10Ef7Ad8442C6D1f9E89e07e7761"; var prepareMigrationCalldata = async ({ threshold, safe4337ModuleAddress, safeWebAuthnSharedSignerContractAddress, migrationContractAddress, p256Verifier, smartAccountAddress, passkeySigner }) => { const transactions = [ { to: migrationContractAddress, value: 0n, data: _viem.encodeFunctionData.call(void 0, { abi: MigrationAbi, functionName: "migrateL2Singleton" }), op: 1 }, { to: smartAccountAddress, value: 0n, data: _viem.encodeFunctionData.call(void 0, { abi: SafeAbi, functionName: "setFallbackHandler", args: [safe4337ModuleAddress] }), op: 0 }, { to: smartAccountAddress, value: 0n, data: _viem.encodeFunctionData.call(void 0, { abi: SafeAbi, functionName: "enableModule", args: [safe4337ModuleAddress] }), op: 0 } ]; if (passkeySigner) { transactions.push( { to: safeWebAuthnSharedSignerContractAddress, value: 0n, data: _viem.encodeFunctionData.call(void 0, { abi: SafeWebAuthnSharedSignerAbi, functionName: "configure", args: [ { x: _viem.hexToBigInt.call(void 0, passkeySigner.publicKeyX), y: _viem.hexToBigInt.call(void 0, passkeySigner.publicKeyY), verifiers: _viem.hexToBigInt.call(void 0, p256Verifier) } ] }), op: 1 }, { to: smartAccountAddress, value: 0n, data: _viem.encodeFunctionData.call(void 0, { abi: SafeAbi, functionName: "addOwnerWithThreshold", args: [safeWebAuthnSharedSignerContractAddress, threshold] }), op: 0 } ); } return _viem.encodeFunctionData.call(void 0, { abi: MultiSendContractABI, functionName: "multiSend", args: [encodeMultiSendTransactions(transactions)] }); }; var importWalletToApiAndSetStorage = async ({ passkeySigner, api, smartAccountAddress, chain, signerAddress }) => { await api.importExternalSafe({ smartAccountAddress, publicKeyId: _optionalChain([passkeySigner, 'optionalAccess', _166 => _166.publicKeyId]), publicKeyY: _optionalChain([passkeySigner, 'optionalAccess', _167 => _167.publicKeyY]), publicKeyX: _optionalChain([passkeySigner, 'optionalAccess', _168 => _168.publicKeyX]), deviceData: _optionalChain([passkeySigner, 'optionalAccess', _169 => _169.deviceData]), signerAddress, chainId: chain.id.toString() }); if (passkeySigner) { setPasskeyInStorage( smartAccountAddress, passkeySigner.publicKeyId, passkeySigner.publicKeyX, passkeySigner.publicKeyY, signerAddress ); } }; var waitForTransactionRelayAndImport = async ({ relayTx, legacyApi, api, smartAccountAddress, chain, safeWebAuthnSharedSignerContractAddress, passkeySigner, eoaSigner }) => { const startDate = Date.now(); const timeoutLimit = new Date( startDate + DEFAULT_CONFIRMATION_TIME ).getTime(); let relayedTransaction = void 0; while (!_optionalChain([relayedTransaction, 'optionalAccess', _170 => _170.status, 'access', _171 => _171.confirmed]) && Date.now() < timeoutLimit) { await new Promise((resolve) => setTimeout(resolve, 4e3)); relayedTransaction = await legacyApi.getRelayedTransaction( relayTx.relayId ); if (_optionalChain([relayedTransaction, 'optionalAccess', _172 => _172.status, 'access', _173 => _173.confirmed])) { await importWalletToApiAndSetStorage({ passkeySigner, api, smartAccountAddress, chain, signerAddress: passkeySigner ? safeWebAuthnSharedSignerContractAddress : _optionalChain([eoaSigner, 'optionalAccess', _174 => _174.address]) }); } } if (!_optionalChain([relayedTransaction, 'optionalAccess', _175 => _175.status, 'access', _176 => _176.confirmed])) throw new RelayedTransactionError(); }; async function createLegacySafeSmartAccount({ apiKeyLegacy, apiKey4337, chain, rpcUrl, smartAccountAddress, comethSignerConfig }) { if (!smartAccountAddress) throw new Error("Account address not found"); const legacyApi = new LEGACY_API(apiKeyLegacy); const api = new API(apiKey4337); const publicClient = _viem.createPublicClient.call(void 0, { chain, transport: _viem.http.call(void 0, rpcUrl), cacheTime: 6e4, batch: { multicall: { wait: 50 } } }); const [client, projectParams, isWebAuthnCompatible2] = await Promise.all([ getViemClient(chain, rpcUrl), getProjectParamsByChain({ api: new API(apiKey4337), chain }), isDeviceCompatibleWithPasskeys({ webAuthnOptions: {} }) ]); const { safe4337ModuleAddress, safeWebAuthnSharedSignerContractAddress, p256Verifier, migrationContractAddress } = projectParams.safeContractParams; if (!migrationContractAddress) { throw new Error( "Migration contract address not available for this network" ); } const [eoaSigner, passkeySigner] = await Promise.all([ !isWebAuthnCompatible2 ? getFallbackEoaSigner({ smartAccountAddress, encryptionSalt: _optionalChain([comethSignerConfig, 'optionalAccess', _177 => _177.encryptionSalt]) }).then((data) => data.signer) : Promise.resolve(void 0), isWebAuthnCompatible2 ? getSigner2({ API: legacyApi, walletAddress: smartAccountAddress, chain }) : Promise.resolve(void 0) ]); const safeSigner = await comethSignerToSafeSigner2( client, { smartAccountAddress, passkeySigner, eoaSigner } ); const smartAccount = toLegacySmartAccount({ address: smartAccountAddress, async signMessage({ message }) { return safeSigner.signMessage({ message }); }, async signTransaction(_tx) { return safeSigner.signTransaction(_tx); }, async signTypedData() { throw new (0, _accounts3.SignTransactionNotSupportedBySmartAccount)(); }, async migrate() { const isDeployed2 = await _permissionless.isSmartAccountDeployed.call(void 0, publicClient, smartAccountAddress ); if (isDeployed2) { const currentVersion = await publicClient.readContract({ address: smartAccountAddress, abi: SafeAbi, functionName: "VERSION" }); if (currentVersion !== "1.3.0") { throw new Error( `Safe is not version 1.3.0. Current version: ${currentVersion}` ); } } const threshold = isDeployed2 ? await publicClient.readContract({ address: smartAccountAddress, abi: SafeAbi, functionName: "getThreshold" }) : 1; const migrateCalldata = await prepareMigrationCalldata({ threshold, safe4337ModuleAddress, safeWebAuthnSharedSignerContractAddress, migrationContractAddress, p256Verifier, smartAccountAddress, passkeySigner }); const nonce = isDeployed2 ? await publicClient.readContract({ address: smartAccountAddress, abi: SafeAbi, functionName: "nonce" }) : 0; const tx = { to: multisendAddress, value: BigInt(0).toString(), data: migrateCalldata, operation: 1, safeTxGas: 0, baseGas: 0, gasPrice: 0, gasToken: _viem.zeroAddress, refundReceiver: _viem.zeroAddress, nonce: Number(nonce) }; const signature = await this.signTransaction(tx); const relayTx = await legacyApi.relayTransaction({ safeTxData: tx, signatures: signature, walletAddress: smartAccountAddress }); await waitForTransactionRelayAndImport({ relayTx, passkeySigner, eoaSigner, legacyApi, api, smartAccountAddress, chain, safeWebAuthnSharedSignerContractAddress }); return relayTx; }, async hasMigrated() { const isDeployed2 = await _permissionless.isSmartAccountDeployed.call(void 0, publicClient, smartAccountAddress ); if (!isDeployed2) { console.debug("Wallet is not deployed"); return false; } const version = await publicClient.readContract({ address: smartAccountAddress, abi: SafeAbi, functionName: "VERSION" }); if (version !== "1.4.1") { console.debug(`Version is still ${version}`); return false; } const isModuleEnabled = await publicClient.readContract({ address: smartAccountAddress, abi: SafeAbi, functionName: "isModuleEnabled", args: [safe4337ModuleAddress] }); if (!isModuleEnabled) { console.debug("4337 module not enabled"); return false; } return true; }, client, source: "safeLegacySmartAccount" }); return { ...smartAccount }; } // src/migrationKit/retrieveLegacyWalletAddress.ts var retrieveLegacyWalletAddress = async (apiKeyLegacy) => { const legacyApi = new LEGACY_API(apiKeyLegacy); let publicKeyId; try { ({ publicKeyId } = await getWebAuthnSignature("Retrieve user wallet")); } catch (e9) { throw new NoPasskeySignerFoundInDeviceError(); } const signingWebAuthnSigner = await legacyApi.getWebAuthnSignerByPublicKeyId(publicKeyId); if (!signingWebAuthnSigner) throw new NoPasskeySignerFoundInDBError(); const { walletAddress } = signingWebAuthnSigner; return walletAddress; }; // src/wagmi/connector.ts var _wagmi = require('wagmi'); function smartAccountConnector({ apiKey, bundlerUrl, rpcUrl, chain, baseUrl, smartAccountAddress, comethSignerConfig, safeContractConfig, sessionKeysEnabled, paymasterUrl, shimDisconnect = true, signer }) { let client; return _wagmi.createConnector.call(void 0, (config) => ({ id: "cometh", name: "Cometh Connect", type: "cometh", async setup() { if (smartAccountAddress) { this.connect(); } }, async connect({ chainId } = {}) { try { if (chainId && chainId !== await this.getChainId()) { throw new Error(`Invalid chainId ${chainId} requested`); } const account = await createSafeSmartAccount({ apiKey, chain, rpcUrl, baseUrl, smartAccountAddress, entryPoint: _permissionless.ENTRYPOINT_ADDRESS_V07, comethSignerConfig, safeContractConfig, sessionKeysEnabled, signer }); if (paymasterUrl) { const paymasterClient = await createComethPaymasterClient({ transport: _viem.http.call(void 0, paymasterUrl), chain, entryPoint: _permissionless.ENTRYPOINT_ADDRESS_V07, rpcUrl }); client = createSmartAccountClient({ account, entryPoint: _permissionless.ENTRYPOINT_ADDRESS_V07, chain, bundlerTransport: _viem.http.call(void 0, bundlerUrl), middleware: { sponsorUserOperation: paymasterClient.sponsorUserOperation, gasPrice: paymasterClient.gasPrice }, rpcUrl }); } else { client = createSmartAccountClient({ account, entryPoint: _permissionless.ENTRYPOINT_ADDRESS_V07, chain, bundlerTransport: _viem.http.call(void 0, bundlerUrl), rpcUrl }); } await _optionalChain([config, 'access', _178 => _178.storage, 'optionalAccess', _179 => _179.removeItem, 'call', _180 => _180(`${this.id}.disconnected`)]); return { accounts: [_optionalChain([client, 'optionalAccess', _181 => _181.account, 'optionalAccess', _182 => _182.address])], chainId: await this.getChainId() }; } catch (error) { if (/(user rejected|connection request reset)/i.test( _optionalChain([error, 'optionalAccess', _183 => _183.message]) )) { throw new (0, _viem.UserRejectedRequestError)(error); } throw error; } }, async disconnect() { if (shimDisconnect) _optionalChain([config, 'access', _184 => _184.storage, 'optionalAccess', _185 => _185.setItem, 'call', _186 => _186(`${this.id}.disconnected`, true)]); }, async getAccounts() { return [_optionalChain([client, 'optionalAccess', _187 => _187.account, 'optionalAccess', _188 => _188.address])]; }, async getChainId() { return chain.id; }, async getProvider() { return client; }, async isAuthorized() { return true; }, onAccountsChanged() { }, onChainChanged() { }, onDisconnect() { config.emitter.emit("disconnect"); }, async getClient({ chainId: requestedChainId }) { const chainId = await this.getChainId(); if (requestedChainId !== chainId) { throw new Error(`Invalid chainId ${chainId} requested`); } return client; } })); } exports.ENTRYPOINT_ADDRESS_V07 = ENTRYPOINT_ADDRESS_V07; exports.createComethPaymasterClient = createComethPaymasterClient; exports.createLegacySafeSmartAccount = createLegacySafeSmartAccount; exports.createNewSigner = createNewSigner; exports.createNewSignerWithAccountAddress = createNewSignerWithAccountAddress; exports.createSafeSmartAccount = createSafeSmartAccount; exports.createSigner = createSigner; exports.createSmartAccountClient = createSmartAccountClient; exports.customChains = customChains; exports.generateQRCodeUrl = generateQRCodeUrl; exports.retrieveAccountAddressFromPasskeyId = retrieveAccountAddressFromPasskeyId; exports.retrieveAccountAddressFromPasskeys = retrieveAccountAddressFromPasskeys; exports.retrieveLegacyWalletAddress = retrieveLegacyWalletAddress; exports.serializeUrlWithSignerPayload = serializeUrlWithSignerPayload; exports.smartAccountConnector = smartAccountConnector;