From be2b51b25a508f1e85486b0d65dbd48186c0606b Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Mon, 29 Jan 2024 23:41:47 +0000 Subject: [PATCH] fix: check if onlt one unit is used in swap fix(mint/redb): id and unit from string TODO: default keyset should have a real derivation path --- crates/cashu-sdk/src/client/minreq_client.rs | 6 ++- crates/cashu-sdk/src/mint/localstore/mod.rs | 4 ++ .../src/mint/localstore/redb_store.rs | 4 +- crates/cashu-sdk/src/mint/mod.rs | 47 ++++++++++++------- 4 files changed, 42 insertions(+), 19 deletions(-) diff --git a/crates/cashu-sdk/src/client/minreq_client.rs b/crates/cashu-sdk/src/client/minreq_client.rs index d40d18a3..ceeac6de 100644 --- a/crates/cashu-sdk/src/client/minreq_client.rs +++ b/crates/cashu-sdk/src/client/minreq_client.rs @@ -13,7 +13,7 @@ use cashu::nuts::{CheckStateRequest, CheckStateResponse}; use cashu::secret::Secret; use cashu::{Amount, Bolt11Invoice}; use serde_json::Value; -use tracing::warn; +use tracing::{error, warn}; use url::Url; use super::join_url; @@ -180,6 +180,10 @@ impl Client for HttpClient { let response: Result = serde_json::from_value(res.json::()?.clone()); + if let Err(err) = &response { + error!("{}", err) + } + Ok(response?) } diff --git a/crates/cashu-sdk/src/mint/localstore/mod.rs b/crates/cashu-sdk/src/mint/localstore/mod.rs index 3cc53414..2c406532 100644 --- a/crates/cashu-sdk/src/mint/localstore/mod.rs +++ b/crates/cashu-sdk/src/mint/localstore/mod.rs @@ -39,6 +39,10 @@ pub enum Error { Serde(#[from] serde_json::Error), #[error("Unknown Mint Info")] UnknownMintInfo, + #[error("`{0}`")] + Cashu(#[from] cashu::error::Error), + #[error("`{0}`")] + CashuNut02(#[from] cashu::nuts::nut02::Error), } #[async_trait] diff --git a/crates/cashu-sdk/src/mint/localstore/redb_store.rs b/crates/cashu-sdk/src/mint/localstore/redb_store.rs index 2d295d0a..f72fdd8a 100644 --- a/crates/cashu-sdk/src/mint/localstore/redb_store.rs +++ b/crates/cashu-sdk/src/mint/localstore/redb_store.rs @@ -106,8 +106,8 @@ impl LocalStore for RedbLocalStore { let mut active_keysets = HashMap::new(); for (unit, id) in (table.iter()?).flatten() { - let unit = serde_json::from_str(unit.value())?; - let id = serde_json::from_str(id.value())?; + let unit = CurrencyUnit::from_str(unit.value())?; + let id = Id::from_str(id.value())?; active_keysets.insert(unit, id); } diff --git a/crates/cashu-sdk/src/mint/mod.rs b/crates/cashu-sdk/src/mint/mod.rs index 6b006d49..a9ef6a95 100644 --- a/crates/cashu-sdk/src/mint/mod.rs +++ b/crates/cashu-sdk/src/mint/mod.rs @@ -74,21 +74,35 @@ impl Mint { ) -> Result { let mut active_units: HashSet = HashSet::default(); - // Check that there is only one active keyset per unit - for keyset_info in keysets_info { - if keyset_info.active && !active_units.insert(keyset_info.unit.clone()) { - // TODO: Handle Error - todo!() - } - + if keysets_info.is_empty() { let keyset = nut02::mint::KeySet::generate( &mnemonic.to_seed_normalized(""), - keyset_info.unit.clone(), - &keyset_info.derivation_path.clone(), - keyset_info.max_order, + CurrencyUnit::Sat, + "", + 64, ); + localstore + .add_active_keyset(CurrencyUnit::Sat, keyset.id.clone()) + .await?; localstore.add_keyset(keyset).await?; + } else { + // Check that there is only one active keyset per unit + for keyset_info in keysets_info { + if keyset_info.active && !active_units.insert(keyset_info.unit.clone()) { + // TODO: Handle Error + todo!() + } + + let keyset = nut02::mint::KeySet::generate( + &mnemonic.to_seed_normalized(""), + keyset_info.unit.clone(), + &keyset_info.derivation_path.clone(), + keyset_info.max_order, + ); + + localstore.add_keyset(keyset).await?; + } } Ok(Self { @@ -339,7 +353,7 @@ impl Mint { let input_keyset_ids: HashSet = swap_request.inputs.iter().map(|p| p.keyset_id).collect(); - let mut keyset_units = Vec::with_capacity(input_keyset_ids.capacity()); + let mut keyset_units = HashSet::with_capacity(input_keyset_ids.capacity()); for id in input_keyset_ids { let keyset = self @@ -347,7 +361,7 @@ impl Mint { .get_keyset(&id) .await? .ok_or(Error::UnknownKeySet)?; - keyset_units.push(keyset.unit); + keyset_units.insert(keyset.unit); } let output_keyset_ids: HashSet = @@ -360,12 +374,13 @@ impl Mint { .await? .ok_or(Error::UnknownKeySet)?; - keyset_units.push(keyset.unit); + keyset_units.insert(keyset.unit); } - // Check that all input and output proofs are the same unit - let seen_units: HashSet = HashSet::new(); - if keyset_units.iter().any(|unit| !seen_units.contains(unit)) && seen_units.len() != 1 { + // Check that all proofs are the same unit + // in the future it maybe possible to support multiple units but unsupported for + // now + if keyset_units.len().gt(&1) { return Err(Error::MultipleUnits); }