"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.newPublicKey = exports.getPublicKeyList = exports._private_key_list = exports._public_key_list = exports.LOCAL_STORAGE_PREFIX_PRIVATE_KEY = exports.LOCAL_STORAGE_PREFIX_PUBLIC_KEY = void 0;
const storage_1 = require("../../client/util/web/storage");
const APINotImplementedError_1 = require("../../exception/APINotImplementedError");
exports.LOCAL_STORAGE_PREFIX_PUBLIC_KEY = '__LOCAL__STORAGE__PREFIX__PUBLIC__KEY__';
exports.LOCAL_STORAGE_PREFIX_PRIVATE_KEY = '__LOCAL__STORAGE__PREFIX__PRIVATE__KEY__';
exports._public_key_list = [];
exports._private_key_list = [];
let init = false;
let last = -1;
function getPublicKeyList($system) {
    return __awaiter(this, void 0, void 0, function* () {
        if (!init) {
            let i = 0;
            let key;
            while (true) {
                key = `${exports.LOCAL_STORAGE_PREFIX_PUBLIC_KEY}${i}`;
                if ((0, storage_1.storageHasKey)(localStorage, key)) {
                    const public_key = localStorage.getItem(key);
                    exports._public_key_list.push(public_key);
                }
                else {
                    last = i - 1;
                    break;
                }
                i++;
            }
            init = true;
        }
        // AD HOC
        while (exports._public_key_list.length < 3) {
            yield newPublicKey($system);
        }
        return exports._public_key_list;
    });
}
exports.getPublicKeyList = getPublicKeyList;
function newPublicKey($system) {
    return __awaiter(this, void 0, void 0, function* () {
        const { api: { storage: { local }, }, } = $system;
        const localStorage = local(undefined);
        if (!localStorage) {
            throw new APINotImplementedError_1.APINotSupportedError('Local Storage');
        }
        const keyPair = yield crypto.subtle.generateKey({
            name: 'RSA-OAEP',
            modulusLength: 4096,
            publicExponent: new Uint8Array([1, 0, 1]),
            hash: 'SHA-256',
        }, true, ['encrypt', 'decrypt']);
        const { publicKey, privateKey } = keyPair;
        // console.log('publicKey', publicKey)
        // console.log('privateKey', privateKey)
        const publicKeyJWK = yield crypto.subtle.exportKey('jwk', publicKey);
        const privateKeyJWK = yield crypto.subtle.exportKey('jwk', privateKey);
        // console.log('publicKeyJWK', publicKeyJWK)
        // console.log('privateKeyJWK', privateKeyJWK)
        const publicKeyJWKJSON = JSON.stringify(publicKeyJWK);
        const privateKeyJWKJSON = JSON.stringify(privateKeyJWK);
        const publicKeyJWKJSON64 = btoa(publicKeyJWKJSON);
        const privateKeyJWKJSON64 = btoa(privateKeyJWKJSON);
        // console.log('publicKeyJWKJSON64', publicKeyJWKJSON64)
        // console.log('privateKeyJWKJSON64', privateKeyJWKJSON64)
        localStorage.set(`${exports.LOCAL_STORAGE_PREFIX_PUBLIC_KEY}${last + 1}`, publicKeyJWKJSON64);
        localStorage.set(`${exports.LOCAL_STORAGE_PREFIX_PRIVATE_KEY}${last + 1}`, privateKeyJWKJSON64);
        exports._public_key_list.push(publicKeyJWKJSON);
        exports._private_key_list.push(privateKeyJWKJSON);
        return publicKeyJWKJSON;
    });
}
exports.newPublicKey = newPublicKey;
//# sourceMappingURL=index.js.map