Skip to content

Commit

Permalink
Unify regex usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mksh committed Aug 23, 2024
1 parent 8690bef commit b730a4c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 14 deletions.
5 changes: 1 addition & 4 deletions src/bls_to_execution_change/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub(crate) mod operator;

use regex::Regex;
use types::{Address, BlsToExecutionChange};

use crate::key_material::VotingKeyMaterial;
Expand All @@ -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"
);
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
10 changes: 10 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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();
}
15 changes: 5 additions & 10 deletions src/validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -298,18 +297,14 @@ fn set_withdrawal_credentials(
) -> Result<Vec<u8>, 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();
Expand Down

0 comments on commit b730a4c

Please sign in to comment.