Browse Source

feat: new mem localstore

thesimplekid 1 year ago
parent
commit
3e6a9eb3b2

+ 1 - 1
crates/cashu-sdk/Cargo.toml

@@ -10,7 +10,7 @@ license.workspace = true
 
 
 [features]
-default = ["mint", "wallet", "all-nuts"]
+default = ["mint", "wallet", "all-nuts", "redb"]
 mint = ["cashu/mint"]
 wallet = ["cashu/wallet", "dep:minreq", "dep:once_cell"]
 gloo = ["dep:gloo"]

+ 35 - 1
crates/cashu-sdk/src/mint/localstore/memory.rs

@@ -3,7 +3,7 @@ use std::sync::Arc;
 
 use async_trait::async_trait;
 use cashu::nuts::nut02::mint::KeySet;
-use cashu::nuts::{CurrencyUnit, Id, Proof};
+use cashu::nuts::{CurrencyUnit, Id, Proof, Proofs};
 use cashu::secret::Secret;
 use cashu::types::{MeltQuote, MintQuote};
 use tokio::sync::Mutex;
@@ -20,6 +20,40 @@ pub struct MemoryLocalStore {
     spent_proofs: Arc<Mutex<HashMap<Secret, Proof>>>,
 }
 
+impl MemoryLocalStore {
+    pub fn new(
+        active_keysets: HashMap<CurrencyUnit, Id>,
+        keysets: Vec<KeySet>,
+        mint_quotes: Vec<MintQuote>,
+        melt_quotes: Vec<MeltQuote>,
+        pending_proofs: Proofs,
+        spent_proofs: Proofs,
+    ) -> Self {
+        Self {
+            active_keysets: Arc::new(Mutex::new(active_keysets)),
+            keysets: Arc::new(Mutex::new(keysets.into_iter().map(|k| (k.id, k)).collect())),
+            mint_quotes: Arc::new(Mutex::new(
+                mint_quotes.into_iter().map(|q| (q.id.clone(), q)).collect(),
+            )),
+            melt_quotes: Arc::new(Mutex::new(
+                melt_quotes.into_iter().map(|q| (q.id.clone(), q)).collect(),
+            )),
+            pending_proofs: Arc::new(Mutex::new(
+                pending_proofs
+                    .into_iter()
+                    .map(|p| (p.secret.clone(), p))
+                    .collect(),
+            )),
+            spent_proofs: Arc::new(Mutex::new(
+                spent_proofs
+                    .into_iter()
+                    .map(|p| (p.secret.clone(), p))
+                    .collect(),
+            )),
+        }
+    }
+}
+
 #[async_trait(?Send)]
 impl LocalStore for MemoryLocalStore {
     async fn add_active_keyset(&self, unit: CurrencyUnit, id: Id) -> Result<(), Error> {

+ 3 - 3
crates/cashu-sdk/src/mint/localstore/mod.rs

@@ -1,6 +1,6 @@
-mod memory;
+pub mod memory;
 #[cfg(all(not(target_arch = "wasm32"), feature = "redb"))]
-mod redb_store;
+pub mod redb_store;
 
 use std::collections::HashMap;
 
@@ -9,7 +9,7 @@ use cashu::nuts::nut02::mint::KeySet;
 use cashu::nuts::{CurrencyUnit, Id, Proof};
 use cashu::secret::Secret;
 use cashu::types::{MeltQuote, MintQuote};
-#[cfg(all(not(target_arch = "wasm32"), feature = "redb"))]
+pub use memory::MemoryLocalStore;
 pub use redb_store::RedbLocalStore;
 use thiserror::Error;
 

+ 12 - 16
crates/cashu-sdk/src/mint/localstore/redb_store.rs

@@ -79,13 +79,11 @@ impl LocalStore for RedbLocalStore {
 
         let mut active_keysets = HashMap::new();
 
-        for keyset in table.iter()? {
-            if let Ok((unit, id)) = keyset {
-                let unit = serde_json::from_str(unit.value())?;
-                let id = serde_json::from_str(id.value())?;
+        for (unit, id) in (table.iter()?).flatten() {
+            let unit = serde_json::from_str(unit.value())?;
+            let id = serde_json::from_str(id.value())?;
 
-                active_keysets.insert(unit, id);
-            }
+            active_keysets.insert(unit, id);
         }
 
         Ok(active_keysets)
@@ -115,7 +113,7 @@ impl LocalStore for RedbLocalStore {
 
         let keyset = table.get(keyset_id.to_string().as_str())?;
 
-        Ok(keyset.map(|k| serde_json::from_str(&k.value()).unwrap()))
+        Ok(keyset.map(|k| serde_json::from_str(k.value()).unwrap()))
     }
 
     async fn get_keysets(&self) -> Result<Vec<KeySet>, Error> {
@@ -125,12 +123,10 @@ impl LocalStore for RedbLocalStore {
 
         let mut keysets = Vec::new();
 
-        for keyset in table.iter()? {
-            if let Ok((_id, keyset)) = keyset {
-                let keyset = serde_json::from_str(keyset.value())?;
+        for (_id, keyset) in (table.iter()?).flatten() {
+            let keyset = serde_json::from_str(keyset.value())?;
 
-                keysets.push(keyset)
-            }
+            keysets.push(keyset)
         }
 
         Ok(keysets)
@@ -157,7 +153,7 @@ impl LocalStore for RedbLocalStore {
 
         let quote = table.get(quote_id)?;
 
-        Ok(quote.map(|q| serde_json::from_str(&q.value()).unwrap()))
+        Ok(quote.map(|q| serde_json::from_str(q.value()).unwrap()))
     }
 
     async fn remove_mint_quote(&self, quote_id: &str) -> Result<(), Error> {
@@ -195,7 +191,7 @@ impl LocalStore for RedbLocalStore {
 
         let quote = table.get(quote_id)?;
 
-        Ok(quote.map(|q| serde_json::from_str(&q.value()).unwrap()))
+        Ok(quote.map(|q| serde_json::from_str(q.value()).unwrap()))
     }
 
     async fn remove_melt_quote(&self, quote_id: &str) -> Result<(), Error> {
@@ -236,7 +232,7 @@ impl LocalStore for RedbLocalStore {
 
         let quote = table.get(secret.to_string().as_str())?;
 
-        Ok(quote.map(|q| serde_json::from_str(&q.value()).unwrap()))
+        Ok(quote.map(|q| serde_json::from_str(q.value()).unwrap()))
     }
 
     async fn add_pending_proof(&self, proof: Proof) -> Result<(), Error> {
@@ -263,7 +259,7 @@ impl LocalStore for RedbLocalStore {
 
         let quote = table.get(secret.to_string().as_str())?;
 
-        Ok(quote.map(|q| serde_json::from_str(&q.value()).unwrap()))
+        Ok(quote.map(|q| serde_json::from_str(q.value()).unwrap()))
     }
 
     async fn remove_pending_proof(&self, secret: &Secret) -> Result<(), Error> {

+ 2 - 2
crates/cashu-sdk/src/mint/mod.rs

@@ -19,8 +19,8 @@ use tracing::{debug, info};
 use crate::Mnemonic;
 
 mod localstore;
-
-pub use localstore::LocalStore;
+use localstore::LocalStore;
+pub use localstore::MemoryLocalStore;
 #[cfg(all(not(target_arch = "wasm32"), feature = "redb"))]
 pub use localstore::RedbLocalStore;
 

+ 23 - 0
crates/cashu-sdk/src/wallet/localstore/memory.rs

@@ -19,6 +19,29 @@ pub struct MemoryLocalStore {
     proofs: Arc<Mutex<HashMap<UncheckedUrl, HashSet<Proof>>>>,
 }
 
+impl MemoryLocalStore {
+    pub fn new(
+        mint_quotes: Vec<MintQuote>,
+        melt_quotes: Vec<MeltQuote>,
+        mint_keys: Vec<Keys>,
+    ) -> Self {
+        Self {
+            mints: Arc::new(Mutex::new(HashMap::new())),
+            mint_keysets: Arc::new(Mutex::new(HashMap::new())),
+            mint_quotes: Arc::new(Mutex::new(
+                mint_quotes.into_iter().map(|q| (q.id.clone(), q)).collect(),
+            )),
+            melt_quotes: Arc::new(Mutex::new(
+                melt_quotes.into_iter().map(|q| (q.id.clone(), q)).collect(),
+            )),
+            mint_keys: Arc::new(Mutex::new(
+                mint_keys.into_iter().map(|k| (Id::from(&k), k)).collect(),
+            )),
+            proofs: Arc::new(Mutex::new(HashMap::new())),
+        }
+    }
+}
+
 #[async_trait(?Send)]
 impl LocalStore for MemoryLocalStore {
     async fn add_mint(

+ 2 - 1
crates/cashu-sdk/src/wallet/localstore/mod.rs

@@ -1,7 +1,7 @@
 mod memory;
 
 #[cfg(all(not(target_arch = "wasm32"), feature = "redb"))]
-pub mod redb_store;
+mod redb_store;
 
 use std::collections::HashMap;
 
@@ -9,6 +9,7 @@ use async_trait::async_trait;
 use cashu::nuts::{Id, KeySetInfo, Keys, MintInfo, Proofs};
 use cashu::types::{MeltQuote, MintQuote};
 use cashu::url::UncheckedUrl;
+pub use memory::MemoryLocalStore;
 #[cfg(all(not(target_arch = "wasm32"), feature = "redb"))]
 pub use redb_store::RedbLocalStore;
 use thiserror::Error;

+ 1 - 20
crates/cashu-sdk/src/wallet/mod.rs

@@ -60,26 +60,7 @@ pub struct Wallet<C: Client, L: LocalStore> {
 }
 
 impl<C: Client, L: LocalStore> Wallet<C, L> {
-    pub async fn new(
-        client: C,
-        localstore: L,
-        mint_quotes: Vec<MintQuote>,
-        melt_quotes: Vec<MeltQuote>,
-        backup_info: Option<BackupInfo>,
-        mint_keys: Vec<Keys>,
-    ) -> Self {
-        for quote in mint_quotes {
-            localstore.add_mint_quote(quote).await.ok();
-        }
-
-        for quote in melt_quotes {
-            localstore.add_melt_quote(quote).await.ok();
-        }
-
-        for keys in mint_keys {
-            localstore.add_keys(keys).await.ok();
-        }
-
+    pub async fn new(client: C, localstore: L, backup_info: Option<BackupInfo>) -> Self {
         Self {
             backup_info,
             client,