feat: update rust nostr

This commit is contained in:
2024-02-06 19:28:46 +07:00
parent a4069dae99
commit 3c4bd39384
22 changed files with 721 additions and 1114 deletions

View File

@@ -4,17 +4,26 @@ use std::time::Duration;
use tauri::State;
#[tauri::command(async)]
pub async fn get_event(id: String, nostr: State<'_, Nostr>) -> Result<String, ()> {
pub async fn get_event(id: &str, nostr: State<'_, Nostr>) -> Result<String, ()> {
let client = &nostr.client;
let event_id;
if id.starts_with("note1") {
event_id = EventId::from_bech32(id).unwrap();
} else if id.starts_with("nevent1") {
event_id = EventId::from_bech32(id).unwrap();
} else if id.starts_with("naddr1") {
event_id = EventId::from_bech32(id).unwrap();
} else {
event_id = EventId::from_hex(id).unwrap();
}
let event_id = EventId::from_bech32(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 metadata failed");
let event = events.into_iter().nth(0).unwrap().as_json();
.expect("Get event failed");
let event = events.first().unwrap().as_json();
Ok(event)
}

View File

@@ -1,5 +1,6 @@
use crate::Nostr;
use nostr_sdk::prelude::*;
use std::str::FromStr;
use tauri::State;
#[derive(serde::Serialize)]
@@ -23,15 +24,16 @@ pub fn create_keys() -> Result<CreateKeysResponse, ()> {
}
#[tauri::command]
pub fn get_public_key(secret_key: SecretKey) -> Result<String, ()> {
pub fn get_public_key(nsec: String) -> Result<String, ()> {
let secret_key = SecretKey::from_bech32(nsec).unwrap();
let keys = Keys::new(secret_key);
Ok(keys.public_key().to_bech32().expect("secret key failed"))
}
#[tauri::command]
pub async fn update_signer(key: String, nostr: State<'_, Nostr>) -> Result<(), ()> {
pub async fn update_signer(nsec: String, nostr: State<'_, Nostr>) -> Result<(), ()> {
let client = &nostr.client;
let secret_key = SecretKey::from_bech32(key).unwrap();
let secret_key = SecretKey::from_bech32(nsec).unwrap();
let keys = Keys::new(secret_key);
let signer = ClientSigner::Keys(keys);
@@ -47,3 +49,19 @@ pub async fn verify_signer(nostr: State<'_, Nostr>) -> Result<bool, ()> {
Ok(status)
}
#[tauri::command]
pub fn event_to_bech32(id: &str, relays: Vec<String>) -> Result<String, ()> {
let event_id = EventId::from_hex(id).unwrap();
let event = Nip19Event::new(event_id, relays);
Ok(event.to_bech32().unwrap())
}
#[tauri::command]
pub fn user_to_bech32(key: &str, relays: Vec<String>) -> Result<String, ()> {
let pubkey = XOnlyPublicKey::from_str(key).unwrap();
let profile = Nip19Profile::new(pubkey, relays);
Ok(profile.to_bech32().unwrap())
}

View File

@@ -1,13 +1,20 @@
use crate::Nostr;
use nostr_sdk::prelude::*;
use std::time::Duration;
use std::{str::FromStr, time::Duration};
use tauri::State;
#[tauri::command(async)]
pub async fn get_metadata(npub: String, nostr: State<'_, Nostr>) -> Result<Metadata, ()> {
pub async fn get_metadata(id: &str, nostr: State<'_, Nostr>) -> Result<Metadata, ()> {
let client = &nostr.client;
let public_key;
let public_key = XOnlyPublicKey::from_bech32(npub).unwrap();
if id.starts_with("nprofile1") {
public_key = XOnlyPublicKey::from_bech32(id).unwrap();
} else if id.starts_with("npub1") {
public_key = XOnlyPublicKey::from_bech32(id).unwrap();
} else {
public_key = XOnlyPublicKey::from_str(id).unwrap();
}
let filter = Filter::new()
.author(public_key)
@@ -19,7 +26,7 @@ pub async fn get_metadata(npub: String, nostr: State<'_, Nostr>) -> Result<Metad
.await
.expect("Get metadata failed");
let event = events.into_iter().nth(0).unwrap();
let event = events.first().unwrap();
let metadata: Metadata = Metadata::from_json(&event.content).unwrap();
Ok(metadata)