Skip to content

Commit

Permalink
update Config::new_with_clients_and_ws_url to parse owner from source
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim-crypto committed Nov 22, 2024
1 parent 76f5da8 commit 58e0441
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
6 changes: 3 additions & 3 deletions token/cli/src/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 1 addition & 5 deletions token/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -79,11 +80,6 @@ use {
std::{collections::HashMap, fmt::Display, process::exit, rc::Rc, str::FromStr, sync::Arc},
};

fn print_error_and_exit<T, E: Display>(e: E) -> T {
eprintln!("error: {}", e);
exit(1)
}

fn amount_to_raw_amount(amount: Amount, decimals: u8, all_amount: Option<u64>, name: &str) -> u64 {
match amount {
Amount::Raw(ui_amount) => ui_amount,
Expand Down
51 changes: 42 additions & 9 deletions token/cli/src/config.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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<Arc<dyn Signer>> = {
if let Some(owner_path) = matches.try_get_one::<String>("owner").ok().flatten() {
signer_from_path_with_config(matches, owner_path, "owner", wallet_manager, &config)
if let Some(source) = matches.try_get_one::<SignerSource>("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
}
Expand Down Expand Up @@ -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<Rc<RemoteWalletManager>>,
config: &SignerFromPathConfig,
) -> Result<Box<dyn Signer>, Box<dyn std::error::Error>> {
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,
)
}
5 changes: 5 additions & 0 deletions token/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ pub mod config;
mod encryption_keypair;
mod output;
mod sort;

fn print_error_and_exit<T, E: std::fmt::Display>(e: E) -> T {
eprintln!("error: {}", e);
std::process::exit(1)
}

0 comments on commit 58e0441

Please sign in to comment.