aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarek Kotewicz <marek.kotewicz@gmail.com>2015-01-15 22:51:25 +0800
committerMarek Kotewicz <marek.kotewicz@gmail.com>2015-01-15 22:51:25 +0800
commit46b932ccc03b0251d97084bbbde5193532b5618f (patch)
tree1bc3cd26f724f7ef83a77cc2ab1d0947c7882f22
parentf85f77f6cc536b94368c51edec2159d17d0672f6 (diff)
downloaddexon-46b932ccc03b0251d97084bbbde5193532b5618f.tar
dexon-46b932ccc03b0251d97084bbbde5193532b5618f.tar.gz
dexon-46b932ccc03b0251d97084bbbde5193532b5618f.tar.bz2
dexon-46b932ccc03b0251d97084bbbde5193532b5618f.tar.lz
dexon-46b932ccc03b0251d97084bbbde5193532b5618f.tar.xz
dexon-46b932ccc03b0251d97084bbbde5193532b5618f.tar.zst
dexon-46b932ccc03b0251d97084bbbde5193532b5618f.zip
negative integers support
-rw-r--r--lib/abi.js16
-rw-r--r--test/abi.parsers.js11
2 files changed, 23 insertions, 4 deletions
diff --git a/lib/abi.js b/lib/abi.js
index 29d3e3e4b..d779944d2 100644
--- a/lib/abi.js
+++ b/lib/abi.js
@@ -57,9 +57,10 @@ var findMethodIndex = function (json, methodName) {
/// @param string string to be padded
/// @param number of characters that result string should have
+/// @param sign, by default 0
/// @returns right aligned string
-var padLeft = function (string, chars) {
- return new Array(chars - string.length + 1).join("0") + string;
+var padLeft = function (string, chars, sign) {
+ return new Array(chars - string.length + 1).join(sign ? sign : "0") + string;
};
/// @param expected type prefix (string)
@@ -86,8 +87,17 @@ var setupInputTypes = function () {
/// @returns right-aligned byte representation of int
var formatInt = function (value) {
var padding = 32 * 2;
- if (typeof value === 'number')
+ 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);
+
+ }
else if (value.indexOf('0x') === 0)
value = value.substr(2);
else if (typeof value === 'string')
diff --git a/test/abi.parsers.js b/test/abi.parsers.js
index 03cc90dfe..925324461 100644
--- a/test/abi.parsers.js
+++ b/test/abi.parsers.js
@@ -55,7 +55,7 @@ describe('abi', function() {
});
- it('should parse input uint', function() {
+ it('should parse input uint256', function() {
// given
var d = clone(description);
@@ -88,6 +88,9 @@ describe('abi', function() {
// then
assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001");
assert.equal(parser.test(10), "000000000000000000000000000000000000000000000000000000000000000a");
+ assert.equal(parser.test(-1), "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
+ assert.equal(parser.test(-2), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe");
+ assert.equal(parser.test(-16), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0");
});
@@ -106,6 +109,9 @@ describe('abi', function() {
// then
assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001");
assert.equal(parser.test(10), "000000000000000000000000000000000000000000000000000000000000000a");
+ assert.equal(parser.test(-1), "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
+ assert.equal(parser.test(-2), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe");
+ assert.equal(parser.test(-16), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0");
});
@@ -124,6 +130,9 @@ describe('abi', function() {
// then
assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001");
assert.equal(parser.test(10), "000000000000000000000000000000000000000000000000000000000000000a");
+ assert.equal(parser.test(-1), "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
+ assert.equal(parser.test(-2), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe");
+ assert.equal(parser.test(-16), "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0");
});