Tutorial

Example of a session key with an action policy.

1 - Create a Session Key

import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
import {
    type ComethSmartAccountClient,
    type SafeSigner,
    erc7579Actions,
    smartSessionActions,
} from "@cometh/connect-sdk-4337";

export const COUNTER_CONTRACT_ADDRESS =
    "0x4FbF9EE4B2AF774D4617eAb027ac2901a41a7b5F";


const safe7559AccountClient = smartAccountClient.extend(smartSessionActions())
            .extend(erc7579Actions());

const privateKey = generatePrivateKey();
const sessionOwner = privateKeyToAccount(privateKey);

const createSessionsResponse = await safe7559AccountClient.grantPermission({
    sessionRequestedInfo: [
        {
            sessionPublicKey: sessionOwner.address,
            actionPoliciesInfo: [
                {
                    contractAddress: COUNTER_CONTRACT_ADDRESS,
                    functionSelector: toFunctionSelector(
                        "function count()"
                    ) as Hex,
                },
            ],
        },
    ],
});

await safe7559AccountClient.waitForUserOperationReceipt({
    hash: createSessionsResponse.userOpHash,
});

2 - Store the Session Key

In our example, we will store the session key details in local storage. You are free to store it wherever you want.

3 - Use the Session Key

Last updated