Function createSession

  • createSession

    Creates a session for a user's smart account. This grants a dapp permission to execute a specific function on a specific contract on behalf of a user. Permissions can be specified by the dapp in the form of rulesRule, and then submitted to the user for approval via signing. The session keys granted with the imparted policy are stored in a StorageClient ISessionStorage. They can later be retrieved and used to validate userops.

    Parameters

    Returns Promise<SessionGrantedPayload>

    Promise<SessionGrantedPayload> - An object containing the status of the transaction and the sessionID.

    Example

    import { createClient } from "viem"
    import { createSmartAccountClient } from "@biconomy/account"
    import { createWalletClient, http } from "viem";
    import { polygonAmoy } from "viem/chains";
    import { SessionFileStorage } from "@biconomy/session-file-storage";
    const signer = createWalletClient({
    account,
    chain: polygonAmoy,
    transport: http(),
    });

    const smartAccount = await createSmartAccountClient({ signer, bundlerUrl, paymasterUrl }); // Retrieve bundler/paymaster url from dashboard
    const smartAccountAddress = await smartAccount.getAccountAddress();
    const nftAddress = "0x1758f42Af7026fBbB559Dc60EcE0De3ef81f665e"
    const sessionStorage = new SessionFileStorage(smartAccountAddress)
    const sessionKeyAddress = (await sessionStorage.addSigner(undefined, polygonAmoy)).getAddress();

    const { wait, sessionID } = await createSession(
    smartAccount,
    [
    {
    sessionKeyAddress,
    contractAddress: nftAddress,
    functionSelector: "safeMint(address)",
    rules: [
    {
    offset: 0,
    condition: 0,
    referenceValue: smartAccountAddress
    }
    ],
    interval: {
    validUntil: 0,
    validAfter: 0
    },
    valueLimit: 0n
    }
    ],
    sessionStorage,
    {
    paymasterServiceData: { mode: PaymasterMode.SPONSORED },
    }
    )

    const {
    receipt: { transactionHash },
    success
    } = await wait();

    console.log({ sessionID, success }); // Use the sessionID later to retrieve the sessionKey from the storage client