Skip to main content

Overview

This comprehensive example demonstrates all available functions in the Dynamic BTC SDK, showing the actual API usage patterns and how to handle common operations.
The SDK supports both Bitcoin Mainnet and Testnet. Use BitcoinNetwork.MAINNET for production and BitcoinNetwork.TESTNET for development and testing. The example below runs on testnet; change NETWORK to switch. See Working with testnet for how addresses behave on each network.

Complete Example Code

Working with testnet

Wallet creation is the same on every network. What changes is how you pass the network and which address you use outside the SDK.
  • Pass network on every call. signMessage, signTransaction and deriveAccountAddress all require network (BitcoinNetwork.MAINNET or BitcoinNetwork.TESTNET). Use the same value everywhere.
  • createWalletAccount returns the mainnet address. walletMetadata.accountAddress is always the wallet’s primary, mainnet-encoded address (bc1q... for Native SegWit, bc1p... for Taproot), whichever network you intend to use. Pass walletMetadata to the SDK as returned.
  • Derive the testnet address to receive funds. Use deriveAccountAddress with network: BitcoinNetwork.TESTNET to get the tb1q... (or tb1p...) address for the same key. Fund that address from a testnet faucet, and use it when looking up UTXOs.
  • Build PSBTs with the matching bitcoinjs-lib network. Use bitcoin.networks.testnet for testnet and bitcoin.networks.bitcoin for mainnet. Amounts are bigint in bitcoinjs-lib v7.
If network is omitted, or does not match the address you are signing for, you can see errors such as Address verification failed: expected tb1q..., got bc1q... from signMessage, or No inputs found that belong to the account address from signTransaction.

Key API Patterns

1. Wallet Creation

A password is required when backUpToDynamic is true. With backUpToDynamic: false you hold the key shares yourself and pass them to later calls as externalServerKeyShares.

2. Message Signing (BIP-322)

3. Transaction Signing (PSBT)

4. Derive Address

5. Export Private Key

exportPrivateKey returns the key in mainnet WIF format (starting with K or L), including for a wallet you use on testnet.

Available Functions

The BTC SDK provides the following main functions:
  • createWalletAccount(): Create new Bitcoin wallet
  • importPrivateKey(): Import existing private key (WIF format)
  • getBitcoinWallets(): Get all Bitcoin wallets
  • deriveAccountAddress(): Derive Bitcoin address from a public key
  • signMessage(): Sign messages using BIP-322
  • signTransaction(): Sign PSBTs
  • exportPrivateKey(): Export wallet private key in WIF format
  • offlineExportPrivateKey(): Export private key from key shares offline

Error Handling

Always implement proper error handling:

Environment Setup

Make sure you have these environment variables:

Dependencies

Install required packages:

Next Steps

Last modified on October 27, 2026