"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.KeyStorageImpl = void 0;
class KeyStorageImpl {
    constructor(storage) {
        this._storage = storage;
    }
    deleteKeypair(key) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this._storage.removeItem(key);
        });
    }
    getPartialKeyPair(key) {
        return __awaiter(this, void 0, void 0, function* () {
            const loaded = yield this._storage.getItem(key);
            if (!loaded)
                return undefined;
            const keyPair = JSON.parse(loaded);
            const res = {};
            if (keyPair.hasOwnProperty('publicKey'))
                res['publicKey'] = keyPair['publicKey'];
            if (keyPair.hasOwnProperty('privateKey'))
                res['privateKey'] = keyPair['privateKey'];
            return res;
        });
    }
    getKeypair(key) {
        return __awaiter(this, void 0, void 0, function* () {
            const partial = yield this.getPartialKeyPair(key);
            if (!partial || !partial.publicKey || !partial.privateKey)
                return undefined;
            return { publicKey: partial.publicKey, privateKey: partial.privateKey };
        });
    }
    getPrivateKey(key) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.getPartialKeyPair(key).then((partial) => partial === null || partial === void 0 ? void 0 : partial.privateKey);
        });
    }
    getPublicKey(key) {
        return __awaiter(this, void 0, void 0, function* () {
            return this.getPartialKeyPair(key).then((partial) => partial === null || partial === void 0 ? void 0 : partial.publicKey);
        });
    }
    storeKeyPair(key, keyPair) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this._storage.setItem(key, JSON.stringify(keyPair));
        });
    }
    storePublicKey(key, publicKey) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this._storage.setItem(key, JSON.stringify({ publicKey }));
        });
    }
    storePrivateKey(key, privateKey) {
        return __awaiter(this, void 0, void 0, function* () {
            return yield this._storage.setItem(key, JSON.stringify({ privateKey }));
        });
    }
}
exports.KeyStorageImpl = KeyStorageImpl;
//# sourceMappingURL=KeyStorageImpl.js.map