diff --git a/lib/bitcoin/client.js b/lib/bitcoin/client.js index d8af16a..610deab 100644 --- a/lib/bitcoin/client.js +++ b/lib/bitcoin/client.js @@ -1,13 +1,13 @@ -var rpc = require('../jsonrpc'); -var _ = require('underscore')._; - +var rpc = require('../jsonrpc') + , _ = require('underscore')._; //===----------------------------------------------------------------------===// // jsonrpc wrappers //===----------------------------------------------------------------------===// var bitcoinAPI = { backupWallet: 'backupwallet', + getAccount: 'getaccount', getBalance: 'getbalance', getBlockCount: 'getblockcount', getBlockNumber: 'getblocknumber', @@ -54,7 +54,7 @@ function Client(host, port, user, pass) { // Call custom jsonrpc commands //===----------------------------------------------------------------------===// Client.prototype.cmd = function() { - var args = [].slice.call(arguments); + var args = [].slice.call(arguments); var cmd = args.shift(); callRpc(cmd, args, this.rpc); @@ -69,12 +69,18 @@ function callRpc(cmd, args, rpc) { // If the last function is a callback, pop it from the args list if(_.isFunction(fn)) { - args.pop(); + args.pop(); } else { fn = function () {}; } - rpc.call(cmd, args, fn); + rpc.call(cmd, args, function(){ + var args = [].slice.call(arguments); + args.unshift(null); + fn.apply(this, args); + }, function(err){ + fn(err); + }); } //===----------------------------------------------------------------------===// @@ -85,7 +91,7 @@ function callRpc(cmd, args, rpc) { Client.prototype[protoFn] = function() { var args = [].slice.call(arguments); callRpc(jsonFn, args, this.rpc); - }; + } }); })(); diff --git a/lib/jsonrpc.js b/lib/jsonrpc.js index 32873ad..5949459 100644 --- a/lib/jsonrpc.js +++ b/lib/jsonrpc.js @@ -38,8 +38,18 @@ var Client = function(port, host, user, password) { headers['Content-Length'] = requestJSON.length; // Now we'll make a request to the server + var request = client.request('POST', path || '/', headers); request.write(requestJSON); + + client.on('error', function(e){ + errback(e); + }); + + request.on('error', function(e){ + errback(e); + }); + request.on('response', function(response) { // We need to buffer the response chunks in a nonblocking way. var buffer = ''; diff --git a/package.json b/package.json index f1b08ad..625c45a 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "bitcoin", "description": "Communicate with bitcoind via JSON-RPC", - "version": "0.0.3", + "version": "1.0.0", "main": "./lib/bitcoin", "keywords": [ "bitcoin", diff --git a/test/api.js b/test/api.js index d164f43..6b49c1a 100644 --- a/test/api.js +++ b/test/api.js @@ -1,70 +1,70 @@ var path = require('path'); require.paths.unshift(path.join(__dirname, '..')); +// test variables + +var test = { + address: "1GbDSt8XieCHQmkQsZmMirwHoUHJukjwXn" +} + +// end test variables + var vows = require('vows'), assert = require('assert'); var bitcoin = require('lib/bitcoin'); -var nullError = function(cb) { - var self = this; - return function() { - var args = [].slice.call(arguments); - args.unshift(null); - cb.apply(self, args); - }; -} - function makeClient() { return new bitcoin.Client('localhost', 8332, 'jb55', 'thisisthepassword'); } -function clientTopic() { - var args = [].slice.call(arguments); - var cmd = args.shift(); - return function(client){ - args.push(nullError(this.callback)); - client[cmd].apply(client, args); - } -} - -function notEmpty(err, data) { +function notEmpty(data) { if (data === 0) return; assert.ok(data); } vows.describe('api').addBatch({ - 'client': { - topic: makeClient(), - 'getTransaction': { - topic: clientTopic('getTransaction', "fff2525b8931402dd09222c50775608f75787bd2b87e56995a7bdd30f79702c4"), - 'should not be empty': notEmpty, + '': { + topic: makeClient, + 'an account': { + topic: function(client){ + client.getAccount(test.address, this.callback); + }, + 'is valid': function(account){ + assert.ok(account, "Update test variables with a valid address?"); + }, }, 'getDifficulty': { - topic: clientTopic('getDifficulty'), + topic: function(client) { client.getDifficulty(this.callback); }, 'should not be empty': notEmpty, - 'is a number': function (err, data) { assert.isNumber(data) }, - 'is greater than 0': function (err, data) { assert.isTrue(data > 0); } + 'is a number': function (data) { + assert.isNumber(data); + }, + 'is greater than 0': function (data) { assert.isTrue(data > 0); } }, 'getInfo': { - topic: clientTopic('getInfo'), + topic: function(client) { client.getInfo(this.callback); }, 'should not be empty': notEmpty, - 'info.errors should be empty': function (err, info) { + 'info.errors should be empty': function (info) { assert.isEmpty(info.errors); }, }, 'getHashesPerSec': { - topic: clientTopic('getHashesPerSec'), + topic: function(client) { client.getHashesPerSec(this.callback); }, 'should not be empty': notEmpty, - 'is a number': function (err, data) { assert.isNumber(data) }, + 'is a number': function (data) { assert.isNumber(data) }, }, 'help': { - topic: clientTopic('help'), + topic: function(client) { client.help(this.callback); }, 'should not be empty': notEmpty, - } + }, + 'getTransaction': { + topic: "TODO: get valid transaction", + 'should not be empty': notEmpty, + }, }, - + }).run();