From dc2888c9a373cfbcfd9bde03892a7f3108778e04 Mon Sep 17 00:00:00 2001 From: Trent Nelson Date: Fri, 31 Jan 2020 17:30:37 -0700 Subject: [PATCH] CLI: De-replicode SigningAuthority instatiation (#8076) automerge --- cli/src/cli.rs | 49 +++++++++++++++--------------- cli/src/nonce.rs | 33 ++++----------------- cli/src/stake.rs | 77 +++++++++--------------------------------------- 3 files changed, 45 insertions(+), 114 deletions(-) diff --git a/cli/src/cli.rs b/cli/src/cli.rs index 6b91d69ed..d98df7c9d 100644 --- a/cli/src/cli.rs +++ b/cli/src/cli.rs @@ -86,21 +86,26 @@ impl SigningAuthority { matches: &ArgMatches<'_>, name: &str, signers: Option<&[(Pubkey, Signature)]>, - ) -> Result { - keypair_of(matches, name) - .map(|keypair| keypair.into()) - .or_else(|| { - pubkey_of(matches, name) - .filter(|pubkey| { - signers - .and_then(|signers| { - signers.iter().find(|(signer, _sig)| *signer == *pubkey) - }) - .is_some() - }) - .map(|pubkey| pubkey.into()) - }) - .ok_or_else(|| CliError::BadParameter("Invalid authority".to_string())) + ) -> Result, CliError> { + if matches.is_present(name) { + keypair_of(matches, name) + .map(|keypair| keypair.into()) + .or_else(|| { + pubkey_of(matches, name) + .filter(|pubkey| { + signers + .and_then(|signers| { + signers.iter().find(|(signer, _sig)| *signer == *pubkey) + }) + .is_some() + }) + .map(|pubkey| pubkey.into()) + }) + .ok_or_else(|| CliError::BadParameter("Invalid authority".to_string())) + .map(Some) + } else { + Ok(None) + } } pub fn keypair(&self) -> &Keypair { @@ -609,15 +614,11 @@ pub fn parse_command(matches: &ArgMatches<'_>) -> Result { pub fn parse_authorize_nonce_account(matches: &ArgMatches<'_>) -> Result { let nonce_account = pubkey_of(matches, "nonce_account_keypair").unwrap(); let new_authority = pubkey_of(matches, "new_authority").unwrap(); - let nonce_authority = if matches.is_present(NONCE_AUTHORITY_ARG.name) { - Some(SigningAuthority::new_from_matches( - &matches, - NONCE_AUTHORITY_ARG.name, - None, - )?) - } else { - None - }; + let nonce_authority = + SigningAuthority::new_from_matches(&matches, NONCE_AUTHORITY_ARG.name, None)?; Ok(CliCommandInfo { command: CliCommand::AuthorizeNonceAccount { @@ -282,15 +275,8 @@ pub fn parse_get_nonce(matches: &ArgMatches<'_>) -> Result) -> Result { let nonce_account = pubkey_of(matches, "nonce_account_keypair").unwrap(); - let nonce_authority = if matches.is_present(NONCE_AUTHORITY_ARG.name) { - Some(SigningAuthority::new_from_matches( - &matches, - NONCE_AUTHORITY_ARG.name, - None, - )?) - } else { - None - }; + let nonce_authority = + SigningAuthority::new_from_matches(&matches, NONCE_AUTHORITY_ARG.name, None)?; Ok(CliCommandInfo { command: CliCommand::NewNonce { @@ -320,15 +306,8 @@ pub fn parse_withdraw_from_nonce_account( let nonce_account = pubkey_of(matches, "nonce_account_keypair").unwrap(); let destination_account_pubkey = pubkey_of(matches, "destination_account_pubkey").unwrap(); let lamports = required_lamports_from(matches, "amount", "unit")?; - let nonce_authority = if matches.is_present(NONCE_AUTHORITY_ARG.name) { - Some(SigningAuthority::new_from_matches( - &matches, - NONCE_AUTHORITY_ARG.name, - None, - )?) - } else { - None - }; + let nonce_authority = + SigningAuthority::new_from_matches(&matches, NONCE_AUTHORITY_ARG.name, None)?; Ok(CliCommandInfo { command: CliCommand::WithdrawFromNonceAccount { diff --git a/cli/src/stake.rs b/cli/src/stake.rs index ca52216bd..fe385efe7 100644 --- a/cli/src/stake.rs +++ b/cli/src/stake.rs @@ -354,24 +354,10 @@ pub fn parse_stake_delegate_stake(matches: &ArgMatches<'_>) -> Result) -> Result) -> Result