feat: upgrade to rust nostr 0.28

This commit is contained in:
2024-02-17 09:29:37 +07:00
parent f28a7ae82f
commit f47eba5af7
16 changed files with 352 additions and 210 deletions

View File

@@ -3,7 +3,7 @@ use nostr_sdk::prelude::*;
use std::{str::FromStr, time::Duration};
use tauri::State;
#[tauri::command(async)]
#[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) {
@@ -28,7 +28,7 @@ pub async fn get_event(id: &str, nostr: State<'_, Nostr>) -> Result<String, Stri
}
}
#[tauri::command(async)]
#[tauri::command]
pub async fn get_text_events(
limit: usize,
until: Option<String>,
@@ -37,7 +37,7 @@ pub async fn get_text_events(
let client = &nostr.client;
let contact_list = &nostr.contact_list.clone().unwrap();
let authors: Vec<XOnlyPublicKey> = contact_list.into_iter().map(|x| x.pk).collect();
let authors: Vec<PublicKey> = contact_list.into_iter().map(|x| x.public_key).collect();
let mut final_until = Timestamp::now();
if let Some(t) = until {
@@ -60,7 +60,7 @@ pub async fn get_text_events(
}
}
#[tauri::command(async)]
#[tauri::command]
pub async fn get_event_thread(id: &str, nostr: State<'_, Nostr>) -> Result<Vec<Event>, ()> {
let client = &nostr.client;
let event_id = EventId::from_hex(id).unwrap();
@@ -76,7 +76,7 @@ pub async fn get_event_thread(id: &str, nostr: State<'_, Nostr>) -> Result<Vec<E
}
}
#[tauri::command(async)]
#[tauri::command]
pub async fn publish(content: &str, nostr: State<'_, Nostr>) -> Result<EventId, ()> {
let client = &nostr.client;
@@ -88,7 +88,7 @@ pub async fn publish(content: &str, nostr: State<'_, Nostr>) -> Result<EventId,
Ok(event)
}
#[tauri::command(async)]
#[tauri::command]
pub async fn reply_to(
content: &str,
tags: Vec<String>,
@@ -108,10 +108,10 @@ pub async fn reply_to(
}
}
#[tauri::command(async)]
#[tauri::command]
pub async fn repost(id: &str, pubkey: &str, nostr: State<'_, Nostr>) -> Result<EventId, ()> {
let client = &nostr.client;
let public_key = XOnlyPublicKey::from_str(pubkey).unwrap();
let public_key = PublicKey::from_str(pubkey).unwrap();
let event_id = EventId::from_hex(id).unwrap();
let event = client
@@ -122,10 +122,10 @@ pub async fn repost(id: &str, pubkey: &str, nostr: State<'_, Nostr>) -> Result<E
Ok(event)
}
#[tauri::command(async)]
#[tauri::command]
pub async fn upvote(id: &str, pubkey: &str, nostr: State<'_, Nostr>) -> Result<EventId, ()> {
let client = &nostr.client;
let public_key = XOnlyPublicKey::from_str(pubkey).unwrap();
let public_key = PublicKey::from_str(pubkey).unwrap();
let event_id = EventId::from_hex(id).unwrap();
let event = client
@@ -136,10 +136,10 @@ pub async fn upvote(id: &str, pubkey: &str, nostr: State<'_, Nostr>) -> Result<E
Ok(event)
}
#[tauri::command(async)]
#[tauri::command]
pub async fn downvote(id: &str, pubkey: &str, nostr: State<'_, Nostr>) -> Result<EventId, ()> {
let client = &nostr.client;
let public_key = XOnlyPublicKey::from_str(pubkey).unwrap();
let public_key = PublicKey::from_str(pubkey).unwrap();
let event_id = EventId::from_hex(id).unwrap();
let event = client

View File

@@ -33,7 +33,7 @@ pub async fn save_key(
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 = ClientSigner::Keys(nostr_keys);
let signer = NostrSigner::Keys(nostr_keys);
// Update client's signer
let client = &nostr.client;
@@ -82,7 +82,7 @@ pub async fn update_signer(nsec: &str, nostr: State<'_, Nostr>) -> Result<(), ()
let client = &nostr.client;
let secret_key = SecretKey::from_bech32(nsec).unwrap();
let keys = Keys::new(secret_key);
let signer = ClientSigner::Keys(keys);
let signer = NostrSigner::Keys(keys);
client.set_signer(Some(signer)).await;
@@ -104,8 +104,8 @@ pub async fn verify_signer(nostr: State<'_, Nostr>) -> Result<bool, ()> {
pub fn load_account(nostr: State<'_, Nostr>) -> Result<String, ()> {
let user = &nostr.client_user;
if let Some(public_key) = user {
Ok(public_key.to_string())
if let Some(key) = user {
Ok(key.to_hex())
} else {
Err(())
}
@@ -121,7 +121,7 @@ pub fn event_to_bech32(id: &str, relays: Vec<String>) -> Result<String, ()> {
#[tauri::command]
pub fn user_to_bech32(key: &str, relays: Vec<String>) -> Result<String, ()> {
let pubkey = XOnlyPublicKey::from_str(key).unwrap();
let pubkey = PublicKey::from_str(key).unwrap();
let profile = Nip19Profile::new(pubkey, relays);
Ok(profile.to_bech32().unwrap())
@@ -129,7 +129,7 @@ pub fn user_to_bech32(key: &str, relays: Vec<String>) -> Result<String, ()> {
#[tauri::command(async)]
pub async fn verify_nip05(key: &str, nip05: &str) -> Result<bool, ()> {
let public_key = XOnlyPublicKey::from_str(key).unwrap();
let public_key = PublicKey::from_str(key).unwrap();
let status = nip05::verify(public_key, nip05, None).await;
if let Ok(_) = status {

View File

@@ -3,16 +3,16 @@ use nostr_sdk::prelude::*;
use std::{str::FromStr, time::Duration};
use tauri::State;
#[tauri::command(async)]
#[tauri::command]
pub async fn get_profile(id: &str, nostr: State<'_, Nostr>) -> Result<Metadata, ()> {
let client = &nostr.client;
let public_key: XOnlyPublicKey = match Nip19::from_bech32(id) {
let public_key: PublicKey = match Nip19::from_bech32(id) {
Ok(val) => match val {
Nip19::Pubkey(pubkey) => pubkey,
Nip19::Profile(profile) => profile.public_key,
_ => panic!("not nip19"),
},
Err(_) => XOnlyPublicKey::from_str(id).unwrap(),
Err(_) => PublicKey::from_str(id).unwrap(),
};
let filter = Filter::new()
@@ -33,7 +33,7 @@ pub async fn get_profile(id: &str, nostr: State<'_, Nostr>) -> Result<Metadata,
}
}
#[tauri::command(async)]
#[tauri::command]
pub async fn create_profile(
name: &str,
display_name: &str,

View File

@@ -2,7 +2,7 @@ use crate::Nostr;
use nostr_sdk::prelude::*;
use tauri::State;
#[tauri::command(async)]
#[tauri::command]
pub async fn list_connected_relays(nostr: State<'_, Nostr>) -> Result<Vec<Url>, ()> {
let client = &nostr.client;
let relays = client.relays().await;
@@ -11,7 +11,7 @@ pub async fn list_connected_relays(nostr: State<'_, Nostr>) -> Result<Vec<Url>,
Ok(list)
}
#[tauri::command(async)]
#[tauri::command]
pub async fn connect_relay(relay: &str, nostr: State<'_, Nostr>) -> Result<bool, ()> {
let client = &nostr.client;
if let Ok(_) = client.add_relay(relay).await {
@@ -21,7 +21,7 @@ pub async fn connect_relay(relay: &str, nostr: State<'_, Nostr>) -> Result<bool,
}
}
#[tauri::command(async)]
#[tauri::command]
pub async fn remove_relay(relay: &str, nostr: State<'_, Nostr>) -> Result<bool, ()> {
let client = &nostr.client;
if let Ok(_) = client.remove_relay(relay).await {