bitcoin-utxo-select
Version:
Bitcoin utxo selections
39 lines (38 loc) • 1.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.accumulative = void 0;
const utils_1 = require("../utils");
function accumulative(preInputs, utxos, outputs, feeRate, changeAddress, changeOutput) {
let bytesAccum = (0, utils_1.transactionBytes)(preInputs, outputs);
let inAccum = 0;
let inputs = [];
let outAccum = (0, utils_1.sumValues)(outputs);
for (const preInput of preInputs) {
inAccum += preInput.value;
inputs.push(preInput);
}
const fee = feeRate * bytesAccum;
if (inAccum > outAccum + fee) {
return (0, utils_1.finalize)(inputs, outputs, feeRate, changeAddress, changeOutput);
}
for (const utxo of utxos) {
const utxoBytes = (0, utils_1.inputBytes)(utxo);
const utxoFee = feeRate * utxoBytes;
const utxoValue = utxo.value;
// find another utxo candidate
if (utxoFee > utxoValue)
continue;
bytesAccum += utxoBytes;
inAccum += utxoValue;
inputs.push(utxo);
const fee = feeRate * bytesAccum;
// let's add others utxos as well
if (inAccum < outAccum + fee)
continue;
return (0, utils_1.finalize)(inputs, outputs, feeRate, changeAddress, changeOutput);
}
return {
fee: feeRate * bytesAccum,
};
}
exports.accumulative = accumulative;