UNPKG

@xchainjs/xchain-bitcoin

Version:

Custom Bitcoin client and utilities used by XChainJS clients

141 lines (140 loc) 5.37 kB
import { AssetInfo, FeeRate } from '@xchainjs/xchain-client'; import { Address } from '@xchainjs/xchain-util'; import { Client as UTXOClient, PreparedTx, TxParams, UTXO, UtxoClientParams, UtxoSelectionPreferences } from '@xchainjs/xchain-utxo'; import * as Bitcoin from 'bitcoinjs-lib'; import { AddressFormat } from './types'; export declare const defaultBTCParams: UtxoClientParams; /** * Custom Bitcoin client */ declare abstract class Client extends UTXOClient { protected addressFormat: AddressFormat; /** * Constructor * Initializes the client with network type and other parameters. * @param {UtxoClientParams} params */ constructor(params?: UtxoClientParams & { addressFormat?: AddressFormat; }); /** * Get BTC asset info. * @returns {AssetInfo} BTC asset information. */ getAssetInfo(): AssetInfo; /** * Validate the given Bitcoin address. * @param {string} address Bitcoin address to validate. * @returns {boolean} `true` if the address is valid, `false` otherwise. */ validateAddress(address: string): boolean; /** * Compile memo into a buffer. * @param {string} memo Memo to compile. * @returns {Buffer} Compiled memo. */ protected compileMemo(memo: string): Buffer; /** * Get transaction fee from UTXOs. * @param {UTXO[]} inputs UTXOs to calculate fee from. * @param {FeeRate} feeRate Fee rate. * @param {Buffer | null} data Compiled memo (Optional). * @returns {number} Transaction fee. */ protected getFeeFromUtxos(inputs: UTXO[], feeRate: FeeRate, data?: Buffer | null): number; /** * Enhanced Bitcoin transaction builder with comprehensive validation and optimal UTXO selection * @param params Transaction parameters * @returns Enhanced transaction build result with PSBT, UTXOs, and inputs */ buildTxEnhanced({ amount, recipient, memo, feeRate, sender, spendPendingUTXO, utxoSelectionPreferences, selectedUtxos, }: TxParams & { feeRate: FeeRate; sender: Address; spendPendingUTXO?: boolean; utxoSelectionPreferences?: UtxoSelectionPreferences; selectedUtxos?: UTXO[]; }): Promise<{ psbt: Bitcoin.Psbt; utxos: UTXO[]; inputs: UTXO[]; }>; /** * Build a Bitcoin transaction with enhanced validation and performance. * Now uses the enhanced logic internally while maintaining the same API. * @param param0 */ buildTx({ amount, recipient, memo, feeRate, sender, spendPendingUTXO, }: TxParams & { feeRate: FeeRate; sender: Address; spendPendingUTXO?: boolean; withTxHex?: boolean; }): Promise<{ psbt: Bitcoin.Psbt; utxos: UTXO[]; inputs: UTXO[]; }>; /** * Send maximum possible amount (sweep) with optimal fee calculation * @param params Send max parameters * @returns Transaction details with maximum sendable amount */ sendMax({ sender, recipient, memo, feeRate, spendPendingUTXO, utxoSelectionPreferences, selectedUtxos, }: { sender: Address; recipient: Address; memo?: string; feeRate: FeeRate; spendPendingUTXO?: boolean; utxoSelectionPreferences?: UtxoSelectionPreferences; selectedUtxos?: UTXO[]; }): Promise<{ psbt: Bitcoin.Psbt; utxos: UTXO[]; inputs: UTXO[]; maxAmount: number; fee: number; }>; /** * Prepare maximum amount transfer (sweep transaction) * @param params Send max parameters * @returns Prepared transaction with maximum sendable amount */ prepareMaxTx({ sender, recipient, memo, feeRate, spendPendingUTXO, utxoSelectionPreferences, selectedUtxos, }: { sender: Address; recipient: Address; memo?: string; feeRate: FeeRate; spendPendingUTXO?: boolean; utxoSelectionPreferences?: UtxoSelectionPreferences; selectedUtxos?: UTXO[]; }): Promise<PreparedTx & { maxAmount: number; fee: number; }>; /** * Enhanced prepare transfer with comprehensive validation and optimal UTXO selection. * * @param params The transfer options with enhanced UTXO selection preferences. * @returns The raw unsigned transaction with enhanced error handling. */ prepareTxEnhanced({ sender, memo, amount, recipient, spendPendingUTXO, feeRate, utxoSelectionPreferences, selectedUtxos, }: TxParams & { sender: Address; feeRate: FeeRate; spendPendingUTXO?: boolean; utxoSelectionPreferences?: UtxoSelectionPreferences; selectedUtxos?: UTXO[]; }): Promise<PreparedTx>; /** * Prepare transfer with enhanced validation and performance. * Now uses the enhanced logic internally while maintaining the same API. * * @deprecated Use `prepareTxEnhanced` directly for explicit enhanced UTXO selection. * @param {TxParams&Address&FeeRate&boolean} params The transfer options. * @returns {PreparedTx} The raw unsigned transaction. */ prepareTx({ sender, memo, amount, recipient, spendPendingUTXO, feeRate, }: TxParams & { sender: Address; feeRate: FeeRate; spendPendingUTXO?: boolean; }): Promise<PreparedTx>; } export { Client };