github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/cmd/mist/assets/ext/ethereum.js/lib/jsonrpc.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 jsonrpc.js
    18   * @authors:
    19   *   Marek Kotewicz <marek@ethdev.com>
    20   * @date 2015
    21   */
    22  
    23  var messageId = 1;
    24  
    25  /// Should be called to valid json create payload object
    26  /// @param method of jsonrpc call, required
    27  /// @param params, an array of method params, optional
    28  /// @returns valid jsonrpc payload object
    29  var toPayload = function (method, params) {
    30      if (!method)
    31          console.error('jsonrpc method should be specified!');
    32  
    33      return {
    34          jsonrpc: '2.0',
    35          method: method,
    36          params: params || [],
    37          id: messageId++
    38      }; 
    39  };
    40  
    41  /// Should be called to check if jsonrpc response is valid
    42  /// @returns true if response is valid, otherwise false 
    43  var isValidResponse = function (response) {
    44      return !!response &&
    45          !response.error &&
    46          response.jsonrpc === '2.0' &&
    47          typeof response.id === 'number' &&
    48          response.result !== undefined; // only undefined is not valid json object
    49  };
    50  
    51  /// Should be called to create batch payload object
    52  /// @param messages, an array of objects with method (required) and params (optional) fields
    53  var toBatchPayload = function (messages) {
    54      return messages.map(function (message) {
    55          return toPayload(message.method, message.params);
    56      }); 
    57  };
    58  
    59  module.exports = {
    60      toPayload: toPayload,
    61      isValidResponse: isValidResponse,
    62      toBatchPayload: toBatchPayload
    63  };
    64  
    65