githubEdit

codeAPI Reference

Complete API documentation for @tetherto/wdk-wallet-btc

Table of Contents

Class
Description
Methods

Main class for managing Bitcoin wallets. Extends WalletManager from @tetherto/wdk-wallet.

Individual Bitcoin wallet account implementation. Implements IWalletAccount.

Read-only Bitcoin wallet account. Extends WalletAccountReadOnly from @tetherto/wdk-wallet.

Read-only Bitcoin wallet account. Extends WalletAccountReadOnly from @tetherto/wdk-wallet.

Standard TCP Electrum client. Implements IElectrumClient.

TLS Electrum client. Implements IElectrumClient.

SSL Electrum client. Implements IElectrumClient.

WebSocket Electrum client for browser environments. Implements IElectrumClient.

WalletManagerBtc

The main class for managing Bitcoin wallets. Extends WalletManager from @tetherto/wdk-wallet.

Constructor

new WalletManagerBtc(seed, config)

Parameters:

  • seed (string | Uint8Array): BIP-39 mnemonic seed phrase or seed bytes

  • config (BtcWalletConfig, optional): Configuration object

    • client (IElectrumClient, optional): Electrum client instance. If provided, host/port/protocol are ignored.

    • host (string, optional): Electrum server hostname (default: "electrum.blockstream.info"). Ignored if client is provided.

    • port (number, optional): Electrum server port (default: 50001). Ignored if client is provided.

    • protocol (string, optional): Transport protocol - "tcp", "tls", or "ssl" (default: "tcp"). Ignored if client is provided.

    • network (string, optional): "bitcoin", "testnet", or "regtest" (default: "bitcoin")

    • bip (number, optional): BIP address type - 44 (legacy) or 84 (native SegWit) (default: 84)

Methods

Method
Description
Returns

getAccount(index)

Returns a wallet account at the specified index

Promise<WalletAccountBtc>

getAccountByPath(path)

Returns a wallet account at the specified derivation path

Promise<WalletAccountBtc>

getFeeRates()

Returns current fee rates for transactions

Promise<{normal: bigint, fast: bigint}>

dispose()

Disposes all wallet accounts, clearing private keys from memory

void

getAccount(index)

Returns a wallet account at the specified index using BIP-84 (default) or BIP-44 derivation.

Parameters:

  • index (number, optional): The index of the account to get (default: 0)

Returns: Promise<WalletAccountBtc> - The wallet account

Example:

getAccountByPath(path)

Returns a wallet account at the specified derivation path.

Parameters:

  • path (string): The derivation path (e.g., "0'/0/0")

Returns: Promise<WalletAccountBtc> - The wallet account

Example:

getFeeRates()

Returns current fee rates from mempool.space API.

Returns: Promise<{normal: bigint, fast: bigint}> - Object containing fee rates in sat/vB

  • normal: Standard fee rate for confirmation within ~1 hour

  • fast: Higher fee rate for faster confirmation

Example:

dispose()

Disposes all wallet accounts and clears sensitive data from memory.

Returns: void

Example:

WalletAccountBtc

Represents an individual Bitcoin wallet account. Extends WalletAccountReadOnlyBtc and implements IWalletAccount from @tetherto/wdk-wallet.

Constructor

Parameters:

  • seed (string | Uint8Array): BIP-39 mnemonic seed phrase or seed bytes

  • path (string): Derivation path suffix (e.g., "0'/0/0")

  • config (BtcWalletConfig, optional): Configuration object

    • client (IElectrumClient, optional): Electrum client instance. If provided, host/port/protocol are ignored.

    • host (string, optional): Electrum server hostname (default: "electrum.blockstream.info"). Ignored if client is provided.

    • port (number, optional): Electrum server port (default: 50001). Ignored if client is provided.

    • protocol (string, optional): Transport protocol - "tcp", "tls", or "ssl" (default: "tcp"). Ignored if client is provided.

    • network (string, optional): "bitcoin", "testnet", or "regtest" (default: "bitcoin")

    • bip (number, optional): BIP address type - 44 (legacy) or 84 (native SegWit) (default: 84)

Methods

Method
Description
Returns

getAddress()

Returns the account's Bitcoin address

Promise<string>

getBalance()

Returns the confirmed account balance in satoshis

Promise<bigint>

sendTransaction(options)

Sends a Bitcoin transaction

Promise<{hash: string, fee: bigint}>

quoteSendTransaction(options)

Estimates the fee for a transaction

Promise<{fee: bigint}>

getTransfers(options?)

Returns the account's transfer history

Promise<BtcTransfer[]>

getTransactionReceipt(hash)

Returns a transaction's receipt

Promise<BtcTransactionReceipt | null>

getMaxSpendable()

Returns the maximum spendable amount

Promise<BtcMaxSpendableResult>

sign(message)

Signs a message with the account's private key

Promise<string>

verify(message, signature)

Verifies a message signature

Promise<boolean>

toReadOnlyAccount()

Creates a read-only version of this account

Promise<WalletAccountReadOnlyBtc>

dispose()

Disposes the wallet account, clearing private keys from memory

void

getAddress()

Returns the account's Bitcoin address (Native SegWit bech32 by default, or legacy if using BIP-44).

Returns: Promise<string> - The Bitcoin address

Example:

getBalance()

Returns the account's confirmed balance in satoshis.

Returns: Promise<bigint> - Balance in satoshis

Example:

sendTransaction(options)

Sends a Bitcoin transaction to a single recipient.

Parameters:

  • options (BtcTransaction): Transaction options

    • to (string): Recipient's Bitcoin address

    • value (number | bigint): Amount in satoshis

    • feeRate (number | bigint, optional): Fee rate in sat/vB. If provided, overrides the fee rate estimated from the blockchain.

    • confirmationTarget (number, optional): Target blocks for confirmation (default: 1)

Returns: Promise<{hash: string, fee: bigint}>

  • hash: Transaction hash

  • fee: Transaction fee in satoshis

Example:

quoteSendTransaction(options)

Estimates the fee for a transaction without broadcasting it.

Parameters:

  • options (BtcTransaction): Same as sendTransaction options

    • to (string): Recipient's Bitcoin address

    • value (number | bigint): Amount in satoshis

    • feeRate (number | bigint, optional): Fee rate in sat/vB. If provided, overrides the fee rate estimated from the blockchain.

    • confirmationTarget (number, optional): Target blocks for confirmation (default: 1)

Returns: Promise<{fee: bigint}>

  • fee: Estimated transaction fee in satoshis

Example:

getTransfers(options?)

Returns the account's transfer history with detailed transaction information.

Parameters:

  • options (object, optional): Filter options

    • direction (string, optional): 'incoming', 'outgoing', or 'all' (default: 'all')

    • limit (number, optional): Maximum number of transfers (default: 10)

    • skip (number, optional): Number of transfers to skip (default: 0)

Returns: Promise<BtcTransfer[]> - Array of transfer objects

  • txid: Transaction ID

  • address: Account's own address

  • vout: Output index in the transaction

  • height: Block height (0 if unconfirmed)

  • value: Transfer value in satoshis (bigint)

  • direction: 'incoming' or 'outgoing'

  • fee: Transaction fee in satoshis (bigint, for outgoing transfers)

  • recipient: Receiving address (for outgoing transfers)

Example:

getTransactionReceipt(hash)

Returns a transaction's receipt if it has been included in a block.

Parameters:

  • hash (string): The transaction hash (64 hex characters)

Returns: Promise<BtcTransactionReceipt | null> - The receipt, or null if the transaction has not been included in a block yet.

Example:

getMaxSpendable()

Returns the maximum spendable amount that can be sent in a single transaction. The maximum spendable amount can differ from the wallet's total balance for several reasons:

  • Transaction fees: Fees are subtracted from the total balance

  • Uneconomic UTXOs: Small UTXOs where the fee to spend them exceeds their value are excluded

  • UTXO limit: A transaction can include at most 200 inputs. Wallets with more UTXOs cannot spend their full balance in a single transaction.

  • Dust limit: Outputs below the dust threshold (294 sats for SegWit, 546 sats for legacy) cannot be created

Returns: Promise<BtcMaxSpendableResult> - Maximum spendable result

  • amount: Maximum spendable amount in satoshis (bigint)

  • fee: Estimated network fee in satoshis (bigint)

  • changeValue: Estimated change value in satoshis (bigint)

Example:

sign(message)

Signs a message using the account's private key.

Parameters:

  • message (string): Message to sign

Returns: Promise<string> - Signature as base64 string

Example:

verify(message, signature)

Verifies a message signature using the account's public key.

Parameters:

  • message (string): Original message

  • signature (string): Signature as base64 string

Returns: Promise<boolean> - True if signature is valid

Example:

getTokenBalance(tokenAddress)

Not supported on the Bitcoin blockchain. Always throws an error.

Parameters:

  • tokenAddress (string): Token contract address

Throws: Error - "The 'getTokenBalance' method is not supported on the bitcoin blockchain."

Example:

transfer(options)

Not supported on the Bitcoin blockchain. Always throws an error.

Parameters:

  • options (object): Transfer options

Throws: Error - "The 'transfer' method is not supported on the bitcoin blockchain."

quoteTransfer(options)

Not supported on the Bitcoin blockchain. Always throws an error.

Parameters:

  • options (object): Transfer options

Throws: Error - "The 'quoteTransfer' method is not supported on the bitcoin blockchain."

toReadOnlyAccount()

Creates a read-only version of this account that can query balances and transactions but cannot sign or send transactions.

Returns: Promise<WalletAccountReadOnlyBtc> - Read-only account instance

Example:

dispose()

Disposes the wallet account, securely erasing the private key from memory and closing the Electrum connection.

Returns: void

Example:

Properties

Property
Type
Description

index

number

The derivation path's index of this account

path

string

The full derivation path of this account

keyPair

KeyPair

The account's public and private key pair

WalletAccountReadOnlyBtc

Represents a read-only Bitcoin wallet account. Extends WalletAccountReadOnly from @tetherto/wdk-wallet.

Constructor

Parameters:

  • address (string): The account's Bitcoin address

  • config (object, optional): Configuration object (same as BtcWalletConfig but without bip)

    • client (IElectrumClient, optional): Electrum client instance. If provided, host/port/protocol are ignored.

    • host (string, optional): Electrum server hostname (default: "electrum.blockstream.info"). Ignored if client is provided.

    • port (number, optional): Electrum server port (default: 50001). Ignored if client is provided.

    • protocol (string, optional): Transport protocol - "tcp", "tls", or "ssl" (default: "tcp"). Ignored if client is provided.

    • network (string, optional): "bitcoin", "testnet", or "regtest" (default: "bitcoin")

Methods

Method
Description
Returns

getAddress()

Returns the account's Bitcoin address

Promise<string>

getBalance()

Returns the confirmed account balance in satoshis

Promise<bigint>

quoteSendTransaction(options)

Estimates the fee for a transaction

Promise<{fee: bigint}>

getTransactionReceipt(hash)

Returns a transaction's receipt

Promise<BtcTransactionReceipt | null>

getMaxSpendable()

Returns the maximum spendable amount

Promise<BtcMaxSpendableResult>

verify(message, signature)

Verifies a message signature

Promise<boolean>

verify(message, signature)

Verifies a message signature using the account's public key.

Parameters:

  • message (string): Original message

  • signature (string): Signature as base64 string

Returns: Promise<boolean> - True if signature is valid

Example:

ElectrumTcp

Electrum client using TCP transport. Standard for command-line and server-side environments. Implements IElectrumClient.

Constructor

Parameters:

  • config (Omit<MempoolElectrumConfig, 'protocol'>): Configuration options

    • host (string): Electrum server hostname

    • port (number): Electrum server port

ElectrumTls

Electrum client using TLS transport. Implements IElectrumClient.

Constructor

Parameters:

  • config (Omit<MempoolElectrumConfig, 'protocol'>): Configuration options

    • host (string): Electrum server hostname

    • port (number): Electrum server port

ElectrumSsl

Electrum client using SSL transport. Implements IElectrumClient.

Constructor

Parameters:

  • config (Omit<MempoolElectrumConfig, 'protocol'>): Configuration options

    • host (string): Electrum server hostname

    • port (number): Electrum server port

ElectrumWs

Electrum client using WebSocket transport. Compatible with browser environments where TCP sockets are not available. Implements IElectrumClient.

Constructor

Parameters:

  • config (ElectrumWsConfig): Configuration options

    • url (string): The WebSocket URL (e.g., 'wss://electrum.example.com:50004')

Methods

Method
Description
Returns

connect()

Establishes connection to Electrum server

Promise<void>

close()

Closes the connection

Promise<void>

reconnect()

Recreates the underlying socket and reinitializes the session

Promise<void>

getBalance(scripthash)

Returns balance for a script hash

Promise<ElectrumBalance>

listUnspent(scripthash)

Returns UTXOs for a script hash

Promise<ElectrumUtxo[]>

getHistory(scripthash)

Returns transaction history

Promise<ElectrumHistoryItem[]>

getTransaction(txHash)

Returns raw transaction hex

Promise<string>

broadcast(rawTx)

Broadcasts raw transaction

Promise<string>

estimateFee(blocks)

Returns estimated fee rate

Promise<number>

Types

BtcTransaction

TransactionResult

FeeRates

BtcTransfer

BtcMaxSpendableResult

KeyPair

BtcWalletConfig

IElectrumClient

Interface for implementing custom Electrum clients.

ElectrumBalance

ElectrumUtxo

ElectrumHistoryItem

MempoolElectrumConfig


Need Help?