From c117c5d739354f67a6cd8fbb996e0cab2e389792 Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Thu, 23 Jul 2020 10:52:38 -0700 Subject: [PATCH] Accept JSON for import --- lib/src/commands.rs | 55 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/lib/src/commands.rs b/lib/src/commands.rs index fadd7f2..349de1e 100644 --- a/lib/src/commands.rs +++ b/lib/src/commands.rs @@ -660,6 +660,8 @@ impl Command for ImportCommand { h.push("Import an external spending or viewing key into the wallet"); h.push("Usage:"); h.push("import [norescan]"); + h.push("OR"); + h.push("import '{'key': , 'birthday': , 'norescan': }'"); h.push(""); h.push("Birthday is the earliest block number that has transactions belonging to the imported key. Rescanning will start from this block. If not sure, you can specify '0', which will start rescanning from the first sapling block."); h.push("Note that you can import only the full spending (private) key or the full viewing key."); @@ -673,27 +675,54 @@ impl Command for ImportCommand { } fn exec(&self, args: &[&str], lightclient: &LightClient) -> String { - if args.len() < 2 || args.len() > 3 { + if args.len() == 0 || args.len() > 3 { return format!("Insufficient arguments\n\n{}", self.help()); } - let key = args[0]; - let birthday = match args[1].parse::() { - Ok(b) => b, - Err(_) => return format!("Couldn't parse {} as birthday. Please specify an integer. Ok to use '0'", args[1]), - }; + let (key, birthday, rescan) = if args.len() == 1 { + // If only one arg, parse it as JSON + let json_args = match json::parse(&args[0]) { + Ok(j) => j, + Err(e) => { + let es = format!("Couldn't understand JSON: {}", e); + return format!("{}\n{}", es, self.help()); + } + }; - let rescan = if args.len() == 3 { - if args[2] == "norescan" || args[2] == "false" || args[2] == "no" { - false - } else { - return format!("Couldn't undestand the argument '{}'. Please pass 'norescan' to prevent rescanning the wallet", args[2]); + if !json_args.is_object() { + return format!("Couldn't parse argument as a JSON object\n{}", self.help()); } + + if !json_args.has_key("key") { + return format!("'key' field is required in the JSON, containing the spending or viewing key to import\n{}", self.help()); + } + + if !json_args.has_key("birthday") { + return format!("'birthday' field is required in the JSON, containing the birthday of the spending or viewing key\n{}", self.help()); + } + + (json_args["key"].as_str().unwrap().to_string(), json_args["birthday"].as_u64().unwrap(), !json_args.has_key("norescan")) } else { - true + let key = args[0]; + let birthday = match args[1].parse::() { + Ok(b) => b, + Err(_) => return format!("Couldn't parse {} as birthday. Please specify an integer. Ok to use '0'", args[1]), + }; + + let rescan = if args.len() == 3 { + if args[2] == "norescan" || args[2] == "false" || args[2] == "no" { + false + } else { + return format!("Couldn't undestand the argument '{}'. Please pass 'norescan' to prevent rescanning the wallet", args[2]); + } + } else { + true + }; + + (key.to_string(), birthday, rescan) }; - let r = match lightclient.do_import_key(key.to_string(), birthday) { + let r = match lightclient.do_import_key(key, birthday) { Ok(r) => r.pretty(2), Err(e) => return format!("Error: {}", e), };