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

Commit ac068bf

Browse files
committed
add custom signer_from_path and signer_from_path_with_config
1 parent b613066 commit ac068bf

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::{
@@ -526,3 +526,43 @@ impl<'a> Config<'a> {
526526
}
527527
}
528528
}
529+
530+
// In clap v2, `value_of` returns `None` if the argument id is not previously specified in
531+
// `Arg`. In contrast, in clap v3, `value_of` panics in this case. Therefore, compared
532+
// to the same function in solana-clap-utils, `signer_from_path` in solana-clap-v3-utils errors
533+
// early when `path` is a valid pubkey, but `SIGNER_ARG.name` is not specified in the args.
534+
// This function behaves exactly as `signer_from_path` from solana-clap-utils by catching
535+
// this special case.
536+
fn signer_from_path(
537+
matches: &ArgMatches,
538+
path: &str,
539+
keypair_name: &str,
540+
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
541+
) -> Result<Box<dyn Signer>, Box<dyn std::error::Error>> {
542+
let config = SignerFromPathConfig::default();
543+
signer_from_path_with_config(matches, path, keypair_name, wallet_manager, &config)
544+
}
545+
546+
fn signer_from_path_with_config(
547+
matches: &ArgMatches,
548+
path: &str,
549+
keypair_name: &str,
550+
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
551+
config: &SignerFromPathConfig,
552+
) -> Result<Box<dyn Signer>, Box<dyn std::error::Error>> {
553+
if let Ok(pubkey) = Pubkey::from_str(path) {
554+
if matches.try_contains_id(SIGNER_ARG.name).is_err()
555+
&& (config.allow_null_signer || matches.try_contains_id(SIGN_ONLY_ARG.name)?)
556+
{
557+
return Ok(Box::new(NullSigner::new(&pubkey)));
558+
}
559+
}
560+
561+
solana_clap_v3_utils::keypair::signer_from_path_with_config(
562+
matches,
563+
path,
564+
keypair_name,
565+
wallet_manager,
566+
config,
567+
)
568+
}

0 commit comments

Comments
 (0)