diff --git a/bindings/core/src/method_handler/wallet.rs b/bindings/core/src/method_handler/wallet.rs index ed3658dac0..7673a1b2df 100644 --- a/bindings/core/src/method_handler/wallet.rs +++ b/bindings/core/src/method_handler/wallet.rs @@ -121,7 +121,7 @@ pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletM ignore_if_bech32_mismatch, } => { wallet - .restore_from_backup( + .restore_backup( source, password, ignore_if_coin_type_mismatch, diff --git a/cli/src/command/wallet.rs b/cli/src/command/wallet.rs index fa15330864..b69f27b7f0 100644 --- a/cli/src/command/wallet.rs +++ b/cli/src/command/wallet.rs @@ -250,10 +250,7 @@ pub async fn restore_command_stronghold( .await?; let password = get_password("Stronghold backup password", false)?; - if let Err(e) = wallet - .restore_from_backup(backup_path, snapshot_path, password, None, None) - .await - { + if let Err(e) = wallet.restore_backup(backup_path.into(), 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 diff --git a/cli/src/wallet.rs b/cli/src/wallet.rs index 0fedd42a60..1b9d07b407 100644 --- a/cli/src/wallet.rs +++ b/cli/src/wallet.rs @@ -31,9 +31,7 @@ pub async fn new_wallet(cli: WalletCli) -> Result<(Option, Option, }, - LedgerNano { - is_connected: bool, - }, + LedgerNano, } let wallet_and_secret_manager = { @@ -59,9 +57,7 @@ pub async fn new_wallet(cli: WalletCli) -> Result<(Option, Option LinkedSecretManager::LedgerNano { - is_connected: ledger_nano.get_ledger_nano_status().await.connected(), - }, + SecretManager::LedgerNano(_) => LinkedSecretManager::LedgerNano, _ => panic!("only Stronghold and LedgerNano supported at the moment."), }; Some((wallet, linked_secret_manager)) @@ -264,22 +260,16 @@ pub async fn new_wallet(cli: WalletCli) -> Result<(Option, Option { - println_log_error!( - "Snapshot file for Stronghold secret manager linked with the wallet not found at '{}'", - snapshot_path.display() - ); - return Ok((None, None)); - } - LinkedSecretManager::LedgerNano { is_connected: false } => { - println_log_error!("Ledger Nano linked with the wallet not connected"); - return Ok((None, None)); - } - _ => {} + if let LinkedSecretManager::Stronghold { + snapshot_exists: None, + snapshot_path, + } = linked_secret_manager + { + println_log_error!( + "Snapshot file for Stronghold secret manager linked with the wallet not found at '{}'", + snapshot_path.display() + ); + return Ok((None, None)); } if wallet.get_accounts().await?.is_empty() { diff --git a/sdk/src/wallet/core/operations/stronghold_backup/mod.rs b/sdk/src/wallet/core/operations/stronghold_backup/mod.rs index d491adeb19..7deac4abef 100644 --- a/sdk/src/wallet/core/operations/stronghold_backup/mod.rs +++ b/sdk/src/wallet/core/operations/stronghold_backup/mod.rs @@ -3,11 +3,7 @@ pub(crate) mod stronghold_snapshot; -use std::{ - fs, - path::{Path, PathBuf}, - sync::atomic::Ordering, -}; +use std::{fs, path::PathBuf, sync::atomic::Ordering}; use futures::{future::try_join_all, FutureExt}; @@ -73,10 +69,9 @@ impl Wallet { /// coin type doesn't match /// if ignore_if_bech32_hrp_mismatch == Some("rms"), but addresses have something different like "smr", no accounts /// will be restored. - pub async fn restore_from_backup( + pub async fn restore_backup( &self, - backup_path: &Path, - snapshot_path: &Path, + backup_path: PathBuf, stronghold_password: impl Into + Send, ignore_if_coin_type_mismatch: Option, ignore_if_bech32_hrp_mismatch: Option, @@ -101,13 +96,13 @@ impl Wallet { let new_snapshot_path = if let SecretManager::Stronghold(stronghold) = &mut *secret_manager { stronghold.snapshot_path.clone() } else { - snapshot_path.into() + PathBuf::from("wallet.stronghold") }; // We'll create a new stronghold to load the backup let new_stronghold = StrongholdSecretManager::builder() .password(stronghold_password.clone()) - .build(backup_path)?; + .build(backup_path.clone())?; #[cfg_attr(not(feature = "storage"), allow(unused))] let chrysalis_data = stronghold_snapshot::migrate_snapshot_from_chrysalis_to_stardust(&new_stronghold).await?; diff --git a/sdk/tests/wallet/backup_restore.rs b/sdk/tests/wallet/backup_restore.rs index 7d15e26622..dcaef000d1 100644 --- a/sdk/tests/wallet/backup_restore.rs +++ b/sdk/tests/wallet/backup_restore.rs @@ -69,7 +69,7 @@ async fn backup_and_restore() -> Result<()> { // Wrong password fails restore_wallet - .restore_from_backup( + .restore_backup( PathBuf::from("test-storage/backup_and_restore/backup.stronghold"), "wrong password".to_owned(), None, @@ -80,7 +80,7 @@ async fn backup_and_restore() -> Result<()> { // Correct password works, even after trying with a wrong one before restore_wallet - .restore_from_backup( + .restore_backup( PathBuf::from("test-storage/backup_and_restore/backup.stronghold"), stronghold_password, None, @@ -162,7 +162,7 @@ async fn backup_and_restore_mnemonic_secret_manager() -> Result<()> { .await?; restore_wallet - .restore_from_backup( + .restore_backup( PathBuf::from("test-storage/backup_and_restore_mnemonic_secret_manager/backup.stronghold"), stronghold_password, None, @@ -247,7 +247,7 @@ async fn backup_and_restore_different_coin_type() -> Result<()> { // restore with ignore_if_coin_type_mismatch: Some(true) to not overwrite the coin type restore_wallet - .restore_from_backup( + .restore_backup( PathBuf::from("test-storage/backup_and_restore_different_coin_type/backup.stronghold"), stronghold_password, Some(true), @@ -331,7 +331,7 @@ async fn backup_and_restore_same_coin_type() -> Result<()> { // restore with ignore_if_coin_type_mismatch: Some(true) to not overwrite the coin type restore_wallet - .restore_from_backup( + .restore_backup( PathBuf::from("test-storage/backup_and_restore_same_coin_type/backup.stronghold"), stronghold_password, Some(true), @@ -413,7 +413,7 @@ async fn backup_and_restore_different_coin_type_dont_ignore() -> Result<()> { // restore with ignore_if_coin_type_mismatch: Some(true) to not overwrite the coin type restore_wallet - .restore_from_backup( + .restore_backup( PathBuf::from("test-storage/backup_and_restore_different_coin_type_dont_ignore/backup.stronghold"), stronghold_password, Some(false), @@ -498,7 +498,7 @@ async fn backup_and_restore_bech32_hrp_mismatch() -> Result<()> { .await?; restore_wallet - .restore_from_backup( + .restore_backup( PathBuf::from("test-storage/backup_and_restore_bech32_hrp_mismatch/backup.stronghold"), stronghold_password, None, @@ -550,7 +550,7 @@ async fn restore_no_secret_manager_data() -> Result<()> { let stronghold_password = "some_hopefully_secure_password".to_owned(); restore_wallet - .restore_from_backup( + .restore_backup( PathBuf::from("./tests/wallet/fixtures/no_secret_manager_data.stronghold"), stronghold_password.clone(), None, diff --git a/sdk/tests/wallet/chrysalis_migration.rs b/sdk/tests/wallet/chrysalis_migration.rs index 0fc0e288a2..71d1ff18ae 100644 --- a/sdk/tests/wallet/chrysalis_migration.rs +++ b/sdk/tests/wallet/chrysalis_migration.rs @@ -287,7 +287,7 @@ async fn migrate_chrysalis_stronghold() -> Result<()> { .await?; wallet - .restore_from_backup( + .restore_backup( "./tests/wallet/fixtures/chrysalis-backup-work-factor-0.stronghold".into(), Password::from("password".to_string()), None, diff --git a/sdk/tests/wallet/migrate_stronghold_snapshot_v2_to_v3.rs b/sdk/tests/wallet/migrate_stronghold_snapshot_v2_to_v3.rs index 448f57680b..5862ce3aef 100644 --- a/sdk/tests/wallet/migrate_stronghold_snapshot_v2_to_v3.rs +++ b/sdk/tests/wallet/migrate_stronghold_snapshot_v2_to_v3.rs @@ -95,7 +95,7 @@ async fn stronghold_snapshot_v2_v3_migration() { // restore with ignore_if_coin_type_mismatch: Some(true) to not overwrite the coin type let error = restore_manager - .restore_from_backup( + .restore_backup( PathBuf::from("./tests/wallet/fixtures/v3.stronghold"), "wrong_password".to_owned(), Some(false),