kora_lib/
state.rs

1use once_cell::sync::Lazy;
2use parking_lot::RwLock;
3use std::sync::Arc;
4
5use crate::{error::KoraError, signer::KoraSigner};
6
7static GLOBAL_SIGNER: Lazy<Arc<RwLock<Option<KoraSigner>>>> =
8    Lazy::new(|| Arc::new(RwLock::new(None)));
9
10/// Initialize the global signer with a KoraSigner instance
11pub fn init_signer(signer: KoraSigner) -> Result<(), KoraError> {
12    let mut signer_guard = GLOBAL_SIGNER.write();
13    if signer_guard.is_some() {
14        return Err(KoraError::InternalServerError("Signer already initialized".to_string()));
15    }
16
17    *signer_guard = Some(signer);
18    Ok(())
19}
20
21/// Get a reference to the global signer
22pub fn get_signer() -> Result<Arc<KoraSigner>, KoraError> {
23    let signer_guard = GLOBAL_SIGNER.read();
24    match &*signer_guard {
25        Some(signer) => Ok(Arc::new(signer.clone())),
26        None => Err(KoraError::InternalServerError("Signer not initialized".to_string())),
27    }
28}