From 12d009ececd7190a9f9f940715832001c03be2f4 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Thu, 2 May 2024 11:38:10 +0900 Subject: [PATCH] [clap-v3-utils] Add functions to directly parse from `SignerSource` (#1062) * add `try_get_keypair` and `try_get_keypairs` * add `try_get_signer` and `try_get_signers` * add `try_get_pubkey` and `try_get_pubkeys` * add `try_resolve` --- clap-v3-utils/src/input_parsers/signer.rs | 109 +++++++++++++++++++++- 1 file changed, 108 insertions(+), 1 deletion(-) diff --git a/clap-v3-utils/src/input_parsers/signer.rs b/clap-v3-utils/src/input_parsers/signer.rs index 1e4e8b96b4ab81..9ce69d250f1140 100644 --- a/clap-v3-utils/src/input_parsers/signer.rs +++ b/clap-v3-utils/src/input_parsers/signer.rs @@ -1,6 +1,7 @@ use { crate::keypair::{ - keypair_from_seed_phrase, pubkey_from_path, resolve_signer_from_path, signer_from_path, + keypair_from_seed_phrase, keypair_from_source, pubkey_from_path, pubkey_from_source, + resolve_signer_from_path, resolve_signer_from_source, signer_from_path, signer_from_source, ASK_KEYWORD, SKIP_SEED_PHRASE_VALIDATION_ARG, }, clap::{builder::ValueParser, ArgMatches}, @@ -89,6 +90,112 @@ impl SignerSource { } } + pub fn try_get_keypair( + matches: &ArgMatches, + name: &str, + ) -> Result, Box> { + let source = matches.try_get_one::(name)?; + if let Some(source) = source { + keypair_from_source(matches, source, name, true).map(Some) + } else { + Ok(None) + } + } + + pub fn try_get_keypairs( + matches: &ArgMatches, + name: &str, + ) -> Result>, Box> { + let sources = matches.try_get_many::(name)?; + if let Some(sources) = sources { + let keypairs = sources + .filter_map(|source| keypair_from_source(matches, source, name, true).ok()) + .collect(); + Ok(Some(keypairs)) + } else { + Ok(None) + } + } + + #[allow(clippy::type_complexity)] + pub fn try_get_signer( + matches: &ArgMatches, + name: &str, + wallet_manager: &mut Option>, + ) -> Result, Pubkey)>, Box> { + let source = matches.try_get_one::(name)?; + if let Some(source) = source { + let signer = signer_from_source(matches, source, name, wallet_manager)?; + let signer_pubkey = signer.pubkey(); + Ok(Some((signer, signer_pubkey))) + } else { + Ok(None) + } + } + + #[allow(clippy::type_complexity)] + pub fn try_get_signers( + matches: &ArgMatches, + name: &str, + wallet_manager: &mut Option>, + ) -> Result, Pubkey)>>, Box> { + let sources = matches.try_get_many::(name)?; + if let Some(sources) = sources { + let signers = sources + .filter_map(|source| { + let signer = signer_from_source(matches, source, name, wallet_manager).ok()?; + let signer_pubkey = signer.pubkey(); + Some((signer, signer_pubkey)) + }) + .collect(); + Ok(Some(signers)) + } else { + Ok(None) + } + } + + pub fn try_get_pubkey( + matches: &ArgMatches, + name: &str, + wallet_manager: &mut Option>, + ) -> Result, Box> { + let source = matches.try_get_one::(name)?; + if let Some(source) = source { + pubkey_from_source(matches, source, name, wallet_manager).map(Some) + } else { + Ok(None) + } + } + + pub fn try_get_pubkeys( + matches: &ArgMatches, + name: &str, + wallet_manager: &mut Option>, + ) -> Result>, Box> { + let sources = matches.try_get_many::(name)?; + if let Some(sources) = sources { + let pubkeys = sources + .filter_map(|source| pubkey_from_source(matches, source, name, wallet_manager).ok()) + .collect(); + Ok(Some(pubkeys)) + } else { + Ok(None) + } + } + + pub fn try_resolve( + matches: &ArgMatches, + name: &str, + wallet_manager: &mut Option>, + ) -> Result, Box> { + let source = matches.try_get_one::(name)?; + if let Some(source) = source { + resolve_signer_from_source(matches, source, name, wallet_manager) + } else { + Ok(None) + } + } + pub fn parse>(source: S) -> Result { let source = source.as_ref(); let source = {