/** * Bitcoin transaction building and signing * * Uses @scure/btc-signer for P2WPKH (native SegWit) transactions. * Follows Leather/Xverse wallet patterns. */ import * as btc from "@scure/btc-signer"; import type { Network } from "../config/networks.js"; import type { UTXO } from "../services/mempool-api.js"; /** * Options for building a Bitcoin transaction */ export interface BuildBtcTransactionOptions { /** * UTXOs to spend from */ utxos: UTXO[]; /** * Recipient Bitcoin address (bc1q... or tb1q...) */ recipient: string; /** * Amount to send in satoshis */ amount: number; /** * Fee rate in sat/vB */ feeRate: number; /** * Sender's public key (compressed, 33 bytes) */ senderPubKey: Uint8Array; /** * Sender's address for change output */ senderAddress: string; /** * Network (mainnet or testnet) */ network: Network; } /** * Result from building a Bitcoin transaction */ export interface BuildBtcTransactionResult { /** * Unsigned transaction object (ready for signing) */ tx: btc.Transaction; /** * Fee paid in satoshis */ fee: number; /** * Change amount in satoshis (0 if no change output) */ change: number; /** * Transaction size estimate in virtual bytes */ vsize: number; /** * UTXOs used as inputs */ inputUtxos: UTXO[]; } /** * Result from signing a Bitcoin transaction */ export interface SignBtcTransactionResult { /** * Signed transaction as hex string (ready for broadcast) */ txHex: string; /** * Transaction ID */ txid: string; /** * Transaction size in virtual bytes */ vsize: number; } /** * Estimate the size of a P2WPKH transaction in virtual bytes * * Formula: overhead + (inputs * input_size) + (outputs * output_size) * * @param inputCount - Number of inputs * @param outputCount - Number of outputs * @returns Estimated size in virtual bytes * * @example * ```typescript * // 1 input, 2 outputs (recipient + change) * const vsize = estimateTxSize(1, 2); * console.log(vsize); // ~140.5 vB * ``` */ export declare function estimateTxSize(inputCount: number, outputCount: number): number; /** * Get the @scure/btc-signer network object for a network name */ export declare function getBtcNetwork(network: Network): typeof btc.NETWORK; /** * Build an unsigned Bitcoin transaction * * Creates a P2WPKH transaction with: * - Selected UTXOs as inputs * - Recipient output * - Change output (if above dust threshold) * * @param options - Transaction building options * @returns Unsigned transaction and metadata * @throws Error if insufficient funds or invalid parameters * * @example * ```typescript * const result = buildBtcTransaction({ * utxos: [...], * recipient: "bc1q...", * amount: 50000, * feeRate: 10, * senderPubKey: pubKeyBytes, * senderAddress: "bc1q...", * network: "mainnet", * }); * ``` */ export declare function buildBtcTransaction(options: BuildBtcTransactionOptions): BuildBtcTransactionResult; /** * Sign a Bitcoin transaction with a private key * * Signs all inputs and finalizes the transaction for broadcast. * * SECURITY: The private key should be a Uint8Array from the wallet session. * Never serialize the private key to WIF/hex. * * @param tx - Unsigned transaction from buildBtcTransaction * @param privateKey - Private key as Uint8Array (32 bytes) * @returns Signed transaction hex and metadata * * @example * ```typescript * const { tx } = buildBtcTransaction({...}); * const result = signBtcTransaction(tx, privateKeyBytes); * console.log(result.txHex); // Ready for broadcast * console.log(result.txid); // Transaction ID * ``` */ export declare function signBtcTransaction(tx: btc.Transaction, privateKey: Uint8Array): SignBtcTransactionResult; /** * Build and sign a Bitcoin transaction in one step * * Convenience function that combines buildBtcTransaction and signBtcTransaction. * * @param options - Transaction building options * @param privateKey - Private key as Uint8Array (32 bytes) * @returns Signed transaction ready for broadcast * * @example * ```typescript * const result = buildAndSignBtcTransaction( * { utxos, recipient, amount, feeRate, senderPubKey, senderAddress, network }, * privateKey * ); * // Broadcast result.txHex * ``` */ export declare function buildAndSignBtcTransaction(options: BuildBtcTransactionOptions, privateKey: Uint8Array): SignBtcTransactionResult & { fee: number; change: number; }; //# sourceMappingURL=bitcoin-builder.d.ts.map