use std::collections::HashMap; use json::{object}; use crate::lightclient::LightClient; pub trait Command { fn help(&self) -> String; fn short_help(&self) -> String; fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String; } struct SyncCommand {} impl Command for SyncCommand { fn help(&self) -> String { let mut h = vec![]; h.push("Sync the light client with the server"); h.push("Usage:"); h.push("sync"); h.push(""); h.join("\n") } fn short_help(&self) -> String { "Download CompactBlocks and sync to the server".to_string() } fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String { match lightclient.do_sync(true) { Ok(j) => j.pretty(2), Err(e) => e } } } struct EncryptionStatusCommand {} impl Command for EncryptionStatusCommand { fn help(&self) -> String { let mut h = vec![]; h.push("Check if the wallet is encrypted and if it is locked"); h.push("Usage:"); h.push("encryptionstatus"); h.push(""); h.join("\n") } fn short_help(&self) -> String { "Check if the wallet is encrypted and if it is locked".to_string() } fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String { lightclient.do_encryption_status().pretty(2) } } struct SyncStatusCommand {} impl Command for SyncStatusCommand { fn help(&self) -> String { let mut h = vec![]; h.push("Get the sync status of the wallet"); h.push("Usage:"); h.push("syncstatus"); h.push(""); h.join("\n") } fn short_help(&self) -> String { "Get the sync status of the wallet".to_string() } fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String { let status = lightclient.do_scan_status(); match status.is_syncing { false => object!{ "syncing" => "false" }, true => object!{ "syncing" => "true", "synced_blocks" => status.synced_blocks, "total_blocks" => status.total_blocks } }.pretty(2) } } struct RescanCommand {} impl Command for RescanCommand { fn help(&self) -> String { let mut h = vec![]; h.push("Rescan the wallet, rescanning all blocks for new transactions"); h.push("Usage:"); h.push("rescan"); h.push(""); h.push("This command will download all blocks since the intial block again from the light client server"); h.push("and attempt to scan each block for transactions belonging to the wallet."); h.join("\n") } fn short_help(&self) -> String { "Rescan the wallet, downloading and scanning all blocks and transactions".to_string() } fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String { match lightclient.do_rescan() { Ok(j) => j.pretty(2), Err(e) => e } } } struct HelpCommand {} impl Command for HelpCommand { fn help(&self) -> String { let mut h = vec![]; h.push("List all available commands"); h.push("Usage:"); h.push("help [command_name]"); h.push(""); h.push("If no \"command_name\" is specified, a list of all available commands is returned"); h.push("Example:"); h.push("help send"); h.push(""); h.join("\n") } fn short_help(&self) -> String { "Lists all available commands".to_string() } fn exec(&self, args: &[&str], _: &LightClient) -> String { let mut responses = vec![]; // Print a list of all commands match args.len() { 0 => { responses.push(format!("Available commands:")); get_commands().iter().for_each(| (cmd, obj) | { responses.push(format!("{} - {}", cmd, obj.short_help())); }); responses.join("\n") }, 1 => { match get_commands().get(args[0]) { Some(cmd) => cmd.help(), None => format!("Command {} not found", args[0]) } }, _ => self.help() } } } struct InfoCommand {} impl Command for InfoCommand { fn help(&self) -> String { let mut h = vec![]; h.push("Get info about the lightwalletd we're connected to"); h.push("Usage:"); h.push("info"); h.push(""); h.join("\n") } fn short_help(&self) -> String { "Get the lightwalletd server's info".to_string() } fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String { lightclient.do_info() } } struct BalanceCommand {} impl Command for BalanceCommand { fn help(&self) -> String { let mut h = vec![]; h.push("Show the current HUSH balance in the wallet"); h.push("Usage:"); h.push("balance"); h.push(""); h.push("Transparent and Shielded balances, along with the addresses they belong to are displayed"); h.join("\n") } fn short_help(&self) -> String { "Show the current HUSH balance in the wallet".to_string() } fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String { match lightclient.do_sync(true) { Ok(_) => format!("{}", lightclient.do_balance().pretty(2)), Err(e) => e } } } struct AddressCommand {} impl Command for AddressCommand { fn help(&self) -> String { let mut h = vec![]; h.push("List current addresses in the wallet"); h.push("Usage:"); h.push("address"); h.push(""); h.join("\n") } fn short_help(&self) -> String { "List all addresses in the wallet".to_string() } fn exec(&self, _args: &[&str], lightclient: &LightClient) -> String { format!("{}", lightclient.do_address().pretty(2)) } } struct ExportCommand {} impl Command for ExportCommand { fn help(&self) -> String { let mut h = vec![]; h.push("Export private key for an individual wallet addresses."); h.push("Note: To backup the whole wallet, use the 'seed' command insted"); h.push("Usage:"); h.push("export [t-address or z-address]"); h.push(""); h.push("If no address is passed, private key for all addresses in the wallet are exported."); h.push(""); h.push("Example:"); h.push("export ztestsapling1x65nq4dgp0qfywgxcwk9n0fvm4fysmapgr2q00p85ju252h6l7mmxu2jg9cqqhtvzd69jwhgv8d"); h.join("\n") } fn short_help(&self) -> String { "Export private key for wallet addresses".to_string() } fn exec(&self, args: &[&str], lightclient: &LightClient) -> String { if args.len() > 1 { return self.help(); } let address = if args.is_empty() { None } else { Some(args[0].to_string()) }; match lightclient.do_export(address) { Ok(j) => j, Err(e) => object!{ "error" => e } }.pretty(2) } } struct EncryptCommand {} impl Command for EncryptCommand { fn help(&self) -> String { let mut h = vec![]; h.push("Encrypt the wallet with a password"); h.push("Note 1: This will encrypt the seed and the sapling and transparent private keys."); h.push(" Use 'unlock' to temporarily unlock the wallet for spending or 'decrypt' "); h.push(" to permanatly remove the encryption"); h.push("Note 2: If you forget the password, the only way to recover the wallet is to restore"); h.push(" from the seed phrase."); h.push("Usage:"); h.push("encrypt password"); h.push(""); h.push("Example:"); h.push("encrypt my_strong_password"); h.join("\n") } fn short_help(&self) -> String { "Encrypt the wallet with a password".to_string() } fn exec(&self, args: &[&str], lightclient: &LightClient) -> String { if args.len() != 1 { return self.help(); } // Refuse to encrypt if the bip39 bug has not been fixed use crate::lightwallet::bugs::BugBip39Derivation; if BugBip39Derivation::has_bug(lightclient) { let mut h = vec![]; h.push("It looks like your wallet has the bip39bug. Please run 'fixbip39bug' to fix it"); h.push("before encrypting your wallet."); h.push("ERROR: Cannot encrypt while wallet has the bip39bug."); return h.join("\n"); } let passwd = args[0].to_string(); match lightclient.wallet.write().unwrap().encrypt(passwd) { Ok(_) => object!{ "result" => "success" }, Err(e) => object!{ "result" => "error", "error" => e.to_string() } }.pretty(2) } } struct DecryptCommand {} impl Command for DecryptCommand { fn help(&self) -> String { let mut h = vec![]; h.push("Completely remove wallet encryption, storing the wallet in plaintext on disk"); h.push("Note 1: This will decrypt the seed and the sapling and transparent private keys and store them on disk."); h.push(" Use 'unlock' to temporarily unlock the wallet for spending"); h.push("Note 2: If you've forgotten the password, the only way to recover the wallet is to restore"); h.push(" from the seed phrase."); h.push("Usage:"); h.push("decrypt password"); h.push(""); h.push("Example:"); h.push("decrypt my_strong_password"); h.join("\n") } fn short_help(&self) -> String { "Completely remove wallet encryption".to_string() } fn exec(&self, args: &[&str], lightclient: &LightClient) -> String { if args.len() != 1 { return self.help(); } let passwd = args[0].to_string(); match lightclient.wallet.write().unwrap().remove_encryption(passwd) { Ok(_) => object!{ "result" => "success" }, Err(e) => object!{ "result" => "error", "error" => e.to_string() } }.pretty(2) } } struct UnlockCommand {} impl Command for UnlockCommand { fn help(&self) -> String { let mut h = vec![]; h.push("Unlock the wallet's encryption in memory, allowing spending from this wallet."); h.push("Note 1: This will decrypt spending keys in memory only. The wallet remains encrypted on disk"); h.push(" Use 'decrypt' to remove the encryption permanatly."); h.push("Note 2: If you've forgotten the password, the only way to recover the wallet is to restore"); h.push(" from the seed phrase."); h.push("Usage:"); h.push("unlock password"); h.push(""); h.push("Example:"); h.push("unlock my_strong_password"); h.join("\n") } fn short_help(&self) -> String { "Unlock wallet encryption for spending".to_string() } fn exec(&self, args: &[&str], lightclient: &LightClient) -> String { if args.len() != 1 { return self.help(); } let passwd = args[0].to_string(); match lightclient.wallet.write().unwrap().unlock(passwd) { Ok(_) => object!{ "result" => "success" }, Err(e) => object!{ "result" => "error", "error" => e.to_string() } }.pretty(2) } } struct LockCommand {} impl Command for LockCommand { fn help(&self) -> String { let mut h = vec![]; h.push("Lock a wallet that's been temporarily unlocked. You should already have encryption enabled."); h.push("Note 1: This will remove all spending keys from memory. The wallet remains encrypted on disk"); h.push("Note 2: If you've forgotten the password, the only way to recover the wallet is to restore"); h.push(" from the seed phrase."); h.push("Usage:"); h.push("lock"); h.push(""); h.push("Example:"); h.push("lock"); h.join("\n") } fn short_help(&self) -> String { "Lock a wallet that's been temporarily unlocked".to_string() } fn exec(&self, args: &[&str], lightclient: &LightClient) -> String { if args.len() != 0 { let mut h = vec![]; h.push("Extra arguments to lock. Did you mean 'encrypt'?"); h.push(""); return format!("{}\n{}", h.join("\n"), self.help()); } match lightclient.wallet.write().unwrap().lock() { Ok(_) => object!{ "result" => "success" }, Err(e) => object!{ "result" => "error", "error" => e.to_string() } }.pretty(2) } } struct SendCommand {} impl Command for SendCommand { fn help(&self) -> String { let mut h = vec![]; h.push("Send HUSH to a given address"); h.push("Usage:"); h.push("send