UNPKG

@nawab_kibria/bitcoin-lib

Version:

A comprehensive Bitcoin HD wallet library with BIP84, BIP44, BIP49 support, mnemonic generation and restoration

144 lines (143 loc) 4.17 kB
import * as bitcoin from 'bitcoinjs-lib'; import { AddressType, ElectrumBalance as WalletBalance, ElectrumUTXO as UTXO, ElectrumHistoryItem as TransactionHistory, PreparedTransaction, FeeEstimate, BroadcastResult } from '../types/wallet'; export interface WalletConfig { electrumServer: { host: string; port: number; ssl?: boolean; }; network: bitcoin.Network; defaultAddressType?: AddressType; defaultAddressCount?: number; } export interface ImportOptions { mnemonic?: string; extendedPrivateKey?: string; extendedPublicKey?: string; passphrase?: string; } export interface AddressInfo { address: string; index: number; addressType: AddressType; path: string; publicKey: string; privateKey?: string; } export interface WalletExport { mnemonic?: string; extendedPrivateKey?: string; extendedPublicKey: string; network: string; isWatchOnly: boolean; } export declare class WalletManager { private config; private wallet?; private electrumClient?; private transactionBuilder?; private isConnected; private mnemonic?; private extendedPrivateKey?; private extendedPublicKey?; private isWatchOnly; constructor(config: WalletConfig); /** * 1. Generate a new wallet with mnemonic and extended keys */ generateWallet(strength?: number): WalletExport; /** * 2. Export wallet (extended keys, mnemonic) */ exportWallet(): WalletExport; /** * 3. Import wallet from extended key or mnemonic */ importWallet(options: ImportOptions): Promise<void>; /** * Connect to Electrum server */ connect(): Promise<void>; /** * Disconnect from Electrum server */ disconnect(): void; /** * Derive address at specific index */ deriveAddress(index: number, addressType?: AddressType): AddressInfo; /** * 4. Fetch balance for address */ fetchBalance(address: string): Promise<WalletBalance>; /** * 5. Fetch UTXOs for address */ fetchUTXOs(address: string): Promise<UTXO[]>; /** * 6. Create transaction */ createTransaction(fromAddress: string, toAddress: string, amount: number, feeRate: number, privateKey: string, addressType?: AddressType): Promise<PreparedTransaction>; /** * 7. Estimate fees */ estimateFees(): Promise<FeeEstimate>; /** * 8. Broadcast transaction */ broadcastTransaction(txHex: string): Promise<BroadcastResult>; /** * 9. Transaction history for address */ getTransactionHistory(address: string): Promise<TransactionHistory[]>; /** * Get wallet status and information */ getWalletInfo(): { isImported: boolean; isWatchOnly: boolean; isConnected: boolean; network: string; defaultAddressType: AddressType; hasMnemonic: boolean; hasExtendedKey: boolean; }; /** * Generate multiple addresses for different types */ generateAddresses(count?: number): { legacy: AddressInfo[]; segwit: AddressInfo[]; nativeSegwit: AddressInfo[]; }; /** * Get balances for multiple addresses */ getMultipleBalances(addresses: string[]): Promise<Record<string, WalletBalance>>; /** * Estimate transaction size and fees for given parameters */ estimateTransactionCost(fromAddress: string, toAddress: string, amount: number, addressType?: AddressType): Promise<{ estimatedSize: number; feeEstimates: FeeEstimate; recommendedFees: { fastestFee: number; halfHourFee: number; hourFee: number; economyFee: number; }; }>; private ensureConnected; /** * Get mnemonic (only if wallet was created/imported with mnemonic) */ getMnemonic(): string | undefined; /** * Get extended private key (only for full control wallets) */ getExtendedPrivateKey(): string | undefined; /** * Get extended public key */ getExtendedPublicKey(): string | undefined; }