2015-02-03 22:24:17 +01:00
|
|
|
/*
|
2015-10-08 15:34:07 +08:00
|
|
|
This file is part of web3.js.
|
2015-02-03 22:24:17 +01:00
|
|
|
|
2015-10-08 15:34:07 +08:00
|
|
|
web3.js is free software: you can redistribute it and/or modify
|
2015-02-03 22:24:17 +01:00
|
|
|
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.
|
|
|
|
|
2015-10-08 15:34:07 +08:00
|
|
|
web3.js is distributed in the hope that it will be useful,
|
2015-02-03 22:24:17 +01:00
|
|
|
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
|
2015-10-08 15:34:07 +08:00
|
|
|
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
|
2015-02-03 22:24:17 +01:00
|
|
|
*/
|
|
|
|
/** @file jsonrpc.js
|
|
|
|
* @authors:
|
|
|
|
* Marek Kotewicz <marek@ethdev.com>
|
2016-08-26 14:55:16 -07:00
|
|
|
* Aaron Kumavis <aaron@kumavis.me>
|
2015-02-03 22:24:17 +01:00
|
|
|
* @date 2015
|
|
|
|
*/
|
|
|
|
|
2016-08-26 14:55:16 -07:00
|
|
|
// Initialize Jsonrpc as a simple object with utility functions.
|
|
|
|
var Jsonrpc = {
|
|
|
|
messageId: 0
|
2015-03-26 15:43:35 +01:00
|
|
|
};
|
|
|
|
|
2015-03-21 23:34:24 +01:00
|
|
|
/**
|
|
|
|
* Should be called to valid json create payload object
|
|
|
|
*
|
|
|
|
* @method toPayload
|
|
|
|
* @param {Function} method of jsonrpc call, required
|
|
|
|
* @param {Array} params, an array of method params, optional
|
|
|
|
* @returns {Object} valid jsonrpc payload object
|
|
|
|
*/
|
2016-08-26 14:55:16 -07:00
|
|
|
Jsonrpc.toPayload = function (method, params) {
|
2015-02-03 22:24:17 +01:00
|
|
|
if (!method)
|
|
|
|
console.error('jsonrpc method should be specified!');
|
|
|
|
|
2016-08-26 14:55:16 -07:00
|
|
|
// advance message ID
|
2016-08-26 16:09:50 -07:00
|
|
|
Jsonrpc.messageId++;
|
2016-08-26 14:55:16 -07:00
|
|
|
|
2015-02-03 22:24:17 +01:00
|
|
|
return {
|
|
|
|
jsonrpc: '2.0',
|
2016-08-26 14:55:16 -07:00
|
|
|
id: Jsonrpc.messageId,
|
2015-02-03 22:24:17 +01:00
|
|
|
method: method,
|
2016-08-26 14:55:16 -07:00
|
|
|
params: params || []
|
2015-03-28 00:32:52 +02:00
|
|
|
};
|
2015-02-03 22:24:17 +01:00
|
|
|
};
|
|
|
|
|
2015-03-21 23:34:24 +01:00
|
|
|
/**
|
|
|
|
* Should be called to check if jsonrpc response is valid
|
|
|
|
*
|
|
|
|
* @method isValidResponse
|
|
|
|
* @param {Object}
|
2015-03-28 00:32:52 +02:00
|
|
|
* @returns {Boolean} true if response is valid, otherwise false
|
2015-03-21 23:34:24 +01:00
|
|
|
*/
|
2016-08-26 14:55:16 -07:00
|
|
|
Jsonrpc.isValidResponse = function (response) {
|
2016-08-26 16:09:50 -07:00
|
|
|
return Array.isArray(response) ? response.every(validateSingleMessage) : validateSingleMessage(response);
|
2016-08-26 16:01:44 -07:00
|
|
|
|
|
|
|
function validateSingleMessage(message){
|
|
|
|
return !!message &&
|
|
|
|
!message.error &&
|
|
|
|
message.jsonrpc === '2.0' &&
|
|
|
|
typeof message.id === 'number' &&
|
|
|
|
message.result !== undefined; // only undefined is not valid json object
|
|
|
|
}
|
2015-02-03 22:24:17 +01:00
|
|
|
};
|
|
|
|
|
2015-03-21 23:34:24 +01:00
|
|
|
/**
|
|
|
|
* Should be called to create batch payload object
|
|
|
|
*
|
|
|
|
* @method toBatchPayload
|
|
|
|
* @param {Array} messages, an array of objects with method (required) and params (optional) fields
|
|
|
|
* @returns {Array} batch payload
|
|
|
|
*/
|
2016-08-26 14:55:16 -07:00
|
|
|
Jsonrpc.toBatchPayload = function (messages) {
|
2015-02-03 22:24:17 +01:00
|
|
|
return messages.map(function (message) {
|
2016-08-26 14:55:16 -07:00
|
|
|
return Jsonrpc.toPayload(message.method, message.params);
|
2015-03-28 00:32:52 +02:00
|
|
|
});
|
2015-02-03 22:24:17 +01:00
|
|
|
};
|
|
|
|
|
2015-03-21 23:34:24 +01:00
|
|
|
module.exports = Jsonrpc;
|
2015-02-03 22:24:17 +01:00
|
|
|
|