From a1835c76a24d53281667e7a6a07d222ab563d347 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Thu, 13 Aug 2015 16:06:22 -0300 Subject: [PATCH] broadcast raw tx --- lib/server.js | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/server.js b/lib/server.js index d12a04b..9ad1b96 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1181,21 +1181,34 @@ WalletService.prototype.removePendingTx = function(opts, cb) { }); }; - -WalletService.prototype._broadcastTx = function(txp, cb) { - var raw; - try { - raw = txp.getRawTx(); - } catch (ex) { - return cb(ex); - } - var bc = this._getBlockchainExplorer(txp.getNetworkName()); +WalletService.prototype._broadcastRawTx = function(network, raw, cb) { + var bc = this._getBlockchainExplorer(network); bc.broadcast(raw, function(err, txid) { if (err) return cb(err); return cb(null, txid); }) }; +/** + * Broadcast a raw transaction. + * @param {Object} opts + * @param {string} [opts.network = 'livenet'] - The Bitcoin network for this transaction. + * @param {string} opts.rawTx - Raw tx data. + */ +WalletService.prototype.broadcastRawTx = function(opts, cb) { + var self = this; + + if (!Utils.checkRequired(opts, ['network', 'rawTx'])) + return cb(new ClientError('Required argument missing')); + + var network = opts.network || 'livenet'; + if (network != 'livenet' && network != 'testnet') + return cb(new ClientError('Invalid network')); + + self._broadcastRawTx(network, opts.rawTx, cb); +}; + + WalletService.prototype._checkTxInBlockchain = function(txp, cb) { var tx = txp.getBitcoreTx(); var bc = this._getBlockchainExplorer(txp.getNetworkName()); @@ -1308,7 +1321,13 @@ WalletService.prototype.broadcastTx = function(opts, cb) { if (txp.status == 'broadcasted') return cb(Errors.TX_ALREADY_BROADCASTED); if (txp.status != 'accepted') return cb(Errors.TX_NOT_ACCEPTED); - self._broadcastTx(txp, function(err, txid) { + var raw; + try { + raw = txp.getRawTx(); + } catch (ex) { + return cb(ex); + } + self._broadcastRawTx(txp.getNetworkName(), raw, function(err, txid) { if (err) { var broadcastErr = err; // Check if tx already in blockchain