Crafting Regex for Smart Contract Address Validation in a Frontend DApp - Handling Edge Cases
I keep running into I'm performance testing and I'm working on a personal project and Currently developing a decentralized application where users need to enter Ethereum addresses for various functionalities... The challenge lies in ensuring that the addresses entered are valid before any transactions proceed. The official Ethereum address format requires that it starts with '0x' followed by 40 hexadecimal characters. I've attempted a regex pattern like this: ```regex /^0x[a-fA-F0-9]{40}$/ ``` This works for most cases, but I'm concerned about potential edge cases, such as an address that appears valid but is not on the blockchain. To enhance the user experience, I want to ensure that the application not only validates the format but also considers checksum validation. I've come across checksum mechanisms that utilize the EIP-55 standard, but implementing it alongside regex is proving tricky. My goal is to allow only valid addresses and potentially provide feedback if the address does not exist on the network. Here's an example of how I tried integrating the checksum validation: ```javascript function isValidEthereumAddress(address) { const pattern = /^0x[a-fA-F0-9]{40}$/; if (!pattern.test(address)) { return false; } // Checksum validation logic here return true; } ``` Iām uncertain how to structure the checksum part within my existing validation logic. Would it be better to perform this validation in the frontend, or should I handle it on the smart contract side as well? Any suggestions on improving this regex or the overall validation approach would be greatly appreciated. Is there a better approach? I'm working on a API that needs to handle this. I'd really appreciate any guidance on this. Thanks in advance! For context: I'm using Javascript on Linux.