# All files / lib/messages NodeAnnouncementMessage.ts

90.57% Statements 48/53
50% Branches 1/2
71.43% Functions 5/7
92% Lines 46/50

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
    1461x
    1x
    1x

    1x
    1x
    1x

    1x
    1x

    1x
    1x
    1x

    1x

    1x
    1x

    1x
    1x
    1x
    1x
    1x

    1x

    1x
    1x

    1x

    1x

    2x

    2x

    1x
    1x

    1x
    1x
    1x

    1x

    1x

    1x

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

    1x

     import { BufferReader, BufferWriter } from '@node-dlc/bufio';
    import { BitField } from '@node-dlc/common';
    import * as crypto from '@node-dlc/crypto';
    import { NodeFeatureFlags } from '@node-dlc/wire';

    import { Address } from '../domain/Address';
    import { MessageType } from '../MessageType';
    import { getTlv } from '../serialize/getTlv';
    import { IWireMessage } from './IWireMessage';

    /**
     * This gossip message allows a node to indicate extra data associated with it,
     * in addition to its public key. To avoid trivial denial of service attacks,
     * nodes not associated with an already known channel are ignored.
     */
    export class NodeAnnouncementMessage implements IWireMessage {
      public static type = MessageType.NodeAnnouncement;

      public static deserialize(payload: Buffer): NodeAnnouncementMessage {
        const instance = new NodeAnnouncementMessage();
        const reader = new BufferReader(payload);
        reader.readUInt16BE(); // read off type

        instance.signature = reader.readBytes(64);

        const flen = reader.readUInt16BE();
        instance.features = BitField.fromBuffer(reader.readBytes(flen));

        instance.timestamp = reader.readUInt32BE();
        instance.nodeId = reader.readBytes(33);
        instance.rgbColor = reader.readBytes(3);
        instance.alias = reader.readBytes(32);
        instance.addresses = [];

        reader.readUInt16BE(); // addr_len

        while (!reader.eof) {
          const address = Address.deserialize(getTlv(reader));

          instance.addresses.push(address);
        }

        return instance;
      }

      /**
       * Message hashing is after the first 66 bytes of the message
       * and excludes the type and signature. It performs a double
       * sha-256 hash of the remaining bytes.
       */
      public static hash(msg: NodeAnnouncementMessage): Buffer {
        const bytes = msg.serialize().slice(66); // type + signature
        return crypto.hash256(bytes);
      }

      /**
       * Verifies the message signature
       */
      public static verifySignatures(msg: NodeAnnouncementMessage): boolean {
        const hash = NodeAnnouncementMessage.hash(msg);
        return crypto.verifySig(hash, msg.signature, msg.nodeId);
      }

      /**
       * Type 257
       */
      public type: MessageType = MessageType.NodeAnnouncement;

      /**
       * Signature of the announcement message by the node's public key
       * returned as a 64-byte Buffer.
       */
      public signature: Buffer;

      public features: BitField<NodeFeatureFlags>;

      public timestamp: number;

      /**
       * Compressed public key of the node that is a 33-byte
       * buffer.
       */
      public nodeId: Buffer;

      /**
       * Color of the node returned as a 3-byte Buffer.
       */
      public rgbColor: Buffer;

      /**
       * Alias of the node returned as a 32-byte Buffer.
       */
      public alias: Buffer;

      /**
       * Addresses that the node allow public network connections
       * on. The type indicates how the address is encoded. Addresses
       * are in order of connectivity preference. Currently
       * supported addresses formats are IPv4, IPv6, Tor2 and Tor3
       */
      public addresses: Address[] = [];

      public serialize(): Buffer {
        const featuresBuffer = this.features.toBuffer();
        const featuresLen = featuresBuffer.length;

        // serialize addresses into buffers so we can obtain the length
        const addressBuffers = [];
        for (const address of this.addresses) {
          addressBuffers.push(address.serialize());
        }

        // obtain total address length
        // eslint-disable-next-line
        const addressBytes: number = addressBuffers.map(b => b.length).reduce((sum, val) => sum + val, 0); // prettier-ignore

        const len =
          2 + // type
          64 + // signature
          2 + // flen
          featuresLen + // features length
          4 + // timestamp
          33 + // node_id
          3 + // rgb_color
          32 + // alias
          2 + // addresses
          addressBytes; // cumulative addr bytes
        const writer = new BufferWriter(Buffer.alloc(len));

        writer.writeUInt16BE(this.type);
        writer.writeBytes(this.signature);
        writer.writeUInt16BE(featuresLen);
        Iif (featuresLen > 0) writer.writeBytes(featuresBuffer);
        writer.writeUInt32BE(this.timestamp);
        writer.writeBytes(this.nodeId);
        writer.writeBytes(this.rgbColor);
        writer.writeBytes(this.alias);
        writer.writeUInt16BE(addressBytes);
        for (const addressBuffer of addressBuffers) {
          writer.writeBytes(addressBuffer);
        }

        return writer.toBuffer();
      }
    }