UNPKG

@radixdlt/tx-parser

Version:
142 lines 6.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Instruction = void 0; const neverthrow_1 = require("neverthrow"); const _types_1 = require("./_types"); const bytes_1 = require("./bytes"); const txSignature_1 = require("./txSignature"); const substate_1 = require("./substate"); const substateId_1 = require("./substateId"); const parseFromBufferReader = (bufferReader) => { const parseSubstate = () => substate_1.Substate.parseFromBufferReader(bufferReader); return bufferReader .readNextBuffer(1) .map(b => ({ insBuf: b, instructionType: b.readUInt8(0), })) .andThen((ii) => { const { insBuf, instructionType } = ii; switch (instructionType) { case _types_1.InstructionType.END: return (0, neverthrow_1.ok)({ instructionType, toBuffer: () => insBuf, toString: () => 'END', }); case _types_1.InstructionType.UP: return parseSubstate().map((substate) => ({ instructionType, substate, toBuffer: () => Buffer.concat([ insBuf, substate.toBuffer(), ]), toString: () => `UP(${substate.toString().trimStart()})`, toHumanReadableString: () => { if (!substate.toHumanReadableString) { return `UP(${substate .toString() .trimStart()})`; } return `UP(${substate .toHumanReadableString() .trimStart()})`; }, })); case _types_1.InstructionType.DOWN: case _types_1.InstructionType.READ: return substateId_1.SubstateId.parseFromBufferReader(bufferReader).map((substateId) => ({ instructionType, substateId, toBuffer: () => Buffer.concat([insBuf, substateId.toBuffer()]), toString: () => `${_types_1.InstructionType[instructionType]}(${substateId.toString()})`, })); case _types_1.InstructionType.LDOWN: return bufferReader.readNextBuffer(2).map((substateIndexBytes) => { const substateIndex = substateIndexBytes.readUInt16BE(0); return { substateIndex, instructionType, toBuffer: () => Buffer.concat([ insBuf, substateIndexBytes, ]), toString: () => `LDOWN(${substateIndex.toString()})`, }; }); case _types_1.InstructionType.MSG: return bytes_1.Bytes.fromBufferReader(bufferReader).map((bytes) => ({ instructionType, bytes, toBuffer: () => Buffer.concat([insBuf, bytes.toBuffer()]), toString: () => `MSG(${bytes.toString()})`, })); case _types_1.InstructionType.SIG: return txSignature_1.TxSignature.fromBufferReader(bufferReader).map((signature) => ({ instructionType, signature, toBuffer: () => Buffer.concat([ insBuf, signature.toBuffer(), ]), toString: () => `SIG(0x${signature .toBuffer() .toString('hex')})`, })); case _types_1.InstructionType.SYSCALL: return bytes_1.Bytes.fromBufferReader(bufferReader).map((callData) => ({ instructionType, callData, toBuffer: () => Buffer.concat([ insBuf, callData.toBuffer(), ]), toString: () => `SYSCALL(${callData.toString()})`, })); case _types_1.InstructionType.HEADER: return (0, neverthrow_1.combine)([ bufferReader.readNextBuffer(1), bufferReader.readNextBuffer(1), ]) .map(resList => { const versionBuf = resList[0]; const flagBuf = resList[1]; const version = versionBuf.readUInt8(0); const flag = flagBuf.readUInt8(0); const buffer = Buffer.concat([ versionBuf, flagBuf, ]); return { version, flag, buffer }; }) .map((partial) => (Object.assign(Object.assign({}, partial), { instructionType, toBuffer: () => Buffer.concat([insBuf, partial.buffer]), toString: () => `HEADER(${partial.version.toString()}, ${partial.flag.toString()})` }))); case _types_1.InstructionType.VDOWN: return bytes_1.Bytes.fromBufferReader(bufferReader).map((callData) => ({ instructionType, callData, toBuffer: () => Buffer.concat([ insBuf, callData.toBuffer(), ]), toString: () => `VDOWN(${callData.toString()})`, })); case _types_1.InstructionType.VREAD: return bytes_1.Bytes.fromBufferReader(bufferReader).map((callData) => ({ instructionType, callData, toBuffer: () => Buffer.concat([ insBuf, callData.toBuffer(), ]), toString: () => `VREAD(${callData.toString()})`, })); default: return (0, neverthrow_1.err)(new Error(`Unrecognized instruction type (${instructionType.toString()}): ${insBuf.toString()}`)); } }); }; exports.Instruction = { parseFromBufferReader, }; //# sourceMappingURL=instruction.js.map