# All files / lib/messages OracleAnnouncementV0.ts

100% Statements 29/29
100% Branches 0/0
100% Functions 5/5
100% Lines 29/29

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

    1x
    1x

    1x

    1x
    1x

    63x
    63x

    63x
    63x
    63x
    63x
    63x

    63x

    69x

    28x

    27x

    27x

    5x

    33x
    33x

    33x
    33x
    33x
    33x

    33x
    33x

    33x

     import { BufferReader, BufferWriter } from '@node-dlc/bufio';
    import { math, verify } from 'bip-schnorr';

    import { MessageType } from '../MessageType';
    import { getTlv } from '../serialize/getTlv';
    import { IDlcMessage } from './DlcMessage';
    import { IOracleEventV0JSON, OracleEventV0 } from './OracleEventV0';

    /**
     * In order to make it possible to hold oracles accountable in cases where
     * they do not release a signature for an event outcome, there needs to be
     * a proof that an oracle has committed to a given outcome. This proof is
     * given in a so-called oracle announcement, which contains an oracle event
     * together with the oracle public key and a signature over its serialization,
     * which must be valid with respect to the specified public key.
     *
     * This also makes it possible for users to obtain oracle event information
     * from an un-trusted peer while being guaranteed that it originates from a
     * given oracle.
     */
    export class OracleAnnouncementV0 implements IDlcMessage {
      public static type = MessageType.OracleAnnouncementV0;

      /**
       * Deserializes an oracle_announcement_v0 message
       * @param buf
       */
      public static deserialize(buf: Buffer): OracleAnnouncementV0 {
        const instance = new OracleAnnouncementV0();
        const reader = new BufferReader(buf);

        reader.readBigSize(); // read type
        instance.length = reader.readBigSize();
        instance.announcementSig = reader.readBytes(64);
        instance.oraclePubkey = reader.readBytes(32);
        instance.oracleEvent = OracleEventV0.deserialize(getTlv(reader));

        return instance;
      }

      /**
       * The type for oracle_announcement_v0 message. oracle_announcement_v0 = 55332
       */
      public type = OracleAnnouncementV0.type;

      public length: bigint;

      public announcementSig: Buffer;

      public oraclePubkey: Buffer;

      public oracleEvent: OracleEventV0;

      public validate(): void {
        this.oracleEvent.validate();

        // Verify announcement sig
        const msg = math.taggedHash(
          'DLC/oracle/announcement/v0',
          this.oracleEvent.serialize(),
        );
        verify(this.oraclePubkey, msg, this.announcementSig);
      }

      /**
       * Converts oracle_announcement_v0 to JSON
       */
      public toJSON(): OracleAnnouncementV0JSON {
        return {
          type: this.type,
          announcementSig: this.announcementSig.toString('hex'),
          oraclePubkey: this.oraclePubkey.toString('hex'),
          oracleEvent: this.oracleEvent.toJSON(),
        };
      }

      /**
       * Serializes the oracle_announcement_v0 message into a Buffer
       */
      public serialize(): Buffer {
        const writer = new BufferWriter();
        writer.writeBigSize(this.type);

        const dataWriter = new BufferWriter();
        dataWriter.writeBytes(this.announcementSig);
        dataWriter.writeBytes(this.oraclePubkey);
        dataWriter.writeBytes(this.oracleEvent.serialize());

        writer.writeBigSize(dataWriter.size);
        writer.writeBytes(dataWriter.toBuffer());

        return writer.toBuffer();
      }
    }

    export interface OracleAnnouncementV0JSON {
      type: number;
      announcementSig: string;
      oraclePubkey: string;
      oracleEvent: IOracleEventV0JSON;
    }