forked from bitcoinjs/coinselect
-
Notifications
You must be signed in to change notification settings - Fork 28
/
blackjack.js
35 lines (27 loc) · 1.09 KB
/
blackjack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var utils = require('./utils')
var ext = require('./bn-extensions')
// only add inputs if they don't bust the target value (aka, exact match)
// worst-case: O(n)
module.exports = function blackjack (utxos, outputs, feeRate) {
if (!utils.uintOrNull(feeRate)) return {}
var bytesAccum = utils.transactionBytes([], outputs)
var inAccum = ext.BN_ZERO
var inputs = []
var outAccum = utils.sumOrNaN(outputs)
var threshold = utils.dustThreshold({}, feeRate)
for (var i = 0; i < utxos.length; ++i) {
var input = utxos[i]
var inputBytes = utils.inputBytes(input)
var fee = ext.mul(feeRate, ext.add(bytesAccum, inputBytes))
var inputValue = utils.uintOrNull(input.value)
// would it waste value?
if (ext.gt(ext.add(inAccum, inputValue), ext.add(outAccum, fee, threshold))) continue
bytesAccum = ext.add(bytesAccum, inputBytes)
inAccum = ext.add(inAccum, inputValue)
inputs.push(input)
// go again?
if (ext.lt(inAccum, ext.add(outAccum, fee))) continue
return utils.finalize(inputs, outputs, feeRate)
}
return { fee: ext.mul(feeRate, bytesAccum) }
}