All files / lib/messages EventDescriptor.ts

96.51% Statements 83/86
67.57% Branches 25/37
100% Functions 13/13
96.51% Lines 83/86

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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 2951x   1x     1x       258x   258x   258x   116x   142x                           163x         163x 76x 87x 87x                                                 1x     1x               76x 76x 76x               121x 121x   121x 121x 121x   121x 430x 430x 430x     121x           246x   246x   246x           42x                     327x 327x   327x 327x   327x 1207x 1207x     327x 327x   327x         1x             1x     1x               87x 87x 87x 87x 87x 87x 87x               144x 144x   144x 144x             144x 144x 144x 144x 144x 144x 144x   144x           238x                                       7x 2x       5x 1x               44x                             371x 371x   371x 371x 371x 371x 371x 371x 371x   371x 371x   371x         1x                                                  
import { BufferReader, BufferWriter } from '@node-dlc/bufio';
 
import { MessageType } from '../MessageType';
import { IDlcMessage } from './DlcMessage';
 
export abstract class EventDescriptor {
  public static deserialize(
    buf: Buffer,
  ): EnumEventDescriptor | DigitDecompositionEventDescriptor {
    const reader = new BufferReader(buf);
 
    const type = Number(reader.readBigSize());
 
    switch (type) {
      case MessageType.EnumEventDescriptor:
        return EnumEventDescriptor.deserialize(buf);
      case MessageType.DigitDecompositionEventDescriptor:
        return DigitDecompositionEventDescriptor.deserialize(buf);
      default:
        throw new Error(
          `Payout function TLV type must be EnumEventDescriptorV0 or DigitDecompositionEventDescriptor`,
        );
    }
  }
 
  /**
   * Creates an EventDescriptor from JSON data
   * @param json JSON object representing event descriptor
   */
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
  public static fromJSON(json: any): EventDescriptor {
    Iif (!json) {
      throw new Error('eventDescriptor is required');
    }
 
    // Check if it's an enum event or digit decomposition event
    if (json.enumEvent || json.enum_event) {
      return EnumEventDescriptor.fromJSON(json.enumEvent || json.enum_event);
    } else Eif (json.digitDecompositionEvent || json.digit_decomposition_event) {
      return DigitDecompositionEventDescriptor.fromJSON(
        json.digitDecompositionEvent || json.digit_decomposition_event,
      );
    } else {
      throw new Error(
        'eventDescriptor must have either enumEvent or digitDecompositionEvent',
      );
    }
  }
 
  public abstract type: number;
 
  public abstract length: bigint;
 
  public abstract toJSON():
    | IEnumEventDescriptorJSON
    | IDigitDecompositionEventDescriptorJSON;
 
  public abstract serialize(): Buffer;
}
 
/**
 * EnumEventDescriptor message contains the event outcomes for enumerated events.
 * Simplified class name (removed V0 suffix).
 */
export class EnumEventDescriptor
  extends EventDescriptor
  implements IDlcMessage {
  public static type = MessageType.EnumEventDescriptorV0;
 
  /**
   * Creates an EnumEventDescriptor from JSON data
   * @param json JSON object representing an enum event descriptor
   */
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
  public static fromJSON(json: any): EnumEventDescriptor {
    const instance = new EnumEventDescriptor();
    instance.outcomes = json.outcomes || [];
    return instance;
  }
 
  /**
   * Deserializes an enum_event_descriptor_v0 message
   * @param buf
   */
  public static deserialize(buf: Buffer): EnumEventDescriptor {
    const instance = new EnumEventDescriptor();
    const reader = new BufferReader(buf);
 
    reader.readBigSize(); // read type
    instance.length = reader.readBigSize(); // need to fix this
    reader.readUInt16BE(); // num_outcomes
 
    while (!reader.eof) {
      const outcomeLen = reader.readBigSize();
      const outcomeBuf = reader.readBytes(Number(outcomeLen));
      instance.outcomes.push(outcomeBuf.toString());
    }
 
    return instance;
  }
 
  /**
   * The type for enum_event_descriptor_v0 message. enum_event_descriptor_v0 = 55302
   */
  public type = EnumEventDescriptor.type;
 
  public length = BigInt(0); // Required by EventDescriptor parent class
 
  public outcomes: string[] = [];
 
  /**
   * Converts enum_event_descriptor to JSON
   */
  public toJSON(): IEnumEventDescriptorJSON {
    return {
      enumEvent: {
        outcomes: this.outcomes,
      },
    };
  }
 
  /**
   * Serializes the enum_event_descriptor_v0 message into a Buffer
   */
  public serialize(): Buffer {
    const writer = new BufferWriter();
    writer.writeBigSize(this.type);
 
    const dataWriter = new BufferWriter();
    dataWriter.writeUInt16BE(this.outcomes.length);
 
    for (const outcome of this.outcomes) {
      dataWriter.writeBigSize(outcome.length);
      dataWriter.writeBytes(Buffer.from(outcome));
    }
 
    writer.writeBigSize(dataWriter.size);
    writer.writeBytes(dataWriter.toBuffer());
 
    return writer.toBuffer();
  }
}
 
// Legacy support - keep V0 alias for backward compatibility
export const EnumEventDescriptorV0 = EnumEventDescriptor;
export type EnumEventDescriptorV0 = EnumEventDescriptor;
 
/**
 * DigitDecompositionEventDescriptor is a simple enumeration of outcomes.
 * Simplified class name (removed V0 suffix).
 */
export class DigitDecompositionEventDescriptor
  extends EventDescriptor
  implements IDlcMessage {
  public static type = MessageType.DigitDecompositionEventDescriptor;
 
  /**
   * Creates a DigitDecompositionEventDescriptor from JSON data
   * @param json JSON object representing digit decomposition event descriptor
   */
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
  public static fromJSON(json: any): DigitDecompositionEventDescriptor {
    const instance = new DigitDecompositionEventDescriptor();
    instance.base = json.base || 10;
    instance.isSigned = json.isSigned || json.is_signed || false;
    instance.unit = json.unit || '';
    instance.precision = json.precision || 0;
    instance.nbDigits = json.nbDigits || json.nb_digits || 0;
    return instance;
  }
 
  /**
   * Deserializes an digit_decomposition_event_descriptor message
   * @param buf
   */
  public static deserialize(buf: Buffer): DigitDecompositionEventDescriptor {
    const instance = new DigitDecompositionEventDescriptor();
    const reader = new BufferReader(buf);
 
    reader.readBigSize(); // read type
    instance.length = reader.readBigSize(); // need to fix this
 
    /**
     * NOTE: BASE IS INCORRECT FORMAT FOR DLC SPEC (SHOULD BE BIGSIZE)
     * Will be fixed in oracle_announcement_v1
     * https://github.com/discreetlogcontracts/dlcspecs/blob/master/Oracle.md#version-0-digit_decomposition_event_descriptor
     */
    instance.base = reader.readUInt16BE();
    instance.isSigned = reader.readUInt8() === 1;
    const unitLen = reader.readBigSize();
    const unitBuf = reader.readBytes(Number(unitLen));
    instance.unit = unitBuf.toString();
    instance.precision = reader.readUInt32BE();
    instance.nbDigits = reader.readUInt16BE();
 
    return instance;
  }
 
  /**
   * The type for digit_decomposition_event_descriptor message. digit_decomposition_event_descriptor = 55306
   */
  public type = DigitDecompositionEventDescriptor.type;
 
  public length: bigint;
 
  public base: number; // Switch to bigint in oracle_announcement_v1
 
  public isSigned: boolean;
 
  public unit: string;
 
  public precision: number;
 
  public nbDigits: number;
 
  /**
   * Validates correctness of all fields in the message
   * https://github.com/discreetlogcontracts/dlcspecs/blob/master/Oracle.md
   * @throws Will throw an error if validation fails
   */
  public validate(): void {
    if (this.base <= 0) {
      throw new Error('base must be greater than 0');
    }
 
    // TODO: support isSigned according to specifications
    if (this.isSigned) {
      throw new Error('node-dlc does not support isSigned');
    }
  }
 
  /**
   * Converts digit_decomposition_event_descriptor to JSON (canonical rust-dlc format)
   */
  public toJSON(): IDigitDecompositionEventDescriptorJSON {
    return {
      digitDecompositionEvent: {
        base: this.base,
        isSigned: this.isSigned,
        unit: this.unit,
        precision: this.precision,
        nbDigits: this.nbDigits,
      },
    };
  }
 
  /**
   * Serializes the digit_decomposition_event_descriptor message into a Buffer
   */
  public serialize(): Buffer {
    const writer = new BufferWriter();
    writer.writeBigSize(this.type);
 
    const dataWriter = new BufferWriter();
    dataWriter.writeUInt16BE(this.base); // Switch to BigSize in oracle_announcement_v1
    dataWriter.writeUInt8(this.isSigned ? 1 : 0);
    dataWriter.writeBigSize(this.unit.length);
    dataWriter.writeBytes(Buffer.from(this.unit));
    dataWriter.writeUInt32BE(this.precision);
    dataWriter.writeUInt16BE(this.nbDigits);
 
    writer.writeBigSize(dataWriter.size);
    writer.writeBytes(dataWriter.toBuffer());
 
    return writer.toBuffer();
  }
}
 
// Legacy support - keep V0 alias for backward compatibility
export const DigitDecompositionEventDescriptorV0 = DigitDecompositionEventDescriptor;
export type DigitDecompositionEventDescriptorV0 = DigitDecompositionEventDescriptor;
 
export interface IEnumEventDescriptorJSON {
  enumEvent: {
    outcomes: string[];
  };
}
 
// Rust-dlc enum variant format for DigitDecompositionEventDescriptor
export interface IDigitDecompositionEventDescriptorJSON {
  digitDecompositionEvent: {
    base: number;
    isSigned: boolean;
    unit: string;
    precision: number;
    nbDigits: number;
  };
}
 
// Legacy interface
export type IEnumEventDescriptorV0JSON = IEnumEventDescriptorJSON;
 
// Legacy interface
export type IDigitDecompositionEventDescriptorV0JSON = IDigitDecompositionEventDescriptorJSON;