tauri-plugin-secure-element 0.1.0-beta.2

Tauri plugin for secure element use on iOS (Secure Enclave) and Android (Strongbox and TEE).
use serde::de::DeserializeOwned;
use tauri::{plugin::PluginApi, plugin::PluginHandle, AppHandle, Runtime};

use crate::models::*;

#[cfg(target_os = "ios")]
tauri::ios_plugin_binding!(init_plugin_secure_element);

/// Initializes the Kotlin or Swift plugin classes for mobile platforms.
pub fn init<R: Runtime, C: DeserializeOwned>(
    _app: &AppHandle<R>,
    api: PluginApi<R, C>,
) -> crate::Result<SecureElement<R>> {
    #[cfg(target_os = "android")]
    {
        let handle =
            api.register_android_plugin("net.kackman.secureelement", "SecureKeysPlugin")?;
        Ok(SecureElement(handle))
    }
    #[cfg(target_os = "ios")]
    {
        let handle = api.register_ios_plugin(init_plugin_secure_element)?;
        Ok(SecureElement(handle))
    }
}

/// Access to the secure-element APIs for mobile platforms.
pub struct SecureElement<R: Runtime>(PluginHandle<R>);

impl<R: Runtime> SecureElement<R> {
    pub fn ping(&self, payload: PingRequest) -> crate::Result<PingResponse> {
        self.0
            .run_mobile_plugin("ping", payload)
            .map_err(Into::into)
    }

    pub fn generate_secure_key(
        &self,
        payload: GenerateSecureKeyRequest,
    ) -> crate::Result<GenerateSecureKeyResponse> {
        self.0
            .run_mobile_plugin("generateSecureKey", payload)
            .map_err(Into::into)
    }

    pub fn list_keys(&self, payload: ListKeysRequest) -> crate::Result<ListKeysResponse> {
        self.0
            .run_mobile_plugin("listKeys", payload)
            .map_err(Into::into)
    }

    pub fn sign_with_key(&self, payload: SignWithKeyRequest) -> crate::Result<SignWithKeyResponse> {
        self.0
            .run_mobile_plugin("signWithKey", payload)
            .map_err(Into::into)
    }

    pub fn delete_key(&self, payload: DeleteKeyRequest) -> crate::Result<DeleteKeyResponse> {
        self.0
            .run_mobile_plugin("deleteKey", payload)
            .map_err(Into::into)
    }

    pub fn check_secure_element_support(&self) -> crate::Result<CheckSecureElementSupportResponse> {
        self.0
            .run_mobile_plugin("checkSecureElementSupport", ())
            .map_err(Into::into)
    }
}