github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/cmd/mist/assets/ext/ethereum.js/lib/web3.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 web3.js
    18   * @authors:
    19   *   Jeffrey Wilcke <jeff@ethdev.com>
    20   *   Marek Kotewicz <marek@ethdev.com>
    21   *   Marian Oancea <marian@ethdev.com>
    22   *   Gav Wood <g@ethdev.com>
    23   * @date 2014
    24   */
    25  
    26  if (process.env.NODE_ENV !== 'build') {
    27      var BigNumber = require('bignumber.js');
    28  }
    29  
    30  var utils = require('./utils');
    31  
    32  /// @returns an array of objects describing web3 api methods
    33  var web3Methods = function () {
    34      return [
    35      { name: 'sha3', call: 'web3_sha3' }
    36      ];
    37  };
    38  
    39  /// @returns an array of objects describing web3.eth api methods
    40  var ethMethods = function () {
    41      var blockCall = function (args) {
    42          return typeof args[0] === "string" ? "eth_blockByHash" : "eth_blockByNumber";
    43      };
    44  
    45      var transactionCall = function (args) {
    46          return typeof args[0] === "string" ? 'eth_transactionByHash' : 'eth_transactionByNumber';
    47      };
    48  
    49      var uncleCall = function (args) {
    50          return typeof args[0] === "string" ? 'eth_uncleByHash' : 'eth_uncleByNumber';
    51      };
    52  
    53      var methods = [
    54      { name: 'balanceAt', call: 'eth_balanceAt' },
    55      { name: 'stateAt', call: 'eth_stateAt' },
    56      { name: 'storageAt', call: 'eth_storageAt' },
    57      { name: 'countAt', call: 'eth_countAt'},
    58      { name: 'codeAt', call: 'eth_codeAt' },
    59      { name: 'transact', call: 'eth_transact' },
    60      { name: 'call', call: 'eth_call' },
    61      { name: 'block', call: blockCall },
    62      { name: 'transaction', call: transactionCall },
    63      { name: 'uncle', call: uncleCall },
    64      { name: 'compilers', call: 'eth_compilers' },
    65      { name: 'flush', call: 'eth_flush' },
    66      { name: 'lll', call: 'eth_lll' },
    67      { name: 'solidity', call: 'eth_solidity' },
    68      { name: 'serpent', call: 'eth_serpent' },
    69      { name: 'logs', call: 'eth_logs' }
    70      ];
    71      return methods;
    72  };
    73  
    74  /// @returns an array of objects describing web3.eth api properties
    75  var ethProperties = function () {
    76      return [
    77      { name: 'coinbase', getter: 'eth_coinbase', setter: 'eth_setCoinbase' },
    78      { name: 'listening', getter: 'eth_listening', setter: 'eth_setListening' },
    79      { name: 'mining', getter: 'eth_mining', setter: 'eth_setMining' },
    80      { name: 'gasPrice', getter: 'eth_gasPrice' },
    81      { name: 'accounts', getter: 'eth_accounts' },
    82      { name: 'peerCount', getter: 'eth_peerCount' },
    83      { name: 'defaultBlock', getter: 'eth_defaultBlock', setter: 'eth_setDefaultBlock' },
    84      { name: 'number', getter: 'eth_number'}
    85      ];
    86  };
    87  
    88  /// @returns an array of objects describing web3.db api methods
    89  var dbMethods = function () {
    90      return [
    91      { name: 'put', call: 'db_put' },
    92      { name: 'get', call: 'db_get' },
    93      { name: 'putString', call: 'db_putString' },
    94      { name: 'getString', call: 'db_getString' }
    95      ];
    96  };
    97  
    98  /// @returns an array of objects describing web3.shh api methods
    99  var shhMethods = function () {
   100      return [
   101      { name: 'post', call: 'shh_post' },
   102      { name: 'newIdentity', call: 'shh_newIdentity' },
   103      { name: 'haveIdentity', call: 'shh_haveIdentity' },
   104      { name: 'newGroup', call: 'shh_newGroup' },
   105      { name: 'addToGroup', call: 'shh_addToGroup' }
   106      ];
   107  };
   108  
   109  /// @returns an array of objects describing web3.eth.watch api methods
   110  var ethWatchMethods = function () {
   111      var newFilter = function (args) {
   112          return typeof args[0] === 'string' ? 'eth_newFilterString' : 'eth_newFilter';
   113      };
   114  
   115      return [
   116      { name: 'newFilter', call: newFilter },
   117      { name: 'uninstallFilter', call: 'eth_uninstallFilter' },
   118      { name: 'getMessages', call: 'eth_filterLogs' }
   119      ];
   120  };
   121  
   122  /// @returns an array of objects describing web3.shh.watch api methods
   123  var shhWatchMethods = function () {
   124      return [
   125      { name: 'newFilter', call: 'shh_newFilter' },
   126      { name: 'uninstallFilter', call: 'shh_uninstallFilter' },
   127      { name: 'getMessages', call: 'shh_getMessages' }
   128      ];
   129  };
   130  
   131  /// creates methods in a given object based on method description on input
   132  /// setups api calls for these methods
   133  var setupMethods = function (obj, methods) {
   134      methods.forEach(function (method) {
   135          obj[method.name] = function () {
   136              var args = Array.prototype.slice.call(arguments);
   137              var call = typeof method.call === 'function' ? method.call(args) : method.call;
   138              return web3.provider.send({
   139                  method: call,
   140                  params: args
   141              });
   142          };
   143      });
   144  };
   145  
   146  /// creates properties in a given object based on properties description on input
   147  /// setups api calls for these properties
   148  var setupProperties = function (obj, properties) {
   149      properties.forEach(function (property) {
   150          var proto = {};
   151          proto.get = function () {
   152              return web3.provider.send({
   153                  method: property.getter
   154              });
   155          };
   156  
   157          if (property.setter) {
   158              proto.set = function (val) {
   159                  return web3.provider.send({
   160                      method: property.setter,
   161                      params: [val]
   162                  });
   163              };
   164          }
   165          Object.defineProperty(obj, property.name, proto);
   166      });
   167  };
   168  
   169  /// setups web3 object, and it's in-browser executed methods
   170  var web3 = {
   171      _callbacks: {},
   172      _events: {},
   173      providers: {},
   174  
   175      /// @returns ascii string representation of hex value prefixed with 0x
   176      toAscii: utils.toAscii,
   177  
   178      /// @returns hex representation (prefixed by 0x) of ascii string
   179      fromAscii: utils.fromAscii,
   180  
   181      /// @returns decimal representaton of hex value prefixed by 0x
   182      toDecimal: function (val) {
   183          // remove 0x and place 0, if it's required
   184          val = val.length > 2 ? val.substring(2) : "0";
   185          return (new BigNumber(val, 16).toString(10));
   186      },
   187  
   188      /// @returns hex representation (prefixed by 0x) of decimal value
   189      fromDecimal: function (val) {
   190          return "0x" + (new BigNumber(val).toString(16));
   191      },
   192  
   193      /// used to transform value/string to eth string
   194      toEth: utils.toEth,
   195  
   196      /// eth object prototype
   197      eth: {
   198          contractFromAbi: function (abi) {
   199              return function(addr) {
   200                  // Default to address of Config. TODO: rremove prior to genesis.
   201                  addr = addr || '0xc6d9d2cd449a754c494264e1809c50e34d64562b';
   202                  var ret = web3.eth.contract(addr, abi);
   203                  ret.address = addr;
   204                  return ret;
   205              };
   206          },
   207  
   208          /// @param filter may be a string, object or event
   209          /// @param indexed is optional, this is an object with optional event indexed params
   210          /// @param options is optional, this is an object with optional event options ('max'...)
   211          watch: function (filter, indexed, options) {
   212              if (filter._isEvent) {
   213                  return filter(indexed, options);
   214              }
   215              return new web3.filter(filter, ethWatch);
   216          }
   217      },
   218  
   219      /// db object prototype
   220      db: {},
   221  
   222      /// shh object prototype
   223      shh: {
   224          
   225          /// @param filter may be a string, object or event
   226          watch: function (filter, indexed) {
   227              return new web3.filter(filter, shhWatch);
   228          }
   229      },
   230  };
   231  
   232  /// setups all api methods
   233  setupMethods(web3, web3Methods());
   234  setupMethods(web3.eth, ethMethods());
   235  setupProperties(web3.eth, ethProperties());
   236  setupMethods(web3.db, dbMethods());
   237  setupMethods(web3.shh, shhMethods());
   238  
   239  var ethWatch = {
   240      changed: 'eth_changed'
   241  };
   242  
   243  setupMethods(ethWatch, ethWatchMethods());
   244  
   245  var shhWatch = {
   246      changed: 'shh_changed'
   247  };
   248  
   249  setupMethods(shhWatch, shhWatchMethods());
   250  
   251  web3.setProvider = function(provider) {
   252      web3.provider.set(provider);
   253  };
   254  
   255  module.exports = web3;
   256