|
| 1 | +import { randomBytesHex } from "../../../utils/random.js"; |
| 2 | +import type { BaseTransactionOptions } from "../../../transaction/types.js"; |
| 3 | +import type { Account } from "../../../wallets/interfaces/wallet.js"; |
| 4 | +import { CallSpecRequest, ConstraintRequest, SessionSpecRequest, TransferSpecRequest, UsageLimitRequest, type CallSpecInput, type TransferSpecInput } from "./types.js"; |
| 5 | +import { isCreateSessionWithSigSupported, createSessionWithSig } from "../__generated__/MinimalAccount/write/createSessionWithSig.js"; |
| 6 | + |
| 7 | +/** |
| 8 | + * @extension ERC7702 |
| 9 | + */ |
| 10 | +export type CreateSessionKeyOptions = { |
| 11 | + /** |
| 12 | + * The admin account that will perform the operation. |
| 13 | + */ |
| 14 | + account: Account; |
| 15 | + /** |
| 16 | + * The address to add as a session key. |
| 17 | + */ |
| 18 | + sessionKeyAddress: `0x${string}`; |
| 19 | + /** |
| 20 | + * How long the session key should be valid for, in seconds. |
| 21 | + */ |
| 22 | + durationInSeconds: number; |
| 23 | + /** |
| 24 | + * Whether to grant full execution permissions to the session key. |
| 25 | + */ |
| 26 | + grantFullPermissions?: boolean; |
| 27 | + /** |
| 28 | + * Smart contract interaction policies to apply to the session key, ignored if grantFullPermissions is true. |
| 29 | + */ |
| 30 | + callPolicies?: CallSpecInput[]; |
| 31 | + /** |
| 32 | + * Value transfer policies to apply to the session key, ignored if grantFullPermissions is true. |
| 33 | + */ |
| 34 | + transferPolicies?: TransferSpecInput[]; |
| 35 | +}; |
| 36 | + |
| 37 | +/** |
| 38 | + * Creates session key permissions for a specified address. |
| 39 | + * @param options - The options for the createSessionKey function. |
| 40 | + * @param {Contract} options.contract - The EIP-7702 smart EOA contract to create the session key from |
| 41 | + * @returns The transaction object to be sent. |
| 42 | + * @example |
| 43 | + * ```ts |
| 44 | + * import { createSessionKey } from 'thirdweb/extensions/7702'; |
| 45 | + * import { sendTransaction } from 'thirdweb'; |
| 46 | + * |
| 47 | + * const transaction = createSessionKey({ |
| 48 | + * account: account, |
| 49 | + * contract: accountContract, |
| 50 | + * sessionKeyAddress: TEST_ACCOUNT_A.address, |
| 51 | + * durationInSeconds: 86400, // 1 day |
| 52 | + * grantFullPermissions: true |
| 53 | + *}) |
| 54 | + * |
| 55 | + * await sendTransaction({ transaction, account }); |
| 56 | + * ``` |
| 57 | + * @extension ERC7702 |
| 58 | + */ |
| 59 | +export function createSessionKey( |
| 60 | + options: BaseTransactionOptions<CreateSessionKeyOptions>, |
| 61 | +) { |
| 62 | + const { contract, account, sessionKeyAddress, durationInSeconds, grantFullPermissions, callPolicies, transferPolicies } = options; |
| 63 | + |
| 64 | + return createSessionWithSig({ |
| 65 | + async asyncParams() { |
| 66 | + const req = { |
| 67 | + signer: sessionKeyAddress, |
| 68 | + isWildcard: grantFullPermissions ?? true, |
| 69 | + expiresAt: BigInt(Math.floor(Date.now() / 1000) + durationInSeconds), |
| 70 | + callPolicies: (callPolicies || []).map(policy => ({ |
| 71 | + target: policy.target, |
| 72 | + selector: policy.selector, |
| 73 | + maxValuePerUse: policy.maxValuePerUse || BigInt(0), |
| 74 | + valueLimit: policy.valueLimit || { limitType: 0, limit: BigInt(0), period: BigInt(0) }, |
| 75 | + constraints: (policy.constraints || []).map(constraint => ({ |
| 76 | + condition: constraint.condition, |
| 77 | + index: constraint.index || BigInt(0), |
| 78 | + refValue: constraint.refValue || "0x", |
| 79 | + limit: constraint.limit || { limitType: 0, limit: BigInt(0), period: BigInt(0) } |
| 80 | + })) |
| 81 | + })), |
| 82 | + transferPolicies: (transferPolicies || []).map(policy => ({ |
| 83 | + target: policy.target, |
| 84 | + maxValuePerUse: policy.maxValuePerUse || BigInt(0), |
| 85 | + valueLimit: policy.valueLimit || { limitType: 0, limit: BigInt(0), period: BigInt(0) } |
| 86 | + })), |
| 87 | + uid: await randomBytesHex(), |
| 88 | + }; |
| 89 | + |
| 90 | + const signature = await account.signTypedData({ |
| 91 | + domain: { |
| 92 | + chainId: contract.chain.id, |
| 93 | + name: "MinimalAccount", |
| 94 | + verifyingContract: contract.address, |
| 95 | + version: "1", |
| 96 | + }, |
| 97 | + message: req, |
| 98 | + primaryType: "SessionSpec", |
| 99 | + types: { |
| 100 | + SessionSpec: SessionSpecRequest, |
| 101 | + CallSpec: CallSpecRequest, |
| 102 | + Constraint: ConstraintRequest, |
| 103 | + TransferSpec: TransferSpecRequest, |
| 104 | + UsageLimit: UsageLimitRequest, |
| 105 | + }, |
| 106 | + }); |
| 107 | + |
| 108 | + return { sessionSpec: req, signature }; |
| 109 | + }, |
| 110 | + contract, |
| 111 | + }); |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * Checks if the `isCreateSessionKeySupported` method is supported by the given contract. |
| 116 | + * @param availableSelectors An array of 4byte function selectors of the contract. You can get this in various ways, such as using "whatsabi" or if you have the ABI of the contract available you can use it to generate the selectors. |
| 117 | + * @returns A boolean indicating if the `isAddSessionKeySupported` method is supported. |
| 118 | + * @extension ERC7702 |
| 119 | + * @example |
| 120 | + * ```ts |
| 121 | + * import { isCreateSessionKeySupported } from "thirdweb/extensions/erc7702"; |
| 122 | + * |
| 123 | + * const supported = isCreateSessionKeySupported(["0x..."]); |
| 124 | + * ``` |
| 125 | + */ |
| 126 | +export function isCreateSessionKeySupported(availableSelectors: string[]) { |
| 127 | + return isCreateSessionWithSigSupported(availableSelectors); |
| 128 | +} |
0 commit comments