Cesar Rodas há 3 semanas atrás
pai
commit
a35ec4b09c
1 ficheiros alterados com 72 adições e 1 exclusões
  1. 72 1
      crates/cdk/src/wallet/auth/auth_wallet.rs

+ 72 - 1
crates/cdk/src/wallet/auth/auth_wallet.rs

@@ -192,6 +192,60 @@ impl AuthWallet {
         Ok(keys)
     }
 
+    /// Fetch keys for mint keyset with a transaction
+    pub(crate) async fn load_keyset_keys_with_key(
+        &self,
+        tx: &mut Tx<'_, '_>,
+        keyset_id: Id,
+    ) -> Result<Keys, Error> {
+        let keys = if let Some(keys) = tx.get_keys(&keyset_id).await? {
+            keys
+        } else {
+            let keys = self.client.get_mint_blind_auth_keyset(keyset_id).await?;
+
+            keys.verify_id()?;
+
+            tx.add_keys(keys.clone()).await?;
+
+            keys.keys
+        };
+
+        Ok(keys)
+    }
+
+    /// Get blind auth keysets from local database or go online if missing (with transaction)
+    ///
+    /// First checks the local database for cached blind auth keysets. If keysets are not found locally,
+    /// goes online to refresh keysets from the mint and updates the local database.
+    /// This is the main method for getting auth keysets in operations that can work offline
+    /// but will fall back to online if needed.
+    ///
+    /// This version requires a database transaction to be passed in.
+    #[instrument(skip(self, tx))]
+    pub(crate) async fn load_mint_keysets_with_tx(
+        &self,
+        tx: &mut Tx<'_, '_>,
+    ) -> Result<Vec<KeySetInfo>, Error> {
+        match tx.get_mint_keysets(self.mint_url.clone()).await? {
+            Some(keysets_info) => {
+                let auth_keysets: Vec<KeySetInfo> =
+                    keysets_info.unit(CurrencyUnit::Sat).cloned().collect();
+                if auth_keysets.is_empty() {
+                    // If we don't have any auth keysets, fetch them from the mint
+                    let keysets = self.refresh_keysets_with_tx(tx).await?;
+                    Ok(keysets)
+                } else {
+                    Ok(auth_keysets)
+                }
+            }
+            None => {
+                // If we don't have any keysets, fetch them from the mint
+                let keysets = self.refresh_keysets_with_tx(tx).await?;
+                Ok(keysets)
+            }
+        }
+    }
+
     /// Get blind auth keysets from local database or go online if missing
     ///
     /// First checks the local database for cached blind auth keysets. If keysets are not found locally,
@@ -255,7 +309,7 @@ impl AuthWallet {
         for keyset in &auth_keysets {
             if self.localstore.get_keys(&keyset.id).await?.is_none() {
                 tracing::debug!("Fetching missing keys for auth keyset {}", keyset.id);
-                self.load_keyset_keys(keyset.id).await?;
+                self.load_keyset_keys_with_key(tx, keyset.id).await?;
             }
         }
 
@@ -297,6 +351,23 @@ impl AuthWallet {
         Ok(auth_keysets)
     }
 
+    /// Get the first active blind auth keyset - always goes online (with transaction)
+    ///
+    /// This method always goes online to refresh keysets from the mint and then returns
+    /// the first active keyset found. Use this when you need the most up-to-date
+    /// keyset information for blind auth operations.
+    ///
+    /// This version requires a database transaction to be passed in.
+    #[instrument(skip(self, tx))]
+    pub(crate) async fn fetch_active_keyset_with_tx(
+        &self,
+        tx: &mut Tx<'_, '_>,
+    ) -> Result<KeySetInfo, Error> {
+        let auth_keysets = self.refresh_keysets_with_tx(tx).await?;
+        let keyset = auth_keysets.first().ok_or(Error::NoActiveKeyset)?;
+        Ok(keyset.clone())
+    }
+
     /// Get the first active blind auth keyset - always goes online
     ///
     /// This method always goes online to refresh keysets from the mint and then returns