refactor: use specta for commands (#192)

* feat: add tauri-specta

* refactor: system library

* chore: format
This commit is contained in:
雨宮蓮
2024-05-25 15:21:40 +07:00
committed by GitHub
parent 7449000f5f
commit bba324ea53
92 changed files with 2164 additions and 2071 deletions

View File

@@ -2,17 +2,20 @@ use crate::Nostr;
use keyring::Entry;
use keyring_search::{Limit, List, Search};
use nostr_sdk::prelude::*;
use serde::Serialize;
use specta::Type;
use std::str::FromStr;
use std::time::Duration;
use tauri::State;
#[derive(serde::Serialize)]
#[derive(Serialize, Type)]
pub struct Account {
npub: String,
nsec: String,
}
#[tauri::command]
#[specta::specta]
pub fn get_accounts() -> Result<String, String> {
let search = Search::new().unwrap();
let results = search.by("Account", "nostr_secret");
@@ -24,6 +27,7 @@ pub fn get_accounts() -> Result<String, String> {
}
#[tauri::command]
#[specta::specta]
pub fn create_account() -> Result<Account, ()> {
let keys = Keys::generate();
let public_key = keys.public_key();
@@ -38,6 +42,7 @@ pub fn create_account() -> Result<Account, ()> {
}
#[tauri::command]
#[specta::specta]
pub async fn save_account(
nsec: &str,
password: &str,
@@ -80,6 +85,7 @@ pub async fn save_account(
}
#[tauri::command]
#[specta::specta]
pub async fn load_account(npub: &str, state: State<'_, Nostr>) -> Result<bool, String> {
let client = &state.client;
let keyring = Entry::new(&npub, "nostr_secret").unwrap();
@@ -165,6 +171,7 @@ pub async fn load_account(npub: &str, state: State<'_, Nostr>) -> Result<bool, S
}
#[tauri::command]
#[specta::specta]
pub async fn nostr_connect(
npub: &str,
uri: &str,
@@ -203,6 +210,7 @@ pub async fn nostr_connect(
}
#[tauri::command(async)]
#[specta::specta]
pub fn get_encrypted_key(npub: &str, password: &str) -> Result<String, String> {
let keyring = Entry::new(npub, "nostr_secret").unwrap();
@@ -221,6 +229,7 @@ pub fn get_encrypted_key(npub: &str, password: &str) -> Result<String, String> {
}
#[tauri::command]
#[specta::specta]
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);
@@ -229,6 +238,7 @@ pub fn event_to_bech32(id: &str, relays: Vec<String>) -> Result<String, ()> {
}
#[tauri::command]
#[specta::specta]
pub fn user_to_bech32(key: &str, relays: Vec<String>) -> Result<String, ()> {
let pubkey = PublicKey::from_str(key).unwrap();
let profile = Nip19Profile::new(pubkey, relays).unwrap();
@@ -237,6 +247,7 @@ pub fn user_to_bech32(key: &str, relays: Vec<String>) -> Result<String, ()> {
}
#[tauri::command]
#[specta::specta]
pub fn to_npub(hex: &str) -> Result<String, ()> {
let public_key = PublicKey::from_str(hex).unwrap();
let npub = Nip19::Pubkey(public_key);
@@ -245,6 +256,7 @@ pub fn to_npub(hex: &str) -> Result<String, ()> {
}
#[tauri::command]
#[specta::specta]
pub async fn verify_nip05(key: &str, nip05: &str) -> Result<bool, String> {
match PublicKey::from_str(key) {
Ok(public_key) => {