broadcast raw tx

This commit is contained in:
Ivan Socolsky 2015-08-13 16:06:22 -03:00
parent 6421790bb1
commit a1835c76a2
1 changed files with 29 additions and 10 deletions

View File

@ -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