diff --git a/lib/server.js b/lib/server.js index d9d9e07..e4d8265 100644 --- a/lib/server.js +++ b/lib/server.js @@ -172,7 +172,7 @@ CopayServer.prototype.verifyMessageSignature = function (opts, cb) { var self = this; self.getWallet({ id: opts.walletId }, function (err, wallet) { - if (err || !wallet) return cb(err); + if (err) return cb(err); var copayer = wallet.getCopayer(opts.copayerId); if (!copayer) return cb('Copayer not found'); @@ -294,37 +294,69 @@ CopayServer.prototype._broadcastTx = function (rawTx, cb) { CopayServer.prototype.signTx = function (opts, cb) { var self = this; - self.getWallet({ id: opts.walletId }, function (err, wallet) { + self.fetchTx(opts.walletId, opts.txProposalId, function (err, txp) { if (err) return cb(err); + if (!txp) return cb('Transaction proposal not found'); + var action = _.find(txp.actions, { copayerId: opts.copayerId }); + if (action) return cb('Copayer already voted on this transaction proposal'); + if (txp.status != 'pending') return cb('The transaction proposal is not pending'); - self.fetchTx(wallet.id, opts.txProposalId, function (err, txp) { - if (err) return cb(err); - if (!txp) return cb('Transaction proposal not found'); - var action = _.find(txp.actions, { copayerId: opts.copayerId }); - if (action) return cb('Copayer already acted upon this transaction proposal'); - if (txp.status != 'pending') return cb('The transaction proposal is not pending'); + txp.sign(opts.copayerId, opts.signature); - txp.sign(opts.copayerId, opts.signature); + self.storage.storeTx(opts.walletId, txp, function (err) { + if (err) return cb(err); - self.storage.storeTx(wallet.id, txp, function (err) { + if (txp.status == 'accepted'); + self._broadcastTx(txp.rawTx, function (err, txid) { if (err) return cb(err); - if (txp.status == 'accepted'); - self._broadcastTx(txp.rawTx, function (err, txid) { + tx.setBroadcasted(txid); + self.storage.storeTx(opts.walletId, txp, function (err) { if (err) return cb(err); - tx.setBroadcasted(txid); - self.storage.storeTx(wallet.id, txp, function (err) { - if (err) return cb(err); - - return cb(); - }); + return cb(); }); }); }); }); }; +/** + * Reject a transaction proposal. + * @param {Object} opts + * @param {string} opts.walletId - The wallet id. + * @param {string} opts.copayerId - The wallet id. + * @param {string} opts.txProposalId - The identifier of the transaction. + */ +CopayServer.prototype.rejectTx = function (opts, cb) { + var self = this; + + self.fetchTx(opts.walletId, opts.txProposalId, function (err, txp) { + if (err) return cb(err); + if (!txp) return cb('Transaction proposal not found'); + var action = _.find(txp.actions, { copayerId: opts.copayerId }); + if (action) return cb('Copayer already voted on this transaction proposal'); + if (txp.status != 'pending') return cb('The transaction proposal is not pending'); + + txp.reject(opts.copayerId); + + self.storage.storeTx(opts.walletId, txp, function (err) { + if (err) return cb(err); + + if (txp.status == 'accepted'); + self._broadcastTx(txp.rawTx, function (err, txid) { + if (err) return cb(err); + + tx.setBroadcasted(txid); + self.storage.storeTx(opts.walletId, txp, function (err) { + if (err) return cb(err); + + return cb(); + }); + }); + }); + }); +}; /** * Retrieves all pending transaction proposals. @@ -336,16 +368,12 @@ CopayServer.prototype.signTx = function (opts, cb) { CopayServer.prototype.getPendingTxs = function (opts, cb) { var self = this; - self.getWallet({ id: opts.walletId }, function (err, wallet) { + self.storage.fetchTxs(opts.walletId, function (err, txps) { if (err) return cb(err); - self.storage.fetchTxs(wallet.id, function (err, txps) { - if (err) return cb(err); - - var pending = _.filter(txps, { status: 'pending' }); + var pending = _.filter(txps, { status: 'pending' }); - return cb(null, pending); - }); + return cb(null, pending); }); };