@btc-vision/transaction
Version:
OPNet transaction library allows you to create and sign transactions for the OPNet network.
62 lines (61 loc) • 2.05 kB
JavaScript
import { TransactionType } from '../enums/TransactionType.js';
import { opcodes, script } from '@btc-vision/bitcoin';
import { TransactionBuilder } from './TransactionBuilder.js';
export class FundingTransaction extends TransactionBuilder {
constructor(parameters) {
super(parameters);
this.type = TransactionType.FUNDING;
this.amount = parameters.amount;
this.splitInputsInto = parameters.splitInputsInto ?? 1;
this.internalInit();
}
async buildTransaction() {
if (!this.to) {
throw new Error('Recipient address is required');
}
this.addInputsFromUTXO();
if (this.splitInputsInto > 1) {
this.splitInputs(this.amount);
}
else 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());
}
splitInputs(amountSpent) {
if (!this.to) {
throw new Error('Recipient address is required');
}
const splitAmount = amountSpent / BigInt(this.splitInputsInto);
for (let i = 0; i < this.splitInputsInto; i++) {
if (this.isPubKeyDestination) {
this.addOutput({
value: Number(splitAmount),
script: Buffer.from(this.to.slice(2), 'hex'),
});
}
else {
this.addOutput({
value: Number(splitAmount),
address: this.to,
});
}
}
}
getSignerKey() {
return this.signer;
}
}