test addresses when transforming toChecksumAddress

This commit is contained in:
Fabian Vogelsteller 2017-03-10 10:23:02 +01:00
parent 4e303dad86
commit dedc8407b7
No known key found for this signature in database
GPG Key ID: E51EADA77F1A4124
5 changed files with 47 additions and 39 deletions

View File

@ -477,35 +477,8 @@ var isChecksumAddress = function (address) {
return true;
};
/**
* Makes a checksum address
*
* @method toChecksumAddress
* @param {String} address the given HEX adress
* @return {String}
*/
var toChecksumAddress = function (address) {
if (typeof address === 'undefined') return '';
address = address.toLowerCase().replace('0x','');
var addressHash = sha3(address).replace('0x','');
var checksumAddress = '0x';
for (var i = 0; i < address.length; i++ ) {
// If ith character is 9 to f then make it uppercase
if (parseInt(addressHash[i], 16) > 7) {
checksumAddress += address[i].toUpperCase();
} else {
checksumAddress += address[i];
}
}
return checksumAddress;
};
/**
* Transforms given string to valid 20 bytes-length addres with 0x prefix
* Transforms given string to valid 20 bytes-length address with 0x prefix
*
* @method toAddress
* @param {String} address
@ -523,6 +496,39 @@ var toAddress = function (address) {
return '0x' + padLeft(toHex(address).substr(2), 40);
};
/**
* Converts to a checksum address
*
* @method toChecksumAddress
* @param {String} address the given HEX adress
* @return {String}
*/
var toChecksumAddress = function (address) {
if (typeof address === 'undefined') return '';
if(!isAddress(address))
throw new Error('Given address "'+ address +'" is not a valid ethereum address.');
address = address.toLowerCase().replace('0x','');
var addressHash = sha3(address).replace('0x','');
var checksumAddress = '0x';
for (var i = 0; i < address.length; i++ ) {
// If ith character is 9 to f then make it uppercase
if (parseInt(addressHash[i], 16) > 7) {
checksumAddress += address[i].toUpperCase();
} else {
checksumAddress += address[i];
}
}
return checksumAddress;
};
/**
* Returns true if object is BN, otherwise false
*
@ -555,18 +561,20 @@ module.exports = {
_: _,
padLeft: padLeft,
padRight: padRight,
toAddress: toAddress,
toHex: toHex,
toNumber: toNumber,
toBN: toBN,
toNumberString: toNumberString,
toNumber: toNumber,
toDecimal: toNumber,
fromNumber: fromNumber,
fromDecimal: fromNumber, // alias
toUtf8: toUtf8,
toAscii: toAscii,
fromUtf8: fromUtf8,
fromAscii: fromAscii,
toWei: toWei,
fromWei: fromWei,
toAddress: toAddress,
toBN: toBN,
isBN: isBN,
isBigNumber: isBigNumber,
isAddress: isAddress,

View File

@ -67,7 +67,7 @@ var blockResultWithTx = {
"blockNumber": "0x15df",
"transactionIndex": "0x1",
"from":"0x407d73d8a49eeb85d32cf465507dd71d507100c1",
"to":"0x85h43d8a49eeb85d32cf465507dd71d507100c1",
"to":"0x85f43d8a49eeb85d32cf465507dd71d507100c1d",
"value":"0x7f110",
"gas": "0x7f110",
"gasPrice":"0x09184e72a000",
@ -100,7 +100,7 @@ var formattedBlockResultWithTx = {
"blockNumber": 5599,
"transactionIndex": 1,
"from":"0x407D73d8a49eeb85D32Cf465507dd71d507100c1",
"to":"0x85h43d8a49EEB85D32CF465507dD71D507100C1",
"to":"0x85F43D8a49eeB85d32Cf465507DD71d507100C1d",
"value": '520464',
"gas": 520464,
"gasPrice": '10000000000000',

View File

@ -9,7 +9,7 @@ var txResult = {
"blockNumber": "0x15df",
"transactionIndex": "0x1",
"from":"0x407d73d8a49eeb85d32cf465507dd71d507100c1",
"to":"0x85h43d8a49eeb85d32cf465507dd71d507100c1",
"to":"0x85f43d8a49eeb85d32cf465507dd71d507100c1d",
"value":"0x7f110",
"gas": "0x7f110",
"gasPrice":"0x09184e72a000",
@ -22,7 +22,7 @@ var formattedTxResult = {
"blockNumber": 5599,
"transactionIndex": 1,
"from":"0x407D73d8a49eeb85D32Cf465507dd71d507100c1", // checksum address
"to":"0x85h43d8a49EEB85D32CF465507dD71D507100C1", // checksum address
"to":"0x85F43D8a49eeB85d32Cf465507DD71d507100C1d", // checksum address
"value": '520464',
"gas": 520464,
"gasPrice": '10000000000000',

View File

@ -10,7 +10,7 @@ var txResult = {
"blockNumber": "0x15df",
"transactionIndex": "0x1",
"from":"0x407d73d8a49eeb85d32cf465507dd71d507100c1",
"to":"0x85h43d8a49eeb85d32cf465507dd71d507100c1",
"to":"0x85f43d8a49eeb85d32cf465507dd71d507100c1d",
"value":"0x7f110",
"gas": "0x7f110",
"gasPrice":"0x09184e72a000",
@ -24,7 +24,7 @@ var formattedTxResult = {
"blockNumber": 5599,
"transactionIndex": 1,
"from":"0x407D73d8a49eeb85D32Cf465507dd71d507100c1", // checksum address
"to":"0x85h43d8a49EEB85D32CF465507dD71D507100C1", // checksum address
"to":"0x85F43D8a49eeB85d32Cf465507DD71d507100C1d", // checksum address
"value": '520464',
"gas": 520464,
"gasPrice": '10000000000000',

View File

@ -67,7 +67,7 @@ var blockResultWithTx = {
"blockNumber": "0x15df",
"transactionIndex": "0x1",
"from":"0x407d73d8a49eeb85d32cf465507dd71d507100c1",
"to":"0x85h43d8a49eeb85d32cf465507dd71d507100c1",
"to":"0x85f43d8a49eeb85d32cf465507dd71d507100c1d",
"value":"0x7f110",
"gas": "0x7f110",
"gasPrice":"0x09184e72a000",
@ -100,7 +100,7 @@ var formattedBlockResultWithTx = {
"blockNumber": 5599,
"transactionIndex": 1,
"from":"0x407D73d8a49eeb85D32Cf465507dd71d507100c1", // checksum address
"to":"0x85h43d8a49EEB85D32CF465507dD71D507100C1", // checksum address
"to":"0x85F43D8a49eeB85d32Cf465507DD71d507100C1d", // checksum address
"value": '520464',
"gas": 520464,
"gasPrice": '10000000000000',