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::{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?)
}

View File

@@ -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]

View File

@@ -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);
}

View File

@@ -74,6 +74,19 @@ impl Mint {
) -> Result<Self, Error> {
let mut active_units: HashSet<CurrencyUnit> = HashSet::default();
if keysets_info.is_empty() {
let keyset = nut02::mint::KeySet::generate(
&mnemonic.to_seed_normalized(""),
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()) {
@@ -90,6 +103,7 @@ impl Mint {
localstore.add_keyset(keyset).await?;
}
}
Ok(Self {
localstore,
@@ -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);
}