clap-utils: Rename KeypairUrl to SignerSource

This commit is contained in:
Trent Nelson 2021-04-14 23:19:01 -06:00 committed by Trent Nelson
parent bb24318ef0
commit 09dcc9ea04
3 changed files with 25 additions and 25 deletions

View File

@ -1,4 +1,4 @@
use crate::keypair::{parse_keypair_path, KeypairUrl, ASK_KEYWORD}; use crate::keypair::{parse_signer_source, SignerSource, ASK_KEYWORD};
use chrono::DateTime; use chrono::DateTime;
use solana_sdk::{ use solana_sdk::{
clock::{Epoch, Slot}, clock::{Epoch, Slot},
@ -108,8 +108,8 @@ pub fn is_valid_pubkey<T>(string: T) -> Result<(), String>
where where
T: AsRef<str> + Display, T: AsRef<str> + Display,
{ {
match parse_keypair_path(string.as_ref()) { match parse_signer_source(string.as_ref()) {
KeypairUrl::Filepath(path) => is_keypair(path), SignerSource::Filepath(path) => is_keypair(path),
_ => Ok(()), _ => Ok(()),
} }
} }

View File

@ -133,7 +133,7 @@ impl DefaultSigner {
} }
} }
pub enum KeypairUrl { pub enum SignerSource {
Ask, Ask,
Filepath(String), Filepath(String),
Usb(String), Usb(String),
@ -141,17 +141,17 @@ pub enum KeypairUrl {
Pubkey(Pubkey), Pubkey(Pubkey),
} }
pub fn parse_keypair_path(path: &str) -> KeypairUrl { pub fn parse_signer_source<S: AsRef<str>>(source: S) -> SignerSource {
if path == "-" { if path == "-" {
KeypairUrl::Stdin SignerSource::Stdin
} else if path == ASK_KEYWORD { } else if path == ASK_KEYWORD {
KeypairUrl::Ask SignerSource::Ask
} else if path.starts_with("usb://") { } else if path.starts_with("usb://") {
KeypairUrl::Usb(path.to_string()) SignerSource::Usb(path.to_string())
} else if let Ok(pubkey) = Pubkey::from_str(path) { } else if let Ok(pubkey) = Pubkey::from_str(path) {
KeypairUrl::Pubkey(pubkey) SignerSource::Pubkey(pubkey)
} else { } else {
KeypairUrl::Filepath(path.to_string()) SignerSource::Filepath(path.to_string())
} }
} }
@ -198,8 +198,8 @@ pub fn signer_from_path_with_config(
wallet_manager: &mut Option<Arc<RemoteWalletManager>>, wallet_manager: &mut Option<Arc<RemoteWalletManager>>,
config: &SignerFromPathConfig, config: &SignerFromPathConfig,
) -> Result<Box<dyn Signer>, Box<dyn error::Error>> { ) -> Result<Box<dyn Signer>, Box<dyn error::Error>> {
match parse_keypair_path(path) { match parse_signer_source(path) {
KeypairUrl::Ask => { SignerSource::Ask => {
let skip_validation = matches.is_present(SKIP_SEED_PHRASE_VALIDATION_ARG.name); let skip_validation = matches.is_present(SKIP_SEED_PHRASE_VALIDATION_ARG.name);
Ok(Box::new(keypair_from_seed_phrase( Ok(Box::new(keypair_from_seed_phrase(
keypair_name, keypair_name,
@ -207,7 +207,7 @@ pub fn signer_from_path_with_config(
false, false,
)?)) )?))
} }
KeypairUrl::Filepath(path) => match read_keypair_file(&path) { SignerSource::Filepath(path) => match read_keypair_file(&path) {
Err(e) => Err(std::io::Error::new( Err(e) => Err(std::io::Error::new(
std::io::ErrorKind::Other, std::io::ErrorKind::Other,
format!("could not read keypair file \"{}\". Run \"solana-keygen new\" to create a keypair file: {}", path, e), format!("could not read keypair file \"{}\". Run \"solana-keygen new\" to create a keypair file: {}", path, e),
@ -215,11 +215,11 @@ pub fn signer_from_path_with_config(
.into()), .into()),
Ok(file) => Ok(Box::new(file)), Ok(file) => Ok(Box::new(file)),
}, },
KeypairUrl::Stdin => { SignerSource::Stdin => {
let mut stdin = std::io::stdin(); let mut stdin = std::io::stdin();
Ok(Box::new(read_keypair(&mut stdin)?)) Ok(Box::new(read_keypair(&mut stdin)?))
} }
KeypairUrl::Usb(path) => { SignerSource::Usb(path) => {
if wallet_manager.is_none() { if wallet_manager.is_none() {
*wallet_manager = maybe_wallet_manager()?; *wallet_manager = maybe_wallet_manager()?;
} }
@ -234,7 +234,7 @@ pub fn signer_from_path_with_config(
Err(RemoteWalletError::NoDeviceFound.into()) Err(RemoteWalletError::NoDeviceFound.into())
} }
} }
KeypairUrl::Pubkey(pubkey) => { SignerSource::Pubkey(pubkey) => {
let presigner = pubkeys_sigs_of(matches, SIGNER_ARG.name) let presigner = pubkeys_sigs_of(matches, SIGNER_ARG.name)
.as_ref() .as_ref()
.and_then(|presigners| presigner_from_pubkey_sigs(&pubkey, presigners)); .and_then(|presigners| presigner_from_pubkey_sigs(&pubkey, presigners));
@ -259,8 +259,8 @@ pub fn pubkey_from_path(
keypair_name: &str, keypair_name: &str,
wallet_manager: &mut Option<Arc<RemoteWalletManager>>, wallet_manager: &mut Option<Arc<RemoteWalletManager>>,
) -> Result<Pubkey, Box<dyn error::Error>> { ) -> Result<Pubkey, Box<dyn error::Error>> {
match parse_keypair_path(path) { match parse_signer_source(path) {
KeypairUrl::Pubkey(pubkey) => Ok(pubkey), SignerSource::Pubkey(pubkey) => Ok(pubkey),
_ => Ok(signer_from_path(matches, path, keypair_name, wallet_manager)?.pubkey()), _ => Ok(signer_from_path(matches, path, keypair_name, wallet_manager)?.pubkey()),
} }
} }
@ -271,14 +271,14 @@ pub fn resolve_signer_from_path(
keypair_name: &str, keypair_name: &str,
wallet_manager: &mut Option<Arc<RemoteWalletManager>>, wallet_manager: &mut Option<Arc<RemoteWalletManager>>,
) -> Result<Option<String>, Box<dyn error::Error>> { ) -> Result<Option<String>, Box<dyn error::Error>> {
match parse_keypair_path(path) { match parse_signer_source(path) {
KeypairUrl::Ask => { SignerSource::Ask => {
let skip_validation = matches.is_present(SKIP_SEED_PHRASE_VALIDATION_ARG.name); let skip_validation = matches.is_present(SKIP_SEED_PHRASE_VALIDATION_ARG.name);
// This method validates the seed phrase, but returns `None` because there is no path // This method validates the seed phrase, but returns `None` because there is no path
// on disk or to a device // on disk or to a device
keypair_from_seed_phrase(keypair_name, skip_validation, false).map(|_| None) keypair_from_seed_phrase(keypair_name, skip_validation, false).map(|_| None)
} }
KeypairUrl::Filepath(path) => match read_keypair_file(&path) { SignerSource::Filepath(path) => match read_keypair_file(&path) {
Err(e) => Err(std::io::Error::new( Err(e) => Err(std::io::Error::new(
std::io::ErrorKind::Other, std::io::ErrorKind::Other,
format!("could not read keypair file \"{}\". Run \"solana-keygen new\" to create a keypair file: {}", path, e), format!("could not read keypair file \"{}\". Run \"solana-keygen new\" to create a keypair file: {}", path, e),
@ -286,13 +286,13 @@ pub fn resolve_signer_from_path(
.into()), .into()),
Ok(_) => Ok(Some(path.to_string())), Ok(_) => Ok(Some(path.to_string())),
}, },
KeypairUrl::Stdin => { SignerSource::Stdin => {
let mut stdin = std::io::stdin(); let mut stdin = std::io::stdin();
// This method validates the keypair from stdin, but returns `None` because there is no // This method validates the keypair from stdin, but returns `None` because there is no
// path on disk or to a device // path on disk or to a device
read_keypair(&mut stdin).map(|_| None) read_keypair(&mut stdin).map(|_| None)
} }
KeypairUrl::Usb(path) => { SignerSource::Usb(path) => {
if wallet_manager.is_none() { if wallet_manager.is_none() {
*wallet_manager = maybe_wallet_manager()?; *wallet_manager = maybe_wallet_manager()?;
} }

View File

@ -2553,7 +2553,7 @@ mod tests {
} }
); );
// Test ResolveSigner Subcommand, KeypairUrl::Filepath // Test ResolveSigner Subcommand, SignerSource::Filepath
let test_resolve_signer = let test_resolve_signer =
test_commands test_commands
.clone() .clone()
@ -2565,7 +2565,7 @@ mod tests {
signers: vec![], signers: vec![],
} }
); );
// Test ResolveSigner Subcommand, KeypairUrl::Pubkey (Presigner) // Test ResolveSigner Subcommand, SignerSource::Pubkey (Presigner)
let test_resolve_signer = let test_resolve_signer =
test_commands test_commands
.clone() .clone()