From 80ae23be66e8bc262495d1a1511103ca839386d7 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Fri, 15 Nov 2024 11:17:23 +0900 Subject: [PATCH] update `Config::pubkey_or_default` --- token/cli/src/clap_app.rs | 43 ++++++++++++++++++++++++--------------- token/cli/src/config.rs | 9 ++++---- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/token/cli/src/clap_app.rs b/token/cli/src/clap_app.rs index b3f9aa5527e..ccdfa303c37 100644 --- a/token/cli/src/clap_app.rs +++ b/token/cli/src/clap_app.rs @@ -4,7 +4,7 @@ use { }, solana_clap_v3_utils::{ fee_payer::fee_payer_arg, - input_parsers::Amount, + input_parsers::{signer::SignerSourceParserBuilder, Amount}, input_validators::{is_pubkey, is_url_or_moniker, is_valid_pubkey, is_valid_signer}, memo::memo_arg, nonce::*, @@ -280,6 +280,17 @@ pub fn owner_address_arg<'a>() -> Arg<'a> { .help(OWNER_ADDRESS_ARG.help) } +// Temporary function that uses proper parsing to minimize commit size +// TODO: use this to replace `owner_address_arg` +pub fn owner_address_arg_temp<'a>() -> Arg<'a> { + Arg::with_name(OWNER_ADDRESS_ARG.name) + .long(OWNER_ADDRESS_ARG.long) + .takes_value(true) + .value_name("OWNER_ADDRESS") + .value_parser(SignerSourceParserBuilder::default().allow_all().build()) + .help(OWNER_ADDRESS_ARG.help) +} + pub fn owner_keypair_arg_with_value_name<'a>(value_name: &'static str) -> Arg<'a> { Arg::with_name(OWNER_KEYPAIR_ARG.name) .long(OWNER_KEYPAIR_ARG.long) @@ -1226,7 +1237,7 @@ pub fn app<'a>( "Lock the owner of this token account from ever being changed" ), ) - .arg(owner_address_arg()) + .arg(owner_address_arg_temp()) .nonce_args(true) ) .subcommand( @@ -1757,7 +1768,7 @@ pub fn app<'a>( .arg( Arg::with_name("recipient") .long("recipient") - .validator(|s| is_valid_pubkey(s)) + .value_parser(SignerSourceParserBuilder::default().allow_all().build()) .value_name("REFUND_ACCOUNT_ADDRESS") .takes_value(true) .help("The address of the account to receive remaining SOL [default: --owner]"), @@ -1805,7 +1816,7 @@ pub fn app<'a>( .arg( Arg::with_name("recipient") .long("recipient") - .validator(|s| is_valid_pubkey(s)) + .value_parser(SignerSourceParserBuilder::default().allow_all().build()) .value_name("REFUND_ACCOUNT_ADDRESS") .takes_value(true) .help("The address of the account to receive remaining SOL [default: --owner]"), @@ -1832,17 +1843,17 @@ pub fn app<'a>( .about("Get token account balance") .arg( Arg::with_name("token") - .validator(|s| is_valid_pubkey(s)) + .value_parser(SignerSourceParserBuilder::default().allow_all().build()) .value_name("TOKEN_MINT_ADDRESS") .takes_value(true) .index(1) .required_unless("address") .help("Token of associated account. To query a specific account, use the `--address` parameter instead"), ) - .arg(owner_address_arg().conflicts_with("address")) + .arg(owner_address_arg_temp().conflicts_with("address")) .arg( Arg::with_name("address") - .validator(|s| is_valid_pubkey(s)) + .value_parser(SignerSourceParserBuilder::default().allow_all().build()) .value_name("TOKEN_ACCOUNT_ADDRESS") .takes_value(true) .long("address") @@ -1903,7 +1914,7 @@ pub fn app<'a>( "Print token account addresses only" ), ) - .arg(owner_address_arg()) + .arg(owner_address_arg_temp()) ) .subcommand( SubCommand::with_name(CommandName::Address.into()) @@ -1919,7 +1930,7 @@ pub fn app<'a>( [Default: return the client keypair address]") ) .arg( - owner_address_arg() + owner_address_arg_temp() .requires("token") .help("Return the associated token address for the given owner. \ [Default: return the associated token address for the client keypair]"), @@ -1931,7 +1942,7 @@ pub fn app<'a>( .setting(AppSettings::Hidden) .arg( Arg::with_name("token") - .validator(|s| is_valid_pubkey(s)) + .value_parser(SignerSourceParserBuilder::default().allow_all().build()) .value_name("TOKEN_MINT_ADDRESS") .takes_value(true) .index(1) @@ -2026,7 +2037,7 @@ pub fn app<'a>( .about("Enable required transfer memos for token account") .arg( Arg::with_name("account") - .validator(|s| is_valid_pubkey(s)) + .value_parser(SignerSourceParserBuilder::default().allow_all().build()) .value_name("TOKEN_ACCOUNT_ADDRESS") .takes_value(true) .index(1) @@ -2044,7 +2055,7 @@ pub fn app<'a>( .about("Disable required transfer memos for token account") .arg( Arg::with_name("account") - .validator(|s| is_valid_pubkey(s)) + .value_parser(SignerSourceParserBuilder::default().allow_all().build()) .value_name("TOKEN_ACCOUNT_ADDRESS") .takes_value(true) .index(1) @@ -2062,7 +2073,7 @@ pub fn app<'a>( .about("Enable CPI Guard for token account") .arg( Arg::with_name("account") - .validator(|s| is_valid_pubkey(s)) + .value_parser(SignerSourceParserBuilder::default().allow_all().build()) .value_name("TOKEN_ACCOUNT_ADDRESS") .takes_value(true) .index(1) @@ -2080,7 +2091,7 @@ pub fn app<'a>( .about("Disable CPI Guard for token account") .arg( Arg::with_name("account") - .validator(|s| is_valid_pubkey(s)) + .value_parser(SignerSourceParserBuilder::default().allow_all().build()) .value_name("TOKEN_ACCOUNT_ADDRESS") .takes_value(true) .index(1) @@ -2355,7 +2366,7 @@ pub fn app<'a>( .about("Withdraw lamports from a Token Program owned account") .arg( Arg::with_name("from") - .validator(|s| is_valid_pubkey(s)) + .value_parser(SignerSourceParserBuilder::default().allow_all().build()) .value_name("SOURCE_ACCOUNT_ADDRESS") .takes_value(true) .required(true) @@ -2363,7 +2374,7 @@ pub fn app<'a>( ) .arg( Arg::with_name("recipient") - .validator(|s| is_valid_pubkey(s)) + .value_parser(SignerSourceParserBuilder::default().allow_all().build()) .value_name("REFUND_ACCOUNT_ADDRESS") .takes_value(true) .required(true) diff --git a/token/cli/src/config.rs b/token/cli/src/config.rs index 0ea6284bb78..fba2f830a28 100644 --- a/token/cli/src/config.rs +++ b/token/cli/src/config.rs @@ -2,7 +2,7 @@ use { crate::clap_app::{Error, COMPUTE_UNIT_LIMIT_ARG, COMPUTE_UNIT_PRICE_ARG, MULTISIG_SIGNER_ARG}, clap::ArgMatches, solana_clap_v3_utils::{ - input_parsers::pubkey_of_signer, + input_parsers::{pubkey_of_signer, signer::SignerSource}, input_validators::normalize_to_url_if_moniker, keypair::SignerFromPathConfig, nonce::{NONCE_ARG, NONCE_AUTHORITY_ARG}, @@ -398,7 +398,7 @@ impl<'a> Config<'a> { override_name: &str, wallet_manager: &mut Option>, ) -> Result { - let token = pubkey_of_signer(arg_matches, "token", wallet_manager) + let token = SignerSource::try_get_pubkey(arg_matches, "token", wallet_manager) .map_err(|e| -> Error { e.to_string().into() })?; self.associated_token_address_for_token_or_override( arg_matches, @@ -449,8 +449,9 @@ impl<'a> Config<'a> { address_name: &str, wallet_manager: &mut Option>, ) -> Result { - if let Some(address) = pubkey_of_signer(arg_matches, address_name, wallet_manager) - .map_err(|e| -> Error { e.to_string().into() })? + if let Some(address) = + SignerSource::try_get_pubkey(arg_matches, address_name, wallet_manager) + .map_err(|e| -> Error { e.to_string().into() })? { return Ok(address); }