UNPKG

@node-dlc/messaging

Version:
122 lines 4.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OrderAcceptContainer = exports.OrderAcceptV0 = exports.OrderAccept = void 0; const bufio_1 = require("@node-dlc/bufio"); const MessageType_1 = require("../MessageType"); const getTlv_1 = require("../serialize/getTlv"); const OrderNegotiationFields_1 = require("./OrderNegotiationFields"); class OrderAccept { static deserialize(buf) { const reader = new bufio_1.BufferReader(buf); const type = Number(reader.readUInt16BE()); switch (type) { case MessageType_1.MessageType.OrderAcceptV0: return OrderAcceptV0.deserialize(buf); default: throw new Error(`Order accept TLV type must be OrderAcceptV0`); } } } exports.OrderAccept = OrderAccept; /** * OrderAccept contains information about a node and indicates its * acceptance of the new order offer. This is the second step towards * order negotiation. */ class OrderAcceptV0 extends OrderAccept { constructor() { super(...arguments); /** * The type for order_accept_v0 message. order_accept_v0 = 62772 */ this.type = OrderAcceptV0.type; } /** * Deserializes an order_accept_v0 message * @param buf */ static deserialize(buf) { const instance = new OrderAcceptV0(); const reader = new bufio_1.BufferReader(buf); reader.readUInt16BE(); // read type instance.tempOrderId = reader.readBytes(32); instance.negotiationFields = OrderNegotiationFields_1.OrderNegotiationFields.deserialize((0, getTlv_1.getTlv)(reader)); return instance; } /** * Converts order_negotiation_fields_v0 to JSON */ toJSON() { return { type: this.type, tempOrderId: this.tempOrderId.toString('hex'), negotiationFields: this.negotiationFields.toJSON(), }; } /** * Serializes the order_accept_v0 message into a Buffer */ serialize() { const writer = new bufio_1.BufferWriter(); writer.writeUInt16BE(this.type); writer.writeBytes(this.tempOrderId); writer.writeBytes(this.negotiationFields.serialize()); return writer.toBuffer(); } } exports.OrderAcceptV0 = OrderAcceptV0; OrderAcceptV0.type = MessageType_1.MessageType.OrderAcceptV0; class OrderAcceptContainer { constructor() { this.accepts = []; } /** * Adds an OrderAccept to the container. * @param accept The OrderAccept to add. */ addAccept(accept) { this.accepts.push(accept); } /** * Returns all OrderAccepts in the container. * @returns An array of OrderAccept instances. */ getAccepts() { return this.accepts; } /** * Serializes all OrderAccepts in the container to a Buffer. * @returns A Buffer containing the serialized OrderAccepts. */ serialize() { const writer = new bufio_1.BufferWriter(); // Write the number of accepts in the container first. writer.writeBigSize(this.accepts.length); // Serialize each accept and write it. this.accepts.forEach((accept) => { const serializedAccept = accept.serialize(); writer.writeBigSize(serializedAccept.length); writer.writeBytes(serializedAccept); }); return writer.toBuffer(); } /** * Deserializes a Buffer into an OrderAcceptContainer with OrderAccepts. * @param buf The Buffer to deserialize. * @returns An OrderAcceptContainer instance. */ static deserialize(buf) { const reader = new bufio_1.BufferReader(buf); const container = new OrderAcceptContainer(); const acceptsCount = reader.readBigSize(); for (let i = 0; i < acceptsCount; i++) { const acceptLength = reader.readBigSize(); const acceptBuf = reader.readBytes(Number(acceptLength)); const accept = OrderAccept.deserialize(acceptBuf); // Adjust based on actual implementation. container.addAccept(accept); } return container; } } exports.OrderAcceptContainer = OrderAcceptContainer; //# sourceMappingURL=OrderAccept.js.map