Installation
First, add the session-keys crate to your Cargo.toml: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_or
macro:
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