From 5f6a207cec1076740091cec9b59b13e185327874 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Fri, 24 May 2024 00:26:13 +0100 Subject: [PATCH] fix: set send proofs to reserved instead of removing --- crates/cdk-redb/src/wallet.rs | 35 ++++++++++++++++++----------------- crates/cdk/src/mint.rs | 2 +- crates/cdk/src/nuts/nut04.rs | 4 ++-- crates/cdk/src/wallet.rs | 23 ++++++++++++----------- 4 files changed, 33 insertions(+), 31 deletions(-) diff --git a/crates/cdk-redb/src/wallet.rs b/crates/cdk-redb/src/wallet.rs index dfd9c745..13af6249 100644 --- a/crates/cdk-redb/src/wallet.rs +++ b/crates/cdk-redb/src/wallet.rs @@ -466,32 +466,33 @@ impl WalletDatabase for RedbWalletDatabase { let table = read_txn.open_table(PROOFS_TABLE).map_err(Error::from)?; let y_slice = y.to_bytes(); - let proof = table.get(y_slice.as_slice()).map_err(Error::from)?; + let proof = table + .get(y_slice.as_slice()) + .map_err(Error::from)? + .ok_or(Error::UnknownY)?; let write_txn = db.begin_write().map_err(Error::from)?; - if let Some(proof) = proof { - let mut proof_info = - serde_json::from_str::(proof.value()).map_err(Error::from)?; + let mut proof_info = + serde_json::from_str::(proof.value()).map_err(Error::from)?; - proof_info.state = state; + proof_info.state = state; - { - let mut table = write_txn.open_table(PROOFS_TABLE).map_err(Error::from)?; - table - .insert( - y_slice.as_slice(), - serde_json::to_string(&proof_info) - .map_err(Error::from)? - .as_str(), - ) - .map_err(Error::from)?; - } + { + let mut table = write_txn.open_table(PROOFS_TABLE).map_err(Error::from)?; + table + .insert( + y_slice.as_slice(), + serde_json::to_string(&proof_info) + .map_err(Error::from)? + .as_str(), + ) + .map_err(Error::from)?; } write_txn.commit().map_err(Error::from)?; - Err(Error::UnknownY.into()) + Ok(()) } #[instrument(skip(self))] diff --git a/crates/cdk/src/mint.rs b/crates/cdk/src/mint.rs index 36891bd3..945d29fd 100644 --- a/crates/cdk/src/mint.rs +++ b/crates/cdk/src/mint.rs @@ -160,7 +160,7 @@ impl Mint { quote: quote.id, request: quote.request, paid: quote.paid, - expiry: quote.expiry, + expiry: Some(quote.expiry), }) } diff --git a/crates/cdk/src/nuts/nut04.rs b/crates/cdk/src/nuts/nut04.rs index 0f61de33..959111ff 100644 --- a/crates/cdk/src/nuts/nut04.rs +++ b/crates/cdk/src/nuts/nut04.rs @@ -27,7 +27,7 @@ pub struct MintQuoteBolt11Response { /// Whether the the request haas be paid pub paid: bool, /// Unix timestamp until the quote is valid - pub expiry: u64, + pub expiry: Option, } impl From for MintQuoteBolt11Response { @@ -36,7 +36,7 @@ impl From for MintQuoteBolt11Response { quote: mint_quote.id, request: mint_quote.request, paid: mint_quote.paid, - expiry: mint_quote.expiry, + expiry: Some(mint_quote.expiry), } } } diff --git a/crates/cdk/src/wallet.rs b/crates/cdk/src/wallet.rs index 0d8edf62..d6219f2f 100644 --- a/crates/cdk/src/wallet.rs +++ b/crates/cdk/src/wallet.rs @@ -194,7 +194,7 @@ impl Wallet { for (mint, _) in mints { if let Some(proofs) = self .localstore - .get_proofs(Some(mint.clone()), None, None, None) + .get_proofs(Some(mint.clone()), None, Some(vec![State::Unspent]), None) .await? { let mut balances = HashMap::new(); @@ -440,7 +440,7 @@ impl Wallet { unit: unit.clone(), request: quote_res.request, paid: quote_res.paid, - expiry: quote_res.expiry, + expiry: quote_res.expiry.unwrap_or(0), }; self.localstore.add_mint_quote(quote.clone()).await?; @@ -693,6 +693,7 @@ impl Wallet { let active_keyset_id = self.active_mint_keyset(mint_url, unit).await?; + // FIXME: Should not increment keyset counter for condition proofs self.localstore .increment_keyset_counter(&active_keyset_id, post_swap_proofs.len() as u32) .await?; @@ -752,8 +753,6 @@ impl Wallet { } } - self.localstore.remove_proofs(&input_proofs).await?; - for proof in input_proofs { self.localstore .set_proof_state(proof.y()?, State::Reserved) @@ -849,7 +848,7 @@ impl Wallet { } }; - // Combine the BlindedMessages totoalling the desired amount with change + // Combine the BlindedMessages totaling the desired amount with change desired_messages.combine(change_messages); // Sort the premint secrets to avoid finger printing desired_messages.sort_secrets(); @@ -926,13 +925,15 @@ impl Wallet { } }; + let send_proofs = send_proofs.ok_or(Error::InsufficientFunds)?; + for proof in send_proofs.iter() { + self.localstore + .set_proof_state(proof.y()?, State::Reserved) + .await?; + } + Ok(self - .proof_to_token( - mint_url.clone(), - send_proofs.ok_or(Error::InsufficientFunds)?, - memo, - Some(unit.clone()), - )? + .proof_to_token(mint_url.clone(), send_proofs, memo, Some(unit.clone()))? .to_string()) }