spark_sdk/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    /// Identity public key
16    pub(crate) identity_public_key: Arc<Vec<u8>>,
17
18    /// Signer
19    pub(crate) signer: Arc<S>,
20
21    /// Leaf manager
22    pub(crate) leaf_manager: Arc<LeafManager>,
23
24    /// Session
25    pub(crate) session: Arc<PlRwLock<Vec<Session>>>,
26}
27
28impl<S: SparkSigner + Send + Sync> SparkSdk<S> {
29    /// Internal method used to insert the authorization header into a request. This should ideally be replaced with an interceptor in the future.
30    pub(crate) fn add_authorization_header_to_request<T>(
31        &self,
32        request: &mut tonic::Request<T>,
33        index: Option<u32>,
34    ) {
35        let index = index.unwrap_or_else(|| self.config.spark_config.coordinator_index);
36        request.metadata_mut().insert(
37            "authorization",
38            format!(
39                "{}",
40                self.session.read().clone()[index as usize]
41                    .session_token
42                    .clone()
43            )
44            .parse()
45            .unwrap(),
46        );
47    }
48
49    #[cfg(any(test, feature = "integration-tests"))]
50    pub fn cleanup(&self) -> Result<(), SparkSdkError> {
51        // let signer_filepath = self.signer.read().get_filepath()?;
52        // let leaf_manager_filepath = self.leaf_manager.get_dirpath();
53
54        // Delete the file
55        // std::fs::remove_file(signer_filepath).map_err(|e| SparkSdkError::IoError(e.to_string()))?;
56        // std::fs::remove_dir_all(leaf_manager_filepath)
57        //     .map_err(|e| SparkSdkError::IoError(e.to_string()))?;
58
59        Ok(())
60    }
61}
62
63/// Session is a struct that represents a session
64#[derive(Debug, Clone)]
65pub struct Session {
66    /// The session token
67    pub(crate) session_token: String,
68
69    /// The expiration timestamp
70    pub(crate) expiration_timestamp: i64,
71}