added separate function for checksum

This commit is contained in:
Alexandre Van de Sande 2016-02-22 15:17:32 -03:00
parent 9f47fa5535
commit cd1974b883
2 changed files with 27 additions and 10 deletions

View File

@ -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,

View File

@ -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;