This commit is contained in:
Ivan Socolsky 2015-01-30 18:29:46 -03:00
parent 94509eebab
commit 9f30af2690
2 changed files with 30 additions and 9 deletions

View File

@ -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);

View File

@ -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();
});
});
});
});