Module: paths

This module contains various utility functions for converting and validating BIP32 derivation paths.

Source:

Methods

(static) bip32PathToSequence(pathString) → {Array.<number>}

Convert BIP32 derivation path to an array of integer values representing the corresponding derivation indices.

Hardened path segments will have the hardening offset added to the index.

Parameters:
Name Type Description
pathString string

BIP32 derivation path string

Source:
Returns:

the derivation indices

Type
Array.<number>
Example
import {bip32PathToSequence} from "unchained-bitcoin";
console.log(bip32PathToSequence("m/45'/1/99")); // [2147483693, 1, 99]

(static) bip32SequenceToPath(sequence) → {string}

Convert a sequence of derivation indices into the corresponding BIP32 derivation path.

Indices above the hardening offset will be represented wiith hardened * path segments (using a trailing single-quote).

Parameters:
Name Type Description
sequence Array.<number>

the derivation indices

Source:
Returns:

BIP32 derivation path

Type
string
Example
import {bip32SequenceToPath} from "unchained-bitcoin";
console.log(bip32SequenceToPath([2147483693, 1, 99])); // m/45'/1/99

(static) getParentBIP32Path(bip32Path) → {string}

Get the path of the parent of the given path

Parameters:
Name Type Description
bip32Path string

e.g. "m/45'/0'/0'/0"

Source:
Returns:

parent path

Type
string
Example
import {getParentBIP32Path} from "unchained-bitcoin";
console.log(getParentBIP32Path("m/45'/0'/0'/0"); // m/45'/0'/0'

(static) getRelativeBIP32Path(parentBIP32Path, childBIP32Path) → {string}

Get the path of under the parentBIP32Path of the given path

Parameters:
Name Type Description
parentBIP32Path string

e.g. "m/45'/0'/0'"

childBIP32Path string

e.g. "m/45'/0'/0'/0/1/2"

Source:
Returns:

relative path below path

Type
string
Example
import {getRelativeBIP32Path} from "unchained-bitcoin";
console.log(getRelativeBIP32Path("m/45'/0'/0'", "m/45'/0'/0'/0/1/2"); // 0/1/2

(static) hardenedBIP32Index(index) → {number}

Return the hardened version of the given BIP32 index.

Hardening is equivalent to adding 2^31.

Parameters:
Name Type Description
index string | number

BIP32 index

Source:
Returns:

the hardened index

Type
number
Example
import {hardenedBIP32Index} from "unchained-bitcoin";
console.log(hardenedBIP32Index(44); // 2147483692

(static) multisigBIP32Path(addressType, network, relativePath) → {string}

Returns a BIP32 path at the given relativePath under the default BIP32 root path for the given addressType and network.

Parameters:
Name Type Description
addressType module:multisig.MULTISIG_ADDRESS_TYPES

type from which to calculate BIP32 root path

network module:networks.NETWORKS

bitcoin network from which to calculate BIP32 root path

relativePath number | string

the relative BIP32 path (no leading /)

Source:
Returns:

child BIP32 path

Type
string
Example
import {multisigBIP32Path} from "unchained-bitcoin";
console.log(multisigBIP32Path(P2SH, MAINNET, 0); // m/45'/0'/0'/0
console.log(multisigBIP32Path(P2SH_P2WSH, TESTNET, "3'/4"); // m/48'/1'/0'/1'/3'/4"

(static) multisigBIP32Root(addressType, network) → {string}

Return the default BIP32 root derivation path for the given addressType and network.

  • Mainnet:
    • P2SH: m/45'/0'/0'
    • P2SH-P2WSH: m/48'/0'/0'/1'
    • P2WSH: m/48'/0'/0'/2'
  • Testnet:
    • P2SH: m/45'/1'/0'
    • P2SH-P2WSH: m/48'/1'/0'/1'
    • P2WSH: m/48'/1'/0'/2'
Parameters:
Name Type Description
addressType module:multisig.MULTISIG_ADDRESS_TYPES

address type

network module:networks.NETWORKS

bitcoin network

Source:
Returns:

derivation path

Type
string
Example
import {multisigBIP32Root} from "unchained-bitcoin";
console.log(multisigBIP32Root(P2SH, MAINNET)); // m/45'/0'/0'
console.log(multisigBIP32Root(P2SH_P2WSH, TESTNET); // m/48'/1'/0'/1'

(static) validateBIP32Index(indexString, optionsopt) → {string}

Validate a given BIP32 index string.

  • Path segments are validated numerically as well as statically (the value of 2^33 is an invalid path segment).

  • By default, 0-4294967295 and 0'-2147483647' are valid.

  • The mode option can be pass to validate index is hardened unhardened paths.

  • hardened paths include 0'-2147483647' and 2147483648-4294967295

  • unharded paths include 0-2147483647

Parameters:
Name Type Attributes Description
indexString string

BIP32 index string

options Object <optional>

additional options

Properties
Name Type Attributes Description
mode string <optional>

"hardened" or "unhardened"

Source:
Returns:

empty if valid or corresponding validation message if not

Type
string
Example
import {validateBIP32Path} from "unchained-bitcoin";
console.log(validateBIP32Path("")); // "BIP32 index cannot be blank."
console.log(validateBIP32Path("foo")); // "BIP32 index is invalid."
console.log(validateBIP32Path("//45")); // "BIP32 index is invalid."
console.log(validateBIP32Path("/45/")); // "BIP32 index is invalid."
console.log(validateBIP32Index("4294967296")); // "BIP32 index is too high."
console.log(validateBIP32Index("2147483648'")); // "BIP32 index is too high."
console.log(validateBIP32Index("45", { mode: "hardened" })); // "BIP32 index must be hardened."
console.log(validateBIP32Index("45'", { mode: "unhardened" })); // "BIP32 index cannot be hardened."
console.log(validateBIP32Index("2147483648", {mode: "unhardened"})); // "BIP32 index cannot be hardened."
console.log(validateBIP32Index("45")); // ""
console.log(validateBIP32Index("45'")); // ""
console.log(validateBIP32Index("0")); // ""
console.log(validateBIP32Index("0'")); // ""
console.log(validateBIP32Index("4294967295")); // ""
console.log(validateBIP32Index("2147483647")); // ""
console.log(validateBIP32Index("2147483647'")); // ""

(static) validateBIP32Path(pathString, optionsopt) → {string}

Validate a given BIP32 derivation path string.

  • Path segments are validated numerically as well as statically (the value of 2^33 is an invalid path segment).

  • The mode option can be pass to validate fully hardened or unhardened paths.

Parameters:
Name Type Attributes Description
pathString string

BIP32 derivation path string

options Object <optional>

additional options

Properties
Name Type Attributes Description
mode string <optional>

"hardened" or "unhardened"

Source:
Returns:

empty if valid or corresponding validation message if not

Type
string
Example
import {validateBIP32Path} from "unchained-bitcoin";
console.log(validateBIP32Path("")); // "BIP32 path cannot be blank."
console.log(validateBIP32Path("foo")); // "BIP32 path is invalid."
console.log(validateBIP32Path("//45")); // "BIP32 path is invalid."
console.log(validateBIP32Path("/45/")); // "BIP32 path is invalid."
console.log(validateBIP32Path("/45''")); // "BIP32 path is invalid."
console.log(validateBIP32Path('/45"')); // "BIP32 path is invalid."
console.log(validateBIP32Path("/-45")); // "BIP32 path is invalid."
console.log(validateBIP32Path("/8589934592")); // "BIP32 index is too high."
console.log(validateBIP32Path("/45")); // ""
console.log(validateBIP32Path("/45/0'")); // ""
console.log(validateBIP32Path("/45/0'", {mode: "hardened")); // "BIP32 path must be fully-hardened."
console.log(validateBIP32Path("/45'/0'", {mode: "hardened")); // ""
console.log(validateBIP32Path("/0'/0", {mode: "unhardened")); // "BIP32 path cannot include hardened segments."
console.log(validateBIP32Path("/0/0", {mode: "unhardened")); // ""