Working with Sessions in Swig
“You can fit so many sessions in this bad boy” - Toly slapping the hood of a gently used 2012 Swig (probably)
This guide explains how to work with sessions in Swig, which allow you to create temporary authorities with limited permissions. Sessions are perfect for dApp integrations where you need to delegate authority for a specific duration. You can find all example code in the swig-ts/examples/transfer directory.
bun start-validator
command.Creating a Basic Session
The simplest way to create a session is with Ed25519 keys. Here are the key parts:
Using Session Authority
Once a session is created, you can use it to sign transactions within its duration:
Secp256k1 Sessions (EVM Compatible)
For EVM compatibility, you can create sessions with Secp256k1 keys:
Revoking Sessions
You can revoke a session by creating a new session with an all-zero session key:
Key Aspects of Sessions
Sessions in Swig have several important characteristics:
- Temporary Authority: Sessions expire after their specified duration
- Unique Keys: Each session must use a unique key (can’t reuse previous session keys)
- Root Override: The root authority can always override or revoke session permissions
- Inheritance: Sessions inherit permissions from their root role
- Multiple Sessions: You can have multiple active sessions for the same root authority
Testing Environment Options
These examples can be run in different environments:
-
Local Validator: Use
transfer-local-session.ts
- Requires running a local Solana validator
- Real blockchain environment
- Good for final testing
-
LiteSVM: Use
transfer-svm-session.ts
ortransfer-svm-secp-session.ts
- No validator required
- Instant transaction confirmation
- Perfect for rapid development
- Simulates the Solana runtime
-
Devnet: All examples work with devnet
- Change connection URL to
https://api.devnet.solana.com
- Real network environment
- Free to use with airdropped SOL
- Change connection URL to
LiteSVM is recommended for initial development as it provides the fastest feedback loop. Move to local validator or devnet for more thorough testing.