diff options
Diffstat (limited to 'lib/abi.js')
-rw-r--r-- | lib/abi.js | 61 |
1 files changed, 45 insertions, 16 deletions
diff --git a/lib/abi.js b/lib/abi.js index e80406ef9..5a4d64515 100644 --- a/lib/abi.js +++ b/lib/abi.js @@ -54,17 +54,44 @@ var padLeft = function (string, chars) { return new Array(chars - string.length + 1).join("0") + string; }; +var calcBitPadding = function (type, expected) { + var value = type.slice(expected.length); + if (value === "") { + return 32; + } + return parseInt(value) / 8; +}; + +var calcBytePadding = function (type, expected) { + var value = type.slice(expected.length); + if (value === "") { + return 32; + } + return parseInt(value); +}; + +var calcRealPadding = function (type, expected) { + var value = type.slice(expected.length); + if (value === "") { + return 32; + } + var sizes = value.split('x'); + for (var padding = 0, i = 0; i < sizes; i++) { + padding += (sizes[i] / 8); + } + return padding; +}; + var setupInputTypes = function () { - // @param prefix is the string prefix of the type - // @param bitsInNumber is number of bits per number in type - var prefixedType = function (prefix, bitsInNumber) { + + var prefixedType = function (prefix, calcPadding) { return function (type, value) { var expected = prefix; if (type.indexOf(expected) !== 0) { return false; } - var padding = parseInt(type.slice(expected.length)) / 8 * bitsInNumber; + var padding = calcPadding(type, expected); if (typeof value === "number") value = value.toString(16); else if (typeof value === "string") @@ -92,10 +119,12 @@ var setupInputTypes = function () { }; return [ - prefixedType('uint', 1), - prefixedType('int', 1), - prefixedType('hash', 1), - prefixedType('string', 8), + prefixedType('uint', calcBitPadding), + prefixedType('int', calcBitPadding), + prefixedType('hash', calcBitPadding), + prefixedType('string', calcBytePadding), + prefixedType('real', calcRealPadding), + prefixedType('ureal', calcRealPadding), namedType('address', 20), namedType('bool', 1, formatBool), ]; @@ -129,16 +158,14 @@ var toAbiInput = function (json, methodName, params) { var setupOutputTypes = function () { - // @param prefix is the string prefix of the type - // @param bitsInNumber is number of bits per number in type - var prefixedType = function (prefix, bitsInNumber) { + var prefixedType = function (prefix, calcPadding) { return function (type) { var expected = prefix; if (type.indexOf(expected) !== 0) { return -1; } - var padding = parseInt(type.slice(expected.length)) / 8 * bitsInNumber; + var padding = calcPadding(type, expected); return padding * 2; }; }; @@ -166,10 +193,12 @@ var setupOutputTypes = function () { }; return [ - { padding: prefixedType('uint', 1), format: formatInt }, - { padding: prefixedType('int', 1), format: formatInt }, - { padding: prefixedType('hash', 1), format: formatHash }, - { padding: prefixedType('string', 8), format: formatString }, + { padding: prefixedType('uint', calcBitPadding), format: formatInt }, + { padding: prefixedType('int', calcBitPadding), format: formatInt }, + { padding: prefixedType('hash', calcBitPadding), format: formatHash }, + { padding: prefixedType('string', calcBytePadding), format: formatString }, + { padding: prefixedType('real', calcRealPadding), format: formatInt }, + { padding: prefixedType('ureal', calcRealPadding), format: formatInt }, { padding: namedType('address', 20) }, { padding: namedType('bool', 1), format: formatBool } ]; |