From b2fc191f54194daa0e1cba99d07e63f52e809cdd Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Thu, 25 Feb 2016 11:45:46 -0300 Subject: [PATCH] remove generation of change address when sending max --- lib/server.js | 77 ++++++++++++++++++++++++-------------- test/integration/server.js | 18 ++++----- 2 files changed, 57 insertions(+), 38 deletions(-) diff --git a/lib/server.js b/lib/server.js index 4b7becc..862c72f 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1174,7 +1174,6 @@ WalletService.prototype.getSendMaxInfo = function(opts, cb) { size: 0, amount: 0, fee: 0, - nbInputs: 0, inputs: [], }; @@ -1210,7 +1209,6 @@ WalletService.prototype.getSendMaxInfo = function(opts, cb) { info.size = txp.getEstimatedSize(); info.fee = txp.getEstimatedFee(); info.amount = _.sum(txp.inputs, 'satoshis') - info.fee; - info.nbInputs = txp.inputs.length; info.inputs = txp.inputs; return cb(null, info); @@ -1803,27 +1801,42 @@ WalletService.prototype._doCreateTx = function(opts, cb) { } self._runLocked(cb, function(cb) { - self.getWallet({}, function(err, wallet) { - if (err) return cb(err); - if (!wallet.isComplete()) return cb(Errors.WALLET_NOT_COMPLETE); - self._canCreateTx(function(err, canCreate) { - if (err) return cb(err); - if (!canCreate) return cb(Errors.TX_CANNOT_CREATE); + var wallet, txp, changeAddress; + async.series([ - if (opts.validateOutputs !== false) { - var validationError = self._validateOutputs(opts, wallet); - if (validationError) { - return cb(validationError); + function(next) { + self.getWallet({}, function(err, w) { + if (err) return next(err); + if (!w.isComplete()) return next(Errors.WALLET_NOT_COMPLETE); + wallet = w; + next(); + }); + }, + function(next) { + self._canCreateTx(function(err, canCreate) { + if (err) return next(err); + if (!canCreate) return next(Errors.TX_CANNOT_CREATE); + + if (opts.validateOutputs !== false) { + var validationError = self._validateOutputs(opts, wallet); + if (validationError) { + return next(validationError); + } } + next(); + }); + }, + function(next) { + if (!opts.sendMax) { + changeAddress = wallet.createAddress(true); } - var txOpts = { walletId: self.walletId, creatorId: self.copayerId, outputs: opts.outputs, message: opts.message, - changeAddress: wallet.createAddress(true), + changeAddress: changeAddress, feePerKb: opts.feePerKb, payProUrl: opts.payProUrl, walletM: wallet.m, @@ -1836,21 +1849,22 @@ WalletService.prototype._doCreateTx = function(opts, cb) { fee: opts.inputs && !_.isNumber(opts.feePerKb) ? opts.fee : null, }; - var txp = Model.TxProposal.create(txOpts); - - self._selectTxInputs(txp, opts.utxosToExclude, function(err) { - if (err) return cb(err); - - self.storage.storeAddressAndWallet(wallet, txp.changeAddress, function(err) { - if (err) return cb(err); - - self.storage.storeTx(wallet.id, txp, function(err) { - if (err) return cb(err); - return cb(null, txp); - }); - }); - }); - }); + txp = Model.TxProposal.create(txOpts); + next(); + }, + function(next) { + self._selectTxInputs(txp, opts.utxosToExclude, next); + }, + function(next) { + if (!changeAddress) return next(); + self.storage.storeAddressAndWallet(wallet, txp.changeAddress, next); + }, + function(next) { + self.storage.storeTx(wallet.id, txp, next); + }, + ], function(err) { + if (err) return cb(err); + return cb(null, txp); }); }); }; @@ -1883,6 +1897,11 @@ WalletService.prototype.createTx = function(opts, cb) { if (!_.isArray(opts.outputs) || opts.outputs.length > 1) { return next(new ClientError('Only one output allowed when sendMax is specified')); } + if (_.isNumber(opts.outputs[0].amount)) + return next(new ClientError('Amount is not allowed when sendMax is specified')); + if (_.isNumber(opts.fee)) + return next(new ClientError('Fee is not allowed when sendMax is specified (use feePerKb instead)')); + self.getSendMaxInfo(opts, function(err, info) { if (err) return next(err); opts.outputs[0].amount = info.amount; diff --git a/test/integration/server.js b/test/integration/server.js index 54e4810..dfa68ac 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -3127,7 +3127,7 @@ describe('Wallet service', function() { server.createTx(txOpts, function(err, tx) { should.not.exist(err); should.exist(tx); - // should.not.exist(tx.changeAddress); + should.not.exist(tx.changeAddress); tx.amount.should.equal(3e8 - tx.fee); var t = tx.getBitcoreTx(); @@ -3645,7 +3645,7 @@ describe('Wallet service', function() { server.createTx(txOpts, function(err, tx) { should.not.exist(err); should.exist(tx); - tx.inputs.length.should.equal(info.nbInputs); + tx.inputs.length.should.equal(info.inputs.length); return cb(); }); }; @@ -3659,7 +3659,7 @@ describe('Wallet service', function() { info.size.should.equal(0); info.amount.should.equal(0); info.fee.should.equal(0); - info.nbInputs.should.equal(0); + info.inputs.should.be.empty; done(); }); }); @@ -3670,7 +3670,7 @@ describe('Wallet service', function() { }, function(err, info) { should.not.exist(err); should.exist(info); - info.nbInputs.should.equal(4); + info.inputs.length.should.equal(4); info.size.should.equal(1342); info.fee.should.equal(info.size * 10000 / 1000.); info.amount.should.equal(1e8 - info.fee); @@ -3686,7 +3686,7 @@ describe('Wallet service', function() { }, function(err, info) { should.not.exist(err); should.exist(info); - info.nbInputs.should.equal(3); + info.inputs.length.should.equal(3); info.size.should.equal(1031); info.fee.should.equal(info.size * 10000 / 1000.); info.amount.should.equal(0.9e8 - info.fee); @@ -3710,7 +3710,7 @@ describe('Wallet service', function() { }, function(err, info) { should.not.exist(err); should.exist(info); - info.nbInputs.should.equal(2); + info.inputs.length.should.equal(2); info.size.should.equal(720); info.fee.should.equal(info.size * 10000 / 1000.); info.amount.should.equal(0.2e8 - info.fee); @@ -3726,7 +3726,7 @@ describe('Wallet service', function() { }, function(err, info) { should.not.exist(err); should.exist(info); - info.nbInputs.should.equal(4); + info.inputs.length.should.equal(4); info.size.should.equal(1342); info.fee.should.equal(info.size * 0.001e8 / 1000.); info.amount.should.equal(1e8 - info.fee); @@ -3735,7 +3735,7 @@ describe('Wallet service', function() { }, function(err, info) { should.not.exist(err); should.exist(info); - info.nbInputs.should.equal(6); + info.inputs.length.should.equal(6); info.size.should.equal(1964); info.fee.should.equal(info.size * 0.0001e8 / 1000.); info.amount.should.equal(1.0005e8 - info.fee); @@ -3754,7 +3754,7 @@ describe('Wallet service', function() { should.not.exist(err); should.exist(info); info.size.should.be.below(2000); - info.nbInputs.should.be.below(9); + info.inputs.length.should.be.below(9); Defaults.MAX_TX_SIZE_IN_KB = _oldDefault; sendTx(info, done); });