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