forked from cerc-io/plugeth
formatInput && formatOutput simplified
This commit is contained in:
parent
b20e972bec
commit
589c4fb30f
50
dist/ethereum.js
vendored
50
dist/ethereum.js
vendored
@ -28,6 +28,10 @@ var types = require('./types');
|
||||
var c = require('./const');
|
||||
var f = require('./formatters');
|
||||
|
||||
var displayTypeError = function (type) {
|
||||
console.error('parser does not support type: ' + type);
|
||||
};
|
||||
|
||||
/// This method should be called if we want to check if givent type is an array type
|
||||
/// @returns true if it is, otherwise false
|
||||
var arrayType = function (type) {
|
||||
@ -44,31 +48,31 @@ var dynamicTypeBytes = function (type, value) {
|
||||
var inputTypes = types.inputTypes();
|
||||
|
||||
/// Formats input params to bytes
|
||||
/// @param abi contract method
|
||||
/// @param abi contract method inputs
|
||||
/// @param array of params that will be formatted to bytes
|
||||
/// @returns bytes representation of input params
|
||||
var toAbiInput = function (method, params) {
|
||||
var formatInput = function (inputs, params) {
|
||||
var bytes = "";
|
||||
var padding = c.ETH_PADDING * 2;
|
||||
|
||||
/// first we iterate in search for dynamic
|
||||
method.inputs.forEach(function (input, index) {
|
||||
inputs.forEach(function (input, index) {
|
||||
bytes += dynamicTypeBytes(input.type, params[index]);
|
||||
});
|
||||
|
||||
method.inputs.forEach(function (input, i) {
|
||||
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]);
|
||||
typeMatch = inputTypes[j].type(inputs[i].type, params[i]);
|
||||
}
|
||||
if (!typeMatch) {
|
||||
console.error('input parser does not support type: ' + method.inputs[i].type);
|
||||
displayTypeError(inputs[i].type);
|
||||
}
|
||||
|
||||
var formatter = inputTypes[j - 1].format;
|
||||
var toAppend = "";
|
||||
|
||||
if (arrayType(method.inputs[i].type))
|
||||
if (arrayType(inputs[i].type))
|
||||
toAppend = params[i].reduce(function (acc, curr) {
|
||||
return acc + formatter(curr);
|
||||
}, "");
|
||||
@ -89,34 +93,34 @@ var dynamicBytesLength = function (type) {
|
||||
var outputTypes = types.outputTypes();
|
||||
|
||||
/// Formats output bytes back to param list
|
||||
/// @param contract abi method
|
||||
/// @param contract abi method outputs
|
||||
/// @param bytes representtion of output
|
||||
/// @returns array of output params
|
||||
var fromAbiOutput = function (method, output) {
|
||||
var formatOutput = function (outs, output) {
|
||||
|
||||
output = output.slice(2);
|
||||
var result = [];
|
||||
var padding = c.ETH_PADDING * 2;
|
||||
|
||||
var dynamicPartLength = method.outputs.reduce(function (acc, curr) {
|
||||
var dynamicPartLength = outs.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) {
|
||||
outs.forEach(function (out, i) {
|
||||
var typeMatch = false;
|
||||
for (var j = 0; j < outputTypes.length && !typeMatch; j++) {
|
||||
typeMatch = outputTypes[j].type(method.outputs[i].type);
|
||||
typeMatch = outputTypes[j].type(outs[i].type);
|
||||
}
|
||||
|
||||
if (!typeMatch) {
|
||||
console.error('output parser does not support type: ' + method.outputs[i].type);
|
||||
displayTypeError(outs[i].type);
|
||||
}
|
||||
|
||||
var formatter = outputTypes[j - 1].format;
|
||||
if (arrayType(method.outputs[i].type)) {
|
||||
if (arrayType(outs[i].type)) {
|
||||
var size = f.formatOutputUInt(dynamicPart.slice(0, padding));
|
||||
dynamicPart = dynamicPart.slice(padding);
|
||||
var array = [];
|
||||
@ -126,7 +130,7 @@ var fromAbiOutput = function (method, output) {
|
||||
}
|
||||
result.push(array);
|
||||
}
|
||||
else if (types.prefixedType('string')(method.outputs[i].type)) {
|
||||
else if (types.prefixedType('string')(outs[i].type)) {
|
||||
dynamicPart = dynamicPart.slice(padding);
|
||||
result.push(formatter(output.slice(0, padding)));
|
||||
output = output.slice(padding);
|
||||
@ -150,7 +154,7 @@ var inputParser = function (json) {
|
||||
|
||||
var impl = function () {
|
||||
var params = Array.prototype.slice.call(arguments);
|
||||
return toAbiInput(method, params);
|
||||
return formatInput(method.inputs, params);
|
||||
};
|
||||
|
||||
if (parser[displayName] === undefined) {
|
||||
@ -173,7 +177,7 @@ var outputParser = function (json) {
|
||||
var typeName = utils.extractTypeName(method.name);
|
||||
|
||||
var impl = function (output) {
|
||||
return fromAbiOutput(method, output);
|
||||
return formatOutput(method.outputs, output);
|
||||
};
|
||||
|
||||
if (parser[displayName] === undefined) {
|
||||
@ -195,6 +199,8 @@ var signatureFromAscii = function (name) {
|
||||
module.exports = {
|
||||
inputParser: inputParser,
|
||||
outputParser: outputParser,
|
||||
formatInput: formatInput,
|
||||
formatOutput: formatOutput,
|
||||
signatureFromAscii: signatureFromAscii
|
||||
};
|
||||
|
||||
@ -485,19 +491,15 @@ var inputWithName = function (inputs, name) {
|
||||
var indexedParamsToTopics = function (event, indexed) {
|
||||
// sort keys?
|
||||
return Object.keys(indexed).map(function (key) {
|
||||
// TODO: simplify this!
|
||||
var parser = abi.inputParser([{
|
||||
name: 'test',
|
||||
inputs: [inputWithName(event.inputs, key)]
|
||||
}]);
|
||||
var inputs = [inputWithName(event.inputs, key)];
|
||||
|
||||
var value = indexed[key];
|
||||
if (value instanceof Array) {
|
||||
return value.map(function (v) {
|
||||
return parser.test(v);
|
||||
return abi.formatInput(inputs, [v]);
|
||||
});
|
||||
}
|
||||
return parser.test(value);
|
||||
return abi.formatInput(inputs, [value]);
|
||||
});
|
||||
};
|
||||
|
||||
|
6
dist/ethereum.js.map
vendored
6
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
40
lib/abi.js
40
lib/abi.js
@ -27,6 +27,10 @@ var types = require('./types');
|
||||
var c = require('./const');
|
||||
var f = require('./formatters');
|
||||
|
||||
var displayTypeError = function (type) {
|
||||
console.error('parser does not support type: ' + type);
|
||||
};
|
||||
|
||||
/// This method should be called if we want to check if givent type is an array type
|
||||
/// @returns true if it is, otherwise false
|
||||
var arrayType = function (type) {
|
||||
@ -43,31 +47,31 @@ var dynamicTypeBytes = function (type, value) {
|
||||
var inputTypes = types.inputTypes();
|
||||
|
||||
/// Formats input params to bytes
|
||||
/// @param abi contract method
|
||||
/// @param abi contract method inputs
|
||||
/// @param array of params that will be formatted to bytes
|
||||
/// @returns bytes representation of input params
|
||||
var toAbiInput = function (method, params) {
|
||||
var formatInput = function (inputs, params) {
|
||||
var bytes = "";
|
||||
var padding = c.ETH_PADDING * 2;
|
||||
|
||||
/// first we iterate in search for dynamic
|
||||
method.inputs.forEach(function (input, index) {
|
||||
inputs.forEach(function (input, index) {
|
||||
bytes += dynamicTypeBytes(input.type, params[index]);
|
||||
});
|
||||
|
||||
method.inputs.forEach(function (input, i) {
|
||||
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]);
|
||||
typeMatch = inputTypes[j].type(inputs[i].type, params[i]);
|
||||
}
|
||||
if (!typeMatch) {
|
||||
console.error('input parser does not support type: ' + method.inputs[i].type);
|
||||
displayTypeError(inputs[i].type);
|
||||
}
|
||||
|
||||
var formatter = inputTypes[j - 1].format;
|
||||
var toAppend = "";
|
||||
|
||||
if (arrayType(method.inputs[i].type))
|
||||
if (arrayType(inputs[i].type))
|
||||
toAppend = params[i].reduce(function (acc, curr) {
|
||||
return acc + formatter(curr);
|
||||
}, "");
|
||||
@ -88,34 +92,34 @@ var dynamicBytesLength = function (type) {
|
||||
var outputTypes = types.outputTypes();
|
||||
|
||||
/// Formats output bytes back to param list
|
||||
/// @param contract abi method
|
||||
/// @param contract abi method outputs
|
||||
/// @param bytes representtion of output
|
||||
/// @returns array of output params
|
||||
var fromAbiOutput = function (method, output) {
|
||||
var formatOutput = function (outs, output) {
|
||||
|
||||
output = output.slice(2);
|
||||
var result = [];
|
||||
var padding = c.ETH_PADDING * 2;
|
||||
|
||||
var dynamicPartLength = method.outputs.reduce(function (acc, curr) {
|
||||
var dynamicPartLength = outs.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) {
|
||||
outs.forEach(function (out, i) {
|
||||
var typeMatch = false;
|
||||
for (var j = 0; j < outputTypes.length && !typeMatch; j++) {
|
||||
typeMatch = outputTypes[j].type(method.outputs[i].type);
|
||||
typeMatch = outputTypes[j].type(outs[i].type);
|
||||
}
|
||||
|
||||
if (!typeMatch) {
|
||||
console.error('output parser does not support type: ' + method.outputs[i].type);
|
||||
displayTypeError(outs[i].type);
|
||||
}
|
||||
|
||||
var formatter = outputTypes[j - 1].format;
|
||||
if (arrayType(method.outputs[i].type)) {
|
||||
if (arrayType(outs[i].type)) {
|
||||
var size = f.formatOutputUInt(dynamicPart.slice(0, padding));
|
||||
dynamicPart = dynamicPart.slice(padding);
|
||||
var array = [];
|
||||
@ -125,7 +129,7 @@ var fromAbiOutput = function (method, output) {
|
||||
}
|
||||
result.push(array);
|
||||
}
|
||||
else if (types.prefixedType('string')(method.outputs[i].type)) {
|
||||
else if (types.prefixedType('string')(outs[i].type)) {
|
||||
dynamicPart = dynamicPart.slice(padding);
|
||||
result.push(formatter(output.slice(0, padding)));
|
||||
output = output.slice(padding);
|
||||
@ -149,7 +153,7 @@ var inputParser = function (json) {
|
||||
|
||||
var impl = function () {
|
||||
var params = Array.prototype.slice.call(arguments);
|
||||
return toAbiInput(method, params);
|
||||
return formatInput(method.inputs, params);
|
||||
};
|
||||
|
||||
if (parser[displayName] === undefined) {
|
||||
@ -172,7 +176,7 @@ var outputParser = function (json) {
|
||||
var typeName = utils.extractTypeName(method.name);
|
||||
|
||||
var impl = function (output) {
|
||||
return fromAbiOutput(method, output);
|
||||
return formatOutput(method.outputs, output);
|
||||
};
|
||||
|
||||
if (parser[displayName] === undefined) {
|
||||
@ -194,6 +198,8 @@ var signatureFromAscii = function (name) {
|
||||
module.exports = {
|
||||
inputParser: inputParser,
|
||||
outputParser: outputParser,
|
||||
formatInput: formatInput,
|
||||
formatOutput: formatOutput,
|
||||
signatureFromAscii: signatureFromAscii
|
||||
};
|
||||
|
||||
|
10
lib/event.js
10
lib/event.js
@ -38,19 +38,15 @@ var inputWithName = function (inputs, name) {
|
||||
var indexedParamsToTopics = function (event, indexed) {
|
||||
// sort keys?
|
||||
return Object.keys(indexed).map(function (key) {
|
||||
// TODO: simplify this!
|
||||
var parser = abi.inputParser([{
|
||||
name: 'test',
|
||||
inputs: [inputWithName(event.inputs, key)]
|
||||
}]);
|
||||
var inputs = [inputWithName(event.inputs, key)];
|
||||
|
||||
var value = indexed[key];
|
||||
if (value instanceof Array) {
|
||||
return value.map(function (v) {
|
||||
return parser.test(v);
|
||||
return abi.formatInput(inputs, [v]);
|
||||
});
|
||||
}
|
||||
return parser.test(value);
|
||||
return abi.formatInput(inputs, [value]);
|
||||
});
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user