diff --git a/commands.js b/commands.js new file mode 100644 index 0000000..9e5f3be --- /dev/null +++ b/commands.js @@ -0,0 +1,62 @@ +module.exports = { + addMultiSigAddress: 'addmultisigaddress', + addNode: 'addnode', // bitcoind v0.8.0+ + backupWallet: 'backupwallet', + createRawTransaction: 'createrawtransaction', // bitcoind v0.7.0+ + decodeRawTransaction: 'decoderawtransaction', // bitcoind v0.7.0+ + dumpPrivKey: 'dumpprivkey', + encryptWallet: 'encryptwallet', + getAccount: 'getaccount', + getAccountAddress: 'getaccountaddress', + getAddedNodeInfo: 'getaddednodeinfo', // bitcoind v0.8.0+ + getAddressesByAccount: 'getaddressesbyaccount', + getBalance: 'getbalance', + getBlock: 'getblock', + getBlockCount: 'getblockcount', + getBlockHash: 'getblockhash', + getConnectionCount: 'getconnectioncount', + getDifficulty: 'getdifficulty', + getGenerate: 'getgenerate', + getHashesPerSecond: 'gethashespersec', + getHashesPerSec: 'gethashespersec', + getInfo: 'getinfo', + getMemorypool: 'getmemorypool', + getMemoryPool: 'getmemorypool', + getMiningInfo: 'getmininginfo', + getNewAddress: 'getnewaddress', + getPeerInfo: 'getpeerinfo', // bitcoind v0.7.0+ + getRawMemPool: 'getrawmempool', // bitcoind v0.7.0+ + getRawTransaction: 'getrawtransaction', // bitcoind v0.7.0+ + getReceivedByAccount: 'getreceivedbyaccount', + getReceivedByAddress: 'getreceivedbyaddress', + getTransaction: 'gettransaction', + getWork: 'getwork', + help: 'help', + importPrivKey: 'importprivkey', + keypoolRefill: 'keypoolrefill', + keyPoolRefill: 'keypoolrefill', + listAccounts: 'listaccounts', + listLockUnspent: 'listlockunspent', // bitcoind v0.8.0 + listReceivedByAccount: 'listreceivedbyaccount', + listReceivedByAddress: 'listreceivedbyaddress', + listSinceBlock: 'listsinceblock', + listTransactions: 'listtransactions', + listUnspent: 'listunspent', // bitcoind v0.7.0+ + lockUnspent: 'lockunspent', // bitcoind v0.8.0+ + move: 'move', + sendFrom: 'sendfrom', + sendMany: 'sendmany', + sendRawTransaction: 'sendrawtransaction', // bitcoind v0.7.0+ + sendToAddress: 'sendtoaddress', + setAccount: 'setaccount', + setGenerate: 'setgenerate', + setTxFee: 'settxfee', + signMessage: 'signmessage', + signRawTransaction: 'signrawtransaction', // bitcoind v0.7.0+ + stop: 'stop', + validateAddress: 'validateaddress', + verifyMessage: 'verifymessage', + walletLock: 'walletlock', + walletPassphrase: 'walletpassphrase', + walletPassphraseChange: 'walletpassphrasechange' +}; diff --git a/index.js b/index.js new file mode 100644 index 0000000..fd280d5 --- /dev/null +++ b/index.js @@ -0,0 +1,85 @@ +var deprecate = require('deprecate'), + commands = require('./commands'), + rpc = require('./jsonrpc'); + +//===----------------------------------------------------------------------===// +// Client +// Pass in an object with host, port, user, pass +//===----------------------------------------------------------------------===// +function Client() { + var args = [].slice.call(arguments); + + if (args.length > 1) { + deprecate('calling bitcoin.Client with more than one argument is deprecated'); + this.host = args[0]; + this.port = args[1]; + this.user = args[2]; + this.pass = args[3]; + this.ssl = false; + this.sslStrict = false; + this.sslCa = null; + } else { + this.host = args[0].host; + this.port = args[0].port; + this.user = args[0].user; + this.pass = args[0].pass; + this.ssl = args[0].ssl ? true : false; + this.sslStrict = (typeof args[0].sslStrict === 'undefined' || args[0].sslStrict); + this.sslCa = args[0].sslCa; + } + + this.rpc = new rpc.Client(this.port, this.host, this.user, this.pass, + this.ssl, this.sslStrict, this.sslCa); +} + + +//===----------------------------------------------------------------------===// +// cmd +// Call custom jsonrpc commands +//===----------------------------------------------------------------------===// +Client.prototype.cmd = function() { + var args = [].slice.call(arguments); + var cmd = args.shift(); + + callRpc(cmd, args, this.rpc); +} + + +//===----------------------------------------------------------------------===// +// callRpc +//===----------------------------------------------------------------------===// +function callRpc(cmd, args, rpc) { + var fn = args[args.length-1]; + + // If the last function is a callback, pop it from the args list + if(typeof fn === 'function') { + args.pop(); + } else { + fn = function () {}; + } + + rpc.call(cmd, args, function(){ + var args = [].slice.call(arguments); + args.unshift(null); + fn.apply(this, args); + }, function(err){ + fn(err); + }); +} + +//===----------------------------------------------------------------------===// +// Initialize wrappers +//===----------------------------------------------------------------------===// +(function() { + for (var protoFn in commands) { + (function(protoFn) { + Client.prototype[protoFn] = function() { + var args = [].slice.call(arguments); + callRpc(commands[protoFn], args, this.rpc); + }; + })(protoFn); + } +})(); + +// Export! +module.exports.Client = Client; diff --git a/lib/jsonrpc.js b/jsonrpc.js similarity index 100% rename from lib/jsonrpc.js rename to jsonrpc.js diff --git a/lib/bitcoin/client.js b/lib/bitcoin/client.js deleted file mode 100644 index fe94116..0000000 --- a/lib/bitcoin/client.js +++ /dev/null @@ -1,152 +0,0 @@ -var rpc = require('../jsonrpc'), - deprecate = require('deprecate'); - -//===----------------------------------------------------------------------===// -// jsonrpc wrappers -//===----------------------------------------------------------------------===// -var bitcoinAPI = { - addMultiSigAddress: 'addmultisigaddress', - addNode: 'addnode', // bitcoind v0.8.0+ - backupWallet: 'backupwallet', - createRawTransaction: 'createrawtransaction', // bitcoind v0.7.0+ - decodeRawTransaction: 'decoderawtransaction', // bitcoind v0.7.0+ - dumpPrivKey: 'dumpprivkey', - encryptWallet: 'encryptwallet', - getAccount: 'getaccount', - getAccountAddress: 'getaccountaddress', - getAddedNodeInfo: 'getaddednodeinfo', // bitcoind v0.8.0+ - getAddressesByAccount: 'getaddressesbyaccount', - getBalance: 'getbalance', - getBlock: 'getblock', - getBlockCount: 'getblockcount', - getBlockHash: 'getblockhash', - getConnectionCount: 'getconnectioncount', - getDifficulty: 'getdifficulty', - getGenerate: 'getgenerate', - getHashesPerSecond: 'gethashespersec', - getHashesPerSec: 'gethashespersec', - getInfo: 'getinfo', - getMemorypool: 'getmemorypool', - getMemoryPool: 'getmemorypool', - getMiningInfo: 'getmininginfo', - getNewAddress: 'getnewaddress', - getPeerInfo: 'getpeerinfo', // bitcoind v0.7.0+ - getRawMemPool: 'getrawmempool', // bitcoind v0.7.0+ - getRawTransaction: 'getrawtransaction', // bitcoind v0.7.0+ - getReceivedByAccount: 'getreceivedbyaccount', - getReceivedByAddress: 'getreceivedbyaddress', - getTransaction: 'gettransaction', - getWork: 'getwork', - help: 'help', - importPrivKey: 'importprivkey', - keypoolRefill: 'keypoolrefill', - keyPoolRefill: 'keypoolrefill', - listAccounts: 'listaccounts', - listLockUnspent: 'listlockunspent', // bitcoind v0.8.0 - listReceivedByAccount: 'listreceivedbyaccount', - listReceivedByAddress: 'listreceivedbyaddress', - listSinceBlock: 'listsinceblock', - listTransactions: 'listtransactions', - listUnspent: 'listunspent', // bitcoind v0.7.0+ - lockUnspent: 'lockunspent', // bitcoind v0.8.0+ - move: 'move', - sendFrom: 'sendfrom', - sendMany: 'sendmany', - sendRawTransaction: 'sendrawtransaction', // bitcoind v0.7.0+ - sendToAddress: 'sendtoaddress', - setAccount: 'setaccount', - setGenerate: 'setgenerate', - setTxFee: 'settxfee', - signMessage: 'signmessage', - signRawTransaction: 'signrawtransaction', // bitcoind v0.7.0+ - stop: 'stop', - validateAddress: 'validateaddress', - verifyMessage: 'verifymessage', - walletLock: 'walletlock', - walletPassphrase: 'walletpassphrase', - walletPassphraseChange: 'walletpassphrasechange' -}; - - -//===----------------------------------------------------------------------===// -// Client -// Pass in an object with host, port, user, pass -//===----------------------------------------------------------------------===// -function Client() { - var args = [].slice.call(arguments); - - if (args.length > 1) { - deprecate('calling bitcoin.Client with more than one argument is deprecated'); - this.host = args[0]; - this.port = args[1]; - this.user = args[2]; - this.pass = args[3]; - this.ssl = false; - this.sslStrict = false; - this.sslCa = null; - } else { - this.host = args[0].host; - this.port = args[0].port; - this.user = args[0].user; - this.pass = args[0].pass; - this.ssl = args[0].ssl ? true : false; - this.sslStrict = (typeof args[0].sslStrict === 'undefined' || args[0].sslStrict); - this.sslCa = args[0].sslCa; - } - - this.rpc = new rpc.Client(this.port, this.host, this.user, this.pass, - this.ssl, this.sslStrict, this.sslCa); -} - - -//===----------------------------------------------------------------------===// -// cmd -// Call custom jsonrpc commands -//===----------------------------------------------------------------------===// -Client.prototype.cmd = function() { - var args = [].slice.call(arguments); - var cmd = args.shift(); - - callRpc(cmd, args, this.rpc); -} - - -//===----------------------------------------------------------------------===// -// callRpc -//===----------------------------------------------------------------------===// -function callRpc(cmd, args, rpc) { - var fn = args[args.length-1]; - - // If the last function is a callback, pop it from the args list - if(typeof fn === 'function') { - args.pop(); - } else { - fn = function () {}; - } - - rpc.call(cmd, args, function(){ - var args = [].slice.call(arguments); - args.unshift(null); - fn.apply(this, args); - }, function(err){ - fn(err); - }); -} - -//===----------------------------------------------------------------------===// -// Initialize wrappers -//===----------------------------------------------------------------------===// -(function() { - for (var protoFn in bitcoinAPI) { - (function(protoFn) { - Client.prototype[protoFn] = function() { - var args = [].slice.call(arguments); - callRpc(bitcoinAPI[protoFn], args, this.rpc); - }; - })(protoFn); - } -})(); - - -// Export! -module.exports = Client; diff --git a/lib/bitcoin/index.js b/lib/bitcoin/index.js deleted file mode 100644 index fc3e662..0000000 --- a/lib/bitcoin/index.js +++ /dev/null @@ -1,2 +0,0 @@ - -module.exports.Client = require('./client'); diff --git a/package.json b/package.json index 70fc410..74c0987 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "bitcoin", "description": "Communicate with bitcoind via JSON-RPC", "version": "1.6.0", - "main": "./lib/bitcoin", + "main": "index.js", "keywords": [ "bitcoin", "rpc" diff --git a/test/index.js b/test/index.js index 58edc56..a8954c8 100644 --- a/test/index.js +++ b/test/index.js @@ -1,5 +1,5 @@ var assert = require('assert'), - bitcoin = require('../lib/bitcoin'), + bitcoin = require('../'), config = require('./config'), deprecate = require('deprecate'); diff --git a/test/ssl.js b/test/ssl.js index fe02123..cc93461 100644 --- a/test/ssl.js +++ b/test/ssl.js @@ -1,7 +1,7 @@ var assert = require('assert'), fs = require('fs'), clone = require('clone'), - bitcoin = require('../lib/bitcoin'), + bitcoin = require('../'), config = require('./config'); var getInfo = function(opts, cb) {