From d38f8bcbe6519d2d6d2e825d8b2ae4330eb04667 Mon Sep 17 00:00:00 2001 From: debris Date: Tue, 28 Jul 2015 10:13:59 +0200 Subject: [PATCH] solidity type int --- lib/solidity/coder.js | 18 +---------- test/coder.decodeParam.js | 67 ++++++++++++++++++++++++--------------- test/coder.encodeParam.js | 28 ++++++++-------- 3 files changed, 56 insertions(+), 57 deletions(-) diff --git a/lib/solidity/coder.js b/lib/solidity/coder.js index 60f0d84..1ed9441 100644 --- a/lib/solidity/coder.js +++ b/lib/solidity/coder.js @@ -28,6 +28,7 @@ var f = require('./formatters'); var SolidityType = require('./type'); var SolidityTypeAddress = require('./address'); var SolidityTypeBool = require('./bool'); +var SolidityTypeInt = require('./int'); /** * SolidityCoder prototype should be used to encode/decode solidity params of any type @@ -207,23 +208,6 @@ SolidityCoder.prototype.getSolidityTypes = function (types) { }); }; - -var SolidityTypeInt = function () { - this._inputFormatter = f.formatInputInt; - this._outputFormatter = f.formatOutputInt; -}; - -SolidityTypeInt.prototype = new SolidityType({}); -SolidityTypeInt.prototype.constructor = SolidityTypeInt; - -SolidityTypeInt.prototype.isType = function (name) { - return !!name.match(/^int([0-9]{1,3})?/); -}; - -SolidityTypeInt.prototype.staticPartLength = function (name) { - return 32; -}; - var SolidityTypeUInt = function () { this._inputFormatter = f.formatInputInt; this._outputFormatter = f.formatOutputUInt; diff --git a/test/coder.decodeParam.js b/test/coder.decodeParam.js index 76d38a1..048cae0 100644 --- a/test/coder.decodeParam.js +++ b/test/coder.decodeParam.js @@ -66,17 +66,40 @@ describe('lib/solidity/coder', function () { '0000000000000000000000000000000000000000000000000000000000000001' + '0000000000000000000000000000000000000000000000000000000000000000'}); - //test({ type: 'int', expected: new bn(1), value: '0000000000000000000000000000000000000000000000000000000000000001'}); - //test({ type: 'int', expected: new bn(1), value: '0000000000000000000000000000000000000000000000000000000000000001'}); - //test({ type: 'int', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'}); - //test({ type: 'int', expected: new bn(-1), value: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}); - //test({ type: 'int256', expected: new bn(1), value: '0000000000000000000000000000000000000000000000000000000000000001'}); - //test({ type: 'int256', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'}); - //test({ type: 'int256', expected: new bn(-1), value: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}); - //test({ type: 'int8', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'}); - //test({ type: 'int32', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'}); - //test({ type: 'int64', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'}); - //test({ type: 'int128', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'}); + test({ type: 'int', expected: new bn(1), value: '0000000000000000000000000000000000000000000000000000000000000001'}); + test({ type: 'int', expected: new bn(1), value: '0000000000000000000000000000000000000000000000000000000000000001'}); + test({ type: 'int', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'}); + test({ type: 'int', expected: new bn(-1), value: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}); + test({ type: 'int256', expected: new bn(1), value: '0000000000000000000000000000000000000000000000000000000000000001'}); + test({ type: 'int256', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'}); + test({ type: 'int256', expected: new bn(-1), value: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}); + test({ type: 'int8', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'}); + test({ type: 'int32', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'}); + test({ type: 'int64', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'}); + test({ type: 'int128', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'}); + test({ type: 'int[]', expected: [], value: '0000000000000000000000000000000000000000000000000000000000000020' + + '0000000000000000000000000000000000000000000000000000000000000000'}); + test({ type: 'int[]', expected: [new bn(3)], value: '0000000000000000000000000000000000000000000000000000000000000020' + + '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000003'}); + test({ type: 'int256[]', expected: [new bn(3)], value: '0000000000000000000000000000000000000000000000000000000000000020' + + '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000003'}); + test({ type: 'int[]', expected: [new bn(1), new bn(2), new bn(3)], + value: '0000000000000000000000000000000000000000000000000000000000000020' + + '0000000000000000000000000000000000000000000000000000000000000003' + + '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000002' + + '0000000000000000000000000000000000000000000000000000000000000003'}); + test({ type: 'int[][3]', expected: [[new bn(1), new bn(2), new bn(3)], [new bn(4), new bn(5), new bn(6)]], + value: '0000000000000000000000000000000000000000000000000000000000000020' + + '0000000000000000000000000000000000000000000000000000000000000002' + + '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000002' + + '0000000000000000000000000000000000000000000000000000000000000003' + + '0000000000000000000000000000000000000000000000000000000000000004' + + '0000000000000000000000000000000000000000000000000000000000000005' + + '0000000000000000000000000000000000000000000000000000000000000006'}); //test({ type: 'bytes32', expected: '0x6761766f66796f726b0000000000000000000000000000000000000000000000', //value: '6761766f66796f726b0000000000000000000000000000000000000000000000'}); //test({ type: 'bytes', expected: '0x6761766f66796f726b', @@ -128,20 +151,6 @@ describe('lib/solidity/coder', function () { //'c3a40000c3a40000000000000000000000000000000000000000000000000000'}); //test({ type: 'bytes32', expected: '0xc3a40000c3a40000000000000000000000000000000000000000000000000000', //value: 'c3a40000c3a40000000000000000000000000000000000000000000000000000'}); - //test({ type: 'int[]', expected: [], value: '0000000000000000000000000000000000000000000000000000000000000020' + - //'0000000000000000000000000000000000000000000000000000000000000000'}); - //test({ type: 'int[]', expected: [new bn(3)], value: '0000000000000000000000000000000000000000000000000000000000000020' + - //'0000000000000000000000000000000000000000000000000000000000000001' + - //'0000000000000000000000000000000000000000000000000000000000000003'}); - //test({ type: 'int256[]', expected: [new bn(3)], value: '0000000000000000000000000000000000000000000000000000000000000020' + - //'0000000000000000000000000000000000000000000000000000000000000001' + - //'0000000000000000000000000000000000000000000000000000000000000003'}); - //test({ type: 'int[]', expected: [new bn(1), new bn(2), new bn(3)], - //value: '0000000000000000000000000000000000000000000000000000000000000020' + - //'0000000000000000000000000000000000000000000000000000000000000003' + - //'0000000000000000000000000000000000000000000000000000000000000001' + - //'0000000000000000000000000000000000000000000000000000000000000002' + - //'0000000000000000000000000000000000000000000000000000000000000003'}); //test({ type: 'bool', expected: true, value: '0000000000000000000000000000000000000000000000000000000000000001'}); //test({ type: 'bool', expected: false, value: '0000000000000000000000000000000000000000000000000000000000000000'}); //test({ type: 'real', expected: new bn(1), value: '0000000000000000000000000000000100000000000000000000000000000000'}); @@ -181,7 +190,13 @@ describe('lib/solidity/coder', function () { '0000000000000000000000000000000000000000000000000000000000000000' + '0000000000000000000000000000000000000000000000000000000000000000' + '0000000000000000000000000000000000000000000000000000000000000001'}); - //test({ types: ['int'], expected: [new bn(1)], values: '0000000000000000000000000000000000000000000000000000000000000001'}); + test({ types: ['int[2]', 'int256[3]'], expected: [[new bn(1), new bn(2)], [new bn(3), new bn(4), new bn(5)]], + values: '0000000000000000000000000000000000000000000000000000000000000001' + + '0000000000000000000000000000000000000000000000000000000000000002' + + '0000000000000000000000000000000000000000000000000000000000000003' + + '0000000000000000000000000000000000000000000000000000000000000004' + + '0000000000000000000000000000000000000000000000000000000000000005'}); + test({ types: ['int'], expected: [new bn(1)], values: '0000000000000000000000000000000000000000000000000000000000000001'}); //test({ types: ['bytes32', 'int'], expected: ['0x6761766f66796f726b0000000000000000000000000000000000000000000000', new bn(5)], //values: '6761766f66796f726b0000000000000000000000000000000000000000000000' + //'0000000000000000000000000000000000000000000000000000000000000005'}); diff --git a/test/coder.encodeParam.js b/test/coder.encodeParam.js index 304e239..723c548 100644 --- a/test/coder.encodeParam.js +++ b/test/coder.encodeParam.js @@ -74,14 +74,14 @@ describe('lib/solidity/coder', function () { '0000000000000000000000000000000000000000000000000000000000000001' + '0000000000000000000000000000000000000000000000000000000000000000'}); - //test({ type: 'int', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'}); - //test({ type: 'int', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'}); - //test({ type: 'int', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}); - //test({ type: 'int', value: 0.1, expected: '0000000000000000000000000000000000000000000000000000000000000000'}); - //test({ type: 'int', value: 3.9, expected: '0000000000000000000000000000000000000000000000000000000000000003'}); - //test({ type: 'int256', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'}); - //test({ type: 'int256', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'}); - //test({ type: 'int256', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}); + test({ type: 'int', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'}); + test({ type: 'int', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'}); + test({ type: 'int', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}); + test({ type: 'int', value: 0.1, expected: '0000000000000000000000000000000000000000000000000000000000000000'}); + test({ type: 'int', value: 3.9, expected: '0000000000000000000000000000000000000000000000000000000000000003'}); + test({ type: 'int256', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'}); + test({ type: 'int256', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'}); + test({ type: 'int256', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}); //test({ type: 'bytes32', value: '0x6761766f66796f726b', //expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'}); //test({ type: 'bytes32', value: '0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b', @@ -185,12 +185,12 @@ describe('lib/solidity/coder', function () { '0000000000000000000000000000000000000000000000000000000000000000' + '0000000000000000000000000000000000000000000000000000000000000000' + '0000000000000000000000000000000000000000000000000000000000000001'}); - //test({ types: ['int'], values: [1], expected: '0000000000000000000000000000000000000000000000000000000000000001'}); - //test({ types: ['int'], values: [16], expected: '0000000000000000000000000000000000000000000000000000000000000010'}); - //test({ types: ['int'], values: [-1], expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}); - //test({ types: ['int256'], values: [1], expected: '0000000000000000000000000000000000000000000000000000000000000001'}); - //test({ types: ['int256'], values: [16], expected: '0000000000000000000000000000000000000000000000000000000000000010'}); - //test({ types: ['int256'], values: [-1], expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}); + test({ types: ['int'], values: [1], expected: '0000000000000000000000000000000000000000000000000000000000000001'}); + test({ types: ['int'], values: [16], expected: '0000000000000000000000000000000000000000000000000000000000000010'}); + test({ types: ['int'], values: [-1], expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}); + test({ types: ['int256'], values: [1], expected: '0000000000000000000000000000000000000000000000000000000000000001'}); + test({ types: ['int256'], values: [16], expected: '0000000000000000000000000000000000000000000000000000000000000010'}); + test({ types: ['int256'], values: [-1], expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}); //test({ types: ['bytes32'], values: ['0x6761766f66796f726b'], //expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'}); //test({ types: ['string'], values: ['gavofyork'], expected: '0000000000000000000000000000000000000000000000000000000000000020' +