Browse Source

add broadcastTx

activeAddress
Matias Alejo Garcia 10 years ago
parent
commit
1a8eaf0c7f
  1. 11
      app.js
  2. 1
      bit-wallet/bit
  3. 30
      bit-wallet/bit-broadcast
  4. 10
      lib/client/API.js
  5. 45
      lib/server.js

11
app.js

@ -204,6 +204,17 @@ router.post('/v1/txproposals/:id/signatures/', function(req, res) {
});
});
// TODO Check HTTP verb and URL name
router.post('/v1/txproposals/:id/broadcast/', function(req, res) {
getServerWithAuth(req, res, function(server) {
req.body.txProposalId = req.params['id'];
server.broadcastTx(req.body, function(err, txp) {
if (err) return returnError(err, res, req);
res.end();
});
});
});
router.post('/v1/txproposals/:id/rejections', function(req, res) {
getServerWithAuth(req, res, function(server) {
req.body.txProposalId = req.params['id'];

1
bit-wallet/bit

@ -13,6 +13,7 @@ program
.command('send <address> <amount> <note>', 'send bitcoins')
.command('sign <txpId>', 'sign a transaction proposal')
.command('reject <txpId> [reason]', 'reject a transaction proposal')
.command('broadcast <txpId>', 'broadcast a transaction proposal to the Bitcoin network')
.command('rm <txpId>', 'remove a transaction proposal')
.parse(process.argv);

30
bit-wallet/bit-broadcast

@ -0,0 +1,30 @@
#!/usr/bin/env node
var _ = require('lodash');
var program = require('commander');
var Client = require('../lib/client');
var utils = require('./cli-utils');
program
.version('0.0.1')
.option('-c,--config [file]', 'Wallet config filename')
.option('-v,--verbose', 'be verbose')
.usage('[options] <txpid>')
.parse(process.argv);
var args = program.args;
if (!args[0])
program.help();
var txpid = args[0];
var client = utils.getClient(program);
client.getTxProposals({}, function(err, txps) {
utils.die(err);
var txp = utils.findOneTxProposal(txps, txpid);
client.broadcastTxProposal(txp, function(err, txid) {
utils.die(err);
console.log('TX Broadcasted: ',txid);
});
});

10
lib/client/API.js

@ -360,6 +360,16 @@ API.prototype.rejectTxProposal = function(txp, reason, cb) {
this._doPostRequest(url, args, data, cb);
};
API.prototype.broadcastTxProposal = function(txp, cb) {
var self = this;
var data = this._loadAndCheck();
var url = '/v1/txproposals/' + txp.id + '/broadcast/';
this._doPostRequest(url, {}, data, cb);
};
API.prototype.removeTxProposal = function(txp, cb) {
var self = this;
var data = this._loadAndCheck();

45
lib/server.js

@ -660,6 +660,51 @@ CopayServer.prototype.signTx = function(opts, cb) {
});
};
/**
* Broadcast a transaction proposal.
* @param {Object} opts
* @param {string} opts.txProposalId - The identifier of the transaction.
*/
CopayServer.prototype.broadcastTx = function(opts, cb) {
var self = this;
if (!Utils.checkRequired(opts, ['txProposalId']))
return cb(new ClientError('Required argument missing'));
self.getWallet({}, function(err, wallet) {
if (err) return cb(err);
self.getTx({
id: opts.txProposalId
}, function(err, txp) {
if (err) return cb(err);
if (txp.status == 'broadcasted')
return cb(new ClientError('TXALREADYBROADCASTED', 'The transaction proposal is already broadcasted'));
if (txp.status != 'accepted')
return cb(new ClientError('TXNOTACCEPTED', 'The transaction proposal is not accepted'));
self._broadcastTx(txp, function(err, txid) {
if (err) return cb(err);
txp.setBroadcasted(txid);
self.storage.storeTx(self.walletId, txp, function(err) {
if (err) return cb(err);
self._notify('NewOutgoingTx', {
txProposalId: opts.txProposalId,
txid: txid
});
return cb(null, txp);
});
});
});
});
};
/**
* Reject a transaction proposal.
* @param {Object} opts

Loading…
Cancel
Save