"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OracleEventV0 = void 0;
const bufio_1 = require("@node-dlc/bufio");
const MessageType_1 = require("../MessageType");
const getTlv_1 = require("../serialize/getTlv");
const EventDescriptor_1 = require("./EventDescriptor");
/**
 * For users to be able to create DLCs based on a given event, they also
 * need to obtain information about the oracle and the time at which it
 * plans on releasing a signature over the event outcome. OracleEvent
 * mesages contain such information, which includes:
 *   - the nonce(s) that will be used to sign the event outcome(s)
 *   - the earliest time (UTC) at which it plans on releasing a signature
 *     over the event outcome, in epoch seconds
 *   - the event descriptor
 *   - the event ID which can be a name or categorization associated with
 *     the event by the oracle
 */
class OracleEventV0 {
    constructor() {
        /**
         * The type for oracle_event message. oracle_event = 55330
         */
        this.type = OracleEventV0.type;
        this.oracleNonces = [];
    }
    /**
     * Deserializes an oracle_event message
     * @param buf
     */
    static deserialize(buf) {
        const instance = new OracleEventV0();
        const reader = new bufio_1.BufferReader(buf);
        reader.readBigSize(); // read type
        instance.length = reader.readBigSize();
        const nonceCount = reader.readUInt16BE();
        for (let i = 0; i < nonceCount; i++) {
            instance.oracleNonces.push(reader.readBytes(32));
        }
        instance.eventMaturityEpoch = reader.readUInt32BE();
        instance.eventDescriptor = EventDescriptor_1.EventDescriptor.deserialize((0, getTlv_1.getTlv)(reader));
        const eventIdLength = reader.readBigSize();
        const eventIdBuf = reader.readBytes(Number(eventIdLength));
        instance.eventId = eventIdBuf.toString();
        return instance;
    }
    /**
     * 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
     */
    validate() {
        if (this.eventMaturityEpoch < 0) {
            throw new Error('eventMaturityEpoch must be greater than or equal to 0');
        }
        if (this.eventDescriptor.type === EventDescriptor_1.DigitDecompositionEventDescriptorV0.type) {
            const eventDescriptor = this
                .eventDescriptor;
            eventDescriptor.validate();
            if (eventDescriptor.nbDigits !== this.oracleNonces.length) {
                throw Error('OracleEvent oracleNonces length must match DigitDecompositionEventDescriptor nbDigits');
            }
        }
    }
    /**
     * Converts oracle_event to JSON
     */
    toJSON() {
        return {
            type: this.type,
            oracleNonces: this.oracleNonces.map((oracle) => oracle.toString('hex')),
            eventMaturityEpoch: this.eventMaturityEpoch,
            eventDescriptor: this.eventDescriptor.toJSON(),
            eventId: this.eventId,
        };
    }
    /**
     * Serializes the oracle_event message into a Buffer
     */
    serialize() {
        const writer = new bufio_1.BufferWriter();
        writer.writeBigSize(this.type);
        const dataWriter = new bufio_1.BufferWriter();
        dataWriter.writeUInt16BE(this.oracleNonces.length);
        for (const nonce of this.oracleNonces) {
            dataWriter.writeBytes(nonce);
        }
        dataWriter.writeUInt32BE(this.eventMaturityEpoch);
        dataWriter.writeBytes(this.eventDescriptor.serialize());
        dataWriter.writeBigSize(this.eventId.length);
        dataWriter.writeBytes(Buffer.from(this.eventId));
        writer.writeBigSize(dataWriter.size);
        writer.writeBytes(dataWriter.toBuffer());
        return writer.toBuffer();
    }
}
exports.OracleEventV0 = OracleEventV0;
OracleEventV0.type = MessageType_1.MessageType.OracleEventV0;
//# sourceMappingURL=OracleEventV0.js.map