Created
June 8, 2022 02:54
-
-
Save huantt/eec35b456f8a8b01eb983a3ec6240268 to your computer and use it in GitHub Desktop.
Validate wallet address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment