# All files / lib/messages DlcMessage.ts

45.16% Statements 14/31
0% Branches 0/14
0% Functions 0/1
45.16% Lines 14/31

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
    881x

    1x
    1x

    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x

     import { BufferReader } from '@node-dlc/bufio';

    import { MessageType } from '../MessageType';
    import {
      EnumeratedDescriptor,
      NumericalDescriptor,
    } from './ContractDescriptor';
    import {
      DisjointContractInfo,
      IContractInfoV0JSON,
      IContractInfoV1JSON,
      SingleContractInfo,
    } from './ContractInfo';
    import { DlcAccept } from './DlcAccept';
    import { DlcClose } from './DlcClose';
    import { DlcOffer } from './DlcOffer';
    import { DlcSign } from './DlcSign';
    import { NodeAnnouncementMessage } from './NodeAnnouncementMessage';
    import { OracleAnnouncement } from './OracleAnnouncement';
    import { OracleAttestation } from './OracleAttestation';
    import { OrderAccept } from './OrderAccept';
    import { OrderOffer } from './OrderOffer';

    export interface IDlcMessage {
      type: number;
      serialize(): Buffer;
    }

    export type ContractInfoV0JSON = IContractInfoV0JSON;
    export type ContractInfoV1JSON = IContractInfoV1JSON;

    export abstract class DlcMessage {
      public static deserialize(
        buf: Buffer,
      ):
        | EnumeratedDescriptor
        | NumericalDescriptor
        | SingleContractInfo
        | DisjointContractInfo
        | OrderOffer
        | OrderAccept
        | DlcOffer
        | DlcAccept
        | DlcSign
        | DlcClose
        | OracleAttestation
        | OracleAnnouncement
        | NodeAnnouncementMessage {
        const reader = new BufferReader(buf);
        const type = reader.readUInt16BE();

        switch (type) {
          case MessageType.ContractDescriptorV0:
            return EnumeratedDescriptor.deserialize(buf);
          case MessageType.ContractDescriptorV1:
            return NumericalDescriptor.deserialize(buf);
          case MessageType.SingleContractInfo:
            return SingleContractInfo.deserialize(buf);
          case MessageType.DisjointContractInfo:
            return DisjointContractInfo.deserialize(buf);
          case MessageType.OrderOffer:
            return OrderOffer.deserialize(buf);
          case MessageType.OrderAccept:
            return OrderAccept.deserialize(buf);
          case MessageType.DlcOffer:
            return DlcOffer.deserialize(buf);
          case MessageType.DlcAccept:
            return DlcAccept.deserialize(buf);
          case MessageType.DlcSign:
            return DlcSign.deserialize(buf);
          case MessageType.DlcClose:
            return DlcClose.deserialize(buf);
          case MessageType.OracleAttestation:
            return OracleAttestation.deserialize(buf);
          case MessageType.OracleAnnouncement:
            return OracleAnnouncement.deserialize(buf);
          case MessageType.NodeAnnouncement:
            return NodeAnnouncementMessage.deserialize(buf);
          default:
            throw new Error(`DlcMessage type ${type} not supported`);
        }
      }

      public abstract type: number;

      public abstract serialize(): Buffer;
    }