From 58e0441e809da1184f6431af5f4119374af33333 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Fri, 22 Nov 2024 11:04:41 +0900 Subject: [PATCH] update `Config::new_with_clients_and_ws_url` to parse owner from source --- token/cli/src/clap_app.rs | 6 ++--- token/cli/src/command.rs | 6 +---- token/cli/src/config.rs | 51 ++++++++++++++++++++++++++++++++------- token/cli/src/lib.rs | 5 ++++ 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/token/cli/src/clap_app.rs b/token/cli/src/clap_app.rs index ad8c0762a10..39f079f14da 100644 --- a/token/cli/src/clap_app.rs +++ b/token/cli/src/clap_app.rs @@ -298,7 +298,7 @@ pub fn owner_keypair_arg_with_value_name<'a>(value_name: &'static str) -> Arg<'a .long(OWNER_KEYPAIR_ARG.long) .takes_value(true) .value_name(value_name) - .validator(|s| is_valid_signer(s)) + .value_parser(SignerSourceParserBuilder::default().allow_all().build()) .help(OWNER_KEYPAIR_ARG.help) } @@ -1798,7 +1798,7 @@ pub fn app<'a>( .help("Specify the token account to close \ [default: owner's associated token account]"), ) - .arg(owner_address_arg()) + .arg(owner_address_arg_temp()) .arg(multisig_signer_arg()) .nonce_args(true) .offline_args(), @@ -2382,7 +2382,7 @@ pub fn app<'a>( .required(true) .help("Specify the address of the account to send lamports to"), ) - .arg(owner_address_arg()) + .arg(owner_address_arg_temp()) .arg(multisig_signer_arg()) ) .subcommand( diff --git a/token/cli/src/command.rs b/token/cli/src/command.rs index 8a7cf30b5dd..7170c1734af 100644 --- a/token/cli/src/command.rs +++ b/token/cli/src/command.rs @@ -6,6 +6,7 @@ use { config::{Config, MintInfo}, encryption_keypair::*, output::*, + print_error_and_exit, sort::{sort_and_parse_token_accounts, AccountFilter}, }, clap::{value_t, value_t_or_exit, ArgMatches}, @@ -79,11 +80,6 @@ use { std::{collections::HashMap, fmt::Display, process::exit, rc::Rc, str::FromStr, sync::Arc}, }; -fn print_error_and_exit(e: E) -> T { - eprintln!("error: {}", e); - exit(1) -} - fn amount_to_raw_amount(amount: Amount, decimals: u8, all_amount: Option, name: &str) -> u64 { match amount { Amount::Raw(ui_amount) => ui_amount, diff --git a/token/cli/src/config.rs b/token/cli/src/config.rs index fba2f830a28..65c20dd5f76 100644 --- a/token/cli/src/config.rs +++ b/token/cli/src/config.rs @@ -1,8 +1,14 @@ use { - crate::clap_app::{Error, COMPUTE_UNIT_LIMIT_ARG, COMPUTE_UNIT_PRICE_ARG, MULTISIG_SIGNER_ARG}, + crate::{ + clap_app::{Error, COMPUTE_UNIT_LIMIT_ARG, COMPUTE_UNIT_PRICE_ARG, MULTISIG_SIGNER_ARG}, + print_error_and_exit, + }, clap::ArgMatches, solana_clap_v3_utils::{ - input_parsers::{pubkey_of_signer, signer::SignerSource}, + input_parsers::{ + pubkey_of_signer, + signer::{SignerSource, SignerSourceKind}, + }, input_validators::normalize_to_url_if_moniker, keypair::SignerFromPathConfig, nonce::{NONCE_ARG, NONCE_AUTHORITY_ARG}, @@ -181,23 +187,23 @@ impl<'a> Config<'a> { }; let default_keypair = cli_config.keypair_path.clone(); - + let default_keypair_source = + SignerSource::parse(&default_keypair).unwrap_or_else(print_error_and_exit); let default_signer: Option> = { - if let Some(owner_path) = matches.try_get_one::("owner").ok().flatten() { - signer_from_path_with_config(matches, owner_path, "owner", wallet_manager, &config) + if let Some(source) = matches.try_get_one::("owner").ok().flatten() { + signer_from_source_with_config(matches, source, "owner", wallet_manager, &config) .ok() } else { - signer_from_path_with_config( + signer_from_source_with_config( matches, - &default_keypair, + &default_keypair_source, "default", wallet_manager, &config, ) .map_err(|e| { if std::fs::metadata(&default_keypair).is_ok() { - eprintln!("error: {}", e); - exit(1); + print_error_and_exit(e) } else { e } @@ -616,3 +622,30 @@ fn signer_from_path_with_config( config, ) } + +/// A wrapper function around the `solana_clap_v3_utils` `signer_from_source +/// with_config` function. If the signer source is a pubkey, then it checks for +/// signing-only or if null signer is allowed and creates a null signer. +/// Otherwise, it invokes the `solana_clap_v3_utils` version of the function. +fn signer_from_source_with_config( + matches: &ArgMatches, + source: &SignerSource, + keypair_name: &str, + wallet_manager: &mut Option>, + config: &SignerFromPathConfig, +) -> Result, Box> { + if let SignerSourceKind::Pubkey(pubkey) = source.kind { + if matches.try_contains_id(SIGNER_ARG.name).is_err() + && (config.allow_null_signer || matches.try_contains_id(SIGN_ONLY_ARG.name)?) + { + return Ok(Box::new(NullSigner::new(&pubkey))); + } + } + solana_clap_v3_utils::keypair::signer_from_source_with_config( + matches, + source, + keypair_name, + wallet_manager, + config, + ) +} diff --git a/token/cli/src/lib.rs b/token/cli/src/lib.rs index b38c26b44fd..d78b11215c7 100644 --- a/token/cli/src/lib.rs +++ b/token/cli/src/lib.rs @@ -5,3 +5,8 @@ pub mod config; mod encryption_keypair; mod output; mod sort; + +fn print_error_and_exit(e: E) -> T { + eprintln!("error: {}", e); + std::process::exit(1) +}