Преглед изворни кода

fix: set send proofs to reserved instead of removing

thesimplekid пре 9 месеци
родитељ
комит
5f6a207cec
4 измењених фајлова са 33 додато и 31 уклоњено
  1. 18 17
      crates/cdk-redb/src/wallet.rs
  2. 1 1
      crates/cdk/src/mint.rs
  3. 2 2
      crates/cdk/src/nuts/nut04.rs
  4. 12 11
      crates/cdk/src/wallet.rs

+ 18 - 17
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 table = read_txn.open_table(PROOFS_TABLE).map_err(Error::from)?;
 
 
         let y_slice = y.to_bytes();
         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)?;
         let write_txn = db.begin_write().map_err(Error::from)?;
 
 
-        if let Some(proof) = proof {
-            let mut proof_info =
-                serde_json::from_str::<ProofInfo>(proof.value()).map_err(Error::from)?;
+        let mut proof_info =
+            serde_json::from_str::<ProofInfo>(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)?;
         write_txn.commit().map_err(Error::from)?;
 
 
-        Err(Error::UnknownY.into())
+        Ok(())
     }
     }
 
 
     #[instrument(skip(self))]
     #[instrument(skip(self))]

+ 1 - 1
crates/cdk/src/mint.rs

@@ -160,7 +160,7 @@ impl Mint {
             quote: quote.id,
             quote: quote.id,
             request: quote.request,
             request: quote.request,
             paid: quote.paid,
             paid: quote.paid,
-            expiry: quote.expiry,
+            expiry: Some(quote.expiry),
         })
         })
     }
     }
 
 

+ 2 - 2
crates/cdk/src/nuts/nut04.rs

@@ -27,7 +27,7 @@ pub struct MintQuoteBolt11Response {
     /// Whether the the request haas be paid
     /// Whether the the request haas be paid
     pub paid: bool,
     pub paid: bool,
     /// Unix timestamp until the quote is valid
     /// Unix timestamp until the quote is valid
-    pub expiry: u64,
+    pub expiry: Option<u64>,
 }
 }
 
 
 impl From<MintQuote> for MintQuoteBolt11Response {
 impl From<MintQuote> for MintQuoteBolt11Response {
@@ -36,7 +36,7 @@ impl From<MintQuote> for MintQuoteBolt11Response {
             quote: mint_quote.id,
             quote: mint_quote.id,
             request: mint_quote.request,
             request: mint_quote.request,
             paid: mint_quote.paid,
             paid: mint_quote.paid,
-            expiry: mint_quote.expiry,
+            expiry: Some(mint_quote.expiry),
         }
         }
     }
     }
 }
 }

+ 12 - 11
crates/cdk/src/wallet.rs

@@ -194,7 +194,7 @@ impl Wallet {
         for (mint, _) in mints {
         for (mint, _) in mints {
             if let Some(proofs) = self
             if let Some(proofs) = self
                 .localstore
                 .localstore
-                .get_proofs(Some(mint.clone()), None, None, None)
+                .get_proofs(Some(mint.clone()), None, Some(vec![State::Unspent]), None)
                 .await?
                 .await?
             {
             {
                 let mut balances = HashMap::new();
                 let mut balances = HashMap::new();
@@ -440,7 +440,7 @@ impl Wallet {
             unit: unit.clone(),
             unit: unit.clone(),
             request: quote_res.request,
             request: quote_res.request,
             paid: quote_res.paid,
             paid: quote_res.paid,
-            expiry: quote_res.expiry,
+            expiry: quote_res.expiry.unwrap_or(0),
         };
         };
 
 
         self.localstore.add_mint_quote(quote.clone()).await?;
         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?;
         let active_keyset_id = self.active_mint_keyset(mint_url, unit).await?;
 
 
+        // FIXME: Should not increment keyset counter for condition proofs
         self.localstore
         self.localstore
             .increment_keyset_counter(&active_keyset_id, post_swap_proofs.len() as u32)
             .increment_keyset_counter(&active_keyset_id, post_swap_proofs.len() as u32)
             .await?;
             .await?;
@@ -752,8 +753,6 @@ impl Wallet {
             }
             }
         }
         }
 
 
-        self.localstore.remove_proofs(&input_proofs).await?;
-
         for proof in input_proofs {
         for proof in input_proofs {
             self.localstore
             self.localstore
                 .set_proof_state(proof.y()?, State::Reserved)
                 .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);
         desired_messages.combine(change_messages);
         // Sort the premint secrets to avoid finger printing
         // Sort the premint secrets to avoid finger printing
         desired_messages.sort_secrets();
         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
         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())
             .to_string())
     }
     }