feat: add backup dialog

This commit is contained in:
2024-03-06 14:40:00 +07:00
parent 95294a80cb
commit 71a2290b8f
5 changed files with 121 additions and 51 deletions

View File

@@ -49,10 +49,10 @@ pub async fn show_in_folder(path: String) {
}
#[tauri::command]
pub fn get_all_nsecs(app_handle: tauri::AppHandle) -> Result<Vec<String>, ()> {
let dir = app_handle.path().app_config_dir().unwrap();
pub fn get_accounts(app_handle: tauri::AppHandle) -> Result<Vec<String>, ()> {
let dir = app_handle.path().home_dir().unwrap();
if let Ok(paths) = std::fs::read_dir(dir) {
if let Ok(paths) = std::fs::read_dir(dir.join("Lume/")) {
let files = paths
.filter_map(|res| res.ok())
.map(|dir_entry| dir_entry.path())

View File

@@ -7,6 +7,8 @@ pub mod commands;
pub mod nostr;
pub mod tray;
use std::fs;
use nostr_sdk::prelude::*;
use tauri::Manager;
use tauri_plugin_autostart::MacosLauncher;
@@ -20,11 +22,15 @@ fn main() {
.setup(|app| {
let _tray = tray::create_tray(app.handle()).unwrap();
let handle = app.handle().clone();
let config_dir = handle.path().app_config_dir().unwrap();
let resource_dir = handle.path().resource_dir().unwrap();
let home_dir = handle.path().home_dir().unwrap();
// create data folder if not exist
fs::create_dir_all(home_dir.join("Lume/")).unwrap();
tauri::async_runtime::spawn(async move {
// Create nostr database connection
let nostr_db = SQLiteDatabase::open(config_dir.join("nostr.db"))
let nostr_db = SQLiteDatabase::open(resource_dir.join("lume.db"))
.await
.expect("Open database failed.");
@@ -79,6 +85,7 @@ fn main() {
.invoke_handler(tauri::generate_handler![
nostr::keys::create_keys,
nostr::keys::save_key,
nostr::keys::get_encrypted_key,
nostr::keys::verify_signer,
nostr::keys::load_selected_account,
nostr::keys::event_to_bech32,
@@ -108,15 +115,21 @@ fn main() {
nostr::event::upvote,
nostr::event::downvote,
commands::folder::show_in_folder,
commands::folder::get_all_nsecs,
commands::folder::get_accounts,
commands::opg::fetch_opg,
])
.build(tauri::generate_context!())
.expect("error while running tauri application")
.run(|_app_handle, event| match event {
tauri::RunEvent::ExitRequested { api, .. } => {
api.prevent_exit();
.run(|app, event| {
if let tauri::RunEvent::Opened { urls } = event {
if let Some(w) = app.get_webview_window("main") {
let urls = urls
.iter()
.map(|u| u.as_str())
.collect::<Vec<_>>()
.join(",");
let _ = w.eval(&format!("window.onFileOpen(`{urls}`)"));
}
}
_ => {}
});
}

View File

@@ -53,13 +53,14 @@ pub async fn save_key(
let npub = nostr_keys.public_key().to_bech32().unwrap();
let nsec = nostr_keys.secret_key().unwrap().to_bech32().unwrap();
if let Ok(config_dir) = app_handle.path().app_config_dir() {
let file_path = npub.clone() + ".npub";
let _ = File::create(config_dir.join(file_path)).unwrap();
let home_dir = app_handle.path().home_dir().unwrap();
let app_dir = home_dir.join("Lume/");
let keyring = Entry::new("Lume Secret Storage", &npub).unwrap();
let _ = keyring.set_password(&nsec);
}
let file_path = npub.clone() + ".npub";
let _ = File::create(app_dir.join(file_path)).unwrap();
let keyring = Entry::new("Lume Secret Storage", &npub).unwrap();
let _ = keyring.set_password(&nsec);
let signer = NostrSigner::Keys(nostr_keys);
let client = &state.client;
@@ -84,6 +85,24 @@ pub async fn verify_signer(state: State<'_, Nostr>) -> Result<bool, ()> {
}
}
#[tauri::command]
pub fn get_encrypted_key(npub: &str, password: &str) -> Result<String, String> {
let keyring = Entry::new("Lume Secret Storage", npub).unwrap();
if let Ok(nsec) = keyring.get_password() {
let secret_key = SecretKey::from_bech32(nsec).expect("Get secret key failed");
let new_key = EncryptedSecretKey::new(&secret_key, password, 16, KeySecurity::Medium);
if let Ok(key) = new_key {
Ok(key.to_bech32().unwrap())
} else {
Err("Encrypt key failed".into())
}
} else {
Err("Key not found".into())
}
}
#[tauri::command]
pub async fn load_selected_account(npub: &str, state: State<'_, Nostr>) -> Result<bool, String> {
let client = &state.client;