"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 {
    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 });
    }
    get className() {
        return DefaultConsortiumProvider.CLASS_NAME;
    }
    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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGVmYXVsdC1jb25zb3J0aXVtLXByb3ZpZGVyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vc3JjL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