Bitcoin Basics: Getting Started with Bitcoin Integration
This guide covers how to use the VerifiedX Web SDK for Bitcoin operations. The SDK provides a comprehensive Bitcoin client that supports wallet management, transactions, and mempool interactions through a mempool.space-style API: mempool.space on mainnet, and the VerifiedX signet’s own explorer API on testnet.
Prerequisites
Section titled “Prerequisites”- Node.js 20+ or a modern browser
- Basic understanding of Bitcoin concepts
vfx-web-sdkinstalled in your project
Understanding Bitcoin Networks
Section titled “Understanding Bitcoin Networks”The SDK supports two Bitcoin networks:
- Mainnet: The production Bitcoin network with real BTC
- Testnet: For VerifiedX development this is the VerifiedX signet, the private Bitcoin test network the VFX testnet watches. It uses testnet address formats (
tb1…,m…/n…,2…) and signet BTC from the testnet faucet
Step 1: Initialize the Bitcoin Client
Section titled “Step 1: Initialize the Bitcoin Client”The BtcClient provides all Bitcoin functionality in a single interface:
import { btc } from 'vfx-web-sdk';
// Initialize for the VerifiedX signet (recommended for development)const client = new btc.BtcClient('testnet', { apiBaseUrl: 'https://signet.verifiedx.io/api' });
// For mainnet (production)// const client = new btc.BtcClient('mainnet');Step 2: Generate Bitcoin Wallets
Section titled “Step 2: Generate Bitcoin Wallets”The SDK supports multiple ways to create Bitcoin wallets:
Method 1: Generate Random Wallet
Section titled “Method 1: Generate Random Wallet”// Generate a completely random walletconst keypair = client.generatePrivateKey();
console.log('Bitcoin Addresses:');console.log('- Legacy (P2PKH):', keypair.addresses.p2pkh);console.log('- Nested SegWit (P2SH):', keypair.addresses.p2sh);console.log('- Native SegWit (Bech32):', keypair.addresses.bech32);console.log('- Taproot (Bech32m):', keypair.addresses.bech32m);console.log('- Default Address:', keypair.address);console.log('- WIF:', keypair.wif);The default address is the native SegWit (bech32) address, which is also the address sendBtc spends from.
Method 2: Generate from Mnemonic
Section titled “Method 2: Generate from Mnemonic”// Generate wallet with mnemonic for recoveryconst mnemonicWallet = client.generateMnemonic();
console.log('Mnemonic Wallet:');console.log('- Mnemonic:', mnemonicWallet.mnemonic);console.log('- Address:', mnemonicWallet.address);console.log('- WIF:', mnemonicWallet.wif);generateMnemonic() returns a keypair whose mnemonic is a 24-word phrase.
Method 3: Restore from Mnemonic
Section titled “Method 3: Restore from Mnemonic”// Restore an existing wallet from mnemonicconst restoredWallet = client.privateKeyFromMnemonic( 'your mnemonic phrase goes here', 0 // account index);
console.log('Restored wallet address:', restoredWallet.address);Method 4: Import from WIF or Private Key
Section titled “Method 4: Import from WIF or Private Key”// Import from Wallet Import Format (WIF)const wifWallet = client.addressFromWif('your-wif-string-here');
// Import from private key hexconst privateKeyWallet = client.publicFromPrivate('your-private-key-hex');Method 5: Email-Based Wallet (Cross-Platform)
Section titled “Method 5: Email-Based Wallet (Cross-Platform)”For cross-platform compatibility with web wallets:
// Generate deterministic wallet from email/passwordconst emailWallet = client.generateEmailKeypair( 'user@example.com', 'secure-password', 0 // index);
console.log('Email-based wallet:', emailWallet.address);Step 3: Check Account Information
Section titled “Step 3: Check Account Information”Get detailed information about any Bitcoin address:
async function getAccountInfo(address: string) { try { const info = await client.getAddressInfo(address);
console.log('Account Information:'); console.log('- Balance:', info.balance, 'satoshis'); console.log('- Balance (BTC):', info.balance / 100000000, 'BTC'); console.log('- Total Received:', info.totalRecieved, 'satoshis'); console.log('- Total Sent:', info.totalSent, 'satoshis'); console.log('- Transaction Count:', info.txCount);
return info; } catch (error) { console.error('Error fetching account info:', error.message); }}
// Check your wallet balanceconst wallet = client.generatePrivateKey();await getAccountInfo(wallet.address);The figures count confirmed transactions only (the API’s chain_stats); an unconfirmed payment shows up after its block. Pass false as the second argument to get BTC instead of satoshis.
Step 4: Get Transaction History
Section titled “Step 4: Get Transaction History”Retrieve transaction history for any address:
async function getTransactionHistory(address: string) { try { const transactions = await client.getTransactions(address);
console.log(`Found ${transactions.length} transactions:`);
transactions.forEach((tx, index) => { console.log(`\nTransaction ${index + 1}:`); console.log('- TX ID:', tx.txid); console.log('- Confirmed:', tx.status.confirmed); console.log('- Block Height:', tx.status.block_height); console.log('- Fee:', tx.fee, 'satoshis'); console.log('- Size:', tx.size, 'bytes'); });
return transactions; } catch (error) { console.error('Error fetching transactions:', error.message); }}
await getTransactionHistory(wallet.address);getTransactions returns the API’s first page for the address (the newest transactions, unconfirmed ones first). In 9.0.3 its limit and before parameters are accepted but not sent, so they have no effect.
Step 5: Check Current Fee Rates
Section titled “Step 5: Check Current Fee Rates”Before sending transactions, check current network fee rates:
async function getCurrentFees() { try { const fees = await client.getFeeRates();
if (fees) { console.log('Current Fee Rates (sat/vB):'); console.log('- Fastest (next block):', fees.fastestFee); console.log('- Half hour:', fees.halfHourFee); console.log('- One hour:', fees.hourFee); console.log('- Economy:', fees.economyFee); console.log('- Minimum:', fees.minimumFee); }
return fees; } catch (error) { console.error('Error fetching fees:', error.message); }}
await getCurrentFees();Step 6: Send Bitcoin Transactions
Section titled “Step 6: Send Bitcoin Transactions”Simple Send (Recommended)
Section titled “Simple Send (Recommended)”The easiest way to send Bitcoin:
async function sendBitcoin(senderWif: string, recipientAddress: string, amountBtc: number) { try { console.log(`Sending ${amountBtc} BTC to ${recipientAddress}`);
// This method handles transaction creation and broadcasting const txId = await client.sendBtc( senderWif, recipientAddress, amountBtc, // in BTC, not satoshis 2 // fee rate in sat/vB (optional; 1 sat/vB is enough on the signet) );
if (!txId) { console.error('Nothing was sent'); return null; }
console.log('Transaction ID:', txId); return txId; } catch (error) { // BtcBroadcastUnknownError: the broadcast went out but no answer came back console.error('Transaction failed:', error.message); }}
// Send 0.0005 BTC (50,000 satoshis)const wallet = client.generatePrivateKey();await sendBitcoin( wallet.wif, 'recipient-bitcoin-address', 0.0005);sendBtc takes the amount in BTC. It spends the UTXOs of the WIF’s native SegWit (bech32) address, sends change back to it, and picks the API’s economy fee rate when feeRate is 0. It resolves to the txid, or null when nothing was sent (the transaction could not be built or the network refused it). It throws BtcBroadcastUnknownError when the broadcast went out but no definite answer came back: do not call sendBtc again, check with checkBroadcast(error.signedTxHex) and re-broadcast that same hex if it is absent.
Advanced Send (Manual Control)
Section titled “Advanced Send (Manual Control)”For more control over the transaction process:
async function advancedSend(senderWif: string, recipientAddress: string, amountBtc: number) { try { // Step 1: Create the transaction (amount in BTC) const createResult = await client.createTransaction( senderWif, recipientAddress, amountBtc, 15 // custom fee rate in sat/vB );
if (!createResult.success) { console.error('Failed to create transaction:', createResult.error); return; }
console.log('Transaction created successfully'); console.log('Raw transaction:', createResult.result);
// Step 2: Broadcast the transaction const broadcastResult = await client.broadcastTransaction(createResult.result!);
if (!broadcastResult.success) { console.error('Failed to broadcast transaction:', broadcastResult.error); return; }
console.log('Transaction broadcast successfully'); console.log('Transaction ID:', broadcastResult.result);
return broadcastResult.result; } catch (error) { console.error('Advanced send failed:', error.message); }}Step 7: Message Signing
Section titled “Step 7: Message Signing”Sign and verify Bitcoin messages:
function signMessage(message: string, wallet: any) { try { // Sign with WIF const signature = client.getSignatureFromWif(message, wallet.wif); console.log('Message signature:', signature);
// Alternative: Sign with private key // const signature2 = client.getSignature(message, wallet.privateKey);
return signature; } catch (error) { console.error('Signing failed:', error.message); }}
const wallet = client.generatePrivateKey();const signature = signMessage('Hello, Bitcoin!', wallet);Complete Example: Bitcoin Wallet Manager
Section titled “Complete Example: Bitcoin Wallet Manager”Here’s a comprehensive Bitcoin wallet manager:
import { btc } from 'vfx-web-sdk';
class BitcoinWalletManager { private client: btc.BtcClient;
constructor(network: 'mainnet' | 'testnet' = 'testnet') { // On testnet, read and broadcast on the VerifiedX signet this.client = network === 'testnet' ? new btc.BtcClient('testnet', { apiBaseUrl: 'https://signet.verifiedx.io/api' }) : new btc.BtcClient('mainnet'); }
// Create new wallet createWallet() { return this.client.generateMnemonic(); }
// Restore wallet from mnemonic restoreWallet(mnemonic: string, index: number = 0) { return this.client.privateKeyFromMnemonic(mnemonic, index); }
// Import wallet from WIF importWallet(wif: string) { return this.client.addressFromWif(wif); }
// Get wallet balance and info async getWalletInfo(address: string) { const [accountInfo, transactions] = await Promise.all([ this.client.getAddressInfo(address), this.client.getTransactions(address) ]);
return { balance: accountInfo.balance, balanceBTC: accountInfo.balance / 100000000, totalReceived: accountInfo.totalRecieved, totalSent: accountInfo.totalSent, transactionCount: accountInfo.txCount, recentTransactions: transactions.slice(0, 5) // Last 5 transactions }; }
// Send Bitcoin (amount in BTC) async sendBitcoin(fromWif: string, toAddress: string, amountBtc: number, feeRate?: number) { return await this.client.sendBtc(fromWif, toAddress, amountBtc, feeRate); }
// Get current network fees async getNetworkFees() { return await this.client.getFeeRates(); }
// Sign a message signMessage(message: string, wif: string) { return this.client.getSignatureFromWif(message, wif); }}
// Usage exampleasync function main() { const walletManager = new BitcoinWalletManager('testnet');
// Create a new wallet const wallet = walletManager.createWallet(); console.log('New Bitcoin wallet created:'); console.log('Mnemonic:', wallet.mnemonic); console.log('Address:', wallet.address); console.log('All address types:', wallet.addresses);
// Get wallet information try { const walletInfo = await walletManager.getWalletInfo(wallet.address); console.log('\nWallet Information:'); console.log('Balance:', walletInfo.balanceBTC, 'BTC'); console.log('Transactions:', walletInfo.transactionCount); } catch (error) { console.log('New wallet - no transactions yet'); }
// Check network fees const fees = await walletManager.getNetworkFees(); if (fees) { console.log('\nCurrent Network Fees:'); console.log('Fast:', fees.fastestFee, 'sat/vB'); console.log('Standard:', fees.hourFee, 'sat/vB'); console.log('Economy:', fees.economyFee, 'sat/vB'); }
// Sign a message const message = 'Hello from VerifiedX SDK!'; const signature = walletManager.signMessage(message, wallet.wif); console.log('\nMessage Signature:'); console.log('Message:', message); console.log('Signature:', signature);}
main().catch(console.error);Address Types Explained
Section titled “Address Types Explained”The SDK generates multiple address types for maximum compatibility:
Legacy (P2PKH) - addresses start with ‘1’ (mainnet) or ‘m’/‘n’ (testnet and signet)
Section titled “Legacy (P2PKH) - addresses start with ‘1’ (mainnet) or ‘m’/‘n’ (testnet and signet)”- Oldest address format
- Highest fees
- Maximum compatibility
Nested SegWit (P2SH) - addresses start with ‘3’ (mainnet) or ‘2’ (testnet and signet)
Section titled “Nested SegWit (P2SH) - addresses start with ‘3’ (mainnet) or ‘2’ (testnet and signet)”- Medium fees
- Good compatibility
- Wrapped SegWit transactions
Native SegWit (Bech32) - addresses start with ‘bc1q’ (mainnet) or ‘tb1q’ (testnet and signet)
Section titled “Native SegWit (Bech32) - addresses start with ‘bc1q’ (mainnet) or ‘tb1q’ (testnet and signet)”- Lower fees
- Modern standard
- Best for most use cases
Taproot (Bech32m) - addresses start with ‘bc1p’ (mainnet) or ‘tb1p’ (testnet and signet)
Section titled “Taproot (Bech32m) - addresses start with ‘bc1p’ (mainnet) or ‘tb1p’ (testnet and signet)”- Lowest fees
- Latest Bitcoin upgrade
- Advanced privacy features
Best Practices
Section titled “Best Practices”Security
Section titled “Security”- Secure WIF storage: WIF keys provide full access to funds
- Use strong passwords: For email-based wallets
- Test on testnet first: Always test with testnet Bitcoin
Performance
Section titled “Performance”- Cache fee rates: Don’t fetch fees for every transaction
- Batch operations: Group multiple API calls when possible
- Handle rate limits: mempool.space (the mainnet default) has API rate limits
Error Handling
Section titled “Error Handling”- Check balances: Ensure sufficient balance before sending
- Validate addresses: Verify recipient addresses
- Monitor confirmations: Track transaction confirmations
What’s Next?
Section titled “What’s Next?”Now that you understand Bitcoin basics:
- Learn Cross-Chain: Explore Cross-Chain Integration
- See Examples: Browse Code Snippets
- API Reference: Browse the BtcClient API
Common Issues
Section titled “Common Issues”Insufficient Balance
Section titled “Insufficient Balance”Make sure your wallet has enough Bitcoin. On testnet, get signet BTC from the testnet faucet (Signet BTC tab).
Transaction Fee Too Low
Section titled “Transaction Fee Too Low”Increase the fee rate if transactions are stuck in mempool. On the VerifiedX signet 1 sat/vB is enough and a block comes about every 10 minutes (see Signet).
Invalid Address Format
Section titled “Invalid Address Format”Ensure you’re using the correct address format for the recipient’s wallet.
Network Connection Issues
Section titled “Network Connection Issues”Check internet connection and try again. The SDK does not retry failed requests on its own. A broadcast that gets no answer within 30 seconds throws BtcBroadcastUnknownError instead of failing, so check it with checkBroadcast before sending again.
Getting Help
Section titled “Getting Help”- Check the BtcClient API Reference
- Browse Bitcoin Examples
- Report issues on GitHub