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 13 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
1 change: 1 addition & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `outputs`, `unspent-outputs` print a list that includes number and type of the output;
- `Account::switch` command to allow changing accounts quickly;
- UX improvements (Ctrl+l, TAB completion/suggestions and more) during interactive account management;
- Check for existing stronghold on `restore`;

### Changed

Expand Down
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
12 changes: 12 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,12 @@ impl StrongholdAdapter {
///
/// [`unload_stronghold_snapshot()`]: Self::unload_stronghold_snapshot()
pub async fn write_stronghold_snapshot(&self, snapshot_path: Option<&Path>) -> Result<(), Error> {
if matches!(snapshot_path, Some(p) if p.is_dir()) {
// TODO: Add Error in 2.0 as its breaking.
// Issue #1197
panic!("Path is not a file: {:?}", snapshot_path.unwrap());
}

// 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