"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OracleAnnouncementV0 = void 0;
const bufio_1 = require("@node-dlc/bufio");
const bip_schnorr_1 = require("bip-schnorr");
const MessageType_1 = require("../MessageType");
const getTlv_1 = require("../serialize/getTlv");
const OracleEventV0_1 = require("./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.
 */
class OracleAnnouncementV0 {
    constructor() {
        /**
         * The type for oracle_announcement_v0 message. oracle_announcement_v0 = 55332
         */
        this.type = OracleAnnouncementV0.type;
    }
    /**
     * Deserializes an oracle_announcement_v0 message
     * @param buf
     */
    static deserialize(buf) {
        const instance = new OracleAnnouncementV0();
        const reader = new bufio_1.BufferReader(buf);
        reader.readBigSize(); // read type
        instance.length = reader.readBigSize();
        instance.announcementSig = reader.readBytes(64);
        instance.oraclePubkey = reader.readBytes(32);
        instance.oracleEvent = OracleEventV0_1.OracleEventV0.deserialize((0, getTlv_1.getTlv)(reader));
        return instance;
    }
    validate() {
        this.oracleEvent.validate();
        // Verify announcement sig
        const msg = bip_schnorr_1.math.taggedHash('DLC/oracle/announcement/v0', this.oracleEvent.serialize());
        (0, bip_schnorr_1.verify)(this.oraclePubkey, msg, this.announcementSig);
    }
    /**
     * Converts oracle_announcement_v0 to JSON
     */
    toJSON() {
        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
     */
    serialize() {
        const writer = new bufio_1.BufferWriter();
        writer.writeBigSize(this.type);
        const dataWriter = new bufio_1.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();
    }
}
exports.OracleAnnouncementV0 = OracleAnnouncementV0;
OracleAnnouncementV0.type = MessageType_1.MessageType.OracleAnnouncementV0;
//# sourceMappingURL=OracleAnnouncementV0.js.map