All files / susyweb-sof-abi/src type.js

96.55% Statements 84/87
87.5% Branches 21/24
94.74% Functions 18/19
96.55% Lines 84/87

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 2561x 1x         1x 7x 7x                   1x                     1x   1469x     1494x     1494x                             1x 2080x 2080x                         1x 1175x 1175x                                 1x 41x 41x 41x                                   1x   157x 157x       157x                     1x 1098x                           1x   4922x                     1x 356x 356x   28x 28x 28x   28x 28x   28x 57x     28x     328x   11x 11x 11x   11x 11x 23x     11x         317x                       1x 456x   456x   21x 21x 21x 21x   21x 21x 21x 21x   21x 37x     21x     435x   30x 30x 30x   30x 30x 30x 30x   30x 64x     30x   405x   42x 42x 42x 42x 42x 42x       363x 363x 363x     1x  
var f = require('./formatters');
var PolynomialParam = require('./param');
 
/**
 * PolynomialType prototype is used to encode/decode polynomial params of certain type
 */
var PolynomialType = function (config) {
    this._inputFormatter = config.inputFormatter;
    this._outputFormatter = config.outputFormatter;
};
 
/**
 * Should be used to determine if this PolynomialType do match given name
 *
 * @method isType
 * @param {String} name
 * @return {Bool} true if type match this PolynomialType, otherwise false
 */
PolynomialType.prototype.isType = function (name) {
    throw "This method should be overwritten 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
 */
PolynomialType.prototype.staticPartLength = function (name) {
    // If name isn't an array then treat it like a single element array.
    return (this.nestedTypes(name) || ['[1]'])
        .map(function (type) {
            // the length of the nested array
            return parseInt(type.slice(1, -1), 10) || 1;
        })
        .reduce(function (previous, current) {
            return previous * current;
        // all basic types are 32 bytes long
        }, 32);
};
 
/**
 * 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
 */
PolynomialType.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
 */
PolynomialType.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
 */
PolynomialType.prototype.staticArrayLength = function (name) {
    var nestedTypes = this.nestedTypes(name);
    Eif (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
 */
PolynomialType.prototype.nestedName = function (name) {
    // remove last [] in name
    var nestedTypes = this.nestedTypes(name);
    Iif (!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
 */
PolynomialType.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
 */
PolynomialType.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
 */
PolynomialType.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
 */
PolynomialType.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 roundedNestedStaticPartLength = Math.floor((nestedStaticPartLength + 31) / 32) * 32;
            var result = [];
 
            for (var i = 0; i < length * roundedNestedStaticPartLength; i += roundedNestedStaticPartLength) {
                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 roundedNestedStaticPartLength = Math.floor((nestedStaticPartLength + 31) / 32) * 32;
            var result = [];
 
            for (var i = 0; i < length * roundedNestedStaticPartLength; i += roundedNestedStaticPartLength) {
                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
            var param = new PolynomialParam(bytes.substr(dynamicOffset * 2, ( 1 + roundedLength) * 64), 0, bytes);
            return self._outputFormatter(param, name);
        })();
    }
 
    var length = this.staticPartLength(name);
    var param = new PolynomialParam(bytes.substr(offset * 2, length * 2), undefined, bytes);
    return this._outputFormatter(param, name);
};
 
module.exports = PolynomialType;