@btc-vision/transaction
Version:
OPNet transaction library allows you to create and sign transactions for the OPNet network.
52 lines (51 loc) • 2.01 kB
JavaScript
import { TransactionType } from '../enums/TransactionType.js';
import { getFinalScripts, opcodes, script } from '@btc-vision/bitcoin';
import { TransactionBuilder } from './TransactionBuilder.js';
export class ChallengeSolutionTransaction extends TransactionBuilder {
constructor(parameters) {
super(parameters);
this.type = TransactionType.CHALLENGE_SOLUTION;
this.customFinalizerP2SH = (inputIndex, input, scriptA, isSegwit, isP2SH, isP2WSH) => {
const inputDecoded = this.inputs[inputIndex];
if (isP2SH && inputDecoded && inputDecoded.redeemScript) {
const scriptSig = script.compile([this.challengeSolution, inputDecoded.redeemScript]);
return {
finalScriptSig: scriptSig,
finalScriptWitness: undefined,
};
}
return getFinalScripts(inputIndex, input, scriptA, isSegwit, isP2SH, isP2WSH, false);
};
this.amount = parameters.amount;
this.challengeSolution = parameters.challengeSolution;
this.internalInit();
}
async buildTransaction() {
if (!this.to) {
throw new Error('Recipient address is required');
}
this.addInputsFromUTXO();
if (this.isPubKeyDestination) {
const pubKeyScript = script.compile([
Buffer.from(this.to.replace('0x', ''), 'hex'),
opcodes.OP_CHECKSIG,
]);
this.addOutput({
value: Number(this.amount),
script: pubKeyScript,
});
}
else {
this.addOutput({
value: Number(this.amount),
address: this.to,
});
}
await this.addRefundOutput(this.amount + this.addOptionalOutputsAndGetAmount());
}
async signInput(transaction, input, i, signer, reverse = false, errored = false) {
}
getSignerKey() {
return this.signer;
}
}