Cesar Rodas 2 周之前
父節點
當前提交
9e7f96c17d
共有 3 個文件被更改,包括 60 次插入35 次删除
  1. 18 0
      crates/cdk/src/wallet/mod.rs
  2. 37 31
      crates/cdk/src/wallet/multi_mint_wallet.rs
  3. 5 4
      crates/cdk/src/wallet/send.rs

+ 18 - 0
crates/cdk/src/wallet/mod.rs

@@ -212,6 +212,24 @@ impl Wallet {
             .build()
     }
 
+    /// Creates a new instance as a wallet, but it does not share the Pub
+    pub fn new_as(from: &Self) -> Self {
+        Self {
+            mint_url: from.mint_url.clone(),
+            unit: from.unit.clone(),
+            localstore: from.localstore.clone(),
+            metadata_cache: from.metadata_cache.clone(),
+            metadata_cache_ttl: from.metadata_cache_ttl.clone(),
+            target_proof_count: from.target_proof_count.clone(),
+            #[cfg(feature = "auth")]
+            auth_wallet: from.auth_wallet.clone(),
+            seed: from.seed.clone(),
+            client: from.client.clone(),
+            subscription: from.subscription.clone(),
+            in_error_swap_reverted_proofs: Default::default(),
+        }
+    }
+
     /// Subscribe to events
     pub async fn subscribe<T: Into<WalletParams>>(
         &self,

+ 37 - 31
crates/cdk/src/wallet/multi_mint_wallet.rs

@@ -180,7 +180,7 @@ pub struct MultiMintWallet {
     /// The currency unit this wallet supports
     unit: CurrencyUnit,
     /// Wallets indexed by mint URL
-    wallets: Arc<RwLock<BTreeMap<MintUrl, Wallet>>>,
+    wallets: Arc<RwLock<BTreeMap<MintUrl, Arc<Wallet>>>>,
     /// Proxy configuration for HTTP clients (optional)
     proxy_config: Option<url::Url>,
     /// Shared Tor transport to be cloned into each TorHttpClient (if enabled)
@@ -293,8 +293,7 @@ impl MultiMintWallet {
             .await?;
 
         // Insert into wallets map
-        let mut wallets = self.wallets.write().await;
-        wallets.insert(mint_url, wallet);
+        self.wallets.write().await.insert(mint_url, wallet.into());
 
         Ok(())
     }
@@ -315,8 +314,7 @@ impl MultiMintWallet {
             .await?;
 
         // Insert into wallets map
-        let mut wallets = self.wallets.write().await;
-        wallets.insert(mint_url, wallet);
+        self.wallets.write().await.insert(mint_url, wallet.into());
 
         Ok(())
     }
@@ -332,31 +330,33 @@ impl MultiMintWallet {
         config: WalletConfig,
     ) -> Result<(), Error> {
         // Check if wallet already exists
-        if self.has_mint(&mint_url).await {
-            // Update existing wallet in place
-            let mut wallets = self.wallets.write().await;
-            if let Some(wallet) = wallets.get_mut(&mint_url) {
-                // Update target_proof_count if provided
-                if let Some(count) = config.target_proof_count {
-                    wallet.set_target_proof_count(count);
-                }
+        // Update existing wallet in place
+        let mut wallets = self.wallets.write().await;
+        if let Some(wallet) = wallets.remove(&mint_url) {
+            let mut wallet = Wallet::new_as(&wallet);
+            // Update target_proof_count if provided
+            if let Some(count) = config.target_proof_count {
+                wallet.set_target_proof_count(count);
+            }
 
-                // Update connector if provided
-                if let Some(connector) = config.mint_connector {
-                    wallet.set_client(connector);
-                }
+            // Update connector if provided
+            if let Some(connector) = config.mint_connector {
+                wallet.set_client(connector);
+            }
 
-                // TODO: Handle auth_connector if provided
-                #[cfg(feature = "auth")]
-                if let Some(_auth_connector) = config.auth_connector {
-                    // For now, we can't easily inject auth_connector into the wallet
-                    // This would require additional work on the Wallet API
-                    // We'll note this as a future enhancement
-                }
+            // TODO: Handle auth_connector if provided
+            #[cfg(feature = "auth")]
+            if let Some(_auth_connector) = config.auth_connector {
+                // For now, we can't easily inject auth_connector into the wallet
+                // This would require additional work on the Wallet API
+                // We'll note this as a future enhancement
             }
+
+            wallets.insert(mint_url, wallet.into());
+
             Ok(())
         } else {
-            // Wallet doesn't exist, create it with the provided config
+            drop(wallets);
             self.add_mint_with_config(mint_url, config).await
         }
     }
@@ -763,9 +763,12 @@ impl MultiMintWallet {
     ) -> Result<PreparedSend, Error> {
         // Ensure the mint exists
         let wallets = self.wallets.read().await;
-        let target_wallet = wallets.get(&mint_url).ok_or(Error::UnknownMint {
-            mint_url: mint_url.to_string(),
-        })?;
+        let target_wallet = wallets
+            .get(&mint_url)
+            .ok_or(Error::UnknownMint {
+                mint_url: mint_url.to_string(),
+            })?
+            .clone();
 
         // Check current balance of target mint
         let target_balance = target_wallet.total_balance().await?;
@@ -830,9 +833,12 @@ impl MultiMintWallet {
 
         // Now prepare the send from the target mint
         let wallets = self.wallets.read().await;
-        let target_wallet = wallets.get(&mint_url).ok_or(Error::UnknownMint {
-            mint_url: mint_url.to_string(),
-        })?;
+        let target_wallet = wallets
+            .get(&mint_url)
+            .ok_or(Error::UnknownMint {
+                mint_url: mint_url.to_string(),
+            })?
+            .clone();
 
         target_wallet.prepare_send(amount, opts.send_options).await
     }

+ 5 - 4
crates/cdk/src/wallet/send.rs

@@ -1,5 +1,6 @@
 use std::collections::HashMap;
 use std::fmt::Debug;
+use std::sync::Arc;
 
 use cdk_common::nut02::KeySetInfosMethods;
 use cdk_common::util::unix_time;
@@ -27,7 +28,7 @@ impl Wallet {
     /// ```
     #[instrument(skip(self), err)]
     pub async fn prepare_send(
-        &self,
+        self: Arc<Self>,
         amount: Amount,
         opts: SendOptions,
     ) -> Result<PreparedSend, Error> {
@@ -150,7 +151,7 @@ impl Wallet {
     }
 
     async fn internal_prepare_send(
-        &self,
+        self: Arc<Self>,
         amount: Amount,
         opts: SendOptions,
         proofs: Proofs,
@@ -218,7 +219,7 @@ impl Wallet {
 
         // Return prepared send
         Ok(PreparedSend {
-            wallet: self.clone(),
+            wallet: self,
             amount,
             options: opts,
             proofs_to_swap: split_result.proofs_to_swap,
@@ -231,7 +232,7 @@ impl Wallet {
 
 /// Prepared send
 pub struct PreparedSend {
-    wallet: Wallet,
+    wallet: Arc<Wallet>,
     amount: Amount,
     options: SendOptions,
     proofs_to_swap: Proofs,