Skip to content
Javascript library for working with bitcoin, esp. validation & multisig.
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs
src Added ESLint, updated babel. Nov 8, 2019
.eslintrc.json Added ESLint, updated babel. Nov 8, 2019
.gitignore moved from private repo Nov 5, 2019
.npmignore added jsdocs Nov 7, 2019
.npmrc.template added steps to publish npm registry Nov 6, 2019
.travis.yml
LICENSE updated copyright date Nov 11, 2019
README.md updated links in README.md Nov 7, 2019
jest.config.js
package-lock.json 0.0.5 Nov 8, 2019
package.json 0.0.5 Nov 8, 2019

README.md

Unchained Capital Bitcoin Utilities

Build Status

This library builds on the excellent bitcoinjs-lib, adding valuable but missing functionality for validation, HD wallets, block explorers, and especially multisig.

Full API documentation can be found at unchained-bitcoin.

This library was built and is maintained by Unchained Capital.

Installation

unchained-bitcoin is distributed as an NPM package. Add it to your application's dependencies:

$ npm install --save unchained-bitcoin

Usage

The library provides a functional API which builds upon data structures used by bitcoinjs-lib.

In particular, many functions accept a multisig argument which is the kind of object returned by functions such as bitcoin.payments.p2ms, bitcoin.payments.p2sh, &c. from bitcoinjs-lib.

The examples below provide an initial idea of how to use this library, but see the API documentation for full details.

Interacting with a multisig address.

Generating multisigs

Multisignature objects can be generated with two functions:

  • generateMultisigFromPublicKeys -- useful when directly passing public keys
  • generateMultisigFromHex -- useful when parsing an existing redeem script

Each of these functions accepts additional arguments which determines the multisig address type (e.g. P2SH or P2WSH).

import {
	generateMultisigFromPublicKeys,
	P2SH,                      // or: P2SH_P2WSH, P2WSH
	MAINNET,                   // or: TESTNET
} from "unchained-bitcoin";

// Public keys are represented as compressed hex.
const publicKeys = [
	"03d703e0d7622f087f27e18b61b1c131d4a7b868a4960ab66d89c76f9daf1a1569",
	"031c27e952c332fc0630e29d6ec2a10ba18c33cf096ccaeb7620983e0daf625946",
	"03f6eed874ad71db090325dc92ccec9b722187dfd4aac19c9f54b9047bc4fac2b0",
];

// A mainnet P2SH 2-of-3 multisig.
const multisig = generateMultisigFromPublicKeys(MAINNET, P2SH, 2, ...publicKeys);

Querying multisigs

The multisig object above can be passed around and queried with other functions in the API.

multisigAddress(multisig); // Returns public address
multisigAddressType(multisig); // P2SH
multisigRequiredSigners(multisig); // 2
multisigTotalSigners(multisig); // 3
multisigRedeemScript(multisig); // Returns redeem script in hex
multisigWitnessScript(multisig); // Returns witness script in hex (null for P2SH)
multisigPublicKeys(multisig); // Returns publicKeys

Validation

This library contains several useful functions for validation not provided by bitcoinjs-lib or other libraries. The validation functions are designed to return an empty string '' on valid input and provide a helpful error message otherwise.

  • validateAddress -- understands Bech32 addresses and is aware of differences in addresses across networks

  • valdiateBIP32Path -- understands absolute and relative BIP32 paths, validates maximum BIP32 index values, and can optionally check for fully hardened or unhardened paths

  • validateExtendedPublicKey -- understands network-dependent differences in encoding extended public keys

  • validatePublicKey -- allows any hexadecimal value

  • validateFeeRate -- implements a reasonable maximum fee rate in Satoshis/byte

  • validateFee -- implements a reasonable maximum fee in BTC

  • validateOutputAmount -- checks for dust and that amount is less than total input amount

  • validateMultisigSignature -- checks signatures for correctness and works across all multisig address types

Developers

Developers who want to work on this library should clone the source code and install dependencies:

$ git clone https://github.com/unchained-capital/unchained-bitcoin`
...
$ cd unchained-bitcoin
$ npm install

Testing

Unit tests are implemented in Jest and can be run via

$ npm test

Contributing

Unchained Capital welcomes bug reports, new features, and better documentation for this library.

If you are fixing a bug or adding a feature, please first check the GitHub issues page to see if there is any existing discussion about it.

To contribute, create a pull request (PR) on GitHub against the Unchained Capital fork of unchained-bitcoin.

Before you submit your PR, make sure to update and run the test suite!

You can’t perform that action at this time.