Skip to content

Commit

Permalink
Refactor/split accounts and nostr commands (#104)
Browse files Browse the repository at this point in the history
* refactor(commands/accounts): move commands to their own file

* refactor(commands/nostr): move commands to their own file
  • Loading branch information
jgmontoya authored Mar 3, 2025
1 parent cfda4d1 commit c666060
Show file tree
Hide file tree
Showing 27 changed files with 984 additions and 868 deletions.
264 changes: 0 additions & 264 deletions src-tauri/src/commands/accounts.rs

This file was deleted.

28 changes: 28 additions & 0 deletions src-tauri/src/commands/accounts/create_identity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::accounts::Account;
use crate::whitenoise::Whitenoise;
use nostr_sdk::prelude::*;

/// Creates a new identity by generating a new keypair and logging in with it.
///
/// # Arguments
///
/// * `wn` - A reference to the Whitenoise state.
/// * `app_handle` - The app handle.
///
/// # Returns
///
/// * `Ok(Account)` - The newly created account.
/// * `Err(String)` - An error message if there was an issue creating the identity.
#[tauri::command]
pub async fn create_identity(
wn: tauri::State<'_, Whitenoise>,
app_handle: tauri::AppHandle,
) -> Result<Account, String> {
let account = Account::new(wn.clone())
.await
.map_err(|e| format!("Error creating account: {}", e))?;
account
.set_active(wn.clone(), &app_handle)
.await
.map_err(|e| format!("Error setting active account: {}", e))
}
20 changes: 20 additions & 0 deletions src-tauri/src/commands/accounts/get_accounts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::accounts::Account;
use crate::whitenoise::Whitenoise;
use nostr_sdk::prelude::*;

/// Lists all accounts.
///
/// # Arguments
///
/// * `wn` - A reference to the Whitenoise state.
///
/// # Returns
///
/// * `Ok(Vec<Account>)` - A vector of accounts if successful.
/// * `Err(String)` - An error message if there was an issue listing the accounts.
#[tauri::command]
pub async fn get_accounts(wn: tauri::State<'_, Whitenoise>) -> Result<Vec<Account>, String> {
Account::all(wn.clone())
.await
.map_err(|e| format!("Error fetching accounts: {}", e))
}
27 changes: 27 additions & 0 deletions src-tauri/src/commands/accounts/has_nostr_wallet_connect_uri.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::accounts::Account;
use crate::whitenoise::Whitenoise;
use nostr_sdk::prelude::*;

/// Checks if a Nostr Wallet Connect URI is configured for the active account.
///
/// # Arguments
///
/// * `wn` - A reference to the Whitenoise state
///
/// # Returns
///
/// * `Ok(bool)` - true if a NWC URI is configured, false otherwise
/// * `Err(String)` - An error message if there was an issue checking the NWC URI
#[tauri::command]
pub async fn has_nostr_wallet_connect_uri(
wn: tauri::State<'_, Whitenoise>,
) -> Result<bool, String> {
let active_account = Account::get_active(wn.clone())
.await
.map_err(|e| format!("Error getting active account: {}", e))?;

active_account
.get_nostr_wallet_connect_uri(wn.clone())
.map(|opt| opt.is_some())
.map_err(|e| format!("Error checking NWC URI: {}", e))
}
39 changes: 39 additions & 0 deletions src-tauri/src/commands/accounts/login.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::accounts::Account;
use crate::whitenoise::Whitenoise;
use nostr_sdk::prelude::*;

/// Logs in with the given public key. Will set the active account if successful.
///
/// # Arguments
///
/// * `wn` - A reference to the Whitenoise state.
/// * `hex_pubkey` - The public key in hexadecimal format.
///
/// # Returns
///
/// * `Ok(Account)` - The account if login was successful.
/// * `Err(String)` - An error message if there was an issue logging in.
#[tauri::command]
pub async fn login(
nsec_or_hex_privkey: String,
wn: tauri::State<'_, Whitenoise>,
app_handle: tauri::AppHandle,
) -> Result<Account, String> {
let keys = Keys::parse(&nsec_or_hex_privkey).map_err(|e| e.to_string())?;

match Account::find_by_pubkey(&keys.public_key, wn.clone()).await {
Ok(account) => {
tracing::debug!("Account found, setting active");
account
.set_active(wn.clone(), &app_handle)
.await
.map_err(|e| format!("Error logging in: {}", e))
}
_ => {
tracing::debug!(target: "whitenoise::commands::accounts","Account not found, adding from keys");
Account::add_from_keys(&keys, true, wn.clone(), &app_handle)
.await
.map_err(|e| format!("Error logging in: {}", e))
}
}
}
Loading

0 comments on commit c666060

Please sign in to comment.