"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_KEY_TYPE = exports.IDENTITY_MAPPING = void 0;
exports.createIdentityFromConfig = createIdentityFromConfig;
const utils_1 = require("@streamr/utils");
const trackerless_network_1 = require("@streamr/trackerless-network");
const EthereumKeyPairIdentity_1 = require("./EthereumKeyPairIdentity");
const EthereumProviderIdentity_1 = require("./EthereumProviderIdentity");
const MLDSAKeyPairIdentity_1 = require("./MLDSAKeyPairIdentity");
const ECDSAKeyPairIdentity_1 = require("./ECDSAKeyPairIdentity");
/**
 * 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 SigningUtil.ts and tests to SigningUtil.test.ts
 * 3. Add the new SigningUtil to exports.ts in the utils package
 * 3. Create the Identity implementation itself (eg. extend KeyPairIdentity) and tests for it
 * 4. Wire everything together below
 */
exports.IDENTITY_MAPPING = [
    {
        keyType: 'ECDSA_SECP256K1_EVM',
        fromConfig: EthereumKeyPairIdentity_1.EthereumKeyPairIdentity.fromConfig,
        signatureType: trackerless_network_1.SignatureType.ECDSA_SECP256K1_EVM,
    },
    {
        keyType: 'ECDSA_SECP256R1',
        fromConfig: ECDSAKeyPairIdentity_1.ECDSAKeyPairIdentity.fromConfig,
        signatureType: trackerless_network_1.SignatureType.ECDSA_SECP256R1,
    },
    {
        keyType: 'ML_DSA_87',
        fromConfig: MLDSAKeyPairIdentity_1.MLDSAKeyPairIdentity.fromConfig,
        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
utils_1.KEY_TYPES.forEach((keyType) => {
    if (!exports.IDENTITY_MAPPING.find((id) => id.keyType === keyType)) {
        throw new Error(`keyType missing from IDENTITIES: ${keyType}`);
    }
});
/**
 * Creates an Identity instance based on what's in the StreamrClient config
 */
function 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;
        const idMapping = exports.IDENTITY_MAPPING.find((id) => id.keyType === keyType);
        if (idMapping) {
            return idMapping.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();
}
//# sourceMappingURL=IdentityMapping.js.map