From 6cba53421e90b5a87b7393d724af6016fd5d5ee0 Mon Sep 17 00:00:00 2001 From: steviez Date: Tue, 18 May 2021 10:35:07 -0500 Subject: [PATCH] Disallow bad combination of arguments in keygen grind (#17251) * Use constant for outfile sentinel value * Require --use-mnemonic flag when --no-outfile flag passed to keygen grind --- clap-utils/src/input_parsers.rs | 3 +++ clap-utils/src/keypair.rs | 6 +++--- keygen/src/keygen.rs | 10 +++++++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/clap-utils/src/input_parsers.rs b/clap-utils/src/input_parsers.rs index e64785142f..5e1f2d44f0 100644 --- a/clap-utils/src/input_parsers.rs +++ b/clap-utils/src/input_parsers.rs @@ -17,6 +17,9 @@ use { std::{str::FromStr, sync::Arc}, }; +// Sentinel value used to indicate to write to screen instead of file +pub const STDOUT_OUTFILE_TOKEN: &str = "-"; + // Return parsed values from matches at `name` pub fn values_of(matches: &ArgMatches<'_>, name: &str) -> Option> where diff --git a/clap-utils/src/keypair.rs b/clap-utils/src/keypair.rs index bace212413..9384b89b9c 100644 --- a/clap-utils/src/keypair.rs +++ b/clap-utils/src/keypair.rs @@ -1,6 +1,6 @@ use { crate::{ - input_parsers::pubkeys_sigs_of, + input_parsers::{pubkeys_sigs_of, STDOUT_OUTFILE_TOKEN}, offline::{SIGNER_ARG, SIGN_ONLY_ARG}, ArgConstant, }, @@ -235,7 +235,7 @@ pub(crate) fn parse_signer_source>( } } else { match source { - "-" => Ok(SignerSource::new(SignerSourceKind::Stdin)), + STDOUT_OUTFILE_TOKEN => Ok(SignerSource::new(SignerSourceKind::Stdin)), ASK_KEYWORD => Ok(SignerSource::new_legacy(SignerSourceKind::Prompt)), _ => match Pubkey::from_str(source) { Ok(pubkey) => Ok(SignerSource::new(SignerSourceKind::Pubkey(pubkey))), @@ -632,7 +632,7 @@ mod tests { #[test] fn test_parse_signer_source() { assert!(matches!( - parse_signer_source("-").unwrap(), + parse_signer_source(STDOUT_OUTFILE_TOKEN).unwrap(), SignerSource { kind: SignerSourceKind::Stdin, derivation_path: None, diff --git a/keygen/src/keygen.rs b/keygen/src/keygen.rs index 2ad9fc1a20..a30e5c47ca 100644 --- a/keygen/src/keygen.rs +++ b/keygen/src/keygen.rs @@ -5,6 +5,7 @@ use clap::{ Arg, ArgMatches, SubCommand, }; use solana_clap_utils::{ + input_parsers::STDOUT_OUTFILE_TOKEN, input_validators::{is_parsable, is_prompt_signer_source}, keypair::{ keypair_from_path, keypair_from_seed_phrase, prompt_passphrase, signer_from_path, @@ -105,6 +106,9 @@ fn no_outfile_arg<'a, 'b>() -> Arg<'a, 'b> { Arg::with_name(NO_OUTFILE_ARG.name) .long(NO_OUTFILE_ARG.long) .conflicts_with_all(&["outfile", "silent"]) + // Require a seed phrase to avoid generating a keypair + // but having no way to get the private key + .requires("use_mnemonic") .help(NO_OUTFILE_ARG.help) } @@ -151,7 +155,7 @@ fn output_keypair( outfile: &str, source: &str, ) -> Result<(), Box> { - if outfile == "-" { + if outfile == STDOUT_OUTFILE_TOKEN { let mut stdout = std::io::stdout(); write_keypair(&keypair, &mut stdout)?; } else { @@ -550,7 +554,7 @@ fn do_main(matches: &ArgMatches<'_>) -> Result<(), Box> { }; match outfile { - Some("-") => (), + Some(STDOUT_OUTFILE_TOKEN) => (), Some(outfile) => check_for_overwrite(&outfile, &matches), None => (), } @@ -592,7 +596,7 @@ fn do_main(matches: &ArgMatches<'_>) -> Result<(), Box> { path.to_str().unwrap() }; - if outfile != "-" { + if outfile != STDOUT_OUTFILE_TOKEN { check_for_overwrite(&outfile, &matches); }