Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor receive_proofs now that multi-mint tokens are not supported #379

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 14 additions & 24 deletions crates/cdk/src/wallet/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::nuts::{Conditions, Token};
use crate::{
amount::SplitTarget,
dhke::construct_proofs,
mint_url::MintUrl,
nuts::{Proofs, PublicKey, SecretKey, SigFlag, State},
types::ProofInfo,
util::hex,
Expand All @@ -26,7 +25,6 @@ impl Wallet {
p2pk_signing_keys: &[SecretKey],
preimages: &[String],
) -> Result<Amount, Error> {
let mut received_proofs: HashMap<MintUrl, Proofs> = HashMap::new();
let mint_url = &self.mint_url;
// Add mint if it does not exist in the store
if self
Expand Down Expand Up @@ -139,37 +137,29 @@ impl Wallet {
.await?;

// Proof to keep
let p = construct_proofs(
let recv_proofs = construct_proofs(
swap_response.signatures,
pre_swap.pre_mint_secrets.rs(),
pre_swap.pre_mint_secrets.secrets(),
&keys,
)?;
let mint_proofs = received_proofs.entry(mint_url.clone()).or_default();

self.localstore
.increment_keyset_counter(&active_keyset_id, p.len() as u32)
.increment_keyset_counter(&active_keyset_id, recv_proofs.len() as u32)
.await?;

mint_proofs.extend(p);

let mut total_amount = Amount::ZERO;
for (mint, recv_proofs) in received_proofs {
total_amount += Amount::try_sum(recv_proofs.iter().map(|p| p.amount))?;
let recv_proof_infos = recv_proofs
.into_iter()
.map(|proof| ProofInfo::new(proof, mint.clone(), State::Unspent, self.unit))
.collect::<Result<Vec<ProofInfo>, _>>()?;
self.localstore
.update_proofs(
recv_proof_infos,
proofs_info
.iter()
.filter_map(|p| if p.mint_url == mint { Some(p.y) } else { None })
.collect(),
)
.await?;
}
let total_amount = Amount::try_sum(recv_proofs.iter().map(|p| p.amount))?;

let recv_proof_infos = recv_proofs
.into_iter()
.map(|proof| ProofInfo::new(proof, mint_url.clone(), State::Unspent, self.unit))
.collect::<Result<Vec<ProofInfo>, _>>()?;
self.localstore
.update_proofs(
recv_proof_infos,
proofs_info.into_iter().map(|p| p.y).collect(),
)
.await?;

Ok(total_amount)
}
Expand Down
Loading