Skip to content

Commit

Permalink
clean up failed restore
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex6323 committed Jan 10, 2024
1 parent 6be0c25 commit 11b641a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
28 changes: 16 additions & 12 deletions cli/src/command/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ pub async fn accounts_command(wallet: &Wallet) -> Result<(), Error> {
Ok(())
}

// TODO: should we allow the backup to have a different password?
// TODO: allow backup wallet with ledger nano?
pub async fn backup_command_stronghold(wallet: &Wallet, password: &Password, backup_path: &Path) -> Result<(), Error> {
wallet.backup(backup_path.into(), password.clone()).await?;

Expand Down Expand Up @@ -253,16 +251,22 @@ pub async fn restore_command_stronghold(
.await?;

let password = get_password("Stronghold backup password", false)?;
wallet
.restore_from_backup(backup_path.into(), password, None, None)
.await?;

println_log_info!(
"Wallet has been restored from the backup file \"{}\".",
backup_path.display()
);

Ok(wallet)
if let Err(e) = wallet
.restore_from_backup(backup_path, snapshot_path, password, None, None)
.await
{
// Clean up a failed restore (typically produces a wallet without a secret manager)
// TODO: a better way would be to not create any files/dirs in the first place when it's not clear yet whether
// the restore will be successful
std::fs::remove_dir_all(storage_path)?;
Err(e.into())
} else {
println_log_info!(
"Wallet has been restored from the backup file \"{}\".",
backup_path.display()
);
Ok(wallet)
}
}

pub async fn set_node_url_command(wallet: &Wallet, url: String) -> Result<(), Error> {
Expand Down
16 changes: 10 additions & 6 deletions sdk/src/wallet/core/operations/stronghold_backup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

pub(crate) mod stronghold_snapshot;

use std::{fs, path::PathBuf, sync::atomic::Ordering};
use std::{
fs,
path::{Path, PathBuf},
sync::atomic::Ordering,
};

use futures::{future::try_join_all, FutureExt};

Expand Down Expand Up @@ -71,7 +75,8 @@ impl Wallet {
/// will be restored.
pub async fn restore_from_backup(
&self,
backup_path: PathBuf,
backup_path: &Path,
snapshot_path: &Path,
stronghold_password: impl Into<Password> + Send,
ignore_if_coin_type_mismatch: Option<bool>,
ignore_if_bech32_hrp_mismatch: Option<Hrp>,
Expand All @@ -96,14 +101,13 @@ impl Wallet {
let new_snapshot_path = if let SecretManager::Stronghold(stronghold) = &mut *secret_manager {
stronghold.snapshot_path.clone()
} else {
// TODO: move and use default constant from cli?
PathBuf::from("wallet.stronghold")
snapshot_path.into()
};

// We'll create a new stronghold to load the backup
let new_stronghold = StrongholdSecretManager::builder()
.password(stronghold_password.clone())
.build(backup_path.clone())?;
.build(backup_path)?;

#[cfg_attr(not(feature = "storage"), allow(unused))]
let chrysalis_data = stronghold_snapshot::migrate_snapshot_from_chrysalis_to_stardust(&new_stronghold).await?;
Expand Down Expand Up @@ -132,7 +136,7 @@ impl Wallet {
if let Some(mut read_secret_manager) = read_secret_manager {
// We have to replace the snapshot path with the current one, when building stronghold
if let SecretManagerDto::Stronghold(stronghold_dto) = &mut read_secret_manager {
stronghold_dto.snapshot_path = new_snapshot_path.to_string_lossy().into_owned();
stronghold_dto.snapshot_path = new_snapshot_path.display().to_string();
}

let restored_secret_manager = SecretManager::from_config(&read_secret_manager)
Expand Down

0 comments on commit 11b641a

Please sign in to comment.