This commit is contained in:
debris 2015-07-28 08:28:46 +02:00
parent 7b27c47d69
commit 9b2b10c8b8
2 changed files with 61 additions and 28 deletions

View File

@ -1,6 +1,16 @@
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;
@ -38,26 +48,5 @@ SolidityTypeAddress.prototype.nestedName = function (name) {
return name.replace(/\[([0-9])*\]/, '');
};
SolidityTypeAddress.prototype.formatOutput = function (param, unused, name) {
if (this.isStaticArray(name)) {
var staticPart = param.staticPart();
var result = [];
for (var i = 0; i < staticPart.length; i += 64) {
result.push(this._outputFormatter(new SolidityParam(staticPart.substr(0, i + 64))));
}
return result;
} else if (this.isDynamicArray(name)) {
var dynamicPart = param.dynamicPart();
var result = [];
// first position of dynamic part is the length of the array
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(dynamicPart.substr(i + 64, 64))));
}
return result;
}
return this._outputFormatter(param);
};
module.exports = SolidityTypeAddress;

View File

@ -1,5 +1,3 @@
var f = require('./formatters');
var SolidityParam = require('./param');
@ -12,7 +10,7 @@ var SolidityType = function (config) {
};
/**
* Should be used to determine if this SolidityType do match given type
* Should be used to determine if this SolidityType do match given name
*
* @method isType
* @param {String} name
@ -22,6 +20,53 @@ SolidityType.prototype.isType = function (name) {
throw "this method should be overrwritten!";
};
/**
* 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!";
};
/**
* 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) {
throw "this method should be overrwritten!";
};
/**
* 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) {
throw "this method should be overrwritten!";
};
/**
* 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) {
if (this.isDynamicArray(name)) {
var length = value.length; // in int
@ -52,13 +97,13 @@ SolidityType.prototype.encode = function (value, name) {
};
/**
* Should be used to decode params from bytes
* Should be used to decode value from bytes
*
* @method decode
* @param {String} bytes
* @param {Number} offset in bytes
* @param {String} name type name
* @returns {SolidityParam} param
* @returns {Object} decoded value
*/
SolidityType.prototype.decode = function (bytes, offset, name) {
if (this.isDynamicArray(name)) {
@ -95,4 +140,3 @@ SolidityType.prototype.decode = function (bytes, offset, name) {
};
module.exports = SolidityType;