aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMarek Kotewicz <marek.kotewicz@gmail.com>2015-01-17 09:14:40 +0800
committerMarek Kotewicz <marek.kotewicz@gmail.com>2015-01-17 09:14:40 +0800
commitb457e88cd0fbc5724cbca68ecebf5e829c65bf98 (patch)
treeb97c0deac182ee29be57481f551733f298fb6137 /lib
parent2d8383d3c8be80dab644fc67c47327872a87f50d (diff)
downloaddexon-b457e88cd0fbc5724cbca68ecebf5e829c65bf98.tar
dexon-b457e88cd0fbc5724cbca68ecebf5e829c65bf98.tar.gz
dexon-b457e88cd0fbc5724cbca68ecebf5e829c65bf98.tar.bz2
dexon-b457e88cd0fbc5724cbca68ecebf5e829c65bf98.tar.lz
dexon-b457e88cd0fbc5724cbca68ecebf5e829c65bf98.tar.xz
dexon-b457e88cd0fbc5724cbca68ecebf5e829c65bf98.tar.zst
dexon-b457e88cd0fbc5724cbca68ecebf5e829c65bf98.zip
support for int array types[]
Diffstat (limited to 'lib')
-rw-r--r--lib/abi.js119
1 files changed, 72 insertions, 47 deletions
diff --git a/lib/abi.js b/lib/abi.js
index 3571472dc..2c0fe92e0 100644
--- a/lib/abi.js
+++ b/lib/abi.js
@@ -76,55 +76,66 @@ var namedType = function (name) {
};
};
+var arrayType = function (type) {
+ return type.slice(-2) === '[]';
+};
+
+/// Formats input value to byte representation of int
+/// If value is negative, return it's two's complement
+/// If the value is floating point, round it down
+/// @returns right-aligned byte representation of int
+var formatInputInt = function (value) {
+ var padding = ETH_PADDING * 2;
+ if (value instanceof BigNumber || typeof value === 'number') {
+ if (typeof value === 'number')
+ value = new BigNumber(value);
+ value = value.round();
+
+ if (value.lessThan(0))
+ value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(value).plus(1);
+ value = value.toString(16);
+ }
+ else if (value.indexOf('0x') === 0)
+ value = value.substr(2);
+ else if (typeof value === 'string')
+ value = formatInputInt(new BigNumber(value));
+ else
+ value = (+value).toString(16);
+ return padLeft(value, padding);
+};
+
+/// Formats input value to byte representation of string
+/// @returns left-algined byte representation of string
+var formatInputString = function (value) {
+ return web3.fromAscii(value, ETH_PADDING).substr(2);
+};
+
+/// Formats input value to byte representation of bool
+/// @returns right-aligned byte representation bool
+var formatInputBool = function (value) {
+ return '000000000000000000000000000000000000000000000000000000000000000' + (value ? '1' : '0');
+};
+
+var dynamicTypeBytes = function (type, value) {
+ // TODO: decide what to do with array of strings
+ if (arrayType(type) || prefixedType('string')(type))
+ return formatInputInt(value.length);
+ return "";
+};
+
/// Setups input formatters for solidity types
/// @returns an array of input formatters
var setupInputTypes = function () {
- /// Formats input value to byte representation of int
- /// If value is negative, return it's two's complement
- /// If the value is floating point, round it down
- /// @returns right-aligned byte representation of int
- var formatInt = function (value) {
- var padding = ETH_PADDING * 2;
- if (value instanceof BigNumber || typeof value === 'number') {
- if (typeof value === 'number')
- value = new BigNumber(value);
- value = value.round();
-
- if (value.lessThan(0))
- value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(value).plus(1);
- value = value.toString(16);
- }
- else if (value.indexOf('0x') === 0)
- value = value.substr(2);
- else if (typeof value === 'string')
- value = formatInt(new BigNumber(value));
- else
- value = (+value).toString(16);
- return padLeft(value, padding);
- };
-
- /// Formats input value to byte representation of string
- /// @returns left-algined byte representation of string
- var formatString = function (value) {
- return web3.fromAscii(value, ETH_PADDING).substr(2);
- };
-
- /// Formats input value to byte representation of bool
- /// @returns right-aligned byte representation bool
- var formatBool = function (value) {
- return '000000000000000000000000000000000000000000000000000000000000000' + (value ? '1' : '0');
- };
-
return [
- { type: prefixedType('uint'), format: formatInt },
- { type: prefixedType('int'), format: formatInt },
- { type: prefixedType('hash'), format: formatInt },
- { type: prefixedType('string'), format: formatString },
- { type: prefixedType('real'), format: formatInt },
- { type: prefixedType('ureal'), format: formatInt },
- { type: namedType('address'), format: formatInt },
- { type: namedType('bool'), format: formatBool }
+ { type: prefixedType('uint'), format: formatInputInt },
+ { type: prefixedType('int'), format: formatInputInt },
+ { type: prefixedType('hash'), format: formatInputInt },
+ { type: prefixedType('string'), format: formatInputString },
+ { type: prefixedType('real'), format: formatInputInt },
+ { type: prefixedType('ureal'), format: formatInputInt },
+ { type: namedType('address'), format: formatInputInt },
+ { type: namedType('bool'), format: formatInputBool }
];
};
@@ -146,7 +157,12 @@ var toAbiInput = function (json, methodName, params) {
var method = json[index];
var padding = ETH_PADDING * 2;
- for (var i = 0; i < method.inputs.length; i++) {
+ /// first we iterate in search for dynamic
+ method.inputs.forEach(function (input, index) {
+ bytes += dynamicTypeBytes(input.type, params[index]);
+ });
+
+ method.inputs.forEach(function (input, i) {
var typeMatch = false;
for (var j = 0; j < inputTypes.length && !typeMatch; j++) {
typeMatch = inputTypes[j].type(method.inputs[i].type, params[i]);
@@ -156,8 +172,17 @@ var toAbiInput = function (json, methodName, params) {
}
var formatter = inputTypes[j - 1].format;
- bytes += (formatter ? formatter(params[i]) : params[i]);
- }
+ var toAppend = "";
+
+ if (arrayType(method.inputs[i].type))
+ toAppend = params[i].reduce(function (acc, curr) {
+ return acc + formatter(curr);
+ }, "");
+ else
+ toAppend = formatter(params[i]);
+
+ bytes += toAppend;
+ });
return bytes;
};