Skip to content

Instantly share code, notes, and snippets.

@kyledrake
Last active December 20, 2015 16:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyledrake/6162554 to your computer and use it in GitHub Desktop.
Save kyledrake/6162554 to your computer and use it in GitHub Desktop.
Probably the smallest feature-complete bitcoind RPC interface in existence.
var request = require('request');
function Bitcoind(url) {
this.url = url;
};
Bitcoind.prototype.rpc = function(method, params, callback) {
this.request({jsonrpc: '2.0', method: method, params: params}, callback);
};
Bitcoind.prototype.batch = function(cmds, callback) {
var payload = [];
for(var i=0;i<cmds.length;i++)
payload.push({jsonrpc: '2.0', method: cmds[i].method, params: cmds[i].params, id: i});
this.request(payload, callback);
};
Bitcoind.prototype.request = function(payload, callback) {
request({uri: this.url, method: 'POST', json: payload}, function (error, response, body) {
if (!error && response.statusCode == 200) {
callback(undefined, body.result);
} else {
if(response.statusCode == 401) {
console.log('bitcoind error 401: invalid auth (check your user/pass)');
callback({message: "Invalid auth"});
} else {
console.log('bitcoind error '+response.statusCode+': '+JSON.stringify(body.error));
callback(body.error);
}
}
});
};
module.exports = Bitcoind;
/*
new Bitcoind('http://user:pass@127.0.0.1:8332').rpc('listunspent', [1, 999999], function(err, res) {
console.log(res);
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment