Implement session keys

Let's go through the steps on how you could enable sessions in your dApps with Argent.

Pre-requisites

  • pnpm add starknetkit@2.2.7 (or newer versions)

    • can be installed with pnpm add starknetkit@next

    • required in order to get the deployment data for an account

  • pnpm add @argent/x-sessions@next

Connect using starknetkit

Using starknetkit modal, with all default connectors

const { wallet, connectorData, connector } = await connect({
  webWalletUrl: ARGENT_WEBWALLET_URL, // env var for webwallet url
  argentMobileOptions: {
    dappName: "Starknetkit example dapp",
    url: window.location.hostname,
    chainId: constants.NetworkName.SN_MAIN,
    icons: [],
  },
})

Using starknetkit modal, with all specific connectors

const { wallet, connectorData, connector } = await connect({
  connectors: [
	  new InjectedConnector({ options: { id: "argentX" } }),
	  new InjectedConnector({ options: { id: "braavos" } }),
	  new ArgentMobileConnector({
	    dappName: "Example dapp",
	    chainId: CHAIN_ID,
	  }),
	  new WebWalletConnector({ url: ARGENT_WEBWALLET_URL })
  ]
})

ARGENT_WEBWALLET_URL

Get deployment data for an account

When connecting with starknetkit, use the wallet object returned by connect .

If an account is already deployed, this will throw an exception (so it will need to be managed on client side)

const { wallet, connectorData } = res;
await wallet.request({
    type: "wallet_deploymentData",
})

Create a session

Before the user can execute transactions, a session needs to be created.

The dapp will need to create a dappKey , both public and private will be needed.

const privateKey = ec.starkCurve.utils.randomPrivateKey();
const dappKey: DappKey = {
  privateKey,
  publicKey: ec.starkCurve.getStarkKey(privateKey),
};

When creating a session, 2 values needs to be stored:

  • sessionRequest, created using createSessionRequest from @argent/x-sessions

  • sessionSignature, that requires signing a message and created using openSession from @argent/x-sessions

const sessionParams: SessionParams = {
  allowedMethods: [
	  {
	    "Contract Address": ETHTokenAddress,
	    selector: "transfer",
	  },
	],
  expiry: Math.floor((Date.now() + 1000 * 60 * 60 * 24) / 1000) as any;,
  metaData: {
	  projectID: "test-dapp",
	  txFees: [
		  {
		    tokenAddress: ETHTokenAddress,
		    maxAmount: parseUnits("0.1", 18).value.toString(),
		  },
		],
	},
  publicDappKey: dappKey.publicKey,
};

const sessionRequest = createSessionRequest(allowedMethods, expiry, metaData(isStarkFeeToken), dappKey.publicKey);
setSessionRequest(sessionRequest);

const accountSessionSignature = await openSession({
  chainId: await provider.getChainId(),
  wallet: wallet as any,
  sessionParams,
});

setAccountSessionSignature(accountSessionSignature);

Execute a tx using session keys

In order to execute a tx, a session account is needed.

To create this account, sessionRequest and accountSessionSignature created on the step before a required.

const sessionAccount = await buildSessionAccount({
  accountSessionSignature: stark.formatSignature(accountSessionSignature),
  sessionRequest,
  provider,
  chainId,
  address,
  dappKey,
  argentSessionServiceBaseUrl: ARGENT_SESSION_SERVICE_BASE_URL,
});

You can now use this account to submit transactions.

ARGENT_SESSION_SERVICE_BASE_URL

Last updated