Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve address regex #7037

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions crates/chisel/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ pub static CHISEL_CHAR: &str = "⚒️";
static COMMENT_RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^\s*(?://.*\s*$)|(/*[\s\S]*?\*/\s*$)").unwrap());

/// Matches Ethereum addresses
static ADDRESS_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"0x[a-fA-F0-9]{40}").unwrap());
/// Matches Ethereum addresses that are not strings
static ADDRESS_RE: Lazy<Regex> = Lazy::new(|| {
Regex::new(r#"(?m)(([^"']\s*)|^)(?P<address>0x[a-fA-F0-9]{40})((\s*[^"'\w])|$)"#).unwrap()
});

/// Chisel input dispatcher
#[derive(Debug)]
Expand Down Expand Up @@ -786,9 +788,10 @@ impl ChiselDispatcher {
// If there is an address (or multiple addresses) in the input, ensure that they are
// encoded with a valid checksum per EIP-55.
let mut heap_input = input.to_string();
ADDRESS_RE.find_iter(input).for_each(|m| {
ADDRESS_RE.captures_iter(input).for_each(|m| {
// Convert the match to a string slice
let match_str = m.as_str();
let match_str = m.name("address").expect("exists").as_str();

// We can always safely unwrap here due to the regex matching.
let addr: Address = match_str.parse().expect("Valid address regex");
// Replace all occurrences of the address with a checksummed version
Expand Down Expand Up @@ -967,4 +970,16 @@ mod tests {
assert!(COMMENT_RE.is_match(" \t\n /* block \n \t comment */\n"));
assert!(!COMMENT_RE.is_match("/* block \n \t comment */\nwith \tother"));
}

#[test]
fn test_address_regex() {
assert!(ADDRESS_RE.is_match("0xe5f3aF50FE5d0bF402a3C6F55ccC47d4307922d4"));
assert!(ADDRESS_RE.is_match(" 0xe5f3aF50FE5d0bF402a3C6F55ccC47d4307922d4 "));
assert!(ADDRESS_RE.is_match("0xe5f3aF50FE5d0bF402a3C6F55ccC47d4307922d4,"));
assert!(ADDRESS_RE.is_match("(0xe5f3aF50FE5d0bF402a3C6F55ccC47d4307922d4)"));
assert!(!ADDRESS_RE.is_match("0xe5f3aF50FE5d0bF402a3C6F55ccC47d4307922d4aaa"));
assert!(!ADDRESS_RE.is_match("'0xe5f3aF50FE5d0bF402a3C6F55ccC47d4307922d4'"));
assert!(!ADDRESS_RE.is_match("' 0xe5f3aF50FE5d0bF402a3C6F55ccC47d4307922d4'"));
assert!(!ADDRESS_RE.is_match("'0xe5f3aF50FE5d0bF402a3C6F55ccC47d4307922d4'"));
}
}
Loading