Ethereum addresses are essential for sending and receiving Ether (ETH) and other Ethereum-based tokens. These addresses come in two primary formats: standard hexadecimal and checksummed (mixed-case). Understanding how to validate them ensures secure transactions and avoids errors. Below, we explore Ethereum address validation using regular expressions (regex), covering formats, examples, and implementation.
Ethereum Address Formats
1. Standard Hexadecimal Format
- Length: 40 hexadecimal characters (0-9, a-f).
- Prefix: Optional "0x" to denote hexadecimal.
- Characters: Lowercase letters (a-f), numbers (0-9).
- Exclusions: No visual ambiguity characters (e.g., O, I, l, or 0).
2. Checksummed Format (Mixed Case)
- Length: 40 hexadecimal characters (0-9, A-F, or a-f).
- Prefix: Optional "0x."
- Case Sensitivity: Mixed uppercase/lowercase letters for error reduction.
- Exclusions: Same as standard format.
Validation Examples
| Input | Output | Explanation |
|---|---|---|
0x742d35Cc6634C0532925a3b844Bc454e4438f44e | True | Valid 40-character address with "0x" prefix. |
0x742d35cc6634c0532925a3b844bc454e4438f44e | True | Valid lowercase address. |
742d35Cc6634C0532925a3b844Bc454e4438f44e | True | Valid without "0x" prefix. |
0x123 | False | Invalid: Too short (requires 40 chars). |
0123456789012345678901234567890123456789 | True | Valid 40-character hexadecimal (no "0x"). |
Validation Approach
Regex Pattern:
^(0x)?[0-9a-fA-F]{40}$^and$: Match start-to-end.(0x)?: Optional "0x" prefix.[0-9a-fA-F]{40}: Exactly 40 hexadecimal chars.
Steps:
- Accept input as a string.
- Match against the regex.
- Return
Trueif valid;Falseotherwise.
Code Implementation
import re
def validate_ethereum_address(address):
pattern = r'^(0x)?[0-9a-fA-F]{40}$'
return bool(re.fullmatch(pattern, address))
# Test Cases
print(validate_ethereum_address("0x742d35Cc6634C0532925a3b844Bc454e4438f44e")) # True
print(validate_ethereum_address("742d35Cc6634C0532925a3b844Bc454e4438f44e")) # True
print(validate_ethereum_address("0x123")) # False
print(validate_ethereum_address("0123456789012345678901234567890123456789")) # TrueOutput:
True
True
False
True
FalseComplexity:
- Time: O(N) (linear scan).
- Space: O(1) (minimal memory usage).
FAQs
1. Is "0x" mandatory in Ethereum addresses?
No. While commonly used, omitting "0x" doesn’t invalidate the address if the 40-character hex is correct.
2. Why use mixed-case (checksummed) addresses?
Mixed-case addresses reduce typo risks by incorporating case-sensitive checksums for validation.
3. Can regex validate checksummed addresses?
Basic regex checks format, but full checksum validation requires additional algorithmic steps (e.g., EIP-55).
👉 Learn more about Ethereum security
Key Takeaways
- Use regex for basic format validation.
- Checksummed addresses need extra checks beyond regex.
- Always verify addresses before sending transactions.
By mastering these techniques, you’ll ensure seamless and secure Ethereum transactions.