"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CetAdaptorSignatures = void 0;
const bufio_1 = require("@node-dlc/bufio");
const MessageType_1 = require("../MessageType");
/**
 * CetAdaptorSignatures contains CET signatures and any necessary
 * information linking the signatures to their corresponding outcome
 */
class CetAdaptorSignatures {
    constructor() {
        /**
         * The type for cet_adaptor_signature message. cet_adaptor_signature = 42774
         */
        this.type = CetAdaptorSignatures.type;
        this.sigs = [];
    }
    /**
     * Deserializes a cet_adaptor_signature message
     * @param buf
     */
    static deserialize(buf) {
        const instance = new CetAdaptorSignatures();
        const reader = new bufio_1.BufferReader(buf);
        const nbSignatures = Number(reader.readBigSize()); // nb_signatures
        // Read exactly nbSignatures * (65 + 97) bytes to match serialize format
        for (let i = 0; i < nbSignatures; i++) {
            const encryptedSig = reader.readBytes(65);
            const dleqProof = reader.readBytes(97);
            instance.sigs.push({ encryptedSig, dleqProof });
        }
        return instance;
    }
    /**
     * Converts cet_adaptor_signature to JSON (canonical rust-dlc format)
     */
    toJSON() {
        return {
            ecdsaAdaptorSignatures: this.sigs.map((sig) => {
                // Combine encryptedSig and dleqProof into single signature field as expected by rust-dlc
                const signature = Buffer.concat([sig.encryptedSig, sig.dleqProof]);
                return {
                    signature: signature.toString('hex'),
                };
            }),
        };
    }
    /**
     * Serializes the cet_adaptor_signature message into a Buffer
     */
    serialize() {
        const writer = new bufio_1.BufferWriter();
        // writer.writeBigSize(this.type);
        const dataWriter = new bufio_1.BufferWriter();
        dataWriter.writeBigSize(this.sigs.length);
        for (const sig of this.sigs) {
            dataWriter.writeBytes(sig.encryptedSig);
            dataWriter.writeBytes(sig.dleqProof);
        }
        // writer.writeBigSize(dataWriter.size);
        writer.writeBytes(dataWriter.toBuffer());
        return writer.toBuffer();
    }
}
exports.CetAdaptorSignatures = CetAdaptorSignatures;
CetAdaptorSignatures.type = MessageType_1.MessageType.CetAdaptorSignatures;
//# sourceMappingURL=CetAdaptorSignatures.js.map