All files / lib/messages OrderIrcInfo.ts

83.33% Statements 25/30
0% Branches 0/2
80% Functions 4/5
83.33% Lines 25/30

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 1011x   1x     1x                                                   1x 1x             1x 1x   1x 1x   1x 1x 1x   1x   1x           2x                       2x                     1x 1x   1x 1x 1x 1x   1x 1x   1x                  
import { BufferReader, BufferWriter } from '@node-dlc/bufio';
 
import { MessageType } from '../MessageType';
import { IDlcMessage } from './DlcMessage';
 
export abstract class OrderIrcInfo {
  public static deserialize(buf: Buffer): OrderIrcInfo {
    const reader = new BufferReader(buf);
 
    const type = Number(reader.readBigSize());
 
    switch (type) {
      case MessageType.OrderIrcInfoV0:
        return OrderIrcInfoV0.deserialize(buf);
      default:
        throw new Error(`Order irc info TLV type must be OrderIrcInfoV0`);
    }
  }
 
  public abstract type: number;
 
  public abstract toJSON(): IOrderIrcInfoJSON;
 
  public abstract serialize(): Buffer;
}
 
/**
 * OrderMetadata message contains information about a node and indicates its
 * desire to enter into a new contract. This is the first step toward
 * order negotiation.
 */
export class OrderIrcInfoV0 extends OrderIrcInfo implements IDlcMessage {
  public static type = MessageType.OrderIrcInfoV0;
 
  /**
   * Deserializes an offer_dlc_v0 message
   * @param buf
   */
  public static deserialize(buf: Buffer): OrderIrcInfoV0 {
    const instance = new OrderIrcInfoV0();
    const reader = new BufferReader(buf);
 
    reader.readBigSize(); // read type
    instance.length = reader.readBigSize();
 
    const nickLength = reader.readBigSize();
    const nickBuf = reader.readBytes(Number(nickLength));
    instance.nick = nickBuf.toString();
 
    instance.pubKey = reader.readBytes(33);
 
    return instance;
  }
 
  /**
   * The type for order_metadata_v0 message. order_metadata_v0 = 62774
   */
  public type = OrderIrcInfoV0.type;
 
  public length: bigint;
 
  public nick: string;
 
  public pubKey: Buffer;
 
  /**
   * Converts order_metadata_v0 to JSON
   */
  public toJSON(): IOrderIrcInfoJSON {
    return {
      type: this.type,
      nick: this.nick,
      pubKey: this.pubKey.toString('hex'),
    };
  }
 
  /**
   * Serializes the oracle_event message into a Buffer
   */
  public serialize(): Buffer {
    const writer = new BufferWriter();
    writer.writeBigSize(this.type);
 
    const dataWriter = new BufferWriter();
    dataWriter.writeBigSize(this.nick.length);
    dataWriter.writeBytes(Buffer.from(this.nick));
    dataWriter.writeBytes(this.pubKey);
 
    writer.writeBigSize(dataWriter.size);
    writer.writeBytes(dataWriter.toBuffer());
 
    return writer.toBuffer();
  }
}
 
export interface IOrderIrcInfoJSON {
  type: number;
  nick: string;
  pubKey: string;
}