diff --git a/src/main.rs b/src/main.rs index b41de5f..d70f7db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -89,6 +89,10 @@ pub fn main() { .long("dangerous") .help("Disable server TLS certificate verification. Use this if you're running a local lightwalletd with a self-signed certificate. WARNING: This is dangerous, don't use it with a server that is not your own.") .takes_value(false)) + .arg(Arg::with_name("recover") + .long("recover") + .help("Attempt to recover the seed from the wallet") + .takes_value(false)) .arg(Arg::with_name("nosync") .help("By default, zecwallet-cli will sync the wallet at startup. Pass --nosync to prevent the automatic sync at startup.") .long("nosync") @@ -104,6 +108,11 @@ pub fn main() { .multiple(true)) .get_matches(); + if matches.is_present("recover") { + attempt_recover_seed(); + return; + } + let command = matches.value_of("COMMAND"); let params = matches.values_of("PARAMS").map(|v| v.collect()).or(Some(vec![])).unwrap(); @@ -129,7 +138,6 @@ pub fn main() { } }; - // Create a Light Client Config let config = lightclient::LightClientConfig { server : server.clone(), @@ -179,6 +187,36 @@ pub fn main() { } } +fn attempt_recover_seed() { + use std::fs::File; + use std::io::prelude::*; + use std::io::{BufReader}; + use byteorder::{LittleEndian, ReadBytesExt,}; + use bip39::{Mnemonic, Language}; + + // Create a Light Client Config in an attempt to recover the file. + let config = LightClientConfig { + server: "0.0.0.0:0".parse().unwrap(), + chain_name: "main".to_string(), + sapling_activation_height: 0, + consensus_branch_id: "000000".to_string(), + anchor_offset: 0, + no_cert_verification: false, + }; + + let mut reader = BufReader::new(File::open(config.get_wallet_path()).unwrap()); + let version = reader.read_u64::().unwrap(); + println!("Reading wallet version {}", version); + + // Seed + let mut seed_bytes = [0u8; 32]; + reader.read_exact(&mut seed_bytes).unwrap(); + + let phrase = Mnemonic::from_entropy(&seed_bytes, Language::English,).unwrap().phrase().to_string(); + + println!("Recovered seed phrase:\n{}", phrase); +} + fn start_interactive(lightclient: Arc, config: &LightClientConfig) { println!("Lightclient connecting to {}", config.server); @@ -267,5 +305,4 @@ fn start_interactive(lightclient: Arc, config: &LightClientConfig) } } } - } \ No newline at end of file