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