UNPKG

@cometh/session-keys

Version:
306 lines (295 loc) 13.5 kB
import { Session, SmartSessionMode, EnableSessionData } from '@rhinestone/module-sdk'; export { SmartSessionMode } from '@rhinestone/module-sdk'; import { Erc7579Actions } from 'permissionless/actions/erc7579'; export { erc7579Actions } from 'permissionless/actions/erc7579'; import { Hex, OneOf, Address, AbiFunction, ByteArray, Abi, Transport, Chain, Client, RpcSchema, Prettify, BundlerRpcSchema, LocalAccount, UnionPartialBy, PublicClient, Hash, PrivateKeyAccount } from 'viem'; import { SmartAccount, BundlerActions, UserOperation } from 'viem/account-abstraction'; type Execution = { target: Address; value: bigint; callData: Hex; }; /** * Represents a hardcoded hex value reference. * Used when you want to bypass automatic hex conversion. */ type HardcodedReference = { /** The raw hex value */ raw: Hex; }; /** * Base types that can be converted to hex references. */ type BaseReferenceValue = string | number | bigint | boolean | ByteArray; /** * Union type of all possible reference values that can be converted to hex. * Includes both basic types and hardcoded references. */ type AnyReferenceValue = BaseReferenceValue | HardcodedReference; type PreparePermissionResponse = { /** Array of permission IDs for the created sessions. */ permissionIds: Hex[]; /** The execution object for the action. */ action: Execution; /** The sessions that were created. */ sessions: Session[]; }; /** * Represents the response for creating sessions. */ type GrantPermissionResponse = { /** The hash of the user operation. */ userOpHash: Hex; } & PreparePermissionResponse; type AddSafe7579Response = { /** The hash of the user operation. */ userOpHash: Hex; }; type Call = { to: Hex; data: Hex; value: bigint; }; type OptionalSessionKeyData = OneOf<{ /** Public key for the session. Required for K1 algorithm validators. */ sessionPublicKey: Hex; } | { /** Data for the session key. */ sessionKeyData: Hex; }>; /** * Represents a rule for action policies. */ type Rule = { /** The condition to apply to the parameter */ condition: ParamCondition; /** The offset index in the calldata where the value to be checked is located */ offsetIndex: number; /** Indicates if the rule has a usage limit */ isLimited: boolean; /** The reference value to compare against */ ref: AnyReferenceValue; /** The usage object containing limit and used values (required if isLimited is true) */ usage: LimitUsage; }; type ActionPolicyInfo = { /** The address of the contract to be included in the policy */ contractAddress: Hex; /** The timeframe policy can be used to restrict a session to only be able to be used within a certain timeframe */ validUntil?: number; /** Timestamp after which the policy becomes valid */ validAfter?: number; /** The value limit policy can be used to enforce that only a certain amount of native value can be spent. For ERC-20 limits, use the spending limit policy */ valueLimit?: bigint; /** The spending limits policy can be used to ensure that only a certain amount of ERC-20 tokens can be spent. For native value spends, use the value limit policy */ tokenLimits?: SpendingLimitPolicyData[]; /** The value limit policy can be used to enforce that only a certain amount of native value can be spent. For ERC-20 limits, use the spending limit policy. */ usageLimit?: bigint; /** The sudo policy is an action policy that will allow any action for the specified target and selector. */ sudo?: boolean; } & OneOf<{ /** The specific function selector from the contract to be included in the policy */ functionSelector: string | AbiFunction; /** Array of rules for the policy */ rules?: Rule[]; } | { /** The ABI of the contract to be included in the policy */ abi: Abi; }>; /** * Parameters for creating a session. */ type CreateSessionDataParams = OptionalSessionKeyData & { /** Public key for the session. Required for K1 algorithm validators. */ sessionPublicKey?: Hex; /** Address of the session validator. */ sessionValidator?: Address; /** Data for the session validator. */ sessionValidatorInitData?: Hex; /** Optional salt for the session. */ salt?: Hex; /** Timestamp until which the session is valid. */ sessionValidUntil?: number; /** Timestamp after which the session becomes valid. */ sessionValidAfter?: number; /** Chain IDs where the session should be enabled. Useful for enable mode. */ chainIds?: bigint[]; /** Array of action policy data for the session. */ actionPoliciesInfo?: ActionPolicyInfo[]; }; declare enum ParamCondition { EQUAL = 0, GREATER_THAN = 1, LESS_THAN = 2, GREATER_THAN_OR_EQUAL = 3, LESS_THAN_OR_EQUAL = 4, NOT_EQUAL = 5 } type SpendingLimitPolicyData = { /** The address of the token to be included in the policy */ token: Address; /** The limit for the token */ limit: bigint; }; /** * Represents the usage limit for a rule. */ type LimitUsage = { limit: bigint; used: bigint; }; /** * Represents the possible modes for a smart session. */ type SmartSessionModeType = (typeof SmartSessionMode)[keyof typeof SmartSessionMode]; /** * Represents the data structure for using a session module. */ type UsePermissionModuleData = { /** The mode of the smart session. */ mode?: SmartSessionModeType; /** Data for enabling the session. */ enableSessionData?: EnableSessionData; /** The index of the permission ID to use for the session. Defaults to 0. */ permissionIdIndex?: number; } & PreparePermissionResponse; type SafeSigner<Name extends string = string> = LocalAccount<Name> & { getStubSignature(): Promise<Hex>; signUserOperation: (parameters: UnionPartialBy<UserOperation, "sender"> & { chainId?: number | undefined; }) => Promise<Hex>; }; type SmartSessionsAccountClient<transport extends Transport = Transport, chain extends Chain | undefined = Chain | undefined, account extends SmartAccount | undefined = SmartAccount | undefined, client extends Client | undefined = Client | undefined, rpcSchema extends RpcSchema | undefined = undefined> = Prettify<Client<transport, chain extends Chain ? chain : client extends Client<any, infer chain> ? chain : undefined, account, rpcSchema extends RpcSchema ? [...BundlerRpcSchema, ...rpcSchema] : BundlerRpcSchema, BundlerActions<account> & SmartSessionCreateActions<account> & Erc7579Actions<account>>>; type GrantPermissionParameters<TAccount extends SmartAccount | undefined = SmartAccount | undefined> = { /** Array of session data parameters for creating multiple sessions. */ sessionRequestedInfo: CreateSessionDataParams[]; /** The maximum fee per gas unit the transaction is willing to pay. */ maxFeePerGas?: bigint; /** The maximum priority fee per gas unit the transaction is willing to pay. */ maxPriorityFeePerGas?: bigint; /** The nonce of the transaction. If not provided, it will be determined automatically. */ nonce?: bigint; /** Optional public client for blockchain interactions. */ publicClient?: PublicClient; /** The modular smart account to create sessions for. If not provided, the client's account will be used. */ account?: TAccount; /** Optional attesters to trust. */ attesters?: Hex[]; /** Additional calls to be included in the user operation. */ calls?: Call[]; }; type IsPermissionInstalledParameters = { session: Session; }; type PreparePermissionParameters<TAccount extends SmartAccount | undefined = SmartAccount | undefined> = { /** Array of session data parameters for creating multiple sessions. */ sessionRequestedInfo: CreateSessionDataParams[]; /** The maximum fee per gas unit the transaction is willing to pay. */ maxFeePerGas?: bigint; /** The maximum priority fee per gas unit the transaction is willing to pay. */ maxPriorityFeePerGas?: bigint; /** The nonce of the transaction. If not provided, it will be determined automatically. */ nonce?: bigint; /** Optional public client for blockchain interactions. */ publicClient?: PublicClient; /** The modular smart account to create sessions for. If not provided, the client's account will be used. */ account?: TAccount; }; type TrustAttestersParameters<TAccount extends SmartAccount | undefined = SmartAccount | undefined> = { /** The addresses of the attesters to be trusted. */ attesters?: Hex[]; /** The address of the registry contract. */ registryAddress?: Hex; /** The maximum fee per gas unit the transaction is willing to pay. */ maxFeePerGas?: bigint; /** The maximum priority fee per gas unit the transaction is willing to pay. */ maxPriorityFeePerGas?: bigint; /** The nonce of the transaction. If not provided, it will be determined automatically. */ nonce?: bigint; /** The smart account to use for trusting attesters. If not provided, the client's account will be used. */ account?: TAccount; /** The threshold of the attesters to be trusted. */ threshold?: number; }; /** * Parameters for using a smart session to execute actions. * */ type UsePermissionParameters$1 = { /** Array of executions to perform in the session. Allows for batch transactions if the session is enabled for multiple actions. */ actions: Execution[]; /** The maximum fee per gas unit the transaction is willing to pay. */ maxFeePerGas?: bigint; /** The maximum priority fee per gas unit the transaction is willing to pay. */ maxPriorityFeePerGas?: bigint; verificationGasLimit?: bigint; }; type SmartSessionCreateActions<TAccount extends SmartAccount | undefined = SmartAccount | undefined> = { /** * Adds the smart sessions module to the smart account. * * @returns A promise that resolves to the transaction hash. */ addSafe7579Module: () => Promise<AddSafe7579Response>; /** * Creates multiple sessions for a smart account. * * @param args - Parameters for creating sessions. * @returns A promise that resolves to the creation response. */ grantPermission: (args: GrantPermissionParameters<TAccount>) => Promise<GrantPermissionResponse>; /** * Prepares permission for a smart account. * * @param args - Parameters for preparing permission. * @returns A promise that resolves to the transaction hash. */ preparePermission: (args: PreparePermissionParameters<TAccount>) => Promise<PreparePermissionResponse>; /** * Creates multiple sessions for a smart account. * * @param args - Parameters for creating sessions. * @returns A promise that resolves to the creation response. */ isPermissionInstalled: (args: IsPermissionInstalledParameters) => Promise<boolean>; /** * Trusts attesters for a smart account. * * @param args - Parameters for trusting attesters. * @returns A promise that resolves to the transaction hash. */ trustAttesters: (args?: TrustAttestersParameters<TAccount>) => Promise<Hash>; /** * Uses a session to perform an action. * * @param args - Parameters for using a session. * @returns A promise that resolves to the transaction hash. */ usePermission: (args: UsePermissionParameters$1) => Promise<Hash>; }; /** * Creates actions for managing smart session creation. * * @returns A function that takes a client and returns SmartSessionCreateActions. */ declare function smartSessionActions(): <TAccount extends SmartAccount | undefined = SmartAccount | undefined>(client: Client<Transport, Chain | undefined, TAccount>) => SmartSessionCreateActions<TAccount>; type UsePermissionParameters<TAccount extends SmartAccount | undefined = SmartAccount | undefined> = { /** Array of executions to perform in the session. Allows for batch transactions if the session is enabled for multiple actions. */ actions: Execution[]; /** The maximum fee per gas unit the transaction is willing to pay. */ maxFeePerGas?: bigint; /** The maximum priority fee per gas unit the transaction is willing to pay. */ maxPriorityFeePerGas?: bigint; /** The nonce of the transaction. If not provided, it will be determined automatically. */ nonce?: bigint; /** The modular smart account to use for the session. If not provided, the client's account will be used. */ account?: TAccount; verificationGasLimit?: bigint; }; declare function toSmartSessionsAccount(smartAccount: SmartAccount, sessionKeySigner: SafeSigner): Promise<SmartAccount>; type UsePermissionModuleParameters = { moduleData?: UsePermissionModuleData; signer: PrivateKeyAccount; }; declare function toSmartSessionsSigner<transport extends Transport, chain extends Chain | undefined = undefined, account extends SmartAccount | undefined = SmartAccount | undefined, client extends Client | undefined = undefined>(smartAccountClient: SmartSessionsAccountClient<transport, chain, account, client>, parameters: UsePermissionModuleParameters): Promise<SafeSigner<"safeSmartSessionsSigner">>; export { type CreateSessionDataParams, type Execution, type GrantPermissionParameters, type GrantPermissionResponse, type SafeSigner, type SmartSessionsAccountClient, type UsePermissionParameters, smartSessionActions, toSmartSessionsAccount, toSmartSessionsSigner };