feat: add check active account before init client
This commit is contained in:
29
src-tauri/src/db.rs
Normal file
29
src-tauri/src/db.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
pub(crate) mod api {
|
||||
use native_db::{native_db, InnerKeyValue};
|
||||
use native_model::{native_model, Model};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub mod v1 {
|
||||
use super::*;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[native_model(id = 1, version = 2)]
|
||||
#[native_db]
|
||||
pub struct Account {
|
||||
#[primary_key]
|
||||
pub pubkey: String,
|
||||
#[secondary_key]
|
||||
status: String,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub static DATABASE_BUILDER: Lazy<native_db::DatabaseBuilder> = Lazy::new(|| {
|
||||
let mut builder = native_db::DatabaseBuilder::new();
|
||||
builder
|
||||
.define::<api::v1::Account>()
|
||||
.expect("failed to define model Account v1");
|
||||
builder
|
||||
});
|
||||
@@ -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(),
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use crate::AppState;
|
||||
use crate::Nostr;
|
||||
use nostr_sdk::prelude::*;
|
||||
use std::time::Duration;
|
||||
use tauri::State;
|
||||
|
||||
#[tauri::command(async)]
|
||||
pub async fn get_event(id: String, app_state: State<'_, AppState>) -> Result<String, ()> {
|
||||
let client = &app_state.nostr;
|
||||
pub async fn get_event(id: String, nostr: State<'_, Nostr>) -> Result<String, ()> {
|
||||
let client = &nostr.client;
|
||||
|
||||
let event_id = EventId::from_bech32(id).unwrap();
|
||||
let filter = Filter::new().id(event_id);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::AppState;
|
||||
use crate::Nostr;
|
||||
use nostr_sdk::prelude::*;
|
||||
use tauri::State;
|
||||
|
||||
@@ -29,8 +29,8 @@ pub fn get_public_key(secret_key: SecretKey) -> Result<String, ()> {
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn update_signer(key: String, app_state: State<'_, AppState>) -> Result<(), ()> {
|
||||
let client = &app_state.nostr;
|
||||
pub async fn update_signer(key: String, nostr: State<'_, Nostr>) -> Result<(), ()> {
|
||||
let client = &nostr.client;
|
||||
let secret_key = SecretKey::from_bech32(key).unwrap();
|
||||
let keys = Keys::new(secret_key);
|
||||
let signer = ClientSigner::Keys(keys);
|
||||
@@ -41,8 +41,8 @@ pub async fn update_signer(key: String, app_state: State<'_, AppState>) -> Resul
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn verify_signer(app_state: State<'_, AppState>) -> Result<bool, ()> {
|
||||
let client = &app_state.nostr;
|
||||
pub async fn verify_signer(nostr: State<'_, Nostr>) -> Result<bool, ()> {
|
||||
let client = &nostr.client;
|
||||
let status = client.signer().await.is_ok();
|
||||
|
||||
Ok(status)
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use crate::AppState;
|
||||
use crate::Nostr;
|
||||
use nostr_sdk::prelude::*;
|
||||
use std::time::Duration;
|
||||
use tauri::State;
|
||||
|
||||
#[tauri::command(async)]
|
||||
pub async fn get_metadata(npub: String, app_state: State<'_, AppState>) -> Result<Metadata, ()> {
|
||||
let client = &app_state.nostr;
|
||||
pub async fn get_metadata(npub: String, nostr: State<'_, Nostr>) -> Result<Metadata, ()> {
|
||||
let client = &nostr.client;
|
||||
|
||||
let public_key = XOnlyPublicKey::from_bech32(npub).unwrap();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user