bitcoinjs/coinselect

Name: coinselect

Owner: BitcoinJS

Description: An unspent transaction output (UTXO) selection module for bitcoin.

Created: 2015-09-21 03:35:19.0

Updated: 2018-04-26 08:09:46.0

Pushed: 2018-04-16 01:51:10.0

Homepage:

Size: 120

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

coinselect

TRAVIS NPM

js-standard-style

An unspent transaction output (UTXO) selection module for bitcoin.

Algorithms

Module | Algorithm | Re-orders UTXOs? -|-|- require('coinselect') | Blackjack, with Accumulative fallback | By Descending Value require('coinselect/accumulative') | Accumulative - accumulates inputs until the target value is reached, skipping detrimental inputs | - require('coinselect/blackjack') | Blackjack - accumulates inputs until the target value is matched, does not accumulate inputs that go over the target value (within a threshold) | - require('coinselect/break') | Break - breaks the input values into equal denominations of output (as provided) | - require('coinselect/split') | Split - splits the input values evenly between all outputs, any provided output with .value remains unchanged | -

Note: Each algorithm will add a change output if the input - output value difference is over a dust threshold. This is calculated independently by utils.finalize, irrespective of the algorithm chosen, for the purposes of safety.

Pro-tip: if you want to send-all inputs to an output address, coinselect/split with a partial output (.address defined, no .value) can be used to send-all, while leaving an appropriate amount for the fee.

Example
coinSelect = require('coinselect')
feeRate = 55 // satoshis per byte
utxos = [
.,

txId: '...',
vout: 0,
...,
value: 10000


targets = [
.,

address: '1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm',
value: 5000



..
{ inputs, outputs, fee } = coinSelect(utxos, targets, feeRate)

he accumulated fee is always returned for analysis
ole.log(fee)

inputs and .outputs will be undefined if no solution was found
!inputs || !outputs) return

txb = new bitcoin.TransactionBuilder()

ts.forEach(input => txb.addInput(input.txId, input.vout))
uts.forEach(output => {
 watch out, outputs may have been added that you need to provide
 an output address/script for
 (!output.address) {
output.address = wallet.getChangeAddress()
wallet.nextChangeAddress()


b.addOutput(output.address, output.value)

License ISC

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.