1.7.0 â€Ē Published 7 months ago

@scure/bip32 v1.7.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

scure-bip32

Audited & minimal implementation of BIP32 hierarchical deterministic (HD) wallets over secp256k1.

  • 🔒 Audited by an independent security firm
  • ðŸ”ŧ Tree-shakeable: unused code is excluded from your builds
  • ðŸ“Ķ ESM and common.js
  • ➰ Only 3 audited dependencies by the same author: noble-curves, noble-hashes, and scure-base
  • ðŸŠķ 18KB gzipped with all dependencies bundled

Check out scure-bip39 if you need mnemonic phrases. See key-producer if you need SLIP-0010/BIP32 ed25519 hdkey implementation.

This library belongs to scure

scure — audited micro-libraries.

Usage

npm install @scure/bip32

deno add jsr:@scure/bip32

deno doc jsr:@scure/bip32 # command-line documentation

This module exports a single class HDKey, which should be used like this:

import { HDKey } from '@scure/bip32';
const hdkey1 = HDKey.fromMasterSeed(seed);
const hdkey2 = HDKey.fromExtendedKey(base58key);
const hdkey3 = HDKey.fromJSON({ xpriv: string });

// props
[hdkey1.depth, hdkey1.index, hdkey1.chainCode];
console.log(hdkey2.privateKey, hdkey2.publicKey);
console.log(hdkey3.derive("m/0/2147483647'/1"));
const sig = hdkey3.sign(hash);
hdkey3.verify(hash, sig);

Note: chainCode property is essentially a private part of a secret "master" key, it should be guarded from unauthorized access.

The full API is:

class HDKey {
  public static HARDENED_OFFSET: number;
  public static fromMasterSeed(seed: Uint8Array, versions: Versions): HDKey;
  public static fromExtendedKey(base58key: string, versions: Versions): HDKey;
  public static fromJSON(json: { xpriv: string }): HDKey;

  readonly versions: Versions;
  readonly depth: number = 0;
  readonly index: number = 0;
  readonly chainCode: Uint8Array | null = null;
  readonly parentFingerprint: number = 0;

  get fingerprint(): number;
  get identifier(): Uint8Array | undefined;
  get pubKeyHash(): Uint8Array | undefined;
  get privateKey(): Uint8Array | null;
  get publicKey(): Uint8Array | null;
  get privateExtendedKey(): string;
  get publicExtendedKey(): string;

  derive(path: string): HDKey;
  deriveChild(index: number): HDKey;
  sign(hash: Uint8Array): Uint8Array;
  verify(hash: Uint8Array, signature: Uint8Array): boolean;
  wipePrivateData(): this;
}

interface Versions {
  private: number;
  public: number;
}

The module implements bip32 standard: check it out for additional documentation.

The implementation is loosely based on cryptocoinjs/hdkey, which has MIT License.

Security

The library has been independently audited:

The library was initially developed for js-ethereum-cryptography. At commit ae00e6d7, it was extracted to a separate package called micro-bip32. After the audit we've decided to use @scure NPM namespace for security.

Supply chain security

  • Commits are signed with PGP keys, to prevent forgery. Make sure to verify commit signatures
  • Releases are transparent and built on GitHub CI. Make sure to verify provenance logs
  • Rare releasing is followed to ensure less re-audit need for end-users
  • Dependencies are minimized and locked-down: any dependency could get hacked and users will be downloading malware with every install.
    • We make sure to use as few dependencies as possible
    • Automatic dep updates are prevented by locking-down version ranges; diffs are checked with npm-diff
  • Dev Dependencies are disabled for end-users; they are only used to develop / build the source code

For this package, there are 3 dependencies; and a few dev dependencies:

  • noble-hashes provides cryptographic hashing functionality
  • noble-curves provides ECDSA
  • scure-base provides base58
  • micro-bmark, micro-should and jsbt are used for benchmarking / testing / build tooling and developed by the same author
  • prettier, fast-check and typescript are used for code quality / test generation / ts compilation. It's hard to audit their source code thoroughly and fully because of their size

Contributing & testing

  • npm install && npm run build && npm test will build the code and run tests.
  • npm run lint / npm run format will run linter / fix linter issues.
  • npm run build:release will build single file

License

MIT License

Copyright (c) 2022 Patricio Palladino, Paul Miller (paulmillr.com)

@btq-js/keyringcasper-js-sdk-test-h2@infinitebrahmanuniverse/nolb-_scuviem-alphaviem-bundle-testviem-testviem-test-bundleviem-test10viem-test11viem-test12viem-test15viem-test16viem-test18viem-test19viem-test2viem-test20viem-test21viem-test22viem-test23viem-test24viem-test3viem-test4viem-test5viem-test6viem-test7viem-test8viem-test9@everything-registry/sub-chunk-814test-opstack-kit-chainsstacks-vanitysui-wallettest-use-viemtest-viemtest-sui.jsv4-client-test-jsvicev-nostr-toolsviemviem_cmcviem_emc@rsodre/create-burneriso-filecoinjs-moi-hdnodejmesindorahocjshsuijs@wgb5445/ts-sdk@wgb5445/aptos-labs-ts-sdk@xdefi/chains-bitcoin@twin.org/crypto@thorswap-lib/toolbox-cosmos@thorswap-lib/toolbox-utxo@tevm/viem-effect@vechain/sdk-core@upheavalprotocol/v4-client-js@usedapptesthello/casper-js-sdk@yaring/mcbf@xhbmygod/xrpl@telestai-project/telestai-key@terra-money/nostr-toolsalephium-web3alphabill-sso-login@ziden-dev/accountairtune-nostr-tools-developmentahau-vc-sdkarcpoolaptos-wallet-apiaptos-wallet-api-v2@hackbg/secretjs-esm@hackbg/4mat@hackbg/formati@gtsc/crypto@okal22/viem@nubit/goldinals-core-js@nostrband/nostr-tools@nostr-chat/nostr-tools@okxweb3/coin-aptos@open4g/onchainstack-sdk@onelabs/sui@optke3/sui.js@osmosis-agent-toolkit/core@hunterfielow/wallet-sdk@gibs/random@hightall/sui.js@heemale/mango.js@heguro/nostr-tools-ws@interchainjs/auth@interc0der/xrpl@poro-wallet/core@pioneer-platform/toolbox-cosmos@pioneer-platform/toolbox-utxofurya-chain-webfuzd-serverfuzd-cf-worker-oldfuzd-chain-protocol@stacks/wallet-sdk@story-protocol/core-sdk@stormcloud266/toolbox-utxo@stacks/cli@swapkit/toolbox-utxo
1.6.2

10 months ago

1.7.0

7 months ago

1.6.1

10 months ago

1.6.0

12 months ago

1.5.0

1 year ago

1.4.0

2 years ago

1.3.3

2 years ago

1.3.2

2 years ago

1.3.1

2 years ago

1.2.0

3 years ago

1.3.0

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.1

4 years ago

1.0.0

4 years ago

1.0.0-rc1

4 years ago