diff --git a/src/commands.rs b/src/commands.rs index ba22de2..4f98559 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -117,7 +117,8 @@ impl Command for InfoCommand { fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String { lightclient.do_sync(true); - lightclient.do_info() + + LightClient::do_info(lightclient.get_server_uri()) } } diff --git a/src/lightclient.rs b/src/lightclient.rs index 14a7009..fee25e3 100644 --- a/src/lightclient.rs +++ b/src/lightclient.rs @@ -99,11 +99,15 @@ impl LightClient { log_path.into_boxed_path() } - pub fn new(seed_phrase: Option, server: Option) -> io::Result { - let server = match server { + pub fn get_server_or_default(server: Option) -> String { + match server { Some(s) => if s.starts_with("http://") {s} else { "http://".to_string() + &s} None => DEFAULT_SERVER.to_string() - }; + } + } + + pub fn new(seed_phrase: Option, server: Option) -> io::Result { + let server = LightClient::get_server_or_default(server); let mut lc = if LightClient::get_wallet_path().exists() { // Make sure that if a wallet exists, there is no seed phrase being attempted @@ -221,15 +225,18 @@ impl LightClient { format!("Saved Wallet") } + pub fn get_server_uri(&self) -> http::Uri { + self.server.parse().unwrap() + } - pub fn do_info(&self) -> String { + + pub fn do_info(uri: http::Uri) -> String { use std::cell::RefCell; - let uri: http::Uri = self.server.parse().unwrap(); let infostr = Arc::new(RefCell::::default()); let infostrinner = infostr.clone(); - let say_hello = self.make_grpc_client(uri).unwrap() + let say_hello = LightClient::make_grpc_client(uri).unwrap() .and_then(move |mut client| { client.get_lightd_info(Request::new(Empty{})) }) @@ -698,7 +705,7 @@ impl LightClient { where F : Fn(&[u8]) { let uri: http::Uri = self.server.parse().unwrap(); - let say_hello = self.make_grpc_client(uri).unwrap() + let say_hello = LightClient::make_grpc_client(uri).unwrap() .and_then(move |mut client| { let txfilter = TxFilter { block: None, index: 0, hash: txid.0.to_vec() }; client.get_transaction(Request::new(txfilter)) @@ -724,7 +731,7 @@ impl LightClient { let infostr = Arc::new(RefCell::::default()); let infostrinner = infostr.clone(); - let say_hello = self.make_grpc_client(uri).unwrap() + let say_hello = LightClient::make_grpc_client(uri).unwrap() .and_then(move |mut client| { client.send_transaction(Request::new(RawTransaction {data: tx_bytes.to_vec(), height: 0})) }) @@ -751,7 +758,7 @@ impl LightClient { where F : FnMut(BlockId) { let uri: http::Uri = self.server.parse().unwrap(); - let say_hello = self.make_grpc_client(uri).unwrap() + let say_hello = LightClient::make_grpc_client(uri).unwrap() .and_then(|mut client| { client.get_latest_block(Request::new(ChainSpec {})) }) @@ -766,7 +773,7 @@ impl LightClient { tokio::runtime::current_thread::Runtime::new().unwrap().block_on(say_hello).unwrap(); } - fn make_grpc_client(&self, uri: http::Uri) -> Result + Send>, Box> { + fn make_grpc_client(uri: http::Uri) -> Result + Send>, Box> { let dst = Destination::try_from_uri(uri.clone())?; let connector = util::Connector::new(HttpConnector::new(4)); let settings = client::Builder::new().http2_only(true).clone(); diff --git a/src/main.rs b/src/main.rs index 4123617..addbc81 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,6 +59,10 @@ pub fn main() { let server = matches.value_of("server").map(|s| s.to_string()); let seed = matches.value_of("seed").map(|s| s.to_string()); + // Do a getinfo first, before opening the wallet + info!("{}", LightClient::do_info( + LightClient::get_server_or_default(server.clone()).parse().unwrap())); + println!("Creating Light Wallet"); let lightclient = match LightClient::new(seed, server) { @@ -66,8 +70,6 @@ pub fn main() { Err(e) => { eprintln!("Failed to start wallet. Error was:\n{}", e); return; } }; - // After getting the lightclient, ask server for info - info!("{}", lightclient.do_info()); // At startup, run a sync let sync_update = lightclient.do_sync(true);