UNPKG

@btc-vision/transaction

Version:

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

81 lines (80 loc) 2.3 kB
import { Psbt, } from '@btc-vision/bitcoin'; import { TweakedTransaction } from '../shared/TweakedTransaction.js'; export class PsbtTransaction extends TweakedTransaction { constructor(data) { super(data); this.logColor = '#00ffe1'; this.feesAddition = 10000n; this.sighashTypes = []; this.signer = data.signer; this.network = data.network; this.transaction = data.psbt; this.ignoreSignatureError(); this.tweakSigner(); this.internalInit(); } static fromBase64(data, params) { const psbt = Psbt.fromBase64(data, { network: params.network, }); return new PsbtTransaction({ ...params, psbt, }); } static fromHex(data, params) { const psbt = Psbt.fromHex(data, { network: params.network, }); return new PsbtTransaction({ ...params, psbt, }); } static from(params) { const psbt = new Psbt({ network: params.network }); return new PsbtTransaction({ ...params, psbt, }); } extractTransaction() { return this.transaction.extractTransaction(); } final() { return this.extractTransaction().toHex(); } toHex() { return this.transaction.toHex(); } addInput(input, checkPartialSigs = false) { this.transaction.addInput(input, checkPartialSigs); } addOutput(output) { if (!output.value) return; this.transaction.addOutput(output); } attemptFinalizeInputs(n = 1) { try { const inputs = this.transaction.data.inputs; for (let i = n; i < inputs.length; i++) { const input = inputs[i]; if (input.finalScriptWitness) { this.transaction.finalizeTaprootInput(i, input.finalScriptWitness); } else { this.transaction.finalizeInput(i); } } return true; } catch (e) { this.warn(e.stack || "Couldn't finalize inputs"); return false; } } getPSBT() { return this.transaction; } }