All files / lib/messages ErrorMessage.ts

100% Statements 20/20
100% Branches 0/0
100% Functions 3/3
100% Lines 20/20

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 771x   1x                         1x           4x 4x   4x 4x   4x 3x   3x           6x                     6x               6x               2x         2x 2x 2x 2x 2x 2x      
import { BufferReader, BufferWriter } from '@node-dlc/bufio';
 
import { MessageType } from '../MessageType';
import { IWireMessage } from './IWireMessage';
 
/**
 * This message is defined in BOLT #1 and is used for telling
 * a peer that something is incorrect. The message can indicate
 * which channel is in error, or if channelId is 0, it refers
 * to all channels.
 *
 * These message can indicate protocol violations or internal
 * errors that make channels unusable or that make further
 * communication unusable.
 */
export class ErrorMessage implements IWireMessage {
  /**
   * Deserializes an error message into an ErrorMessage
   * instance.
   */
  public static deserialize(payload: Buffer): ErrorMessage {
    const reader = new BufferReader(payload);
    reader.readUInt16BE(); // read type
 
    const instance = new ErrorMessage();
    instance.channelId = reader.readUInt32BE();
 
    const len = reader.readUInt16BE();
    instance.data = reader.readBytes(len);
 
    return instance;
  }
 
  /**
   * Message type 17
   */
  public type: MessageType = MessageType.Error;
 
  /**
   * channelId is used to indicate the failing channel. It
   * can have a value of 0 to indicate there is an error with
   * all channels.
   *
   * All error messsagees sent before (and including) the
   * funding_created messagee should use the temporary_channel_id
   * instead of the channel_id.
   */
  public channelId: any = 0;
 
  /**
   * Data field may be empty. May contain the raw, hex-encoded
   * transaction in reply to a invalid signature check in
   * funding_created, funding_signed, closing_signed, or
   * commitment_signed messages.
   */
  public data: Buffer = Buffer.alloc(0);
 
  /**
   * Serialize the ErorrMessage into a Buffer that
   * can be send on the wire.
   */
  public serialize(): Buffer {
    const len =
      2 + // type
      4 + // channel_id
      2 + // len
      this.data.length;
 
    const writer = new BufferWriter(Buffer.alloc(len));
    writer.writeUInt16BE(this.type);
    writer.writeUInt32BE(this.channelId);
    writer.writeUInt16BE(this.data.length);
    writer.writeBytes(this.data);
    return writer.toBuffer();
  }
}