• Stars
    star
    178
  • Rank 214,989 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

coinselect

TRAVIS NPM

js-standard-style

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

WARNING: Value units are in satoshis, not 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 (+fees) is reached, skipping detrimental inputs -
require('coinselect/blackjack') Blackjack - accumulates inputs until the target value (+fees) 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 - fee 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

let coinSelect = require('coinselect')
let feeRate = 55 // satoshis per byte
let utxos = [
  ...,
  {
    txId: '...',
    vout: 0,
    ...,
    value: 10000,
    // For use with PSBT:
    // not needed for coinSelect, but will be passed on to inputs later
    nonWitnessUtxo: Buffer.from('...full raw hex of txId tx...', 'hex'),
    // OR
    // if your utxo is a segwit output, you can use witnessUtxo instead
    witnessUtxo: {
      script: Buffer.from('... scriptPubkey hex...', 'hex'),
      value: 10000 // 0.0001 BTC and is the exact same as the value above
    }
  }
]
let targets = [
  ...,
  {
    address: '1EHNa6Q4Jz2uvNExL497mE43ikXhwF6kZm',
    value: 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 psbt = new bitcoin.Psbt()

inputs.forEach(input =>
  psbt.addInput({
    hash: input.txId,
    index: input.vout,
    nonWitnessUtxo: input.nonWitnessUtxo,
    // OR (not both)
    witnessUtxo: input.witnessUtxo,
  })
)
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()
  }

  psbt.addOutput({
    address: output.address,
    value: output.value,
  })
})

License MIT

More Repositories

1

bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
TypeScript
5,592
star
2

bip39

JavaScript implementation of Bitcoin BIP39: Mnemonic code for generating deterministic keys
JavaScript
983
star
3

bip38

BIP38 is a standard process to encrypt Bitcoin and crypto currency private keys that is less susceptible to brute force attacks thus protecting the user.
JavaScript
206
star
4

bip32

A BIP32 compatible library.
JavaScript
151
star
5

bitcoinjs-message

JavaScript
126
star
6

bolt11

A library for encoding and decoding lightning network payment requests as defined in BOLT #11.
JavaScript
93
star
7

bip44-constants

This package provides BIP44 coin constants as found here: https://github.com/satoshilabs/slips/blob/master/slip-0044.md
JavaScript
87
star
8

bs58check

A straight forward implementation of base58check extending upon bs58.
JavaScript
85
star
9

bech32

Bech32 encoding / decoding
TypeScript
84
star
10

wif

Bitcoin Wallet Import Format JS encoding/decoding module
JavaScript
75
star
11

bip32-utils

A small set of utilities for use with BIP32 HD key nodes
JavaScript
74
star
12

fast-dat-parser

Superfast blockchain parser for stats
C++
73
star
13

tiny-secp256k1

A tiny secp256k1 native/JS wrapper
JavaScript
58
star
14

bip21

A BIP21 compatible URL encoding utility library
JavaScript
55
star
15

indexd

An external bitcoind index management service module
JavaScript
53
star
16

merkle-lib

A performance conscious library for merkle root and tree calculations.
JavaScript
29
star
17

bitcoinjs.org

HTML
20
star
18

bip32-wallet

A BIP32 Wallet backed by bitcoinjs-lib, lite on features but heavily tested.
JavaScript
19
star
19

regtest-client

A client library based off of the integration tests of bitcoinjs-lib. This requires a server component.
JavaScript
18
star
20

regtest-server

A regtest server for bitcoinjs-lib testing
JavaScript
18
star
21

bip174

A BIP174 compatible partial Transaction encoding library.
TypeScript
17
star
22

electrum-mnemonic

Electrum Mnemonics (electrum v2 and greater)
TypeScript
15
star
23

payjoin-client

A Payjoin Client Library in JS with TypeScript types
TypeScript
15
star
24

blkdat-stream

A blk*.dat streaming module, useful for parsing the Bitcoin blockchain
JavaScript
14
star
25

aezeed

A package for encoding, decoding, and generating mnemonics of the aezeed specification. (WIP)
TypeScript
13
star
26

bip66

Strict DER signatures
JavaScript
12
star
27

bitcoin-ops

bitcoin OP codes
JavaScript
11
star
28

ecpair

The ECPair module for bitcoinjs-lib
TypeScript
9
star
29

varuint-bitcoin

encode/decode number as bitcoin variable length integer https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
JavaScript
6
star
30

bip69

Lexicographical Indexing of Transaction Inputs and Outputs
JavaScript
4
star
31

playground

Go nuts! PRs can be useful for discussion, and won't be rejected
JavaScript
3
star
32

bip65

A BIP65 absolute lock-time encoding library.
JavaScript
2
star
33

utxo

JavaScript
2
star
34

bip68

A BIP68 relative lock-time encoding library.
JavaScript
2
star
35

minimaldata

A module to check bitcoin policy: SCRIPT_VERIFY_MINIMALDATA
JavaScript
2
star
36

pushdata-bitcoin

encode/decode number as bitcoin pushdata integer
JavaScript
2
star
37

uint8array-tools

A set of tools for Uint8Array to aide in the move from Buffers.
JavaScript
1
star
38

tif

DEPRECATED: Bitcoin Transaction Interchange Format (TIF) decoding/encoding module
JavaScript
1
star