"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@maci-protocol/core");
const domainobjs_1 = require("@maci-protocol/domainobjs");
const logger_1 = require("../../../ts/logger");
const proofs_1 = require("../../../ts/proofs");
const constants_1 = require("../../helpers/constants");
const ContractStorage_1 = require("../../helpers/ContractStorage");
const Deployment_1 = require("../../helpers/Deployment");
const types_1 = require("../../helpers/types");
const deployment = Deployment_1.Deployment.getInstance();
const storage = ContractStorage_1.ContractStorage.getInstance();
/**
 * Deploy step registration and task itself
 */
deployment.deployTask(constants_1.EDeploySteps.VerifyingKeysRegistry, "Deploy verifying key Registry and set keys").then((task) => task.setAction(async ({ incremental }, hre) => {
    deployment.setHre(hre);
    const deployer = await deployment.getDeployer();
    const verifyingKeysRegistryContractAddress = storage.getAddress(types_1.EContracts.VerifyingKeysRegistry, hre.network.name);
    if (incremental && verifyingKeysRegistryContractAddress) {
        // eslint-disable-next-line no-console
        (0, logger_1.logGreen)({ text: (0, logger_1.info)(`Skipping deployment of the ${types_1.EContracts.VerifyingKeysRegistry} contract`) });
        return;
    }
    const stateTreeDepth = deployment.getDeployConfigField(types_1.EContracts.VerifyingKeysRegistry, "stateTreeDepth");
    const pollStateTreeDepth = deployment.getDeployConfigField(types_1.EContracts.Poll, "stateTreeDepth") || stateTreeDepth;
    const tallyProcessingStateTreeDepth = deployment.getDeployConfigField(types_1.EContracts.VerifyingKeysRegistry, "tallyProcessingStateTreeDepth");
    const messageBatchSize = deployment.getDeployConfigField(types_1.EContracts.VerifyingKeysRegistry, "messageBatchSize");
    const voteOptionTreeDepth = deployment.getDeployConfigField(types_1.EContracts.VerifyingKeysRegistry, "voteOptionTreeDepth");
    const pollJoiningZkeyPath = deployment.getDeployConfigField(types_1.EContracts.VerifyingKeysRegistry, "zkeys.pollJoiningZkey.zkey");
    const pollJoinedZkeyPath = deployment.getDeployConfigField(types_1.EContracts.VerifyingKeysRegistry, "zkeys.pollJoinedZkey.zkey");
    const messageProcessorZkeyPathQv = deployment.getDeployConfigField(types_1.EContracts.VerifyingKeysRegistry, "zkeys.qv.messageProcessorZkey");
    const messageProcessorZkeyPathNonQv = deployment.getDeployConfigField(types_1.EContracts.VerifyingKeysRegistry, "zkeys.nonQv.messageProcessorZkey");
    const voteTallyZkeyPathQv = deployment.getDeployConfigField(types_1.EContracts.VerifyingKeysRegistry, "zkeys.qv.voteTallyZkey");
    const voteTallyZkeyPathNonQv = deployment.getDeployConfigField(types_1.EContracts.VerifyingKeysRegistry, "zkeys.nonQv.voteTallyZkey");
    const tallyVotesZkeyPathFull = deployment.getDeployConfigField(types_1.EContracts.VerifyingKeysRegistry, "zkeys.full.voteTallyZkey");
    const messageProcessorZkeyPathFull = deployment.getDeployConfigField(types_1.EContracts.VerifyingKeysRegistry, "zkeys.full.messageProcessorZkey");
    const mode = deployment.getDeployConfigField(types_1.EContracts.Poll, "mode") ?? core_1.EMode.QV;
    if (mode === core_1.EMode.QV && (!voteTallyZkeyPathQv || !messageProcessorZkeyPathQv)) {
        throw new Error("QV zkeys are not set");
    }
    if (mode === core_1.EMode.NON_QV && (!voteTallyZkeyPathNonQv || !messageProcessorZkeyPathNonQv)) {
        throw new Error("Non-QV zkeys are not set");
    }
    if (mode === core_1.EMode.FULL && (!tallyVotesZkeyPathFull || !messageProcessorZkeyPathFull)) {
        throw new Error("Full zkeys are not set");
    }
    if (!pollJoiningZkeyPath) {
        throw new Error("Poll zkeys are not set");
    }
    const [qvProcessVerifyingKey, qvTallyVerifyingKey, nonQvProcessVerifyingKey, nonQvTallyQv, fullProcessVerifyingKey, pollJoiningVerifyingKey, pollJoinedVerifyingKey,] = await Promise.all([
        messageProcessorZkeyPathQv && (0, proofs_1.extractVerifyingKey)(messageProcessorZkeyPathQv),
        voteTallyZkeyPathQv && (0, proofs_1.extractVerifyingKey)(voteTallyZkeyPathQv),
        messageProcessorZkeyPathNonQv && (0, proofs_1.extractVerifyingKey)(messageProcessorZkeyPathNonQv),
        voteTallyZkeyPathNonQv && (0, proofs_1.extractVerifyingKey)(voteTallyZkeyPathNonQv),
        messageProcessorZkeyPathFull && (0, proofs_1.extractVerifyingKey)(messageProcessorZkeyPathFull),
        pollJoiningZkeyPath && (0, proofs_1.extractVerifyingKey)(pollJoiningZkeyPath),
        pollJoinedZkeyPath && (0, proofs_1.extractVerifyingKey)(pollJoinedZkeyPath),
    ]).then((verifyingKeys) => verifyingKeys.map((verifyingKey) => verifyingKey && domainobjs_1.VerifyingKey.fromObj(verifyingKey).asContractParam()));
    const initialOwner = await deployer.getAddress();
    const verifyingKeysRegistryContract = await deployment.deployContract({
        name: types_1.EContracts.VerifyingKeysRegistry,
        signer: deployer,
    }, initialOwner);
    const processZkeys = [qvProcessVerifyingKey, nonQvProcessVerifyingKey, fullProcessVerifyingKey].filter(Boolean);
    const tallyZkeys = [qvTallyVerifyingKey, nonQvTallyQv, nonQvTallyQv].filter(Boolean);
    const modes = [];
    if (qvProcessVerifyingKey && qvTallyVerifyingKey) {
        modes.push(core_1.EMode.QV);
    }
    if (nonQvProcessVerifyingKey && nonQvTallyQv) {
        modes.push(core_1.EMode.NON_QV);
    }
    if (fullProcessVerifyingKey && nonQvTallyQv) {
        modes.push(core_1.EMode.FULL);
    }
    await verifyingKeysRegistryContract
        .setVerifyingKeysBatch({
        stateTreeDepth,
        pollStateTreeDepth,
        tallyProcessingStateTreeDepth,
        voteOptionTreeDepth,
        messageBatchSize,
        modes,
        pollJoiningVerifyingKey: pollJoiningVerifyingKey,
        pollJoinedVerifyingKey: pollJoinedVerifyingKey,
        processVerifyingKeys: processZkeys,
        tallyVerifyingKeys: tallyZkeys,
    })
        .then((tx) => tx.wait());
    await storage.register({
        id: types_1.EContracts.VerifyingKeysRegistry,
        contract: verifyingKeysRegistryContract,
        args: [initialOwner],
        network: hre.network.name,
    });
}));
//# sourceMappingURL=07-verifyingKeysRegistry.js.map