CLI: De-replicode SigningAuthority instatiation (#8076)

automerge
This commit is contained in:
Trent Nelson 2020-01-31 17:30:37 -07:00 committed by GitHub
parent 9739be9ecf
commit dc2888c9a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 114 deletions

View File

@ -86,21 +86,26 @@ impl SigningAuthority {
matches: &ArgMatches<'_>,
name: &str,
signers: Option<&[(Pubkey, Signature)]>,
) -> Result<Self, CliError> {
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<Option<Self>, 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<CliCommandInfo, Box<dyn
let signers = pubkeys_sigs_of(&matches, SIGNER_ARG.name);
let blockhash_query = BlockhashQuery::new_from_matches(&matches);
let nonce_account = pubkey_of(&matches, NONCE_ARG.name);
let nonce_authority = if matches.is_present(NONCE_AUTHORITY_ARG.name) {
Some(SigningAuthority::new_from_matches(
&matches,
NONCE_AUTHORITY_ARG.name,
signers.as_deref(),
)?)
} else {
None
};
let nonce_authority = SigningAuthority::new_from_matches(
&matches,
NONCE_AUTHORITY_ARG.name,
signers.as_deref(),
)?;
Ok(CliCommandInfo {
command: CliCommand::Pay(PayCommand {

View File

@ -234,15 +234,8 @@ impl NonceSubCommands for App<'_, '_> {
pub fn parse_authorize_nonce_account(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, CliError> {
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<CliCommandInfo, CliEr
pub fn parse_new_nonce(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, CliError> {
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 {

View File

@ -354,24 +354,10 @@ pub fn parse_stake_delegate_stake(matches: &ArgMatches<'_>) -> Result<CliCommand
let blockhash_query = BlockhashQuery::new_from_matches(matches);
let require_keypair = signers.is_none();
let nonce_account = pubkey_of(&matches, NONCE_ARG.name);
let stake_authority = if matches.is_present(STAKE_AUTHORITY_ARG.name) {
Some(SigningAuthority::new_from_matches(
&matches,
STAKE_AUTHORITY_ARG.name,
signers.as_deref(),
)?)
} else {
None
};
let nonce_authority = if matches.is_present(NONCE_AUTHORITY_ARG.name) {
Some(SigningAuthority::new_from_matches(
&matches,
NONCE_AUTHORITY_ARG.name,
signers.as_deref(),
)?)
} else {
None
};
let stake_authority =
SigningAuthority::new_from_matches(&matches, STAKE_AUTHORITY_ARG.name, signers.as_deref())?;
let nonce_authority =
SigningAuthority::new_from_matches(&matches, NONCE_AUTHORITY_ARG.name, signers.as_deref())?;
Ok(CliCommandInfo {
command: CliCommand::DelegateStake {
@ -401,26 +387,12 @@ pub fn parse_stake_authorize(
};
let sign_only = matches.is_present(SIGN_ONLY_ARG.name);
let signers = pubkeys_sigs_of(&matches, SIGNER_ARG.name);
let authority = if matches.is_present(authority_flag) {
Some(SigningAuthority::new_from_matches(
&matches,
authority_flag,
signers.as_deref(),
)?)
} else {
None
};
let authority =
SigningAuthority::new_from_matches(&matches, authority_flag, signers.as_deref())?;
let blockhash_query = BlockhashQuery::new_from_matches(matches);
let nonce_account = pubkey_of(&matches, NONCE_ARG.name);
let nonce_authority = if matches.is_present(NONCE_AUTHORITY_ARG.name) {
Some(SigningAuthority::new_from_matches(
&matches,
NONCE_AUTHORITY_ARG.name,
signers.as_deref(),
)?)
} else {
None
};
let nonce_authority =
SigningAuthority::new_from_matches(&matches, NONCE_AUTHORITY_ARG.name, signers.as_deref())?;
Ok(CliCommandInfo {
command: CliCommand::StakeAuthorize {
@ -445,24 +417,10 @@ pub fn parse_stake_deactivate_stake(matches: &ArgMatches<'_>) -> Result<CliComma
let blockhash_query = BlockhashQuery::new_from_matches(matches);
let require_keypair = signers.is_none();
let nonce_account = pubkey_of(&matches, NONCE_ARG.name);
let stake_authority = if matches.is_present(STAKE_AUTHORITY_ARG.name) {
Some(SigningAuthority::new_from_matches(
&matches,
STAKE_AUTHORITY_ARG.name,
signers.as_deref(),
)?)
} else {
None
};
let nonce_authority = if matches.is_present(NONCE_AUTHORITY_ARG.name) {
Some(SigningAuthority::new_from_matches(
&matches,
NONCE_AUTHORITY_ARG.name,
signers.as_deref(),
)?)
} else {
None
};
let stake_authority =
SigningAuthority::new_from_matches(&matches, STAKE_AUTHORITY_ARG.name, signers.as_deref())?;
let nonce_authority =
SigningAuthority::new_from_matches(&matches, NONCE_AUTHORITY_ARG.name, signers.as_deref())?;
Ok(CliCommandInfo {
command: CliCommand::DeactivateStake {
@ -482,15 +440,8 @@ pub fn parse_stake_withdraw_stake(matches: &ArgMatches<'_>) -> Result<CliCommand
let stake_account_pubkey = pubkey_of(matches, "stake_account_pubkey").unwrap();
let destination_account_pubkey = pubkey_of(matches, "destination_account_pubkey").unwrap();
let lamports = required_lamports_from(matches, "amount", "unit")?;
let withdraw_authority = if matches.is_present(WITHDRAW_AUTHORITY_ARG.name) {
Some(SigningAuthority::new_from_matches(
&matches,
WITHDRAW_AUTHORITY_ARG.name,
None,
)?)
} else {
None
};
let withdraw_authority =
SigningAuthority::new_from_matches(&matches, WITHDRAW_AUTHORITY_ARG.name, None)?;
Ok(CliCommandInfo {
command: CliCommand::WithdrawStake {