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
This commit is contained in:
thesimplekid
2024-01-29 23:41:47 +00:00
parent 30c552e164
commit be2b51b25a
4 changed files with 42 additions and 19 deletions

View File

@@ -13,7 +13,7 @@ use cashu::nuts::{CheckStateRequest, CheckStateResponse};
use cashu::secret::Secret; use cashu::secret::Secret;
use cashu::{Amount, Bolt11Invoice}; use cashu::{Amount, Bolt11Invoice};
use serde_json::Value; use serde_json::Value;
use tracing::warn; use tracing::{error, warn};
use url::Url; use url::Url;
use super::join_url; use super::join_url;
@@ -180,6 +180,10 @@ impl Client for HttpClient {
let response: Result<SwapResponse, serde_json::Error> = let response: Result<SwapResponse, serde_json::Error> =
serde_json::from_value(res.json::<Value>()?.clone()); serde_json::from_value(res.json::<Value>()?.clone());
if let Err(err) = &response {
error!("{}", err)
}
Ok(response?) Ok(response?)
} }

View File

@@ -39,6 +39,10 @@ pub enum Error {
Serde(#[from] serde_json::Error), Serde(#[from] serde_json::Error),
#[error("Unknown Mint Info")] #[error("Unknown Mint Info")]
UnknownMintInfo, UnknownMintInfo,
#[error("`{0}`")]
Cashu(#[from] cashu::error::Error),
#[error("`{0}`")]
CashuNut02(#[from] cashu::nuts::nut02::Error),
} }
#[async_trait] #[async_trait]

View File

@@ -106,8 +106,8 @@ impl LocalStore for RedbLocalStore {
let mut active_keysets = HashMap::new(); let mut active_keysets = HashMap::new();
for (unit, id) in (table.iter()?).flatten() { for (unit, id) in (table.iter()?).flatten() {
let unit = serde_json::from_str(unit.value())?; let unit = CurrencyUnit::from_str(unit.value())?;
let id = serde_json::from_str(id.value())?; let id = Id::from_str(id.value())?;
active_keysets.insert(unit, id); active_keysets.insert(unit, id);
} }

View File

@@ -74,21 +74,35 @@ impl Mint {
) -> Result<Self, Error> { ) -> Result<Self, Error> {
let mut active_units: HashSet<CurrencyUnit> = HashSet::default(); let mut active_units: HashSet<CurrencyUnit> = HashSet::default();
// Check that there is only one active keyset per unit if keysets_info.is_empty() {
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( let keyset = nut02::mint::KeySet::generate(
&mnemonic.to_seed_normalized(""), &mnemonic.to_seed_normalized(""),
keyset_info.unit.clone(), CurrencyUnit::Sat,
&keyset_info.derivation_path.clone(), "",
keyset_info.max_order, 64,
); );
localstore
.add_active_keyset(CurrencyUnit::Sat, keyset.id.clone())
.await?;
localstore.add_keyset(keyset).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 { Ok(Self {
@@ -339,7 +353,7 @@ impl Mint {
let input_keyset_ids: HashSet<Id> = let input_keyset_ids: HashSet<Id> =
swap_request.inputs.iter().map(|p| p.keyset_id).collect(); 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 { for id in input_keyset_ids {
let keyset = self let keyset = self
@@ -347,7 +361,7 @@ impl Mint {
.get_keyset(&id) .get_keyset(&id)
.await? .await?
.ok_or(Error::UnknownKeySet)?; .ok_or(Error::UnknownKeySet)?;
keyset_units.push(keyset.unit); keyset_units.insert(keyset.unit);
} }
let output_keyset_ids: HashSet<Id> = let output_keyset_ids: HashSet<Id> =
@@ -360,12 +374,13 @@ impl Mint {
.await? .await?
.ok_or(Error::UnknownKeySet)?; .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 // Check that all proofs are the same unit
let seen_units: HashSet<CurrencyUnit> = HashSet::new(); // in the future it maybe possible to support multiple units but unsupported for
if keyset_units.iter().any(|unit| !seen_units.contains(unit)) && seen_units.len() != 1 { // now
if keyset_units.len().gt(&1) {
return Err(Error::MultipleUnits); return Err(Error::MultipleUnits);
} }