tauri_plugin_crypto_hw/
lib.rs

1use tauri::{
2    plugin::{Builder, TauriPlugin},
3    Manager, Runtime,
4};
5
6pub use models::*;
7
8#[cfg(desktop)]
9mod desktop;
10#[cfg(mobile)]
11mod mobile;
12
13mod commands;
14mod error;
15mod models;
16
17pub use error::{Error, Result};
18
19#[cfg(desktop)]
20use desktop::Crypto;
21#[cfg(mobile)]
22use mobile::Crypto;
23
24/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the crypto APIs.
25pub trait CryptoExt<R: Runtime> {
26    fn crypto(&self) -> &Crypto<R>;
27}
28
29impl<R: Runtime, T: Manager<R>> crate::CryptoExt<R> for T {
30    fn crypto(&self) -> &Crypto<R> {
31        self.state::<Crypto<R>>().inner()
32    }
33}
34
35/// Initializes the plugin.
36pub fn init<R: Runtime>() -> TauriPlugin<R> {
37    Builder::new("crypto-hw")
38        .invoke_handler(tauri::generate_handler![
39            commands::generate,
40            commands::exists,
41            commands::get_public_key,
42            commands::sign_payload,
43            commands::verify_signature,
44        ])
45        .setup(|app, api| {
46            #[cfg(mobile)]
47            let crypto = mobile::init(app, api)?;
48            #[cfg(desktop)]
49            let crypto = desktop::init(app, api)?;
50            app.manage(crypto);
51            Ok(())
52        })
53        .build()
54}