Skip to main content

breez_sdk_spark/session_store/
adapter.rs

1use std::sync::Arc;
2
3use bitcoin::secp256k1::PublicKey;
4
5use super::SessionStore;
6
7/// Adapts an SDK-facing [`SessionStore`] to the [`spark_wallet`] session-store
8/// trait (which has its own identical-shape trait).
9///
10/// Used by the WASM bindings to plumb a JS-side session store into a
11/// caller-supplied [`StorageBackend`](crate::StorageBackend).
12pub struct SessionStoreAdapter(pub Arc<dyn SessionStore>);
13
14impl SessionStoreAdapter {
15    /// Wraps an SDK-facing [`SessionStore`] so it can be used as a
16    /// [`spark_wallet`] session store.
17    #[must_use]
18    pub fn new(inner: Arc<dyn SessionStore>) -> Self {
19        Self(inner)
20    }
21}
22
23#[macros::async_trait]
24impl spark_wallet::SessionStore for SessionStoreAdapter {
25    async fn get_session(
26        &self,
27        service_identity_key: &PublicKey,
28    ) -> Result<spark_wallet::Session, spark_wallet::SessionStoreError> {
29        self.0
30            .get_session(*service_identity_key)
31            .await
32            .map(Into::into)
33            .map_err(Into::into)
34    }
35
36    async fn set_session(
37        &self,
38        service_identity_key: &PublicKey,
39        session: spark_wallet::Session,
40    ) -> Result<(), spark_wallet::SessionStoreError> {
41        self.0
42            .set_session(*service_identity_key, session.into())
43            .await
44            .map_err(Into::into)
45    }
46}
47
48/// Adapts a [`spark_wallet`] session store to the SDK-facing [`SessionStore`]
49/// trait (the reverse of [`SessionStoreAdapter`]), so a storage backend's own
50/// session store can be handed to an integrator (via
51/// [`default_session_store`](crate::default_session_store)) to wrap.
52pub(crate) struct SparkSessionStoreAdapter(pub(crate) Arc<dyn spark_wallet::SessionStore>);
53
54#[macros::async_trait]
55impl SessionStore for SparkSessionStoreAdapter {
56    async fn get_session(
57        &self,
58        service_identity_key: PublicKey,
59    ) -> Result<super::Session, super::SessionStoreError> {
60        self.0
61            .get_session(&service_identity_key)
62            .await
63            .map(Into::into)
64            .map_err(Into::into)
65    }
66
67    async fn set_session(
68        &self,
69        service_identity_key: PublicKey,
70        session: super::Session,
71    ) -> Result<(), super::SessionStoreError> {
72        self.0
73            .set_session(&service_identity_key, session.into())
74            .await
75            .map_err(Into::into)
76    }
77}