Validating ethereum address

Posted By : Ekesh Bahuguna | 30-Jul-2018

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

   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.
   

 

 

About Author

Author Image
Ekesh Bahuguna

Ekesh is Java Developer. Along with that he is good in linux, c, networking and competitive programming. He love to answer over Quora.

Request for Proposal

Name is required

Comment is required

Sending message..