UNPKG

@abstract-foundation/agw-client

Version:
70 lines 2.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createSession = createSession; exports.prepareCreateSessionCall = prepareCreateSessionCall; const viem_1 = require("viem"); const actions_1 = require("viem/actions"); const utils_1 = require("viem/utils"); const AGWAccount_js_1 = require("../abis/AGWAccount.js"); const SessionKeyValidator_js_1 = require("../abis/SessionKeyValidator.js"); const constants_js_1 = require("../constants.js"); const account_js_1 = require("../errors/account.js"); const sessions_js_1 = require("../sessions.js"); const utils_js_1 = require("../utils.js"); async function createSession(client, args) { const { account: account_ = client.account, chain = client.chain, session, ...rest } = args; if (typeof account_ === "undefined") throw new account_js_1.AccountNotFoundError({ docsPath: "/docs/actions/wallet/sendTransaction", }); const account = (0, utils_1.parseAccount)(account_); const createSessionCall = await prepareCreateSessionCall(account, client, session); const transactionHash = await (0, utils_1.getAction)(client, actions_1.sendTransaction, "sendTransaction")({ ...createSessionCall, ...rest, account: account, chain: chain, }); return { transactionHash, session }; } async function prepareCreateSessionCall(accountOrAddress, client, session) { const account = (0, utils_1.parseAccount)(accountOrAddress); const isDeployed = await (0, utils_js_1.isSmartAccountDeployed)(client, account.address); const hasModule = isDeployed ? await hasSessionModule(account, client) : false; if (!hasModule) { const encodedSession = (0, sessions_js_1.encodeSession)(session); return { to: account.address, value: 0n, data: (0, viem_1.encodeFunctionData)({ abi: AGWAccount_js_1.default, functionName: "addModule", args: [(0, viem_1.concatHex)([constants_js_1.SESSION_KEY_VALIDATOR_ADDRESS, encodedSession])], }), }; } else { return { to: constants_js_1.SESSION_KEY_VALIDATOR_ADDRESS, value: 0n, data: (0, viem_1.encodeFunctionData)({ abi: SessionKeyValidator_js_1.SessionKeyValidatorAbi, functionName: "createSession", args: [session], }), }; } } async function hasSessionModule(account, client) { const validationHooks = await (0, utils_1.getAction)(client, actions_1.readContract, "readContract")({ address: account.address, abi: AGWAccount_js_1.default, functionName: "listHooks", args: [true], }); const hasSessionModule = validationHooks.some((hook) => hook === constants_js_1.SESSION_KEY_VALIDATOR_ADDRESS); return hasSessionModule; } //# sourceMappingURL=createSession.js.map