package com.icing.dialer

import java.security.KeyStore

object KeyDeleterHelper {

    private const val ANDROID_KEYSTORE = "AndroidKeyStore"

    /**
     * Deletes the key pair associated with the given alias from the Android Keystore.
     *
     * @param alias The alias of the key pair to delete.
     * @throws Exception if deletion fails.
     */
    fun deleteKeyPair(alias: String) {
        try {
            val keyStore = KeyStore.getInstance(ANDROID_KEYSTORE).apply { load(null) }

            if (!keyStore.containsAlias(alias)) {
                throw Exception("No key found with alias \"$alias\" to delete.")
            }

            keyStore.deleteEntry(alias)
        } catch (e: Exception) {
            throw Exception("Failed to delete key pair: ${e.message}", e)
        }
    }
}
