Using Session Keys

Session Keys are currently an experimental feature in the SDK. We are actively working on simplifying the usage, please note that there could be breaking changes as we improve this feature.

Once session keys are added, using them is straightforward - just create another client instance with the session key connected along with properties of the session key (session key entityId and global validation is used). These properties were set during the installValidation call.

1import { createModularAccountV2Client } from "@account-kit/smart-contracts";
2import { LocalAccountSigner } from "@aa-sdk/core";
3import { generatePrivateKey } from "viem/accounts";
4import { type SmartAccountSigner } from "@aa-sdk/core";
5import { parseEther } from "viem";
6import { sepolia, alchemy } from "@account-kit/infra";
7
8const client = await createModularAccountV2Client({
9 chain: sepolia,
10 transport: alchemy({ apiKey: "your-api-key" }),
11 signer: LocalAccountSigner.privateKeyToAccountSigner(generatePrivateKey()),
12});
13
14let sessionKeyEntityId = 1;
15const sessionKeySigner: SmartAccountSigner =
16 LocalAccountSigner.privateKeyToAccountSigner(generatePrivateKey());
17
18const sessionKeyClient = await createModularAccountV2Client({
19 chain: sepolia,
20 transport: alchemy({ apiKey: "your-api-key" }),
21 signer: sessionKeySigner,
22 accountAddress: client.getAddress(client.account),
23 signerEntity: {
24 entityId: sessionKeyEntityId,
25 isGlobalValidation: true,
26 },
27});
28
29await sessionKeyClient.sendUserOperation({
30 uo: {
31 target: "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045", // The address to call in the UO
32 data: "0x", // The calldata to send in the UO
33 value: parseEther("1"), // The value to send in the UO
34 },
35});

Note that you have to pass in accountAddress to session key clients. By default, the client uses an account address counterfactual that assumes that the connected signer is the owner.