All files / lib/messages OrderNegotiationFields.ts

92.68% Statements 38/41
66.67% Branches 2/3
77.78% Functions 7/9
92.68% Lines 38/41

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 154 155 156 157 1581x   1x   1x   1x       6x   6x   6x   5x   1x                                             1x     1x             7x 7x   7x 7x   7x           8x                                 10x 10x 10x   10x               1x     1x             1x 1x   1x 1x 1x 1x   1x           2x                                       1x 1x   1x 1x   1x 1x   1x                        
import { BufferReader, BufferWriter } from '@node-dlc/bufio';
 
import { MessageType } from '../MessageType';
import { IDlcMessage } from './DlcMessage';
import { IOrderOfferJSON, OrderOffer } from './OrderOffer';
 
export abstract class OrderNegotiationFields {
  public static deserialize(
    buf: Buffer,
  ): OrderNegotiationFieldsV0 | OrderNegotiationFieldsV1 {
    const reader = new BufferReader(buf);
 
    const type = Number(reader.readBigSize());
 
    switch (type) {
      case MessageType.OrderNegotiationFieldsV0:
        return OrderNegotiationFieldsV0.deserialize(buf);
      case MessageType.OrderNegotiationFieldsV1:
        return OrderNegotiationFieldsV1.deserialize(buf);
      default:
        throw new Error(
          `Order Negotiation Fields TLV type must be OrderNegotiationFieldsV0 or OrderNegotiationFieldsV1`,
        );
    }
  }
 
  public abstract type: number;
 
  public abstract length: bigint;
 
  public abstract toJSON():
    | IOrderNegotiationFieldsV0JSON
    | IOrderNegotiationFieldsV1JSON;
 
  public abstract serialize(): Buffer;
}
 
/**
 * OrderNegotiationFields V0 contains preferences of the accepter of a order
 * offer which are taken into account during DLC construction.
 */
export class OrderNegotiationFieldsV0
  extends OrderNegotiationFields
  implements IDlcMessage {
  public static type = MessageType.OrderNegotiationFieldsV0;
 
  /**
   * Deserializes an order_negotiation_fields_v0 message
   * @param buf
   */
  public static deserialize(buf: Buffer): OrderNegotiationFieldsV0 {
    const instance = new OrderNegotiationFieldsV0();
    const reader = new BufferReader(buf);
 
    reader.readBigSize(); // read type
    instance.length = reader.readBigSize();
 
    return instance;
  }
 
  /**
   * The type for order_negotiation_fields_v0 message. order_negotiation_fields_v0 = 65334
   */
  public type = OrderNegotiationFieldsV0.type;
 
  public length: bigint;
 
  /**
   * Converts order_negotiation_fields_v0 to JSON
   */
  public toJSON(): IOrderNegotiationFieldsV0JSON {
    return {
      type: this.type,
    };
  }
 
  /**
   * Serializes the order_negotiation_fields_v0 message into a Buffer
   */
  public serialize(): Buffer {
    const writer = new BufferWriter();
    writer.writeBigSize(this.type);
    writer.writeBigSize(0);
 
    return writer.toBuffer();
  }
}
 
/**
 * OrderNegotiationFields V1 contains preferences of the acceptor of a order
 * offer which are taken into account during DLC construction.
 */
export class OrderNegotiationFieldsV1
  extends OrderNegotiationFields
  implements IDlcMessage {
  public static type = MessageType.OrderNegotiationFieldsV1;
 
  /**
   * Deserializes an order_negotiation_fields_v1 message
   * @param buf
   */
  public static deserialize(buf: Buffer): OrderNegotiationFieldsV1 {
    const instance = new OrderNegotiationFieldsV1();
    const reader = new BufferReader(buf);
 
    reader.readBigSize(); // read type
    instance.length = reader.readBigSize();
    const newBuf = reader.readBytes();
    instance.orderOffer = OrderOffer.deserialize(newBuf);
 
    return instance;
  }
 
  /**
   * The type for order_negotiation_fields_v1 message. order_negotiation_fields_v1 = 65336
   */
  public type = OrderNegotiationFieldsV1.type;
 
  public length: bigint;
 
  public orderOffer: OrderOffer;
 
  /**
   * Converts order_negotiation_fields_v1 to JSON
   */
  public toJSON(): IOrderNegotiationFieldsV1JSON {
    return {
      type: this.type,
      orderOffer: this.orderOffer.toJSON(),
    };
  }
 
  /**
   * Serializes the order_negotiation_fields_v1 message into a Buffer
   */
  public serialize(): Buffer {
    const writer = new BufferWriter();
    writer.writeBigSize(this.type);
 
    const dataWriter = new BufferWriter();
    dataWriter.writeBytes(this.orderOffer.serialize());
 
    writer.writeBigSize(dataWriter.size);
    writer.writeBytes(dataWriter.toBuffer());
 
    return writer.toBuffer();
  }
}
 
export interface IOrderNegotiationFieldsV0JSON {
  type: number;
}
 
export interface IOrderNegotiationFieldsV1JSON {
  type: number;
  orderOffer: IOrderOfferJSON;
}