All files / src/utils address.js

52.94% Statements 9/17
33.33% Branches 4/12
50% Functions 1/2
56.25% Lines 9/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60        1x                   15x   9x 6x   6x                                                         1x 1x 1x 1x                
/**
 * Address validation functions
 * source: https://ethereum.stackexchange.com/a/1379/7804
 */
const Address = {
 
    /**
     * Checks if the given string is an address
     *
     * @method isAddress
     * @param {String} address the given HEX adress
     * @return {Boolean}
     */
    isHexAddress(address) {
        if (!/^(0x)?[0-9a-f]{42}$/i.test(address)) {
            // check if it has the basic requirements of an address
            return false
        } else Eif (/^(0x)?[0-9a-f]{42}$/.test(address) || /^(0x)?[0-9A-F]{42}$/.test(address)) {
            // If it's all small caps or all all caps, return true
            return true
        } else {
            // Otherwise check each case
            return this.isChecksumHexAddress(address)
        }
    },
 
    /**
     * Checks if the given string is a checksummed address
     *
     * @method isChecksumAddress
     * @param {String} address the given HEX adress
     * @return {Boolean}
     */
    isChecksumHexAddress(address) {
        // Check each case
        address = address.replace("0x", "")
        var addressHash = sha3(address.toLowerCase())
        for (var i = 0; i < 40; i++) {
            // the nth letter should be uppercase if the nth digit of casemap is 1
            if ((parseInt(addressHash[i], 16) > 7 && address[i].toUpperCase() !== address[i])
                || (parseInt(addressHash[i], 16) <= 7 && address[i].toLowerCase() !== address[i])) {
                return false
            }
        }
        return true
    }
 
}
const ADDRESS_SIZE = 34;
const ADDRESS_PREFIX = "41";
const ADDRESS_PREFIX_BYTE = 0x41;
const ADDRESS_PREFIX_REGEX = /^(41)/;
export {
    ADDRESS_SIZE,
    ADDRESS_PREFIX,
    ADDRESS_PREFIX_BYTE,
    ADDRESS_PREFIX_REGEX,
    Address
}