7 stable releases

new 2.0.7 Apr 25, 2025
2.0.6 Jan 6, 2025
2.0.5 Jun 21, 2024
2.0.4 May 7, 2024
2.0.1 Sep 11, 2023

#2 in #magicblock

Download history 300/week @ 2025-01-03 195/week @ 2025-01-10 171/week @ 2025-01-17 107/week @ 2025-01-24 93/week @ 2025-01-31 88/week @ 2025-02-07 90/week @ 2025-02-14 168/week @ 2025-02-21 154/week @ 2025-02-28 108/week @ 2025-03-07 136/week @ 2025-03-14 114/week @ 2025-03-21 115/week @ 2025-03-28 120/week @ 2025-04-04 147/week @ 2025-04-11 541/week @ 2025-04-18

959 downloads per month
Used in bolt-lang

GPL-3.0-or-later

11KB
169 lines

GPL Session

Manage sessions in your Solana Anchor Programs.

Installation

cargo add session-keys --features no-entrypoint

Usage

  1. Import the dependencies
use session_keys::{SessionError, SessionToken, session_auth_or, Session};
  1. Derive the Session trait on your instruction struct
#[derive(Accounts, Session)]
pub struct Instruction<'info> {
    .....
    pub user: Account<'info, User>,

    #[session(
        // The ephemeral keypair signing the transaction
        signer = signer,
        // The authority of the user account which must have created the session
        authority = user.authority.key()
    )]
    // Session Tokens are passed as optional accounts
    pub session_token: Option<Account<'info, SessionToken>>,

    #[account(mut)]
    pub signer: Signer<'info>,
    .....
}
  1. Add the session_auth_or macro to your instruction handler with fallback logic on who the instruction should validate the signer when sessions are not present and an appropirate ErrorCode. If you've used require*! macros in anchor_lang you already know how this works.
#[session_auth_or(
    ctx.accounts.user.authority.key() == ctx.accounts.authority.key(),
    ErrorCode
)]
pub fn ix_handler(ctx: Context<Instruction>,) -> Result<()> {
.....
}

Example

See KamikazeJoe for an example of a game using session-keys.

Dependencies

~12–21MB
~329K SLoC