Last active
December 20, 2015 16:38
-
-
Save kyledrake/6162554 to your computer and use it in GitHub Desktop.
Probably the smallest feature-complete bitcoind RPC interface in existence.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:[email protected]: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