mirror of https://github.com/status-im/web3.js.git
cleanup and documentation
This commit is contained in:
parent
6e60e1ffcd
commit
c6a57b359c
|
@ -163,11 +163,13 @@ SolidityType.prototype.isVariadicType = function (type) {
|
|||
};
|
||||
|
||||
/**
|
||||
* Should be used to shift param from params group
|
||||
* Should be used to slice single param from bytes
|
||||
*
|
||||
* @method shiftParam
|
||||
* @method sliceParam
|
||||
* @param {String} bytes
|
||||
* @param {Number} index of param to slice
|
||||
* @param {String} type
|
||||
* @returns {SolidityParam} shifted param
|
||||
* @returns {SolidityParam} param
|
||||
*/
|
||||
SolidityType.prototype.sliceParam = function (bytes, index, type) {
|
||||
if (this._mode === 'bytes') {
|
||||
|
@ -205,25 +207,6 @@ SolidityCoder.prototype._requireType = function (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 prefixLength = types.filter(function (type) {
|
||||
//return self._requireType(type).isVariadicType(type);
|
||||
//}).length;
|
||||
|
||||
//var prefix = bytes.slice(0, prefixLength * 64);
|
||||
//var suffix = bytes.slice(prefixLength * 64);
|
||||
//return new SolidityParam(prefix, suffix);
|
||||
//};
|
||||
|
||||
/**
|
||||
* Should be used to transform plain param of given type to SolidityParam
|
||||
*
|
||||
|
@ -623,22 +606,56 @@ var SolidityParam = function (value, offset) {
|
|||
this.offset = offset; // offset in bytes
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be used to get length of params's dynamic part
|
||||
*
|
||||
* @method dynamicPartLength
|
||||
* @returns {Number} length of dynamic part (in bytes)
|
||||
*/
|
||||
SolidityParam.prototype.dynamicPartLength = function () {
|
||||
return this.dynamicPart().length / 2;
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be used to create copy of solidity param with different offset
|
||||
*
|
||||
* @method withOffset
|
||||
* @param {Number} offset length in bytes
|
||||
* @returns {SolidityParam} new solidity param with applied offset
|
||||
*/
|
||||
SolidityParam.prototype.withOffset = function (offset) {
|
||||
return new SolidityParam(this.value, offset);
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be used to combine solidity params together
|
||||
* eg. when appending an array
|
||||
*
|
||||
* @method combine
|
||||
* @param {SolidityParam} param with which we should combine
|
||||
* @param {SolidityParam} result of combination
|
||||
*/
|
||||
SolidityParam.prototype.combine = function (param) {
|
||||
return new SolidityParam(this.value + param.value);
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be called to check if param has dynamic size.
|
||||
* If it has, it returns true, otherwise false
|
||||
*
|
||||
* @method isDynamic
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
SolidityParam.prototype.isDynamic = function () {
|
||||
return this.value.length > 64;
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be called to transform offset to bytes
|
||||
*
|
||||
* @method offsetAsBytes
|
||||
* @returns {String} bytes representation of offset
|
||||
*/
|
||||
SolidityParam.prototype.offsetAsBytes = function () {
|
||||
if (!this.isDynamic()) {
|
||||
return '';
|
||||
|
@ -646,6 +663,12 @@ SolidityParam.prototype.offsetAsBytes = function () {
|
|||
return utils.padLeft(utils.toTwosComplement(this.offset).toString(16), 64);
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be called to get static part of param
|
||||
*
|
||||
* @method staticPart
|
||||
* @returns {String} offset if it is a dynamic param, otherwise value
|
||||
*/
|
||||
SolidityParam.prototype.staticPart = function () {
|
||||
if (!this.isDynamic()) {
|
||||
return this.value;
|
||||
|
@ -653,14 +676,33 @@ SolidityParam.prototype.staticPart = function () {
|
|||
return this.offsetAsBytes();
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be called to get dynamic part of param
|
||||
*
|
||||
* @method dynamicPart
|
||||
* @returns {String} returns a value if it is a dynamic param, otherwise empty string
|
||||
*/
|
||||
SolidityParam.prototype.dynamicPart = function () {
|
||||
return this.isDynamic() ? this.value : '';
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be called to encode param
|
||||
*
|
||||
* @method encode
|
||||
* @returns {String}
|
||||
*/
|
||||
SolidityParam.prototype.encode = function () {
|
||||
return this.staticPart() + this.dynamicPart();
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be called to encode array of params
|
||||
*
|
||||
* @method encodeList
|
||||
* @param {Array[SolidityParam]} params
|
||||
* @returns {String}
|
||||
*/
|
||||
SolidityParam.encodeList = function (params) {
|
||||
|
||||
// updating offsets
|
||||
|
@ -682,16 +724,40 @@ 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;
|
||||
//TODO add support for strings longer than 32 bytes
|
||||
|
@ -703,91 +769,21 @@ SolidityParam.decodeBytes = function (bytes, index) {
|
|||
return new SolidityParam(bytes.substr(offset * 2, 2 * 64));
|
||||
};
|
||||
|
||||
/**
|
||||
* 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 offset = getOffset(bytes, index);
|
||||
var length = parseInt('0x' + bytes.substr(offset * 2, 64));
|
||||
return new SolidityParam(bytes.substr(offset * 2, (length + 1) * 64));
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be used to encode two params one after another
|
||||
*
|
||||
* @method append
|
||||
* @param {SolidityParam} param that it appended after this
|
||||
*/
|
||||
//SolidityParam.prototype.append = function (param) {
|
||||
//this.prefix += param.prefix;
|
||||
//this.suffix += param.suffix;
|
||||
//};
|
||||
|
||||
/**
|
||||
* This method should be used to encode next param in an array
|
||||
*
|
||||
* @method appendArrayElement
|
||||
* @param {SolidityParam} param that is appended to an array
|
||||
*/
|
||||
//SolidityParam.prototype.appendArrayElement = function (param) {
|
||||
//this.value += param.value;
|
||||
//};
|
||||
|
||||
/**
|
||||
* This method should be used to create bytearrays from param
|
||||
*
|
||||
* @method encode
|
||||
* @return {String} encoded param(s)
|
||||
*/
|
||||
//SolidityParam.prototype.encode = function () {
|
||||
//return this.offset + this.value;
|
||||
//};
|
||||
|
||||
/**
|
||||
* This method should be used to shift first param from group of params
|
||||
*
|
||||
* @method shiftValue
|
||||
* @return {SolidityParam} first prefix param
|
||||
*/
|
||||
//SolidityParam.prototype.shiftValue = function () {
|
||||
//var suffix = this.suffix.slice(0, 64);
|
||||
//this.suffix = this.suffix.slice(64);
|
||||
//return new SolidityParam('', suffix);
|
||||
//};
|
||||
|
||||
/**
|
||||
* This method should be used to first bytes param from group of params
|
||||
*
|
||||
* @method shiftBytes
|
||||
* @return {SolidityParam} first bytes param
|
||||
*/
|
||||
//SolidityParam.prototype.shiftBytes = function () {
|
||||
//return this.shiftArray(1);
|
||||
//};
|
||||
|
||||
/**
|
||||
* This method should be used to shift an array from group of params
|
||||
*
|
||||
* @method shiftArray
|
||||
* @param {Number} size of an array to shift
|
||||
* @return {SolidityParam} first array param
|
||||
*/
|
||||
//SolidityParam.prototype.shiftArray = function (length) {
|
||||
//var prefix = this.prefix.slice(0, 64);
|
||||
//this.prefix = this.prefix.slice(64);
|
||||
//var suffix = this.suffix.slice(0, 64 * length);
|
||||
//this.suffix = this.suffix.slice(64 * length);
|
||||
//return new SolidityParam(prefix, suffix);
|
||||
//};
|
||||
|
||||
/**
|
||||
* This method should be used to create new parram by swapping it's prefix and suffix
|
||||
*
|
||||
* @method swap
|
||||
* @return {SolidityParam} param with swaped bytes
|
||||
*/
|
||||
//SolidityParam.prototype.swap = function () {
|
||||
//return new SolidityParam(this.suffix, this.prefix);
|
||||
//};
|
||||
|
||||
module.exports = SolidityParam;
|
||||
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -163,11 +163,13 @@ SolidityType.prototype.isVariadicType = function (type) {
|
|||
};
|
||||
|
||||
/**
|
||||
* Should be used to shift param from params group
|
||||
* Should be used to slice single param from bytes
|
||||
*
|
||||
* @method shiftParam
|
||||
* @method sliceParam
|
||||
* @param {String} bytes
|
||||
* @param {Number} index of param to slice
|
||||
* @param {String} type
|
||||
* @returns {SolidityParam} shifted param
|
||||
* @returns {SolidityParam} param
|
||||
*/
|
||||
SolidityType.prototype.sliceParam = function (bytes, index, type) {
|
||||
if (this._mode === 'bytes') {
|
||||
|
@ -205,25 +207,6 @@ SolidityCoder.prototype._requireType = function (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 prefixLength = types.filter(function (type) {
|
||||
//return self._requireType(type).isVariadicType(type);
|
||||
//}).length;
|
||||
|
||||
//var prefix = bytes.slice(0, prefixLength * 64);
|
||||
//var suffix = bytes.slice(prefixLength * 64);
|
||||
//return new SolidityParam(prefix, suffix);
|
||||
//};
|
||||
|
||||
/**
|
||||
* Should be used to transform plain param of given type to SolidityParam
|
||||
*
|
||||
|
@ -623,22 +606,56 @@ var SolidityParam = function (value, offset) {
|
|||
this.offset = offset; // offset in bytes
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be used to get length of params's dynamic part
|
||||
*
|
||||
* @method dynamicPartLength
|
||||
* @returns {Number} length of dynamic part (in bytes)
|
||||
*/
|
||||
SolidityParam.prototype.dynamicPartLength = function () {
|
||||
return this.dynamicPart().length / 2;
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be used to create copy of solidity param with different offset
|
||||
*
|
||||
* @method withOffset
|
||||
* @param {Number} offset length in bytes
|
||||
* @returns {SolidityParam} new solidity param with applied offset
|
||||
*/
|
||||
SolidityParam.prototype.withOffset = function (offset) {
|
||||
return new SolidityParam(this.value, offset);
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be used to combine solidity params together
|
||||
* eg. when appending an array
|
||||
*
|
||||
* @method combine
|
||||
* @param {SolidityParam} param with which we should combine
|
||||
* @param {SolidityParam} result of combination
|
||||
*/
|
||||
SolidityParam.prototype.combine = function (param) {
|
||||
return new SolidityParam(this.value + param.value);
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be called to check if param has dynamic size.
|
||||
* If it has, it returns true, otherwise false
|
||||
*
|
||||
* @method isDynamic
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
SolidityParam.prototype.isDynamic = function () {
|
||||
return this.value.length > 64;
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be called to transform offset to bytes
|
||||
*
|
||||
* @method offsetAsBytes
|
||||
* @returns {String} bytes representation of offset
|
||||
*/
|
||||
SolidityParam.prototype.offsetAsBytes = function () {
|
||||
if (!this.isDynamic()) {
|
||||
return '';
|
||||
|
@ -646,6 +663,12 @@ SolidityParam.prototype.offsetAsBytes = function () {
|
|||
return utils.padLeft(utils.toTwosComplement(this.offset).toString(16), 64);
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be called to get static part of param
|
||||
*
|
||||
* @method staticPart
|
||||
* @returns {String} offset if it is a dynamic param, otherwise value
|
||||
*/
|
||||
SolidityParam.prototype.staticPart = function () {
|
||||
if (!this.isDynamic()) {
|
||||
return this.value;
|
||||
|
@ -653,14 +676,33 @@ SolidityParam.prototype.staticPart = function () {
|
|||
return this.offsetAsBytes();
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be called to get dynamic part of param
|
||||
*
|
||||
* @method dynamicPart
|
||||
* @returns {String} returns a value if it is a dynamic param, otherwise empty string
|
||||
*/
|
||||
SolidityParam.prototype.dynamicPart = function () {
|
||||
return this.isDynamic() ? this.value : '';
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be called to encode param
|
||||
*
|
||||
* @method encode
|
||||
* @returns {String}
|
||||
*/
|
||||
SolidityParam.prototype.encode = function () {
|
||||
return this.staticPart() + this.dynamicPart();
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be called to encode array of params
|
||||
*
|
||||
* @method encodeList
|
||||
* @param {Array[SolidityParam]} params
|
||||
* @returns {String}
|
||||
*/
|
||||
SolidityParam.encodeList = function (params) {
|
||||
|
||||
// updating offsets
|
||||
|
@ -682,16 +724,40 @@ 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;
|
||||
//TODO add support for strings longer than 32 bytes
|
||||
|
@ -703,91 +769,21 @@ SolidityParam.decodeBytes = function (bytes, index) {
|
|||
return new SolidityParam(bytes.substr(offset * 2, 2 * 64));
|
||||
};
|
||||
|
||||
/**
|
||||
* 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 offset = getOffset(bytes, index);
|
||||
var length = parseInt('0x' + bytes.substr(offset * 2, 64));
|
||||
return new SolidityParam(bytes.substr(offset * 2, (length + 1) * 64));
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be used to encode two params one after another
|
||||
*
|
||||
* @method append
|
||||
* @param {SolidityParam} param that it appended after this
|
||||
*/
|
||||
//SolidityParam.prototype.append = function (param) {
|
||||
//this.prefix += param.prefix;
|
||||
//this.suffix += param.suffix;
|
||||
//};
|
||||
|
||||
/**
|
||||
* This method should be used to encode next param in an array
|
||||
*
|
||||
* @method appendArrayElement
|
||||
* @param {SolidityParam} param that is appended to an array
|
||||
*/
|
||||
//SolidityParam.prototype.appendArrayElement = function (param) {
|
||||
//this.value += param.value;
|
||||
//};
|
||||
|
||||
/**
|
||||
* This method should be used to create bytearrays from param
|
||||
*
|
||||
* @method encode
|
||||
* @return {String} encoded param(s)
|
||||
*/
|
||||
//SolidityParam.prototype.encode = function () {
|
||||
//return this.offset + this.value;
|
||||
//};
|
||||
|
||||
/**
|
||||
* This method should be used to shift first param from group of params
|
||||
*
|
||||
* @method shiftValue
|
||||
* @return {SolidityParam} first prefix param
|
||||
*/
|
||||
//SolidityParam.prototype.shiftValue = function () {
|
||||
//var suffix = this.suffix.slice(0, 64);
|
||||
//this.suffix = this.suffix.slice(64);
|
||||
//return new SolidityParam('', suffix);
|
||||
//};
|
||||
|
||||
/**
|
||||
* This method should be used to first bytes param from group of params
|
||||
*
|
||||
* @method shiftBytes
|
||||
* @return {SolidityParam} first bytes param
|
||||
*/
|
||||
//SolidityParam.prototype.shiftBytes = function () {
|
||||
//return this.shiftArray(1);
|
||||
//};
|
||||
|
||||
/**
|
||||
* This method should be used to shift an array from group of params
|
||||
*
|
||||
* @method shiftArray
|
||||
* @param {Number} size of an array to shift
|
||||
* @return {SolidityParam} first array param
|
||||
*/
|
||||
//SolidityParam.prototype.shiftArray = function (length) {
|
||||
//var prefix = this.prefix.slice(0, 64);
|
||||
//this.prefix = this.prefix.slice(64);
|
||||
//var suffix = this.suffix.slice(0, 64 * length);
|
||||
//this.suffix = this.suffix.slice(64 * length);
|
||||
//return new SolidityParam(prefix, suffix);
|
||||
//};
|
||||
|
||||
/**
|
||||
* This method should be used to create new parram by swapping it's prefix and suffix
|
||||
*
|
||||
* @method swap
|
||||
* @return {SolidityParam} param with swaped bytes
|
||||
*/
|
||||
//SolidityParam.prototype.swap = function () {
|
||||
//return new SolidityParam(this.suffix, this.prefix);
|
||||
//};
|
||||
|
||||
module.exports = SolidityParam;
|
||||
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -116,11 +116,13 @@ SolidityType.prototype.isVariadicType = function (type) {
|
|||
};
|
||||
|
||||
/**
|
||||
* Should be used to shift param from params group
|
||||
* Should be used to slice single param from bytes
|
||||
*
|
||||
* @method shiftParam
|
||||
* @method sliceParam
|
||||
* @param {String} bytes
|
||||
* @param {Number} index of param to slice
|
||||
* @param {String} type
|
||||
* @returns {SolidityParam} shifted param
|
||||
* @returns {SolidityParam} param
|
||||
*/
|
||||
SolidityType.prototype.sliceParam = function (bytes, index, type) {
|
||||
if (this._mode === 'bytes') {
|
||||
|
@ -158,25 +160,6 @@ SolidityCoder.prototype._requireType = function (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 prefixLength = types.filter(function (type) {
|
||||
//return self._requireType(type).isVariadicType(type);
|
||||
//}).length;
|
||||
|
||||
//var prefix = bytes.slice(0, prefixLength * 64);
|
||||
//var suffix = bytes.slice(prefixLength * 64);
|
||||
//return new SolidityParam(prefix, suffix);
|
||||
//};
|
||||
|
||||
/**
|
||||
* Should be used to transform plain param of given type to SolidityParam
|
||||
*
|
||||
|
|
|
@ -31,22 +31,56 @@ var SolidityParam = function (value, offset) {
|
|||
this.offset = offset; // offset in bytes
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be used to get length of params's dynamic part
|
||||
*
|
||||
* @method dynamicPartLength
|
||||
* @returns {Number} length of dynamic part (in bytes)
|
||||
*/
|
||||
SolidityParam.prototype.dynamicPartLength = function () {
|
||||
return this.dynamicPart().length / 2;
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be used to create copy of solidity param with different offset
|
||||
*
|
||||
* @method withOffset
|
||||
* @param {Number} offset length in bytes
|
||||
* @returns {SolidityParam} new solidity param with applied offset
|
||||
*/
|
||||
SolidityParam.prototype.withOffset = function (offset) {
|
||||
return new SolidityParam(this.value, offset);
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be used to combine solidity params together
|
||||
* eg. when appending an array
|
||||
*
|
||||
* @method combine
|
||||
* @param {SolidityParam} param with which we should combine
|
||||
* @param {SolidityParam} result of combination
|
||||
*/
|
||||
SolidityParam.prototype.combine = function (param) {
|
||||
return new SolidityParam(this.value + param.value);
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be called to check if param has dynamic size.
|
||||
* If it has, it returns true, otherwise false
|
||||
*
|
||||
* @method isDynamic
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
SolidityParam.prototype.isDynamic = function () {
|
||||
return this.value.length > 64;
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be called to transform offset to bytes
|
||||
*
|
||||
* @method offsetAsBytes
|
||||
* @returns {String} bytes representation of offset
|
||||
*/
|
||||
SolidityParam.prototype.offsetAsBytes = function () {
|
||||
if (!this.isDynamic()) {
|
||||
return '';
|
||||
|
@ -54,6 +88,12 @@ SolidityParam.prototype.offsetAsBytes = function () {
|
|||
return utils.padLeft(utils.toTwosComplement(this.offset).toString(16), 64);
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be called to get static part of param
|
||||
*
|
||||
* @method staticPart
|
||||
* @returns {String} offset if it is a dynamic param, otherwise value
|
||||
*/
|
||||
SolidityParam.prototype.staticPart = function () {
|
||||
if (!this.isDynamic()) {
|
||||
return this.value;
|
||||
|
@ -61,14 +101,33 @@ SolidityParam.prototype.staticPart = function () {
|
|||
return this.offsetAsBytes();
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be called to get dynamic part of param
|
||||
*
|
||||
* @method dynamicPart
|
||||
* @returns {String} returns a value if it is a dynamic param, otherwise empty string
|
||||
*/
|
||||
SolidityParam.prototype.dynamicPart = function () {
|
||||
return this.isDynamic() ? this.value : '';
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be called to encode param
|
||||
*
|
||||
* @method encode
|
||||
* @returns {String}
|
||||
*/
|
||||
SolidityParam.prototype.encode = function () {
|
||||
return this.staticPart() + this.dynamicPart();
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be called to encode array of params
|
||||
*
|
||||
* @method encodeList
|
||||
* @param {Array[SolidityParam]} params
|
||||
* @returns {String}
|
||||
*/
|
||||
SolidityParam.encodeList = function (params) {
|
||||
|
||||
// updating offsets
|
||||
|
@ -90,16 +149,40 @@ 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;
|
||||
//TODO add support for strings longer than 32 bytes
|
||||
|
@ -111,6 +194,14 @@ SolidityParam.decodeBytes = function (bytes, index) {
|
|||
return new SolidityParam(bytes.substr(offset * 2, 2 * 64));
|
||||
};
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
@ -118,83 +209,5 @@ SolidityParam.decodeArray = function (bytes, index) {
|
|||
return new SolidityParam(bytes.substr(offset * 2, (length + 1) * 64));
|
||||
};
|
||||
|
||||
/**
|
||||
* This method should be used to encode two params one after another
|
||||
*
|
||||
* @method append
|
||||
* @param {SolidityParam} param that it appended after this
|
||||
*/
|
||||
//SolidityParam.prototype.append = function (param) {
|
||||
//this.prefix += param.prefix;
|
||||
//this.suffix += param.suffix;
|
||||
//};
|
||||
|
||||
/**
|
||||
* This method should be used to encode next param in an array
|
||||
*
|
||||
* @method appendArrayElement
|
||||
* @param {SolidityParam} param that is appended to an array
|
||||
*/
|
||||
//SolidityParam.prototype.appendArrayElement = function (param) {
|
||||
//this.value += param.value;
|
||||
//};
|
||||
|
||||
/**
|
||||
* This method should be used to create bytearrays from param
|
||||
*
|
||||
* @method encode
|
||||
* @return {String} encoded param(s)
|
||||
*/
|
||||
//SolidityParam.prototype.encode = function () {
|
||||
//return this.offset + this.value;
|
||||
//};
|
||||
|
||||
/**
|
||||
* This method should be used to shift first param from group of params
|
||||
*
|
||||
* @method shiftValue
|
||||
* @return {SolidityParam} first prefix param
|
||||
*/
|
||||
//SolidityParam.prototype.shiftValue = function () {
|
||||
//var suffix = this.suffix.slice(0, 64);
|
||||
//this.suffix = this.suffix.slice(64);
|
||||
//return new SolidityParam('', suffix);
|
||||
//};
|
||||
|
||||
/**
|
||||
* This method should be used to first bytes param from group of params
|
||||
*
|
||||
* @method shiftBytes
|
||||
* @return {SolidityParam} first bytes param
|
||||
*/
|
||||
//SolidityParam.prototype.shiftBytes = function () {
|
||||
//return this.shiftArray(1);
|
||||
//};
|
||||
|
||||
/**
|
||||
* This method should be used to shift an array from group of params
|
||||
*
|
||||
* @method shiftArray
|
||||
* @param {Number} size of an array to shift
|
||||
* @return {SolidityParam} first array param
|
||||
*/
|
||||
//SolidityParam.prototype.shiftArray = function (length) {
|
||||
//var prefix = this.prefix.slice(0, 64);
|
||||
//this.prefix = this.prefix.slice(64);
|
||||
//var suffix = this.suffix.slice(0, 64 * length);
|
||||
//this.suffix = this.suffix.slice(64 * length);
|
||||
//return new SolidityParam(prefix, suffix);
|
||||
//};
|
||||
|
||||
/**
|
||||
* This method should be used to create new parram by swapping it's prefix and suffix
|
||||
*
|
||||
* @method swap
|
||||
* @return {SolidityParam} param with swaped bytes
|
||||
*/
|
||||
//SolidityParam.prototype.swap = function () {
|
||||
//return new SolidityParam(this.suffix, this.prefix);
|
||||
//};
|
||||
|
||||
module.exports = SolidityParam;
|
||||
|
||||
|
|
Loading…
Reference in New Issue