"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseReceiptFile = exports.writeReceiptFile = exports.parseSecretsFile = void 0;
const fs = __importStar(require("fs"));
const dotenv = __importStar(require("dotenv"));
const airnode_validator_1 = require("@api3/airnode-validator");
const promise_utils_1 = require("@api3/promise-utils");
const infrastructure_1 = require("./infrastructure");
const logger = __importStar(require("../utils/logger"));
const utils_1 = require("../utils");
function parseSecretsFile(secretsPath) {
    logger.debug('Parsing secrets file');
    const goDotenvParse = (0, promise_utils_1.goSync)(() => dotenv.parse(fs.readFileSync(secretsPath)));
    if (!goDotenvParse.success)
        throw (0, infrastructure_1.logAndReturnError)(`Failed to parse secrets file: ${goDotenvParse.error}`);
    return goDotenvParse.data;
}
exports.parseSecretsFile = parseSecretsFile;
function writeReceiptFile(receiptFilename, config, success) {
    const mnemonic = config.nodeSettings.airnodeWalletMnemonic;
    const airnodeAddress = (0, utils_1.deriveAirnodeAddress)(mnemonic);
    const airnodeAddressShort = (0, utils_1.shortenAirnodeAddress)(airnodeAddress);
    const receipt = {
        airnodeWallet: {
            airnodeAddress,
            airnodeAddressShort,
            airnodeXpub: (0, utils_1.deriveAirnodeXpub)(mnemonic),
        },
        deployment: {
            cloudProvider: config.nodeSettings.cloudProvider,
            stage: config.nodeSettings.stage,
            nodeVersion: config.nodeSettings.nodeVersion,
            timestamp: new Date().toISOString(),
        },
        success,
    };
    logger.debug('Writing receipt.json file');
    fs.writeFileSync(receiptFilename, JSON.stringify(receipt, null, 2));
    logger.info(`Outputted ${receiptFilename}\n` + '  This file does not contain any sensitive information.');
}
exports.writeReceiptFile = writeReceiptFile;
function parseReceiptFile(receiptFilename) {
    logger.debug('Parsing receipt file');
    const goParse = (0, promise_utils_1.goSync)(() => JSON.parse(fs.readFileSync(receiptFilename, 'utf8')));
    if (!goParse.success)
        throw (0, infrastructure_1.logAndReturnError)(`Failed to parse receipt file: ${goParse.error}`);
    const receipt = goParse.data;
    const validationResult = (0, airnode_validator_1.parseReceipt)(receipt);
    if (!validationResult.success) {
        throw (0, infrastructure_1.logAndReturnError)(`Invalid Airnode receipt file: ${validationResult.error}`);
    }
    return receipt;
}
exports.parseReceiptFile = parseReceiptFile;
//# sourceMappingURL=files.js.map