From 27fb3f984b75ac0c4a1ad0627f3c9192f374bb96 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Mon, 21 Oct 2019 12:32:15 -0700 Subject: [PATCH] Fix issue where new wallets weren't being created --- lib/src/lightclient.rs | 28 ++++++++++++++++++++++++++-- src/main.rs | 11 +++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/src/lightclient.rs b/lib/src/lightclient.rs index 877bebc..45dfe32 100644 --- a/lib/src/lightclient.rs +++ b/lib/src/lightclient.rs @@ -244,8 +244,32 @@ impl LightClient { Ok(l) } + /// Create a brand new wallet with a new seed phrase. Will fail if a wallet file + /// already exists on disk + pub fn new(config: &LightClientConfig, latest_block: u64) -> io::Result { + if config.wallet_exists() { + return Err(Error::new(ErrorKind::AlreadyExists, + "Cannot create a new wallet from seed, because a wallet already exists")); + } + + let mut l = LightClient { + wallet : Arc::new(RwLock::new(LightWallet::new(None, config, latest_block)?)), + config : config.clone(), + sapling_output : vec![], + sapling_spend : vec![] + }; + + l.set_wallet_initial_state(); + l.read_sapling_params(); + + info!("Created new wallet with a new seed!"); + info!("Created LightClient to {}", &config.server); + + Ok(l) + } + pub fn new_from_phrase(seed_phrase: String, config: &LightClientConfig, latest_block: u64) -> io::Result { - if config.get_wallet_path().exists() { + if config.wallet_exists() { return Err(Error::new(ErrorKind::AlreadyExists, "Cannot create a new wallet from seed, because a wallet already exists")); } @@ -267,7 +291,7 @@ impl LightClient { } pub fn read_from_disk(config: &LightClientConfig) -> io::Result { - if !config.get_wallet_path().exists() { + if !config.wallet_exists() { return Err(Error::new(ErrorKind::AlreadyExists, format!("Cannot read wallet. No file at {}", config.get_wallet_path().display()))); } diff --git a/src/main.rs b/src/main.rs index 96acb52..125f20a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,7 +120,7 @@ pub fn main() { Some(13) => { startup_helpers::report_permission_error(); }, - _ => eprintln!("Something else!") + _ => {} } return; } @@ -162,7 +162,14 @@ fn startup(server: http::Uri, dangerous: bool, seed: Option, first_sync: let lightclient = match seed { Some(phrase) => Arc::new(LightClient::new_from_phrase(phrase, &config, latest_block_height)?), - None => Arc::new(LightClient::read_from_disk(&config)?) + None => { + if config.wallet_exists() { + Arc::new(LightClient::read_from_disk(&config)?) + } else { + println!("Creating a new wallet"); + Arc::new(LightClient::new(&config, latest_block_height)?) + } + } }; // Print startup Messages