Blog (2024)

Validating ethereum address:

Purpose: In an application where user enters ethereum address, If we have no check or validation for address then there may be a chance of amount loss. So before entering any address one must ensure that the address should be valid one. In application level we can ensure this using two level of checks.

1: By simply creating a regular expression: The expression is a a validator for hexadecimal checking and length checking

following method ensures that given address should have length 40(excluding 0x), and the address must begin with 0x prefix. If following conditions are satisfied then it will return true else will return false.

 public static boolean validAddress(String ethereumAddress) { String regex = "^0x[0-9a-f]{40}$"; if (ethereumAddress.matches(regex)) { return true; } return false; }

2- By performing checksum: Before explaning checksum let's check how ethereum addresses are generated

Blog (1)

reference (Ethereum yellow paper)

A noobie may wonder after analizing ethereum address that sometime it appears in lowercase and sometime in mixedcase, Actually ethereum address displayed in mixed case after checksum. You can verify this by following:

>web3.toChecksumAddress("0xa8546f98cae1da1ddb72d7d6b179df93214f2e2c") //It will return the checksum address
"0xa8546f98CAE1dA1dDB72D7D6B179df93214F2E2c"

How checksum actully works:

1- Remove the 0x prefix of the hex address.
2- Convert the rest of the address to lower-case (Just to ensure that it should not be in Uppercase or Mixedcase)
3- Using Keccak 256 algorithm, Compare obtained hash with the original hex address
4- Change the ith letter of hexadecimal address to uppercase if the ith bit of the obtained hash is greater than 7
5- Otherwise, the ith letter of the hexadecimal address should be lowercased.
6- If the address obtained matches with original, then the address is checksummed. Otherwise not.


 public static boolean checksumAddress(String ethereumAddress) { // to fetch the part after 0x String subAddr = ethereumAddress.substring(2); // Make it to original lower case address String subAddrLower = subAddr.toLowerCase(); // Create a SHA3256 hash (Keccak-256) SHA3.DigestSHA3 digestSHA3 = new SHA3.Digest256(); digestSHA3.update(subAddrLower.getBytes()); String digestMessage = Hex.toHexString(digestSHA3.digest()); /* * Check each letter is upper case or not if it is upper case then the * corresponding binary position of the hashed address should be 1 i.e the * message digest letter should be getter than 7 as 7 is the last Hex digit * which starts with 0 in binary rest of all 8 to f starts with 1 (i.e 7: 0111, 8: 1000) */ for (short i = 0; i < subAddr.length(); i++) { if (subAddr.charAt(i) >= 65 && subAddr.charAt(i) <= 91) { String ss = Character.toString(digestMessage.charAt(i)); if (!(Integer.parseInt(ss, 16) > 7)) { return false; } } } return true; }

Note: above two methods are just to validate whether given address is valid or not. Moreover, this can't be predicted that the address belongs to particular user or not.

Blog (2024)
Top Articles
Latest Posts
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 5657

Rating: 5 / 5 (70 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.