From b730a4cef9b6b0f591f9327351a6b30dddb02c75 Mon Sep 17 00:00:00 2001 From: Maksym Kulish Date: Fri, 23 Aug 2024 17:39:19 +0300 Subject: [PATCH] Unify regex usage --- src/bls_to_execution_change/mod.rs | 5 +---- src/lib.rs | 9 +++++++++ src/utils.rs | 10 ++++++++++ src/validators.rs | 15 +++++---------- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/bls_to_execution_change/mod.rs b/src/bls_to_execution_change/mod.rs index eb06438..c605e15 100644 --- a/src/bls_to_execution_change/mod.rs +++ b/src/bls_to_execution_change/mod.rs @@ -1,6 +1,5 @@ pub(crate) mod operator; -use regex::Regex; use types::{Address, BlsToExecutionChange}; use crate::key_material::VotingKeyMaterial; @@ -13,9 +12,7 @@ pub fn bls_execution_change_from_mnemonic( ) -> (BlsToExecutionChange, VotingKeyMaterial) { let (seed, _) = crate::seed::get_eth2_seed(Some(mnemonic_phrase)); - let execution_addr_regex: Regex = Regex::new(r"^(0x[a-fA-F0-9]{40})$").unwrap(); - - if !execution_addr_regex.is_match(execution_address) { + if !crate::utils::EXECUTION_ADDR_REGEX.is_match(execution_address) { panic!( "Invalid execution address: Please pass in a valid execution address with the correct format" ); diff --git a/src/lib.rs b/src/lib.rs index e61b504..4282e67 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,3 +13,12 @@ pub mod voluntary_exit; pub use deposit::DepositError; pub use validators::*; + +use regex::Regex; + +lazy_static::lazy_static! { + pub static ref EXECUTION_ADDR_REGEX: Regex = Regex::new(r"^(0x[a-fA-F0-9]{40})$").unwrap(); + pub static ref EXECUTION_CREDS_REGEX: Regex = + Regex::new(r"^(0x01[0]{22}[a-fA-F0-9]{40})$").unwrap(); + pub static ref BLS_CREDS_REGEX: Regex = Regex::new(r"^(0x00[a-fA-F0-9]{62})$").unwrap(); +} diff --git a/src/utils.rs b/src/utils.rs index 719cb56..c62a629 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,6 @@ use eth2_keystore::json_keystore::{HexBytes, Kdf, Pbkdf2, Prf, Scrypt}; use eth2_keystore::{DKLEN, SALT_SIZE}; +use regex::Regex; use ssz::Encode; use types::{Hash256, PublicKeyBytes}; @@ -45,3 +46,12 @@ pub fn withdrawal_creds_from_pk(withdrawal_pk: &PublicKeyBytes) -> String { let credentials_hash = Hash256::from_slice(&withdrawal_creds); hex::encode(credentials_hash.as_bytes()) } + +// Various regexes used for input validation +lazy_static::lazy_static! { + /// see format of execution address: https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/validator.md#eth1_address_withdrawal_prefix + pub static ref EXECUTION_ADDR_REGEX: Regex = Regex::new(r"^(0x[a-fA-F0-9]{40})$").unwrap(); + pub static ref EXECUTION_CREDS_REGEX: Regex = + Regex::new(r"^(0x01[0]{22}[a-fA-F0-9]{40})$").unwrap(); + pub static ref BLS_CREDS_REGEX: Regex = Regex::new(r"^(0x00[a-fA-F0-9]{62})$").unwrap(); +} diff --git a/src/validators.rs b/src/validators.rs index 5b8f2eb..32a7822 100644 --- a/src/validators.rs +++ b/src/validators.rs @@ -8,7 +8,6 @@ use crate::utils::get_withdrawal_credentials; use bip39::{Mnemonic, Seed as Bip39Seed}; use eth2_keystore::Keystore; use eth2_wallet::json_wallet::Kdf; -use regex::Regex; use serde::{Deserialize, Serialize}; use tree_hash::TreeHash; use types::{ @@ -298,18 +297,14 @@ fn set_withdrawal_credentials( ) -> Result, DepositError> { let withdrawal_credentials = match existing_withdrawal_credentials { Some(creds) => { - let execution_addr_regex: Regex = Regex::new(r"^(0x[a-fA-F0-9]{40})$").unwrap(); - let execution_creds_regex: Regex = - Regex::new(r"^(0x01[0]{22}[a-fA-F0-9]{40})$").unwrap(); - let bls_creds_regex: Regex = Regex::new(r"^(0x00[a-fA-F0-9]{62})$").unwrap(); - - let withdrawal_credentials = if execution_addr_regex.is_match(creds.as_str()) { - // see format of execution address: https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/validator.md#eth1_address_withdrawal_prefix + let withdrawal_credentials = if crate::utils::EXECUTION_ADDR_REGEX + .is_match(creds.as_str()) + { let mut formatted_creds = ETH1_CREDENTIALS_PREFIX.to_vec(); formatted_creds.extend_from_slice(&creds.as_bytes()[2..]); formatted_creds - } else if execution_creds_regex.is_match(creds.as_str()) - || bls_creds_regex.is_match(creds.as_str()) + } else if crate::utils::EXECUTION_CREDS_REGEX.is_match(creds.as_str()) + || crate::utils::BLS_CREDS_REGEX.is_match(creds.as_str()) { // see format of execution & bls credentials https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/validator.md#bls_withdrawal_prefix let formatted_creds = creds.as_bytes()[2..].to_vec();