@btc-vision/transaction
Version:
OPNet transaction library allows you to create and sign transactions for the OPNet network.
185 lines (184 loc) • 6.77 kB
JavaScript
import { crypto as bitCrypto, PaymentType, toXOnly, } from '@btc-vision/bitcoin';
import { TransactionType } from '../enums/TransactionType.js';
import { TransactionBuilder } from './TransactionBuilder.js';
import { CustomGenerator } from '../../generators/builders/CustomGenerator.js';
import { BitcoinUtils } from '../../utils/BitcoinUtils.js';
import { EcKeyPair } from '../../keypair/EcKeyPair.js';
import { AddressGenerator } from '../../generators/AddressGenerator.js';
export class CustomScriptTransaction extends TransactionBuilder {
constructor(parameters) {
super(parameters);
this.type = TransactionType.CUSTOM_CODE;
this.tapLeafScript = null;
this.targetScriptRedeem = null;
this.leftOverFundsScriptRedeem = null;
this.customFinalizer = (_inputIndex, input) => {
if (!this.tapLeafScript) {
throw new Error('Tap leaf script is required');
}
const scriptSolution = this.getScriptSolution(input);
const witness = scriptSolution
.concat(this.tapLeafScript.script)
.concat(this.tapLeafScript.controlBlock);
if (this.annexData && this.annexData.length > 0) {
const annex = this.annexData[0] === 0x50
? this.annexData
: Buffer.concat([Buffer.from([0x50]), this.annexData]);
witness.push(annex);
}
return {
finalScriptWitness: TransactionBuilder.witnessStackToScriptWitness(witness),
};
};
if (!parameters.script)
throw new Error('Bitcoin script is required');
if (!parameters.witnesses)
throw new Error('Witness(es) are required');
this.witnesses = parameters.witnesses;
this.randomBytes = parameters.randomBytes || BitcoinUtils.rndBytes();
this.scriptSeed = this.getContractSeed();
this.contractSigner = EcKeyPair.fromSeedKeyPair(this.scriptSeed, this.network);
this.generator = new CustomGenerator(this.internalPubKeyToXOnly(), this.network);
this.compiledTargetScript = this.generator.compile(parameters.script);
this.scriptTree = this.getScriptTree();
this.internalInit();
this._scriptAddress = AddressGenerator.generatePKSH(this.scriptSeed, this.network);
}
get scriptAddress() {
return this._scriptAddress;
}
get p2trAddress() {
return this.to || this.getScriptAddress();
}
getRndBytes() {
return this.randomBytes;
}
contractSignerXOnlyPubKey() {
return toXOnly(Buffer.from(this.contractSigner.publicKey));
}
async buildTransaction() {
if (!this.to) {
this.to = this.getScriptAddress();
}
const selectedRedeem = this.contractSigner
? this.targetScriptRedeem
: this.leftOverFundsScriptRedeem;
if (!selectedRedeem) {
throw new Error('Left over funds script redeem is required');
}
if (!selectedRedeem.redeemVersion) {
throw new Error('Left over funds script redeem version is required');
}
if (!selectedRedeem.output) {
throw new Error('Left over funds script redeem output is required');
}
this.tapLeafScript = {
leafVersion: selectedRedeem.redeemVersion,
script: selectedRedeem.output,
controlBlock: this.getWitness(),
};
this.addInputsFromUTXO();
const amountSpent = this.getTransactionOPNetFee();
this.addOutput({
value: Number(amountSpent),
address: this.to,
});
await this.addRefundOutput(amountSpent + this.addOptionalOutputsAndGetAmount());
}
async signInputs(transaction) {
if (!this.contractSigner) {
await super.signInputs(transaction);
return;
}
for (let i = 0; i < transaction.data.inputs.length; i++) {
if (i === 0) {
try {
transaction.signInput(0, this.contractSigner);
}
catch (e) { }
transaction.signInput(0, this.getSignerKey());
transaction.finalizeInput(0, this.customFinalizer);
}
else {
transaction.signInput(i, this.getSignerKey());
transaction.finalizeInput(i);
}
}
}
generateScriptAddress() {
return {
internalPubkey: this.internalPubKeyToXOnly(),
network: this.network,
scriptTree: this.scriptTree,
name: PaymentType.P2TR,
};
}
generateTapData() {
const selectedRedeem = this.contractSigner
? this.targetScriptRedeem
: this.leftOverFundsScriptRedeem;
if (!selectedRedeem) {
throw new Error('Left over funds script redeem is required');
}
if (!this.scriptTree) {
throw new Error('Script tree is required');
}
return {
internalPubkey: this.internalPubKeyToXOnly(),
network: this.network,
scriptTree: this.scriptTree,
redeem: selectedRedeem,
name: PaymentType.P2TR,
};
}
getScriptSolution(input) {
if (!input.tapScriptSig) {
throw new Error('Tap script signature is required');
}
const witnesses = [...this.witnesses];
if (input.tapScriptSig) {
for (const sig of input.tapScriptSig) {
witnesses.push(sig.signature);
}
}
return witnesses;
}
getContractSeed() {
return bitCrypto.hash256(this.randomBytes);
}
getPubKeys() {
const pubkeys = [Buffer.from(this.signer.publicKey)];
if (this.contractSigner) {
pubkeys.push(Buffer.from(this.contractSigner.publicKey));
}
return pubkeys;
}
generateRedeemScripts() {
this.targetScriptRedeem = {
name: PaymentType.P2TR,
output: this.compiledTargetScript,
redeemVersion: 192,
};
this.leftOverFundsScriptRedeem = {
name: PaymentType.P2TR,
output: this.getLeafScript(),
redeemVersion: 192,
};
}
getLeafScript() {
return TransactionBuilder.LOCK_LEAF_SCRIPT;
}
getScriptTree() {
this.generateRedeemScripts();
return [
{
output: this.compiledTargetScript,
version: 192,
},
{
output: this.getLeafScript(),
version: 192,
},
];
}
}