UNPKG

@node-dlc/messaging

Version:
212 lines 8.72 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DigitDecompositionEventDescriptorV0 = exports.DigitDecompositionEventDescriptor = exports.EnumEventDescriptorV0 = exports.EnumEventDescriptor = exports.EventDescriptor = void 0; const bufio_1 = require("@node-dlc/bufio"); const MessageType_1 = require("../MessageType"); class EventDescriptor { static deserialize(buf) { const reader = new bufio_1.BufferReader(buf); const type = Number(reader.readBigSize()); switch (type) { case MessageType_1.MessageType.EnumEventDescriptor: return EnumEventDescriptor.deserialize(buf); case MessageType_1.MessageType.DigitDecompositionEventDescriptor: return DigitDecompositionEventDescriptor.deserialize(buf); default: throw new Error(`Payout function TLV type must be EnumEventDescriptorV0 or DigitDecompositionEventDescriptor`); } } /** * Creates an EventDescriptor from JSON data * @param json JSON object representing event descriptor */ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any static fromJSON(json) { if (!json) { throw new Error('eventDescriptor is required'); } // Check if it's an enum event or digit decomposition event if (json.enumEvent || json.enum_event) { return EnumEventDescriptor.fromJSON(json.enumEvent || json.enum_event); } else if (json.digitDecompositionEvent || json.digit_decomposition_event) { return DigitDecompositionEventDescriptor.fromJSON(json.digitDecompositionEvent || json.digit_decomposition_event); } else { throw new Error('eventDescriptor must have either enumEvent or digitDecompositionEvent'); } } } exports.EventDescriptor = EventDescriptor; /** * EnumEventDescriptor message contains the event outcomes for enumerated events. * Simplified class name (removed V0 suffix). */ class EnumEventDescriptor extends EventDescriptor { constructor() { super(...arguments); /** * The type for enum_event_descriptor_v0 message. enum_event_descriptor_v0 = 55302 */ this.type = EnumEventDescriptor.type; this.length = BigInt(0); // Required by EventDescriptor parent class this.outcomes = []; } /** * Creates an EnumEventDescriptor from JSON data * @param json JSON object representing an enum event descriptor */ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any static fromJSON(json) { const instance = new EnumEventDescriptor(); instance.outcomes = json.outcomes || []; return instance; } /** * Deserializes an enum_event_descriptor_v0 message * @param buf */ static deserialize(buf) { const instance = new EnumEventDescriptor(); const reader = new bufio_1.BufferReader(buf); reader.readBigSize(); // read type instance.length = reader.readBigSize(); // need to fix this reader.readUInt16BE(); // num_outcomes while (!reader.eof) { const outcomeLen = reader.readBigSize(); const outcomeBuf = reader.readBytes(Number(outcomeLen)); instance.outcomes.push(outcomeBuf.toString()); } return instance; } /** * Converts enum_event_descriptor to JSON */ toJSON() { return { enumEvent: { outcomes: this.outcomes, }, }; } /** * Serializes the enum_event_descriptor_v0 message into a Buffer */ serialize() { const writer = new bufio_1.BufferWriter(); writer.writeBigSize(this.type); const dataWriter = new bufio_1.BufferWriter(); dataWriter.writeUInt16BE(this.outcomes.length); for (const outcome of this.outcomes) { dataWriter.writeBigSize(outcome.length); dataWriter.writeBytes(Buffer.from(outcome)); } writer.writeBigSize(dataWriter.size); writer.writeBytes(dataWriter.toBuffer()); return writer.toBuffer(); } } exports.EnumEventDescriptor = EnumEventDescriptor; EnumEventDescriptor.type = MessageType_1.MessageType.EnumEventDescriptorV0; // Legacy support - keep V0 alias for backward compatibility exports.EnumEventDescriptorV0 = EnumEventDescriptor; /** * DigitDecompositionEventDescriptor is a simple enumeration of outcomes. * Simplified class name (removed V0 suffix). */ class DigitDecompositionEventDescriptor extends EventDescriptor { constructor() { super(...arguments); /** * The type for digit_decomposition_event_descriptor message. digit_decomposition_event_descriptor = 55306 */ this.type = DigitDecompositionEventDescriptor.type; } /** * Creates a DigitDecompositionEventDescriptor from JSON data * @param json JSON object representing digit decomposition event descriptor */ // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any static fromJSON(json) { const instance = new DigitDecompositionEventDescriptor(); instance.base = json.base || 10; instance.isSigned = json.isSigned || json.is_signed || false; instance.unit = json.unit || ''; instance.precision = json.precision || 0; instance.nbDigits = json.nbDigits || json.nb_digits || 0; return instance; } /** * Deserializes an digit_decomposition_event_descriptor message * @param buf */ static deserialize(buf) { const instance = new DigitDecompositionEventDescriptor(); const reader = new bufio_1.BufferReader(buf); reader.readBigSize(); // read type instance.length = reader.readBigSize(); // need to fix this /** * NOTE: BASE IS INCORRECT FORMAT FOR DLC SPEC (SHOULD BE BIGSIZE) * Will be fixed in oracle_announcement_v1 * https://github.com/discreetlogcontracts/dlcspecs/blob/master/Oracle.md#version-0-digit_decomposition_event_descriptor */ instance.base = reader.readUInt16BE(); instance.isSigned = reader.readUInt8() === 1; const unitLen = reader.readBigSize(); const unitBuf = reader.readBytes(Number(unitLen)); instance.unit = unitBuf.toString(); instance.precision = reader.readUInt32BE(); instance.nbDigits = reader.readUInt16BE(); return instance; } /** * Validates correctness of all fields in the message * https://github.com/discreetlogcontracts/dlcspecs/blob/master/Oracle.md * @throws Will throw an error if validation fails */ validate() { if (this.base <= 0) { throw new Error('base must be greater than 0'); } // TODO: support isSigned according to specifications if (this.isSigned) { throw new Error('node-dlc does not support isSigned'); } } /** * Converts digit_decomposition_event_descriptor to JSON (canonical rust-dlc format) */ toJSON() { return { digitDecompositionEvent: { base: this.base, isSigned: this.isSigned, unit: this.unit, precision: this.precision, nbDigits: this.nbDigits, }, }; } /** * Serializes the digit_decomposition_event_descriptor message into a Buffer */ serialize() { const writer = new bufio_1.BufferWriter(); writer.writeBigSize(this.type); const dataWriter = new bufio_1.BufferWriter(); dataWriter.writeUInt16BE(this.base); // Switch to BigSize in oracle_announcement_v1 dataWriter.writeUInt8(this.isSigned ? 1 : 0); dataWriter.writeBigSize(this.unit.length); dataWriter.writeBytes(Buffer.from(this.unit)); dataWriter.writeUInt32BE(this.precision); dataWriter.writeUInt16BE(this.nbDigits); writer.writeBigSize(dataWriter.size); writer.writeBytes(dataWriter.toBuffer()); return writer.toBuffer(); } } exports.DigitDecompositionEventDescriptor = DigitDecompositionEventDescriptor; DigitDecompositionEventDescriptor.type = MessageType_1.MessageType.DigitDecompositionEventDescriptor; // Legacy support - keep V0 alias for backward compatibility exports.DigitDecompositionEventDescriptorV0 = DigitDecompositionEventDescriptor; //# sourceMappingURL=EventDescriptor.js.map