Переглянути джерело

fix(wallet): proof slection with conditions

thesimplekid 9 місяців тому
батько
коміт
36c93b9211
2 змінених файлів з 23 додано та 34 видалено
  1. 8 26
      crates/cdk-rexie/src/wallet.rs
  2. 15 8
      crates/cdk/src/wallet.rs

+ 8 - 26
crates/cdk-rexie/src/wallet.rs

@@ -64,15 +64,9 @@ impl RexieWalletDatabase {
             // Set the version of the database to 1.0
             .version(DATABASE_VERSION)
             // Add an object store named `employees`
+            .add_object_store(ObjectStore::new(PROOFS).add_index(Index::new("y", "y").unique(true)))
             .add_object_store(
-                ObjectStore::new(PROOFS)
-                    // Add an index named `email` with the key path `email` with unique enabled
-                    .add_index(Index::new("y", "y").unique(true)),
-            )
-            .add_object_store(
-                ObjectStore::new(MINTS)
-                    // Add an index named `email` with the key path `email` with unique enabled
-                    .add_index(Index::new("mint_url", "mint_url").unique(true)),
+                ObjectStore::new(MINTS).add_index(Index::new("mint_url", "mint_url").unique(true)),
             )
             .add_object_store(
                 ObjectStore::new(MINT_KEYSETS)
@@ -82,22 +76,10 @@ impl RexieWalletDatabase {
                 ObjectStore::new(MINT_KEYS)
                     .add_index(Index::new("keyset_id", "keyset_id").unique(true)),
             )
-            .add_object_store(
-                ObjectStore::new(MINT_QUOTES)
-                    .add_index(Index::new("keyset_id", "keyset_id").unique(true)),
-            )
-            .add_object_store(
-                ObjectStore::new(MELT_QUOTES)
-                    .add_index(Index::new("keyset_id", "keyset_id").unique(true)),
-            )
-            .add_object_store(
-                ObjectStore::new(CONFIG)
-                    .add_index(Index::new("keyset_id", "keyset_id").unique(true)),
-            )
-            .add_object_store(
-                ObjectStore::new(KEYSET_COUNTER)
-                    .add_index(Index::new("keyset_id", "keyset_id").unique(true)),
-            )
+            .add_object_store(ObjectStore::new(MINT_QUOTES))
+            .add_object_store(ObjectStore::new(MELT_QUOTES))
+            .add_object_store(ObjectStore::new(CONFIG))
+            .add_object_store(ObjectStore::new(KEYSET_COUNTER))
             // Build the database
             .build()
             .await
@@ -245,7 +227,7 @@ impl WalletDatabase for RexieWalletDatabase {
         let quote = serde_wasm_bindgen::to_value(&quote).map_err(Error::from)?;
 
         quotes_store
-            .add(&quote, Some(&quote_id))
+            .put(&quote, Some(&quote_id))
             .await
             .map_err(Error::from)?;
 
@@ -323,7 +305,7 @@ impl WalletDatabase for RexieWalletDatabase {
         let quote = serde_wasm_bindgen::to_value(&quote).map_err(Error::from)?;
 
         quotes_store
-            .add(&quote, Some(&quote_id))
+            .put(&quote, Some(&quote_id))
             .await
             .map_err(Error::from)?;
 

+ 15 - 8
crates/cdk/src/wallet.rs

@@ -908,15 +908,21 @@ impl Wallet {
         conditions: Option<SpendingConditions>,
     ) -> Result<String, Error> {
         let (condition_input_proofs, input_proofs) = self
-            .select_proofs(mint_url.clone(), unit.clone(), amount, conditions.clone())
+            .select_proofs(
+                mint_url.clone(),
+                unit.clone(),
+                amount,
+                conditions.clone().map(|p| vec![p]),
+            )
             .await?;
 
         let send_proofs = match conditions {
             Some(_) => {
-                let needed_amount = condition_input_proofs
-                    .iter()
-                    .map(|p| p.amount)
-                    .sum::<Amount>();
+                let needed_amount = amount
+                    - condition_input_proofs
+                        .iter()
+                        .map(|p| p.amount)
+                        .sum::<Amount>();
 
                 let top_up_proofs = self
                     .swap(
@@ -1038,7 +1044,7 @@ impl Wallet {
         mint_url: UncheckedUrl,
         unit: CurrencyUnit,
         amount: Amount,
-        conditions: Option<SpendingConditions>,
+        conditions: Option<Vec<SpendingConditions>>,
     ) -> Result<(Proofs, Proofs), Error> {
         let mut condition_mint_proofs = Vec::new();
 
@@ -1049,7 +1055,7 @@ impl Wallet {
                     Some(mint_url.clone()),
                     Some(unit.clone()),
                     Some(vec![State::Unspent]),
-                    None,
+                    conditions,
                 )
                 .await?
                 .unwrap_or_default()
@@ -1139,7 +1145,8 @@ impl Wallet {
             }
         }
 
-        if selected_proofs.iter().map(|p| p.amount).sum::<Amount>() < amount {
+        if selected_proofs.iter().map(|p| p.amount).sum::<Amount>() + condition_proof_total < amount
+        {
             return Err(Error::InsufficientFunds);
         }