"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocalGroupKeyStore = void 0;
const tsyringe_1 = require("tsyringe");
const PersistenceManager_1 = require("../PersistenceManager");
const events_1 = require("../events");
const LoggerFactory_1 = require("../utils/LoggerFactory");
const GroupKey_1 = require("./GroupKey");
function formLookupKey1(keyId, publisherId) {
    return `${publisherId}::${keyId}`;
}
function formLookupKey2(publisherId, streamId) {
    return `${publisherId}::${streamId}`;
}
let LocalGroupKeyStore = class LocalGroupKeyStore {
    persistenceManager;
    eventEmitter;
    logger;
    constructor(persistenceManager, eventEmitter, loggerFactory) {
        this.persistenceManager = persistenceManager;
        this.eventEmitter = eventEmitter;
        this.logger = loggerFactory.createLogger(module);
    }
    async get(keyId, publisherId) {
        const persistence = await this.persistenceManager.getPersistence(PersistenceManager_1.NAMESPACES.ENCRYPTION_KEYS);
        const value = await persistence.get(formLookupKey1(keyId, publisherId));
        if (value !== undefined) {
            return new GroupKey_1.GroupKey(keyId, Buffer.from(value, 'hex'));
        }
        else {
            return this.getLegacyKey(keyId);
        }
    }
    /**
     * Legacy keys refer to group keys migrated from a previous version of the client where group keys were not tied
     * to a specific publisherId, therefore any publisherId for a given legacy key id is considered a match.
     *
     * TODO: remove this functionality in the future
     */
    async getLegacyKey(keyId) {
        const persistence = await this.persistenceManager.getPersistence(PersistenceManager_1.NAMESPACES.ENCRYPTION_KEYS);
        const value = await persistence.get(formLookupKey1(keyId, 'LEGACY'));
        return value !== undefined ? new GroupKey_1.GroupKey(keyId, Buffer.from(value, 'hex')) : undefined;
    }
    async set(keyId, publisherId, data) {
        const persistence = await this.persistenceManager.getPersistence(PersistenceManager_1.NAMESPACES.ENCRYPTION_KEYS);
        await persistence.set(formLookupKey1(keyId, publisherId), Buffer.from(data).toString('hex'));
        this.logger.debug('Set key', { keyId, publisherId });
        this.eventEmitter.emit('storeEncryptionKeyToLocalStore', keyId);
    }
    async setLatestEncryptionKeyId(keyId, publisherId, streamId) {
        const persistence = await this.persistenceManager.getPersistence(PersistenceManager_1.NAMESPACES.LATEST_ENCRYPTION_KEY_IDS);
        this.logger.debug('Set latest encryptionKeyId', { keyId, publisherId, streamId });
        await persistence.set(formLookupKey2(publisherId, streamId), keyId);
    }
    async getLatestEncryptionKeyId(publisherId, streamId) {
        const persistence = await this.persistenceManager.getPersistence(PersistenceManager_1.NAMESPACES.LATEST_ENCRYPTION_KEY_IDS);
        const value = await persistence.get(formLookupKey2(publisherId, streamId));
        return value;
    }
};
exports.LocalGroupKeyStore = LocalGroupKeyStore;
exports.LocalGroupKeyStore = LocalGroupKeyStore = __decorate([
    (0, tsyringe_1.scoped)(tsyringe_1.Lifecycle.ContainerScoped),
    __metadata("design:paramtypes", [PersistenceManager_1.PersistenceManager,
        events_1.StreamrClientEventEmitter,
        LoggerFactory_1.LoggerFactory])
], LocalGroupKeyStore);
//# sourceMappingURL=LocalGroupKeyStore.js.map