From b46140cd07df034aba5e060fc024cd58881b8748 Mon Sep 17 00:00:00 2001 From: debris Date: Wed, 29 Jul 2015 17:00:11 +0200 Subject: [PATCH] fixed array order in address, bool, bytes array order --- lib/solidity/address.js | 21 ----------- lib/solidity/bool.js | 23 +----------- lib/solidity/bytes.js | 25 ++----------- lib/solidity/type.js | 75 +++++++++++++++++++++++++++++++++++++-- test/coder.decodeParam.js | 4 +-- test/coder.encodeParam.js | 4 +-- 6 files changed, 80 insertions(+), 72 deletions(-) diff --git a/lib/solidity/address.js b/lib/solidity/address.js index 6fa83728..e7345495 100644 --- a/lib/solidity/address.js +++ b/lib/solidity/address.js @@ -27,26 +27,5 @@ SolidityTypeAddress.prototype.staticPartLength = function (name) { return 32 * this.staticArrayLength(name); }; -SolidityTypeAddress.prototype.isDynamicArray = function (name) { - var matches = name.match(/address(\[([0-9]*)\])?/); - // is array && doesn't have length specified - return !!matches[1] && !matches[2]; -}; - -SolidityTypeAddress.prototype.isStaticArray = function (name) { - var matches = name.match(/address(\[([0-9]*)\])?/); - // is array && have length specified - return !!matches[1] && !!matches[2]; -}; - -SolidityTypeAddress.prototype.staticArrayLength = function (name) { - return name.match(/address(\[([0-9]*)\])?/)[2] || 1; -}; - -SolidityTypeAddress.prototype.nestedName = function (name) { - // removes first [] in name - return name.replace(/\[([0-9])*\]/, ''); -}; - module.exports = SolidityTypeAddress; diff --git a/lib/solidity/bool.js b/lib/solidity/bool.js index 69f232b4..cdc04390 100644 --- a/lib/solidity/bool.js +++ b/lib/solidity/bool.js @@ -20,32 +20,11 @@ SolidityTypeBool.prototype = new SolidityType({}); SolidityTypeBool.prototype.constructor = SolidityTypeBool; SolidityTypeBool.prototype.isType = function (name) { - return !!name.match(/bool(\[([0-9]*)\])?/); + return !!name.match(/^bool(\[([0-9]*)\])*$/); }; SolidityTypeBool.prototype.staticPartLength = function (name) { return 32 * this.staticArrayLength(name); }; -SolidityTypeBool.prototype.isDynamicArray = function (name) { - var matches = name.match(/bool(\[([0-9]*)\])?/); - // is array && doesn't have length specified - return !!matches[1] && !matches[2]; -}; - -SolidityTypeBool.prototype.isStaticArray = function (name) { - var matches = name.match(/bool(\[([0-9]*)\])?/); - // is array && have length specified - return !!matches[1] && !!matches[2]; -}; - -SolidityTypeBool.prototype.staticArrayLength = function (name) { - return name.match(/bool(\[([0-9]*)\])?/)[2] || 1; -}; - -SolidityTypeBool.prototype.nestedName = function (name) { - // removes first [] in name - return name.replace(/\[([0-9])*\]/, ''); -}; - module.exports = SolidityTypeBool; diff --git a/lib/solidity/bytes.js b/lib/solidity/bytes.js index 451d6106..92dfb0cf 100644 --- a/lib/solidity/bytes.js +++ b/lib/solidity/bytes.js @@ -26,34 +26,13 @@ SolidityTypeBytes.prototype = new SolidityType({}); SolidityTypeBytes.prototype.constructor = SolidityTypeBytes; SolidityTypeBytes.prototype.isType = function (name) { - return !!name.match(/bytes([0-9]*)(\[([0-9]*)\])?/); + return !!name.match(/^bytes([0-9]{1,})(\[([0-9]*)\])*$/); }; SolidityTypeBytes.prototype.staticPartLength = function (name) { - var matches = name.match(/bytes([0-9]*)(\[([0-9]*)\])?/); + var matches = name.match(/^bytes([0-9]*)/); var size = parseInt(matches[1]); return size * this.staticArrayLength(name); }; -SolidityTypeBytes.prototype.isDynamicArray = function (name) { - var matches = name.match(/bytes([0-9]*)(\[([0-9]*)\])?/); - // is array && doesn't have length specified - return !!matches[2] && !matches[3]; -}; - -SolidityTypeBytes.prototype.isStaticArray = function (name) { - var matches = name.match(/bytes([0-9]*)(\[([0-9]*)\])?/); - // is array && have length specified - return !!matches[2] && !!matches[3]; -}; - -SolidityTypeBytes.prototype.staticArrayLength = function (name) { - return name.match(/bytes([0-9]*)(\[([0-9]*)\])?/)[3] || 1; -}; - -SolidityTypeBytes.prototype.nestedName = function (name) { - // removes first [] in name - return name.replace(/\[([0-9])*\]/, ''); -}; - module.exports = SolidityTypeBytes; diff --git a/lib/solidity/type.js b/lib/solidity/type.js index a14945d0..8c57e53c 100644 --- a/lib/solidity/type.js +++ b/lib/solidity/type.js @@ -42,7 +42,8 @@ SolidityType.prototype.staticPartLength = function (name) { * @return {Bool} true if the type is dynamic array */ SolidityType.prototype.isDynamicArray = function (name) { - throw "this method should be overrwritten!"; + var nestedTypes = this.nestedTypes(name); + return !!nestedTypes && !nestedTypes[nestedTypes.length - 1].match(/[0-9]{1,}/g); }; /** @@ -56,13 +57,83 @@ SolidityType.prototype.isDynamicArray = function (name) { * @return {Bool} true if the type is static array */ SolidityType.prototype.isStaticArray = function (name) { - throw "this method should be overrwritten!"; + var nestedTypes = this.nestedTypes(name); + return !!nestedTypes && !!nestedTypes[nestedTypes.length - 1].match(/[0-9]{1,}/g); }; +/** + * Should return length of static array + * eg. + * "int[32]" => 32 + * "int256[14]" => 14 + * "int[2][3]" => 3 + * "int" => 1 + * "int[1]" => 1 + * "int[]" => 1 + * + * @method staticArrayLength + * @param {String} name + * @return {Number} static array length + */ +SolidityType.prototype.staticArrayLength = function (name) { + var nestedTypes = this.nestedTypes(name); + if (nestedTypes) { + return parseInt(nestedTypes[nestedTypes.length - 1].match(/[0-9]{1,}/g) || 1); + } + return 1; +}; + +/** + * Should return nested type + * eg. + * "int[32]" => "int" + * "int256[14]" => "int256" + * "int[2][3]" => "int[2]" + * "int" => "int" + * "int[]" => "int" + * + * @method nestedName + * @param {String} name + * @return {String} nested name + */ +SolidityType.prototype.nestedName = function (name) { + // remove last [] in name + var nestedTypes = this.nestedTypes(name); + if (!nestedTypes) { + return name; + } + + return name.substr(0, name.length - nestedTypes[nestedTypes.length - 1].length); +}; + +/** + * Should return true if type has dynamic size by default + * such types are "string", "bytes" + * + * @method isDynamicType + * @param {String} name + * @return {Bool} true if is dynamic, otherwise false + */ SolidityType.prototype.isDynamicType = function (name) { return false; }; +/** + * Should return array of nested types + * eg. + * "int[2][3][]" => ["[2]", "[3]", "[]"] + * "int[] => ["[]"] + * "int" => null + * + * @method nestedTypes + * @param {String} name + * @return {Array} array of nested types + */ +SolidityType.prototype.nestedTypes = function (name) { + // return list of strings eg. "[]", "[3]", "[]", "[2]" + return name.match(/(\[[0-9]*\])/g); +}; + /** * Should be used to encode the value * diff --git a/test/coder.decodeParam.js b/test/coder.decodeParam.js index 31b167b3..9f09a2cb 100644 --- a/test/coder.decodeParam.js +++ b/test/coder.decodeParam.js @@ -24,7 +24,7 @@ describe('lib/solidity/coder', function () { '0000000000000000000000000000000000000000000000000000000000000002' + '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' + '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' }); - test({ type: 'address[2][]', expected: [['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c2'], + test({ type: 'address[][2]', expected: [['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c2'], ['0x407d73d8a49eeb85d32cf465507dd71d507100c3', '0x407d73d8a49eeb85d32cf465507dd71d507100c4']], value: '0000000000000000000000000000000000000000000000000000000000000040' + '00000000000000000000000000000000000000000000000000000000000000a0' + @@ -34,7 +34,7 @@ describe('lib/solidity/coder', function () { '0000000000000000000000000000000000000000000000000000000000000002' + /* a0 */ '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' + '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c4' }); - test({ type: 'address[][2]', expected: [['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c2'], + test({ type: 'address[2][]', expected: [['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c2'], ['0x407d73d8a49eeb85d32cf465507dd71d507100c3', '0x407d73d8a49eeb85d32cf465507dd71d507100c4']], value: '0000000000000000000000000000000000000000000000000000000000000020' + '0000000000000000000000000000000000000000000000000000000000000002' + /* 20 */ diff --git a/test/coder.encodeParam.js b/test/coder.encodeParam.js index 65d3d777..b786a28f 100644 --- a/test/coder.encodeParam.js +++ b/test/coder.encodeParam.js @@ -22,7 +22,7 @@ describe('lib/solidity/coder', function () { '0000000000000000000000000000000000000000000000000000000000000002' + '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' + '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' }); - test({ type: 'address[2][]', value: [['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c2'], + test({ type: 'address[][2]', value: [['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c2'], ['0x407d73d8a49eeb85d32cf465507dd71d507100c3', '0x407d73d8a49eeb85d32cf465507dd71d507100c4']], expected: '0000000000000000000000000000000000000000000000000000000000000040' + '00000000000000000000000000000000000000000000000000000000000000a0' + @@ -32,7 +32,7 @@ describe('lib/solidity/coder', function () { '0000000000000000000000000000000000000000000000000000000000000002' + '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' + '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c4' }); - test({ type: 'address[][2]', value: [['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c2'], + test({ type: 'address[2][]', value: [['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c2'], ['0x407d73d8a49eeb85d32cf465507dd71d507100c3', '0x407d73d8a49eeb85d32cf465507dd71d507100c4']], expected: '0000000000000000000000000000000000000000000000000000000000000020' + '0000000000000000000000000000000000000000000000000000000000000002' +