UNPKG

@btc-vision/transaction

Version:

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

64 lines 2.87 kB
import { concat, crypto as bitCrypto, equals, networks, opcodes, payments, script, toXOnly, } from '@btc-vision/bitcoin'; import { DeploymentGenerator } from '../generators/builders/DeploymentGenerator.js'; import { Features } from '../generators/Features.js'; export class TapscriptVerificator { static TAP_SCRIPT_VERSION = 192; static getContractAddress(params) { const { scriptTree } = TapscriptVerificator.buildScriptTree(params); return TapscriptVerificator.generateAddressFromScript(params, scriptTree); } static verifyControlBlock(params, controlBlock) { const { scriptTree, compiledTargetScript, network } = TapscriptVerificator.buildScriptTree(params); const tapData = payments.p2tr({ internalPubkey: toXOnly(params.deployerPubKey), network: network, scriptTree: scriptTree, redeem: { output: compiledTargetScript, redeemVersion: TapscriptVerificator.TAP_SCRIPT_VERSION, }, }); const witness = tapData.witness; if (!witness || witness.length === 0) { return false; } const requiredControlBlock = witness[witness.length - 1]; return equals(requiredControlBlock, controlBlock); } static getContractSeed(deployerPubKey, bytecode, saltHash) { const sha256OfBytecode = bitCrypto.hash256(bytecode); const buf = concat([deployerPubKey, saltHash, sha256OfBytecode]); return bitCrypto.hash256(buf); } static generateAddressFromScript(params, scriptTree) { const network = params.network || networks.bitcoin; const transactionData = { internalPubkey: toXOnly(params.deployerPubKey), network: network, scriptTree: scriptTree, }; const tx = payments.p2tr(transactionData); return tx.address; } static buildScriptTree(params) { const network = params.network || networks.bitcoin; const scriptBuilder = new DeploymentGenerator(params.deployerPubKey, toXOnly(params.contractSaltPubKey), network); const compiledTargetScript = scriptBuilder.compile(params.bytecode, params.originalSalt, params.challenge, params.priorityFee, params.calldata, params.features); const lockLeafScript = script.compile([ toXOnly(params.deployerPubKey), opcodes.OP_CHECKSIG, ]); const scriptTree = [ { output: compiledTargetScript, version: TapscriptVerificator.TAP_SCRIPT_VERSION, }, { output: lockLeafScript, version: TapscriptVerificator.TAP_SCRIPT_VERSION, }, ]; return { scriptTree, compiledTargetScript, network }; } } //# sourceMappingURL=TapscriptVerificator.js.map