Merge branch 'abi_refactor' into develop

This commit is contained in:
debris 2015-08-03 15:16:22 +02:00
commit 9f5f20152f
20 changed files with 2517 additions and 942 deletions

1033
dist/web3-light.js vendored

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

1035
dist/web3.js vendored

File diff suppressed because it is too large Load Diff

85
dist/web3.js.map vendored

File diff suppressed because one or more lines are too long

7
dist/web3.min.js vendored

File diff suppressed because one or more lines are too long

31
lib/solidity/address.js Normal file
View File

@ -0,0 +1,31 @@
var f = require('./formatters');
var SolidityType = require('./type');
/**
* SolidityTypeAddress is a prootype that represents address type
* It matches:
* address
* address[]
* address[4]
* address[][]
* address[3][]
* address[][6][], ...
*/
var SolidityTypeAddress = function () {
this._inputFormatter = f.formatInputInt;
this._outputFormatter = f.formatOutputAddress;
};
SolidityTypeAddress.prototype = new SolidityType({});
SolidityTypeAddress.prototype.constructor = SolidityTypeAddress;
SolidityTypeAddress.prototype.isType = function (name) {
return !!name.match(/address(\[([0-9]*)\])?/);
};
SolidityTypeAddress.prototype.staticPartLength = function (name) {
return 32 * this.staticArrayLength(name);
};
module.exports = SolidityTypeAddress;

30
lib/solidity/bool.js Normal file
View File

@ -0,0 +1,30 @@
var f = require('./formatters');
var SolidityType = require('./type');
/**
* SolidityTypeBool is a prootype that represents bool type
* It matches:
* bool
* bool[]
* bool[4]
* bool[][]
* bool[3][]
* bool[][6][], ...
*/
var SolidityTypeBool = function () {
this._inputFormatter = f.formatInputBool;
this._outputFormatter = f.formatOutputBool;
};
SolidityTypeBool.prototype = new SolidityType({});
SolidityTypeBool.prototype.constructor = SolidityTypeBool;
SolidityTypeBool.prototype.isType = function (name) {
return !!name.match(/^bool(\[([0-9]*)\])*$/);
};
SolidityTypeBool.prototype.staticPartLength = function (name) {
return 32 * this.staticArrayLength(name);
};
module.exports = SolidityTypeBool;

38
lib/solidity/bytes.js Normal file
View File

@ -0,0 +1,38 @@
var f = require('./formatters');
var SolidityType = require('./type');
/**
* SolidityTypeBytes is a prootype that represents bytes type
* It matches:
* bytes
* bytes[]
* bytes[4]
* bytes[][]
* bytes[3][]
* bytes[][6][], ...
* bytes32
* bytes64[]
* bytes8[4]
* bytes256[][]
* bytes[3][]
* bytes64[][6][], ...
*/
var SolidityTypeBytes = function () {
this._inputFormatter = f.formatInputBytes;
this._outputFormatter = f.formatOutputBytes;
};
SolidityTypeBytes.prototype = new SolidityType({});
SolidityTypeBytes.prototype.constructor = SolidityTypeBytes;
SolidityTypeBytes.prototype.isType = function (name) {
return !!name.match(/^bytes([0-9]{1,})(\[([0-9]*)\])*$/);
};
SolidityTypeBytes.prototype.staticPartLength = function (name) {
var matches = name.match(/^bytes([0-9]*)/);
var size = parseInt(matches[1]);
return size * this.staticArrayLength(name);
};
module.exports = SolidityTypeBytes;

View File

@ -20,107 +20,17 @@
* @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) {
return acc.combine(current);
}, f.formatInputInt(param.length)).withOffset(32);
}
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.dynamicPart().slice(0, 64), 16);
for (var i = 0; i < length * 64; i += 64) {
result.push(this._outputFormatter(new SolidityParam(param.dynamicPart().substr(i + 64, 64))));
}
return result;
}
return this._outputFormatter(param);
};
/**
* Should be used to slice single param from bytes
*
* @method sliceParam
* @param {String} bytes
* @param {Number} index of param to slice
* @param {String} type
* @returns {SolidityParam} param
*/
SolidityType.prototype.sliceParam = function (bytes, index, type) {
if (this._mode === 'bytes') {
return SolidityParam.decodeBytes(bytes, index);
} else if (isArrayType(type)) {
return SolidityParam.decodeArray(bytes, index);
}
return SolidityParam.decodeParam(bytes, index);
};
var SolidityTypeAddress = require('./address');
var SolidityTypeBool = require('./bool');
var SolidityTypeInt = require('./int');
var SolidityTypeUInt = require('./uint');
var SolidityTypeDynamicBytes = require('./dynamicbytes');
var SolidityTypeString = require('./string');
var SolidityTypeReal = require('./real');
var SolidityTypeUReal = require('./ureal');
var SolidityTypeBytes = require('./bytes');
/**
* SolidityCoder prototype should be used to encode/decode solidity params of any type
@ -149,18 +59,6 @@ SolidityCoder.prototype._requireType = function (type) {
return solidityType;
};
/**
* 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
*
@ -170,7 +68,7 @@ SolidityCoder.prototype._formatInput = function (type, param) {
* @return {String} encoded plain param
*/
SolidityCoder.prototype.encodeParam = function (type, param) {
return this._formatInput(type, param).encode();
return this.encodeParams([type], [param]);
};
/**
@ -182,12 +80,112 @@ SolidityCoder.prototype.encodeParam = function (type, param) {
* @return {String} encoded list of params
*/
SolidityCoder.prototype.encodeParams = function (types, params) {
var self = this;
var solidityParams = types.map(function (type, index) {
return self._formatInput(type, params[index]);
var solidityTypes = this.getSolidityTypes(types);
var encodeds = solidityTypes.map(function (solidityType, index) {
return solidityType.encode(params[index], types[index]);
});
return SolidityParam.encodeList(solidityParams);
var dynamicOffset = solidityTypes.reduce(function (acc, solidityType, index) {
return acc + solidityType.staticPartLength(types[index]);
}, 0);
var result = this.encodeMultiWithOffset(types, solidityTypes, encodeds, dynamicOffset);
return result;
};
SolidityCoder.prototype.encodeMultiWithOffset = function (types, solidityTypes, encodeds, dynamicOffset) {
var result = "";
var self = this;
var isDynamic = function (i) {
return solidityTypes[i].isDynamicArray(types[i]) || solidityTypes[i].isDynamicType(types[i]);
};
types.forEach(function (type, i) {
if (isDynamic(i)) {
result += f.formatInputInt(dynamicOffset).encode();
var e = self.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset);
dynamicOffset += e.length / 2;
} else {
// don't add length to dynamicOffset. it's already counted
result += self.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset);
}
// TODO: figure out nested arrays
});
types.forEach(function (type, i) {
if (isDynamic(i)) {
var e = self.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset);
dynamicOffset += e.length / 2;
result += e;
}
});
return result;
};
// TODO: refactor whole encoding!
SolidityCoder.prototype.encodeWithOffset = function (type, solidityType, encoded, offset) {
var self = this;
if (solidityType.isDynamicArray(type)) {
return (function () {
// offset was already set
var nestedName = solidityType.nestedName(type);
var nestedStaticPartLength = solidityType.staticPartLength(nestedName);
var result = encoded[0];
(function () {
var previousLength = 2; // in int
if (solidityType.isDynamicArray(nestedName)) {
for (var i = 1; i < encoded.length; i++) {
previousLength += +(encoded[i - 1])[0] || 0;
result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode();
}
}
})();
// first element is length, skip it
(function () {
for (var i = 0; i < encoded.length - 1; i++) {
var additionalOffset = result / 2;
result += self.encodeWithOffset(nestedName, solidityType, encoded[i + 1], offset + additionalOffset);
}
})();
return result;
})();
} else if (solidityType.isStaticArray(type)) {
return (function () {
var nestedName = solidityType.nestedName(type);
var nestedStaticPartLength = solidityType.staticPartLength(nestedName);
var result = "";
(function () {
var previousLength = 0; // in int
if (solidityType.isDynamicArray(nestedName)) {
for (var i = 0; i < encoded.length; i++) {
// calculate length of previous item
previousLength += +(encoded[i - 1] || [])[0] || 0;
result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode();
}
}
})();
(function () {
for (var i = 0; i < encoded.length; i++) {
var additionalOffset = result / 2;
result += self.encodeWithOffset(nestedName, solidityType, encoded[i], offset + additionalOffset);
}
})();
return result;
})();
}
return encoded;
};
/**
@ -211,78 +209,49 @@ SolidityCoder.prototype.decodeParam = function (type, bytes) {
* @return {Array} array of plain params
*/
SolidityCoder.prototype.decodeParams = function (types, bytes) {
var solidityTypes = this.getSolidityTypes(types);
var offsets = this.getOffsets(types, solidityTypes);
return solidityTypes.map(function (solidityType, index) {
return solidityType.decode(bytes, offsets[index], types[index], index);
});
};
SolidityCoder.prototype.getOffsets = function (types, solidityTypes) {
var lengths = solidityTypes.map(function (solidityType, index) {
return solidityType.staticPartLength(types[index]);
// get length
});
for (var i = 0; i < lengths.length; i++) {
// sum with length of previous element
var previous = (lengths[i - 1] || 0);
lengths[i] += previous;
}
return lengths.map(function (length, index) {
// remove the current length, so the length is sum of previous elements
return length - solidityTypes[index].staticPartLength(types[index]);
});
};
SolidityCoder.prototype.getSolidityTypes = function (types) {
var self = this;
return types.map(function (type, index) {
var solidityType = self._requireType(type);
var p = solidityType.sliceParam(bytes, index, type);
return solidityType.formatOutput(p, isArrayType(type));
return types.map(function (type) {
return self._requireType(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: 'string',
match: 'strict',
mode: 'bytes',
inputFormatter: f.formatInputString,
outputFormatter: f.formatOutputString
}),
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
})
new SolidityTypeAddress(),
new SolidityTypeBool(),
new SolidityTypeInt(),
new SolidityTypeUInt(),
new SolidityTypeDynamicBytes(),
new SolidityTypeBytes(),
new SolidityTypeString(),
new SolidityTypeReal(),
new SolidityTypeUReal()
]);
module.exports = coder;

View File

@ -0,0 +1,25 @@
var f = require('./formatters');
var SolidityType = require('./type');
var SolidityTypeDynamicBytes = function () {
this._inputFormatter = f.formatInputDynamicBytes;
this._outputFormatter = f.formatOutputDynamicBytes;
};
SolidityTypeDynamicBytes.prototype = new SolidityType({});
SolidityTypeDynamicBytes.prototype.constructor = SolidityTypeDynamicBytes;
SolidityTypeDynamicBytes.prototype.isType = function (name) {
return !!name.match(/^bytes(\[([0-9]*)\])*$/);
};
SolidityTypeDynamicBytes.prototype.staticPartLength = function (name) {
return 32 * this.staticArrayLength(name);
};
SolidityTypeDynamicBytes.prototype.isDynamicType = function () {
return true;
};
module.exports = SolidityTypeDynamicBytes;

View File

@ -36,9 +36,8 @@ var SolidityParam = require('./param');
* @returns {SolidityParam}
*/
var formatInputInt = function (value) {
var padding = c.ETH_PADDING * 2;
BigNumber.config(c.ETH_BIGNUMBER_ROUNDING_MODE);
var result = utils.padLeft(utils.toTwosComplement(value).round().toString(16), padding);
var result = utils.padLeft(utils.toTwosComplement(value).round().toString(16), 64);
return new SolidityParam(result);
};
@ -50,7 +49,9 @@ var formatInputInt = function (value) {
* @returns {SolidityParam}
*/
var formatInputBytes = function (value) {
var result = utils.padRight(utils.toHex(value).substr(2), 64);
var result = utils.toHex(value).substr(2);
var l = Math.floor((result.length + 63) / 64);
result = utils.padRight(result, l * 64);
return new SolidityParam(result);
};
@ -66,7 +67,7 @@ var formatInputDynamicBytes = function (value) {
var length = result.length / 2;
var l = Math.floor((result.length + 63) / 64);
result = utils.padRight(result, l * 64);
return new SolidityParam(formatInputInt(length).value + result, 32);
return new SolidityParam(formatInputInt(length).value + result);
};
/**
@ -81,7 +82,7 @@ var formatInputString = function (value) {
var length = result.length / 2;
var l = Math.floor((result.length + 63) / 64);
result = utils.padRight(result, l * 64);
return new SolidityParam(formatInputInt(length).value + result, 32);
return new SolidityParam(formatInputInt(length).value + result);
};
/**

36
lib/solidity/int.js Normal file
View File

@ -0,0 +1,36 @@
var f = require('./formatters');
var SolidityType = require('./type');
/**
* SolidityTypeInt is a prootype that represents int type
* It matches:
* int
* int[]
* int[4]
* int[][]
* int[3][]
* int[][6][], ...
* int32
* int64[]
* int8[4]
* int256[][]
* int[3][]
* int64[][6][], ...
*/
var SolidityTypeInt = function () {
this._inputFormatter = f.formatInputInt;
this._outputFormatter = f.formatOutputInt;
};
SolidityTypeInt.prototype = new SolidityType({});
SolidityTypeInt.prototype.constructor = SolidityTypeInt;
SolidityTypeInt.prototype.isType = function (name) {
return !!name.match(/^int([0-9]*)?(\[([0-9]*)\])*$/);
};
SolidityTypeInt.prototype.staticPartLength = function (name) {
return 32 * this.staticArrayLength(name);
};
module.exports = SolidityTypeInt;

View File

@ -72,7 +72,7 @@ SolidityParam.prototype.combine = function (param) {
* @returns {Boolean}
*/
SolidityParam.prototype.isDynamic = function () {
return this.value.length > 64 || this.offset !== undefined;
return this.offset !== undefined;
};
/**
@ -146,66 +146,7 @@ SolidityParam.encodeList = function (params) {
}, ''));
};
/**
* This method should be used to decode plain (static) solidity param at given index
*
* @method decodeParam
* @param {String} bytes
* @param {Number} index
* @returns {SolidityParam}
*/
SolidityParam.decodeParam = function (bytes, index) {
index = index || 0;
return new SolidityParam(bytes.substr(index * 64, 64));
};
/**
* This method should be called to get offset value from bytes at given index
*
* @method getOffset
* @param {String} bytes
* @param {Number} index
* @returns {Number} offset as number
*/
var getOffset = function (bytes, index) {
// we can do this cause offset is rather small
return parseInt('0x' + bytes.substr(index * 64, 64));
};
/**
* This method should be called to decode solidity bytes param at given index
*
* @method decodeBytes
* @param {String} bytes
* @param {Number} index
* @returns {SolidityParam}
*/
SolidityParam.decodeBytes = function (bytes, index) {
index = index || 0;
var offset = getOffset(bytes, index);
var l = parseInt('0x' + bytes.substr(offset * 2, 64));
l = Math.floor((l + 31) / 32);
// (1 + l) * , cause we also parse length
return new SolidityParam(bytes.substr(offset * 2, (1 + l) * 64), 0);
};
/**
* This method should be used to decode solidity array at given index
*
* @method decodeArray
* @param {String} bytes
* @param {Number} index
* @returns {SolidityParam}
*/
SolidityParam.decodeArray = function (bytes, index) {
index = index || 0;
var offset = getOffset(bytes, index);
var length = parseInt('0x' + bytes.substr(offset * 2, 64));
return new SolidityParam(bytes.substr(offset * 2, (length + 1) * 64), 0);
};
module.exports = SolidityParam;

36
lib/solidity/real.js Normal file
View File

@ -0,0 +1,36 @@
var f = require('./formatters');
var SolidityType = require('./type');
/**
* SolidityTypeReal is a prootype that represents real type
* It matches:
* real
* real[]
* real[4]
* real[][]
* real[3][]
* real[][6][], ...
* real32
* real64[]
* real8[4]
* real256[][]
* real[3][]
* real64[][6][], ...
*/
var SolidityTypeReal = function () {
this._inputFormatter = f.formatInputReal;
this._outputFormatter = f.formatOutputReal;
};
SolidityTypeReal.prototype = new SolidityType({});
SolidityTypeReal.prototype.constructor = SolidityTypeReal;
SolidityTypeReal.prototype.isType = function (name) {
return !!name.match(/real([0-9]*)?(\[([0-9]*)\])?/);
};
SolidityTypeReal.prototype.staticPartLength = function (name) {
return 32 * this.staticArrayLength(name);
};
module.exports = SolidityTypeReal;

25
lib/solidity/string.js Normal file
View File

@ -0,0 +1,25 @@
var f = require('./formatters');
var SolidityType = require('./type');
var SolidityTypeString = function () {
this._inputFormatter = f.formatInputString;
this._outputFormatter = f.formatOutputString;
};
SolidityTypeString.prototype = new SolidityType({});
SolidityTypeString.prototype.constructor = SolidityTypeString;
SolidityTypeString.prototype.isType = function (name) {
return !!name.match(/^string(\[([0-9]*)\])*$/);
};
SolidityTypeString.prototype.staticPartLength = function (name) {
return 32 * this.staticArrayLength(name);
};
SolidityTypeString.prototype.isDynamicType = function () {
return true;
};
module.exports = SolidityTypeString;

243
lib/solidity/type.js Normal file
View File

@ -0,0 +1,243 @@
var f = require('./formatters');
var SolidityParam = require('./param');
/**
* SolidityType prototype is used to encode/decode solidity params of certain type
*/
var SolidityType = function (config) {
this._inputFormatter = config.inputFormatter;
this._outputFormatter = config.outputFormatter;
};
/**
* Should be used to determine if this SolidityType do match given name
*
* @method isType
* @param {String} name
* @return {Bool} true if type match this SolidityType, otherwise false
*/
SolidityType.prototype.isType = function (name) {
throw "this method should be overrwritten for type " + name;
};
/**
* Should be used to determine what is the length of static part in given type
*
* @method staticPartLength
* @param {String} name
* @return {Number} length of static part in bytes
*/
SolidityType.prototype.staticPartLength = function (name) {
throw "this method should be overrwritten for type: " + name;
};
/**
* Should be used to determine if type is dynamic array
* eg:
* "type[]" => true
* "type[4]" => false
*
* @method isDynamicArray
* @param {String} name
* @return {Bool} true if the type is dynamic array
*/
SolidityType.prototype.isDynamicArray = function (name) {
var nestedTypes = this.nestedTypes(name);
return !!nestedTypes && !nestedTypes[nestedTypes.length - 1].match(/[0-9]{1,}/g);
};
/**
* Should be used to determine if type is static array
* eg:
* "type[]" => false
* "type[4]" => true
*
* @method isStaticArray
* @param {String} name
* @return {Bool} true if the type is static array
*/
SolidityType.prototype.isStaticArray = function (name) {
var nestedTypes = this.nestedTypes(name);
return !!nestedTypes && !!nestedTypes[nestedTypes.length - 1].match(/[0-9]{1,}/g);
};
/**
* Should return length of static array
* eg.
* "int[32]" => 32
* "int256[14]" => 14
* "int[2][3]" => 3
* "int" => 1
* "int[1]" => 1
* "int[]" => 1
*
* @method staticArrayLength
* @param {String} name
* @return {Number} static array length
*/
SolidityType.prototype.staticArrayLength = function (name) {
var nestedTypes = this.nestedTypes(name);
if (nestedTypes) {
return parseInt(nestedTypes[nestedTypes.length - 1].match(/[0-9]{1,}/g) || 1);
}
return 1;
};
/**
* Should return nested type
* eg.
* "int[32]" => "int"
* "int256[14]" => "int256"
* "int[2][3]" => "int[2]"
* "int" => "int"
* "int[]" => "int"
*
* @method nestedName
* @param {String} name
* @return {String} nested name
*/
SolidityType.prototype.nestedName = function (name) {
// remove last [] in name
var nestedTypes = this.nestedTypes(name);
if (!nestedTypes) {
return name;
}
return name.substr(0, name.length - nestedTypes[nestedTypes.length - 1].length);
};
/**
* Should return true if type has dynamic size by default
* such types are "string", "bytes"
*
* @method isDynamicType
* @param {String} name
* @return {Bool} true if is dynamic, otherwise false
*/
SolidityType.prototype.isDynamicType = function () {
return false;
};
/**
* Should return array of nested types
* eg.
* "int[2][3][]" => ["[2]", "[3]", "[]"]
* "int[] => ["[]"]
* "int" => null
*
* @method nestedTypes
* @param {String} name
* @return {Array} array of nested types
*/
SolidityType.prototype.nestedTypes = function (name) {
// return list of strings eg. "[]", "[3]", "[]", "[2]"
return name.match(/(\[[0-9]*\])/g);
};
/**
* Should be used to encode the value
*
* @method encode
* @param {Object} value
* @param {String} name
* @return {String} encoded value
*/
SolidityType.prototype.encode = function (value, name) {
var self = this;
if (this.isDynamicArray(name)) {
return (function () {
var length = value.length; // in int
var nestedName = self.nestedName(name);
var result = [];
result.push(f.formatInputInt(length).encode());
value.forEach(function (v) {
result.push(self.encode(v, nestedName));
});
return result;
})();
} else if (this.isStaticArray(name)) {
return (function () {
var length = self.staticArrayLength(name); // in int
var nestedName = self.nestedName(name);
var result = [];
for (var i = 0; i < length; i++) {
result.push(self.encode(value[i], nestedName));
}
return result;
})();
}
return this._inputFormatter(value, name).encode();
};
/**
* Should be used to decode value from bytes
*
* @method decode
* @param {String} bytes
* @param {Number} offset in bytes
* @param {String} name type name
* @returns {Object} decoded value
*/
SolidityType.prototype.decode = function (bytes, offset, name) {
var self = this;
if (this.isDynamicArray(name)) {
return (function () {
var arrayOffset = parseInt('0x' + bytes.substr(offset * 2, 64)); // in bytes
var length = parseInt('0x' + bytes.substr(arrayOffset * 2, 64)); // in int
var arrayStart = arrayOffset + 32; // array starts after length; // in bytes
var nestedName = self.nestedName(name);
var nestedStaticPartLength = self.staticPartLength(nestedName); // in bytes
var result = [];
for (var i = 0; i < length * nestedStaticPartLength; i += nestedStaticPartLength) {
result.push(self.decode(bytes, arrayStart + i, nestedName));
}
return result;
})();
} else if (this.isStaticArray(name)) {
return (function () {
var length = self.staticArrayLength(name); // in int
var arrayStart = offset; // in bytes
var nestedName = self.nestedName(name);
var nestedStaticPartLength = self.staticPartLength(nestedName); // in bytes
var result = [];
for (var i = 0; i < length * nestedStaticPartLength; i += nestedStaticPartLength) {
result.push(self.decode(bytes, arrayStart + i, nestedName));
}
return result;
})();
} else if (this.isDynamicType(name)) {
return (function () {
var dynamicOffset = parseInt('0x' + bytes.substr(offset * 2, 64)); // in bytes
var length = parseInt('0x' + bytes.substr(dynamicOffset * 2, 64)); // in bytes
var roundedLength = Math.floor((length + 31) / 32); // in int
return self._outputFormatter(new SolidityParam(bytes.substr(dynamicOffset * 2, ( 1 + roundedLength) * 64), 0));
})();
}
var length = this.staticPartLength(name);
return this._outputFormatter(new SolidityParam(bytes.substr(offset * 2, length * 2)));
};
module.exports = SolidityType;

36
lib/solidity/uint.js Normal file
View File

@ -0,0 +1,36 @@
var f = require('./formatters');
var SolidityType = require('./type');
/**
* SolidityTypeUInt is a prootype that represents uint type
* It matches:
* uint
* uint[]
* uint[4]
* uint[][]
* uint[3][]
* uint[][6][], ...
* uint32
* uint64[]
* uint8[4]
* uint256[][]
* uint[3][]
* uint64[][6][], ...
*/
var SolidityTypeUInt = function () {
this._inputFormatter = f.formatInputInt;
this._outputFormatter = f.formatOutputInt;
};
SolidityTypeUInt.prototype = new SolidityType({});
SolidityTypeUInt.prototype.constructor = SolidityTypeUInt;
SolidityTypeUInt.prototype.isType = function (name) {
return !!name.match(/^uint([0-9]*)?(\[([0-9]*)\])*$/);
};
SolidityTypeUInt.prototype.staticPartLength = function (name) {
return 32 * this.staticArrayLength(name);
};
module.exports = SolidityTypeUInt;

36
lib/solidity/ureal.js Normal file
View File

@ -0,0 +1,36 @@
var f = require('./formatters');
var SolidityType = require('./type');
/**
* SolidityTypeUReal is a prootype that represents ureal type
* It matches:
* ureal
* ureal[]
* ureal[4]
* ureal[][]
* ureal[3][]
* ureal[][6][], ...
* ureal32
* ureal64[]
* ureal8[4]
* ureal256[][]
* ureal[3][]
* ureal64[][6][], ...
*/
var SolidityTypeUReal = function () {
this._inputFormatter = f.formatInputReal;
this._outputFormatter = f.formatOutputUReal;
};
SolidityTypeUReal.prototype = new SolidityType({});
SolidityTypeUReal.prototype.constructor = SolidityTypeUReal;
SolidityTypeUReal.prototype.isType = function (name) {
return !!name.match(/^ureal([0-9]*)?(\[([0-9]*)\])*$/);
};
SolidityTypeUReal.prototype.staticPartLength = function (name) {
return 32 * this.staticArrayLength(name);
};
module.exports = SolidityTypeUReal;

View File

@ -14,6 +14,59 @@ describe('lib/solidity/coder', function () {
};
test({ type: 'address', expected: '0x407d73d8a49eeb85d32cf465507dd71d507100c1',
value: '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1'});
test({ type: 'address[2]', expected: ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c3'],
value: '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' });
test({ type: 'address[]', expected: ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c3'],
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' });
test({ type: 'address[][2]', expected: [['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c2'],
['0x407d73d8a49eeb85d32cf465507dd71d507100c3', '0x407d73d8a49eeb85d32cf465507dd71d507100c4']],
value: '0000000000000000000000000000000000000000000000000000000000000040' +
'00000000000000000000000000000000000000000000000000000000000000a0' +
'0000000000000000000000000000000000000000000000000000000000000002' + /* 40 */
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' + /* 60 */
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c2' +
'0000000000000000000000000000000000000000000000000000000000000002' + /* a0 */
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c4' });
test({ type: 'address[2][]', expected: [['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c2'],
['0x407d73d8a49eeb85d32cf465507dd71d507100c3', '0x407d73d8a49eeb85d32cf465507dd71d507100c4']],
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000002' + /* 20 */
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c2' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c4' });
test({ type: 'address[][]', expected: [['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c2'],
['0x407d73d8a49eeb85d32cf465507dd71d507100c3', '0x407d73d8a49eeb85d32cf465507dd71d507100c4']],
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000002' + /* 20 */
'0000000000000000000000000000000000000000000000000000000000000080' +
'00000000000000000000000000000000000000000000000000000000000000e0' +
'0000000000000000000000000000000000000000000000000000000000000002' + /* 80 */
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' + /* a0 */
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c2' +
'0000000000000000000000000000000000000000000000000000000000000002' + /* e0 */
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c4' });
test({ type: 'bool', expected: true, value: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'bool', expected: false, value: '0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'bool[2]', expected: [true, false],
value: '0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'bool[]', expected: [true, true, false],
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'int', expected: new bn(1), value: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'int', expected: new bn(1), value: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'int', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'int', expected: new bn(-1), value: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
@ -24,24 +77,72 @@ describe('lib/solidity/coder', function () {
test({ type: 'int32', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'int64', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'int128', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'bytes32', expected: '0x6761766f66796f726b0000000000000000000000000000000000000000000000',
value: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ type: 'int[]', expected: [], value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'int[]', expected: [new bn(3)], value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ type: 'int256[]', expected: [new bn(3)], value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ type: 'int[]', expected: [new bn(1), new bn(2), new bn(3)],
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ type: 'int[3][]', expected: [[new bn(1), new bn(2), new bn(3)], [new bn(4), new bn(5), new bn(6)]],
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000004' +
'0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000006'});
test({ type: 'uint', expected: new bn(1), value: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'uint', expected: new bn(1), value: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'uint', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'uint', expected: new bn(-1), value: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
test({ type: 'uint256', expected: new bn(1), value: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'uint256', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'uint256', expected: new bn(-1), value: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
test({ type: 'uint8', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'uint32', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'uint64', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'uint128', expected: new bn(16), value: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'uint[]', expected: [], value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'uint[]', expected: [new bn(3)], value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ type: 'uint256[]', expected: [new bn(3)], value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ type: 'uint[]', expected: [new bn(1), new bn(2), new bn(3)],
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ type: 'uint[3][]', expected: [[new bn(1), new bn(2), new bn(3)], [new bn(4), new bn(5), new bn(6)]],
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000004' +
'0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000006'});
test({ type: 'bytes', expected: '0x6761766f66796f726b',
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ type: 'bytes32', expected: '0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
value: '731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
test({ type: 'bytes', expected: '0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000020' +
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
test({ type: 'bytes', expected: '0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000040' +
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
test({ type: 'bytes', expected: '0x131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
@ -50,6 +151,52 @@ describe('lib/solidity/coder', function () {
'131a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'231a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'331a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
test({ type: 'bytes', expected: '0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000040' +
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
test({ type: 'bytes[2]', expected: ['0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134a',
'0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'],
value: '0000000000000000000000000000000000000000000000000000000000000040' +
'0000000000000000000000000000000000000000000000000000000000000080' +
'0000000000000000000000000000000000000000000000000000000000000020' +
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134a' +
'0000000000000000000000000000000000000000000000000000000000000020' +
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b'});
test({ type: 'bytes[][2]', expected: [['0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134a'],
['0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134c',
'0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134d']],
value: '0000000000000000000000000000000000000000000000000000000000000040' +
'0000000000000000000000000000000000000000000000000000000000000080' +
'0000000000000000000000000000000000000000000000000000000000000001' + // 40 //
'00000000000000000000000000000000000000000000000000000000000000e0' +
'0000000000000000000000000000000000000000000000000000000000000002' + // 80 //
'0000000000000000000000000000000000000000000000000000000000000120' +
'0000000000000000000000000000000000000000000000000000000000000180' +
'0000000000000000000000000000000000000000000000000000000000000020' + // e0 //
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134a' +
'0000000000000000000000000000000000000000000000000000000000000040' + // 120 //
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134c' +
'0000000000000000000000000000000000000000000000000000000000000020' + // 180 //
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134d'});
test({ type: 'bytes32', expected: '0x6761766f66796f726b0000000000000000000000000000000000000000000000',
value: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ type: 'bytes64', expected: '0xc3a40000c3a40000000000000000000000000000000000000000000000000000' +
'c3a40000c3a40000000000000000000000000000000000000000000000000000',
value: 'c3a40000c3a40000000000000000000000000000000000000000000000000000' +
'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'string', expected: 'gavofyork', value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
@ -71,22 +218,6 @@ describe('lib/solidity/coder', function () {
'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'bytes32', expected: '0xc3a40000c3a40000000000000000000000000000000000000000000000000000',
value: 'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'int[]', expected: [], value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'int[]', expected: [new bn(3)], value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ type: 'int256[]', expected: [new bn(3)], value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ type: 'int[]', expected: [new bn(1), new bn(2), new bn(3)],
value: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ type: 'bool', expected: true, value: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'bool', expected: false, value: '0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'real', expected: new bn(1), value: '0000000000000000000000000000000100000000000000000000000000000000'});
test({ type: 'real', expected: new bn(2.125), value: '0000000000000000000000000000000220000000000000000000000000000000'});
test({ type: 'real', expected: new bn(8.5), value: '0000000000000000000000000000000880000000000000000000000000000000'});
@ -113,7 +244,45 @@ describe('lib/solidity/coder', function () {
};
test({ types: ['address'], expected: ['0x407d73d8a49eeb85d32cf465507dd71d507100c1'],
values: '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1'});
test({ types: ['address', 'address'], expected: ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c3'],
values: '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3'});
test({ types: ['bool[2]', 'bool[3]'], expected: [[true, false], [false, false, true]],
values: '0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000001'});
test({ types: ['int[2]', 'int256[3]'], expected: [[new bn(1), new bn(2)], [new bn(3), new bn(4), new bn(5)]],
values: '0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000004' +
'0000000000000000000000000000000000000000000000000000000000000005'});
test({ types: ['int'], expected: [new bn(1)], values: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ types: ['uint[2]', 'uint256[3]'], expected: [[new bn(1), new bn(2)], [new bn(3), new bn(4), new bn(5)]],
values: '0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000004' +
'0000000000000000000000000000000000000000000000000000000000000005'});
test({ types: ['uint'], expected: [new bn(1)], values: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ types: ['bytes', 'bytes'], expected: ['0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
'0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134c'],
values: '0000000000000000000000000000000000000000000000000000000000000040' +
'0000000000000000000000000000000000000000000000000000000000000080' +
'0000000000000000000000000000000000000000000000000000000000000020' +
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b' +
'0000000000000000000000000000000000000000000000000000000000000020' +
'731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134c'});
test({ types: ['int', 'string', 'int'], expected: [new bn(1), 'gavofyork', new bn(5)],
values: '0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000060' +
'0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['bytes32', 'int'], expected: ['0x6761766f66796f726b0000000000000000000000000000000000000000000000', new bn(5)],
values: '6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000005'});

View File

@ -12,6 +12,71 @@ describe('lib/solidity/coder', function () {
};
test({ type: 'address', value: '0x407d73d8a49eeb85d32cf465507dd71d507100c1',
expected: '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1'});
test({ type: 'address[2]', value: ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c3'],
expected: '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' });
test({ type: 'address[]', value: ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c3'],
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' });
test({ type: 'address[][2]', value: [['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c2'],
['0x407d73d8a49eeb85d32cf465507dd71d507100c3', '0x407d73d8a49eeb85d32cf465507dd71d507100c4']],
expected: '0000000000000000000000000000000000000000000000000000000000000040' +
'00000000000000000000000000000000000000000000000000000000000000a0' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c2' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c4' });
test({ type: 'address[2][]', value: [['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c2'],
['0x407d73d8a49eeb85d32cf465507dd71d507100c3', '0x407d73d8a49eeb85d32cf465507dd71d507100c4']],
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c2' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c4' });
//test({ type: 'address[][]', value: [['0x407d73d8a49eeb85d32cf465507dd71d507100c5'],
//['0x407d73d8a49eeb85d32cf465507dd71d507100c3']],
//expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'0000000000000000000000000000000000000000000000000000000000000002' +
//'0000000000000000000000000000000000000000000000000000000000000080' +
//'00000000000000000000000000000000000000000000000000000000000000c0' +
//'0000000000000000000000000000000000000000000000000000000000000001' +
//'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c5' +
//'0000000000000000000000000000000000000000000000000000000000000001' +
//'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' });
//test({ type: 'address[][]', value: [['0x407d73d8a49eeb85d32cf465507dd71d507100cf', '0x407d73d8a49eeb85d32cf465507dd71d507100c2'],
//['0x407d73d8a49eeb85d32cf465507dd71d507100c3', '0x407d73d8a49eeb85d32cf465507dd71d507100c4']],
//expected: '0000000000000000000000000000000000000000000000000000000000000020' +
//'0000000000000000000000000000000000000000000000000000000000000002' +
//'0000000000000000000000000000000000000000000000000000000000000080' +
//'00000000000000000000000000000000000000000000000000000000000000e0' +
//'0000000000000000000000000000000000000000000000000000000000000002' +
//'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100cf' +
//'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c2' +
//'0000000000000000000000000000000000000000000000000000000000000002' +
//'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3' +
//'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c4' });
test({ type: 'bool', value: true, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'bool', value: false, expected: '0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'bool[1][2]', value: [[false], [false]],
expected: '0000000000000000000000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'bool[2]', value: [true, false],
expected: '0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'bool[]', value: [true, true, false],
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'int', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'int', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'int', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
@ -20,6 +85,13 @@ describe('lib/solidity/coder', function () {
test({ type: 'int256', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'int256', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'int256', value: -1, expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
test({ type: 'uint', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'uint', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'uint', value: 0.1, expected: '0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'uint', value: 3.9, expected: '0000000000000000000000000000000000000000000000000000000000000003'});
test({ type: 'uint256', value: 1, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'uint256', value: 16, expected: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ type: 'bytes32', value: '0x6761766f66796f726b',
expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ type: 'bytes32', value: '0x731a3afc00d1b1e3461b955e53fc866dcf303b3eb9f4c16f89e388930f48134b',
@ -43,6 +115,10 @@ describe('lib/solidity/coder', function () {
'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'bytes32', value: '0xc3a40000c3a4',
expected: 'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
//test({ type: 'bytes64', value: '0xc3a40000c3a40000000000000000000000000000000000000000000000000000' +
//'c3a40000c3a40000000000000000000000000000000000000000000000000000',
//expected: 'c3a40000c3a40000000000000000000000000000000000000000000000000000' +
//'c3a40000c3a40000000000000000000000000000000000000000000000000000'});
test({ type: 'string', value: 'ää',
expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000008' +
@ -68,10 +144,9 @@ describe('lib/solidity/coder', function () {
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003'});
test({ type: 'bool', value: true, expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ type: 'bool', value: false, expected: '0000000000000000000000000000000000000000000000000000000000000000'});
test({ type: 'address', value: '0x407d73d8a49eeb85d32cf465507dd71d507100c1',
expected: '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1'});
test({ type: 'real', value: 1, expected: '0000000000000000000000000000000100000000000000000000000000000000'});
test({ type: 'real', value: 2.125, expected: '0000000000000000000000000000000220000000000000000000000000000000'});
test({ type: 'real', value: 8.5, expected: '0000000000000000000000000000000880000000000000000000000000000000'});
@ -111,17 +186,21 @@ describe('lib/solidity/coder', function () {
};
test({ types: ['address', 'address'], values: ['0x407d73d8a49eeb85d32cf465507dd71d507100c1', '0x407d73d8a49eeb85d32cf465507dd71d507100c3'],
expected: '000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1' +
'000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c3'});
test({ types: ['bool[2]', 'bool[3]'], values: [[true, false], [false, false, true]],
expected: '0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000001'});
test({ types: ['int'], values: [1], expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ types: ['int'], values: [16], expected: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ types: ['int'], values: [-1], expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
test({ types: ['int256'], values: [1], expected: '0000000000000000000000000000000000000000000000000000000000000001'});
test({ types: ['int256'], values: [16], expected: '0000000000000000000000000000000000000000000000000000000000000010'});
test({ types: ['int256'], values: [-1], expected: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'});
test({ types: ['bytes32'], values: ['0x6761766f66796f726b'],
expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['string'], values: ['gavofyork'], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['int[]'], values: [[3]], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000003'});
@ -142,12 +221,45 @@ describe('lib/solidity/coder', function () {
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000004'});
test({ types: ['int[]', 'int[]', 'int[]'], values: [[1,2], [3,4], [5,6,7]],
expected: '0000000000000000000000000000000000000000000000000000000000000060' +
'00000000000000000000000000000000000000000000000000000000000000c0' +
'0000000000000000000000000000000000000000000000000000000000000120' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000002' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000004' +
'0000000000000000000000000000000000000000000000000000000000000003' +
'0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000006' +
'0000000000000000000000000000000000000000000000000000000000000007'});
test({ types: ['bytes32'], values: ['0x6761766f66796f726b'],
expected: '6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['string'], values: ['gavofyork'], expected: '0000000000000000000000000000000000000000000000000000000000000020' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['string', 'string'], values: ['gavofyork', 'gavofyork'],
expected: '0000000000000000000000000000000000000000000000000000000000000040' +
'0000000000000000000000000000000000000000000000000000000000000080' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['bytes32', 'int'], values: ['0x6761766f66796f726b', 5],
expected: '6761766f66796f726b0000000000000000000000000000000000000000000000' +
'0000000000000000000000000000000000000000000000000000000000000005'});
test({ types: ['int', 'bytes32'], values: [5, '0x6761766f66796f726b'],
expected: '0000000000000000000000000000000000000000000000000000000000000005' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['int', 'string'], values: [5, 'gavofyork'],
expected: '0000000000000000000000000000000000000000000000000000000000000005' +
'0000000000000000000000000000000000000000000000000000000000000040' +
'0000000000000000000000000000000000000000000000000000000000000009' +
'6761766f66796f726b0000000000000000000000000000000000000000000000'});
test({ types: ['string', 'int'], values: ['gavofyork', 5],
expected: '0000000000000000000000000000000000000000000000000000000000000040' +
'0000000000000000000000000000000000000000000000000000000000000005' +