Learn how to work with imported Bitcoin wallets for common operations like checking balances and signing transactions
Overview
This guide shows you how to work with imported Bitcoin wallets for common operations. Once you’ve imported a private key, you can perform various wallet operations directly.
Use your imported wallet to sign Bitcoin transactions. walletMetadata is the object that importPrivateKey returned. Store it when you import the wallet and load it here, because signing needs its derivation path and address type. bitcoinjs-lib v7 uses bigint for satoshi amounts.
import * as bitcoin from 'bitcoinjs-lib';import { BitcoinNetwork } from '@dynamic-labs-wallet/core';const btcClient = await authenticatedBtcClient();const walletAddress = walletMetadata.accountAddress;// Create PSBT with your UTXOconst psbt = new bitcoin.Psbt({ network: bitcoin.networks.bitcoin });// Add input from your walletpsbt.addInput({ hash: 'previous_txid', index: 0, witnessUtxo: { script: bitcoin.address.toOutputScript(walletAddress, bitcoin.networks.bitcoin), value: 50_000n, // satoshis },});// Add outputpsbt.addOutput({ address: 'bc1q...recipient-address', value: 40_000n, // satoshis (minus fee)});// Sign with imported walletconst signedPsbt = await btcClient.signTransaction({ transaction: psbt.toBase64(), walletMetadata, network: BitcoinNetwork.MAINNET, password: process.env.WALLET_PASSWORD!,});console.log('Signed PSBT (base64):', signedPsbt);
Step 4: Sign Messages with Imported Wallet
Sign messages for authentication or data integrity:
import { BitcoinNetwork } from '@dynamic-labs-wallet/core';const message = 'Hello from my imported Bitcoin wallet!';const signature = await btcClient.signMessage({ message, walletMetadata, network: BitcoinNetwork.MAINNET, password: process.env.WALLET_PASSWORD!,});// Returns BIP-322 signature in base64 formatconsole.log('Message signed (base64):', signature);
Complete Example: Sign and Send from Imported Wallet
In @dynamic-labs-wallet/node-btc 1.1.52, getBitcoinWallets() can fail with a 404 error, because it requests the project’s users list. The wallets it returns also don’t include the full walletMetadata, so keep the walletMetadata you stored when you created or imported each wallet for signing.
Best Practices
Balance Checking: Verify sufficient balance before sending transactions
UTXO Management: Select UTXOs efficiently to minimize fees
Fee Estimation: Use current fee rates from mempool APIs
Error Handling: Implement proper error handling for all wallet operations
Security: Never expose sensitive information in server-side code
Network Selection: Use testnet for development and testing
Fee Estimation
Estimate appropriate fees before sending:
// Fetch recommended fee ratesconst feeResponse = await fetch('https://blockstream.info/api/fee-estimates');const feeEstimates = await feeResponse.json();// feeEstimates contains sat/vB for different confirmation targets// e.g., { "1": 25, "3": 15, "6": 10 } means:// - 25 sat/vB for ~1 block confirmation// - 15 sat/vB for ~3 block confirmation// - 10 sat/vB for ~6 block confirmation// For Native SegWit P2WPKH: ~110 vBytes for 1-in-1-out txconst estimatedFee = feeEstimates["3"] * 110; // sat for ~3 block confirmation