Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | 1x 1x 1x 1x 1x 2x 2x 2x 2x 1x 1x 4x 4x 4x 4x 4x 4x 6x 8x 8x 8x 8x 8x 1x 2x 4x 3x 3x 3x 6x 6x 6x 3x 1x 1x 1x 1x 2x 2x 2x 2x 1x | import { BufferReader, BufferWriter } from '@node-dlc/bufio';
import { MessageType } from '../MessageType';
import { getTlv } from '../serialize/getTlv';
import { IDlcMessage } from './DlcMessage';
import {
IOrderNegotiationFieldsV0JSON,
IOrderNegotiationFieldsV1JSON,
OrderNegotiationFields,
} from './OrderNegotiationFields';
export abstract class OrderAccept {
public static deserialize(buf: Buffer): OrderAccept {
const reader = new BufferReader(buf);
const type = Number(reader.readUInt16BE());
switch (type) {
case MessageType.OrderAcceptV0:
return OrderAcceptV0.deserialize(buf);
default:
throw new Error(`Order accept TLV type must be OrderAcceptV0`);
}
}
public abstract type: number;
public abstract toJSON(): IOrderAcceptV0JSON;
public abstract serialize(): Buffer;
}
/**
* OrderAccept contains information about a node and indicates its
* acceptance of the new order offer. This is the second step towards
* order negotiation.
*/
export class OrderAcceptV0 extends OrderAccept implements IDlcMessage {
public static type = MessageType.OrderAcceptV0;
/**
* Deserializes an order_accept_v0 message
* @param buf
*/
public static deserialize(buf: Buffer): OrderAcceptV0 {
const instance = new OrderAcceptV0();
const reader = new BufferReader(buf);
reader.readUInt16BE(); // read type
instance.tempOrderId = reader.readBytes(32);
instance.negotiationFields = OrderNegotiationFields.deserialize(
getTlv(reader),
);
return instance;
}
/**
* The type for order_accept_v0 message. order_accept_v0 = 62772
*/
public type = OrderAcceptV0.type;
public tempOrderId: Buffer;
public negotiationFields: OrderNegotiationFields;
/**
* Converts order_negotiation_fields_v0 to JSON
*/
public toJSON(): IOrderAcceptV0JSON {
return {
type: this.type,
tempOrderId: this.tempOrderId.toString('hex'),
negotiationFields: this.negotiationFields.toJSON(),
};
}
/**
* Serializes the order_accept_v0 message into a Buffer
*/
public serialize(): Buffer {
const writer = new BufferWriter();
writer.writeUInt16BE(this.type);
writer.writeBytes(this.tempOrderId);
writer.writeBytes(this.negotiationFields.serialize());
return writer.toBuffer();
}
}
export interface IOrderAcceptV0JSON {
type: number;
tempOrderId: string;
negotiationFields:
| IOrderNegotiationFieldsV0JSON
| IOrderNegotiationFieldsV1JSON;
}
export class OrderAcceptContainer {
private accepts: OrderAccept[] = [];
/**
* Adds an OrderAccept to the container.
* @param accept The OrderAccept to add.
*/
public addAccept(accept: OrderAccept): void {
this.accepts.push(accept);
}
/**
* Returns all OrderAccepts in the container.
* @returns An array of OrderAccept instances.
*/
public getAccepts(): OrderAccept[] {
return this.accepts;
}
/**
* Serializes all OrderAccepts in the container to a Buffer.
* @returns A Buffer containing the serialized OrderAccepts.
*/
public serialize(): Buffer {
const writer = new 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.
*/
public static deserialize(buf: Buffer): OrderAcceptContainer {
const reader = new 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;
}
}
|