spark_rust/wallet/
client.rs

1use super::leaf_manager::LeafManager;
2use crate::error::SparkSdkError;
3use crate::signer::default_signer::DefaultSigner;
4use crate::signer::traits::SparkSigner;
5use crate::wallet::config::WalletConfig;
6use parking_lot::RwLock as PlRwLock;
7use std::sync::Arc;
8
9/// SparkSdk is the main struct for the Spark wallet
10#[derive(Clone)]
11pub struct SparkSdk<S: SparkSigner + Send + Sync = DefaultSigner> {
12    /// The network to connect to
13    pub(crate) config: WalletConfig,
14
15    /// Signer
16    pub(crate) signer: Arc<S>,
17
18    /// Leaf manager
19    pub(crate) leaf_manager: Arc<LeafManager>,
20
21    /// Session
22    pub(crate) session: Arc<PlRwLock<Vec<Session>>>,
23}
24
25impl<S: SparkSigner + Send + Sync> SparkSdk<S> {
26    /// Internal method used to insert the authorization header into a request. This should ideally be replaced with an interceptor in the future.
27    pub(crate) fn add_authorization_header_to_request<T>(
28        &self,
29        request: &mut tonic::Request<T>,
30        index: Option<u32>,
31    ) {
32        let index = index.unwrap_or_else(|| self.config.spark_config.coordinator_index);
33        request.metadata_mut().insert(
34            "authorization",
35            self.session.read().clone()[index as usize]
36                .session_token
37                .clone()
38                .to_string()
39                .parse()
40                .unwrap(),
41        );
42    }
43
44    #[cfg(any(test, feature = "integration-tests"))]
45    pub fn cleanup(&self) -> Result<(), SparkSdkError> {
46        // let signer_filepath = self.signer.read().get_filepath()?;
47        // let leaf_manager_filepath = self.leaf_manager.get_dirpath();
48
49        // Delete the file
50        // std::fs::remove_file(signer_filepath).map_err(|e| SparkSdkError::IoError(e.to_string()))?;
51        // std::fs::remove_dir_all(leaf_manager_filepath)
52        //     .map_err(|e| SparkSdkError::IoError(e.to_string()))?;
53
54        Ok(())
55    }
56}
57
58/// Session is a struct that represents a session
59#[derive(Debug, Clone)]
60pub struct Session {
61    /// The session token
62    pub(crate) session_token: String,
63
64    /// The expiration timestamp
65    pub(crate) _expiration_timestamp: i64, // TODO: add auto-refresh until Spark Authn server adds refresh token support
66}