Skip to content

Using Session Keys

Estimated time to read: 2 minutes

Learn how to use the Session Keys feature and pre-approve the transactions issued through the smart contract wallet account by setting the time and transaction value limits.

Only for Third-party wallets

The Arcana Gasless (Standalone) SDK can be used for apps that require the session keys feature enabled for transactions via any supported third-party browser-based wallets.

To enable session keys feature in the Arcana wallet, integrate the app with the Arcana Auth SDK and the Arcana Gasless (Standalone) SDK.

Prerequisites

1. Configure Session Keys

  import { SCW, StorageType  } from "@arcana/scw";

  let scWallet: SCW;  

  scWallet = new SCW();
  await scWallet.init({
          arcana_key: arcana_app_id, 
          provider: window.arcana.provider,
          private_key: none,
          rpc_url: none,
          gateway_url: none,
          sessionStorageType: StorageType.LOCAL_STORAGE });

  scwAddress = await scWallet.getSCWAddress();
  console.log("Address: " + scwAddress);

  sess = scWallet.initSession(StorageType.LOCAL_STORAGE); // Options: LOCAL_STORAGE, MEMORY_STORAGE

  const contractAddress = "0xFeCD581c539f8858c556Ab8FEf681975a6A25ACa";
  const functionSelector = "deposit()";
  const rules = [{
      offset: 0,
      condition: 0,
      referenceValue: "0x7a8713E21e7434dC5441Fb666D252D13F380a97d",
    }]

  const config: CreateSessionParam = {
    contractAddress: getUsdcContract(scWallet.chain_id), //Specify your contract, this example uses some value
    functionSelector: "transfer(address,uint256)",  //specify function in the contract listed above
    rules: rules, // Specify rules for the session policy
    validUntil: 0,  //no end time, infinite always allow
    validAfter: 0,   //no start time, infinite always allow
    valueLimit: 10,   //maximum 10 GWEI transaction is pre approved
  };

  await scWallet.createSession(config);  //wait for user approval via the UI pop up accept/reject notification

  ...

  // Perform doTx() after setting up managed session rules via createSession

  ...

StorageType

To initialize a session with initSession, choose either:

  • StorageType.LOCAL_STORAGE
  • StorageType.MEMORY_STORAGE

Selecting MEMORY_STORAGE means the session keys won’t persist if you close and reopen the browser/tab or reboot the device.

2. Initiate Transactions

    let amount = inputValue;
    const erc20Address =  getLinkContract(scWallet.chain_id) // getUsdcContract(scWallet.chain_id);
    const toAddress = "0x7a8713E21e7434dC5441Fb666D252D13F380a97d";
    const Erc20Interface = new Interface(erc20abi);

    const encodedData = Erc20Interface.encodeFunctionData("transfer", [
      toAddress,
      amount,
    ]);

    // You need to create transaction objects of the following interface
    const tx1 = {
      from: scWallet.getSCWAddress(),
      to: erc20Address, // destination smart contract address
      data: encodedData,
    };

    /// Session txn
    let tx = await scWallet.doTx(tx1, {
      session: true, //default is false, provide session id to be specific
    });
    tx = await tx.wait();  //If this transaction is within specified limits, it will not require user confirmation
    console.log(`Transfer done ${tx.userOpHash}`);

What's Next?

After adding code for enabling session keys via third-party wallets in apps integrated with the Arcana Gasless (Standalone) SDK, deploy and run the app. Learn more...

See also

'Gasless' integration example: See sample-gasless-metamask-only submodule in Auth Examples

Arcana Gasless (Standalone) SDK Quick Links


Last update: October 29, 2024 by shaloo