UNPKG

@btc-vision/transaction

Version:

OPNet transaction library allows you to create and sign transactions for the OPNet network.

205 lines 11.3 kB
import { Transaction } from '@btc-vision/bitcoin'; import type { UTXO } from '../utxo/interfaces/IUTXO.js'; import { FundingTransaction } from './builders/FundingTransaction.js'; import { TransactionBuilder } from './builders/TransactionBuilder.js'; import { TransactionType } from './enums/TransactionType.js'; import type { IDeploymentParameters, IFundingTransactionParameters, IInteractionParameters } from './interfaces/ITransactionParameters.js'; import type { ICancelTransactionParametersWithoutSigner, ICustomTransactionWithoutSigner, IFundingTransactionParametersWithoutSigner, InteractionParametersWithoutSigner } from './interfaces/IWeb3ProviderTypes.js'; import type { RawChallenge } from '../epoch/interfaces/IChallengeSolution.js'; import type { IConsolidatedInteractionParameters } from './interfaces/IConsolidatedTransactionParameters.js'; import type { BitcoinTransferBase, CancelledTransaction, DeploymentResult, InteractionResponse } from './interfaces/ITransactionResponses.js'; import type { ICancelTransactionParameters } from './interfaces/ICancelTransactionParameters.js'; import type { ICustomTransactionParameters } from './interfaces/ICustomTransactionParameters.js'; export interface FundingTransactionResponse { readonly tx: Transaction; readonly original: FundingTransaction; readonly estimatedFees: bigint; readonly nextUTXOs: UTXO[]; readonly inputUtxos: UTXO[]; } export type { BitcoinTransferBase } from './interfaces/ITransactionResponses.js'; export interface BitcoinTransferResponse extends BitcoinTransferBase { readonly original: FundingTransaction; } /** * Response from signConsolidatedInteraction. * Contains both setup and reveal transactions for the CHCT system. */ export interface ConsolidatedInteractionResponse { /** Setup transaction hex - creates P2WSH commitment outputs */ readonly setupTransaction: string; /** Reveal transaction hex - spends commitments, reveals data in witnesses */ readonly revealTransaction: string; /** Setup transaction ID */ readonly setupTxId: string; /** Reveal transaction ID */ readonly revealTxId: string; /** Total fees across both transactions in satoshis */ readonly totalFees: bigint; /** Number of data chunks */ readonly chunkCount: number; /** Total compiled data size in bytes */ readonly dataSize: number; /** Challenge for the interaction */ readonly challenge: RawChallenge; /** Input UTXOs used */ readonly inputUtxos: UTXO[]; /** Compiled target script (same as InteractionTransaction) */ readonly compiledTargetScript: string; } export declare class TransactionFactory { debug: boolean; private readonly DUMMY_PUBKEY; private readonly P2TR_SCRIPT; private readonly INITIAL_FUNDING_ESTIMATE; private readonly MAX_ITERATIONS; /** * @description Creates a cancellable transaction. * @param {ICancelTransactionParameters | ICancelTransactionParametersWithoutSigner} params - The cancel transaction parameters * @returns {Promise<CancelledTransaction>} - The cancelled transaction result */ createCancellableTransaction(params: ICancelTransactionParameters | ICancelTransactionParametersWithoutSigner): Promise<CancelledTransaction>; /** * @description Generate a transaction with a custom script. * @param {ICustomTransactionParameters | ICustomTransactionWithoutSigner} interactionParameters - The custom transaction parameters * @returns {Promise<[string, string, UTXO[], UTXO[]]>} - The signed transaction tuple [fundingTx, customTx, nextUTXOs, inputUtxos] */ createCustomScriptTransaction(interactionParameters: ICustomTransactionParameters | ICustomTransactionWithoutSigner): Promise<[string, string, UTXO[], UTXO[]]>; /** * @description Generates the required transactions. * @param {IInteractionParameters | InteractionParametersWithoutSigner} interactionParameters - The interaction parameters * @returns {Promise<InteractionResponse>} - The signed transaction */ signInteraction(interactionParameters: IInteractionParameters | InteractionParametersWithoutSigner): Promise<InteractionResponse>; /** * @description Generates a consolidated interaction transaction (CHCT system). * * Drop-in replacement for signInteraction that bypasses BIP110/Bitcoin Knots censorship. * Uses P2WSH with HASH160 commitments instead of Tapscript (which uses OP_IF and gets censored). * * Returns two transactions: * - Setup: Creates P2WSH outputs with hash commitments to data chunks * - Reveal: Spends those outputs, revealing data in witnesses * * Data integrity is consensus-enforced - if data is stripped/modified, * HASH160(data) != committed_hash and the transaction is INVALID. * * @param {IConsolidatedInteractionParameters} interactionParameters - Same parameters as signInteraction * @returns {Promise<ConsolidatedInteractionResponse>} - Both setup and reveal transactions */ signConsolidatedInteraction(interactionParameters: IConsolidatedInteractionParameters): Promise<ConsolidatedInteractionResponse>; /** * @description Generates the required transactions. * @param {IDeploymentParameters} deploymentParameters - The deployment parameters * @returns {Promise<DeploymentResult>} - The signed transaction */ signDeployment(deploymentParameters: IDeploymentParameters): Promise<DeploymentResult>; /** * @description Creates a funding transaction. * @param {IFundingTransactionParameters} parameters - The funding transaction parameters * @returns {Promise<BitcoinTransferBase>} - The signed transaction */ createBTCTransfer(parameters: IFundingTransactionParameters | IFundingTransactionParametersWithoutSigner): Promise<BitcoinTransferBase>; /** * Get all new UTXOs of a generated transaction. * @param {TransactionBuilder<TransactionType>} original - The original transaction * @param {Transaction} tx - The transaction * @param {string} to - The address to filter * @returns {UTXO[]} - The new UTXOs belonging to the specified address */ getAllNewUTXOs(original: TransactionBuilder<TransactionType>, tx: Transaction, to: string): UTXO[]; /** * Parse optional inputs and normalize nonWitnessUtxo format. * @param {UTXO[]} optionalInputs - The optional inputs to parse * @returns {UTXO[]} - The parsed inputs with normalized nonWitnessUtxo */ private parseOptionalInputs; /** * Detect and use OP_WALLET for funding transactions if available. * * @param {IFundingTransactionParameters | IFundingTransactionParametersWithoutSigner} fundingParams - The funding transaction parameters * @return {Promise<BitcoinTransferBase | null>} - The funding transaction response or null if OP_WALLET not available */ private detectFundingOPWallet; /** * Detect and use OP_WALLET for cancel transactions if available. * @param {ICancelTransactionParameters | ICancelTransactionParametersWithoutSigner} interactionParameters - The cancel parameters * @returns {Promise<CancelledTransaction | null>} - The cancelled transaction or null if OP_WALLET not available */ private detectCancelOPWallet; /** * Detect and use OP_WALLET for interaction transactions if available. * @param {IInteractionParameters | InteractionParametersWithoutSigner} interactionParameters - The interaction parameters * @returns {Promise<InteractionResponse | null>} - The interaction response or null if OP_WALLET not available */ private detectInteractionOPWallet; /** * Detect and use OP_WALLET for deployment transactions if available. * @param {IDeploymentParameters | IDeploymentParametersWithoutSigner} deploymentParameters - The deployment parameters * @returns {Promise<DeploymentResult | null>} - The deployment result or null if OP_WALLET not available */ private detectDeploymentOPWallet; /** * Create and sign a funding transaction. * @param {IFundingTransactionParameters} parameters - The funding transaction parameters * @returns {Promise<FundingTransactionResponse>} - The funding transaction response */ private createFundTransaction; /** * Check if the UTXOs contain any P2WDA inputs * * This method examines both main UTXOs and optional inputs to determine * if any of them are P2WDA addresses. P2WDA detection is based on the * witness script pattern: (OP_2DROP * 5) <pubkey> OP_CHECKSIG * * @param {UTXO[]} utxos - The main UTXOs to check * @returns {boolean} - true if any UTXO is P2WDA, false otherwise */ private hasP2WDAInputs; /** * Sign a P2WDA interaction transaction * * P2WDA interactions are fundamentally different from standard OP_NET interactions. * Instead of using a two-transaction model (funding + interaction), P2WDA embeds * the operation data directly in the witness field of a single transaction. * This achieves significant cost savings through the witness discount. * * Key differences: * - Single transaction instead of two * - Operation data in witness field instead of taproot script * - 75% cost reduction for data storage * - No separate funding transaction needed * * @param {IInteractionParameters | InteractionParametersWithoutSigner} interactionParameters - The interaction parameters * @returns {Promise<InteractionResponse>} - The signed P2WDA interaction response */ private signP2WDAInteraction; /** * Get the priority fee from transaction parameters. * @param {ITransactionParameters} params - The transaction parameters * @returns {bigint} - The priority fee, minimum dust if below threshold */ private getPriorityFee; /** * Common iteration logic for finding the correct funding amount. * * This method iteratively estimates the required funding amount by simulating * transactions until the amount converges or max iterations is reached. * * @param {P extends IInteractionParameters | IDeploymentParameters | ICustomTransactionParameters} params - The transaction parameters * @param {new (params: P) => T} TransactionClass - The transaction class constructor * @param {(tx: T extends InteractionTransaction | DeploymentTransaction | CustomScriptTransaction) => Promise<bigint>} calculateAmount - Function to calculate required amount * @param {string} debugPrefix - Prefix for debug logging * @returns {Promise<{finalTransaction: T extends InteractionTransaction | DeploymentTransaction | CustomScriptTransaction, estimatedAmount: bigint, challenge: IChallengeSolution | null}>} - The final transaction and estimated amount */ private iterateFundingAmount; /** * Convert a transaction output to a UTXO. * @param {Transaction} tx - The transaction * @param {string} to - The address * @param {number} index - The output index * @returns {UTXO[]} - The UTXO array (empty if output doesn't exist) */ private getUTXOAsTransaction; } //# sourceMappingURL=TransactionFactory.d.ts.map