Forráskód Böngészése

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

thesimplekid 1 éve
szülő
commit
be2b51b25a

+ 5 - 1
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<SwapResponse, serde_json::Error> =
             serde_json::from_value(res.json::<Value>()?.clone());
 
+        if let Err(err) = &response {
+            error!("{}", err)
+        }
+
         Ok(response?)
     }
 

+ 4 - 0
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]

+ 2 - 2
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);
         }

+ 31 - 16
crates/cashu-sdk/src/mint/mod.rs

@@ -74,21 +74,35 @@ impl Mint {
     ) -> Result<Self, Error> {
         let mut active_units: HashSet<CurrencyUnit> = 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<Id> =
             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<Id> =
@@ -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<CurrencyUnit> = 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);
         }