Initial commit

This commit is contained in:
Bill Casarin 2010-12-19 08:50:53 -05:00
commit 0be96d9656
3 changed files with 290 additions and 0 deletions

68
lib/bitcoin/index.js Normal file
View File

@ -0,0 +1,68 @@
var rpc = require('../jsonrpc');
var _ = require('underscore')._;
//===----------------------------------------------------------------------===//
// jsonrpc wrappers
//===----------------------------------------------------------------------===//
var bitcoinAPI = {
backupWallet: 'backupwallet',
getBalance: 'getbalance',
getBlockCount: 'getblockcount',
getBlockNumber: 'getblocknumber',
getConnectionCount: 'getconnectioncount',
getDifficulty: 'getdifficulty',
getGenerate: 'getgenerate',
getHashesPerSecond: 'gethashespersec',
getInfo: 'getinfo',
getNewAddress: 'getnewaddress',
getReceivedByAccount: 'getreceivedbyaccount',
listReceivedByAccount: 'listreceivedbyaccount',
getReceivedByAddress: 'getreceivedbyaddress',
listReceivedByAddress: 'listreceivedbyaddress',
getTransaction: 'gettransaction',
getWork: 'getwork',
setGenerate: 'setgenerate',
validateAddress: 'validateaddress',
help: 'help',
};
//===----------------------------------------------------------------------===//
// Client
//===----------------------------------------------------------------------===//
function Client(host, port, user, pass) {
this.host = host;
this.port = port;
this.user = user;
this.pass = pass;
this.rpc = rpc.getClient(port, host, user, pass);
}
//===----------------------------------------------------------------------===//
// Initialize wrappers
//===----------------------------------------------------------------------===//
(function() {
_.each(bitcoinAPI, function(jsonFn, protoFn) {
Client.prototype[protoFn] = function() {
var args = [].slice.call(arguments);
var fn = args[args.length-1];
// If the last function is a callback, pop it from the args list
if(_.isFunction(fn)) {
args.pop();
} else {
fn = function () {};
}
this.rpc.call(jsonFn, args, fn);
};
});
})();
// Export!
module.exports = Client;

205
lib/jsonrpc.js Normal file
View File

@ -0,0 +1,205 @@
var sys = require('sys');
var http = require('http');
var functions = {};
var METHOD_NOT_ALLOWED = "Method Not Allowed\n";
var INVALID_REQUEST = "Invalid Request\n";
var JSONRPCClient = function(port, host, user, password) {
this.port = port;
this.host = host;
this.user = user;
this.password = password;
this.call = function(method, params, callback, errback, path) {
var client = http.createClient(port, host);
// First we encode the request into JSON
var requestJSON = JSON.stringify({
'id': '' + (new Date()).getTime(),
'method': method,
'params': params
});
var headers = {};
if (user && password) {
var buff = new Buffer(this.user + ":" + this.password).toString('base64');
var auth = 'Basic ' + buff;
headers['Authorization'] = auth;
}
// Then we build some basic headers.
headers['Host'] = host;
headers['Content-Length'] = requestJSON.length;
// Now we'll make a request to the server
var request = client.request('POST', path || '/', headers);
request.write(requestJSON);
request.on('response', function(response) {
// We need to buffer the response chunks in a nonblocking way.
var buffer = '';
response.on('data', function(chunk) {
buffer = buffer + chunk;
});
// When all the responses are finished, we decode the JSON and
// depending on whether it's got a result or an error, we call
// emitSuccess or emitError on the promise.
response.on('end', function() {
var decoded = JSON.parse(buffer);
if(decoded.hasOwnProperty('result')) {
if (callback)
callback(decoded.result);
}
else {
if (errback)
errback(decoded.error);
}
});
});
};
}
var JSONRPC = {
functions: functions,
exposeModule: function(mod, object) {
var funcs = [];
for(var funcName in object) {
var funcObj = object[funcName];
if(typeof(funcObj) == 'function') {
functions[mod + '.' + funcName] = funcObj;
funcs.push(funcName);
}
}
JSONRPC.trace('***', 'exposing module: ' + mod + ' [funs: ' + funcs.join(', ') + ']');
return object;
},
expose: function(name, func) {
JSONRPC.trace('***', 'exposing: ' + name);
functions[name] = func;
},
trace: function(direction, message) {
sys.puts(' ' + direction + ' ' + message);
},
listen: function(port, host) {
JSONRPC.server.listen(port, host);
JSONRPC.trace('***', 'Server listening on http://' + (host || '127.0.0.1') + ':' + port + '/');
},
handleInvalidRequest: function(req, res) {
res.writeHead(400, {'Content-Type': 'text/plain',
'Content-Length': INVALID_REQUEST.length});
res.write(INVALID_REQUEST);
res.finish();
},
handlePOST: function(req, res) {
var buffer = '';
var promise = new process.Promise();
promise.addCallback(function(buf) {
var decoded = JSON.parse(buf);
// Check for the required fields, and if they aren't there, then
// dispatch to the handleInvalidRequest function.
if(!(decoded.method && decoded.params && decoded.id)) {
return JSONRPC.handleInvalidRequest(req, res);
}
if(!JSONRPC.functions.hasOwnProperty(decoded.method)) {
return JSONRPC.handleInvalidRequest(req, res);
}
// Build our success handler
var onSuccess = function(funcResp) {
JSONRPC.trace('-->', 'response (id ' + decoded.id + '): ' + JSON.stringify(funcResp));
var encoded = JSON.stringify({
'result': funcResp,
'error': null,
'id': decoded.id
});
res.writeHead(200, {'Content-Type': 'application/json',
'Content-Length': encoded.length});
res.write(encoded);
res.finish();
};
// Build our failure handler (note that error must not be null)
var onFailure = function(failure) {
JSONRPC.trace('-->', 'failure: ' + JSON.stringify(failure));
var encoded = JSON.stringify({
'result': null,
'error': failure || 'Unspecified Failure',
'id': decoded.id
});
res.writeHead(200, {'Content-Type': 'application/json',
'Content-Length': encoded.length});
res.write(encoded);
res.finish();
};
JSONRPC.trace('<--', 'request (id ' + decoded.id + '): ' + decoded.method + '(' + decoded.params.join(', ') + ')');
// Try to call the method, but intercept errors and call our
// onFailure handler.
var method = JSONRPC.functions[decoded.method];
var resp = null;
try {
resp = method.apply(null, decoded.params);
}
catch(err) {
return onFailure(err);
}
// If it's a promise, we should add callbacks and errbacks,
// but if it's not, we can just go ahead and call the callback.
if(resp instanceof process.Promise) {
resp.addCallback(onSuccess);
resp.addErrback(onFailure);
}
else {
onSuccess(resp);
}
});
req.addListener('body', function(chunk) {
buffer = buffer + chunk;
});
req.addListener('complete', function() {
promise.emitSuccess(buffer);
});
},
handleNonPOST: function(req, res) {
res.writeHead(405, {'Content-Type': 'text/plain',
'Content-Length': METHOD_NOT_ALLOWED.length,
'Allow': 'POST'});
res.write(METHOD_NOT_ALLOWED);
res.finish();
},
handleRequest: function(req, res) {
JSONRPC.trace('<--', 'accepted request');
if(req.method === 'POST') {
JSONRPC.handlePOST(req, res);
}
else {
JSONRPC.handleNonPOST(req, res);
}
},
server: http.createServer(function(req, res) {
// TODO: Get rid of this extraneous extra function call.
JSONRPC.handleRequest(req, res);
}),
getClient: function(port, host, user, password) {
return new JSONRPCClient(port, host, user, password);
}
};
module.exports = JSONRPC;

17
test.js Normal file
View File

@ -0,0 +1,17 @@
var bitcoin = require('./lib/bitcoin/index.js');
var client = new bitcoin.Client('localhost', 8332, 'jb55', 'thisisthepassword');
doCmd('getBalance');
doCmd('getConnectionCount');
doCmd('getDifficulty');
doCmd('getInfo');
doCmd('getHashesPerSecond');
doCmd('getGenerate');
function doCmd(cmd) {
client[cmd](function(data) {
console.log(cmd);
console.log(data);
});
}