From f696ef9a92e712e262e02c3fab89534586e74428 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sun, 14 Jan 2024 23:12:38 +0000 Subject: [PATCH] feat: get_pubkeys --- .../cashu-sdk/src/mint/localstore/memory.rs | 8 +++++ crates/cashu-sdk/src/mint/localstore/mod.rs | 2 ++ .../src/mint/localstore/redb_store.rs | 32 +++++++++++++++++++ crates/cashu-sdk/src/mint/mod.rs | 15 +++++++-- 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/crates/cashu-sdk/src/mint/localstore/memory.rs b/crates/cashu-sdk/src/mint/localstore/memory.rs index c68bb5fb..7d5e363a 100644 --- a/crates/cashu-sdk/src/mint/localstore/memory.rs +++ b/crates/cashu-sdk/src/mint/localstore/memory.rs @@ -94,6 +94,10 @@ impl LocalStore for MemoryLocalStore { Ok(self.mint_quotes.lock().await.get(quote_id).cloned()) } + async fn get_mint_quotes(&self) -> Result, Error> { + Ok(self.mint_quotes.lock().await.values().cloned().collect()) + } + async fn remove_mint_quote(&self, quote_id: &str) -> Result<(), Error> { self.mint_quotes.lock().await.remove(quote_id); @@ -112,6 +116,10 @@ impl LocalStore for MemoryLocalStore { Ok(self.melt_quotes.lock().await.get(quote_id).cloned()) } + async fn get_melt_quotes(&self) -> Result, Error> { + Ok(self.melt_quotes.lock().await.values().cloned().collect()) + } + async fn remove_melt_quote(&self, quote_id: &str) -> Result<(), Error> { self.melt_quotes.lock().await.remove(quote_id); diff --git a/crates/cashu-sdk/src/mint/localstore/mod.rs b/crates/cashu-sdk/src/mint/localstore/mod.rs index b25e2fcf..64709861 100644 --- a/crates/cashu-sdk/src/mint/localstore/mod.rs +++ b/crates/cashu-sdk/src/mint/localstore/mod.rs @@ -47,10 +47,12 @@ pub trait LocalStore { async fn add_mint_quote(&self, quote: MintQuote) -> Result<(), Error>; async fn get_mint_quote(&self, quote_id: &str) -> Result, Error>; + async fn get_mint_quotes(&self) -> Result, Error>; async fn remove_mint_quote(&self, quote_id: &str) -> Result<(), Error>; async fn add_melt_quote(&self, quote: MeltQuote) -> Result<(), Error>; async fn get_melt_quote(&self, quote_id: &str) -> Result, Error>; + async fn get_melt_quotes(&self) -> Result, Error>; async fn remove_melt_quote(&self, quote_id: &str) -> Result<(), Error>; async fn add_keyset(&self, keyset: KeySet) -> Result<(), Error>; diff --git a/crates/cashu-sdk/src/mint/localstore/redb_store.rs b/crates/cashu-sdk/src/mint/localstore/redb_store.rs index 8829b401..fccb5f39 100644 --- a/crates/cashu-sdk/src/mint/localstore/redb_store.rs +++ b/crates/cashu-sdk/src/mint/localstore/redb_store.rs @@ -156,6 +156,22 @@ impl LocalStore for RedbLocalStore { Ok(quote.map(|q| serde_json::from_str(q.value()).unwrap())) } + async fn get_mint_quotes(&self) -> Result, Error> { + let db = self.db.lock().await; + let read_txn = db.begin_read()?; + let table = read_txn.open_table(MINT_QUOTES_TABLE)?; + + let mut quotes = Vec::new(); + + for (_id, quote) in (table.iter()?).flatten() { + let quote = serde_json::from_str(quote.value())?; + + quotes.push(quote) + } + + Ok(quotes) + } + async fn remove_mint_quote(&self, quote_id: &str) -> Result<(), Error> { let db = self.db.lock().await; @@ -194,6 +210,22 @@ impl LocalStore for RedbLocalStore { Ok(quote.map(|q| serde_json::from_str(q.value()).unwrap())) } + async fn get_melt_quotes(&self) -> Result, Error> { + let db = self.db.lock().await; + let read_txn = db.begin_read()?; + let table = read_txn.open_table(MELT_QUOTES_TABLE)?; + + let mut quotes = Vec::new(); + + for (_id, quote) in (table.iter()?).flatten() { + let quote = serde_json::from_str(quote.value())?; + + quotes.push(quote) + } + + Ok(quotes) + } + async fn remove_melt_quote(&self, quote_id: &str) -> Result<(), Error> { let db = self.db.lock().await; diff --git a/crates/cashu-sdk/src/mint/mod.rs b/crates/cashu-sdk/src/mint/mod.rs index 2f3fa873..0d7e25d9 100644 --- a/crates/cashu-sdk/src/mint/mod.rs +++ b/crates/cashu-sdk/src/mint/mod.rs @@ -19,10 +19,9 @@ use tracing::{debug, info}; use crate::Mnemonic; mod localstore; -use localstore::LocalStore; -pub use localstore::MemoryLocalStore; #[cfg(all(not(target_arch = "wasm32"), feature = "redb"))] pub use localstore::RedbLocalStore; +pub use localstore::{LocalStore, MemoryLocalStore}; #[derive(Debug, Error)] pub enum Error { @@ -58,7 +57,7 @@ pub struct Mint { // pub pubkey: PublicKey mnemonic: Mnemonic, pub fee_reserve: FeeReserve, - localstore: L, + pub localstore: L, } impl Mint { @@ -142,6 +141,16 @@ impl Mint { })) } + /// Retrieve the public keys of the active keyset for distribution to + /// wallet clients + pub async fn pubkeys(&self) -> Result { + let keysets = self.localstore.get_keysets().await?; + + Ok(KeysResponse { + keysets: keysets.into_iter().map(|k| k.into()).collect(), + }) + } + /// Return a list of all supported keysets pub async fn keysets(&self) -> Result { let keysets = self.localstore.get_keysets().await?;