fixed formatting address

This commit is contained in:
Marek Kotewicz 2015-04-02 13:08:40 +02:00
parent 2536888f81
commit 5e150378c8
11 changed files with 130 additions and 79 deletions

View File

@ -272,19 +272,6 @@ var BigNumber = require('bignumber.js');
var utils = require('../utils/utils');
var c = require('../utils/config');
/**
* Should be called to pad string to expected length
*
* @method padLeft
* @param {String} string to be padded
* @param {Number} characters that result string should have
* @param {String} sign, by default 0
* @returns {String} right aligned string
*/
var padLeft = function (string, chars, sign) {
return new Array(chars - string.length + 1).join(sign ? sign : "0") + string;
};
/**
* Formats input value to byte representation of int
* If value is negative, return it's two's complement
@ -297,7 +284,7 @@ var padLeft = function (string, chars, sign) {
var formatInputInt = function (value) {
var padding = c.ETH_PADDING * 2;
BigNumber.config(c.ETH_BIGNUMBER_ROUNDING_MODE);
return padLeft(utils.toTwosComplement(value).round().toString(16), padding);
return utils.padLeft(utils.toTwosComplement(value).round().toString(16), padding);
};
/**
@ -676,6 +663,18 @@ var unitMap = {
'tether': '1000000000000000000000000000000'
};
/**
* Should be called to pad string to expected length
*
* @method padLeft
* @param {String} string to be padded
* @param {Number} characters that result string should have
* @param {String} sign, by default 0
* @returns {String} right aligned string
*/
var padLeft = function (string, chars, sign) {
return new Array(chars - string.length + 1).join(sign ? sign : "0") + string;
};
/** Finds first index of array element matching pattern
*
@ -965,13 +964,27 @@ var toTwosComplement = function (number) {
* @param {String} address the given HEX adress
* @return {Boolean}
*/
var isAddress = function(address) {
if (!isString(address)) {
return false;
var isAddress = function (address) {
return /^0x[0-9a-f]{40}$/.test(address);
};
/**
* Transforms given string to valid 20 bytes-length addres with 0x prefix
*
* @method toAddress
* @param {String} address
* @return {String} formatted address
*/
var toAddress = function (address) {
if (isAddress(address)) {
return address;
}
if (/^[0-9a-f]{40}$/.test(address)) {
return '0x' + address;
}
return ((address.indexOf('0x') === 0 && address.length === 42) ||
(address.indexOf('0x') === -1 && address.length === 40));
return '0x' + padLeft(toHex(address).substr(2), 40);
};
/**
@ -1058,6 +1071,7 @@ var isJson = function (str) {
};
module.exports = {
padLeft: padLeft,
findIndex: findIndex,
toHex: toHex,
toDecimal: toDecimal,
@ -1072,6 +1086,7 @@ module.exports = {
fromWei: fromWei,
toBigNumber: toBigNumber,
toTwosComplement: toTwosComplement,
toAddress: toAddress,
isBigNumber: isBigNumber,
isAddress: isAddress,
isFunction: isFunction,
@ -1627,7 +1642,7 @@ var getBalance = new Method({
name: 'getBalance',
call: 'eth_getBalance',
params: 2,
inputFormatter: [utils.toHex, formatters.inputDefaultBlockNumberFormatter],
inputFormatter: [utils.toAddress, formatters.inputDefaultBlockNumberFormatter],
outputFormatter: formatters.outputBigNumberFormatter
});
@ -1642,7 +1657,7 @@ var getCode = new Method({
name: 'getCode',
call: 'eth_getCode',
params: 2,
inputFormatter: [null, formatters.inputDefaultBlockNumberFormatter]
inputFormatter: [utils.toAddress, formatters.inputDefaultBlockNumberFormatter]
});
var getBlock = new Method({

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

57
dist/ethereum.js vendored
View File

@ -272,19 +272,6 @@ var BigNumber = require('bignumber.js');
var utils = require('../utils/utils');
var c = require('../utils/config');
/**
* Should be called to pad string to expected length
*
* @method padLeft
* @param {String} string to be padded
* @param {Number} characters that result string should have
* @param {String} sign, by default 0
* @returns {String} right aligned string
*/
var padLeft = function (string, chars, sign) {
return new Array(chars - string.length + 1).join(sign ? sign : "0") + string;
};
/**
* Formats input value to byte representation of int
* If value is negative, return it's two's complement
@ -297,7 +284,7 @@ var padLeft = function (string, chars, sign) {
var formatInputInt = function (value) {
var padding = c.ETH_PADDING * 2;
BigNumber.config(c.ETH_BIGNUMBER_ROUNDING_MODE);
return padLeft(utils.toTwosComplement(value).round().toString(16), padding);
return utils.padLeft(utils.toTwosComplement(value).round().toString(16), padding);
};
/**
@ -676,6 +663,18 @@ var unitMap = {
'tether': '1000000000000000000000000000000'
};
/**
* Should be called to pad string to expected length
*
* @method padLeft
* @param {String} string to be padded
* @param {Number} characters that result string should have
* @param {String} sign, by default 0
* @returns {String} right aligned string
*/
var padLeft = function (string, chars, sign) {
return new Array(chars - string.length + 1).join(sign ? sign : "0") + string;
};
/** Finds first index of array element matching pattern
*
@ -965,13 +964,27 @@ var toTwosComplement = function (number) {
* @param {String} address the given HEX adress
* @return {Boolean}
*/
var isAddress = function(address) {
if (!isString(address)) {
return false;
var isAddress = function (address) {
return /^0x[0-9a-f]{40}$/.test(address);
};
/**
* Transforms given string to valid 20 bytes-length addres with 0x prefix
*
* @method toAddress
* @param {String} address
* @return {String} formatted address
*/
var toAddress = function (address) {
if (isAddress(address)) {
return address;
}
if (/^[0-9a-f]{40}$/.test(address)) {
return '0x' + address;
}
return ((address.indexOf('0x') === 0 && address.length === 42) ||
(address.indexOf('0x') === -1 && address.length === 40));
return '0x' + padLeft(toHex(address).substr(2), 40);
};
/**
@ -1058,6 +1071,7 @@ var isJson = function (str) {
};
module.exports = {
padLeft: padLeft,
findIndex: findIndex,
toHex: toHex,
toDecimal: toDecimal,
@ -1072,6 +1086,7 @@ module.exports = {
fromWei: fromWei,
toBigNumber: toBigNumber,
toTwosComplement: toTwosComplement,
toAddress: toAddress,
isBigNumber: isBigNumber,
isAddress: isAddress,
isFunction: isFunction,
@ -1627,7 +1642,7 @@ var getBalance = new Method({
name: 'getBalance',
call: 'eth_getBalance',
params: 2,
inputFormatter: [utils.toHex, formatters.inputDefaultBlockNumberFormatter],
inputFormatter: [utils.toAddress, formatters.inputDefaultBlockNumberFormatter],
outputFormatter: formatters.outputBigNumberFormatter
});
@ -1642,7 +1657,7 @@ var getCode = new Method({
name: 'getCode',
call: 'eth_getCode',
params: 2,
inputFormatter: [null, formatters.inputDefaultBlockNumberFormatter]
inputFormatter: [utils.toAddress, formatters.inputDefaultBlockNumberFormatter]
});
var getBlock = new Method({

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -24,19 +24,6 @@ var BigNumber = require('bignumber.js');
var utils = require('../utils/utils');
var c = require('../utils/config');
/**
* Should be called to pad string to expected length
*
* @method padLeft
* @param {String} string to be padded
* @param {Number} characters that result string should have
* @param {String} sign, by default 0
* @returns {String} right aligned string
*/
var padLeft = function (string, chars, sign) {
return new Array(chars - string.length + 1).join(sign ? sign : "0") + string;
};
/**
* Formats input value to byte representation of int
* If value is negative, return it's two's complement
@ -49,7 +36,7 @@ var padLeft = function (string, chars, sign) {
var formatInputInt = function (value) {
var padding = c.ETH_PADDING * 2;
BigNumber.config(c.ETH_BIGNUMBER_ROUNDING_MODE);
return padLeft(utils.toTwosComplement(value).round().toString(16), padding);
return utils.padLeft(utils.toTwosComplement(value).round().toString(16), padding);
};
/**

View File

@ -54,6 +54,18 @@ var unitMap = {
'tether': '1000000000000000000000000000000'
};
/**
* Should be called to pad string to expected length
*
* @method padLeft
* @param {String} string to be padded
* @param {Number} characters that result string should have
* @param {String} sign, by default 0
* @returns {String} right aligned string
*/
var padLeft = function (string, chars, sign) {
return new Array(chars - string.length + 1).join(sign ? sign : "0") + string;
};
/** Finds first index of array element matching pattern
*
@ -343,13 +355,27 @@ var toTwosComplement = function (number) {
* @param {String} address the given HEX adress
* @return {Boolean}
*/
var isAddress = function(address) {
if (!isString(address)) {
return false;
var isAddress = function (address) {
return /^0x[0-9a-f]{40}$/.test(address);
};
/**
* Transforms given string to valid 20 bytes-length addres with 0x prefix
*
* @method toAddress
* @param {String} address
* @return {String} formatted address
*/
var toAddress = function (address) {
if (isAddress(address)) {
return address;
}
if (/^[0-9a-f]{40}$/.test(address)) {
return '0x' + address;
}
return ((address.indexOf('0x') === 0 && address.length === 42) ||
(address.indexOf('0x') === -1 && address.length === 40));
return '0x' + padLeft(toHex(address).substr(2), 40);
};
/**
@ -436,6 +462,7 @@ var isJson = function (str) {
};
module.exports = {
padLeft: padLeft,
findIndex: findIndex,
toHex: toHex,
toDecimal: toDecimal,
@ -450,6 +477,7 @@ module.exports = {
fromWei: fromWei,
toBigNumber: toBigNumber,
toTwosComplement: toTwosComplement,
toAddress: toAddress,
isBigNumber: isBigNumber,
isAddress: isAddress,
isFunction: isFunction,

View File

@ -80,7 +80,7 @@ var getBalance = new Method({
name: 'getBalance',
call: 'eth_getBalance',
params: 2,
inputFormatter: [utils.toHex, formatters.inputDefaultBlockNumberFormatter],
inputFormatter: [utils.toAddress, formatters.inputDefaultBlockNumberFormatter],
outputFormatter: formatters.outputBigNumberFormatter
});
@ -95,7 +95,7 @@ var getCode = new Method({
name: 'getCode',
call: 'eth_getCode',
params: 2,
inputFormatter: [null, formatters.inputDefaultBlockNumberFormatter]
inputFormatter: [utils.toAddress, formatters.inputDefaultBlockNumberFormatter]
});
var getBlock = new Method({

View File

@ -8,7 +8,7 @@ var tests = [
{ value: 'function', is: false},
{ value: {}, is: false},
{ value: '0xc6d9d2cd449a754c494264e1809c50e34d64562b', is: true },
{ value: 'c6d9d2cd449a754c494264e1809c50e34d64562b', is: true }
{ value: 'c6d9d2cd449a754c494264e1809c50e34d64562b', is: false }
];
describe('lib/utils/utils', function () {

View File

@ -6,25 +6,25 @@ var method = 'getBalance';
var tests = [{
args: [301, 2],
formattedArgs: ['0x12d', '0x2'],
formattedArgs: ['0x000000000000000000000000000000000000012d', '0x2'],
result: '0x31981',
formattedResult: new BigNumber('0x31981', 16),
call: 'eth_'+ method
},{
args: ['0x12d', '0x1'],
formattedArgs: ['0x12d', '0x1'],
formattedArgs: ['0x000000000000000000000000000000000000012d', '0x1'],
result: '0x31981',
formattedResult: new BigNumber('0x31981', 16),
call: 'eth_'+ method
}, {
args: [0x12d, 0x1],
formattedArgs: ['0x12d', '0x1'],
formattedArgs: ['0x000000000000000000000000000000000000012d', '0x1'],
result: '0x31981',
formattedResult: new BigNumber('0x31981', 16),
call: 'eth_'+ method
}, {
args: [0x12d],
formattedArgs: ['0x12d', web3.eth.defaultBlock],
formattedArgs: ['0x000000000000000000000000000000000000012d', web3.eth.defaultBlock],
result: '0x31981',
formattedResult: new BigNumber('0x31981', 16),
call: 'eth_'+ method
@ -46,6 +46,12 @@ var tests = [{
result: '0x31981',
formattedResult: new BigNumber('0x31981', 16),
call: 'eth_'+ method
}, {
args: ['0x00000000000000000000aaaaaaaaaaaaaaaaaaa'],
formattedArgs: ['0x000000000000000000000aaaaaaaaaaaaaaaaaaa', 'latest'],
result: '0x31981',
formattedResult: new BigNumber('0x31981', 16),
call: 'eth_'+ method
}];
testMethod.runTests('eth', method, tests);