Browse Source

remove generation of change address when sending max

activeAddress
Ivan Socolsky 9 years ago
parent
commit
b2fc191f54
  1. 77
      lib/server.js
  2. 18
      test/integration/server.js

77
lib/server.js

@ -1174,7 +1174,6 @@ WalletService.prototype.getSendMaxInfo = function(opts, cb) {
size: 0, size: 0,
amount: 0, amount: 0,
fee: 0, fee: 0,
nbInputs: 0,
inputs: [], inputs: [],
}; };
@ -1210,7 +1209,6 @@ WalletService.prototype.getSendMaxInfo = function(opts, cb) {
info.size = txp.getEstimatedSize(); info.size = txp.getEstimatedSize();
info.fee = txp.getEstimatedFee(); info.fee = txp.getEstimatedFee();
info.amount = _.sum(txp.inputs, 'satoshis') - info.fee; info.amount = _.sum(txp.inputs, 'satoshis') - info.fee;
info.nbInputs = txp.inputs.length;
info.inputs = txp.inputs; info.inputs = txp.inputs;
return cb(null, info); return cb(null, info);
@ -1803,27 +1801,42 @@ WalletService.prototype._doCreateTx = function(opts, cb) {
} }
self._runLocked(cb, function(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) { var wallet, txp, changeAddress;
if (err) return cb(err); async.series([
if (!canCreate) return cb(Errors.TX_CANNOT_CREATE);
if (opts.validateOutputs !== false) { function(next) {
var validationError = self._validateOutputs(opts, wallet); self.getWallet({}, function(err, w) {
if (validationError) { if (err) return next(err);
return cb(validationError); 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 = { var txOpts = {
walletId: self.walletId, walletId: self.walletId,
creatorId: self.copayerId, creatorId: self.copayerId,
outputs: opts.outputs, outputs: opts.outputs,
message: opts.message, message: opts.message,
changeAddress: wallet.createAddress(true), changeAddress: changeAddress,
feePerKb: opts.feePerKb, feePerKb: opts.feePerKb,
payProUrl: opts.payProUrl, payProUrl: opts.payProUrl,
walletM: wallet.m, walletM: wallet.m,
@ -1836,21 +1849,22 @@ WalletService.prototype._doCreateTx = function(opts, cb) {
fee: opts.inputs && !_.isNumber(opts.feePerKb) ? opts.fee : null, fee: opts.inputs && !_.isNumber(opts.feePerKb) ? opts.fee : null,
}; };
var txp = Model.TxProposal.create(txOpts); txp = Model.TxProposal.create(txOpts);
next();
self._selectTxInputs(txp, opts.utxosToExclude, function(err) { },
if (err) return cb(err); function(next) {
self._selectTxInputs(txp, opts.utxosToExclude, next);
self.storage.storeAddressAndWallet(wallet, txp.changeAddress, function(err) { },
if (err) return cb(err); function(next) {
if (!changeAddress) return next();
self.storage.storeTx(wallet.id, txp, function(err) { self.storage.storeAddressAndWallet(wallet, txp.changeAddress, next);
if (err) return cb(err); },
return cb(null, txp); 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) { if (!_.isArray(opts.outputs) || opts.outputs.length > 1) {
return next(new ClientError('Only one output allowed when sendMax is specified')); 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) { self.getSendMaxInfo(opts, function(err, info) {
if (err) return next(err); if (err) return next(err);
opts.outputs[0].amount = info.amount; opts.outputs[0].amount = info.amount;

18
test/integration/server.js

@ -3127,7 +3127,7 @@ describe('Wallet service', function() {
server.createTx(txOpts, function(err, tx) { server.createTx(txOpts, function(err, tx) {
should.not.exist(err); should.not.exist(err);
should.exist(tx); should.exist(tx);
// should.not.exist(tx.changeAddress); should.not.exist(tx.changeAddress);
tx.amount.should.equal(3e8 - tx.fee); tx.amount.should.equal(3e8 - tx.fee);
var t = tx.getBitcoreTx(); var t = tx.getBitcoreTx();
@ -3645,7 +3645,7 @@ describe('Wallet service', function() {
server.createTx(txOpts, function(err, tx) { server.createTx(txOpts, function(err, tx) {
should.not.exist(err); should.not.exist(err);
should.exist(tx); should.exist(tx);
tx.inputs.length.should.equal(info.nbInputs); tx.inputs.length.should.equal(info.inputs.length);
return cb(); return cb();
}); });
}; };
@ -3659,7 +3659,7 @@ describe('Wallet service', function() {
info.size.should.equal(0); info.size.should.equal(0);
info.amount.should.equal(0); info.amount.should.equal(0);
info.fee.should.equal(0); info.fee.should.equal(0);
info.nbInputs.should.equal(0); info.inputs.should.be.empty;
done(); done();
}); });
}); });
@ -3670,7 +3670,7 @@ describe('Wallet service', function() {
}, function(err, info) { }, function(err, info) {
should.not.exist(err); should.not.exist(err);
should.exist(info); should.exist(info);
info.nbInputs.should.equal(4); info.inputs.length.should.equal(4);
info.size.should.equal(1342); info.size.should.equal(1342);
info.fee.should.equal(info.size * 10000 / 1000.); info.fee.should.equal(info.size * 10000 / 1000.);
info.amount.should.equal(1e8 - info.fee); info.amount.should.equal(1e8 - info.fee);
@ -3686,7 +3686,7 @@ describe('Wallet service', function() {
}, function(err, info) { }, function(err, info) {
should.not.exist(err); should.not.exist(err);
should.exist(info); should.exist(info);
info.nbInputs.should.equal(3); info.inputs.length.should.equal(3);
info.size.should.equal(1031); info.size.should.equal(1031);
info.fee.should.equal(info.size * 10000 / 1000.); info.fee.should.equal(info.size * 10000 / 1000.);
info.amount.should.equal(0.9e8 - info.fee); info.amount.should.equal(0.9e8 - info.fee);
@ -3710,7 +3710,7 @@ describe('Wallet service', function() {
}, function(err, info) { }, function(err, info) {
should.not.exist(err); should.not.exist(err);
should.exist(info); should.exist(info);
info.nbInputs.should.equal(2); info.inputs.length.should.equal(2);
info.size.should.equal(720); info.size.should.equal(720);
info.fee.should.equal(info.size * 10000 / 1000.); info.fee.should.equal(info.size * 10000 / 1000.);
info.amount.should.equal(0.2e8 - info.fee); info.amount.should.equal(0.2e8 - info.fee);
@ -3726,7 +3726,7 @@ describe('Wallet service', function() {
}, function(err, info) { }, function(err, info) {
should.not.exist(err); should.not.exist(err);
should.exist(info); should.exist(info);
info.nbInputs.should.equal(4); info.inputs.length.should.equal(4);
info.size.should.equal(1342); info.size.should.equal(1342);
info.fee.should.equal(info.size * 0.001e8 / 1000.); info.fee.should.equal(info.size * 0.001e8 / 1000.);
info.amount.should.equal(1e8 - info.fee); info.amount.should.equal(1e8 - info.fee);
@ -3735,7 +3735,7 @@ describe('Wallet service', function() {
}, function(err, info) { }, function(err, info) {
should.not.exist(err); should.not.exist(err);
should.exist(info); should.exist(info);
info.nbInputs.should.equal(6); info.inputs.length.should.equal(6);
info.size.should.equal(1964); info.size.should.equal(1964);
info.fee.should.equal(info.size * 0.0001e8 / 1000.); info.fee.should.equal(info.size * 0.0001e8 / 1000.);
info.amount.should.equal(1.0005e8 - info.fee); info.amount.should.equal(1.0005e8 - info.fee);
@ -3754,7 +3754,7 @@ describe('Wallet service', function() {
should.not.exist(err); should.not.exist(err);
should.exist(info); should.exist(info);
info.size.should.be.below(2000); 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; Defaults.MAX_TX_SIZE_IN_KB = _oldDefault;
sendTx(info, done); sendTx(info, done);
}); });

Loading…
Cancel
Save