From be603a8075bcdd31c1b5dd696fc4f5d4417fe9c4 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Sun, 24 May 2020 14:13:57 -0700 Subject: [PATCH] Recover now saves the recovered seed --- cli/src/lib.rs | 36 +++++++++++++++++++++++++++++++++++- lib/src/lightclient.rs | 15 +++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index b81f592..419507b 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -246,7 +246,41 @@ pub fn attempt_recover_seed(password: Option) { }; match LightClient::attempt_recover_seed(&config, password) { - Ok(seed) => println!("Recovered seed: '{}'", seed), + Ok(seed) => { + println!("Recovered seed: '{}'", seed); + println!("Do you want to use this seed to re-create a new wallet?"); + + let mut rl = rustyline::Editor::<()>::new(); + match rl.readline("(Y / N): ") { + Ok(response) => { + if response.to_ascii_uppercase() == "Y" { + match attempt_save_recovered(&config, seed){ + Ok(backup_path) => { + eprintln!("Backed up existing wallet to {}", backup_path); + eprintln!("Saved a new wallet. Please start Zecwallet Lite to rescan your wallet."); + }, + Err(e) => { + eprintln!("Failed to save recovered seed. Error: {}", e) + } + }; + } else { + println!("Leaving wallet unchanged"); + } + }, + Err(_) => { + println!("Leaving wallet unchanged"); + } + } + }, Err(e) => eprintln!("Failed to recover seed. Error: {}", e) }; } + +fn attempt_save_recovered(config: &LightClientConfig, seed: String) -> Result { + let backup_path = config.backup_existing_wallet()?; + let lightclient = LightClient::new_from_phrase(seed, &config, 0, true).map_err(|e| format!("{}", e))?; + + lightclient.do_save()?; + + Ok(backup_path) +} \ No newline at end of file diff --git a/lib/src/lightclient.rs b/lib/src/lightclient.rs index 846a756..59c6c45 100644 --- a/lib/src/lightclient.rs +++ b/lib/src/lightclient.rs @@ -185,6 +185,21 @@ impl LightClientConfig { return self.get_wallet_path().exists() } + pub fn backup_existing_wallet(&self) -> Result { + if !self.wallet_exists() { + return Err(format!("Couldn't find existing wallet to backup. Looked in {:?}", self.get_wallet_path().to_str())); + } + use std::time::{SystemTime, UNIX_EPOCH}; + + let mut backup_file_path = self.get_zcash_data_path().into_path_buf(); + backup_file_path.push(&format!("zecwallet-light-wallet.backup.{}.dat", SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs())); + + let backup_file_str = backup_file_path.to_string_lossy().to_string(); + std::fs::copy(self.get_wallet_path(), backup_file_path).map_err(|e| format!("{}", e))?; + + Ok(backup_file_str) + } + pub fn get_log_path(&self) -> Box { let mut log_path = self.get_zcash_data_path().into_path_buf(); log_path.push(LOGFILE_NAME);