feat: add check active account before init client

This commit is contained in:
2024-02-05 13:36:10 +07:00
parent 2a58326cd1
commit 08fa7de01d
9 changed files with 85 additions and 76 deletions

View File

@@ -4,15 +4,20 @@
)]
pub mod commands;
pub mod db;
pub mod nostr;
use crate::db::api::v1::AccountKey;
use crate::db::DATABASE_BUILDER;
use db::api::v1::Account;
use keyring::Entry;
use nostr_sdk::prelude::*;
use std::sync::Arc;
use tauri::Manager;
use tauri_plugin_autostart::MacosLauncher;
pub struct AppState {
pub nostr: Arc<Client>,
pub struct Nostr {
pub client: Arc<Client>,
}
fn main() {
@@ -21,6 +26,16 @@ fn main() {
.setup(|app| {
let handle = app.handle().clone();
let config_dir = app.path().app_config_dir().unwrap();
let db = DATABASE_BUILDER
.create(config_dir.join("app.db"))
.expect("failed to create app database");
// run db migrate
let rw = db
.rw_transaction()
.expect("failed to create rw migration transaction");
rw.migrate::<Account>().expect("failed to migrate Account");
rw.commit().expect("failed to commit migration");
tauri::async_runtime::spawn(async move {
// Create database connection
@@ -31,8 +46,30 @@ fn main() {
// Create nostr connection
let client = ClientBuilder::default().database(nostr_db).build();
// get stored account
let r = db.r_transaction().expect("failed to create ro transaction");
let accounts: Vec<Account> = r
.scan()
.secondary(AccountKey::status)
.expect("failed to scan accounts")
.start_with("active")
.collect();
if let Some(account) = accounts.into_iter().nth(0) {
let entry = Entry::new("Lume", &account.pubkey).expect("failed to load secret");
if let Ok(key) = entry.get_password() {
let secret_key = SecretKey::from_bech32(key).unwrap();
let keys = Keys::new(secret_key);
let signer = ClientSigner::Keys(keys);
// update client's signer
client.set_signer(Some(signer)).await;
}
}
// Add some bootstrap relays
// #TODO: Add option to user config bootstrap relay
// #TODO: Pull bootstrap relays from user's settings
client
.add_relay("wss://nostr.mutinywallet.com")
.await
@@ -46,8 +83,8 @@ fn main() {
client.connect().await;
// Init global state
handle.manage(AppState {
nostr: client.into(),
handle.manage(Nostr {
client: client.into(),
})
});