@metamask/key-tree
An interface over BIP-44 key derivation paths.
Installation
yarn add @metamask/key-tree
or
npm install @metamask/key-tree
Usage
This package is designed to accommodate the creation of keys for any level of a BIP-44 path.
Recall that a BIP-44 HD tree path consists of the following nodes (and depths):
m / 44' / coin_type' / account' / change / address_index
0 / 1 / 2 / 3 / 4 / 5
Where m
is the "master" or seed node, coin_type
indicates the protocol for which deeper keys are intended,
and address_index
usually furnishes key pairs intended for user addresses / accounts.
For details, refer to the BIP-44 specification.
For the authoritative list of protocol coin_type
indices, see SLIP-44.
This package exports two classes intended to facilitate the creation of keys in contexts with different privileges.
They are used as follows.
import { BIP44CoinTypeNode } from '@metamask/key-tree';
const coinType = 60;
const mnemonic = getMnemonic();
const coinTypeNode = new BIP44CoinTypeNode([
`bip39:${mnemonic}`,
`bip32:44'`,
`bip32:${coinType}'`,
]);
stream.write(coinTypeNode.toJSON());
import { getBIP44AddressKeyDeriver } from '@metamask/key-tree';
const coinTypeNode = await getCoinTypeNode();
const addressKeyDeriver = getBIP44AddressKeyDeriver(coinTypeNode);
const addressKey0 = addressKeyDeriver(0);
const addressKey1 = addressKeyDeriver(1);
There are other ways of deriving keys in addition to the above example.
See the docstrings in the BIP44Node and BIP44CoinTypeNode files for details.
Internals
This package also has methods for deriving arbitrary BIP-32 keys, and generating seeds from BIP-39 mnemonics.
These methods do not constitute a safe key derivation API, and their use is strongly discouraged.
Nevertheless, since those methods were the main exports of this package prior to version 3.0.0
, consumers can
still access them by importing @metamask/key-tree/derivation
.
Security
This package is rigorously tested against reference implementations and the BIP-32 specification.
See the reference implementation tests for details.
Further Reading