diff --git a/swap/src/monero/wallet.rs b/swap/src/monero/wallet.rs index 0f9490c0..c01c74e3 100644 --- a/swap/src/monero/wallet.rs +++ b/swap/src/monero/wallet.rs @@ -64,6 +64,11 @@ impl Wallet { Ok(()) } + pub async fn open(&self, filename: &str) -> Result<()> { + self.inner.lock().await.open_wallet(filename).await?; + Ok(()) + } + /// Close the wallet and open (load) another wallet by generating it from /// keys. The generated wallet will remain loaded. pub async fn create_from_and_load( diff --git a/swap/src/protocol/bob/swap.rs b/swap/src/protocol/bob/swap.rs index e926c12d..a14725ba 100644 --- a/swap/src/protocol/bob/swap.rs +++ b/swap/src/protocol/bob/swap.rs @@ -193,11 +193,18 @@ async fn next_state( BobState::BtcRedeemed(state) => { let (spend_key, view_key) = state.xmr_keys(); - // NOTE: This actually generates and opens a new wallet, closing the currently - // open one. - monero_wallet + if monero_wallet .create_from_and_load(spend_key, view_key, state.monero_wallet_restore_blockheight) - .await?; + .await + .is_err() + { + // In case we failed to refresh/sweep, when resuming the wallet might already + // exist! This is a very unlikely scenario, but if we don't take care of it we + // might not be able to ever transfer the Monero. + let wallet_name = &monero::PrivateKey::from(view_key).to_string(); + tracing::warn!("Failed to generate monero wallet from keys, falling back to trying to open the the wallet if it already exists: {}", wallet_name); + monero_wallet.open(wallet_name).await?; + } // Ensure that the generated wallet is synced so we have a proper balance monero_wallet.refresh().await?;