# bitcore-coinselect

> A transaction input selection module for bitcoin.

Latest version **0.0.1** (published 2018-12-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install bitcore-coinselect
pnpm add bitcore-coinselect
yarn add bitcore-coinselect
bun add bitcore-coinselect
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2018-12-10 |
| First published | 2018-12-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 11.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Daniel Cousens |
| Maintainers | ryunishida |
| Keywords | coinselect, coin, unspents, wallet, BIP32, management, utxo, transaction, fee, optimization, optimizing, bitcoin |

## Links

- npm: https://www.npmjs.com/package/bitcore-coinselect
- Repository: https://github.com/NishidaRyu416/coinselect
- Issues: https://github.com/NishidaRyu416/coinselect/issues
- npm.io page: https://npm.io/package/bitcore-coinselect

## Recent versions

- 0.0.1 (latest) — 2018-12-10

## README

# coinselect

[![TRAVIS](https://secure.travis-ci.org/bitcoinjs/coinselect.png)](http://travis-ci.org/bitcoinjs/coinselect)
[![NPM](http://img.shields.io/npm/v/coinselect.svg)](https://www.npmjs.org/package/coinselect)

[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)

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

This was built for bitcoin-lib specifically. However, as the original version of coinselect uses value for bitcoins, while most block explorers use amount for them, the module must have been annoying, so this could be the exact solution for you!

##Instllation

```
npm install bitcore-coinselect --save
```

**WARNING:** Amount units are in `satoshi`s, **not** Bitcoin.


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


**Note:** Each algorithm will add a change output if the `input - output - fee` amount 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 `.amount`) can be used to send-all, while leaving an appropriate amount for the `fee`. 

## Example

``` javascript
let coinSelect = require('coinselect')
let feeRate = 55 // satoshis per byte
let utxos = [
  ...,
  {
    txId: '...',
    vout: 0,
    ...,
    amount: 10000
  }
]
let targets = [
  ...,
  {
    address: '1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm',
    amount: 5000
  }
]

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

// the accumulated fee is always returned for analysis
console.log(fee)

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

let txb = new bitcoin.TransactionBuilder()

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

  txb.addOutput(output.address, output.amount)
})
```


## License [MIT](LICENSE)

---
_Source: https://npm.io/package/bitcore-coinselect · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
