github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/cmd/mist/assets/ext/ethereum.js/lib/formatters.js (about)

     1  /*
     2      This file is part of ethereum.js.
     3  
     4      ethereum.js is free software: you can redistribute it and/or modify
     5      it under the terms of the GNU Lesser General Public License as published by
     6      the Free Software Foundation, either version 3 of the License, or
     7      (at your option) any later version.
     8  
     9      ethereum.js is distributed in the hope that it will be useful,
    10      but WITHOUT ANY WARRANTY; without even the implied warranty of
    11      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12      GNU Lesser General Public License for more details.
    13  
    14      You should have received a copy of the GNU Lesser General Public License
    15      along with ethereum.js.  If not, see <http://www.gnu.org/licenses/>.
    16  */
    17  /** @file formatters.js
    18   * @authors:
    19   *   Marek Kotewicz <marek@ethdev.com>
    20   * @date 2015
    21   */
    22  
    23  if (process.env.NODE_ENV !== 'build') {
    24      var BigNumber = require('bignumber.js'); // jshint ignore:line
    25  }
    26  
    27  var utils = require('./utils');
    28  var c = require('./const');
    29  
    30  /// @param string string to be padded
    31  /// @param number of characters that result string should have
    32  /// @param sign, by default 0
    33  /// @returns right aligned string
    34  var padLeft = function (string, chars, sign) {
    35      return new Array(chars - string.length + 1).join(sign ? sign : "0") + string;
    36  };
    37  
    38  /// Formats input value to byte representation of int
    39  /// If value is negative, return it's two's complement
    40  /// If the value is floating point, round it down
    41  /// @returns right-aligned byte representation of int
    42  var formatInputInt = function (value) {
    43      var padding = c.ETH_PADDING * 2;
    44      if (value instanceof BigNumber || typeof value === 'number') {
    45          if (typeof value === 'number')
    46              value = new BigNumber(value);
    47          BigNumber.config(c.ETH_BIGNUMBER_ROUNDING_MODE);
    48          value = value.round();
    49  
    50          if (value.lessThan(0)) 
    51              value = new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(value).plus(1);
    52          value = value.toString(16);
    53      }
    54      else if (value.indexOf('0x') === 0)
    55          value = value.substr(2);
    56      else if (typeof value === 'string')
    57          value = formatInputInt(new BigNumber(value));
    58      else
    59          value = (+value).toString(16);
    60      return padLeft(value, padding);
    61  };
    62  
    63  /// Formats input value to byte representation of string
    64  /// @returns left-algined byte representation of string
    65  var formatInputString = function (value) {
    66      return utils.fromAscii(value, c.ETH_PADDING).substr(2);
    67  };
    68  
    69  /// Formats input value to byte representation of bool
    70  /// @returns right-aligned byte representation bool
    71  var formatInputBool = function (value) {
    72      return '000000000000000000000000000000000000000000000000000000000000000' + (value ?  '1' : '0');
    73  };
    74  
    75  /// Formats input value to byte representation of real
    76  /// Values are multiplied by 2^m and encoded as integers
    77  /// @returns byte representation of real
    78  var formatInputReal = function (value) {
    79      return formatInputInt(new BigNumber(value).times(new BigNumber(2).pow(128))); 
    80  };
    81  
    82  
    83  /// Check if input value is negative
    84  /// @param value is hex format
    85  /// @returns true if it is negative, otherwise false
    86  var signedIsNegative = function (value) {
    87      return (new BigNumber(value.substr(0, 1), 16).toString(2).substr(0, 1)) === '1';
    88  };
    89  
    90  /// Formats input right-aligned input bytes to int
    91  /// @returns right-aligned input bytes formatted to int
    92  var formatOutputInt = function (value) {
    93      value = value || "0";
    94      // check if it's negative number
    95      // it it is, return two's complement
    96      if (signedIsNegative(value)) {
    97          return new BigNumber(value, 16).minus(new BigNumber('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)).minus(1);
    98      }
    99      return new BigNumber(value, 16);
   100  };
   101  
   102  /// Formats big right-aligned input bytes to uint
   103  /// @returns right-aligned input bytes formatted to uint
   104  var formatOutputUInt = function (value) {
   105      value = value || "0";
   106      return new BigNumber(value, 16);
   107  };
   108  
   109  /// @returns input bytes formatted to real
   110  var formatOutputReal = function (value) {
   111      return formatOutputInt(value).dividedBy(new BigNumber(2).pow(128)); 
   112  };
   113  
   114  /// @returns input bytes formatted to ureal
   115  var formatOutputUReal = function (value) {
   116      return formatOutputUInt(value).dividedBy(new BigNumber(2).pow(128)); 
   117  };
   118  
   119  /// @returns right-aligned input bytes formatted to hex
   120  var formatOutputHash = function (value) {
   121      return "0x" + value;
   122  };
   123  
   124  /// @returns right-aligned input bytes formatted to bool
   125  var formatOutputBool = function (value) {
   126      return value === '0000000000000000000000000000000000000000000000000000000000000001' ? true : false;
   127  };
   128  
   129  /// @returns left-aligned input bytes formatted to ascii string
   130  var formatOutputString = function (value) {
   131      return utils.toAscii(value);
   132  };
   133  
   134  /// @returns right-aligned input bytes formatted to address
   135  var formatOutputAddress = function (value) {
   136      return "0x" + value.slice(value.length - 40, value.length);
   137  };
   138  
   139  
   140  module.exports = {
   141      formatInputInt: formatInputInt,
   142      formatInputString: formatInputString,
   143      formatInputBool: formatInputBool,
   144      formatInputReal: formatInputReal,
   145      formatOutputInt: formatOutputInt,
   146      formatOutputUInt: formatOutputUInt,
   147      formatOutputReal: formatOutputReal,
   148      formatOutputUReal: formatOutputUReal,
   149      formatOutputHash: formatOutputHash,
   150      formatOutputBool: formatOutputBool,
   151      formatOutputString: formatOutputString,
   152      formatOutputAddress: formatOutputAddress
   153  };
   154