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

Fixed cli restore in existing wallet folder #1187

Merged
merged 21 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions cli/src/command/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,14 +238,22 @@ pub async fn node_info_command(storage_path: &Path) -> Result<Wallet, Error> {
pub async fn restore_command(storage_path: &Path, snapshot_path: &Path, backup_path: &Path) -> Result<Wallet, Error> {
check_file_exists(backup_path).await?;

let password = get_password("Stronghold password", false)?;
let secret_manager = SecretManager::Stronghold(
StrongholdSecretManager::builder()
.password(password.clone())
.build(snapshot_path)?,
);
let wallet = Wallet::builder()
.with_secret_manager(secret_manager)
let mut builder = Wallet::builder();
if check_file_exists(snapshot_path).await.is_ok() {
println_log_info!(
"Detected a stronghold file at {}. Enter password to unlock:",
snapshot_path.to_str().unwrap()
);
kwek20 marked this conversation as resolved.
Show resolved Hide resolved
let password = get_password("Stronghold password", false)?;
let secret_manager = SecretManager::Stronghold(
StrongholdSecretManager::builder()
.password(password.clone())
.build(snapshot_path)?,
);
builder = builder.with_secret_manager(secret_manager);
}

let wallet: Wallet = builder
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
// Will be overwritten by the backup's value.
.with_client_options(ClientOptions::new().with_node(DEFAULT_NODE_URL)?)
.with_storage_path(storage_path.to_str().expect("invalid unicode"))
Expand All @@ -254,6 +262,7 @@ pub async fn restore_command(storage_path: &Path, snapshot_path: &Path, backup_p
.finish()
.await?;

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

println_log_info!(
Expand Down
14 changes: 14 additions & 0 deletions sdk/src/client/stronghold/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ impl StrongholdAdapterBuilder {
/// [`password()`]: Self::password()
/// [`timeout()`]: Self::timeout()
pub fn build<P: AsRef<Path>>(self, snapshot_path: P) -> Result<StrongholdAdapter, Error> {
if snapshot_path.as_ref().is_dir() {
// TODO: Add Error in 2.0 as its breaking.
// Issue #1197
panic!("Path is not a file: {:?}", snapshot_path.as_ref().to_path_buf());
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
}

// In any case, Stronghold - as a necessary component - needs to be present at this point.
let stronghold = self.stronghold.unwrap_or_default();

Expand Down Expand Up @@ -484,6 +490,14 @@ impl StrongholdAdapter {
///
/// [`unload_stronghold_snapshot()`]: Self::unload_stronghold_snapshot()
pub async fn write_stronghold_snapshot(&self, snapshot_path: Option<&Path>) -> Result<(), Error> {
if let Some(p) = snapshot_path {
if p.is_dir() {
thibault-martinez marked this conversation as resolved.
Show resolved Hide resolved
// TODO: Add Error in 2.0 as its breaking.
// Issue #1197
panic!("Path is not a file: {:?}", p);
}
}

// The key needs to be supplied first.
let locked_key_provider = self.key_provider.lock().await;
let key_provider = if let Some(key_provider) = &*locked_key_provider {
Expand Down
Loading