"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FundingSignaturesV0 = void 0;
const bufio_1 = require("@node-dlc/bufio");
const MessageType_1 = require("../MessageType");
const ScriptWitnessV0_1 = require("./ScriptWitnessV0");
/**
 * FundingSignatures V0 contains signatures of the funding transaction
 * and any necessary information linking the signatures to their inputs.
 */
class FundingSignaturesV0 {
    constructor() {
        /**
         * The type for funding_signatures_v0 message. funding_signatures_v0 = 42776
         */
        this.type = FundingSignaturesV0.type;
        this.witnessElements = [];
    }
    /**
     * Deserializes an funding_signatures_v0 message
     * @param buf
     */
    static deserialize(buf) {
        const instance = new FundingSignaturesV0();
        const reader = new bufio_1.BufferReader(buf);
        reader.readBigSize(); // read type
        instance.length = reader.readBigSize();
        const numWitnesses = reader.readUInt16BE();
        for (let i = 0; i < numWitnesses; i++) {
            const numWitnessElements = reader.readUInt16BE();
            const witnessElements = [];
            for (let j = 0; j < numWitnessElements; j++) {
                const witness = ScriptWitnessV0_1.ScriptWitnessV0.getWitness(reader);
                witnessElements.push(ScriptWitnessV0_1.ScriptWitnessV0.deserialize(witness));
            }
            instance.witnessElements.push(witnessElements);
        }
        return instance;
    }
    /**
     * Converts funding_signatures_v0 to JSON
     */
    toJSON() {
        return {
            type: this.type,
            witnessElements: this.witnessElements.map((witnessElement) => {
                return witnessElement.map((witness) => witness.toJSON());
            }),
        };
    }
    /**
     * Serializes the funding_signatures_v0 message into a Buffer
     */
    serialize() {
        const writer = new bufio_1.BufferWriter();
        writer.writeBigSize(this.type);
        const dataWriter = new bufio_1.BufferWriter();
        dataWriter.writeUInt16BE(this.witnessElements.length);
        for (const witnessElements of this.witnessElements) {
            dataWriter.writeUInt16BE(witnessElements.length);
            for (const witnessElement of witnessElements) {
                dataWriter.writeBytes(witnessElement.serialize());
            }
        }
        writer.writeBigSize(dataWriter.size);
        writer.writeBytes(dataWriter.toBuffer());
        return writer.toBuffer();
    }
}
exports.FundingSignaturesV0 = FundingSignaturesV0;
FundingSignaturesV0.type = MessageType_1.MessageType.FundingSignaturesV0;
//# sourceMappingURL=FundingSignaturesV0.js.map