Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 18336ec

Browse files
committed
add custom signer_from_path and signer_from_path_with_config
1 parent 73b4caf commit 18336ec

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

token/cli/src/config.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ use {
44
solana_clap_v3_utils::{
55
input_parsers::{pubkey_of_signer, value_of},
66
input_validators::normalize_to_url_if_moniker,
7-
keypair::{signer_from_path, signer_from_path_with_config, SignerFromPathConfig},
7+
keypair::SignerFromPathConfig,
88
nonce::{NONCE_ARG, NONCE_AUTHORITY_ARG},
9-
offline::{BLOCKHASH_ARG, DUMP_TRANSACTION_MESSAGE, SIGN_ONLY_ARG},
9+
offline::{BLOCKHASH_ARG, DUMP_TRANSACTION_MESSAGE, SIGNER_ARG, SIGN_ONLY_ARG},
1010
},
1111
solana_cli_output::OutputFormat,
1212
solana_client::nonblocking::rpc_client::RpcClient,
1313
solana_remote_wallet::remote_wallet::RemoteWalletManager,
1414
solana_sdk::{
1515
account::Account as RawAccount, commitment_config::CommitmentConfig, hash::Hash,
16-
pubkey::Pubkey, signature::Signer,
16+
pubkey::Pubkey, signature::Signer, signer::null_signer::NullSigner,
1717
},
1818
spl_associated_token_account::*,
1919
spl_token_2022::{
@@ -559,3 +559,43 @@ impl<'a> Config<'a> {
559559
}
560560
}
561561
}
562+
563+
// In clap v2, `value_of` returns `None` if the argument id is not previously specified in
564+
// `Arg`. In contrast, in clap v3, `value_of` panics in this case. Therefore, compared
565+
// to the same function in solana-clap-utils, `signer_from_path` in solana-clap-v3-utils errors
566+
// early when `path` is a valid pubkey, but `SIGNER_ARG.name` is not specified in the args.
567+
// This function behaves exactly as `signer_from_path` from solana-clap-utils by catching
568+
// this special case.
569+
fn signer_from_path(
570+
matches: &ArgMatches,
571+
path: &str,
572+
keypair_name: &str,
573+
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
574+
) -> Result<Box<dyn Signer>, Box<dyn std::error::Error>> {
575+
let config = SignerFromPathConfig::default();
576+
signer_from_path_with_config(matches, path, keypair_name, wallet_manager, &config)
577+
}
578+
579+
fn signer_from_path_with_config(
580+
matches: &ArgMatches,
581+
path: &str,
582+
keypair_name: &str,
583+
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
584+
config: &SignerFromPathConfig,
585+
) -> Result<Box<dyn Signer>, Box<dyn std::error::Error>> {
586+
if let Ok(pubkey) = Pubkey::from_str(path) {
587+
if matches.try_contains_id(SIGNER_ARG.name).is_err()
588+
&& (config.allow_null_signer || matches.try_contains_id(SIGN_ONLY_ARG.name)?)
589+
{
590+
return Ok(Box::new(NullSigner::new(&pubkey)));
591+
}
592+
}
593+
594+
solana_clap_v3_utils::keypair::signer_from_path_with_config(
595+
matches,
596+
path,
597+
keypair_name,
598+
wallet_manager,
599+
config,
600+
)
601+
}

0 commit comments

Comments
 (0)