Skip to content

Commit c2fb7bd

Browse files
authored
docs(cast): improve vanity help naming (#8306)
1 parent 8ca3b68 commit c2fb7bd

File tree

1 file changed

+21
-41
lines changed

1 file changed

+21
-41
lines changed

crates/cast/bin/cmd/wallet/vanity.rs

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use alloy_primitives::{hex, Address};
22
use alloy_signer::{k256::ecdsa::SigningKey, utils::secret_key_to_address};
33
use alloy_signer_local::PrivateKeySigner;
4-
use clap::{builder::TypedValueParser, Parser};
4+
use clap::Parser;
55
use eyre::Result;
6+
use itertools::Either;
67
use rayon::iter::{self, ParallelIterator};
78
use regex::Regex;
89
use serde::{Deserialize, Serialize};
@@ -19,16 +20,11 @@ pub type GeneratedWallet = (SigningKey, Address);
1920
#[derive(Clone, Debug, Parser)]
2021
pub struct VanityArgs {
2122
/// Prefix regex pattern or hex string.
22-
#[arg(
23-
long,
24-
required_unless_present = "ends_with",
25-
value_parser = HexAddressValidator,
26-
value_name = "HEX"
27-
)]
23+
#[arg(long, value_name = "PATTERN", required_unless_present = "ends_with")]
2824
pub starts_with: Option<String>,
2925

3026
/// Suffix regex pattern or hex string.
31-
#[arg(long, value_parser = HexAddressValidator, value_name = "HEX")]
27+
#[arg(long, value_name = "PATTERN")]
3228
pub ends_with: Option<String>,
3329

3430
// 2^64-1 is max possible nonce per [eip-2681](https://eips.ethereum.org/EIPS/eip-2681).
@@ -74,24 +70,22 @@ impl WalletData {
7470
impl VanityArgs {
7571
pub fn run(self) -> Result<PrivateKeySigner> {
7672
let Self { starts_with, ends_with, nonce, save_path } = self;
73+
7774
let mut left_exact_hex = None;
7875
let mut left_regex = None;
79-
let mut right_exact_hex = None;
80-
let mut right_regex = None;
81-
8276
if let Some(prefix) = starts_with {
83-
if let Ok(decoded) = hex::decode(&prefix) {
84-
left_exact_hex = Some(decoded)
85-
} else {
86-
left_regex = Some(Regex::new(&format!(r"^{prefix}"))?);
77+
match parse_pattern(&prefix, true)? {
78+
Either::Left(left) => left_exact_hex = Some(left),
79+
Either::Right(re) => left_regex = Some(re),
8780
}
8881
}
8982

83+
let mut right_exact_hex = None;
84+
let mut right_regex = None;
9085
if let Some(suffix) = ends_with {
91-
if let Ok(decoded) = hex::decode(&suffix) {
92-
right_exact_hex = Some(decoded)
93-
} else {
94-
right_regex = Some(Regex::new(&format!(r"{suffix}$"))?);
86+
match parse_pattern(&suffix, false)? {
87+
Either::Left(right) => right_exact_hex = Some(right),
88+
Either::Right(re) => right_regex = Some(re),
9589
}
9690
}
9791

@@ -331,29 +325,15 @@ impl VanityMatcher for RegexMatcher {
331325
}
332326
}
333327

334-
/// Parse 40 byte addresses
335-
#[derive(Clone, Copy, Debug, Default)]
336-
pub struct HexAddressValidator;
337-
338-
impl TypedValueParser for HexAddressValidator {
339-
type Value = String;
340-
341-
fn parse_ref(
342-
&self,
343-
_cmd: &clap::Command,
344-
_arg: Option<&clap::Arg>,
345-
value: &std::ffi::OsStr,
346-
) -> Result<Self::Value, clap::Error> {
347-
if value.len() > 40 {
348-
return Err(clap::Error::raw(
349-
clap::error::ErrorKind::InvalidValue,
350-
"vanity patterns length exceeded. cannot be more than 40 characters",
351-
))
328+
fn parse_pattern(pattern: &str, is_start: bool) -> Result<Either<Vec<u8>, Regex>> {
329+
if let Ok(decoded) = hex::decode(pattern) {
330+
if decoded.len() > 20 {
331+
return Err(eyre::eyre!("Hex pattern must be less than 20 bytes"));
352332
}
353-
let value = value.to_str().ok_or_else(|| {
354-
clap::Error::raw(clap::error::ErrorKind::InvalidUtf8, "address must be valid utf8")
355-
})?;
356-
Ok(value.to_string())
333+
Ok(Either::Left(decoded))
334+
} else {
335+
let (prefix, suffix) = if is_start { ("^", "") } else { ("", "$") };
336+
Ok(Either::Right(Regex::new(&format!("{prefix}{pattern}{suffix}"))?))
357337
}
358338
}
359339

0 commit comments

Comments
 (0)