meta-contract-debug
Version:
Meta Contract SDK
116 lines (115 loc) • 5.16 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createPrevGenesisTxOutputProof = exports.createGenesisTxInputProof = exports.createTxOutputProof = exports.createTxInputProof = exports.getTxidInfo = exports.getUInt64Buf = exports.getUInt32Buf = exports.getUInt16Buf = exports.getUInt8Buf = void 0;
const scryptlib_1 = require("../scryptlib");
const mvc_1 = require("../mvc");
const __1 = require("..");
const jsonDescr = require('../mcp01/contract-desc/txUtil_desc.json');
const { TxInputProof, TxOutputProof } = (0, scryptlib_1.buildTypeClasses)(jsonDescr);
let getUInt8Buf = function (amount) {
const buf = Buffer.alloc(1, 0);
buf.writeUInt8(amount);
return buf;
};
exports.getUInt8Buf = getUInt8Buf;
let getUInt16Buf = function (amount) {
const buf = Buffer.alloc(2, 0);
buf.writeUInt16LE(amount);
return buf;
};
exports.getUInt16Buf = getUInt16Buf;
let getUInt32Buf = function (index) {
const buf = Buffer.alloc(4, 0);
buf.writeUInt32LE(index);
return buf;
};
exports.getUInt32Buf = getUInt32Buf;
let getUInt64Buf = function (amount) {
return new __1.BN(amount.toString()).toBuffer({ endian: 'little', size: 8 });
};
exports.getUInt64Buf = getUInt64Buf;
function getTxidInfo(tx) {
const writer = new mvc_1.encoding.BufferWriter();
writer.writeUInt32LE(tx.version);
writer.writeUInt32LE(tx.nLockTime);
writer.writeUInt32LE(tx.inputs.length);
writer.writeUInt32LE(tx.outputs.length);
const inputWriter = new mvc_1.encoding.BufferWriter();
const inputWriter2 = new mvc_1.encoding.BufferWriter();
for (const input of tx.inputs) {
inputWriter.writeReverse(input.prevTxId);
inputWriter.writeUInt32LE(input.outputIndex);
inputWriter.writeUInt32LE(input.sequenceNumber);
inputWriter2.write(mvc_1.crypto.Hash.sha256(input.script.toBuffer()));
}
const inputHashProof = inputWriter.toBuffer();
writer.write(mvc_1.crypto.Hash.sha256(inputHashProof));
writer.write(mvc_1.crypto.Hash.sha256(inputWriter2.toBuffer()));
const outputWriter = new mvc_1.encoding.BufferWriter();
for (const output of tx.outputs) {
outputWriter.writeUInt64LEBN(output.satoshisBN);
outputWriter.write(mvc_1.crypto.Hash.sha256(output.script.toBuffer()));
}
const outputHashProof = outputWriter.toBuffer();
writer.write(mvc_1.crypto.Hash.sha256(outputHashProof));
const txHeader = writer.toBuffer().toString('hex');
return {
txHeader,
inputHashProof: inputHashProof.toString('hex'),
outputHashProof: outputHashProof.toString('hex'),
};
}
exports.getTxidInfo = getTxidInfo;
function createTxInputProof(tx, inputIndex) {
const info = getTxidInfo(tx);
const txHeader = new scryptlib_1.Bytes(info.txHeader);
const input = tx.inputs[inputIndex];
const res = {
hashProof: new scryptlib_1.Bytes(info.inputHashProof),
txHash: new scryptlib_1.Bytes(Buffer.from(input.prevTxId, 'hex')
.reverse()
.toString('hex')),
outputIndexBytes: new scryptlib_1.Bytes((0, exports.getUInt32Buf)(input.outputIndex).toString('hex')),
sequenceBytes: new scryptlib_1.Bytes((0, exports.getUInt32Buf)(input.sequenceNumber).toString('hex')),
};
return [res, txHeader];
}
exports.createTxInputProof = createTxInputProof;
function createTxOutputProof(tx, outputIndex) {
const info = getTxidInfo(tx);
const output = tx.outputs[outputIndex];
// console.log({
// sats: output.satoshis,
// satsInBytes: new Bytes(getUInt64Buf(output.satoshis).toString('hex')).toString('hex'),
// outputIndex,
// txId: tx.id,
// })
const res = {
txHeader: new scryptlib_1.Bytes(info.txHeader),
hashProof: new scryptlib_1.Bytes(info.outputHashProof),
satoshiBytes: new scryptlib_1.Bytes((0, exports.getUInt64Buf)(output.satoshis).toString('hex')),
scriptHash: new scryptlib_1.Bytes(mvc_1.crypto.Hash.sha256(output.script.toBuffer()).toString('hex')),
};
return res;
}
exports.createTxOutputProof = createTxOutputProof;
function createGenesisTxInputProof(genesisUtxo) {
const genesisTx = new mvc_1.Transaction(genesisUtxo.satotxInfo.txHex);
const prevInputIndex = 0;
const inputRes = createTxInputProof(genesisTx, prevInputIndex);
const genesisTxInputProof = new TxInputProof(inputRes[0]);
const genesisTxHeader = inputRes[1]; // TODO:
return { genesisTxHeader, prevInputIndex, genesisTxInputProof };
}
exports.createGenesisTxInputProof = createGenesisTxInputProof;
function createPrevGenesisTxOutputProof(genesisUtxo) {
const preGenesisOutputIndex = genesisUtxo.satotxInfo.preOutputIndex;
const preGenesisTx = new mvc_1.Transaction(genesisUtxo.satotxInfo.preTxHex);
const prevOutputProof = createTxOutputProof(preGenesisTx, preGenesisOutputIndex);
return {
prevGenesisTxHeader: prevOutputProof.txHeader,
prevTxOutputHashProof: prevOutputProof.hashProof,
prevTxOutputSatoshiBytes: prevOutputProof.satoshiBytes,
};
}
exports.createPrevGenesisTxOutputProof = createPrevGenesisTxOutputProof;