Skip to content

Instantly share code, notes, and snippets.

@akirattii
Created December 27, 2017 22:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akirattii/fd1564243e1c338aee28a91291746aff to your computer and use it in GitHub Desktop.
Save akirattii/fd1564243e1c338aee28a91291746aff to your computer and use it in GitHub Desktop.
JS: JSON RPC client
//
// An example of a bitcoind JSON RPC request.
//
// Example request of curl:
// $ curl --data-binary '{"jsonrpc": "1.0", "id":"hoge", "method": "getblockhash", "params": [9999] }' http://rpc:rpc@127.0.0.1:18332/
//
const URL = "http://rpc:rpc@127.0.0.1:18332"; // bitcoind testnet. user:"rpc", password:"rpc"
const RpcClient = require("./rpc-client.js");
const rpc = new RpcClient({ url: URL, debug: true });
const p = {
// @see: https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list
method: "getblockhash",
params: [9999],
id: "hoge",
};
rpc.call(p, console.log);
/* [console log]
ON SUCCESS:
null { result: '000000001655e2a7293f28383a2965b2f0add77fd6ac383986e90971a07467d4',
error: null,
id: 123 }
ON ERROR:
{ code: -32601, message: 'Method not found' } { result: null,
error: { code: -32601, message: 'Method not found' },
id: 'hoge' }
*/
const util = require("util");
const request = require("request");
// Example of curl to the bitcoind json rpc server:
// $ curl --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getinfo", "params": [] }' http://rpc:rpc@127.0.0.1:18332/
module.exports = class RpcClient {
constructor({
url,
jsonrpc = "2.0",
debug = false,
logFn = console.log,
}) {
if (!RpcClient.isValidUrl(url))
throw Error(`invalid parameter: url=${url}`);
this.url = url;
this.jsonrpc = jsonrpc;
this.debug = debug;
this.logFn = logFn;
}
//**************************************************************
// main methods
//**************************************************************
/**
* RPC Call
* Example: `request({method:"getblockhash", params:["1"], id:1} (err, res)=>{...});
* @param {Object} - params:
* - method {String} - RPC method
* - params {Object} - RPC params
* - id {String|Number} - [Optional] RPC request ID
*/
call({ method, params, id }, cb) {
const self = this;
const p = self._createRequestParam(method, params, id);
request(p, (err, response, body) => {
err = (body && body.error) ? body.error : err;
return cb && cb(err, body);
});
}
//**************************************************************
// sub methods
//**************************************************************
/**
* Creates request parameter
*/
_createRequestParam(method, params, id) {
// making form data for example like this: '{"jsonrpc": "1.0", "id":"curltest", "method": "getinfo", "params": [] }'
const data = {
jsonrpc: this.jsonrpc,
id,
method,
params,
};
const p = {
url: this.url,
form: JSON.stringify(data),
headers: {
"Content-Type": "application/json; charset=UTF-8",
"Accept": "application/json, text/javascript",
},
method: "POST", // JSONRPC server handles only POST requests
encoding: "UTF-8",
json: true,
};
return p;
}
//**************************************************************
// utility static methods
//**************************************************************
static isValidUrl(v) {
return /^https?:\/\/.+$/.test(v);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment