From edcbafb98cb86b3206e5c73701f5f12b0b48da06 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Mon, 16 Sep 2019 15:45:51 -0700 Subject: [PATCH] connect to custom servers --- src/lightclient.rs | 29 +++++++++++++++++++++-------- src/main.rs | 11 +++++++++-- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/lightclient.rs b/src/lightclient.rs index c066935..9281dc5 100644 --- a/src/lightclient.rs +++ b/src/lightclient.rs @@ -35,6 +35,10 @@ type Client = crate::grpc_client::client::CompactTxStreamer, + + pub server : String, // Connection URL + + // zcash-params pub sapling_output : Vec, pub sapling_spend : Vec, } @@ -48,7 +52,12 @@ impl LightClient { } - pub fn new(seed_phrase: Option) -> io::Result { + pub fn new(seed_phrase: Option, server: Option) -> io::Result { + let server = match server { + Some(s) => if s.starts_with("http://") {s} else { "http://".to_string() + &s} + None => "http://127.0.0.1:9067".to_string() + }; + let mut lc = if Path::new("wallet.dat").exists() { // Make sure that if a wallet exists, there is no seed phrase being attempted if !seed_phrase.is_none() { @@ -61,12 +70,14 @@ impl LightClient { let wallet = LightWallet::read(&mut file_buffer)?; LightClient { wallet : Arc::new(wallet), + server : server.clone(), sapling_output : vec![], sapling_spend : vec![] } } else { let l = LightClient { wallet : Arc::new(LightWallet::new(seed_phrase)?), + server : server.clone(), sapling_output : vec![], sapling_spend : vec![] }; @@ -82,6 +93,8 @@ impl LightClient { let mut f = File::open("/home/adityapk/.zcash-params/sapling-spend.params")?; f.read_to_end(&mut lc.sapling_spend)?; + println!("Lightclient connecting to {}", server); + Ok(lc) } @@ -136,7 +149,7 @@ impl LightClient { pub fn do_info(&self) -> String { use std::cell::RefCell; - let uri: http::Uri = format!("http://127.0.0.1:9067").parse().unwrap(); + let uri: http::Uri = self.server.parse().unwrap(); let infostr = Arc::new(RefCell::::default()); let infostrinner = infostr.clone(); @@ -503,7 +516,7 @@ impl LightClient { pub fn fetch_blocks(&self, start_height: u64, end_height: u64, c: F) where F : Fn(&[u8]) { // Fetch blocks - let uri: http::Uri = format!("http://127.0.0.1:9067").parse().unwrap(); + let uri: http::Uri = self.server.parse().unwrap(); let dst = Destination::try_from_uri(uri.clone()).unwrap(); let connector = util::Connector::new(HttpConnector::new(4)); @@ -555,7 +568,7 @@ impl LightClient { pub fn fetch_utxos(&self, address: String, c: F) where F : Fn(crate::lightwallet::Utxo) { - let uri: http::Uri = format!("http://127.0.0.1:9067").parse().unwrap(); + let uri: http::Uri = self.server.parse().unwrap(); let dst = Destination::try_from_uri(uri.clone()).unwrap(); let connector = util::Connector::new(HttpConnector::new(4)); @@ -616,7 +629,7 @@ impl LightClient { pub fn fetch_transparent_txids(&self, address: String, start_height: u64, end_height: u64,c: F) where F : Fn(&[u8], u64) { - let uri: http::Uri = format!("http://127.0.0.1:9067").parse().unwrap(); + let uri: http::Uri = self.server.parse().unwrap(); let dst = Destination::try_from_uri(uri.clone()).unwrap(); let connector = util::Connector::new(HttpConnector::new(4)); @@ -666,7 +679,7 @@ impl LightClient { pub fn fetch_full_tx(&self, txid: TxId, c: F) where F : Fn(&[u8]) { - let uri: http::Uri = format!("http://127.0.0.1:9067").parse().unwrap(); + let uri: http::Uri = self.server.parse().unwrap(); let say_hello = self.make_grpc_client(uri).unwrap() .and_then(move |mut client| { @@ -689,7 +702,7 @@ impl LightClient { pub fn broadcast_raw_tx(&self, tx_bytes: Box<[u8]>) -> String { use std::cell::RefCell; - let uri: http::Uri = format!("http://127.0.0.1:9067").parse().unwrap(); + let uri: http::Uri = self.server.parse().unwrap(); let infostr = Arc::new(RefCell::::default()); let infostrinner = infostr.clone(); @@ -714,7 +727,7 @@ impl LightClient { pub fn fetch_latest_block(&self, mut c : F) where F : FnMut(BlockId) { - let uri: http::Uri = format!("http://127.0.0.1:9067").parse().unwrap(); + let uri: http::Uri = self.server.parse().unwrap(); let say_hello = self.make_grpc_client(uri).unwrap() .and_then(|mut client| { diff --git a/src/main.rs b/src/main.rs index f5f7544..6b40678 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,13 +28,20 @@ pub fn main() { .value_name("seed_phrase") .help("Create a new wallet with the given 24-word seed phrase. Will fail if wallet already exists") .takes_value(true)) + .arg(Arg::with_name("server") + .long("server") + .value_name("server") + .help("Lightwalletd server to connect to.") + .takes_value(true) + .default_value("http://127.0.0.1:9067")) .get_matches(); - let seed: Option = matches.value_of("seed").map(|s| s.to_string()); + let server = matches.value_of("server").map(|s| s.to_string()); + let seed = matches.value_of("seed").map(|s| s.to_string()); println!("Creating Light Wallet"); - let lightclient = match LightClient::new(seed) { + let lightclient = match LightClient::new(seed, server) { Ok(lc) => Arc::new(lc), Err(e) => { eprintln!("Failed to start wallet. Error was:\n{}", e); return; } };