"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DefaultConsortiumProvider = void 0;
const cactus_common_1 = require("@hyperledger/cactus-common");
const cactus_common_2 = require("@hyperledger/cactus-common");
class DefaultConsortiumProvider {
    get className() {
        return DefaultConsortiumProvider.CLASS_NAME;
    }
    constructor(options) {
        this.options = options;
        const fnTag = `${this.className}#constructor()`;
        cactus_common_2.Checks.truthy(options, `${fnTag} arg options`);
        const level = this.options.logLevel || "INFO";
        const label = this.className;
        this.log = cactus_common_1.LoggerProvider.getOrCreate({ level, label });
    }
    parseConsortiumJws(response) {
        const fnTag = `DefaultConsortiumProvider#parseConsortiumJws()`;
        cactus_common_2.Checks.truthy(response, `${fnTag}::response`);
        cactus_common_2.Checks.truthy(response.jws, `${fnTag}::response.jws`);
        cactus_common_2.Checks.truthy(response.jws.payload, `${fnTag}::response.jws.payload`);
        const json = Buffer.from(response.jws.payload, "base64").toString();
        const body = JSON.parse(json);
        const { consortiumDatabase } = body;
        cactus_common_2.Checks.truthy(consortiumDatabase, `${fnTag}::consortiumDatabase`);
        // FIXME Ideally there would be an option here to validate the JWS based on
        // all the signatures and the corresponding public keys (which the caller
        // would have to be able to supply).
        // We do not yet have this crypto functions available in a cross platform
        // manner so it is omitted for now but much needed prior to any GA release.
        return consortiumDatabase;
    }
    async get() {
        try {
            const res = await this.options.apiClient.getConsortiumJwsV1();
            return this.parseConsortiumJws(res.data);
        }
        catch (ex) {
            const innerException = (ex.toJSON && ex.toJSON()) || ex;
            this.log.error(`Request for Consortium JWS failed: `, innerException);
            throw ex;
        }
    }
}
exports.DefaultConsortiumProvider = DefaultConsortiumProvider;
DefaultConsortiumProvider.CLASS_NAME = "DefaultConsortiumProvider";
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGVmYXVsdC1jb25zb3J0aXVtLXByb3ZpZGVyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vc3JjL21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