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
networkon every call.signMessage,signTransactionandderiveAccountAddressall requirenetwork(BitcoinNetwork.MAINNETorBitcoinNetwork.TESTNET). Use the same value everywhere. createWalletAccountreturns the mainnet address.walletMetadata.accountAddressis always the wallet’s primary, mainnet-encoded address (bc1q...for Native SegWit,bc1p...for Taproot), whichever network you intend to use. PasswalletMetadatato the SDK as returned.- Derive the testnet address to receive funds. Use
deriveAccountAddresswithnetwork: BitcoinNetwork.TESTNETto get thetb1q...(ortb1p...) 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.testnetfor testnet andbitcoin.networks.bitcoinfor mainnet. Amounts arebigintin bitcoinjs-lib v7.
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 walletimportPrivateKey(): Import existing private key (WIF format)getBitcoinWallets(): Get all Bitcoin walletsderiveAccountAddress(): Derive Bitcoin address from a public keysignMessage(): Sign messages using BIP-322signTransaction(): Sign PSBTsexportPrivateKey(): Export wallet private key in WIF formatofflineExportPrivateKey(): Export private key from key shares offline