jsonrpc.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. This file is part of web3.js.
  3. web3.js is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. web3.js is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with web3.js. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file jsonrpc.js
  15. * @authors:
  16. * Fabian Vogelsteller <fabian@ethereum.org>
  17. * Marek Kotewicz <marek@ethdev.com>
  18. * Aaron Kumavis <aaron@kumavis.me>
  19. * @date 2015
  20. */
  21. "use strict";
  22. // Initialize Jsonrpc as a simple object with utility functions.
  23. var Jsonrpc = {
  24. // This is the starting counter for the Jsonrpc.id.
  25. // Pick a random number between 0 and the maximum safe integer
  26. messageId: Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)
  27. };
  28. /**
  29. * Should be called to valid json create payload object
  30. *
  31. * @method toPayload
  32. * @param {Function} method of jsonrpc call, required
  33. * @param {Array} params, an array of method params, optional
  34. * @returns {Object} valid jsonrpc payload object
  35. */
  36. Jsonrpc.toPayload = function (method, params) {
  37. if (!method) {
  38. throw new Error('JSONRPC method should be specified for params: "' + JSON.stringify(params) + '"!');
  39. }
  40. if (Jsonrpc.messageId === Number.MAX_SAFE_INTEGER) {
  41. // if the maximum safe integer has been reached, restart from a random number
  42. Jsonrpc.messageId = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
  43. }
  44. else {
  45. // advance message ID
  46. Jsonrpc.messageId++;
  47. }
  48. return {
  49. jsonrpc: '2.0',
  50. id: Jsonrpc.messageId,
  51. method: method,
  52. params: params || []
  53. };
  54. };
  55. /**
  56. * Should be called to check if jsonrpc response is valid
  57. *
  58. * @method isValidResponse
  59. * @param {Object}
  60. * @returns {Boolean} true if response is valid, otherwise false
  61. */
  62. Jsonrpc.isValidResponse = function (response) {
  63. return Array.isArray(response) ? response.every(validateSingleMessage) : validateSingleMessage(response);
  64. function validateSingleMessage(message) {
  65. return !!message &&
  66. !message.error &&
  67. message.jsonrpc === '2.0' &&
  68. (typeof message.id === 'number' || typeof message.id === 'string') &&
  69. message.result !== undefined; // only undefined is not valid json object
  70. }
  71. };
  72. /**
  73. * Should be called to create batch payload object
  74. *
  75. * @method toBatchPayload
  76. * @param {Array} messages, an array of objects with method (required) and params (optional) fields
  77. * @returns {Array} batch payload
  78. */
  79. Jsonrpc.toBatchPayload = function (messages) {
  80. return messages.map(function (message) {
  81. return Jsonrpc.toPayload(message.method, message.params);
  82. });
  83. };
  84. module.exports = Jsonrpc;