Ver Fonte

feat: refresh keysets (#974)

* feat: refresh keysets

* feat: rename get_mint_info to fetch_mint_info
thesimplekid há 1 mês atrás
pai
commit
5c5075af71

+ 1 - 1
crates/cdk-cli/src/sub_commands/cat_device_login.rs

@@ -49,7 +49,7 @@ pub async fn cat_device_login(
     };
 
     let mint_info = wallet
-        .get_mint_info()
+        .fetch_mint_info()
         .await?
         .ok_or(anyhow!("Mint info not found"))?;
 

+ 1 - 1
crates/cdk-cli/src/sub_commands/cat_login.rs

@@ -51,7 +51,7 @@ pub async fn cat_login(
     };
 
     let mint_info = wallet
-        .get_mint_info()
+        .fetch_mint_info()
         .await?
         .ok_or(anyhow!("Mint info not found"))?;
 

+ 3 - 3
crates/cdk-cli/src/sub_commands/mint_blind_auth.rs

@@ -47,7 +47,7 @@ pub async fn mint_blind_auth(
         }
     };
 
-    wallet.get_mint_info().await?;
+    wallet.fetch_mint_info().await?;
 
     // Try to get the token from the provided argument or from the stored file
     let cat = match &sub_command_args.cat {
@@ -83,7 +83,7 @@ pub async fn mint_blind_auth(
             println!("Attempting to refresh the access token...");
 
             // Get the mint info to access OIDC configuration
-            if let Some(mint_info) = wallet.get_mint_info().await? {
+            if let Some(mint_info) = wallet.fetch_mint_info().await? {
                 match refresh_access_token(&mint_info, &token_data.refresh_token).await {
                     Ok((new_access_token, new_refresh_token)) => {
                         println!("Successfully refreshed access token");
@@ -137,7 +137,7 @@ pub async fn mint_blind_auth(
         Some(amount) => amount,
         None => {
             let mint_info = wallet
-                .get_mint_info()
+                .fetch_mint_info()
                 .await?
                 .ok_or(anyhow!("Unknown mint info"))?;
             mint_info

+ 0 - 4
crates/cdk-integration-tests/tests/bolt12.rs

@@ -266,8 +266,6 @@ async fn test_regtest_bolt12_melt() -> Result<()> {
         None,
     )?;
 
-    wallet.get_mint_info().await?;
-
     let mint_amount = Amount::from(20_000);
 
     // Create a single-use BOLT12 quote
@@ -323,8 +321,6 @@ async fn test_regtest_bolt12_mint_extra() -> Result<()> {
         None,
     )?;
 
-    wallet.get_mint_info().await?;
-
     // Create a single-use BOLT12 quote
     let mint_quote = wallet.mint_bolt12_quote(None, None).await?;
 

+ 11 - 11
crates/cdk-integration-tests/tests/fake_auth.rs

@@ -42,7 +42,7 @@ async fn test_invalid_credentials() {
         .expect("Wallet");
 
     let mint_info = wallet
-        .get_mint_info()
+        .fetch_mint_info()
         .await
         .expect("mint info")
         .expect("could not get mint info");
@@ -278,7 +278,7 @@ async fn test_mint_blind_auth() {
         .seed(Mnemonic::generate(12).unwrap().to_seed_normalized(""))
         .build()
         .expect("Wallet");
-    let mint_info = wallet.get_mint_info().await.unwrap().unwrap();
+    let mint_info = wallet.fetch_mint_info().await.unwrap().unwrap();
 
     let (access_token, _) = get_access_token(&mint_info).await;
 
@@ -310,7 +310,7 @@ async fn test_mint_with_auth() {
         .expect("Wallet");
 
     let mint_info = wallet
-        .get_mint_info()
+        .fetch_mint_info()
         .await
         .expect("mint info")
         .expect("could not get mint info");
@@ -355,7 +355,7 @@ async fn test_swap_with_auth() {
         .seed(Mnemonic::generate(12).unwrap().to_seed_normalized(""))
         .build()
         .expect("Wallet");
-    let mint_info = wallet.get_mint_info().await.unwrap().unwrap();
+    let mint_info = wallet.fetch_mint_info().await.unwrap().unwrap();
     let (access_token, _) = get_access_token(&mint_info).await;
 
     wallet.set_cat(access_token).await.unwrap();
@@ -410,7 +410,7 @@ async fn test_melt_with_auth() {
         .expect("Wallet");
 
     let mint_info = wallet
-        .get_mint_info()
+        .fetch_mint_info()
         .await
         .expect("Mint info not found")
         .expect("Mint info not found");
@@ -452,7 +452,7 @@ async fn test_mint_auth_over_max() {
     let wallet = Arc::new(wallet);
 
     let mint_info = wallet
-        .get_mint_info()
+        .fetch_mint_info()
         .await
         .expect("Mint info not found")
         .expect("Mint info not found");
@@ -490,7 +490,7 @@ async fn test_reuse_auth_proof() {
         .seed(Mnemonic::generate(12).unwrap().to_seed_normalized(""))
         .build()
         .expect("Wallet");
-    let mint_info = wallet.get_mint_info().await.unwrap().unwrap();
+    let mint_info = wallet.fetch_mint_info().await.unwrap().unwrap();
 
     let (access_token, _) = get_access_token(&mint_info).await;
 
@@ -542,7 +542,7 @@ async fn test_melt_with_invalid_auth() {
         .seed(Mnemonic::generate(12).unwrap().to_seed_normalized(""))
         .build()
         .expect("Wallet");
-    let mint_info = wallet.get_mint_info().await.unwrap().unwrap();
+    let mint_info = wallet.fetch_mint_info().await.unwrap().unwrap();
 
     let (access_token, _) = get_access_token(&mint_info).await;
 
@@ -607,7 +607,7 @@ async fn test_refresh_access_token() {
         .expect("Wallet");
 
     let mint_info = wallet
-        .get_mint_info()
+        .fetch_mint_info()
         .await
         .expect("mint info")
         .expect("could not get mint info");
@@ -663,7 +663,7 @@ async fn test_invalid_refresh_token() {
         .expect("Wallet");
 
     let mint_info = wallet
-        .get_mint_info()
+        .fetch_mint_info()
         .await
         .expect("mint info")
         .expect("could not get mint info");
@@ -699,7 +699,7 @@ async fn test_auth_token_spending_order() {
         .expect("Wallet");
 
     let mint_info = wallet
-        .get_mint_info()
+        .fetch_mint_info()
         .await
         .expect("mint info")
         .expect("could not get mint info");

+ 1 - 1
crates/cdk-integration-tests/tests/integration_tests_pure.rs

@@ -196,7 +196,7 @@ async fn test_mint_nut06() {
 
     let initial_mint_url = wallet_alice.mint_url.clone();
     let mint_info_before = wallet_alice
-        .get_mint_info()
+        .fetch_mint_info()
         .await
         .expect("Failed to get mint info")
         .unwrap();

+ 1 - 1
crates/cdk/examples/auth_wallet.rs

@@ -37,7 +37,7 @@ async fn main() -> Result<(), Error> {
     let wallet = Wallet::new(mint_url, unit, Arc::new(localstore), seed, None)?;
 
     let mint_info = wallet
-        .get_mint_info()
+        .fetch_mint_info()
         .await
         .expect("mint info")
         .expect("could not get mint info");

+ 3 - 9
crates/cdk/src/wallet/issue/issue_bolt11.rs

@@ -52,6 +52,8 @@ impl Wallet {
         let mint_url = self.mint_url.clone();
         let unit = self.unit.clone();
 
+        self.refresh_keysets().await?;
+
         // If we have a description, we check that the mint supports it.
         if description.is_some() {
             let settings = self
@@ -194,15 +196,7 @@ impl Wallet {
         amount_split_target: SplitTarget,
         spending_conditions: Option<SpendingConditions>,
     ) -> Result<Proofs, Error> {
-        // Check that mint is in store of mints
-        if self
-            .localstore
-            .get_mint(self.mint_url.clone())
-            .await?
-            .is_none()
-        {
-            self.get_mint_info().await?;
-        }
+        self.refresh_keysets().await?;
 
         let quote_info = self
             .localstore

+ 3 - 9
crates/cdk/src/wallet/issue/issue_bolt12.rs

@@ -29,6 +29,8 @@ impl Wallet {
         let mint_url = self.mint_url.clone();
         let unit = &self.unit;
 
+        self.refresh_keysets().await?;
+
         // If we have a description, we check that the mint supports it.
         if description.is_some() {
             let mint_method_settings = self
@@ -83,15 +85,7 @@ impl Wallet {
         amount_split_target: SplitTarget,
         spending_conditions: Option<SpendingConditions>,
     ) -> Result<Proofs, Error> {
-        // Check that mint is in store of mints
-        if self
-            .localstore
-            .get_mint(self.mint_url.clone())
-            .await?
-            .is_none()
-        {
-            self.get_mint_info().await?;
-        }
+        self.refresh_keysets().await?;
 
         let quote_info = self.localstore.get_mint_quote(quote_id).await?;
 

+ 1 - 1
crates/cdk/src/wallet/keysets.rs

@@ -82,7 +82,7 @@ impl Wallet {
     #[instrument(skip(self))]
     pub async fn refresh_keysets(&self) -> Result<KeySetInfos, Error> {
         tracing::debug!("Refreshing keysets and ensuring we have keys");
-        let _ = self.get_mint_info().await?;
+        let _ = self.fetch_mint_info().await?;
 
         // Fetch all current keysets from mint
         let keysets_response = self.client.get_mint_keysets().await?;

+ 2 - 0
crates/cdk/src/wallet/melt/melt_bolt11.rs

@@ -48,6 +48,8 @@ impl Wallet {
         request: String,
         options: Option<MeltOptions>,
     ) -> Result<MeltQuote, Error> {
+        self.refresh_keysets().await?;
+
         let invoice = Bolt11Invoice::from_str(&request)?;
 
         let quote_request = MeltQuoteBolt11Request {

+ 2 - 2
crates/cdk/src/wallet/mod.rs

@@ -249,7 +249,7 @@ impl Wallet {
 
     /// Query mint for current mint information
     #[instrument(skip(self))]
-    pub async fn get_mint_info(&self) -> Result<Option<MintInfo>, Error> {
+    pub async fn fetch_mint_info(&self) -> Result<Option<MintInfo>, Error> {
         match self.client.get_mint_info().await {
             Ok(mint_info) => {
                 // If mint provides time make sure it is accurate
@@ -382,7 +382,7 @@ impl Wallet {
             .await?
             .is_none()
         {
-            self.get_mint_info().await?;
+            self.fetch_mint_info().await?;
         }
 
         let keysets = self.load_mint_keysets().await?;

+ 1 - 1
crates/cdk/src/wallet/multi_mint_wallet.rs

@@ -78,7 +78,7 @@ impl MultiMintWallet {
             target_proof_count,
         )?;
 
-        wallet.get_mint_info().await?;
+        wallet.fetch_mint_info().await?;
 
         self.add_wallet(wallet.clone()).await;
 

+ 1 - 11
crates/cdk/src/wallet/receive.rs

@@ -27,18 +27,8 @@ impl Wallet {
         memo: Option<String>,
     ) -> Result<Amount, Error> {
         let mint_url = &self.mint_url;
-        // Add mint if it does not exist in the store
-        if self
-            .localstore
-            .get_mint(self.mint_url.clone())
-            .await?
-            .is_none()
-        {
-            tracing::debug!("Mint not in localstore fetching info for: {mint_url}");
-            self.get_mint_info().await?;
-        }
 
-        let _ = self.fetch_active_keyset().await?;
+        self.refresh_keysets().await?;
 
         let active_keyset_id = self.fetch_active_keyset().await?.id;
 

+ 2 - 0
crates/cdk/src/wallet/swap.rs

@@ -21,6 +21,8 @@ impl Wallet {
         spending_conditions: Option<SpendingConditions>,
         include_fees: bool,
     ) -> Result<Option<Proofs>, Error> {
+        self.refresh_keysets().await?;
+
         tracing::info!("Swapping");
         let mint_url = &self.mint_url;
         let unit = &self.unit;