Code coverage report for ethereum.js/lib/solidity/param.js

Statements: 100% (26 / 26)      Branches: 100% (6 / 6)      Functions: 100% (7 / 7)      Lines: 100% (26 / 26)      Ignored: none     

All files » ethereum.js/lib/solidity/ » param.js
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                                                    1 357 357 357                 1 99 99 99                 1 12 12                   1 100                 1 59 59 59                 1 7                   1 9 9 9 9 9     1    
/*
    This file is part of ethereum.js.
 
    ethereum.js is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    ethereum.js is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public License
    along with ethereum.js.  If not, see <http://www.gnu.org/licenses/>.
*/
/** 
 * @file param.js
 * @author Marek Kotewicz <marek@ethdev.com>
 * @date 2015
 */
 
/**
 * SolidityParam object prototype.
 * Should be used when encoding, decoding solidity bytes
 */
var SolidityParam = function (value, prefix, suffix) {
    this.prefix = prefix || '';
    this.value = value || '';
    this.suffix = suffix || '';
};
 
/**
 * 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.value += param.value;
    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.suffix += param.value;
    this.prefix += param.prefix;
    // TODO: suffix not supported = it's required for nested arrays;
};
 
/**
 * This method should be used to create bytearrays from param
 *
 * @method encode
 * @return {String} encoded param(s)
 */
SolidityParam.prototype.encode = function () {
    return this.prefix + this.value + this.suffix;
};
 
/**
 * This method should be used to shift first param from group of params
 *
 * @method shiftValue
 * @return {SolidityParam} first value param
 */
SolidityParam.prototype.shiftValue = function () {
    var value = this.value.slice(0, 64);
    this.value = this.value.slice(64);
    return new SolidityParam(value);
};
 
/**
 * 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.value.slice(64);
    var suffix = this.suffix.slice(0, 64 * length);
    this.suffix = this.suffix.slice(64 * length);
    return new SolidityParam('', prefix, suffix);
};
 
module.exports = SolidityParam;