From b68164213253dc5f4f00c0eaf7bfc4e79e6e250f Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 25 Sep 2014 13:58:54 -0700 Subject: [PATCH] fixes. emit broadcast event. --- lib/bitcoind.js | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/bitcoind.js b/lib/bitcoind.js index 2ce76752..48b32b33 100644 --- a/lib/bitcoind.js +++ b/lib/bitcoind.js @@ -20,6 +20,10 @@ var bitcoin = Bitcoin; function Bitcoin(options) { var self = this; + if (Bitcoin.global) { + throw new Error('bitcoindjs cannot be instantiated more than once.'); + } + if (!(this instanceof Bitcoin)) { return new Bitcoin(options); } @@ -27,6 +31,8 @@ function Bitcoin(options) { EventEmitter.call(this); this.options = options; + + Bitcoin.global = this; } Bitcoin.prototype.__proto__ = EventEmitter.prototype; @@ -464,22 +470,31 @@ Transaction.broadcast = function(tx, options, callback) { options = {}; } - options.overrideFees = options.overrideFees || false; - options.ownOnly = options.ownOnly || false; + var fee = options.overrideFees = options.overrideFees || false; + var own = options.ownOnly = options.ownOnly || false; + + if (!callback) { + callback = function() {}; + } if (!tx.hex) { tx = bitcoin.tx(tx); tx.toHex(); } - return bitcoindjs.broadcastTx(tx, - options.overrideFees, - options.ownOnly, - callback); + return bitcoindjs.broadcastTx(tx, fee, own, function(err, hash) { + if (err) return callback(err); + bitcoin.global.emit('broadcast', hash); + return callback(null, err); + }); }; -Transaction.prototype.broadcast = function(callback) { - return Transaction.broadcast(this, callback); +Transaction.prototype.broadcast = function(options, callback) { + if (!callback) { + callback = options; + options = null; + } + return Transaction.broadcast(this, options, callback); }; /**