"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const types_1 = require("@requestnetwork/types");
const utils_1 = require("@requestnetwork/utils");
/**
 * Implementation of the decryption provider from private key
 * Allows to decrypt() with "ethereumAddress" identities thanks to their private key given in constructor() or addDecryptionParameters()
 * @deprecated Use EthereumPrivateKeyCipherProvider instead
 */
class EthereumPrivateKeyDecryptionProvider {
    constructor(decryptionParameters) {
        /** list of supported encryption method */
        this.supportedMethods = [types_1.EncryptionTypes.METHOD.ECIES];
        /** list of supported identity types */
        this.supportedIdentityTypes = [types_1.IdentityTypes.TYPE.ETHEREUM_ADDRESS];
        console.warn('EthereumPrivateKeyDecryptionProvider is deprecated. Use EthereumPrivateKeyCipherProvider instead.');
        this.decryptionParametersDictionary = new Map();
        if (decryptionParameters) {
            this.addDecryptionParameters(decryptionParameters);
        }
    }
    /**
     * Decrypts data
     *
     * @param data the encrypted data
     * @param identity identity to decrypt with
     *
     * @returns the data decrypted
     */
    decrypt(encryptedData, identity) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (encryptedData.type !== types_1.EncryptionTypes.METHOD.ECIES) {
                throw Error(`The data must be encrypted with ${types_1.EncryptionTypes.METHOD.ECIES}`);
            }
            if (!this.supportedIdentityTypes.includes(identity.type)) {
                throw Error(`Identity type not supported ${identity.type}`);
            }
            // toLowerCase to avoid mismatch because of case
            const decryptionParameters = this.decryptionParametersDictionary.get(identity.value.toLowerCase());
            if (!decryptionParameters) {
                throw Error(`private key unknown for the identity: ${identity.value}`);
            }
            return (0, utils_1.decrypt)(encryptedData, decryptionParameters);
        });
    }
    /**
     * Check if an identity is registered in the provider
     *
     * @param identity identity to check
     *
     * @returns true if the identity is registered, false otherwise
     */
    isIdentityRegistered(identity) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return Array.from(this.decryptionParametersDictionary.keys()).some((address) => identity.value.toLowerCase() === address.toLowerCase());
        });
    }
    /**
     * Adds a new private key in the provider
     *
     * @param decryptionParameters decryption parameters to add
     *
     * @returns identity from the decryption parameter added
     */
    addDecryptionParameters(decryptionParameters) {
        if (!this.supportedMethods.includes(decryptionParameters.method)) {
            throw Error(`Encryption method not supported ${decryptionParameters.method}`);
        }
        // compute the address from private key
        // toLowerCase to avoid mismatch because of case
        const address = (0, utils_1.getAddressFromPrivateKey)(decryptionParameters.key).toLowerCase();
        this.decryptionParametersDictionary.set(address, decryptionParameters);
        return {
            type: types_1.IdentityTypes.TYPE.ETHEREUM_ADDRESS,
            value: address,
        };
    }
    /**
     * Removes a private key from the provider
     *
     * @param identity identity to remove the private key
     *
     * @returns void
     */
    removeRegisteredIdentity(identity) {
        // Check the type of the identity to be sure that the value used to delete will be the right type
        if (!this.supportedIdentityTypes.includes(identity.type)) {
            throw Error(`Identity type not supported ${identity.type}`);
        }
        this.decryptionParametersDictionary.delete(identity.value);
    }
    /**
     * Removes all private keys from the provider
     *
     * @param identity identity to remove the private key
     *
     * @returns void
     */
    clearAllRegisteredIdentities() {
        this.decryptionParametersDictionary.clear();
    }
    /**
     * Gets all the identities available to decrypt with
     *
     * @returns all the identities registered
     */
    getAllRegisteredIdentities() {
        return Array.from(this.decryptionParametersDictionary.keys(), (address) => ({
            type: types_1.IdentityTypes.TYPE.ETHEREUM_ADDRESS,
            value: address,
        }));
    }
}
exports.default = EthereumPrivateKeyDecryptionProvider;
//# sourceMappingURL=ethereum-private-key-decryption-provider.js.map