Using Session Keys

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 initCode: await client.account.getInitCode(),
24 signerEntity: {
25 entityId: sessionKeyEntityId,
26 isGlobalValidation: true,
27 },
28});
29
30await sessionKeyClient.sendUserOperation({
31 uo: {
32 target: "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045", // The address to call in the UO
33 data: "0x", // The calldata to send in the UO
34 value: parseEther("1"), // The value to send in the UO
35 },
36});

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