Skip to content

Commit

Permalink
revert breaking change
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex6323 committed Jan 12, 2024
1 parent b32d194 commit 7813b6d
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 47 deletions.
2 changes: 1 addition & 1 deletion bindings/core/src/method_handler/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions cli/src/command/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 12 additions & 22 deletions cli/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ pub async fn new_wallet(cli: WalletCli) -> Result<(Option<Wallet>, Option<Accoun
snapshot_path: std::path::PathBuf,
snapshot_exists: Option<iota_sdk::client::Password>,
},
LedgerNano {
is_connected: bool,
},
LedgerNano,
}

let wallet_and_secret_manager = {
Expand All @@ -59,9 +57,7 @@ pub async fn new_wallet(cli: WalletCli) -> Result<(Option<Wallet>, Option<Accoun
}
}
}
SecretManager::LedgerNano(ledger_nano) => 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))
Expand Down Expand Up @@ -264,22 +260,16 @@ pub async fn new_wallet(cli: WalletCli) -> Result<(Option<Wallet>, Option<Accoun
} else {
// no wallet command provided
if let Some((wallet, linked_secret_manager)) = wallet_and_secret_manager {
match linked_secret_manager {
LinkedSecretManager::Stronghold {
snapshot_exists: None,
snapshot_path,
} => {
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() {
Expand Down
15 changes: 5 additions & 10 deletions sdk/src/wallet/core/operations/stronghold_backup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<Password> + Send,
ignore_if_coin_type_mismatch: Option<bool>,
ignore_if_bech32_hrp_mismatch: Option<Hrp>,
Expand All @@ -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?;
Expand Down
16 changes: 8 additions & 8 deletions sdk/tests/wallet/backup_restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion sdk/tests/wallet/chrysalis_migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion sdk/tests/wallet/migrate_stronghold_snapshot_v2_to_v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 7813b6d

Please sign in to comment.