Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix zingocli viewkey #1451

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions zingocli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ pub fn build_clap_app() -> clap::ArgMatches {
.arg(Arg::new("chain")
.long("chain").short('c')
.help(r#"What chain to expect, if it's not inferable from the server URI. One of "mainnet", "testnet", or "regtest""#))
.arg(Arg::new("from")
.short('f')
.short_alias('s')
.long("from")
.alias("seed")
.alias("viewing-key")
.value_name("from")
.arg(Arg::new("seed")
.short('s')
.long("seed")
.value_name("SEED PHRASE")
.value_parser(parse_seed)
.help("Create a new wallet with the given key. Can be a 24-word seed phrase or a viewkey. Will fail if wallet already exists"))
.help("Create a new wallet with the given 24-word seed phrase. Will fail if wallet already exists"))
.arg(Arg::new("viewkey")
.long("viewkey")
.value_name("UFVK")
.value_parser(parse_ufvk)
.help("Create a new wallet with the given encoded unified full viewing key. Will fail if wallet already exists"))
.arg(Arg::new("birthday")
.long("birthday")
.value_name("birthday")
Expand Down Expand Up @@ -93,6 +95,19 @@ fn parse_seed(s: &str) -> Result<String, String> {
Err("Unexpected failure to parse String!!".to_string())
}
}
/// Parse encoded UFVK to String and check for whitespaces
fn parse_ufvk(s: &str) -> Result<String, String> {
if let Ok(s) = s.parse::<String>() {
let count = s.split_whitespace().count();
if count == 1 {
Ok(s)
} else {
Err("Encoded UFVK should not contain whitespace!".to_string())
}
} else {
Err("Unexpected failure to parse String!!".to_string())
}
}
#[cfg(target_os = "linux")]
/// This function is only tested against Linux.
fn report_permission_error() {
Expand Down Expand Up @@ -282,6 +297,7 @@ fn short_circuit_on_help(params: Vec<String>) {
impl ConfigTemplate {
fn fill(matches: clap::ArgMatches) -> Result<Self, String> {
let is_regtest = matches.get_flag("regtest"); // Begin short_circuit section

let params = if let Some(vals) = matches.get_many::<String>("extra_args") {
vals.cloned().collect()
} else {
Expand All @@ -295,7 +311,17 @@ impl ConfigTemplate {
} else {
None
};
let from = matches.get_one::<String>("from");
let seed = matches.get_one::<String>("seed");
let viewkey = matches.get_one::<String>("viewkey");
let from = if seed.is_some() && viewkey.is_some() {
return Err("Cannot load a wallet from both seed phrase and viewkey!".to_string());
} else if seed.is_some() {
seed
} else if viewkey.is_some() {
viewkey
} else {
None
};
let maybe_birthday = matches
.get_one::<u32>("birthday")
.map(|bday| bday.to_string());
Expand Down
Loading