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);
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.