From fbcc6d0d25d1a9e9eed5be22874e559a2bf49911 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Fri, 16 Jan 2015 10:47:43 +0100 Subject: BigNumber support --- lib/abi.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/abi.js b/lib/abi.js index b51fbab59..13d6e45fc 100644 --- a/lib/abi.js +++ b/lib/abi.js @@ -26,6 +26,8 @@ if (process.env.NODE_ENV !== 'build') { var web3 = require('./web3'); // jshint ignore:line } +var BigNumber = require('bignumber.js'); + // TODO: make these be actually accurate instead of falling back onto JS's doubles. var hexToDec = function (hex) { return parseInt(hex, 16).toString(); @@ -84,24 +86,26 @@ var namedType = function (name) { var setupInputTypes = function () { /// Formats input value to byte representation of int + /// If value is negative, return it's two's complement /// @returns right-aligned byte representation of int var formatInt = function (value) { var padding = 32 * 2; - if (typeof value === 'number') { - if (value < 0) { - - // two's complement - // TODO: fix big numbers support - value = ((value) >>> 0).toString(16); - return padLeft(value, padding, 'f'); - } - value = value.toString(16); - + if (value instanceof BigNumber) { + if (value.lessThan(0)) + value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(value).plus(1).toString(16); + else + value = value.toString(16); + } + else if (typeof value === 'number') { + if (value < 0) + value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(value).plus(1).toString(16); + else + value = new BigNumber(value).toString(16); } else if (value.indexOf('0x') === 0) value = value.substr(2); else if (typeof value === 'string') - value = value.toHex(value); + value = new BigNumber(value).toString(16); else value = (+value).toString(16); return padLeft(value, padding); -- cgit v1.2.3 From 2c36d5ff457952c557b467e580514b08126d7dd7 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Fri, 16 Jan 2015 11:58:26 +0100 Subject: big integers on abi.js output, tests --- lib/abi.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/abi.js b/lib/abi.js index 13d6e45fc..8af10c382 100644 --- a/lib/abi.js +++ b/lib/abi.js @@ -175,7 +175,16 @@ var setupOutputTypes = function () { /// Formats input right-aligned input bytes to int /// @returns right-aligned input bytes formatted to int var formatInt = function (value) { - return value.length <= 8 ? +parseInt(value, 16) : hexToDec(value); + // check if it's negative number + // it it is, return two's complement + if (value.substr(0, 1).toLowerCase() === 'f') { + return new BigNumber(value, 16).minus(new BigNumber('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)).minus(1); + } + return new BigNumber(value, 16); + }; + + var formatUInt = function (value) { + return new BigNumber(value, 16); }; /// @returns right-aligned input bytes formatted to hex @@ -199,7 +208,7 @@ var setupOutputTypes = function () { }; return [ - { type: prefixedType('uint'), format: formatInt }, + { type: prefixedType('uint'), format: formatUInt }, { type: prefixedType('int'), format: formatInt }, { type: prefixedType('hash'), format: formatHash }, { type: prefixedType('string'), format: formatString }, -- cgit v1.2.3