UNPKG

@node-dlc/messaging

Version:
150 lines 5.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContractDescriptorV1 = exports.ContractDescriptorV0 = exports.ContractDescriptor = void 0; const bufio_1 = require("@node-dlc/bufio"); const MessageType_1 = require("../MessageType"); const getTlv_1 = require("../serialize/getTlv"); const PayoutFunction_1 = require("./PayoutFunction"); const RoundingIntervalsV0_1 = require("./RoundingIntervalsV0"); class ContractDescriptor { static deserialize(buf) { const reader = new bufio_1.BufferReader(buf); const type = Number(reader.readBigSize()); switch (type) { case MessageType_1.MessageType.ContractDescriptorV0: return ContractDescriptorV0.deserialize(buf); case MessageType_1.MessageType.ContractDescriptorV1: return ContractDescriptorV1.deserialize(buf); default: throw new Error(`Contract Descriptor TLV type must be ContractDescriptorV0 or ContractDescriptorV1`); } } } exports.ContractDescriptor = ContractDescriptor; /** * ContractDescriptor V0 contains information about a contract's outcomes * and their corresponding payouts. */ class ContractDescriptorV0 extends ContractDescriptor { constructor() { super(...arguments); /** * The type for contract_descriptor_v0 message. contract_descriptor_v0 = 42768 */ this.type = ContractDescriptorV0.type; this.outcomes = []; } /** * Deserializes an contract_descriptor_v0 message * @param buf */ static deserialize(buf) { const instance = new ContractDescriptorV0(); const reader = new bufio_1.BufferReader(buf); reader.readBigSize(); // read type instance.length = reader.readBigSize(); reader.readBigSize(); // num_outcomes while (!reader.eof) { instance.outcomes.push({ outcome: reader.readBytes(32), localPayout: reader.readUInt64BE(), }); } return instance; } /** * Converts contract_descriptor_v0 to JSON */ toJSON() { return { type: this.type, outcomes: this.outcomes.map((outcome) => { return { outcome: outcome.outcome.toString('hex'), localPayout: Number(outcome.localPayout), }; }), }; } /** * Serializes the contract_descriptor_v0 message into a Buffer */ serialize() { const writer = new bufio_1.BufferWriter(); writer.writeBigSize(this.type); const dataWriter = new bufio_1.BufferWriter(); dataWriter.writeBigSize(this.outcomes.length); for (const outcome of this.outcomes) { dataWriter.writeBytes(outcome.outcome); dataWriter.writeUInt64BE(outcome.localPayout); } writer.writeBigSize(dataWriter.size); writer.writeBytes(dataWriter.toBuffer()); return writer.toBuffer(); } } exports.ContractDescriptorV0 = ContractDescriptorV0; ContractDescriptorV0.type = MessageType_1.MessageType.ContractDescriptorV0; /** * ContractDescriptor V1 contains information about a contract's outcomes * and their corresponding payouts. */ class ContractDescriptorV1 extends ContractDescriptor { constructor() { super(...arguments); /** * The type for contract_descriptor_v1 message. contract_descriptor_v1 = 42784 */ this.type = ContractDescriptorV1.type; } /** * Deserializes an contract_descriptor_v1 message * @param buf */ static deserialize(buf) { const instance = new ContractDescriptorV1(); const reader = new bufio_1.BufferReader(buf); reader.readBigSize(); // read type instance.length = reader.readBigSize(); instance.numDigits = reader.readUInt16BE(); // num_digits instance.payoutFunction = PayoutFunction_1.PayoutFunction.deserialize((0, getTlv_1.getTlv)(reader)); instance.roundingIntervals = RoundingIntervalsV0_1.RoundingIntervalsV0.deserialize((0, getTlv_1.getTlv)(reader)); return instance; } /** * Validates correctness of all fields in the message * https://github.com/discreetlogcontracts/dlcspecs/blob/master/Messaging.md#the-contract_descriptor-type * @throws Will throw an error if validation fails */ validate() { this.roundingIntervals.validate(); } /** * Converts contract_descriptor_v1 to JSON */ toJSON() { return { type: this.type, numDigits: this.numDigits, payoutFunction: this.payoutFunction.toJSON(), roundingIntervals: this.roundingIntervals.toJSON(), }; } /** * Serializes the contract_descriptor_v1 message into a Buffer */ serialize() { const writer = new bufio_1.BufferWriter(); writer.writeBigSize(this.type); const dataWriter = new bufio_1.BufferWriter(); dataWriter.writeUInt16BE(this.numDigits); dataWriter.writeBytes(this.payoutFunction.serialize()); dataWriter.writeBytes(this.roundingIntervals.serialize()); writer.writeBigSize(dataWriter.size); writer.writeBytes(dataWriter.toBuffer()); return writer.toBuffer(); } } exports.ContractDescriptorV1 = ContractDescriptorV1; ContractDescriptorV1.type = MessageType_1.MessageType.ContractDescriptorV1; //# sourceMappingURL=ContractDescriptor.js.map