From 1a53c8bb144a1e07bdb948ce53b96cd6a74c6557 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 19 Mar 2024 17:13:04 +0900 Subject: [PATCH 01/16] add `deprecated` feature to produce warnings on use of deprecated functions --- clap-v3-utils/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clap-v3-utils/Cargo.toml b/clap-v3-utils/Cargo.toml index 85cc2092ec07a9..3a1b3a06726c1b 100644 --- a/clap-v3-utils/Cargo.toml +++ b/clap-v3-utils/Cargo.toml @@ -11,7 +11,7 @@ edition = { workspace = true } [dependencies] chrono = { workspace = true, features = ["default"] } -clap = { version = "3.2.23", features = ["cargo"] } +clap = { version = "3.2.23", features = ["cargo", "deprecated"] } rpassword = { workspace = true } solana-remote-wallet = { workspace = true } solana-sdk = { workspace = true } From e1a931cb47020e5fde371594bb9814aeb1c08cae Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 19 Mar 2024 17:14:55 +0900 Subject: [PATCH 02/16] replace `multiple_occurrences` with arg actions --- clap-v3-utils/src/input_parsers/mod.rs | 4 ++-- clap-v3-utils/src/input_parsers/signer.rs | 4 ++-- clap-v3-utils/src/offline.rs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/clap-v3-utils/src/input_parsers/mod.rs b/clap-v3-utils/src/input_parsers/mod.rs index d96af9516b9e5d..676787e7154d7b 100644 --- a/clap-v3-utils/src/input_parsers/mod.rs +++ b/clap-v3-utils/src/input_parsers/mod.rs @@ -298,7 +298,7 @@ pub fn pubkeys_of(matches: &ArgMatches, name: &str) -> Option> { mod tests { use { super::*, - clap::{Arg, Command}, + clap::{Arg, ArgAction, Command}, solana_sdk::{hash::Hash, pubkey::Pubkey}, }; @@ -308,7 +308,7 @@ mod tests { Arg::new("multiple") .long("multiple") .takes_value(true) - .multiple_occurrences(true) + .action(ArgAction::Append) .multiple_values(true), ) .arg(Arg::new("single").takes_value(true).long("single")) diff --git a/clap-v3-utils/src/input_parsers/signer.rs b/clap-v3-utils/src/input_parsers/signer.rs index d71a37b888646a..38b328c45da318 100644 --- a/clap-v3-utils/src/input_parsers/signer.rs +++ b/clap-v3-utils/src/input_parsers/signer.rs @@ -381,7 +381,7 @@ mod tests { use { super::*, assert_matches::assert_matches, - clap::{Arg, Command}, + clap::{Arg, ArgAction, Command}, solana_remote_wallet::locator::Manufacturer, solana_sdk::signature::write_keypair_file, std::fs, @@ -512,7 +512,7 @@ mod tests { Arg::new("multiple") .long("multiple") .takes_value(true) - .multiple_occurrences(true) + .action(ArgAction::Append) .multiple_values(true), ) .arg(Arg::new("single").takes_value(true).long("single")) diff --git a/clap-v3-utils/src/offline.rs b/clap-v3-utils/src/offline.rs index cfd71e2a6b23e5..57cb4f35defa6f 100644 --- a/clap-v3-utils/src/offline.rs +++ b/clap-v3-utils/src/offline.rs @@ -1,6 +1,6 @@ use { crate::{input_parsers::signer::PubkeySignature, ArgConstant}, - clap::{value_parser, Arg, Command}, + clap::{value_parser, Arg, ArgAction, Command}, solana_sdk::hash::Hash, }; @@ -52,7 +52,7 @@ fn signer_arg<'a>() -> Arg<'a> { .value_name("PUBKEY=SIGNATURE") .value_parser(value_parser!(PubkeySignature)) .requires(BLOCKHASH_ARG.name) - .multiple_occurrences(true) + .action(ArgAction::Append) .multiple_values(false) .help(SIGNER_ARG.help) } From 5e716dca52c4899b73728697c6b49ca8527c4145 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 19 Mar 2024 17:16:52 +0900 Subject: [PATCH 03/16] replace `possible_values` with `PossibleValueParser` --- clap-v3-utils/src/keygen/mnemonic.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/clap-v3-utils/src/keygen/mnemonic.rs b/clap-v3-utils/src/keygen/mnemonic.rs index 5813e535098504..0bcc20a58bee85 100644 --- a/clap-v3-utils/src/keygen/mnemonic.rs +++ b/clap-v3-utils/src/keygen/mnemonic.rs @@ -1,7 +1,7 @@ use { crate::{keypair::prompt_passphrase, ArgConstant}, bip39::Language, - clap::{Arg, ArgMatches}, + clap::{builder::PossibleValuesParser, Arg, ArgMatches}, std::error, }; @@ -28,7 +28,7 @@ pub const NO_PASSPHRASE_ARG: ArgConstant<'static> = ArgConstant { pub fn word_count_arg<'a>() -> Arg<'a> { Arg::new(WORD_COUNT_ARG.name) .long(WORD_COUNT_ARG.long) - .possible_values(["12", "15", "18", "21", "24"]) + .value_parser(PossibleValuesParser::new(["12", "15", "18", "21", "24"])) .default_value("12") .value_name("NUMBER") .takes_value(true) @@ -38,7 +38,7 @@ pub fn word_count_arg<'a>() -> Arg<'a> { pub fn language_arg<'a>() -> Arg<'a> { Arg::new(LANGUAGE_ARG.name) .long(LANGUAGE_ARG.long) - .possible_values([ + .value_parser(PossibleValuesParser::new([ "english", "chinese-simplified", "chinese-traditional", @@ -47,7 +47,7 @@ pub fn language_arg<'a>() -> Arg<'a> { "korean", "french", "italian", - ]) + ])) .default_value("english") .value_name("LANGUAGE") .takes_value(true) From c1a522ce35fe56214f0c2b78397f7d8045146b1c Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 19 Mar 2024 17:18:05 +0900 Subject: [PATCH 04/16] deprecated `value_of` and `values_of` --- clap-v3-utils/src/input_parsers/mod.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/clap-v3-utils/src/input_parsers/mod.rs b/clap-v3-utils/src/input_parsers/mod.rs index 676787e7154d7b..0ca9a7c9b13698 100644 --- a/clap-v3-utils/src/input_parsers/mod.rs +++ b/clap-v3-utils/src/input_parsers/mod.rs @@ -27,6 +27,11 @@ pub use signer::{ }; // Return parsed values from matches at `name` +#[deprecated( + since = "1.19.0", + note = "Please use the functions `ArgMatches::get_many` or `ArgMatches::try_get_many` instead" +)] +#[allow(deprecated)] pub fn values_of(matches: &ArgMatches, name: &str) -> Option> where T: std::str::FromStr, @@ -38,6 +43,11 @@ where } // Return a parsed value from matches at `name` +#[deprecated( + since = "1.19.0", + note = "Please use the functions `ArgMatches::get_one` or `ArgMatches::try_get_one` instead" +)] +#[allow(deprecated)] pub fn value_of(matches: &ArgMatches, name: &str) -> Option where T: std::str::FromStr, @@ -63,6 +73,7 @@ pub fn unix_timestamp_from_rfc3339_datetime( since = "1.17.0", note = "please use `Amount::parse_decimal` and `Amount::sol_to_lamport` instead" )] +#[allow(deprecated)] pub fn lamports_of_sol(matches: &ArgMatches, name: &str) -> Option { value_of(matches, name).map(sol_to_lamports) } From 02cc5fd64bfd5a25c0e12146a512c9ebd16b9f86 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 19 Mar 2024 17:18:51 +0900 Subject: [PATCH 05/16] deprecate `unix_timestamp_from_rfc3339_datetime` --- clap-v3-utils/src/input_parsers/mod.rs | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/clap-v3-utils/src/input_parsers/mod.rs b/clap-v3-utils/src/input_parsers/mod.rs index 0ca9a7c9b13698..1970394f61bbac 100644 --- a/clap-v3-utils/src/input_parsers/mod.rs +++ b/clap-v3-utils/src/input_parsers/mod.rs @@ -58,6 +58,11 @@ where .and_then(|value| value.parse::().ok()) } +#[deprecated( + since = "1.19.0", + note = "Please use `ArgMatches::get_one::(...)` instead" +)] +#[allow(deprecated)] pub fn unix_timestamp_from_rfc3339_datetime( matches: &ArgMatches, name: &str, @@ -556,4 +561,31 @@ mod tests { } } } + + #[test] + fn test_unix_timestamp_from_rfc3339_datetime() { + let command = Command::new("test").arg( + Arg::new("timestamp") + .long("timestamp") + .takes_value(true) + .value_parser(clap::value_parser!(UnixTimestamp)), + ); + + // success case + let matches = command + .clone() + .try_get_matches_from(vec!["test", "--timestamp", "1234"]) + .unwrap(); + assert_eq!( + *matches.get_one::("timestamp").unwrap(), + 1234, + ); + + // validation fails + let matches_error = command + .clone() + .try_get_matches_from(vec!["test", "--timestamp", "this_is_an_invalid_arg"]) + .unwrap_err(); + assert_eq!(matches_error.kind, clap::error::ErrorKind::ValueValidation); + } } From a326515858cd0aeb273c089e6349f2af5d3877be Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 19 Mar 2024 17:19:43 +0900 Subject: [PATCH 06/16] deprecate `cluster_type_of` --- clap-v3-utils/src/input_parsers/mod.rs | 32 ++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/clap-v3-utils/src/input_parsers/mod.rs b/clap-v3-utils/src/input_parsers/mod.rs index 1970394f61bbac..d8142b6ef06e02 100644 --- a/clap-v3-utils/src/input_parsers/mod.rs +++ b/clap-v3-utils/src/input_parsers/mod.rs @@ -83,6 +83,11 @@ pub fn lamports_of_sol(matches: &ArgMatches, name: &str) -> Option { value_of(matches, name).map(sol_to_lamports) } +#[deprecated( + since = "1.19.0", + note = "Please use `ArgMatches::get_one::(...)` instead" +)] +#[allow(deprecated)] pub fn cluster_type_of(matches: &ArgMatches, name: &str) -> Option { value_of(matches, name) } @@ -588,4 +593,31 @@ mod tests { .unwrap_err(); assert_eq!(matches_error.kind, clap::error::ErrorKind::ValueValidation); } + + #[test] + fn test_cluster_type() { + let command = Command::new("test").arg( + Arg::new("cluster") + .long("cluster") + .takes_value(true) + .value_parser(clap::value_parser!(ClusterType)), + ); + + // success case + let matches = command + .clone() + .try_get_matches_from(vec!["test", "--cluster", "testnet"]) + .unwrap(); + assert_eq!( + *matches.get_one::("cluster").unwrap(), + ClusterType::Testnet + ); + + // validation fails + let matches_error = command + .clone() + .try_get_matches_from(vec!["test", "--cluster", "this_is_an_invalid_arg"]) + .unwrap_err(); + assert_eq!(matches_error.kind, clap::error::ErrorKind::ValueValidation); + } } From 4ca5359e2d5ad69ae020fbbaece1bb1f483330d9 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 19 Mar 2024 17:20:59 +0900 Subject: [PATCH 07/16] deprecate `commitment_of` --- clap-v3-utils/src/input_parsers/mod.rs | 36 +++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/clap-v3-utils/src/input_parsers/mod.rs b/clap-v3-utils/src/input_parsers/mod.rs index d8142b6ef06e02..15b75d4e526add 100644 --- a/clap-v3-utils/src/input_parsers/mod.rs +++ b/clap-v3-utils/src/input_parsers/mod.rs @@ -92,6 +92,11 @@ pub fn cluster_type_of(matches: &ArgMatches, name: &str) -> Option value_of(matches, name) } +#[deprecated( + since = "1.19.0", + note = "Please use `ArgMatches::get_one::(...)` instead" +)] +#[allow(deprecated)] pub fn commitment_of(matches: &ArgMatches, name: &str) -> Option { matches .value_of(name) @@ -320,7 +325,7 @@ mod tests { use { super::*, clap::{Arg, ArgAction, Command}, - solana_sdk::{hash::Hash, pubkey::Pubkey}, + solana_sdk::{commitment_config::CommitmentLevel, hash::Hash, pubkey::Pubkey}, }; fn app<'ab>() -> Command<'ab> { @@ -620,4 +625,33 @@ mod tests { .unwrap_err(); assert_eq!(matches_error.kind, clap::error::ErrorKind::ValueValidation); } + + #[test] + fn test_commitment_config() { + let command = Command::new("test").arg( + Arg::new("commitment") + .long("commitment") + .takes_value(true) + .value_parser(clap::value_parser!(CommitmentConfig)), + ); + + // success case + let matches = command + .clone() + .try_get_matches_from(vec!["test", "--commitment", "finalized"]) + .unwrap(); + assert_eq!( + *matches.get_one::("commitment").unwrap(), + CommitmentConfig { + commitment: CommitmentLevel::Finalized + }, + ); + + // validation fails + let matches_error = command + .clone() + .try_get_matches_from(vec!["test", "--commitment", "this_is_an_invalid_arg"]) + .unwrap_err(); + assert_eq!(matches_error.kind, clap::error::ErrorKind::ValueValidation); + } } From a4422c77d8e9fe67ae99d4b23b9aac16667cd07b Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 19 Mar 2024 17:22:03 +0900 Subject: [PATCH 08/16] deprecate `keypair_of`, `keypairs_of`, `pubkey_of`, and `pubkeys_of` functions --- clap-v3-utils/src/input_parsers/mod.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/clap-v3-utils/src/input_parsers/mod.rs b/clap-v3-utils/src/input_parsers/mod.rs index 15b75d4e526add..4c98b3347fa421 100644 --- a/clap-v3-utils/src/input_parsers/mod.rs +++ b/clap-v3-utils/src/input_parsers/mod.rs @@ -272,6 +272,11 @@ pub fn parse_derived_address_seed(arg: &str) -> Result { } // Return the keypair for an argument with filename `name` or None if not present. +#[deprecated( + since = "1.19.0", + note = "Please use `input_parsers::signer::try_keypair_of` instead" +)] +#[allow(deprecated)] pub fn keypair_of(matches: &ArgMatches, name: &str) -> Option { if let Some(value) = matches.value_of(name) { if value == ASK_KEYWORD { @@ -285,6 +290,11 @@ pub fn keypair_of(matches: &ArgMatches, name: &str) -> Option { } } +#[deprecated( + since = "1.19.0", + note = "Please use `input_parsers::signer::try_keypairs_of` instead" +)] +#[allow(deprecated)] pub fn keypairs_of(matches: &ArgMatches, name: &str) -> Option> { matches.values_of(name).map(|values| { values @@ -302,10 +312,20 @@ pub fn keypairs_of(matches: &ArgMatches, name: &str) -> Option> { // Return a pubkey for an argument that can itself be parsed into a pubkey, // or is a filename that can be read as a keypair +#[deprecated( + since = "1.19.0", + note = "Please use `input_parsers::signer::try_pubkey_of` instead" +)] +#[allow(deprecated)] pub fn pubkey_of(matches: &ArgMatches, name: &str) -> Option { value_of(matches, name).or_else(|| keypair_of(matches, name).map(|keypair| keypair.pubkey())) } +#[deprecated( + since = "1.19.0", + note = "Please use `input_parsers::signer::try_pubkeys_of` instead" +)] +#[allow(deprecated)] pub fn pubkeys_of(matches: &ArgMatches, name: &str) -> Option> { matches.values_of(name).map(|values| { values From d233d3e6c7b7c2c413f8fcc19a51272aeb217a58 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 19 Mar 2024 17:43:32 +0900 Subject: [PATCH 09/16] replace deprecated functions from `try_keypair_of`, `try_keypairs_of`, `try_pubkey_of`, and `try_pubkeys_of` --- clap-v3-utils/src/input_parsers/signer.rs | 57 ++++++++++++++++++----- 1 file changed, 45 insertions(+), 12 deletions(-) diff --git a/clap-v3-utils/src/input_parsers/signer.rs b/clap-v3-utils/src/input_parsers/signer.rs index 38b328c45da318..36bdba9c13ecb3 100644 --- a/clap-v3-utils/src/input_parsers/signer.rs +++ b/clap-v3-utils/src/input_parsers/signer.rs @@ -1,7 +1,7 @@ use { - crate::{ - input_parsers::{keypair_of, keypairs_of, pubkey_of, pubkeys_of}, - keypair::{pubkey_from_path, resolve_signer_from_path, signer_from_path, ASK_KEYWORD}, + crate::keypair::{ + keypair_from_seed_phrase, pubkey_from_path, resolve_signer_from_path, signer_from_path, + ASK_KEYWORD, SKIP_SEED_PHRASE_VALIDATION_ARG, }, clap::{builder::ValueParser, ArgMatches}, solana_remote_wallet::{ @@ -11,7 +11,7 @@ use { solana_sdk::{ derivation_path::{DerivationPath, DerivationPathError}, pubkey::Pubkey, - signature::{Keypair, Signature, Signer}, + signature::{read_keypair_file, Keypair, Signature, Signer}, }, std::{error, rc::Rc, str::FromStr}, thiserror::Error, @@ -236,16 +236,36 @@ pub fn try_keypair_of( matches: &ArgMatches, name: &str, ) -> Result, Box> { - matches.try_contains_id(name)?; - Ok(keypair_of(matches, name)) + if let Some(value) = matches.try_get_one::(name)? { + if value == ASK_KEYWORD { + let skip_validation = matches.try_contains_id(SKIP_SEED_PHRASE_VALIDATION_ARG.name)?; + keypair_from_seed_phrase(name, skip_validation, true, None, true).map(Some) + } else { + read_keypair_file(value).map(Some) + } + } else { + Ok(None) + } } pub fn try_keypairs_of( matches: &ArgMatches, name: &str, ) -> Result>, Box> { - matches.try_contains_id(name)?; - Ok(keypairs_of(matches, name)) + Ok(matches.try_get_many::(name)?.map(|values| { + values + .filter_map(|value| { + if value == ASK_KEYWORD { + let skip_validation = matches + .try_contains_id(SKIP_SEED_PHRASE_VALIDATION_ARG.name) + .unwrap(); + keypair_from_seed_phrase(name, skip_validation, true, None, true).ok() + } else { + read_keypair_file(value).ok() + } + }) + .collect() + })) } // Return a `Result` wrapped pubkey for an argument that can itself be parsed into a pubkey, @@ -254,16 +274,28 @@ pub fn try_pubkey_of( matches: &ArgMatches, name: &str, ) -> Result, Box> { - matches.try_contains_id(name)?; - Ok(pubkey_of(matches, name)) + if let Some(pubkey) = matches.try_get_one::(name)? { + Ok(Some(*pubkey)) + } else { + Ok(try_keypair_of(matches, name)?.map(|keypair| keypair.pubkey())) + } } pub fn try_pubkeys_of( matches: &ArgMatches, name: &str, ) -> Result>, Box> { - matches.try_contains_id(name)?; - Ok(pubkeys_of(matches, name)) + Ok(matches.try_get_many::(name)?.map(|values| { + values + .map(|value| { + value.parse::().unwrap_or_else(|_| { + read_keypair_file(value) + .expect("read_keypair_file failed") + .pubkey() + }) + }) + .collect() + })) } // Return pubkey/signature pairs for a string of the form pubkey=signature @@ -380,6 +412,7 @@ impl FromStr for PubkeySignature { mod tests { use { super::*, + crate::input_parsers::{keypair_of, pubkey_of, pubkeys_of}, assert_matches::assert_matches, clap::{Arg, ArgAction, Command}, solana_remote_wallet::locator::Manufacturer, From 2b556a24ea76520ac10fbdc2942bedc7a2216d1a Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 19 Mar 2024 17:44:00 +0900 Subject: [PATCH 10/16] deprecate `pubkeys_sigs_of` --- clap-v3-utils/src/input_parsers/mod.rs | 3 +-- clap-v3-utils/src/input_parsers/signer.rs | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/clap-v3-utils/src/input_parsers/mod.rs b/clap-v3-utils/src/input_parsers/mod.rs index 4c98b3347fa421..ebd7173050c7a4 100644 --- a/clap-v3-utils/src/input_parsers/mod.rs +++ b/clap-v3-utils/src/input_parsers/mod.rs @@ -22,8 +22,7 @@ pub mod signer; note = "Please use the functions in `solana_clap_v3_utils::input_parsers::signer` directly instead" )] pub use signer::{ - pubkey_of_signer, pubkeys_of_multiple_signers, pubkeys_sigs_of, resolve_signer, signer_of, - STDOUT_OUTFILE_TOKEN, + pubkey_of_signer, pubkeys_of_multiple_signers, resolve_signer, signer_of, STDOUT_OUTFILE_TOKEN, }; // Return parsed values from matches at `name` diff --git a/clap-v3-utils/src/input_parsers/signer.rs b/clap-v3-utils/src/input_parsers/signer.rs index 36bdba9c13ecb3..fdfd3008455ecb 100644 --- a/clap-v3-utils/src/input_parsers/signer.rs +++ b/clap-v3-utils/src/input_parsers/signer.rs @@ -299,6 +299,8 @@ pub fn try_pubkeys_of( } // Return pubkey/signature pairs for a string of the form pubkey=signature +#[deprecated(since = "1.19.0", note = "Please use `try_pubkeys_sigs_of` instead")] +#[allow(deprecated)] pub fn pubkeys_sigs_of(matches: &ArgMatches, name: &str) -> Option> { matches.values_of(name).map(|values| { values @@ -318,8 +320,16 @@ pub fn try_pubkeys_sigs_of( matches: &ArgMatches, name: &str, ) -> Result>, Box> { - matches.try_contains_id(name)?; - Ok(pubkeys_sigs_of(matches, name)) + Ok(matches.try_get_many::(name)?.map(|values| { + values + .map(|pubkey_signer_string| { + let mut signer = pubkey_signer_string.split('='); + let key = Pubkey::from_str(signer.next().unwrap()).unwrap(); + let sig = Signature::from_str(signer.next().unwrap()).unwrap(); + (key, sig) + }) + .collect() + })) } // Return a signer from matches at `name` From e0b98e5cf762a3cbd5ac40fa453d771e71e66cdd Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 19 Mar 2024 17:44:11 +0900 Subject: [PATCH 11/16] allow deprecated on tests --- clap-v3-utils/src/input_parsers/mod.rs | 1 + clap-v3-utils/src/input_parsers/signer.rs | 1 + clap-v3-utils/src/keypair.rs | 1 + 3 files changed, 3 insertions(+) diff --git a/clap-v3-utils/src/input_parsers/mod.rs b/clap-v3-utils/src/input_parsers/mod.rs index ebd7173050c7a4..09a34689dde2ad 100644 --- a/clap-v3-utils/src/input_parsers/mod.rs +++ b/clap-v3-utils/src/input_parsers/mod.rs @@ -339,6 +339,7 @@ pub fn pubkeys_of(matches: &ArgMatches, name: &str) -> Option> { }) } +#[allow(deprecated)] #[cfg(test)] mod tests { use { diff --git a/clap-v3-utils/src/input_parsers/signer.rs b/clap-v3-utils/src/input_parsers/signer.rs index fdfd3008455ecb..addd1cefd29db9 100644 --- a/clap-v3-utils/src/input_parsers/signer.rs +++ b/clap-v3-utils/src/input_parsers/signer.rs @@ -418,6 +418,7 @@ impl FromStr for PubkeySignature { } } +#[allow(deprecated)] #[cfg(test)] mod tests { use { diff --git a/clap-v3-utils/src/keypair.rs b/clap-v3-utils/src/keypair.rs index 7e41b3c82fbbb3..c140f9573ba38d 100644 --- a/clap-v3-utils/src/keypair.rs +++ b/clap-v3-utils/src/keypair.rs @@ -1257,6 +1257,7 @@ mod tests { } #[test] + #[allow(deprecated)] fn signer_from_path_with_file() -> Result<(), Box> { let dir = TempDir::new()?; let dir = dir.path(); From 14c44ad7fd55fe885e4ceeff4a32f4aa3508e25d Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 19 Mar 2024 17:44:27 +0900 Subject: [PATCH 12/16] remove `deprecation` feature from clap-v3-utils --- clap-v3-utils/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clap-v3-utils/Cargo.toml b/clap-v3-utils/Cargo.toml index 3a1b3a06726c1b..85cc2092ec07a9 100644 --- a/clap-v3-utils/Cargo.toml +++ b/clap-v3-utils/Cargo.toml @@ -11,7 +11,7 @@ edition = { workspace = true } [dependencies] chrono = { workspace = true, features = ["default"] } -clap = { version = "3.2.23", features = ["cargo", "deprecated"] } +clap = { version = "3.2.23", features = ["cargo"] } rpassword = { workspace = true } solana-remote-wallet = { workspace = true } solana-sdk = { workspace = true } From 769afef0498af63c0c24fce2106f91cb61e1e9b0 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 19 Mar 2024 17:44:36 +0900 Subject: [PATCH 13/16] re-export `pubkeys_sigs_of` --- clap-v3-utils/src/input_parsers/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/clap-v3-utils/src/input_parsers/mod.rs b/clap-v3-utils/src/input_parsers/mod.rs index 09a34689dde2ad..722cab903aafb4 100644 --- a/clap-v3-utils/src/input_parsers/mod.rs +++ b/clap-v3-utils/src/input_parsers/mod.rs @@ -21,8 +21,10 @@ pub mod signer; since = "1.17.0", note = "Please use the functions in `solana_clap_v3_utils::input_parsers::signer` directly instead" )] +#[allow(deprecated)] pub use signer::{ - pubkey_of_signer, pubkeys_of_multiple_signers, resolve_signer, signer_of, STDOUT_OUTFILE_TOKEN, + pubkey_of_signer, pubkeys_of_multiple_signers, pubkeys_sigs_of, resolve_signer, signer_of, + STDOUT_OUTFILE_TOKEN, }; // Return parsed values from matches at `name` From 2b38fec959fc8865d68ee4e9ee65fb972c7f1e3d Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 19 Mar 2024 17:46:34 +0900 Subject: [PATCH 14/16] add helper `extract_keypair` to dedupe `try_keypair_of` and `try_keypairs_of` --- clap-v3-utils/src/input_parsers/signer.rs | 31 +++++++++++------------ 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/clap-v3-utils/src/input_parsers/signer.rs b/clap-v3-utils/src/input_parsers/signer.rs index addd1cefd29db9..c6238a49382690 100644 --- a/clap-v3-utils/src/input_parsers/signer.rs +++ b/clap-v3-utils/src/input_parsers/signer.rs @@ -237,12 +237,7 @@ pub fn try_keypair_of( name: &str, ) -> Result, Box> { if let Some(value) = matches.try_get_one::(name)? { - if value == ASK_KEYWORD { - let skip_validation = matches.try_contains_id(SKIP_SEED_PHRASE_VALIDATION_ARG.name)?; - keypair_from_seed_phrase(name, skip_validation, true, None, true).map(Some) - } else { - read_keypair_file(value).map(Some) - } + extract_keypair(matches, name, value) } else { Ok(None) } @@ -254,20 +249,24 @@ pub fn try_keypairs_of( ) -> Result>, Box> { Ok(matches.try_get_many::(name)?.map(|values| { values - .filter_map(|value| { - if value == ASK_KEYWORD { - let skip_validation = matches - .try_contains_id(SKIP_SEED_PHRASE_VALIDATION_ARG.name) - .unwrap(); - keypair_from_seed_phrase(name, skip_validation, true, None, true).ok() - } else { - read_keypair_file(value).ok() - } - }) + .filter_map(|value| extract_keypair(matches, name, value).ok().flatten()) .collect() })) } +fn extract_keypair( + matches: &ArgMatches, + name: &str, + path: &str, +) -> Result, Box> { + if path == ASK_KEYWORD { + let skip_validation = matches.try_contains_id(SKIP_SEED_PHRASE_VALIDATION_ARG.name)?; + keypair_from_seed_phrase(name, skip_validation, true, None, true).map(Some) + } else { + read_keypair_file(path).map(Some) + } +} + // Return a `Result` wrapped pubkey for an argument that can itself be parsed into a pubkey, // or is a filename that can be read as a keypair pub fn try_pubkey_of( From d5e80c9f13cff9b98a7c8054b804010eeead4034 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 19 Mar 2024 17:48:08 +0900 Subject: [PATCH 15/16] remove unwraps and expects --- clap-v3-utils/src/input_parsers/signer.rs | 44 ++++++++++++----------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/clap-v3-utils/src/input_parsers/signer.rs b/clap-v3-utils/src/input_parsers/signer.rs index c6238a49382690..72f989be7cfb12 100644 --- a/clap-v3-utils/src/input_parsers/signer.rs +++ b/clap-v3-utils/src/input_parsers/signer.rs @@ -284,17 +284,15 @@ pub fn try_pubkeys_of( matches: &ArgMatches, name: &str, ) -> Result>, Box> { - Ok(matches.try_get_many::(name)?.map(|values| { - values - .map(|value| { - value.parse::().unwrap_or_else(|_| { - read_keypair_file(value) - .expect("read_keypair_file failed") - .pubkey() - }) - }) - .collect() - })) + if let Some(pubkey_strings) = matches.try_get_many::(name)? { + let mut pubkeys = Vec::with_capacity(pubkey_strings.len()); + for pubkey_string in pubkey_strings { + pubkeys.push(pubkey_string.parse::()?); + } + Ok(Some(pubkeys)) + } else { + Ok(None) + } } // Return pubkey/signature pairs for a string of the form pubkey=signature @@ -319,16 +317,20 @@ pub fn try_pubkeys_sigs_of( matches: &ArgMatches, name: &str, ) -> Result>, Box> { - Ok(matches.try_get_many::(name)?.map(|values| { - values - .map(|pubkey_signer_string| { - let mut signer = pubkey_signer_string.split('='); - let key = Pubkey::from_str(signer.next().unwrap()).unwrap(); - let sig = Signature::from_str(signer.next().unwrap()).unwrap(); - (key, sig) - }) - .collect() - })) + if let Some(pubkey_signer_strings) = matches.try_get_many::(name)? { + let mut pubkey_sig_pairs = Vec::with_capacity(pubkey_signer_strings.len()); + for pubkey_signer_string in pubkey_signer_strings { + let (pubkey_string, sig_string) = pubkey_signer_string + .split_once('=') + .ok_or("failed to parse `pubkey=signature` pair")?; + let pubkey = Pubkey::from_str(pubkey_string)?; + let sig = Signature::from_str(sig_string)?; + pubkey_sig_pairs.push((pubkey, sig)); + } + Ok(Some(pubkey_sig_pairs)) + } else { + Ok(None) + } } // Return a signer from matches at `name` From f9b44747d91b1d75c54360f94530a642069983e9 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Thu, 21 Mar 2024 09:26:36 +0900 Subject: [PATCH 16/16] bump deprecation version --- clap-v3-utils/src/input_parsers/mod.rs | 18 +++++++++--------- clap-v3-utils/src/input_parsers/signer.rs | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/clap-v3-utils/src/input_parsers/mod.rs b/clap-v3-utils/src/input_parsers/mod.rs index 722cab903aafb4..5aa269274253d0 100644 --- a/clap-v3-utils/src/input_parsers/mod.rs +++ b/clap-v3-utils/src/input_parsers/mod.rs @@ -29,7 +29,7 @@ pub use signer::{ // Return parsed values from matches at `name` #[deprecated( - since = "1.19.0", + since = "2.0.0", note = "Please use the functions `ArgMatches::get_many` or `ArgMatches::try_get_many` instead" )] #[allow(deprecated)] @@ -45,7 +45,7 @@ where // Return a parsed value from matches at `name` #[deprecated( - since = "1.19.0", + since = "2.0.0", note = "Please use the functions `ArgMatches::get_one` or `ArgMatches::try_get_one` instead" )] #[allow(deprecated)] @@ -60,7 +60,7 @@ where } #[deprecated( - since = "1.19.0", + since = "2.0.0", note = "Please use `ArgMatches::get_one::(...)` instead" )] #[allow(deprecated)] @@ -85,7 +85,7 @@ pub fn lamports_of_sol(matches: &ArgMatches, name: &str) -> Option { } #[deprecated( - since = "1.19.0", + since = "2.0.0", note = "Please use `ArgMatches::get_one::(...)` instead" )] #[allow(deprecated)] @@ -94,7 +94,7 @@ pub fn cluster_type_of(matches: &ArgMatches, name: &str) -> Option } #[deprecated( - since = "1.19.0", + since = "2.0.0", note = "Please use `ArgMatches::get_one::(...)` instead" )] #[allow(deprecated)] @@ -274,7 +274,7 @@ pub fn parse_derived_address_seed(arg: &str) -> Result { // Return the keypair for an argument with filename `name` or None if not present. #[deprecated( - since = "1.19.0", + since = "2.0.0", note = "Please use `input_parsers::signer::try_keypair_of` instead" )] #[allow(deprecated)] @@ -292,7 +292,7 @@ pub fn keypair_of(matches: &ArgMatches, name: &str) -> Option { } #[deprecated( - since = "1.19.0", + since = "2.0.0", note = "Please use `input_parsers::signer::try_keypairs_of` instead" )] #[allow(deprecated)] @@ -314,7 +314,7 @@ pub fn keypairs_of(matches: &ArgMatches, name: &str) -> Option> { // Return a pubkey for an argument that can itself be parsed into a pubkey, // or is a filename that can be read as a keypair #[deprecated( - since = "1.19.0", + since = "2.0.0", note = "Please use `input_parsers::signer::try_pubkey_of` instead" )] #[allow(deprecated)] @@ -323,7 +323,7 @@ pub fn pubkey_of(matches: &ArgMatches, name: &str) -> Option { } #[deprecated( - since = "1.19.0", + since = "2.0.0", note = "Please use `input_parsers::signer::try_pubkeys_of` instead" )] #[allow(deprecated)] diff --git a/clap-v3-utils/src/input_parsers/signer.rs b/clap-v3-utils/src/input_parsers/signer.rs index 72f989be7cfb12..0580799a6bf675 100644 --- a/clap-v3-utils/src/input_parsers/signer.rs +++ b/clap-v3-utils/src/input_parsers/signer.rs @@ -296,7 +296,7 @@ pub fn try_pubkeys_of( } // Return pubkey/signature pairs for a string of the form pubkey=signature -#[deprecated(since = "1.19.0", note = "Please use `try_pubkeys_sigs_of` instead")] +#[deprecated(since = "2.0.0", note = "Please use `try_pubkeys_sigs_of` instead")] #[allow(deprecated)] pub fn pubkeys_sigs_of(matches: &ArgMatches, name: &str) -> Option> { matches.values_of(name).map(|values| {