Try to open wallet in case generate_from_keys fails

This commit is contained in:
Daniel Karzel 2021-04-08 15:27:00 +10:00
parent bad6ca95e0
commit 548f057726
No known key found for this signature in database
GPG Key ID: 30C3FC2E438ADB6E
2 changed files with 16 additions and 4 deletions

View File

@ -64,6 +64,11 @@ impl Wallet {
Ok(()) 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 /// Close the wallet and open (load) another wallet by generating it from
/// keys. The generated wallet will remain loaded. /// keys. The generated wallet will remain loaded.
pub async fn create_from_and_load( pub async fn create_from_and_load(

View File

@ -193,11 +193,18 @@ async fn next_state(
BobState::BtcRedeemed(state) => { BobState::BtcRedeemed(state) => {
let (spend_key, view_key) = state.xmr_keys(); let (spend_key, view_key) = state.xmr_keys();
// NOTE: This actually generates and opens a new wallet, closing the currently if monero_wallet
// open one.
monero_wallet
.create_from_and_load(spend_key, view_key, state.monero_wallet_restore_blockheight) .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 // Ensure that the generated wallet is synced so we have a proper balance
monero_wallet.refresh().await?; monero_wallet.refresh().await?;