diff --git a/example/index.js b/example/index.js index 963f8a37..c309e6d2 100755 --- a/example/index.js +++ b/example/index.js @@ -1,46 +1,63 @@ #!/usr/bin/env node +/** + * bitcoind.js example + */ + process.title = 'bitcoind.js'; var util = require('util'); + +/** + * bitcoind + */ + var bitcoind = require('../')(); var genesisBlock = '0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'; var genesisTx = '0x4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b'; -bitcoind.start(function(err) { - bitcoind.on('error', function(err) { - console.log('bitcoind: error="%s"', err.message); +bitcoind.on('error', function(err) { + print('bitcoind: error="%s"', err.message); +}); + +bitcoind.on('open', function(status) { + print('bitcoind: status="%s"', status); + + // getBlocks(bitcoind); + + // bitcoind.on('block', function(block) { + // print('Found block:'); + // print(block); + // }); + + // bitcoind.on('tx', function(tx) { + // print('Found tx:'); + // print(tx); + // }); + + bitcoind.once('tx', function(tx) { + print('Broadcasting tx...'); + return tx.broadcast(function(err, hash, tx) { + if (err) throw err; + print('tx hash: %s', hash); + print(tx); + }); }); - bitcoind.on('open', function(status) { - console.log('bitcoind: status="%s"', status); - // getBlocks(bitcoind); - // bitcoind.on('block', function(block) { - // print('Found block:'); - // print(block); - // }); - // bitcoind.on('tx', function(tx) { - // print('Found tx:'); - // print(tx); - // }); - bitcoind.once('tx', function(tx) { - console.log('Broadcasting tx...'); - tx.broadcast(function(err, hash, tx) { - if (err) throw err; - console.log('tx hash: %s', hash); - print(tx); - }); - }); - bitcoind.on('mptx', function(mptx) { - print('Found mempool tx:'); - print(mptx); - }); + + bitcoind.on('mptx', function(mptx) { + print('Found mempool tx:'); + print(mptx); }); }); +/** + * Helpers + */ + function getBlocks(bitcoind) { - setTimeout(function() { - (function next(hash) { + return setTimeout(function() { + return (function next(hash) { return bitcoind.getBlock(hash, function(err, block) { if (err) return print(err.message); print(block); @@ -56,7 +73,7 @@ function getBlocks(bitcoind) { }); } if (process.argv[2] === '-r' && block.nextblockhash) { - setTimeout(next.bind(null, block.nextblockhash), 500); + return setTimeout(next.bind(null, block.nextblockhash), 500); } }); })(genesisBlock); @@ -68,5 +85,8 @@ function inspect(obj) { } function print(obj) { + if (typeof obj === 'string') { + return process.stdout.write(util.format.apply(util, arguments) + '\n'); + } return process.stdout.write(inspect(obj) + '\n'); } diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 4b48e2a7..5ccbf877 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -33,6 +33,12 @@ function Bitcoin(options) { this.options = options; Bitcoin.global = this; + + this.on('newListener', function(name) { + if (name === 'open') { + self.start(); + } + }); } Bitcoin.prototype.__proto__ = EventEmitter.prototype; @@ -40,6 +46,9 @@ Bitcoin.prototype.__proto__ = EventEmitter.prototype; Bitcoin.prototype.start = function(callback) { var self = this; + if (this._started) return; + this._started = true; + var none = {}; var isSignal = {}; var sigint = { name: 'SIGINT', signal: isSignal };