From 126506522dd3de287edb26ebfb7df5f5a94b3479 Mon Sep 17 00:00:00 2001 From: reya Date: Thu, 21 Nov 2024 09:03:48 +0700 Subject: [PATCH] feat: save user key to keyring --- Cargo.toml | 6 ++++++ crates/client/Cargo.toml | 2 ++ crates/client/src/lib.rs | 12 ++++++++++++ crates/ui/Cargo.toml | 5 ++--- crates/ui/src/utils.rs | 2 +- crates/ui/src/views/setup.rs | 11 ++++++++--- 6 files changed, 31 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 49f0d03..d53056a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,3 +27,9 @@ chrono = "0.4.38" tracing = "0.1.40" anyhow = "1.0.44" smallvec = "1.13.2" +keyring-search = "1.2.0" +keyring = { version = "3", features = [ + "apple-native", + "windows-native", + "sync-secret-service", +] } diff --git a/crates/client/Cargo.toml b/crates/client/Cargo.toml index cdbc61d..43485f3 100644 --- a/crates/client/Cargo.toml +++ b/crates/client/Cargo.toml @@ -8,3 +8,5 @@ gpui.workspace = true nostr-sdk.workspace = true dirs.workspace = true tokio.workspace = true +keyring-search.workspace = true +keyring.workspace = true diff --git a/crates/client/src/lib.rs b/crates/client/src/lib.rs index f4e4a72..86ff30c 100644 --- a/crates/client/src/lib.rs +++ b/crates/client/src/lib.rs @@ -1,4 +1,5 @@ use gpui::Global; +use keyring::Entry; use nostr_sdk::prelude::*; use state::get_client; @@ -17,4 +18,15 @@ impl NostrClient { Self { client } } + + pub fn add_account(&self, keys: Keys) -> Result<()> { + let public_key = keys.public_key().to_bech32()?; + let secret = keys.secret_key().to_secret_hex(); + let entry = Entry::new("Coop Safe Storage", &public_key)?; + + // Add secret to keyring + entry.set_password(&secret)?; + + Ok(()) + } } diff --git a/crates/ui/Cargo.toml b/crates/ui/Cargo.toml index 8f8ced5..5796762 100644 --- a/crates/ui/Cargo.toml +++ b/crates/ui/Cargo.toml @@ -13,8 +13,7 @@ gpui.workspace = true components.workspace = true tokio.workspace = true nostr-sdk.workspace = true +keyring-search.workspace = true +keyring.workspace = true client = { version = "0.1.0", path = "../client" } - -keyring-search = "1.2.0" -keyring-lib = { version = "1.0.2", features = ["tokio", "derive"] } diff --git a/crates/ui/src/utils.rs b/crates/ui/src/utils.rs index f9b5f1b..2fbcc0c 100644 --- a/crates/ui/src/utils.rs +++ b/crates/ui/src/utils.rs @@ -4,7 +4,7 @@ use std::collections::HashSet; pub fn get_all_accounts_from_keyring() -> HashSet { let search = Search::new().expect("Keyring not working."); - let results = search.by_service("coop"); + let results = search.by_service("Coop Safe Storage"); let list = List::list_credentials(&results, Limit::All); let accounts: HashSet = list .split_whitespace() diff --git a/crates/ui/src/views/setup.rs b/crates/ui/src/views/setup.rs index 31efcab..e5a3b77 100644 --- a/crates/ui/src/views/setup.rs +++ b/crates/ui/src/views/setup.rs @@ -1,3 +1,4 @@ +use ::client::NostrClient; use components::{ input::{InputEvent, TextInput}, label::Label, @@ -23,9 +24,13 @@ impl SetupView { if let InputEvent::PressEnter = input_event { let content = text_input.read(cx).text().to_string(); - if let Ok(public_key) = PublicKey::parse(content) { - cx.global_mut::().accounts.insert(public_key); - cx.notify(); + if let Ok(keys) = Keys::parse(content) { + let public_key = keys.public_key(); + + if cx.global::().add_account(keys).is_ok() { + cx.global_mut::().accounts.insert(public_key); + cx.notify(); + } }; } })