From 9f30af26909de63fe5f89f82c4fea45274aed3b1 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Fri, 30 Jan 2015 18:29:46 -0300 Subject: [PATCH] . --- lib/server.js | 30 +++++++++++++++++++++++------- test/integration.js | 9 +++++++-- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/lib/server.js b/lib/server.js index d12c60b..94ec2e3 100644 --- a/lib/server.js +++ b/lib/server.js @@ -226,7 +226,7 @@ CopayServer.prototype._getUtxos = function (opts, cb) { if (err) return cb(err); var inputs = _.chain(txps) - .pluck('input') + .pluck('inputs') .flatten() .map(function (utxo) { return utxo.txid + '|' + utxo.vout }); @@ -260,23 +260,39 @@ CopayServer.prototype._getUtxos = function (opts, cb) { if (err) return cb(err); var balance = {}; - balance.totalAmount = _.reduce(utxos, function(sum, utxo) { return sum + utxo.amount; }); - balance.lockedAmount = _.reduce(_.without(utxos, { locked: true }), function(sum, utxo) { return sum + utxo.amount; }); + balance.totalAmount = _.reduce(utxos, function(sum, utxo) { return sum + utxo.amount; }, 0); + balance.lockedAmount = _.reduce(_.without(utxos, { locked: true }), function(sum, utxo) { return sum + utxo.amount; }, 0); return cb(null, balance); }); }; -CopayServer.prototype._createRawTx = function (txp, utxos) { +CopayServer.prototype._createRawTx = function (txp) { var rawTx = new Bitcore.Transaction() - .from(utxos) + .from(tx.inputs) .to(txp.toAddress, txp.amount) .change(txp.changeAddress); return rawTx; }; +CopayServer.prototype._selectUtxos = function (txp, utxos) { + var i = 0; + var total = 0; + var selected = []; + var inputs = _.sortBy(utxos, 'amount'); + while (i < inputs.length) { + selected.push(inputs[i]); + total += inputs[i].amount; + if (total >= txp.amount) { + break; + } + i++; + }; + return selected; +}; + /** * Creates a new transaction proposal. @@ -303,13 +319,13 @@ CopayServer.prototype.createTx = function (opts, cb) { creatorId: opts.copayerId, toAddress: opts.toAddress, amount: opts.amount, + inputs: self._selectUtxos(opts.amount, utxos), changeAddress: opts.changeAddress, requiredSignatures: wallet.m, maxRejections: wallet.n - wallet.m, }); - txp.rawTx = self._createRawTx(txp, utxos); - // TODO: assign used inputs + txp.rawTx = self._createRawTx(txp); self.storage.storeTx(wallet.id, txp, function (err) { if (err) return cb(err); diff --git a/test/integration.js b/test/integration.js index dfcd580..ee7db7c 100644 --- a/test/integration.js +++ b/test/integration.js @@ -423,7 +423,7 @@ describe('Copay server', function() { copayerId: '1', walletId: '123', toAddress: 'dummy', - amount: 100, + amount: 80, message: 'some message', otToken: 'dummy', requestSignature: 'dummy', @@ -437,7 +437,12 @@ describe('Copay server', function() { server.getPendingTxs({ walletId: '123' }, function (err, txs) { should.not.exist(err); txs.length.should.equal(1); - done(); + server.getBalance({ walletId: '123' }, function (err, balance) { + should.not.exist(err); + balance.totalAmount.should.equal(300); + balance.lockedAmount.should.equal(200); + done(); + }); }); }); });