diff --git a/Cargo.toml b/Cargo.toml index f71cbe1..b9cb018 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,14 +6,6 @@ resolver = "2" [workspace.dependencies] coop = { path = "crates/*" } -# Account -keyring-search = "1.2.0" -keyring = { version = "3", features = [ - "apple-native", - "windows-native", - "linux-native", -] } - # UI gpui = { git = "https://github.com/zed-industries/zed" } components = { package = "ui", git = "https://github.com/longbridgeapp/gpui-component", version = "0.1.0" } diff --git a/crates/nostr/Cargo.toml b/crates/nostr/Cargo.toml index ae340bd..0a03c66 100644 --- a/crates/nostr/Cargo.toml +++ b/crates/nostr/Cargo.toml @@ -4,6 +4,10 @@ version = "0.1.0" edition = "2021" [dependencies] +gpui.workspace = true nostr-sdk.workspace = true dirs.workspace = true tokio.workspace = true + +keyring-search = "1.2.0" +keyring-lib = { version = "1.0.2", features = ["tokio", "derive"] } diff --git a/crates/nostr/src/lib.rs b/crates/nostr/src/lib.rs index 266c62a..aa28fc0 100644 --- a/crates/nostr/src/lib.rs +++ b/crates/nostr/src/lib.rs @@ -1 +1,39 @@ +use gpui::Global; +use keyring_search::{Limit, List, Search}; +use nostr_sdk::prelude::*; +use state::get_client; +use std::collections::HashSet; + pub mod state; + +pub struct Nostr { + pub client: &'static Client, + pub accounts: HashSet, +} + +// Set Nostr as Global State +impl Global for Nostr {} + +impl Nostr { + pub async fn init() -> Self { + // Initialize nostr client + let client = get_client().await; + // Get all accounts + let accounts = Self::get_accounts(); + + Self { client, accounts } + } + + pub fn get_accounts() -> HashSet { + let search = Search::new().expect("Keyring not working."); + let results = search.by_service("coop"); + let list = List::list_credentials(&results, Limit::All); + let accounts: HashSet = list + .split_whitespace() + .filter(|v| v.starts_with("npub1") && !v.ends_with("coop")) + .filter_map(|i| PublicKey::from_bech32(i).ok()) + .collect(); + + accounts + } +} diff --git a/crates/ui/src/main.rs b/crates/ui/src/main.rs index 8a74505..d0370e8 100644 --- a/crates/ui/src/main.rs +++ b/crates/ui/src/main.rs @@ -1,5 +1,5 @@ use gpui::*; -use nostr::state::get_client; +use nostr::Nostr; struct HelloWorld { text: SharedString, @@ -19,10 +19,13 @@ impl Render for HelloWorld { #[tokio::main] async fn main() { - let _client = get_client().await; + let nostr = Nostr::init().await; App::new().run(|cx: &mut AppContext| { + // Set window size let bounds = Bounds::centered(None, size(px(860.0), px(650.0)), cx); + // Set global state + cx.set_global(nostr); cx.open_window( WindowOptions {