Installation
First, add the session-keys crate to your Cargo.toml:This page uses the V1
SessionToken. The React hooks in
@magicblock-labs/gum-react-sdk (useSessionKeyManager().createSession and
revokeSession) create and revoke V1 tokens. The session program also has a
V2 SessionTokenV2 (created with create_session_v2), which records the fee
payer and can only be revoked by its authority while active. The two types
are not interchangeable: an instruction that expects one rejects the other
with AccountDiscriminatorMismatch (error 3002). Use the token type that your
client creates. Any wallet can revoke an active V1 token.Usage
- Importing session-keys:
- Deriving the Session trait:
Session trait is derived on the Increment struct, enabling session functionality.
- Defining the session token account:
SessionToken account, specifying the signer and authority for the session.
- session_token.authority: account which created the session token
- counter.authority.key(): account which created the counter The authority condition checks if the session token is created by the same user as the counter.
- Using the
session_auth_ormacro:
increment function.
It checks for a valid session token, or if not present, verifies that the signer is the counter’s authority.
Full Example
Here’s a complete example of a counter program using session keys. Each user gets his own counter account, so we can show how authentication is done with session keys.Tests
Here’s an example of how to test the counter program with session keys:Testing locally
To test it locally withsolana-test-validator, you need to start it with the session keys program and account.
- Make sure your Solana CLI points to DEVNET:
- Dump Session Keys program to local file:
- Start solana-test-validator with session keys program and account:
-r- reset the ledger to genesis-ud- URL for Solana’s JSON RPC or moniker (-ud = DEVNET)--clone- Copy an account from the cluster--bpf-program- add a SBF program to the genesis configuration

