From 6421790bb1e67955a6860f6ec3b6b60a02281592 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Thu, 13 Aug 2015 16:06:14 -0300 Subject: [PATCH 1/5] test --- test/integration/server.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/test/integration/server.js b/test/integration/server.js index 0dceb24..fd3086f 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -2571,7 +2571,7 @@ describe('Wallet service', function() { }); }); - describe('#broadcastTx', function() { + describe('#broadcastTx & #braodcastRawTx', function() { var server, wallet, txpid; beforeEach(function(done) { helpers.createAndJoinWallet(1, 1, function(s, w) { @@ -2619,6 +2619,18 @@ describe('Wallet service', function() { }); }); + it('should broadcast a raw tx', function(done) { + helpers.stubBroadcast('999'); + server.broadcastRawTx({ + network: 'testnet', + rawTx: 'raw tx', + }, function(err, txid) { + should.not.exist(err); + txid.should.equal('999'); + done(); + }); + }); + it('should fail to brodcast a tx already marked as broadcasted', function(done) { helpers.stubBroadcast('999'); server.broadcastTx({ From a1835c76a24d53281667e7a6a07d222ab563d347 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Thu, 13 Aug 2015 16:06:22 -0300 Subject: [PATCH 2/5] 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 From 4502dd284ac0ebd30e2e613153e36b1d819901c6 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Thu, 13 Aug 2015 16:08:27 -0300 Subject: [PATCH 3/5] express endpoint --- lib/expressapp.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/expressapp.js b/lib/expressapp.js index 5944b5e..3324372 100644 --- a/lib/expressapp.js +++ b/lib/expressapp.js @@ -288,6 +288,16 @@ ExpressApp.prototype.start = function(opts, cb) { }); }); + router.post('/v1/broadcast_raw/', function(req, res) { + getServerWithAuth(req, res, function(server) { + server.broadcastRawTx(req.body, function(err, txp) { + if (err) return returnError(err, res, req); + res.json(txp); + res.end(); + }); + }); + }); + router.post('/v1/txproposals/:id/signatures/', function(req, res) { getServerWithAuth(req, res, function(server) { req.body.txProposalId = req.params['id']; From dd778de47599c462b1d69ddedb7dc0da694db0a7 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Thu, 13 Aug 2015 16:40:50 -0300 Subject: [PATCH 4/5] v0.1.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fa1209c..aa40182 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "bitcore-wallet-service", "description": "A service for Mutisig HD Bitcoin Wallets", "author": "BitPay Inc", - "version": "0.1.4", + "version": "0.1.5", "keywords": [ "bitcoin", "copay", From c0f673f1ed0202a1fe661c4acf1a9d526b897afd Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Thu, 13 Aug 2015 18:13:34 -0300 Subject: [PATCH 5/5] fix result type --- lib/expressapp.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/expressapp.js b/lib/expressapp.js index 3324372..55a838a 100644 --- a/lib/expressapp.js +++ b/lib/expressapp.js @@ -290,9 +290,9 @@ ExpressApp.prototype.start = function(opts, cb) { router.post('/v1/broadcast_raw/', function(req, res) { getServerWithAuth(req, res, function(server) { - server.broadcastRawTx(req.body, function(err, txp) { + server.broadcastRawTx(req.body, function(err, txid) { if (err) return returnError(err, res, req); - res.json(txp); + res.json(txid); res.end(); }); });