2015-02-03 21:24:17 +00:00
|
|
|
/*
|
|
|
|
This file is part of ethereum.js.
|
|
|
|
|
|
|
|
ethereum.js is free software: you can redistribute it and/or modify
|
|
|
|
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.
|
|
|
|
|
|
|
|
ethereum.js is distributed in the hope that it will be useful,
|
|
|
|
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
|
|
|
|
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/** @file jsonrpc.js
|
|
|
|
* @authors:
|
|
|
|
* Marek Kotewicz <marek@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
*/
|
|
|
|
|
2015-03-21 22:34:24 +00:00
|
|
|
var Jsonrpc = function () {
|
|
|
|
this.messageId = 1;
|
|
|
|
};
|
2015-02-03 21:24:17 +00:00
|
|
|
|
2015-03-21 22:34:24 +00: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
|
|
|
|
*/
|
|
|
|
Jsonrpc.prototype.toPayload = function (method, params) {
|
2015-02-03 21:24:17 +00:00
|
|
|
if (!method)
|
|
|
|
console.error('jsonrpc method should be specified!');
|
|
|
|
|
|
|
|
return {
|
|
|
|
jsonrpc: '2.0',
|
|
|
|
method: method,
|
|
|
|
params: params || [],
|
2015-03-21 22:34:24 +00:00
|
|
|
id: this.messageId++
|
2015-02-03 21:24:17 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2015-03-21 22:34:24 +00:00
|
|
|
/**
|
|
|
|
* Should be called to check if jsonrpc response is valid
|
|
|
|
*
|
|
|
|
* @method isValidResponse
|
|
|
|
* @param {Object}
|
|
|
|
* @returns {Boolean} true if response is valid, otherwise false
|
|
|
|
*/
|
|
|
|
Jsonrpc.prototype.isValidResponse = function (response) {
|
2015-02-03 22:00:02 +00:00
|
|
|
return !!response &&
|
|
|
|
!response.error &&
|
|
|
|
response.jsonrpc === '2.0' &&
|
|
|
|
typeof response.id === 'number' &&
|
2015-02-04 10:23:23 +00:00
|
|
|
response.result !== undefined; // only undefined is not valid json object
|
2015-02-03 21:24:17 +00:00
|
|
|
};
|
|
|
|
|
2015-03-21 22:34:24 +00: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
|
|
|
|
*/
|
|
|
|
Jsonrpc.prototype.toBatchPayload = function (messages) {
|
|
|
|
var self = this;
|
2015-02-03 21:24:17 +00:00
|
|
|
return messages.map(function (message) {
|
2015-03-21 22:34:24 +00:00
|
|
|
return self.toPayload(message.method, message.params);
|
2015-02-03 21:24:17 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-03-21 22:34:24 +00:00
|
|
|
module.exports = Jsonrpc;
|
2015-02-03 21:24:17 +00:00
|
|
|
|