feat: auto login on startup
This commit is contained in:
@@ -12,8 +12,8 @@ components = { package = "ui", git = "https://github.com/longbridgeapp/gpui-comp
|
||||
reqwest_client = { git = "https://github.com/huacnlee/zed.git", branch = "export-platform-window" }
|
||||
|
||||
# Nostr
|
||||
nostr-relay-builder = { git = "https://github.com/rust-nostr/nostr", branch = "nip17" }
|
||||
nostr-sdk = { git = "https://github.com/rust-nostr/nostr", branch = "nip17", features = [
|
||||
nostr-relay-builder = { git = "https://github.com/rust-nostr/nostr" }
|
||||
nostr-sdk = { git = "https://github.com/rust-nostr/nostr", features = [
|
||||
"lmdb",
|
||||
"all-nips",
|
||||
] }
|
||||
|
||||
@@ -23,7 +23,7 @@ pub async fn get_client() -> &'static Client {
|
||||
// Add some bootstrap relays
|
||||
let _ = client.add_relay("wss://relay.damus.io").await;
|
||||
let _ = client.add_relay("wss://relay.primal.net").await;
|
||||
let _ = client.add_relay("wss://nostr.fmt.wiz.biz").await;
|
||||
let _ = client.add_relay("wss://nos.lol").await;
|
||||
let _ = client.add_relay("wss://directory.yabu.me").await;
|
||||
|
||||
let _ = client.add_discovery_relay("wss://user.kindpag.es/").await;
|
||||
|
||||
2
crates/ui/src/constants.rs
Normal file
2
crates/ui/src/constants.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub const KEYRING_SERVICE: &str = "Coop Safe Storage";
|
||||
pub const APP_NAME: &str = "coop";
|
||||
@@ -1,12 +1,17 @@
|
||||
use asset::Assets;
|
||||
use client::NostrClient;
|
||||
use components::theme::{Theme, ThemeColor, ThemeMode};
|
||||
use constants::{APP_NAME, KEYRING_SERVICE};
|
||||
use gpui::*;
|
||||
use keyring::Entry;
|
||||
use nostr_sdk::prelude::*;
|
||||
use state::AppState;
|
||||
use std::sync::Arc;
|
||||
use utils::get_all_accounts_from_keyring;
|
||||
use views::app::AppView;
|
||||
|
||||
pub mod asset;
|
||||
pub mod constants;
|
||||
pub mod state;
|
||||
pub mod utils;
|
||||
pub mod views;
|
||||
@@ -45,6 +50,33 @@ async fn main() {
|
||||
// Refresh
|
||||
cx.refresh();
|
||||
|
||||
// Login
|
||||
let async_cx = cx.to_async();
|
||||
cx.foreground_executor()
|
||||
.spawn(async move {
|
||||
let accounts = get_all_accounts_from_keyring();
|
||||
|
||||
if let Some(account) = accounts.first() {
|
||||
let client = async_cx
|
||||
.read_global(|nostr: &NostrClient, _cx| nostr.client)
|
||||
.unwrap();
|
||||
let entry =
|
||||
Entry::new(KEYRING_SERVICE, account.to_bech32().unwrap().as_ref())
|
||||
.unwrap();
|
||||
let password = entry.get_password().unwrap();
|
||||
let keys = Keys::parse(password).unwrap();
|
||||
|
||||
client.set_signer(keys).await;
|
||||
|
||||
async_cx
|
||||
.update_global(|app_state: &mut AppState, _cx| {
|
||||
app_state.signer = Some(*account);
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
// Set window size
|
||||
let bounds = Bounds::centered(None, size(px(860.0), px(650.0)), cx);
|
||||
|
||||
@@ -53,7 +85,7 @@ async fn main() {
|
||||
window_bounds: Some(WindowBounds::Windowed(bounds)),
|
||||
window_decorations: Some(WindowDecorations::Client),
|
||||
titlebar: Some(TitlebarOptions {
|
||||
title: Some(SharedString::new_static("coop")),
|
||||
title: Some(SharedString::new_static(APP_NAME)),
|
||||
appears_transparent: true,
|
||||
..Default::default()
|
||||
}),
|
||||
|
||||
@@ -3,6 +3,7 @@ use nostr_sdk::prelude::*;
|
||||
|
||||
pub struct AppState {
|
||||
pub signer: Option<PublicKey>,
|
||||
// TODO: add more app state
|
||||
}
|
||||
|
||||
impl Global for AppState {}
|
||||
|
||||
@@ -7,7 +7,7 @@ use gpui::*;
|
||||
use keyring::Entry;
|
||||
use nostr_sdk::prelude::*;
|
||||
|
||||
use crate::state::AppState;
|
||||
use crate::{constants::KEYRING_SERVICE, state::AppState};
|
||||
|
||||
pub struct Onboarding {
|
||||
input: View<TextInput>,
|
||||
@@ -36,7 +36,7 @@ impl Onboarding {
|
||||
let secret = keys.secret_key().to_secret_hex();
|
||||
|
||||
let entry =
|
||||
Entry::new("Coop Safe Storage", &public_key.to_bech32().unwrap())
|
||||
Entry::new(KEYRING_SERVICE, &public_key.to_bech32().unwrap())
|
||||
.unwrap();
|
||||
|
||||
// Store private key to OS Keyring
|
||||
|
||||
Reference in New Issue
Block a user