From cd1974b883ff6246d0f0868b5b8b1363f3af5e0f Mon Sep 17 00:00:00 2001 From: Alexandre Van de Sande Date: Mon, 22 Feb 2016 15:17:32 -0300 Subject: [PATCH] added separate function for checksum --- lib/utils/utils.js | 36 ++++++++++++++++++++++++++---------- lib/web3.js | 1 + 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/lib/utils/utils.js b/lib/utils/utils.js index b2ab243..7dc9b22 100644 --- a/lib/utils/utils.js +++ b/lib/utils/utils.js @@ -405,20 +405,35 @@ var isAddress = function (address) { return true; } else { // Otherwise check each case - address = address.replace('0x',''); - var addressHash = web3.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) > 8 && address[i].toUpperCase() != address[i]) || (parseInt(addressHash[i], 16) <= 8 && address[i].toLowerCase() != address[i])) { - return false; - } - } - return true; + return isChecksumAddress(address); } }; + +/** + * Checks if the given string is a checksummed address + * + * @method isChecksumAddress + * @param {String} address the given HEX adress + * @return {Boolean} +*/ +var isChecksumAddress = function (address) { + // Check each case + address = address.replace('0x',''); + var addressHash = web3.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) > 8 && address[i].toUpperCase() != address[i]) || (parseInt(addressHash[i], 16) <= 8 && address[i].toLowerCase() != address[i])) { + return false; + } + } + return true; +}; + + + /** * Makes a checksum address * @@ -565,6 +580,7 @@ module.exports = { isBigNumber: isBigNumber, isStrictAddress: isStrictAddress, isAddress: isAddress, + isChecksumAddress: isChecksumAddress, toChecksumAddress: toChecksumAddress, isFunction: isFunction, isString: isString, diff --git a/lib/web3.js b/lib/web3.js index 3d814db..9300060 100644 --- a/lib/web3.js +++ b/lib/web3.js @@ -91,6 +91,7 @@ Web3.prototype.toBigNumber = utils.toBigNumber; Web3.prototype.toWei = utils.toWei; Web3.prototype.fromWei = utils.fromWei; Web3.prototype.isAddress = utils.isAddress; +Web3.prototype.isChecksumAddress = utils.isChecksumAddress; Web3.prototype.toChecksumAddress = utils.toChecksumAddress; Web3.prototype.isIBAN = utils.isIBAN; Web3.prototype.sha3 = sha3;