Skip to content

Instantly share code, notes, and snippets.

@huantt
Created June 8, 2022 02:54
Show Gist options
  • Select an option

  • Save huantt/eec35b456f8a8b01eb983a3ec6240268 to your computer and use it in GitHub Desktop.

Select an option

Save huantt/eec35b456f8a8b01eb983a3ec6240268 to your computer and use it in GitHub Desktop.
Validate wallet address
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