"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Holder_1 = require("../../../../../Class/Holder");
const CryptoKey_1 = require("../../../../../wrap/CryptoKey");
const _ids_1 = require("../../../../_ids");
class GenerateKeyPair extends Holder_1.Holder {
    constructor(system) {
        super({
            fi: ['algorithm', 'extractable', 'keyUsages'],
            fo: ['public', 'private'],
        }, {
            input: {
                algorithm: {
                    ref: true,
                },
            },
            output: {
                public: {
                    ref: true,
                },
                private: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_GENERATE_KEY_PAIR);
    }
    async f({ algorithm, extractable, keyUsages }, done, fail) {
        const { api: { crypto: { generateKey }, }, } = this.__system;
        const algorithm_ = algorithm.raw();
        if (!['RSASSA-PKCS1-v1_5', 'RSA-PSS', 'RSA-OAEP', 'ECDSA'].includes(algorithm_.name)) {
            fail('invalid algorithm for key pair generation');
            return;
        }
        let keyPair;
        try {
            keyPair = (await generateKey(algorithm_, extractable, keyUsages));
        }
        catch (err) {
            fail(err.message.toLowerCase());
            return;
        }
        const { publicKey, privateKey } = keyPair;
        const public_ = (0, CryptoKey_1.wrapCryptoKey)(publicKey, this.__system);
        const private_ = (0, CryptoKey_1.wrapCryptoKey)(privateKey, this.__system);
        done({ public: public_, private: private_ });
    }
}
exports.default = GenerateKeyPair;
//# sourceMappingURL=index.js.map