Bitcoin wallet utilities
The package contains bitcoin utilities to work with wallet software as well as a set of device specific modules that allow to communicate with hardware keys. Supports the following devices:
npm i bitcoin-sdk
Generic bitcoin utils are exported as the root props of the main export, and device specific are
namespaced by brand name. Device modules implement either Connected
or Airgapped
interface.
API docs available here.
getXpubDescriptorPattern: ({ derivationPath, xpub, masterFingerprint }) => string
- computes
descriptor from key params.getNetworkFromDp: (derivationPath) => NetworkEnum
- extracts network from derivation path.parseMultisigWalletDescriptor: (descriptor) => { policy, keys }
- computes wallet policy and
extracts keys.getPsbtSignatureCount(psbt) => number
- gets signature count from psbt hex.checkForNewSignature: (psbtOrig, psbtNew) => void
- validates if new PSBT contains a new
signature compared to the original.getAccountNumberFromDp: (derivationPath) => string
- computes account number from derivation
path.Ledger: Connected
a class with a set of Ledger utilities that implements "Connected" interface.Trezor: Connected
a class implements "Connected" interface for Trezor.Coldcard: Airgapped
implements "Airgapped" interface for Coldcard.Foundation: Airgapped
implements "Airgapped" interface for Foundation.Device interface for hardware keys that requires physical connection to a computer (via USB or Bluetooth).
init: (options) => void
initializes device specific module.getMasterFingerprint: (network) => Promise<string>
- extracts master fingerprint from device.getXpubInfo: (dp, network) => Promise<{ xpub; master_fingerprint }>
- extracts key params for
given derivation path.getXpubByDp: (dp, network) => Promise<string>
- extracts XPUB for given derivation path and
network.signTransaction: (psbtHex, walletType, walletKey, options) => Promise<string>
signs psbt
(handles single key and multisig wallets).policyRegistrationRequired: boolean
- indicates if wallet policy needs to be registered on the
device.registerDevicePolicy: (walletKeys, options) => Promise<{ policy, policy_hmac }>
- registers
wallet policy on device.Device Interface to work with hardware keys in "airgap" mode (via microSD card or QR codes).
getXpubFromFile: (fileContent: any, isMultisigWallet: boolean) => DeviceKeyParams
getSignatureFromFile: (fileContent: any) => { addr: string; signature: string }
policyRegistrationRequired: boolean
- indicates if wallet policy needs to be registered on the
device.buildWalletConfig: (walletName, descriptor) => string
- creates a multi-line string for
downlowding a text file with wallet config for registering multisig wallet on the device.See */__tests__
folders for more examples.
Usage:
import { getNetworkFromDp, Coldcard, LedgerDI, TrezorDI } from 'bitcoin-sdk';
const network = getNetworkFromDp(`m/48'/0'/0'/2'`);
// => 'mainnet'
// Ledger:
const ledger = await LedgerDI.create();
const masterFingerprint = await ledger.getMasterFingerprint();
// Trezor:
const trezor = await TrezorDI.create();
const masterFingerprint = await trezor.getMasterFingerprint('testnet');
// Coldcard:
const descriptor = 'wsh(sortedmulti(2, ...)';
const walletConfig = Coldcard.buildWalletConfig('My wallet', descriptor);
// Returns multiline string:
/*
# Multisig wallet configuration
#
Name: My wallet
Policy: 2 of 3
Format: P2WSH
...
*/