Using session keys
How to create and manage session keys
Session keys are programmable access tokens with specific permissions, designed for controlled interactions. Examples include:
- Granting access to specific areas or features.
- Limiting usage to a set amount of resources (e.g., 1000 units of currency).
- Time-bound validity (e.g., expiring after 3 days). P ermissions can be combined, enabling fine-tuned, context-specific capabilities.
Session Keys streamline interactions by:
- Seamless Interactions: Reducing the need for repeated confirmations by delegating specific permissions to locally stored keys, enhancing user experience.
- Automated Processes: Enabling automation through server-bound keys with defined permissions for tasks like recurring actions, resource management, or condition-based triggers.
Check out our sample registering a session key with an account with a non-custodial signer: GitHub source.
Configuration#
This section will guide you through the process of registering a session key and using it to mint an asset with a player's smart account. You can choose to create the session key using a web3 libraries or using the Openfort native method.
Using EIP-7715. The request
method of the EIP-1193 provider can be used to request signatures. First, get the provider:
Smart wallets support sending a batch of transactions in a single, atomic submission to the network.
To register a session key with a smart wallet, call the wallet_grantPermissions
method.
As an example, you might batch together a transaction to approve a USDC spender and to transfer USDC like so:
_29import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';_29_29const sessionKey = generatePrivateKey();_29const accountSession = privateKeyToAccount(sessionKey).address;_29_29await provider.request({_29 method: 'wallet_grantPermissions',_29 params: [_29 {_29 signer:{_29 type: "account",_29 data:{_29 id: accountSession_29 }_29 },_29 expiry: 60 * 60 * 24,_29 permissions: [_29 {_29 type: 'contract-call',_29 data: {_29 address: '0x2522f4fc9af2e1954a3d13f7a5b2683a00a4543a',_29 calls: []_29 },_29 policies: []_29 }_29 ],_29 },_29 ],_29});
Popular web3 libraries provide convenient methods for registering session keys:
Library | Method |
---|---|
Viem | Use the wallet_grantPermissions action |