Skip to content

Commit

Permalink
refactor: ensure swap inputs and outputs are same unit
Browse files Browse the repository at this point in the history
ensure swap outputs are from active keyset
  • Loading branch information
thesimplekid committed Jan 3, 2024
1 parent 8753e71 commit 44918fc
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions crates/cashu-sdk/src/mint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,53 @@ impl<L: LocalStore> Mint<L> {
self.verify_proof(proof).await?
}

for (secret, proof) in secrets.iter().zip(swap_request.inputs) {
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());

for id in input_keyset_ids {
let keyset = self
.localstore
.get_keyset(&id)
.await?
.ok_or(Error::UnknownKeySet)?;
keyset_units.push(keyset.unit);
}

let output_keyset_ids: HashSet<Id> =
swap_request.outputs.iter().map(|p| p.keyset_id).collect();

for id in &output_keyset_ids {
let keyset = self
.localstore
.get_keyset(&id)
.await?
.ok_or(Error::UnknownKeySet)?;

// Get the active keyset for the unit
let active_keyset_id = self
.localstore
.get_active_keyset_id(&keyset.unit)
.await?
.ok_or(Error::InactiveKeyset)?;

// Check output is for current active keyset
if id.ne(&active_keyset_id) {
return Err(Error::InactiveKeyset);
}
keyset_units.push(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 {
return Err(Error::MultipleUnits);
}

for proof in swap_request.inputs {
self.localstore
.add_spent_proof(secret.clone(), proof)
.add_spent_proof(proof.secret.clone(), proof)
.await
.unwrap();
}
Expand Down

0 comments on commit 44918fc

Please sign in to comment.