You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
329 lines
9.4 KiB
329 lines
9.4 KiB
/*
|
|
This file is part of ethereum.js.
|
|
|
|
ethereum.js is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
ethereum.js is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
/**
|
|
* @file coder.js
|
|
* @author Marek Kotewicz <marek@ethdev.com>
|
|
* @date 2015
|
|
*/
|
|
|
|
var BigNumber = require('bignumber.js');
|
|
var utils = require('../utils/utils');
|
|
var f = require('./formatters');
|
|
var SolidityParam = require('./param');
|
|
|
|
/**
|
|
* Should be used to check if a type is an array type
|
|
*
|
|
* @method isArrayType
|
|
* @param {String} type
|
|
* @return {Bool} true is the type is an array, otherwise false
|
|
*/
|
|
var isArrayType = function (type) {
|
|
return type.slice(-2) === '[]';
|
|
};
|
|
|
|
/**
|
|
* SolidityType prototype is used to encode/decode solidity params of certain type
|
|
*/
|
|
var SolidityType = function (config) {
|
|
this._name = config.name;
|
|
this._match = config.match;
|
|
this._mode = config.mode;
|
|
this._inputFormatter = config.inputFormatter;
|
|
this._outputFormatter = config.outputFormatter;
|
|
};
|
|
|
|
/**
|
|
* Should be used to determine if this SolidityType do match given type
|
|
*
|
|
* @method isType
|
|
* @param {String} name
|
|
* @return {Bool} true if type match this SolidityType, otherwise false
|
|
*/
|
|
SolidityType.prototype.isType = function (name) {
|
|
if (this._match === 'strict') {
|
|
return this._name === name || (name.indexOf(this._name) === 0 && name.slice(this._name.length) === '[]');
|
|
} else if (this._match === 'prefix') {
|
|
// TODO better type detection!
|
|
return name.indexOf(this._name) === 0;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Should be used to transform plain param to SolidityParam object
|
|
*
|
|
* @method formatInput
|
|
* @param {Object} param - plain object, or an array of objects
|
|
* @param {Bool} arrayType - true if a param should be encoded as an array
|
|
* @return {SolidityParam} encoded param wrapped in SolidityParam object
|
|
*/
|
|
SolidityType.prototype.formatInput = function (param, arrayType) {
|
|
if (utils.isArray(param) && arrayType) { // TODO: should fail if this two are not the same
|
|
var self = this;
|
|
return param.map(function (p) {
|
|
return self._inputFormatter(p);
|
|
}).reduce(function (acc, current) {
|
|
acc.appendArrayElement(current);
|
|
return acc;
|
|
}, new SolidityParam('', f.formatInputInt(param.length).value));
|
|
}
|
|
return this._inputFormatter(param);
|
|
};
|
|
|
|
/**
|
|
* Should be used to transoform SolidityParam to plain param
|
|
*
|
|
* @method formatOutput
|
|
* @param {SolidityParam} byteArray
|
|
* @param {Bool} arrayType - true if a param should be decoded as an array
|
|
* @return {Object} plain decoded param
|
|
*/
|
|
SolidityType.prototype.formatOutput = function (param, arrayType) {
|
|
if (arrayType) {
|
|
// let's assume, that we solidity will never return long arrays :P
|
|
var result = [];
|
|
var length = new BigNumber(param.prefix, 16);
|
|
for (var i = 0; i < length * 64; i += 64) {
|
|
result.push(this._outputFormatter(new SolidityParam(param.suffix.slice(i, i + 64))));
|
|
}
|
|
return result;
|
|
}
|
|
return this._outputFormatter(param);
|
|
};
|
|
|
|
/**
|
|
* Should be used to check if a type is variadic
|
|
*
|
|
* @method isVariadicType
|
|
* @param {String} type
|
|
* @returns {Bool} true if the type is variadic
|
|
*/
|
|
SolidityType.prototype.isVariadicType = function (type) {
|
|
return isArrayType(type) || this._mode === 'bytes';
|
|
};
|
|
|
|
/**
|
|
* Should be used to shift param from params group
|
|
*
|
|
* @method shiftParam
|
|
* @param {String} type
|
|
* @returns {SolidityParam} shifted param
|
|
*/
|
|
SolidityType.prototype.shiftParam = function (type, param) {
|
|
if (this._mode === 'bytes') {
|
|
return param.shiftBytes();
|
|
} else if (isArrayType(type)) {
|
|
var length = new BigNumber(param.prefix.slice(0, 64), 16);
|
|
return param.shiftArray(length);
|
|
}
|
|
return param.shiftValue();
|
|
};
|
|
|
|
/**
|
|
* SolidityCoder prototype should be used to encode/decode solidity params of any type
|
|
*/
|
|
var SolidityCoder = function (types) {
|
|
this._types = types;
|
|
};
|
|
|
|
/**
|
|
* This method should be used to transform type to SolidityType
|
|
*
|
|
* @method _requireType
|
|
* @param {String} type
|
|
* @returns {SolidityType}
|
|
* @throws {Error} throws if no matching type is found
|
|
*/
|
|
SolidityCoder.prototype._requireType = function (type) {
|
|
var solidityType = this._types.filter(function (t) {
|
|
return t.isType(type);
|
|
})[0];
|
|
|
|
if (!solidityType) {
|
|
throw Error('invalid solidity type!: ' + type);
|
|
}
|
|
|
|
return solidityType;
|
|
};
|
|
|
|
/**
|
|
* Should be used to transform plain bytes to SolidityParam object
|
|
*
|
|
* @method _bytesToParam
|
|
* @param {Array} types of params
|
|
* @param {String} bytes to be transformed to SolidityParam
|
|
* @return {SolidityParam} SolidityParam for this group of params
|
|
*/
|
|
SolidityCoder.prototype._bytesToParam = function (types, bytes) {
|
|
var self = this;
|
|
var prefixTypes = types.reduce(function (acc, type) {
|
|
return self._requireType(type).isVariadicType(type) ? acc + 1 : acc;
|
|
}, 0);
|
|
var valueTypes = types.length - prefixTypes;
|
|
|
|
var prefix = bytes.slice(0, prefixTypes * 64);
|
|
bytes = bytes.slice(prefixTypes * 64);
|
|
var value = bytes.slice(0, valueTypes * 64);
|
|
var suffix = bytes.slice(valueTypes * 64);
|
|
return new SolidityParam(value, prefix, suffix);
|
|
};
|
|
|
|
/**
|
|
* Should be used to transform plain param of given type to SolidityParam
|
|
*
|
|
* @method _formatInput
|
|
* @param {String} type of param
|
|
* @param {Object} plain param
|
|
* @return {SolidityParam}
|
|
*/
|
|
SolidityCoder.prototype._formatInput = function (type, param) {
|
|
return this._requireType(type).formatInput(param, isArrayType(type));
|
|
};
|
|
|
|
/**
|
|
* Should be used to encode plain param
|
|
*
|
|
* @method encodeParam
|
|
* @param {String} type
|
|
* @param {Object} plain param
|
|
* @return {String} encoded plain param
|
|
*/
|
|
SolidityCoder.prototype.encodeParam = function (type, param) {
|
|
return this._formatInput(type, param).encode();
|
|
};
|
|
|
|
/**
|
|
* Should be used to encode list of params
|
|
*
|
|
* @method encodeParams
|
|
* @param {Array} types
|
|
* @param {Array} params
|
|
* @return {String} encoded list of params
|
|
*/
|
|
SolidityCoder.prototype.encodeParams = function (types, params) {
|
|
var self = this;
|
|
return types.map(function (type, index) {
|
|
return self._formatInput(type, params[index]);
|
|
}).reduce(function (acc, solidityParam) {
|
|
acc.append(solidityParam);
|
|
return acc;
|
|
}, new SolidityParam()).encode();
|
|
};
|
|
|
|
/**
|
|
* Should be used to transform SolidityParam to plain param
|
|
*
|
|
* @method _formatOutput
|
|
* @param {String} type
|
|
* @param {SolidityParam} param
|
|
* @return {Object} plain param
|
|
*/
|
|
SolidityCoder.prototype._formatOutput = function (type, param) {
|
|
return this._requireType(type).formatOutput(param, isArrayType(type));
|
|
};
|
|
|
|
/**
|
|
* Should be used to decode bytes to plain param
|
|
*
|
|
* @method decodeParam
|
|
* @param {String} type
|
|
* @param {String} bytes
|
|
* @return {Object} plain param
|
|
*/
|
|
SolidityCoder.prototype.decodeParam = function (type, bytes) {
|
|
return this._formatOutput(type, this._bytesToParam([type], bytes));
|
|
};
|
|
|
|
/**
|
|
* Should be used to decode list of params
|
|
*
|
|
* @method decodeParam
|
|
* @param {Array} types
|
|
* @param {String} bytes
|
|
* @return {Array} array of plain params
|
|
*/
|
|
SolidityCoder.prototype.decodeParams = function (types, bytes) {
|
|
var self = this;
|
|
var param = this._bytesToParam(types, bytes);
|
|
return types.map(function (type) {
|
|
var solidityType = self._requireType(type);
|
|
var p = solidityType.shiftParam(type, param);
|
|
return solidityType.formatOutput(p, isArrayType(type));
|
|
});
|
|
};
|
|
|
|
var coder = new SolidityCoder([
|
|
new SolidityType({
|
|
name: 'address',
|
|
match: 'strict',
|
|
mode: 'value',
|
|
inputFormatter: f.formatInputInt,
|
|
outputFormatter: f.formatOutputAddress
|
|
}),
|
|
new SolidityType({
|
|
name: 'bool',
|
|
match: 'strict',
|
|
mode: 'value',
|
|
inputFormatter: f.formatInputBool,
|
|
outputFormatter: f.formatOutputBool
|
|
}),
|
|
new SolidityType({
|
|
name: 'int',
|
|
match: 'prefix',
|
|
mode: 'value',
|
|
inputFormatter: f.formatInputInt,
|
|
outputFormatter: f.formatOutputInt,
|
|
}),
|
|
new SolidityType({
|
|
name: 'uint',
|
|
match: 'prefix',
|
|
mode: 'value',
|
|
inputFormatter: f.formatInputInt,
|
|
outputFormatter: f.formatOutputUInt
|
|
}),
|
|
new SolidityType({
|
|
name: 'bytes',
|
|
match: 'strict',
|
|
mode: 'bytes',
|
|
inputFormatter: f.formatInputDynamicBytes,
|
|
outputFormatter: f.formatOutputDynamicBytes
|
|
}),
|
|
new SolidityType({
|
|
name: 'bytes',
|
|
match: 'prefix',
|
|
mode: 'value',
|
|
inputFormatter: f.formatInputBytes,
|
|
outputFormatter: f.formatOutputBytes
|
|
}),
|
|
new SolidityType({
|
|
name: 'real',
|
|
match: 'prefix',
|
|
mode: 'value',
|
|
inputFormatter: f.formatInputReal,
|
|
outputFormatter: f.formatOutputReal
|
|
}),
|
|
new SolidityType({
|
|
name: 'ureal',
|
|
match: 'prefix',
|
|
mode: 'value',
|
|
inputFormatter: f.formatInputReal,
|
|
outputFormatter: f.formatOutputUReal
|
|
})
|
|
]);
|
|
|
|
module.exports = coder;
|
|
|
|
|