1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use crate::{mnemonic::Mnemonic, wallet_library::WalletLibrary};
use anyhow::{ensure, Result};
use std::{
fs::File,
io::{BufRead, BufReader, Write},
path::Path,
};
pub const DELIMITER: &str = ";";
pub fn recover<P: AsRef<Path>>(path: &P) -> Result<WalletLibrary> {
let input = File::open(path)?;
let mut buffered = BufReader::new(input);
let mut line = String::new();
let _ = buffered.read_line(&mut line)?;
let parts: Vec<&str> = line.split(DELIMITER).collect();
ensure!(parts.len() == 2, format!("Invalid entry '{}'", line));
let mnemonic = Mnemonic::from(&parts[0].to_string()[..])?;
let mut wallet = WalletLibrary::new_from_mnemonic(mnemonic);
wallet.generate_addresses(parts[1].trim().to_string().parse::<u64>()?)?;
Ok(wallet)
}
pub fn write_recovery<P: AsRef<Path>>(wallet: &WalletLibrary, path: &P) -> Result<()> {
let mut output = File::create(path)?;
writeln!(
output,
"{}{}{}",
wallet.mnemonic(),
DELIMITER,
wallet.key_leaf()
)?;
Ok(())
}