Merge commit '6a383d6ed3fb37cf6739f6ac441652adfdb70463' into ethereumjs
This commit is contained in:
commit
2ce109eb53
144
dist/ethereum.js
vendored
144
dist/ethereum.js
vendored
@ -33,15 +33,6 @@ BigNumber.config({ ROUNDING_MODE: BigNumber.ROUND_DOWN });
|
|||||||
|
|
||||||
var ETH_PADDING = 32;
|
var ETH_PADDING = 32;
|
||||||
|
|
||||||
// TODO: make these be actually accurate instead of falling back onto JS's doubles.
|
|
||||||
var hexToDec = function (hex) {
|
|
||||||
return parseInt(hex, 16).toString();
|
|
||||||
};
|
|
||||||
|
|
||||||
var decToHex = function (dec) {
|
|
||||||
return parseInt(dec).toString(16);
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Finds first index of array element matching pattern
|
/// Finds first index of array element matching pattern
|
||||||
/// @param array
|
/// @param array
|
||||||
/// @param callback pattern
|
/// @param callback pattern
|
||||||
@ -86,15 +77,15 @@ var namedType = function (name) {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Setups input formatters for solidity types
|
var arrayType = function (type) {
|
||||||
/// @returns an array of input formatters
|
return type.slice(-2) === '[]';
|
||||||
var setupInputTypes = function () {
|
};
|
||||||
|
|
||||||
/// Formats input value to byte representation of int
|
/// Formats input value to byte representation of int
|
||||||
/// If value is negative, return it's two's complement
|
/// If value is negative, return it's two's complement
|
||||||
/// If the value is floating point, round it down
|
/// If the value is floating point, round it down
|
||||||
/// @returns right-aligned byte representation of int
|
/// @returns right-aligned byte representation of int
|
||||||
var formatInt = function (value) {
|
var formatInputInt = function (value) {
|
||||||
var padding = ETH_PADDING * 2;
|
var padding = ETH_PADDING * 2;
|
||||||
if (value instanceof BigNumber || typeof value === 'number') {
|
if (value instanceof BigNumber || typeof value === 'number') {
|
||||||
if (typeof value === 'number')
|
if (typeof value === 'number')
|
||||||
@ -108,7 +99,7 @@ var setupInputTypes = function () {
|
|||||||
else if (value.indexOf('0x') === 0)
|
else if (value.indexOf('0x') === 0)
|
||||||
value = value.substr(2);
|
value = value.substr(2);
|
||||||
else if (typeof value === 'string')
|
else if (typeof value === 'string')
|
||||||
value = formatInt(new BigNumber(value));
|
value = formatInputInt(new BigNumber(value));
|
||||||
else
|
else
|
||||||
value = (+value).toString(16);
|
value = (+value).toString(16);
|
||||||
return padLeft(value, padding);
|
return padLeft(value, padding);
|
||||||
@ -116,25 +107,36 @@ var setupInputTypes = function () {
|
|||||||
|
|
||||||
/// Formats input value to byte representation of string
|
/// Formats input value to byte representation of string
|
||||||
/// @returns left-algined byte representation of string
|
/// @returns left-algined byte representation of string
|
||||||
var formatString = function (value) {
|
var formatInputString = function (value) {
|
||||||
return web3.fromAscii(value, ETH_PADDING).substr(2);
|
return web3.fromAscii(value, ETH_PADDING).substr(2);
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Formats input value to byte representation of bool
|
/// Formats input value to byte representation of bool
|
||||||
/// @returns right-aligned byte representation bool
|
/// @returns right-aligned byte representation bool
|
||||||
var formatBool = function (value) {
|
var formatInputBool = function (value) {
|
||||||
return '000000000000000000000000000000000000000000000000000000000000000' + (value ? '1' : '0');
|
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 () {
|
||||||
|
|
||||||
return [
|
return [
|
||||||
{ type: prefixedType('uint'), format: formatInt },
|
{ type: prefixedType('uint'), format: formatInputInt },
|
||||||
{ type: prefixedType('int'), format: formatInt },
|
{ type: prefixedType('int'), format: formatInputInt },
|
||||||
{ type: prefixedType('hash'), format: formatInt },
|
{ type: prefixedType('hash'), format: formatInputInt },
|
||||||
{ type: prefixedType('string'), format: formatString },
|
{ type: prefixedType('string'), format: formatInputString },
|
||||||
{ type: prefixedType('real'), format: formatInt },
|
{ type: prefixedType('real'), format: formatInputInt },
|
||||||
{ type: prefixedType('ureal'), format: formatInt },
|
{ type: prefixedType('ureal'), format: formatInputInt },
|
||||||
{ type: namedType('address'), format: formatInt },
|
{ type: namedType('address'), format: formatInputInt },
|
||||||
{ type: namedType('bool'), format: formatBool }
|
{ type: namedType('bool'), format: formatInputBool }
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -156,7 +158,12 @@ var toAbiInput = function (json, methodName, params) {
|
|||||||
var method = json[index];
|
var method = json[index];
|
||||||
var padding = ETH_PADDING * 2;
|
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;
|
var typeMatch = false;
|
||||||
for (var j = 0; j < inputTypes.length && !typeMatch; j++) {
|
for (var j = 0; j < inputTypes.length && !typeMatch; j++) {
|
||||||
typeMatch = inputTypes[j].type(method.inputs[i].type, params[i]);
|
typeMatch = inputTypes[j].type(method.inputs[i].type, params[i]);
|
||||||
@ -166,18 +173,23 @@ var toAbiInput = function (json, methodName, params) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var formatter = inputTypes[j - 1].format;
|
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;
|
return bytes;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Setups output formaters for solidity types
|
|
||||||
/// @returns an array of output formatters
|
|
||||||
var setupOutputTypes = function () {
|
|
||||||
|
|
||||||
/// Formats input right-aligned input bytes to int
|
/// Formats input right-aligned input bytes to int
|
||||||
/// @returns right-aligned input bytes formatted to int
|
/// @returns right-aligned input bytes formatted to int
|
||||||
var formatInt = function (value) {
|
var formatOutputInt = function (value) {
|
||||||
// check if it's negative number
|
// check if it's negative number
|
||||||
// it it is, return two's complement
|
// it it is, return two's complement
|
||||||
var firstBit = new BigNumber(value.substr(0, 1), 16).toString(2).substr(0, 1);
|
var firstBit = new BigNumber(value.substr(0, 1), 16).toString(2).substr(0, 1);
|
||||||
@ -189,39 +201,49 @@ var setupOutputTypes = function () {
|
|||||||
|
|
||||||
/// Formats big right-aligned input bytes to uint
|
/// Formats big right-aligned input bytes to uint
|
||||||
/// @returns right-aligned input bytes formatted to uint
|
/// @returns right-aligned input bytes formatted to uint
|
||||||
var formatUInt = function (value) {
|
var formatOutputUInt = function (value) {
|
||||||
return new BigNumber(value, 16);
|
return new BigNumber(value, 16);
|
||||||
};
|
};
|
||||||
|
|
||||||
/// @returns right-aligned input bytes formatted to hex
|
/// @returns right-aligned input bytes formatted to hex
|
||||||
var formatHash = function (value) {
|
var formatOutputHash = function (value) {
|
||||||
return "0x" + value;
|
return "0x" + value;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// @returns right-aligned input bytes formatted to bool
|
/// @returns right-aligned input bytes formatted to bool
|
||||||
var formatBool = function (value) {
|
var formatOutputBool = function (value) {
|
||||||
return value === '0000000000000000000000000000000000000000000000000000000000000001' ? true : false;
|
return value === '0000000000000000000000000000000000000000000000000000000000000001' ? true : false;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// @returns left-aligned input bytes formatted to ascii string
|
/// @returns left-aligned input bytes formatted to ascii string
|
||||||
var formatString = function (value) {
|
var formatOutputString = function (value) {
|
||||||
return web3.toAscii(value);
|
return web3.toAscii(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
/// @returns right-aligned input bytes formatted to address
|
/// @returns right-aligned input bytes formatted to address
|
||||||
var formatAddress = function (value) {
|
var formatOutputAddress = function (value) {
|
||||||
return "0x" + value.slice(value.length - 40, value.length);
|
return "0x" + value.slice(value.length - 40, value.length);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var dynamicBytesLength = function (type) {
|
||||||
|
if (arrayType(type) || prefixedType('string')(type))
|
||||||
|
return ETH_PADDING * 2;
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Setups output formaters for solidity types
|
||||||
|
/// @returns an array of output formatters
|
||||||
|
var setupOutputTypes = function () {
|
||||||
|
|
||||||
return [
|
return [
|
||||||
{ type: prefixedType('uint'), format: formatUInt },
|
{ type: prefixedType('uint'), format: formatOutputUInt },
|
||||||
{ type: prefixedType('int'), format: formatInt },
|
{ type: prefixedType('int'), format: formatOutputInt },
|
||||||
{ type: prefixedType('hash'), format: formatHash },
|
{ type: prefixedType('hash'), format: formatOutputHash },
|
||||||
{ type: prefixedType('string'), format: formatString },
|
{ type: prefixedType('string'), format: formatOutputString },
|
||||||
{ type: prefixedType('real'), format: formatInt },
|
{ type: prefixedType('real'), format: formatOutputInt },
|
||||||
{ type: prefixedType('ureal'), format: formatInt },
|
{ type: prefixedType('ureal'), format: formatOutputInt },
|
||||||
{ type: namedType('address'), format: formatAddress },
|
{ type: namedType('address'), format: formatOutputAddress },
|
||||||
{ type: namedType('bool'), format: formatBool }
|
{ type: namedType('bool'), format: formatOutputBool }
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -244,22 +266,44 @@ var fromAbiOutput = function (json, methodName, output) {
|
|||||||
var result = [];
|
var result = [];
|
||||||
var method = json[index];
|
var method = json[index];
|
||||||
var padding = ETH_PADDING * 2;
|
var padding = ETH_PADDING * 2;
|
||||||
for (var i = 0; i < method.outputs.length; i++) {
|
|
||||||
|
var dynamicPartLength = method.outputs.reduce(function (acc, curr) {
|
||||||
|
return acc + dynamicBytesLength(curr.type);
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
var dynamicPart = output.slice(0, dynamicPartLength);
|
||||||
|
output = output.slice(dynamicPartLength);
|
||||||
|
|
||||||
|
method.outputs.forEach(function (out, i) {
|
||||||
var typeMatch = false;
|
var typeMatch = false;
|
||||||
for (var j = 0; j < outputTypes.length && !typeMatch; j++) {
|
for (var j = 0; j < outputTypes.length && !typeMatch; j++) {
|
||||||
typeMatch = outputTypes[j].type(method.outputs[i].type);
|
typeMatch = outputTypes[j].type(method.outputs[i].type);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!typeMatch) {
|
if (!typeMatch) {
|
||||||
// not found output parsing
|
|
||||||
console.error('output parser does not support type: ' + method.outputs[i].type);
|
console.error('output parser does not support type: ' + method.outputs[i].type);
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
var res = output.slice(0, padding);
|
|
||||||
var formatter = outputTypes[j - 1].format;
|
var formatter = outputTypes[j - 1].format;
|
||||||
result.push(formatter ? formatter(res) : ("0x" + res));
|
if (arrayType(method.outputs[i].type)) {
|
||||||
|
var size = formatOutputUInt(dynamicPart.slice(0, padding));
|
||||||
|
dynamicPart = dynamicPart.slice(padding);
|
||||||
|
var array = [];
|
||||||
|
for (var k = 0; k < size; k++) {
|
||||||
|
array.push(formatter(output.slice(0, padding)));
|
||||||
output = output.slice(padding);
|
output = output.slice(padding);
|
||||||
}
|
}
|
||||||
|
result.push(array);
|
||||||
|
}
|
||||||
|
else if (prefixedType('string')(method.outputs[i].type)) {
|
||||||
|
dynamicPart = dynamicPart.slice(padding);
|
||||||
|
result.push(formatter(output.slice(0, padding)));
|
||||||
|
output = output.slice(padding);
|
||||||
|
} else {
|
||||||
|
result.push(formatter(output.slice(0, padding)));
|
||||||
|
output = output.slice(padding);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
4
dist/ethereum.js.map
vendored
4
dist/ethereum.js.map
vendored
File diff suppressed because one or more lines are too long
2
dist/ethereum.min.js
vendored
2
dist/ethereum.min.js
vendored
File diff suppressed because one or more lines are too long
144
lib/abi.js
144
lib/abi.js
@ -32,15 +32,6 @@ BigNumber.config({ ROUNDING_MODE: BigNumber.ROUND_DOWN });
|
|||||||
|
|
||||||
var ETH_PADDING = 32;
|
var ETH_PADDING = 32;
|
||||||
|
|
||||||
// TODO: make these be actually accurate instead of falling back onto JS's doubles.
|
|
||||||
var hexToDec = function (hex) {
|
|
||||||
return parseInt(hex, 16).toString();
|
|
||||||
};
|
|
||||||
|
|
||||||
var decToHex = function (dec) {
|
|
||||||
return parseInt(dec).toString(16);
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Finds first index of array element matching pattern
|
/// Finds first index of array element matching pattern
|
||||||
/// @param array
|
/// @param array
|
||||||
/// @param callback pattern
|
/// @param callback pattern
|
||||||
@ -85,15 +76,15 @@ var namedType = function (name) {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Setups input formatters for solidity types
|
var arrayType = function (type) {
|
||||||
/// @returns an array of input formatters
|
return type.slice(-2) === '[]';
|
||||||
var setupInputTypes = function () {
|
};
|
||||||
|
|
||||||
/// Formats input value to byte representation of int
|
/// Formats input value to byte representation of int
|
||||||
/// If value is negative, return it's two's complement
|
/// If value is negative, return it's two's complement
|
||||||
/// If the value is floating point, round it down
|
/// If the value is floating point, round it down
|
||||||
/// @returns right-aligned byte representation of int
|
/// @returns right-aligned byte representation of int
|
||||||
var formatInt = function (value) {
|
var formatInputInt = function (value) {
|
||||||
var padding = ETH_PADDING * 2;
|
var padding = ETH_PADDING * 2;
|
||||||
if (value instanceof BigNumber || typeof value === 'number') {
|
if (value instanceof BigNumber || typeof value === 'number') {
|
||||||
if (typeof value === 'number')
|
if (typeof value === 'number')
|
||||||
@ -107,7 +98,7 @@ var setupInputTypes = function () {
|
|||||||
else if (value.indexOf('0x') === 0)
|
else if (value.indexOf('0x') === 0)
|
||||||
value = value.substr(2);
|
value = value.substr(2);
|
||||||
else if (typeof value === 'string')
|
else if (typeof value === 'string')
|
||||||
value = formatInt(new BigNumber(value));
|
value = formatInputInt(new BigNumber(value));
|
||||||
else
|
else
|
||||||
value = (+value).toString(16);
|
value = (+value).toString(16);
|
||||||
return padLeft(value, padding);
|
return padLeft(value, padding);
|
||||||
@ -115,25 +106,36 @@ var setupInputTypes = function () {
|
|||||||
|
|
||||||
/// Formats input value to byte representation of string
|
/// Formats input value to byte representation of string
|
||||||
/// @returns left-algined byte representation of string
|
/// @returns left-algined byte representation of string
|
||||||
var formatString = function (value) {
|
var formatInputString = function (value) {
|
||||||
return web3.fromAscii(value, ETH_PADDING).substr(2);
|
return web3.fromAscii(value, ETH_PADDING).substr(2);
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Formats input value to byte representation of bool
|
/// Formats input value to byte representation of bool
|
||||||
/// @returns right-aligned byte representation bool
|
/// @returns right-aligned byte representation bool
|
||||||
var formatBool = function (value) {
|
var formatInputBool = function (value) {
|
||||||
return '000000000000000000000000000000000000000000000000000000000000000' + (value ? '1' : '0');
|
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 () {
|
||||||
|
|
||||||
return [
|
return [
|
||||||
{ type: prefixedType('uint'), format: formatInt },
|
{ type: prefixedType('uint'), format: formatInputInt },
|
||||||
{ type: prefixedType('int'), format: formatInt },
|
{ type: prefixedType('int'), format: formatInputInt },
|
||||||
{ type: prefixedType('hash'), format: formatInt },
|
{ type: prefixedType('hash'), format: formatInputInt },
|
||||||
{ type: prefixedType('string'), format: formatString },
|
{ type: prefixedType('string'), format: formatInputString },
|
||||||
{ type: prefixedType('real'), format: formatInt },
|
{ type: prefixedType('real'), format: formatInputInt },
|
||||||
{ type: prefixedType('ureal'), format: formatInt },
|
{ type: prefixedType('ureal'), format: formatInputInt },
|
||||||
{ type: namedType('address'), format: formatInt },
|
{ type: namedType('address'), format: formatInputInt },
|
||||||
{ type: namedType('bool'), format: formatBool }
|
{ type: namedType('bool'), format: formatInputBool }
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -155,7 +157,12 @@ var toAbiInput = function (json, methodName, params) {
|
|||||||
var method = json[index];
|
var method = json[index];
|
||||||
var padding = ETH_PADDING * 2;
|
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;
|
var typeMatch = false;
|
||||||
for (var j = 0; j < inputTypes.length && !typeMatch; j++) {
|
for (var j = 0; j < inputTypes.length && !typeMatch; j++) {
|
||||||
typeMatch = inputTypes[j].type(method.inputs[i].type, params[i]);
|
typeMatch = inputTypes[j].type(method.inputs[i].type, params[i]);
|
||||||
@ -165,18 +172,23 @@ var toAbiInput = function (json, methodName, params) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var formatter = inputTypes[j - 1].format;
|
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;
|
return bytes;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Setups output formaters for solidity types
|
|
||||||
/// @returns an array of output formatters
|
|
||||||
var setupOutputTypes = function () {
|
|
||||||
|
|
||||||
/// Formats input right-aligned input bytes to int
|
/// Formats input right-aligned input bytes to int
|
||||||
/// @returns right-aligned input bytes formatted to int
|
/// @returns right-aligned input bytes formatted to int
|
||||||
var formatInt = function (value) {
|
var formatOutputInt = function (value) {
|
||||||
// check if it's negative number
|
// check if it's negative number
|
||||||
// it it is, return two's complement
|
// it it is, return two's complement
|
||||||
var firstBit = new BigNumber(value.substr(0, 1), 16).toString(2).substr(0, 1);
|
var firstBit = new BigNumber(value.substr(0, 1), 16).toString(2).substr(0, 1);
|
||||||
@ -188,39 +200,49 @@ var setupOutputTypes = function () {
|
|||||||
|
|
||||||
/// Formats big right-aligned input bytes to uint
|
/// Formats big right-aligned input bytes to uint
|
||||||
/// @returns right-aligned input bytes formatted to uint
|
/// @returns right-aligned input bytes formatted to uint
|
||||||
var formatUInt = function (value) {
|
var formatOutputUInt = function (value) {
|
||||||
return new BigNumber(value, 16);
|
return new BigNumber(value, 16);
|
||||||
};
|
};
|
||||||
|
|
||||||
/// @returns right-aligned input bytes formatted to hex
|
/// @returns right-aligned input bytes formatted to hex
|
||||||
var formatHash = function (value) {
|
var formatOutputHash = function (value) {
|
||||||
return "0x" + value;
|
return "0x" + value;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// @returns right-aligned input bytes formatted to bool
|
/// @returns right-aligned input bytes formatted to bool
|
||||||
var formatBool = function (value) {
|
var formatOutputBool = function (value) {
|
||||||
return value === '0000000000000000000000000000000000000000000000000000000000000001' ? true : false;
|
return value === '0000000000000000000000000000000000000000000000000000000000000001' ? true : false;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// @returns left-aligned input bytes formatted to ascii string
|
/// @returns left-aligned input bytes formatted to ascii string
|
||||||
var formatString = function (value) {
|
var formatOutputString = function (value) {
|
||||||
return web3.toAscii(value);
|
return web3.toAscii(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
/// @returns right-aligned input bytes formatted to address
|
/// @returns right-aligned input bytes formatted to address
|
||||||
var formatAddress = function (value) {
|
var formatOutputAddress = function (value) {
|
||||||
return "0x" + value.slice(value.length - 40, value.length);
|
return "0x" + value.slice(value.length - 40, value.length);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var dynamicBytesLength = function (type) {
|
||||||
|
if (arrayType(type) || prefixedType('string')(type))
|
||||||
|
return ETH_PADDING * 2;
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Setups output formaters for solidity types
|
||||||
|
/// @returns an array of output formatters
|
||||||
|
var setupOutputTypes = function () {
|
||||||
|
|
||||||
return [
|
return [
|
||||||
{ type: prefixedType('uint'), format: formatUInt },
|
{ type: prefixedType('uint'), format: formatOutputUInt },
|
||||||
{ type: prefixedType('int'), format: formatInt },
|
{ type: prefixedType('int'), format: formatOutputInt },
|
||||||
{ type: prefixedType('hash'), format: formatHash },
|
{ type: prefixedType('hash'), format: formatOutputHash },
|
||||||
{ type: prefixedType('string'), format: formatString },
|
{ type: prefixedType('string'), format: formatOutputString },
|
||||||
{ type: prefixedType('real'), format: formatInt },
|
{ type: prefixedType('real'), format: formatOutputInt },
|
||||||
{ type: prefixedType('ureal'), format: formatInt },
|
{ type: prefixedType('ureal'), format: formatOutputInt },
|
||||||
{ type: namedType('address'), format: formatAddress },
|
{ type: namedType('address'), format: formatOutputAddress },
|
||||||
{ type: namedType('bool'), format: formatBool }
|
{ type: namedType('bool'), format: formatOutputBool }
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -243,22 +265,44 @@ var fromAbiOutput = function (json, methodName, output) {
|
|||||||
var result = [];
|
var result = [];
|
||||||
var method = json[index];
|
var method = json[index];
|
||||||
var padding = ETH_PADDING * 2;
|
var padding = ETH_PADDING * 2;
|
||||||
for (var i = 0; i < method.outputs.length; i++) {
|
|
||||||
|
var dynamicPartLength = method.outputs.reduce(function (acc, curr) {
|
||||||
|
return acc + dynamicBytesLength(curr.type);
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
var dynamicPart = output.slice(0, dynamicPartLength);
|
||||||
|
output = output.slice(dynamicPartLength);
|
||||||
|
|
||||||
|
method.outputs.forEach(function (out, i) {
|
||||||
var typeMatch = false;
|
var typeMatch = false;
|
||||||
for (var j = 0; j < outputTypes.length && !typeMatch; j++) {
|
for (var j = 0; j < outputTypes.length && !typeMatch; j++) {
|
||||||
typeMatch = outputTypes[j].type(method.outputs[i].type);
|
typeMatch = outputTypes[j].type(method.outputs[i].type);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!typeMatch) {
|
if (!typeMatch) {
|
||||||
// not found output parsing
|
|
||||||
console.error('output parser does not support type: ' + method.outputs[i].type);
|
console.error('output parser does not support type: ' + method.outputs[i].type);
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
var res = output.slice(0, padding);
|
|
||||||
var formatter = outputTypes[j - 1].format;
|
var formatter = outputTypes[j - 1].format;
|
||||||
result.push(formatter ? formatter(res) : ("0x" + res));
|
if (arrayType(method.outputs[i].type)) {
|
||||||
|
var size = formatOutputUInt(dynamicPart.slice(0, padding));
|
||||||
|
dynamicPart = dynamicPart.slice(padding);
|
||||||
|
var array = [];
|
||||||
|
for (var k = 0; k < size; k++) {
|
||||||
|
array.push(formatter(output.slice(0, padding)));
|
||||||
output = output.slice(padding);
|
output = output.slice(padding);
|
||||||
}
|
}
|
||||||
|
result.push(array);
|
||||||
|
}
|
||||||
|
else if (prefixedType('string')(method.outputs[i].type)) {
|
||||||
|
dynamicPart = dynamicPart.slice(padding);
|
||||||
|
result.push(formatter(output.slice(0, padding)));
|
||||||
|
output = output.slice(padding);
|
||||||
|
} else {
|
||||||
|
result.push(formatter(output.slice(0, padding)));
|
||||||
|
output = output.slice(padding);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
@ -307,8 +307,14 @@ describe('abi', function() {
|
|||||||
var parser = abi.inputParser(d);
|
var parser = abi.inputParser(d);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assert.equal(parser.test('hello'), "68656c6c6f000000000000000000000000000000000000000000000000000000");
|
assert.equal(
|
||||||
assert.equal(parser.test('world'), "776f726c64000000000000000000000000000000000000000000000000000000");
|
parser.test('hello'),
|
||||||
|
"000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000"
|
||||||
|
);
|
||||||
|
assert.equal(
|
||||||
|
parser.test('world'),
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000005776f726c64000000000000000000000000000000000000000000000000000000"
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should use proper method name', function () {
|
it('should use proper method name', function () {
|
||||||
@ -346,9 +352,34 @@ describe('abi', function() {
|
|||||||
|
|
||||||
//then
|
//then
|
||||||
assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001");
|
assert.equal(parser.test(1), "0000000000000000000000000000000000000000000000000000000000000001");
|
||||||
assert.equal(parser.test2('hello'), "68656c6c6f000000000000000000000000000000000000000000000000000000");
|
assert.equal(
|
||||||
|
parser.test2('hello'),
|
||||||
|
"000000000000000000000000000000000000000000000000000000000000000568656c6c6f000000000000000000000000000000000000000000000000000000"
|
||||||
|
);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should parse input array of ints', function () {
|
||||||
|
|
||||||
|
// given
|
||||||
|
var d = clone(description);
|
||||||
|
|
||||||
|
d[0].inputs = [
|
||||||
|
{ type: "int[]" }
|
||||||
|
];
|
||||||
|
|
||||||
|
// when
|
||||||
|
var parser = abi.inputParser(d);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assert.equal(
|
||||||
|
parser.test([5, 6]),
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000002" +
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000006"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('outputParser', function() {
|
describe('outputParser', function() {
|
||||||
@ -365,8 +396,18 @@ describe('abi', function() {
|
|||||||
var parser = abi.outputParser(d);
|
var parser = abi.outputParser(d);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assert.equal(parser.test("0x68656c6c6f000000000000000000000000000000000000000000000000000000")[0], 'hello');
|
assert.equal(
|
||||||
assert.equal(parser.test("0x776f726c64000000000000000000000000000000000000000000000000000000")[0], 'world');
|
parser.test("0x" +
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||||
|
"68656c6c6f000000000000000000000000000000000000000000000000000000")[0],
|
||||||
|
'hello'
|
||||||
|
);
|
||||||
|
assert.equal(
|
||||||
|
parser.test("0x" +
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||||
|
"776f726c64000000000000000000000000000000000000000000000000000000")[0],
|
||||||
|
'world'
|
||||||
|
);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -613,12 +654,21 @@ describe('abi', function() {
|
|||||||
|
|
||||||
// then
|
// then
|
||||||
assert.equal(
|
assert.equal(
|
||||||
parser.test("0x68656c6c6f000000000000000000000000000000000000000000000000000000776f726c64000000000000000000000000000000000000000000000000000000")[0],
|
parser.test("0x" +
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||||
|
"68656c6c6f000000000000000000000000000000000000000000000000000000" +
|
||||||
|
"776f726c64000000000000000000000000000000000000000000000000000000")[0],
|
||||||
'hello'
|
'hello'
|
||||||
);
|
);
|
||||||
assert.equal(
|
assert.equal(
|
||||||
parser.test("0x68656c6c6f000000000000000000000000000000000000000000000000000000776f726c64000000000000000000000000000000000000000000000000000000")[1],
|
parser.test("0x" +
|
||||||
'world');
|
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||||
|
"68656c6c6f000000000000000000000000000000000000000000000000000000" +
|
||||||
|
"776f726c64000000000000000000000000000000000000000000000000000000")[1],
|
||||||
|
'world'
|
||||||
|
);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -658,7 +708,38 @@ describe('abi', function() {
|
|||||||
|
|
||||||
//then
|
//then
|
||||||
assert.equal(parser.test("0000000000000000000000000000000000000000000000000000000000000001")[0], 1);
|
assert.equal(parser.test("0000000000000000000000000000000000000000000000000000000000000001")[0], 1);
|
||||||
assert.equal(parser.test2("0x68656c6c6f000000000000000000000000000000000000000000000000000000")[0], "hello");
|
assert.equal(parser.test2("0x" +
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||||
|
"68656c6c6f000000000000000000000000000000000000000000000000000000")[0],
|
||||||
|
"hello"
|
||||||
|
);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should parse output array', function () {
|
||||||
|
|
||||||
|
// given
|
||||||
|
var d = clone(description);
|
||||||
|
d[0].outputs = [
|
||||||
|
{ type: 'int[]' }
|
||||||
|
];
|
||||||
|
|
||||||
|
// when
|
||||||
|
var parser = abi.outputParser(d);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assert.equal(parser.test("0x" +
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000002" +
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000006")[0][0],
|
||||||
|
5
|
||||||
|
);
|
||||||
|
assert.equal(parser.test("0x" +
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000002" +
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000005" +
|
||||||
|
"0000000000000000000000000000000000000000000000000000000000000006")[0][1],
|
||||||
|
6
|
||||||
|
);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user