Browse Source

Proper send command

checkpoints
Aditya Kulkarni 5 years ago
parent
commit
ee19337187
  1. 1
      rust-lightclient/Cargo.toml
  2. 51
      rust-lightclient/src/commands.rs
  3. 2
      rust-lightclient/src/lightclient.rs
  4. 13
      rust-lightclient/src/main.rs

1
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"

51
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::<u64>() {
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<HashMap<String, Box<dyn Command>>> {
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);
}

2
rust-lightclient/src/lightclient.rs

@ -310,7 +310,7 @@ impl LightClient {
};
}
pub fn do_send(&self, addr: String, value: u64, memo: Option<String>) {
pub fn do_send(&self, addr: &str, value: u64, memo: Option<String>) {
let rawtx = self.wallet.send_to_address(
u32::from_str_radix("2bb40e60", 16).unwrap(), // Blossom ID
&self.sapling_spend, &self.sapling_output,

13
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" {

Loading…
Cancel
Save