All files / lib/messages DlcCancel.ts

100% Statements 22/22
100% Branches 2/2
100% Functions 4/4
100% Lines 22/22

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 581x   1x             1x 1x             1x 1x   1x 1x 1x   1x           2x       2x           1x 1x 1x 1x   1x       1x 1x 1x 1x       1x  
import { BufferReader, BufferWriter } from '@node-dlc/bufio';
 
import { MessageType } from '../MessageType';
import { IDlcMessage } from './DlcMessage';
 
/**
 * DlcCancel message contains information about a node's desire to cancel
 * a DLC contract negotiation.
 */
export class DlcCancel implements IDlcMessage {
  public static type = MessageType.DlcCancel;
 
  /**
   * Deserializes a dlc_cancel message
   * @param buf
   */
  public static deserialize(buf: Buffer): DlcCancel {
    const instance = new DlcCancel();
    const reader = new BufferReader(buf);
 
    reader.readUInt16BE(); // read type
    instance.contractId = reader.readBytes(32);
    instance.cancelType = reader.readUInt8();
 
    return instance;
  }
 
  /**
   * The type for dlc_cancel message. dlc_cancel = 52172
   */
  public type = DlcCancel.type;
 
  public contractId: Buffer;
 
  public cancelType: CancelType = 0;
 
  /**
   * Serializes the dlc_cancel message into a Buffer
   */
  public serialize(): Buffer {
    const writer = new BufferWriter();
    writer.writeUInt16BE(this.type);
    writer.writeBytes(this.contractId);
    writer.writeUInt8(this.cancelType);
 
    return writer.toBuffer();
  }
}
 
export enum CancelType {
  Unknown = 0,
  Market = 1,
  Error = 2,
}
 
// Backward compatibility alias
export const DlcCancelV0 = DlcCancel;