notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

commit 95f8623c4138bd42c767a7f14064494beceb41d9
parent bb25fd4ae1cc36ca1fe070ad46b2ef52e272478c
Author: kernelkind <kernelkind@gmail.com>
Date:   Tue, 14 May 2024 16:23:45 -0400

migrate AccountManagementView to enostr Keypair

Signed-off-by: kernelkind <kernelkind@gmail.com>
Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Menostr/src/keypair.rs | 18++++++++++++++++++
Msrc/account_manager.rs | 14+++++++-------
Msrc/key_storage.rs | 18++++++++++--------
Msrc/relay_generation.rs | 13++++++++-----
Msrc/ui/account_management.rs | 7+++++--
Msrc/ui/profile/preview.rs | 2+-
Msrc/user_account.rs | 4++--
7 files changed, 51 insertions(+), 25 deletions(-)

diff --git a/enostr/src/keypair.rs b/enostr/src/keypair.rs @@ -46,6 +46,24 @@ impl FullKeypair { pub fn new(pubkey: Pubkey, secret_key: SecretKey) -> Self { FullKeypair { pubkey, secret_key } } + + pub fn generate() -> Self { + let mut rng = nostr::secp256k1::rand::rngs::OsRng; + let (secret_key, _) = &nostr::SECP256K1.generate_keypair(&mut rng); + let (xopk, _) = secret_key.x_only_public_key(&nostr::SECP256K1); + let secret_key = nostr::SecretKey::from(*secret_key); + FullKeypair { + pubkey: Pubkey::new(&xopk.serialize()), + secret_key: SecretKey::from(secret_key), + } + } + + pub fn to_keypair(self) -> Keypair { + Keypair { + pubkey: self.pubkey, + secret_key: Some(self.secret_key), + } + } } impl std::fmt::Display for Keypair { diff --git a/src/account_manager.rs b/src/account_manager.rs @@ -1,4 +1,4 @@ -use nostr_sdk::Keys; +use enostr::FullKeypair; use nostrdb::{Ndb, Transaction}; pub use crate::user_account::UserAccount; @@ -35,7 +35,7 @@ impl<'a> SimpleProfilePreviewController<'a> { if let Ok(txn) = Transaction::new(self.ndb) { let profile = self .ndb - .get_profile_by_pubkey(&txn, &account.key.public_key().to_bytes()); + .get_profile_by_pubkey(&txn, account.key.pubkey.bytes()); if let Ok(profile) = profile { let preview = SimpleProfilePreview::new(&profile, self.img_cache); @@ -67,7 +67,7 @@ impl<'a> SimpleProfilePreviewController<'a> { if let Ok(txn) = Transaction::new(self.ndb) { let profile = self .ndb - .get_profile_by_pubkey(&txn, &account.key.public_key().to_bytes()); + .get_profile_by_pubkey(&txn, account.key.pubkey.bytes()); if let Ok(profile) = profile { let preview = SimpleProfilePreview::new(&profile, self.img_cache); @@ -115,16 +115,16 @@ impl<'a> AccountManager<'a> { pub fn remove_account(&mut self, index: usize) { if let Some(account) = self.accounts.get(index) { - self.key_store.remove_key(&account.key); + let _ = self.key_store.remove_key(&account.key); } if index < self.accounts.len() { self.accounts.remove(index); } } - pub fn add_account(&'a mut self, key: Keys, ctx: &egui::Context) { - self.key_store.add_key(&key); - let relays = self.relay_generator.generate_relays_for(&key, ctx); + pub fn add_account(&'a mut self, key: FullKeypair, ctx: &egui::Context) { + let _ = self.key_store.add_key(&key); + let relays = self.relay_generator.generate_relays_for(&key.pubkey, ctx); let account = UserAccount { key, relays }; self.accounts.push(account) diff --git a/src/key_storage.rs b/src/key_storage.rs @@ -1,4 +1,4 @@ -use nostr_sdk::Keys; +use enostr::FullKeypair; pub enum KeyStorage { None, @@ -9,19 +9,21 @@ pub enum KeyStorage { } impl KeyStorage { - pub fn get_keys(&self) -> Result<Vec<Keys>, KeyStorageError> { + pub fn get_keys(&self) -> Result<Vec<FullKeypair>, KeyStorageError> { match self { Self::None => Ok(Vec::new()), } } - pub fn add_key(&self, key: &Keys) -> Result<(), KeyStorageError> { + pub fn add_key(&self, key: &FullKeypair) -> Result<(), KeyStorageError> { + let _ = key; match self { Self::None => Ok(()), } } - pub fn remove_key(&self, key: &Keys) -> Result<(), KeyStorageError> { + pub fn remove_key(&self, key: &FullKeypair) -> Result<(), KeyStorageError> { + let _ = key; match self { Self::None => Ok(()), } @@ -31,16 +33,16 @@ impl KeyStorage { #[derive(Debug, PartialEq)] pub enum KeyStorageError<'a> { Retrieval, - Addition(&'a Keys), - Removal(&'a Keys), + Addition(&'a FullKeypair), + Removal(&'a FullKeypair), } impl std::fmt::Display for KeyStorageError<'_> { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { Self::Retrieval => write!(f, "Failed to retrieve keys."), - Self::Addition(key) => write!(f, "Failed to add key: {:?}", key.public_key()), - Self::Removal(key) => write!(f, "Failed to remove key: {:?}", key.public_key()), + Self::Addition(key) => write!(f, "Failed to add key: {:?}", key.pubkey), + Self::Removal(key) => write!(f, "Failed to remove key: {:?}", key.pubkey), } } } diff --git a/src/relay_generation.rs b/src/relay_generation.rs @@ -1,6 +1,5 @@ use crate::relay_pool_manager::create_wakeup; -use enostr::RelayPool; -use nostr_sdk::Keys; +use enostr::{Pubkey, RelayPool}; use tracing::error; pub enum RelayGenerator { @@ -10,7 +9,7 @@ pub enum RelayGenerator { } impl RelayGenerator { - pub fn generate_relays_for(&self, key: &Keys, ctx: &egui::Context) -> RelayPool { + pub fn generate_relays_for(&self, key: &Pubkey, ctx: &egui::Context) -> RelayPool { match self { Self::GossipModel => generate_relays_gossip(key, ctx), Self::Nip65 => generate_relays_nip65(key, ctx), @@ -19,11 +18,15 @@ impl RelayGenerator { } } -fn generate_relays_gossip(key: &Keys, ctx: &egui::Context) -> RelayPool { +fn generate_relays_gossip(key: &Pubkey, ctx: &egui::Context) -> RelayPool { + let _ = ctx; + let _ = key; todo!() } -fn generate_relays_nip65(key: &Keys, ctx: &egui::Context) -> RelayPool { +fn generate_relays_nip65(key: &Pubkey, ctx: &egui::Context) -> RelayPool { + let _ = ctx; + let _ = key; todo!() } diff --git a/src/ui/account_management.rs b/src/ui/account_management.rs @@ -292,7 +292,7 @@ impl<'a> AccountSelectionWidget<'a> { // PREVIEWS mod preview { - use nostr_sdk::{Keys, PublicKey}; + use enostr::{FullKeypair, Pubkey}; use nostrdb::{Config, Ndb}; use super::*; @@ -325,7 +325,10 @@ mod preview { ACCOUNT_HEXES .iter() .map(|account_hex| { - let key = Keys::from_public_key(PublicKey::from_hex(account_hex).unwrap()); + let key = FullKeypair::new( + Pubkey::from_hex(account_hex).unwrap(), + FullKeypair::generate().secret_key, + ); UserAccount { key, diff --git a/src/ui/profile/preview.rs b/src/ui/profile/preview.rs @@ -3,7 +3,7 @@ use crate::imgcache::ImageCache; use crate::ui::ProfilePic; use crate::{colors, images, DisplayName}; use egui::load::TexturePoll; -use egui::{Frame, Layout, RichText, Sense, Vec2, Widget}; +use egui::{Frame, RichText, Sense, Vec2, Widget}; use egui_extras::Size; use nostrdb::ProfileRecord; diff --git a/src/user_account.rs b/src/user_account.rs @@ -1,6 +1,6 @@ -use enostr::RelayPool; +use enostr::{FullKeypair, RelayPool}; pub struct UserAccount { - pub key: nostr_sdk::Keys, + pub key: FullKeypair, pub relays: RelayPool, }