Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename backup fns #2074

Merged
merged 14 commits into from
Feb 28, 2024
4 changes: 2 additions & 2 deletions bindings/core/src/method/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub enum WalletMethod {
/// Expected response: [`Ok`](crate::Response::Ok)
#[cfg(feature = "stronghold")]
#[cfg_attr(docsrs, doc(cfg(feature = "stronghold")))]
Backup {
BackupToStrongholdSnapshot {
/// The backup destination.
destination: PathBuf,
/// Stronghold file password.
Expand All @@ -71,7 +71,7 @@ pub enum WalletMethod {
#[cfg(feature = "stronghold")]
#[cfg_attr(docsrs, doc(cfg(feature = "stronghold")))]
#[serde(rename_all = "camelCase")]
RestoreBackup {
RestoreFromStrongholdSnapshot {
/// The path to the backed up Stronghold.
source: PathBuf,
/// Stronghold file password.
Expand Down
8 changes: 4 additions & 4 deletions bindings/core/src/method_handler/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletM
let response = match method {
WalletMethod::Accounts => Response::OutputsData(wallet.ledger().await.accounts().cloned().collect()),
#[cfg(feature = "stronghold")]
WalletMethod::Backup { destination, password } => {
wallet.backup(destination, password).await?;
WalletMethod::BackupToStrongholdSnapshot { destination, password } => {
wallet.backup_to_stronghold_snapshot(destination, password).await?;
Response::Ok
}
#[cfg(feature = "stronghold")]
Expand All @@ -45,14 +45,14 @@ pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletM
Response::Bool(is_available)
}
#[cfg(feature = "stronghold")]
WalletMethod::RestoreBackup {
WalletMethod::RestoreFromStrongholdSnapshot {
source,
password,
ignore_if_coin_type_mismatch,
ignore_if_bech32_mismatch,
} => {
wallet
.restore_backup(
.restore_from_stronghold_snapshot(
source,
password,
ignore_if_coin_type_mismatch,
Expand Down
8 changes: 4 additions & 4 deletions bindings/nodejs/lib/types/wallet/bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ import type {
__PrepareIncreaseVotingPowerMethod__,
__PrepareDecreaseVotingPowerMethod__,
__PrepareStopParticipatingMethod__,
__BackupMethod__,
__BackupToStrongholdSnapshotMethod__,
__ChangeStrongholdPasswordMethod__,
__ClearStrongholdPasswordMethod__,
__ClearListenersMethod__,
__EmitTestEventMethod__,
__GenerateMnemonicMethod__,
__GetLedgerNanoStatusMethod__,
__IsStrongholdPasswordAvailableMethod__,
__RestoreBackupMethod__,
__RestoreFromStrongholdSnapshotMethod__,
__SetClientOptionsMethod__,
__SetStrongholdPasswordClearIntervalMethod__,
__SetStrongholdPasswordMethod__,
Expand Down Expand Up @@ -131,15 +131,15 @@ export type __WalletMethod__ =
| __GetParticipationOverviewMethod__
| __PrepareIncreaseVotingPowerMethod__
| __PrepareDecreaseVotingPowerMethod__
| __BackupMethod__
| __BackupToStrongholdSnapshotMethod__
| __ChangeStrongholdPasswordMethod__
| __ClearListenersMethod__
| __ClearStrongholdPasswordMethod__
| __EmitTestEventMethod__
| __GenerateMnemonicMethod__
| __GetLedgerNanoStatusMethod__
| __IsStrongholdPasswordAvailableMethod__
| __RestoreBackupMethod__
| __RestoreFromStrongholdSnapshotMethod__
| __SetClientOptionsMethod__
| __SetStrongholdPasswordClearIntervalMethod__
| __SetStrongholdPasswordMethod__
Expand Down
8 changes: 4 additions & 4 deletions bindings/nodejs/lib/types/wallet/bridge/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export type __AnnounceCandidacyMethod__ = {
};
};

export type __BackupMethod__ = {
name: 'backup';
export type __BackupToStrongholdSnapshotMethod__ = {
name: 'backupToStrongholdSnapshot';
data: {
destination: string;
password: string;
Expand Down Expand Up @@ -93,8 +93,8 @@ export type __IsStrongholdPasswordAvailableMethod__ = {
name: 'isStrongholdPasswordAvailable';
};

export type __RestoreBackupMethod__ = {
name: 'restoreBackup';
export type __RestoreFromStrongholdSnapshotMethod__ = {
name: 'restoreFromStrongholdSnapshot';
data: {
source: string;
password: string;
Expand Down
11 changes: 7 additions & 4 deletions bindings/nodejs/lib/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ export class Wallet {
/**
* Backup the data to a Stronghold snapshot.
*/
async backup(destination: string, password: string): Promise<void> {
async backupToStrongholdSnapshot(
destination: string,
password: string,
): Promise<void> {
await this.methodHandler.callMethod({
name: 'backup',
name: 'backupToStrongholdSnapshot',
data: {
destination,
password,
Expand Down Expand Up @@ -200,14 +203,14 @@ export class Wallet {
* if ignore_if_coin_type_mismatch == true, client options coin type and accounts will not be restored if the cointype doesn't match
* If a bech32 hrp is provided to ignore_if_bech32_hrp_mismatch, that doesn't match the one of the current address, the wallet will not be restored.
*/
async restoreBackup(
async restoreFromStrongholdSnapshot(
source: string,
password: string,
ignoreIfCoinTypeMismatch?: boolean,
ignoreIfBech32Mismatch?: string,
): Promise<void> {
await this.methodHandler.callMethod({
name: 'restoreBackup',
name: 'restoreFromStrongholdSnapshot',
data: {
source,
password,
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/examples/wallet/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@
# done once.
wallet.store_mnemonic(os.environ['MNEMONIC'])

wallet.backup("backup.stronghold", os.environ['STRONGHOLD_PASSWORD'])
wallet.backup_to_stronghold_snapshot("backup.stronghold", os.environ['STRONGHOLD_PASSWORD'])
print('Created backup')
2 changes: 1 addition & 1 deletion bindings/python/examples/wallet/restore_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
if 'STRONGHOLD_PASSWORD' not in os.environ:
raise Exception(".env STRONGHOLD_PASSWORD is undefined, see .env.example")

wallet.restore_backup("backup.stronghold", os.environ['STRONGHOLD_PASSWORD'])
wallet.restore_from_stronghold_snapshot("backup.stronghold", os.environ['STRONGHOLD_PASSWORD'])

print(f'Restored wallet: {json.dumps(wallet, indent=4)}')
4 changes: 2 additions & 2 deletions bindings/python/iota_sdk/wallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _call_method(self, name: str, data=None):
message['data'] = data
return message

def backup(self, destination: str, password: str):
def backup_to_stronghold_snapshot(self, destination: str, password: str):
"""Backup storage.
"""
return self._call_method(
Expand Down Expand Up @@ -150,7 +150,7 @@ def clear_listeners(self, events: Optional[List[int]] = None):
}
)

def restore_backup(self, source: str, password: str):
def restore_from_stronghold_snapshot(self, source: str, password: str):
"""Restore a backup from a Stronghold file.
Replaces `client_options`, `coin_type`, `secret_manager` and wallet.
Returns an error if the wallet was already created. If Stronghold is used
Expand Down
28 changes: 19 additions & 9 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn parse_bip_path(arg: &str) -> Result<Bip44, String> {

#[derive(Debug, Clone, Subcommand)]
pub enum CliCommand {
/// Create a stronghold backup file.
/// Create a backup file. Currently only Stronghold backup is supported.
Backup {
/// Path of the created stronghold backup file.
backup_path: String,
Expand All @@ -140,7 +140,7 @@ pub enum CliCommand {
},
/// Get information about currently set node.
NodeInfo,
/// Restore a stronghold backup file.
/// Restore a backup file. Currently only Stronghold backup is supported.
Restore {
/// Path of the to be restored stronghold backup file.
backup_path: String,
Expand Down Expand Up @@ -202,7 +202,7 @@ pub async fn new_wallet(cli: Cli) -> Result<Option<Wallet>, Error> {
snapshot_exists: true, ..
} => {
let password = get_password("Stronghold password", false)?;
backup_command_stronghold(&wallet, &password, Path::new(&backup_path)).await?;
backup_to_stronghold_snapshot_command(&wallet, &password, Path::new(&backup_path)).await?;
return Ok(None);
}
LinkedSecretManager::Stronghold { snapshot_path, .. } => {
Expand Down Expand Up @@ -294,7 +294,7 @@ pub async fn new_wallet(cli: Cli) -> Result<Option<Wallet>, Error> {
// we need to explicitly drop the current wallet here to prevent:
// "error accessing storage: IO error: lock hold by current process"
drop(wallet);
let wallet = restore_command_stronghold(
let wallet = restore_from_stronghold_snapshot_command(
storage_path,
snapshot_path.as_path(),
Path::new(&backup_path),
Expand All @@ -312,7 +312,8 @@ pub async fn new_wallet(cli: Cli) -> Result<Option<Wallet>, Error> {
let init_params = InitParameters::default();
let snapshot_path = Path::new(&init_params.stronghold_snapshot_path);
let wallet =
restore_command_stronghold(storage_path, snapshot_path, Path::new(&backup_path)).await?;
restore_from_stronghold_snapshot_command(storage_path, snapshot_path, Path::new(&backup_path))
.await?;
Some(wallet)
}
}
Expand Down Expand Up @@ -382,8 +383,14 @@ pub async fn new_wallet(cli: Cli) -> Result<Option<Wallet>, Error> {
})
}

pub async fn backup_command_stronghold(wallet: &Wallet, password: &Password, backup_path: &Path) -> Result<(), Error> {
wallet.backup(backup_path.into(), password.clone()).await?;
pub async fn backup_to_stronghold_snapshot_command(
wallet: &Wallet,
password: &Password,
backup_path: &Path,
) -> Result<(), Error> {
wallet
.backup_to_stronghold_snapshot(backup_path.into(), password.clone())
.await?;

println_log_info!("Wallet has been backed up to \"{}\".", backup_path.display());

Expand Down Expand Up @@ -453,7 +460,7 @@ pub async fn node_info_command(wallet: &Wallet) -> Result<(), Error> {
Ok(())
}

pub async fn restore_command_stronghold(
pub async fn restore_from_stronghold_snapshot_command(
storage_path: &Path,
snapshot_path: &Path,
backup_path: &Path,
Expand Down Expand Up @@ -495,7 +502,10 @@ pub async fn restore_command_stronghold(
.await?;

let password = get_password("Stronghold backup password", false)?;
if let Err(e) = wallet.restore_backup(backup_path.into(), password, None, None).await {
if let Err(e) = wallet
.restore_from_stronghold_snapshot(backup_path.into(), password, None, None)
.await
{
// Clean up the file system after 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. https://github.com/iotaledger/iota-sdk/issues/2018
Expand Down
20 changes: 10 additions & 10 deletions sdk/src/wallet/core/operations/stronghold_backup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub(crate) mod stronghold_snapshot;

use std::{fs, path::PathBuf};

use self::stronghold_snapshot::restore_from_stronghold_snapshot;
use self::stronghold_snapshot::restore_with_stronghold;
#[cfg(feature = "storage")]
use crate::wallet::WalletBuilder;
use crate::{
Expand All @@ -21,7 +21,7 @@ impl Wallet {
/// Backup the wallet in a Stronghold snapshot file.
///
/// `stronghold_password` must be the current one when Stronghold is used as SecretManager.
pub async fn backup(
pub async fn backup_to_stronghold_snapshot(
&self,
backup_path: PathBuf,
stronghold_password: impl Into<Password> + Send,
Expand All @@ -35,7 +35,7 @@ impl Wallet {
// Backup with existing stronghold
SecretManager::Stronghold(stronghold) => {
stronghold.set_password(stronghold_password).await?;
self.backup_to_stronghold_snapshot(stronghold).await?;
self.backup_with_stronghold(stronghold).await?;
// Write snapshot to backup path
stronghold.write_stronghold_snapshot(Some(&backup_path)).await?;
}
Expand All @@ -46,7 +46,7 @@ impl Wallet {
.password(stronghold_password)
.build(backup_path)?;

self.backup_to_stronghold_snapshot(&backup_stronghold).await?;
self.backup_with_stronghold(&backup_stronghold).await?;

// Write snapshot to backup path
backup_stronghold.write_stronghold_snapshot(None).await?;
Expand All @@ -66,7 +66,7 @@ impl Wallet {
/// coin type doesn't match
/// If a bech32 hrp is provided to ignore_if_bech32_hrp_mismatch, that doesn't match the one of the current address,
/// the wallet will not be restored.
pub async fn restore_backup(
pub async fn restore_from_stronghold_snapshot(
&self,
backup_path: PathBuf,
stronghold_password: impl Into<Password> + Send,
Expand Down Expand Up @@ -101,7 +101,7 @@ impl Wallet {
.build(backup_path.clone())?;

let (read_address, read_bip_path, read_alias, read_client_options, read_secret_manager, read_wallet_ledger) =
restore_from_stronghold_snapshot::<SecretManager>(&new_stronghold).await?;
restore_with_stronghold::<SecretManager>(&new_stronghold).await?;

// If the bip path is not matching the current one, we may ignore the backup
let ignore_backup_values = ignore_if_bip_path_mismatch.map_or(false, |ignore| {
Expand Down Expand Up @@ -205,7 +205,7 @@ impl Wallet<StrongholdSecretManager> {
/// Backup the wallet in a Stronghold snapshot file.
///
/// `stronghold_password` must be the current one when Stronghold is used as SecretManager.
pub async fn backup(
pub async fn backup_to_stronghold_snapshot(
&self,
backup_path: PathBuf,
stronghold_password: impl Into<Password> + Send,
Expand All @@ -215,7 +215,7 @@ impl Wallet<StrongholdSecretManager> {

secret_manager.set_password(stronghold_password).await?;

self.backup_to_stronghold_snapshot(&secret_manager).await?;
self.backup_with_stronghold(&secret_manager).await?;

// Write snapshot to backup path
secret_manager.write_stronghold_snapshot(Some(&backup_path)).await?;
Expand All @@ -233,7 +233,7 @@ impl Wallet<StrongholdSecretManager> {
/// bip path doesn't match
/// If a bech32 hrp is provided to ignore_if_bech32_hrp_mismatch, that doesn't match the one of the current address,
/// the wallet will not be restored.
pub async fn restore_backup(
pub async fn restore_from_stronghold_snapshot(
&self,
backup_path: PathBuf,
stronghold_password: impl Into<Password> + Send,
Expand Down Expand Up @@ -268,7 +268,7 @@ impl Wallet<StrongholdSecretManager> {
.build(backup_path.clone())?;

let (read_address, read_bip_path, read_alias, read_client_options, read_secret_manager, read_wallet_ledger) =
restore_from_stronghold_snapshot::<StrongholdSecretManager>(&new_stronghold).await?;
restore_with_stronghold::<StrongholdSecretManager>(&new_stronghold).await?;

// If the bip path is not matching the current one, we may ignore the backup
let ignore_backup_values = ignore_if_bip_path_mismatch.map_or(false, |ignore| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ where
crate::wallet::Error: From<S::Error>,
crate::client::Error: From<S::Error>,
{
pub(crate) async fn backup_to_stronghold_snapshot(
&self,
stronghold: &StrongholdAdapter,
) -> crate::wallet::Result<()> {
pub(crate) async fn backup_with_stronghold(&self, stronghold: &StrongholdAdapter) -> crate::wallet::Result<()> {
// Set migration version
stronghold
.set(MIGRATION_VERSION_KEY, &latest_backup_migration_version())
Expand Down Expand Up @@ -62,7 +59,7 @@ where
}
}

pub(crate) async fn restore_from_stronghold_snapshot<S: 'static + SecretManagerConfig>(
pub(crate) async fn restore_with_stronghold<S: 'static + SecretManagerConfig>(
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
stronghold: &StrongholdAdapter,
) -> crate::wallet::Result<(
Bech32Address,
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 @@ -103,7 +103,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_backup(
.restore_from_stronghold_snapshot(
PathBuf::from("./tests/wallet/fixtures/v3.stronghold"),
"wrong_password".to_owned(),
Some(false),
Expand Down
Loading