pub struct SessionStorage;Expand description
Persistent session key storage using Soroban contract storage.
Session keys are stored in the contract’s persistent storage, keyed
by account address. This allows session keys to survive across
contract invocations (unlike ContractAccount’s in-memory storage).
§Example
use cougr_core::accounts::{SessionKey, SessionScope, SessionStorage};
use soroban_sdk::{symbol_short, testutils::Address as _, Address, BytesN, Env, Vec};
let env = Env::default();
let player_address = Address::generate(&env);
let key_id = BytesN::from_array(&env, &[7u8; 32]);
let session_key = SessionKey {
key_id: key_id.clone(),
scope: SessionScope {
allowed_actions: Vec::from_array(&env, [symbol_short!("move")]),
max_operations: 10,
expires_at: 1_000,
},
created_at: 0,
operations_used: 0,
next_nonce: 0,
};
SessionStorage::store(&env, &player_address, &session_key);
let loaded = SessionStorage::load(&env, &player_address, &key_id);
assert!(loaded.is_some());Implementations§
Source§impl SessionStorage
impl SessionStorage
Sourcepub fn store(env: &Env, account: &Address, key: &SessionKey)
pub fn store(env: &Env, account: &Address, key: &SessionKey)
Store a session key for an account.
If a key with the same key_id already exists, it is overwritten.
Sourcepub fn load(
env: &Env,
account: &Address,
key_id: &BytesN<32>,
) -> Option<SessionKey>
pub fn load( env: &Env, account: &Address, key_id: &BytesN<32>, ) -> Option<SessionKey>
Load a specific session key by ID.
Sourcepub fn load_all(env: &Env, account: &Address) -> Vec<SessionKey>
pub fn load_all(env: &Env, account: &Address) -> Vec<SessionKey>
Load all session keys for an account.
Sourcepub fn remove(env: &Env, account: &Address, key_id: &BytesN<32>) -> bool
pub fn remove(env: &Env, account: &Address, key_id: &BytesN<32>) -> bool
Remove a session key by ID. Returns true if found and removed.
Sourcepub fn increment_usage(
env: &Env,
account: &Address,
key_id: &BytesN<32>,
) -> Result<(), AccountError>
pub fn increment_usage( env: &Env, account: &Address, key_id: &BytesN<32>, ) -> Result<(), AccountError>
Increment the usage count of a session key.
Consume one authorized session action and advance its nonce.
Sourcepub fn cleanup_expired(env: &Env, account: &Address) -> u32
pub fn cleanup_expired(env: &Env, account: &Address) -> u32
Remove all expired session keys for an account. Returns the number of keys removed.
Auto Trait Implementations§
impl Freeze for SessionStorage
impl RefUnwindSafe for SessionStorage
impl Send for SessionStorage
impl Sync for SessionStorage
impl Unpin for SessionStorage
impl UnsafeUnpin for SessionStorage
impl UnwindSafe for SessionStorage
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for C
impl<T, U, V, W, E, C> Compare<(T, U, V, W)> for C
type Error = E
fn compare( &self, a: &(T, U, V, W), b: &(T, U, V, W), ) -> Result<Ordering, <C as Compare<(T, U, V, W)>>::Error>
Source§impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for C
impl<T, U, V, W, X, E, C> Compare<(T, U, V, W, X)> for C
type Error = E
fn compare( &self, a: &(T, U, V, W, X), b: &(T, U, V, W, X), ) -> Result<Ordering, <C as Compare<(T, U, V, W, X)>>::Error>
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Convert
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Convert
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more