@theyabtc/bitcoin-sdk
v0.0.18
Published
Bitcoin utilities
Downloads
83
Readme
bitcoin-sdk
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:
- Ledger
- Trezor
- Foundation Passport
- Coldcard
Install
npm i bitcoin-sdk
API
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.
Top level utilities:
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.
Top level device interface modules:
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 - "Connected"
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 - "Airgapped"
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.
Example
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 = new LedgerDI();
const masterFingerprint = await ledger.getMasterFingerprint();
// Trezor:
const trezor = new TrezorDI({ manifest: { email: '[email protected]' } });
const masterFingerprint = await trezor.getMasterFingerprint('testnet');
// Coldcard:
const descriptor = 'wsh(sortedmulti(2, ...)';
const coldcard = new ColdcardDI();
const walletConfig = coldcard.buildWalletConfig('My wallet', descriptor);
// Returns multiline string:
/*
# Multisig wallet configuration
#
Name: My wallet
Policy: 2 of 3
Format: P2WSH
...
*/