githubEdit

book-openUsage

Installation, quick start, and usage examples for @tetherto/wdk-wallet-btc

Installation

To install the @tetherto/wdk-wallet-btc package, follow these instructions:

npm install @tetherto/wdk-wallet-btc

Quick Start

Importing from @tetherto/wdk-wallet-btc

  1. WalletManagerBtc: Main class for managing Bitcoin wallets and multiple accounts

  2. WalletAccountBtc: Class for individual Bitcoin wallet accounts with full transaction capabilities

  3. Transport Clients: ElectrumTcp, ElectrumTls, ElectrumSsl, ElectrumWs for connecting to Electrum servers

  4. IElectrumClient: Interface for implementing custom Electrum clients

Creating a New Wallet

import WalletManagerBtc, { ElectrumTcp } from '@tetherto/wdk-wallet-btc'

// Use a BIP-39 seed phrase (replace with your own secure phrase)
const seedPhrase = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'

// Create Electrum client
const client = new ElectrumTcp({
  host: 'electrum.blockstream.info',
  port: 50001
})

// Create wallet manager with client
const wallet = new WalletManagerBtc(seedPhrase, {
  client,
  network: 'bitcoin' // 'bitcoin', 'testnet', or 'regtest'
})

// Get a full access account (uses BIP-84 derivation path by default)
const account = await wallet.getAccount(0)

// Get the account's address (Native SegWit by default)
const address = await account.getAddress()
console.log('Account address:', address)

Note: This implementation uses BIP-84 derivation paths and generates Native SegWit (bech32) addresses by default. Set bip: 44 in config for legacy (P2PKH) addresses.

Important Note about Electrum Servers: While the package defaults to electrum.blockstream.info if no host is specified, we strongly recommend configuring your own Electrum server for production use. Public servers like Blockstream's can be significantly slower (10-300x) and may fail when fetching transaction history for popular addresses with many transactions. For better performance, consider using alternative public servers like fulcrum.frznode.com for development, or set up your own Fulcrum server for production environments.

Managing Multiple Accounts

Note: This implementation generates Native SegWit (bech32) addresses by default using BIP-84 derivation paths (m/84'/0'/account'/0/index). Set bip: 44 in config for legacy (P2PKH) addresses using BIP-44 derivation paths.

Checking Balances

Account Balance

Important Notes:

  • getBalance() returns confirmed balance only (no unconfirmed balance option)

  • There's no direct UTXO access method - UTXOs are managed internally

  • Use getTransfers() instead of getTransactionHistory() for transaction data

  • Transfer objects include transaction ID, value, direction, fee, and block height information

Sending Bitcoin Transactions

Important Notes:

  • Bitcoin transactions support single recipient only

  • Amounts and fees are always in satoshis (1 BTC = 100,000,000 satoshis)

  • Minimum amount must be above dust limit (294 satoshis for SegWit, 546 for legacy)

  • Fee rates are calculated automatically based on network conditions if not provided

Message Signing and Verification

Getting Transaction History

Important Notes:

  • Returns transfer objects with detailed transaction information

  • Direction options: 'incoming', 'outgoing', or 'all' (default)

  • Default limit is 10 transfers

  • Change outputs are automatically filtered out

  • Transfers are sorted by block height (newest first)

Fee Management

Important Notes:

  • getFeeRates() fetches from mempool.space API (returns normal and fast rates)

  • sendTransaction()/quoteSendTransaction() without feeRate uses the Electrum server's estimateFee() method

  • These are different fee sources - use getFeeRates() for display, but transactions estimate fees directly from the connected Electrum server

  • Actual fees depend on transaction size (inputs/outputs) and UTXO selection

Memory Management

Important Notes:

  • Always call dispose() when finished with accounts

  • Private keys are securely wiped from memory using sodium_memzero

  • Electrum server connections are automatically closed

  • Disposal is irreversible - create new accounts if needed

Complete Examples

Complete Bitcoin Wallet Setup

Multi-Account Management

Transaction History Analysis

Fee Optimization and Transaction Sending

Comprehensive Error Handling


Need Help?