From ee19337187d5a27918a7a8099600b9f7af0f2e8b Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Tue, 10 Sep 2019 14:19:52 -0700 Subject: [PATCH] Proper send command --- rust-lightclient/Cargo.toml | 1 + rust-lightclient/src/commands.rs | 51 +++++++++++++++++++---------- rust-lightclient/src/lightclient.rs | 2 +- rust-lightclient/src/main.rs | 13 +++++++- 4 files changed, 47 insertions(+), 20 deletions(-) diff --git a/rust-lightclient/Cargo.toml b/rust-lightclient/Cargo.toml index 9ffbdc1..2ba2123 100644 --- a/rust-lightclient/Cargo.toml +++ b/rust-lightclient/Cargo.toml @@ -24,6 +24,7 @@ rustyline = "5.0.2" byteorder = "1" rand = "0.5.6" json = "0.12.0" +shellwords = "1.0.0" bip39 = "0.6.0-beta.1" clap = "2.33" secp256k1 = "=0.15.0" diff --git a/rust-lightclient/src/commands.rs b/rust-lightclient/src/commands.rs index e20a4d5..6e8704c 100644 --- a/rust-lightclient/src/commands.rs +++ b/rust-lightclient/src/commands.rs @@ -7,7 +7,7 @@ pub trait Command { fn short_help(&self) -> String; - fn exec(&self, _args: &[String], lightclient: &mut LightClient); + fn exec(&self, _args: &[&str], lightclient: &mut LightClient); } struct SyncCommand {} @@ -21,7 +21,7 @@ impl Command for SyncCommand { "Download CompactBlocks and sync to the server".to_string() } - fn exec(&self, _args: &[String], lightclient: &mut LightClient) { + fn exec(&self, _args: &[&str], lightclient: &mut LightClient) { lightclient.do_sync(); } } @@ -37,7 +37,7 @@ impl Command for HelpCommand { "Lists all available commands".to_string() } - fn exec(&self, _args: &[String], _: &mut LightClient) { + fn exec(&self, _args: &[&str], _: &mut LightClient) { // Print a list of all commands println!("Available commands:"); get_commands().iter().for_each(| (cmd, obj) | { @@ -56,7 +56,7 @@ impl Command for InfoCommand { "Get the lightwalletd server's info".to_string() } - fn exec(&self, _args: &[String], lightclient: &mut LightClient) { + fn exec(&self, _args: &[&str], lightclient: &mut LightClient) { lightclient.do_info(); } } @@ -71,7 +71,7 @@ impl Command for AddressCommand { "List all current addresses".to_string() } - fn exec(&self, _args: &[String], lightclient: &mut LightClient) { + fn exec(&self, _args: &[&str], lightclient: &mut LightClient) { let res = lightclient.do_address(); println!("{}", res.pretty(2)); } @@ -80,18 +80,33 @@ impl Command for AddressCommand { struct SendCommand {} impl Command for SendCommand { fn help(&self) { - println!("Send ZEC"); + println!("Sends ZEC to an address"); + println!("Usage:"); + println!("send recipient_address value memo"); } fn short_help(&self) -> String { "Send ZEC to the given address".to_string() } - fn exec(&self, _args: &[String], lightclient: &mut LightClient) { - lightclient.do_send( - "ztestsapling1x65nq4dgp0qfywgxcwk9n0fvm4fysmapgr2q00p85ju252h6l7mmxu2jg9cqqhtvzd69jwhgv8d".to_string(), - 50000000 - 10000, - None); + fn exec(&self, args: &[&str], lightclient: &mut LightClient) { + // Parse the args. + // 1 - Destination address. T or Z address + if args.len() != 3 { + self.help(); + return; + } + + // Make sure we can parse the amount + let value = match args[1].parse::() { + Ok(amt) => amt, + Err(e) => { + println!("Couldn't parse amount: {}", e); + return; + } + }; + + lightclient.do_send(args[0], value, Some(args[2].to_string())); } } @@ -105,7 +120,7 @@ impl Command for SaveCommand { "Save wallet file to disk".to_string() } - fn exec(&self, _args: &[String], lightclient: &mut LightClient) { + fn exec(&self, _args: &[&str], lightclient: &mut LightClient) { lightclient.do_save(); } } @@ -120,7 +135,7 @@ impl Command for SeedCommand { "Display the seed phrase".to_string() } - fn exec(&self, _args: &[String], lightclient: &mut LightClient) { + fn exec(&self, _args: &[&str], lightclient: &mut LightClient) { let phrase = lightclient.do_seed_phrase(); println!("PLEASE SAVE YOUR SEED PHRASE CAREFULLY AND DO NOT SHARE IT"); @@ -140,7 +155,7 @@ impl Command for TransactionsCommand { "List all transactions in the wallet".to_string() } - fn exec(&self, _args: &[String], lightclient: &mut LightClient) { + fn exec(&self, _args: &[&str], lightclient: &mut LightClient) { let txns = lightclient.do_list_transactions(); println!("{}", txns.pretty(2)); } @@ -158,7 +173,7 @@ impl Command for NotesCommand { "List all sapling notes in the wallet".to_string() } - fn exec(&self, _args: &[String], lightclient: &mut LightClient) { + fn exec(&self, _args: &[&str], lightclient: &mut LightClient) { let txns = lightclient.do_list_notes(); println!("{}", txns.pretty(2)); } @@ -176,7 +191,7 @@ impl Command for QuitCommand { "Quit the lightwallet, saving state to disk".to_string() } - fn exec(&self, _args: &[String], lightclient: &mut LightClient) { + fn exec(&self, _args: &[&str], lightclient: &mut LightClient) { lightclient.do_save(); } } @@ -199,9 +214,9 @@ pub fn get_commands() -> Box>> { Box::new(map) } -pub fn do_user_command(cmd: &String, lightclient: &mut LightClient) { +pub fn do_user_command(cmd: &str, args: &Vec<&str>, lightclient: &mut LightClient) { match get_commands().get(cmd) { - Some(cmd) => cmd.exec(&[], lightclient), + Some(cmd) => cmd.exec(args, lightclient), None => { println!("Unknown command : {}. Type 'help' for a list of commands", cmd); } diff --git a/rust-lightclient/src/lightclient.rs b/rust-lightclient/src/lightclient.rs index a7280d1..34e8479 100644 --- a/rust-lightclient/src/lightclient.rs +++ b/rust-lightclient/src/lightclient.rs @@ -310,7 +310,7 @@ impl LightClient { }; } - pub fn do_send(&self, addr: String, value: u64, memo: Option) { + pub fn do_send(&self, addr: &str, value: u64, memo: Option) { let rawtx = self.wallet.send_to_address( u32::from_str_radix("2bb40e60", 16).unwrap(), // Blossom ID &self.sapling_spend, &self.sapling_output, diff --git a/rust-lightclient/src/main.rs b/rust-lightclient/src/main.rs index 9b83e37..6247727 100644 --- a/rust-lightclient/src/main.rs +++ b/rust-lightclient/src/main.rs @@ -49,7 +49,18 @@ pub fn main() { match readline { Ok(line) => { rl.add_history_entry(line.as_str()); - commands::do_user_command(&line, &mut lightclient); + // Parse command line arguments + let mut cmd_args = match shellwords::split(&line) { + Ok(args) => args, + Err(_) => { + println!("Mismatched Quotes"); + continue; + } + }; + + let cmd = cmd_args.remove(0); + let args: Vec<&str> = cmd_args.iter().map(|s| s.as_ref()).collect(); + commands::do_user_command(&cmd, &args, &mut lightclient); // Special check for Quit command. if line == "quit" {