Recover now saves the recovered seed

This commit is contained in:
Aditya Kulkarni 2020-05-24 14:13:57 -07:00
parent 555b49c9df
commit be603a8075
2 changed files with 50 additions and 1 deletions

View File

@ -246,7 +246,41 @@ pub fn attempt_recover_seed(password: Option<String>) {
};
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<String, String> {
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)
}

View File

@ -185,6 +185,21 @@ impl LightClientConfig {
return self.get_wallet_path().exists()
}
pub fn backup_existing_wallet(&self) -> Result<String, String> {
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<Path> {
let mut log_path = self.get_zcash_data_path().into_path_buf();
log_path.push(LOGFILE_NAME);