From a55b31b0e6c520b9030ab8403898260cf63858d7 Mon Sep 17 00:00:00 2001 From: reya <123083837+reyamir@users.noreply.github.com> Date: Wed, 31 Jul 2024 10:59:54 +0700 Subject: [PATCH] feat: add keyring support for linux and windows --- packages/system/src/account.ts | 7 +---- packages/system/src/commands.ts | 9 ++---- src-tauri/Cargo.lock | 51 ++++++++++++++++++++++++++------- src-tauri/Cargo.toml | 20 +++++++------ src-tauri/src/nostr/keys.rs | 35 ++++++++++------------ src-tauri/src/nostr/metadata.rs | 2 +- 6 files changed, 72 insertions(+), 52 deletions(-) diff --git a/packages/system/src/account.ts b/packages/system/src/account.ts index a506988b..7954e664 100644 --- a/packages/system/src/account.ts +++ b/packages/system/src/account.ts @@ -4,12 +4,7 @@ import { type Result, commands } from "./commands"; export const NostrAccount = { getAccounts: async () => { const query = await commands.getAccounts(); - - if (query.status === "ok") { - return query.data; - } else { - return []; - } + return query; }, loadAccount: async (npub: string) => { const bunker: string = localStorage.getItem(`${npub}_bunker`); diff --git a/packages/system/src/commands.ts b/packages/system/src/commands.ts index 3804c318..35178b2c 100644 --- a/packages/system/src/commands.ts +++ b/packages/system/src/commands.ts @@ -44,13 +44,8 @@ try { else return { status: "error", error: e as any }; } }, -async getAccounts() : Promise> { -try { - return { status: "ok", data: await TAURI_INVOKE("get_accounts") }; -} catch (e) { - if(e instanceof Error) throw e; - else return { status: "error", error: e as any }; -} +async getAccounts() : Promise { +return await TAURI_INVOKE("get_accounts"); }, async createAccount() : Promise> { try { diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 5814e136..34e19104 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1136,6 +1136,30 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c297a1c74b71ae29df00c3e22dd9534821d60eb9af5a0192823fa2acea70c2a" +[[package]] +name = "dbus" +version = "0.9.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bb21987b9fb1613058ba3843121dd18b163b254d8a6e797e144cbac14d96d1b" +dependencies = [ + "libc", + "libdbus-sys", + "winapi", +] + +[[package]] +name = "dbus-secret-service" +version = "4.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1caa0c241c01ad8d99a78d553567d38f873dd3ac16eca33a5370d650ab25584e" +dependencies = [ + "dbus", + "futures-util", + "num", + "once_cell", + "rand 0.8.5", +] + [[package]] name = "deranged" version = "0.3.11" @@ -2602,23 +2626,21 @@ dependencies = [ [[package]] name = "keyring" -version = "2.3.3" +version = "3.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "363387f0019d714aa60cc30ab4fe501a747f4c08fc58f069dd14be971bd495a0" +checksum = "c118b1bc529b034aad851808f41f49a69a337d10e112039e7f342e5fd514635b" dependencies = [ "byteorder", - "lazy_static", - "linux-keyutils", - "secret-service", + "dbus-secret-service", "security-framework", "windows-sys 0.52.0", ] [[package]] name = "keyring-search" -version = "0.2.2" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61d87b1244397d73833b1ba785317621049558a9755026dc947b411e05eb9bd6" +checksum = "8fba83ff0a0efb658afeaaa6de89c7abd3ccd34333f5a36d5dae417334fcd488" dependencies = [ "byteorder", "lazy_static", @@ -2684,6 +2706,15 @@ version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +[[package]] +name = "libdbus-sys" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06085512b750d640299b79be4bad3d2fa90a9c00b1fd9e1b46364f66f0485c72" +dependencies = [ + "pkg-config", +] + [[package]] name = "libloading" version = "0.7.4" @@ -4653,9 +4684,9 @@ dependencies = [ [[package]] name = "secret-service" -version = "3.1.0" +version = "4.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5204d39df37f06d1944935232fd2dfe05008def7ca599bf28c0800366c8a8f9" +checksum = "e4d35ad99a181be0a60ffcbe85d680d98f87bdc4d7644ade319b87076b9dbfd4" dependencies = [ "aes", "cbc", @@ -4667,7 +4698,7 @@ dependencies = [ "rand 0.8.5", "serde", "sha2", - "zbus 3.15.2", + "zbus 4.3.1", ] [[package]] diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 8815c87d..b257e0ed 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -16,11 +16,11 @@ tokio = { version = "1", features = ["full"] } serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } tauri = { version = "2.0.0-beta", features = [ - "unstable", - "tray-icon", - "macos-private-api", - "native-tls-vendored", - "protocol-asset", + "unstable", + "tray-icon", + "macos-private-api", + "native-tls-vendored", + "protocol-asset", ] } tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" } tauri-plugin-clipboard-manager = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" } @@ -34,18 +34,22 @@ tauri-plugin-shell = { git = "https://github.com/tauri-apps/plugins-workspace", tauri-plugin-updater = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" } tauri-plugin-upload = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" } tauri-specta = { git = "https://github.com/reyamir/tauri-specta", branch = "feat/tauri-v2", features = [ - "typescript", + "typescript", ] } tauri-plugin-theme = "0.4.1" tauri-plugin-decorum = { git = "https://github.com/reyamir/tauri-plugin-decorum", branch = "feat/tauri-v2" } specta = "^2.0.0-rc.12" -keyring = "2" -keyring-search = "0.2.0" reqwest = "0.12.4" url = "2.5.0" futures = "0.3.30" linkify = "0.10.0" regex = "1.10.4" +keyring = { version = "3", features = [ + "apple-native", + "windows-native", + "sync-secret-service", +] } +keyring-search = "1.2.0" [target.'cfg(target_os = "macos")'.dependencies] cocoa = "0.25.0" diff --git a/src-tauri/src/nostr/keys.rs b/src-tauri/src/nostr/keys.rs index 82ff223e..090c3e49 100644 --- a/src-tauri/src/nostr/keys.rs +++ b/src-tauri/src/nostr/keys.rs @@ -22,22 +22,17 @@ pub struct Account { #[tauri::command] #[specta::specta] -pub fn get_accounts() -> Result, String> { - let search = Search::new().map_err(|e| e.to_string())?; - let results = search.by("Account", "nostr_secret"); +pub fn get_accounts() -> Vec { + let search = Search::new().expect("Unexpected."); + let results = search.by_service("Lume"); + let list = List::list_credentials(&results, Limit::All); + let accounts: HashSet = list + .split_whitespace() + .filter(|v| v.starts_with("npub1")) + .map(String::from) + .collect(); - match List::list_credentials(results, Limit::All) { - Ok(list) => { - let accounts: HashSet = list - .split_whitespace() - .filter(|v| v.starts_with("npub1")) - .map(String::from) - .collect(); - - Ok(accounts.into_iter().collect()) - } - Err(_) => Err("Empty.".into()), - } + accounts.into_iter().collect() } #[tauri::command] @@ -58,7 +53,7 @@ pub fn create_account() -> Result { #[tauri::command] #[specta::specta] pub fn get_private_key(npub: &str) -> Result { - let keyring = Entry::new(npub, "nostr_secret").unwrap(); + let keyring = Entry::new("Lume", npub).unwrap(); if let Ok(nsec) = keyring.get_password() { let secret_key = SecretKey::from_bech32(nsec).unwrap(); @@ -90,7 +85,7 @@ pub async fn save_account( let npub = nostr_keys.public_key().to_bech32().unwrap(); let nsec = nostr_keys.secret_key().unwrap().to_bech32().unwrap(); - let keyring = Entry::new(&npub, "nostr_secret").unwrap(); + let keyring = Entry::new("Lume", &npub).unwrap(); let _ = keyring.set_password(&nsec); let signer = NostrSigner::Keys(nostr_keys); @@ -121,7 +116,7 @@ pub async fn connect_remote_account(uri: &str, state: State<'_, Nostr>) -> Resul match Nip46Signer::new(bunker_uri, app_keys, Duration::from_secs(120), None).await { Ok(signer) => { - let keyring = Entry::new(&remote_npub, "nostr_secret").unwrap(); + let keyring = Entry::new("Lume", &remote_npub).unwrap(); let _ = keyring.set_password(&app_secret); // Update signer @@ -139,7 +134,7 @@ pub async fn connect_remote_account(uri: &str, state: State<'_, Nostr>) -> Resul #[tauri::command] #[specta::specta] pub async fn get_encrypted_key(npub: &str, password: &str) -> Result { - let keyring = Entry::new(npub, "nostr_secret").unwrap(); + let keyring = Entry::new("Lume", npub).unwrap(); if let Ok(nsec) = keyring.get_password() { let secret_key = SecretKey::from_bech32(nsec).unwrap(); @@ -165,7 +160,7 @@ pub async fn load_account( ) -> Result { let handle = app.clone(); let client = &state.client; - let keyring = Entry::new(npub, "nostr_secret").unwrap(); + let keyring = Entry::new("Lume", npub).unwrap(); let password = match keyring.get_password() { Ok(pw) => pw, diff --git a/src-tauri/src/nostr/metadata.rs b/src-tauri/src/nostr/metadata.rs index e1fe2558..968b8e37 100644 --- a/src-tauri/src/nostr/metadata.rs +++ b/src-tauri/src/nostr/metadata.rs @@ -359,7 +359,7 @@ pub async fn remove_wallet(state: State<'_, Nostr>) -> Result<(), ()> { let client = &state.client; let keyring = Entry::new("Lume Secret", "Bitcoin Connect").unwrap(); - match keyring.delete_password() { + match keyring.delete_credential() { Ok(_) => { client.unset_zapper().await; Ok(())