Usage

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

Note: ElectrumClient is an internal class and not intended for direct use.

Creating a New Wallet

import WalletManagerBtc, { WalletAccountBtc } 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 wallet manager with Electrum server config
const wallet = new WalletManagerBtc(seedPhrase, {
  host: 'electrum.blockstream.info', // Electrum server hostname
  port: 50001, // Electrum server port (50001 for TCP, 50002 for SSL)
  network: 'bitcoin' // 'bitcoin', 'testnet', or 'regtest'
})

// Get a full access account (uses BIP-84 derivation path)
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. The address type is determined by the BIP-84 standard, not by configuration.

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 only. Legacy and P2SH-wrapped SegWit address types are not supported. All accounts use BIP-84 derivation paths (m/84'/0'/account'/0/index).

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 (546 satoshis)

  • Fee rates are calculated automatically based on network conditions

Message Signing and Verification

Important Notes:

  • Messages are hashed with SHA256 before signing

  • Signatures are returned as hex strings

  • Both sign() and verify() methods are synchronous but marked as async

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:

  • Fee rates are fetched from mempool.space API

  • Returns only normal and fast fee rates

  • Fee selection is automatic based on current network conditions

  • 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?