-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor/split accounts and nostr commands (#104)
* refactor(commands/accounts): move commands to their own file * refactor(commands/nostr): move commands to their own file
- Loading branch information
Showing
27 changed files
with
984 additions
and
868 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
src-tauri/src/commands/accounts/has_nostr_wallet_connect_uri.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
} | ||
} |
Oops, something went wrong.