wip: multi accounts

This commit is contained in:
2024-02-19 09:58:27 +07:00
parent 0f06522c28
commit bfe35ad885
13 changed files with 537 additions and 465 deletions

View File

@@ -1,68 +1,85 @@
use crate::Nostr;
use crate::NostrClient;
use nostr_sdk::prelude::*;
use std::{str::FromStr, time::Duration};
use tauri::State;
#[tauri::command]
pub async fn get_event(id: &str, nostr: State<'_, Nostr>) -> Result<String, String> {
let client = &nostr.client;
let event_id: EventId = match Nip19::from_bech32(id) {
pub async fn get_event(id: &str, state: State<'_, NostrClient>) -> Result<String, String> {
let client = state.0.lock().await;
let event_id: Option<EventId> = match Nip19::from_bech32(id) {
Ok(val) => match val {
Nip19::EventId(id) => id,
Nip19::Event(event) => event.event_id,
_ => panic!("not nip19"),
Nip19::EventId(id) => Some(id),
Nip19::Event(event) => Some(event.event_id),
_ => None,
},
Err(_) => match EventId::from_hex(id) {
Ok(val) => Some(val),
Err(_) => None,
},
Err(_) => EventId::from_hex(id).unwrap(),
};
let filter = Filter::new().id(event_id);
let events = client
.get_events_of(vec![filter], Some(Duration::from_secs(10)))
.await
.expect("Get event failed");
if let Some(id) = event_id {
let filter = Filter::new().id(id);
if let Some(event) = events.first() {
Ok(event.as_json())
if let Ok(events) = &client
.get_events_of(vec![filter], Some(Duration::from_secs(10)))
.await
{
if let Some(event) = events.first() {
Ok(event.as_json())
} else {
Err("Event not found with current relay list".into())
}
} else {
Err("Event not found with current relay list".into())
}
} else {
Err("Event not found".into())
Err("EventId is not valid".into())
}
}
#[tauri::command]
pub async fn get_text_events(
limit: usize,
until: Option<String>,
nostr: State<'_, Nostr>,
until: Option<&str>,
contact_list: Option<Vec<&str>>,
state: State<'_, NostrClient>,
) -> Result<Vec<Event>, ()> {
let client = &nostr.client;
let contact_list = &nostr.contact_list.clone().unwrap();
let client = state.0.lock().await;
let authors: Vec<PublicKey> = contact_list.into_iter().map(|x| x.public_key).collect();
let mut final_until = Timestamp::now();
if let Some(list) = contact_list {
let authors: Vec<PublicKey> = list
.into_iter()
.map(|x| PublicKey::from_str(x).unwrap())
.collect();
let mut final_until = Timestamp::now();
if let Some(t) = until {
final_until = Timestamp::from_str(&t).unwrap();
}
if let Some(t) = until {
final_until = Timestamp::from_str(&t).unwrap();
}
let filter = Filter::new()
.kinds(vec![Kind::TextNote, Kind::Repost])
.authors(authors)
.limit(limit)
.until(final_until);
let filter = Filter::new()
.kinds(vec![Kind::TextNote, Kind::Repost])
.authors(authors)
.limit(limit)
.until(final_until);
if let Ok(events) = client
.get_events_of(vec![filter], Some(Duration::from_secs(10)))
.await
{
Ok(events)
if let Ok(events) = client
.get_events_of(vec![filter], Some(Duration::from_secs(10)))
.await
{
Ok(events)
} else {
Err(())
}
} else {
Err(())
}
}
#[tauri::command]
pub async fn get_event_thread(id: &str, nostr: State<'_, Nostr>) -> Result<Vec<Event>, ()> {
let client = &nostr.client;
pub async fn get_event_thread(id: &str, state: State<'_, NostrClient>) -> Result<Vec<Event>, ()> {
let client = state.0.lock().await;
let event_id = EventId::from_hex(id).unwrap();
let filter = Filter::new().kinds(vec![Kind::TextNote]).event(event_id);
@@ -77,9 +94,8 @@ pub async fn get_event_thread(id: &str, nostr: State<'_, Nostr>) -> Result<Vec<E
}
#[tauri::command]
pub async fn publish(content: &str, nostr: State<'_, Nostr>) -> Result<EventId, ()> {
let client = &nostr.client;
pub async fn publish(content: &str, state: State<'_, NostrClient>) -> Result<EventId, ()> {
let client = state.0.lock().await;
let event = client
.publish_text_note(content, [])
.await
@@ -92,10 +108,9 @@ pub async fn publish(content: &str, nostr: State<'_, Nostr>) -> Result<EventId,
pub async fn reply_to(
content: &str,
tags: Vec<String>,
nostr: State<'_, Nostr>,
state: State<'_, NostrClient>,
) -> Result<EventId, String> {
let client = &nostr.client;
let client = state.0.lock().await;
if let Ok(event_tags) = Tag::parse(tags) {
let event = client
.publish_text_note(content, vec![event_tags])
@@ -109,8 +124,8 @@ pub async fn reply_to(
}
#[tauri::command]
pub async fn repost(id: &str, pubkey: &str, nostr: State<'_, Nostr>) -> Result<EventId, ()> {
let client = &nostr.client;
pub async fn repost(id: &str, pubkey: &str, state: State<'_, NostrClient>) -> Result<EventId, ()> {
let client = state.0.lock().await;
let public_key = PublicKey::from_str(pubkey).unwrap();
let event_id = EventId::from_hex(id).unwrap();
@@ -123,8 +138,8 @@ pub async fn repost(id: &str, pubkey: &str, nostr: State<'_, Nostr>) -> Result<E
}
#[tauri::command]
pub async fn upvote(id: &str, pubkey: &str, nostr: State<'_, Nostr>) -> Result<EventId, ()> {
let client = &nostr.client;
pub async fn upvote(id: &str, pubkey: &str, state: State<'_, NostrClient>) -> Result<EventId, ()> {
let client = state.0.lock().await;
let public_key = PublicKey::from_str(pubkey).unwrap();
let event_id = EventId::from_hex(id).unwrap();
@@ -137,8 +152,12 @@ pub async fn upvote(id: &str, pubkey: &str, nostr: State<'_, Nostr>) -> Result<E
}
#[tauri::command]
pub async fn downvote(id: &str, pubkey: &str, nostr: State<'_, Nostr>) -> Result<EventId, ()> {
let client = &nostr.client;
pub async fn downvote(
id: &str,
pubkey: &str,
state: State<'_, NostrClient>,
) -> Result<EventId, ()> {
let client = state.0.lock().await;
let public_key = PublicKey::from_str(pubkey).unwrap();
let event_id = EventId::from_hex(id).unwrap();

View File

@@ -1,6 +1,9 @@
use crate::Nostr;
use crate::NostrClient;
use keyring::Entry;
use nostr_sdk::prelude::*;
use std::io::{BufReader, Read};
use std::iter;
use std::time::Duration;
use std::{fs::File, io::Write, str::FromStr};
use tauri::{Manager, State};
@@ -28,7 +31,7 @@ pub fn create_keys() -> Result<CreateKeysResponse, ()> {
pub async fn save_key(
nsec: &str,
app_handle: tauri::AppHandle,
nostr: State<'_, Nostr>,
state: State<'_, NostrClient>,
) -> Result<bool, ()> {
if let Ok(nostr_secret_key) = SecretKey::from_bech32(nsec) {
let nostr_keys = Keys::new(nostr_secret_key);
@@ -36,7 +39,7 @@ pub async fn save_key(
let signer = NostrSigner::Keys(nostr_keys);
// Update client's signer
let client = &nostr.client;
let client = state.0.lock().await;
client.set_signer(Some(signer)).await;
let keyring_entry = Entry::new("Lume Secret Storage", "AppKey").unwrap();
@@ -78,8 +81,8 @@ pub fn get_public_key(nsec: &str) -> Result<String, ()> {
}
#[tauri::command]
pub async fn update_signer(nsec: &str, nostr: State<'_, Nostr>) -> Result<(), ()> {
let client = &nostr.client;
pub async fn update_signer(nsec: &str, state: State<'_, NostrClient>) -> Result<(), ()> {
let client = state.0.lock().await;
let secret_key = SecretKey::from_bech32(nsec).unwrap();
let keys = Keys::new(secret_key);
let signer = NostrSigner::Keys(keys);
@@ -90,8 +93,8 @@ pub async fn update_signer(nsec: &str, nostr: State<'_, Nostr>) -> Result<(), ()
}
#[tauri::command]
pub async fn verify_signer(nostr: State<'_, Nostr>) -> Result<bool, ()> {
let client = &nostr.client;
pub async fn verify_signer(state: State<'_, NostrClient>) -> Result<bool, ()> {
let client = state.0.lock().await;
if let Ok(_) = client.signer().await {
Ok(true)
@@ -101,13 +104,61 @@ pub async fn verify_signer(nostr: State<'_, Nostr>) -> Result<bool, ()> {
}
#[tauri::command]
pub fn load_account(nostr: State<'_, Nostr>) -> Result<String, ()> {
let user = &nostr.client_user;
pub async fn load_selected_account(
npub: &str,
app_handle: tauri::AppHandle,
state: State<'_, NostrClient>,
) -> Result<bool, String> {
let client = state.0.lock().await;
let config_dir = app_handle.path().app_config_dir().unwrap();
let keyring_entry = Entry::new("Lume Secret Storage", "AppKey").unwrap();
if let Some(key) = user {
Ok(key.to_hex())
// Get master password
if let Ok(key) = keyring_entry.get_password() {
// Build master key
let app_key = age::x25519::Identity::from_str(&key.to_string()).unwrap();
// Open nsec file
if let Ok(file) = File::open(config_dir.join(npub)) {
let file_buf = BufReader::new(file);
let decryptor = match age::Decryptor::new_buffered(file_buf).expect("Decryptor failed") {
age::Decryptor::Recipients(d) => d,
_ => unreachable!(),
};
let mut decrypted = vec![];
let mut reader = decryptor
.decrypt(iter::once(&app_key as &dyn age::Identity))
.expect("Decrypt nsec file failed");
reader
.read_to_end(&mut decrypted)
.expect("Read secret key failed");
// Get decrypted nsec key
let nsec_key = String::from_utf8(decrypted).unwrap();
// Build nostr signer
let secret_key = SecretKey::from_bech32(nsec_key).expect("Get secret key failed");
let keys = Keys::new(secret_key);
let signer = NostrSigner::Keys(keys);
// Update signer
client.set_signer(Some(signer)).await;
// Update contact list
let _contact_list = Some(
client
.get_contact_list(Some(Duration::from_secs(10)))
.await
.unwrap(),
);
Ok(true)
} else {
Ok(false)
}
} else {
Err(())
Err("App Key not found".into())
}
}

View File

@@ -1,35 +1,52 @@
use crate::Nostr;
use crate::NostrClient;
use nostr_sdk::prelude::*;
use std::{str::FromStr, time::Duration};
use tauri::State;
#[tauri::command]
pub async fn get_profile(id: &str, nostr: State<'_, Nostr>) -> Result<Metadata, ()> {
let client = &nostr.client;
let public_key: PublicKey = match Nip19::from_bech32(id) {
pub async fn get_profile(id: &str, state: State<'_, NostrClient>) -> Result<Metadata, String> {
let client = state.0.lock().await;
let public_key: Option<PublicKey> = match Nip19::from_bech32(id) {
Ok(val) => match val {
Nip19::Pubkey(pubkey) => pubkey,
Nip19::Profile(profile) => profile.public_key,
_ => panic!("not nip19"),
Nip19::Pubkey(pubkey) => Some(pubkey),
Nip19::Profile(profile) => Some(profile.public_key),
_ => None,
},
Err(_) => match PublicKey::from_str(id) {
Ok(val) => Some(val),
Err(_) => None,
},
Err(_) => PublicKey::from_str(id).unwrap(),
};
let filter = Filter::new()
.author(public_key)
.kind(Kind::Metadata)
.limit(1);
if let Some(author) = public_key {
let filter = Filter::new().author(author).kind(Kind::Metadata).limit(1);
let events = client
.get_events_of(vec![filter], Some(Duration::from_secs(10)))
.await
.expect("Get metadata failed");
let events = client
.get_events_of(vec![filter], Some(Duration::from_secs(10)))
.await
.expect("Get metadata failed");
if let Some(event) = events.first() {
let metadata: Metadata = Metadata::from_json(&event.content).unwrap();
Ok(metadata)
if let Some(event) = events.first() {
let metadata: Metadata = Metadata::from_json(&event.content).unwrap();
Ok(metadata)
} else {
let rand_metadata = Metadata::new();
Ok(rand_metadata)
}
} else {
Err(())
Err("Public Key is not valid".into())
}
}
#[tauri::command]
pub async fn get_contact_list(state: State<'_, NostrClient>) -> Result<Vec<String>, String> {
let client = state.0.lock().await;
let contact_list = client.get_contact_list(Some(Duration::from_secs(10))).await;
if let Ok(list) = contact_list {
let v = list.into_iter().map(|f| f.public_key.to_hex()).collect();
Ok(v)
} else {
Err("Contact list not found".into())
}
}
@@ -43,10 +60,9 @@ pub async fn create_profile(
nip05: &str,
lud16: &str,
website: &str,
nostr: State<'_, Nostr>,
state: State<'_, NostrClient>,
) -> Result<EventId, ()> {
let client = &nostr.client;
let client = state.0.lock().await;
let metadata = Metadata::new()
.name(name)
.display_name(display_name)

View File

@@ -1,10 +1,10 @@
use crate::Nostr;
use crate::NostrClient;
use nostr_sdk::prelude::*;
use tauri::State;
#[tauri::command]
pub async fn list_connected_relays(nostr: State<'_, Nostr>) -> Result<Vec<Url>, ()> {
let client = &nostr.client;
pub async fn list_connected_relays(state: State<'_, NostrClient>) -> Result<Vec<Url>, ()> {
let client = state.0.lock().await;
let relays = client.relays().await;
let list: Vec<Url> = relays.into_keys().collect();
@@ -12,8 +12,8 @@ pub async fn list_connected_relays(nostr: State<'_, Nostr>) -> Result<Vec<Url>,
}
#[tauri::command]
pub async fn connect_relay(relay: &str, nostr: State<'_, Nostr>) -> Result<bool, ()> {
let client = &nostr.client;
pub async fn connect_relay(relay: &str, state: State<'_, NostrClient>) -> Result<bool, ()> {
let client = state.0.lock().await;
if let Ok(_) = client.add_relay(relay).await {
Ok(true)
} else {
@@ -22,8 +22,8 @@ pub async fn connect_relay(relay: &str, nostr: State<'_, Nostr>) -> Result<bool,
}
#[tauri::command]
pub async fn remove_relay(relay: &str, nostr: State<'_, Nostr>) -> Result<bool, ()> {
let client = &nostr.client;
pub async fn remove_relay(relay: &str, state: State<'_, NostrClient>) -> Result<bool, ()> {
let client = state.0.lock().await;
if let Ok(_) = client.remove_relay(relay).await {
Ok(true)
} else {