"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createIdentityFromConfig = exports.DEFAULT_KEY_TYPE = exports.IdentityMapping = exports.validKeyTypeValues = void 0;
const utils_1 = require("@streamr/utils");
const EthereumKeyPairIdentity_1 = require("./EthereumKeyPairIdentity");
const EthereumProviderIdentity_1 = require("./EthereumProviderIdentity");
const MLDSAKeyPairIdentity_1 = require("./MLDSAKeyPairIdentity");
const trackerless_network_1 = require("@streamr/trackerless-network");
/**
 * This is where config keyTypes are connected to Identity implementations.
 *
 * How to configure new Identity types:
 * 1. Add a new SignatureType entry to NetworkRpc.proto in network package
 * 2. Add the needed SigningUtil to signingUtils.ts
 * 3. Create the Identity implementation itself (eg. extend KeyPairIdentity)
 * 4. Wire everything together below
 */
exports.validKeyTypeValues = ['ECDSA_SECP256K1_EVM', 'ML_DSA_87'];
/** @internal */
exports.IdentityMapping = {
    'ECDSA_SECP256K1_EVM': {
        fromConfig: EthereumKeyPairIdentity_1.EthereumKeyPairIdentity.fromConfig,
        generate: EthereumKeyPairIdentity_1.EthereumKeyPairIdentity.generate,
        signingUtil: utils_1.ECDSA_SECP256K1_EVM,
        signatureType: trackerless_network_1.SignatureType.ECDSA_SECP256K1_EVM,
    },
    'ML_DSA_87': {
        fromConfig: MLDSAKeyPairIdentity_1.MLDSAKeyPairIdentity.fromConfig,
        generate: MLDSAKeyPairIdentity_1.MLDSAKeyPairIdentity.generate,
        signingUtil: utils_1.ML_DSA_87,
        signatureType: trackerless_network_1.SignatureType.ML_DSA_87,
    }
};
exports.DEFAULT_KEY_TYPE = 'ECDSA_SECP256K1_EVM';
// Static check that all valid key types have corresponding factory functions above
exports.validKeyTypeValues.forEach((keyType) => {
    if (!(keyType in exports.IdentityMapping)) {
        throw new Error(`Missing factory function for keyType: ${keyType}`);
    }
});
/**
 * Creates an Identity instance based on what's in the StreamrClient config
 */
const createIdentityFromConfig = (config) => {
    // Key pair -based identities
    if (config.auth?.privateKey !== undefined) {
        // Default key type is secp256k1 private key (="Ethereum private key")
        const keyType = config.auth.keyType ?? exports.DEFAULT_KEY_TYPE;
        if (exports.IdentityMapping[keyType]) {
            return exports.IdentityMapping[keyType].fromConfig(config);
        }
        else {
            throw new Error(`Unsupported keyType given in config: ${keyType}`);
        }
    }
    // If a custom identity implementation is given, simply use that
    if (config.auth?.identity !== undefined) {
        return config.auth?.identity;
    }
    // Ethereum provider
    if (config.auth?.ethereum !== undefined) {
        return EthereumProviderIdentity_1.EthereumProviderIdentity.fromConfig(config);
    }
    // If no identity is configured, generate a random EthereumKeyPairIdentity
    return EthereumKeyPairIdentity_1.EthereumKeyPairIdentity.generate();
};
exports.createIdentityFromConfig = createIdentityFromConfig;
//# sourceMappingURL=IdentityMapping.js.map