UNPKG

@btc-vision/transaction

Version:

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

68 lines (67 loc) 2.69 kB
import { crypto, networks, opcodes, script } from '@btc-vision/bitcoin'; import { Generator } from '../Generator.js'; export const OPNET_DEPLOYMENT_VERSION = 0x00; export const versionBuffer = Buffer.from([OPNET_DEPLOYMENT_VERSION]); export class DeploymentGenerator extends Generator { constructor(senderPubKey, contractSaltPubKey, network = networks.bitcoin) { super(senderPubKey, contractSaltPubKey, network); } compile(contractBytecode, contractSalt, challenge, maxPriority, calldata, features) { const asm = this.getAsm(contractBytecode, contractSalt, challenge, maxPriority, calldata, features); const compiled = script.compile(asm); const decompiled = script.decompile(compiled); if (!decompiled) { throw new Error('Failed to decompile script??'); } return compiled; } getAsm(contractBytecode, contractSalt, challenge, maxPriority, calldata, features) { if (!this.contractSaltPubKey) throw new Error('Contract salt public key not set'); const dataChunks = this.splitBufferIntoChunks(contractBytecode); const calldataChunks = calldata ? this.splitBufferIntoChunks(calldata) : []; const featuresList = []; const featureData = []; if (features) { for (let i = 0; i < features.length; i++) { const feature = features[i]; featuresList.push(feature.opcode); const data = this.encodeFeature(feature); featureData.push(...data); } } const compiledData = [ this.getHeader(maxPriority, featuresList), opcodes.OP_TOALTSTACK, challenge.publicKey.originalPublicKeyBuffer(), opcodes.OP_TOALTSTACK, challenge.solution, opcodes.OP_TOALTSTACK, this.xSenderPubKey, opcodes.OP_DUP, opcodes.OP_HASH256, crypto.hash256(this.xSenderPubKey), opcodes.OP_EQUALVERIFY, opcodes.OP_CHECKSIGVERIFY, this.contractSaltPubKey, opcodes.OP_CHECKSIGVERIFY, opcodes.OP_HASH256, crypto.hash256(contractSalt), opcodes.OP_EQUALVERIFY, opcodes.OP_DEPTH, opcodes.OP_1, opcodes.OP_NUMEQUAL, opcodes.OP_IF, Generator.MAGIC, ...featureData, opcodes.OP_0, ...calldataChunks, opcodes.OP_1NEGATE, ...dataChunks, opcodes.OP_ELSE, opcodes.OP_1, opcodes.OP_ENDIF, ]; return compiledData.flat(); } }