Removing 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.

Removing session keys is done with the uninstallValidation method.

1import { createModularAccountV2Client } from "@account-kit/smart-contracts";
2import {
3 installValidationActions,
4 getDefaultSingleSignerValidationModuleAddress,
5 SingleSignerValidationModule,
6 modularAccountAbi,
7} from "@account-kit/smart-contracts/experimental";
8import { LocalAccountSigner } from "@aa-sdk/core";
9import { sepolia, alchemy } from "@account-kit/infra";
10import { generatePrivateKey } from "viem/accounts";
11
12const client = (
13 await createModularAccountV2Client({
14 chain: sepolia,
15 transport: alchemy({ apiKey: "your-api-key" }),
16 signer: LocalAccountSigner.privateKeyToAccountSigner(generatePrivateKey()),
17 })
18).extend(installValidationActions);
19
20let sessionKeyEntityId = 1;
21
22// Removing a basic session key
23await client.uninstallValidation({
24 moduleAddress: getDefaultSingleSignerValidationModuleAddress(client.chain),
25 entityId: sessionKeyEntityId,
26 uninstallData: SingleSignerValidationModule.encodeOnUninstallData({
27 entityId: sessionKeyEntityId,
28 }),
29 hookUninstallDatas: [],
30});
31
32// Removing a session key with hooks
33await client.uninstallValidation({
34 moduleAddress: getDefaultSingleSignerValidationModuleAddress(client.chain),
35 entityId: sessionKeyEntityId,
36 uninstallData: SingleSignerValidationModule.encodeOnUninstallData({
37 entityId: sessionKeyEntityId,
38 }),
39 hookUninstallDatas: [],
40});

If there are hooks on the validation, you have to provide the hook uninstallation data to uninstall them too. Each module provides an encodeOnUninstallData helper function to generate the uninstallation hook for that module.

1import { createModularAccountV2Client } from "@account-kit/smart-contracts";
2import {
3 installValidationActions,
4 getDefaultSingleSignerValidationModuleAddress,
5 SingleSignerValidationModule,
6 modularAccountAbi,
7 AllowlistModule,
8} from "@account-kit/smart-contracts/experimental";
9import { LocalAccountSigner } from "@aa-sdk/core";
10import { sepolia, alchemy } from "@account-kit/infra";
11import { generatePrivateKey } from "viem/accounts";
12
13const client = (
14 await createModularAccountV2Client({
15 chain: sepolia,
16 transport: alchemy({ apiKey: "your-api-key" }),
17 signer: LocalAccountSigner.privateKeyToAccountSigner(generatePrivateKey()),
18 })
19).extend(installValidationActions);
20
21let sessionKeyEntityId = 1;
22
23// Removing a basic session key
24await client.uninstallValidation({
25 moduleAddress: getDefaultSingleSignerValidationModuleAddress(client.chain),
26 entityId: sessionKeyEntityId,
27 uninstallData: SingleSignerValidationModule.encodeOnUninstallData({
28 entityId: sessionKeyEntityId,
29 }),
30 hookUninstallDatas: [],
31});
32
33const hookEntityId = 1;
34const target = "0xd8da6bf26964af9d7eed9e03e53415d37aa96045";
35
36// Removing a session key with an allowlist hook
37await client.uninstallValidation({
38 moduleAddress: getDefaultSingleSignerValidationModuleAddress(client.chain),
39 entityId: sessionKeyEntityId,
40 uninstallData: SingleSignerValidationModule.encodeOnUninstallData({
41 entityId: sessionKeyEntityId,
42 }),
43 hookUninstallDatas: [
44 AllowlistModule.encodeOnUninstallData({
45 entityId: hookEntityId,
46 inputs: [
47 {
48 target,
49 hasSelectorAllowlist: false,
50 hasERC20SpendLimit: false,
51 erc20SpendLimit: 0n,
52 selectors: [],
53 },
54 ],
55 }),
56 ],
57});