feat: add login dialog

This commit is contained in:
2024-03-06 09:42:44 +07:00
parent 86183d799a
commit 8eaf47f6d2
17 changed files with 336 additions and 167 deletions

View File

@@ -89,7 +89,6 @@ fn main() {
.invoke_handler(tauri::generate_handler![
nostr::keys::create_keys,
nostr::keys::save_key,
nostr::keys::get_public_key,
nostr::keys::update_signer,
nostr::keys::verify_signer,
nostr::keys::load_selected_account,

View File

@@ -30,54 +30,58 @@ pub fn create_keys() -> Result<CreateKeysResponse, ()> {
#[tauri::command]
pub async fn save_key(
nsec: &str,
password: &str,
app_handle: tauri::AppHandle,
state: State<'_, Nostr>,
) -> Result<bool, ()> {
if let Ok(nostr_secret_key) = SecretKey::from_bech32(nsec) {
let nostr_keys = Keys::new(nostr_secret_key);
let nostr_npub = nostr_keys.public_key().to_bech32().unwrap();
let signer = NostrSigner::Keys(nostr_keys);
) -> Result<bool, String> {
let secret_key: Result<SecretKey, String>;
// Update client's signer
let client = &state.client;
client.set_signer(Some(signer)).await;
let keyring_entry = Entry::new("Lume Secret Storage", "AppKey").unwrap();
let secret_key = keyring_entry.get_password().unwrap();
let app_key = age::x25519::Identity::from_str(&secret_key).unwrap();
let app_pubkey = app_key.to_public();
let config_dir = app_handle.path().app_config_dir().unwrap();
let encryptor =
age::Encryptor::with_recipients(vec![Box::new(app_pubkey)]).expect("we provided a recipient");
let file_ext = ".nsec".to_owned();
let file_path = nostr_npub + &file_ext;
let mut file = File::create(config_dir.join(file_path)).unwrap();
let mut writer = encryptor
.wrap_output(&mut file)
.expect("Init writer failed");
writer
.write_all(nsec.as_bytes())
.expect("Write nsec failed");
writer.finish().expect("Save nsec failed");
Ok(true)
if nsec.starts_with("ncrypto") {
let encrypted_key = EncryptedSecretKey::from_bech32(nsec).unwrap();
secret_key = match encrypted_key.to_secret_key(password) {
Ok(val) => Ok(val),
Err(_) => Err("Wrong passphase".into()),
};
} else {
Ok(false)
secret_key = match SecretKey::from_bech32(nsec) {
Ok(val) => Ok(val),
Err(_) => Err("nsec is not valid".into()),
}
}
}
#[tauri::command]
pub fn get_public_key(nsec: &str) -> Result<String, ()> {
let secret_key = SecretKey::from_bech32(nsec).unwrap();
let keys = Keys::new(secret_key);
Ok(
keys
.public_key()
.to_bech32()
.expect("get public key failed"),
)
match secret_key {
Ok(val) => {
let nostr_keys = Keys::new(val);
let nostr_npub = nostr_keys.public_key().to_bech32().unwrap();
let signer = NostrSigner::Keys(nostr_keys);
// Update client's signer
let client = &state.client;
client.set_signer(Some(signer)).await;
let keyring_entry = Entry::new("Lume Secret Storage", "AppKey").unwrap();
let master_key = keyring_entry.get_password().unwrap();
let app_key = age::x25519::Identity::from_str(&master_key).unwrap();
let app_pubkey = app_key.to_public();
let config_dir = app_handle.path().app_config_dir().unwrap();
let encryptor = age::Encryptor::with_recipients(vec![Box::new(app_pubkey)])
.expect("we provided a recipient");
let file_path = nostr_npub + ".nsec";
let mut file = File::create(config_dir.join(file_path)).unwrap();
let mut writer = encryptor
.wrap_output(&mut file)
.expect("Init writer failed");
writer
.write_all(nsec.as_bytes())
.expect("Write nsec failed");
writer.finish().expect("Save nsec failed");
Ok(true)
}
Err(msg) => Err(msg.into()),
}
}
#[tauri::command]

View File

@@ -1,22 +1,12 @@
use tauri::{tray::ClickType, Manager, Runtime};
pub fn create_tray<R: Runtime>(app: &tauri::AppHandle<R>) -> tauri::Result<()> {
let tray = app.tray().unwrap();
let menu = tauri::menu::MenuBuilder::new(app)
.item(&tauri::menu::MenuItem::with_id(app, "quit", "Quit", true, None::<&str>).unwrap())
.build()
.unwrap();
let tray = tauri::tray::TrayIconBuilder::with_id("main_tray")
.tooltip("Lume")
.icon(tauri::Icon::Rgba {
rgba: include_bytes!("../icons/icon.png").to_vec(),
width: 500,
height: 500,
})
.icon_as_template(true)
.menu(&menu)
.build(app)
.unwrap();
let _ = tray.set_menu(Some(menu));
tray.on_menu_event(move |app, event| match event.id.0.as_str() {
"quit" => {

View File

@@ -12,6 +12,11 @@
"app": {
"macOSPrivateApi": true,
"withGlobalTauri": true,
"trayIcon": {
"id": "main_tray",
"iconPath": "./icons/tray.png",
"iconAsTemplate": true
},
"security": {
"assetProtocol": {
"enable": true,
@@ -92,7 +97,15 @@
"type": "downloadBootstrapper"
},
"wix": null
}
},
"fileAssociations": [
{
"name": "bech32",
"description": "Nostr Bech32",
"ext": ["nsec", "nprofile", "nevent", "naddr", "nrelay"],
"role": "Viewer"
}
]
},
"plugins": {
"updater": {