Skip to content

Commit 95f8623

Browse files
kernelkindjb55
authored andcommitted
migrate AccountManagementView to enostr Keypair
Signed-off-by: kernelkind <[email protected]> Signed-off-by: William Casarin <[email protected]>
1 parent bb25fd4 commit 95f8623

File tree

7 files changed

+51
-25
lines changed

7 files changed

+51
-25
lines changed

enostr/src/keypair.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ impl FullKeypair {
4646
pub fn new(pubkey: Pubkey, secret_key: SecretKey) -> Self {
4747
FullKeypair { pubkey, secret_key }
4848
}
49+
50+
pub fn generate() -> Self {
51+
let mut rng = nostr::secp256k1::rand::rngs::OsRng;
52+
let (secret_key, _) = &nostr::SECP256K1.generate_keypair(&mut rng);
53+
let (xopk, _) = secret_key.x_only_public_key(&nostr::SECP256K1);
54+
let secret_key = nostr::SecretKey::from(*secret_key);
55+
FullKeypair {
56+
pubkey: Pubkey::new(&xopk.serialize()),
57+
secret_key: SecretKey::from(secret_key),
58+
}
59+
}
60+
61+
pub fn to_keypair(self) -> Keypair {
62+
Keypair {
63+
pubkey: self.pubkey,
64+
secret_key: Some(self.secret_key),
65+
}
66+
}
4967
}
5068

5169
impl std::fmt::Display for Keypair {

src/account_manager.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use nostr_sdk::Keys;
1+
use enostr::FullKeypair;
22
use nostrdb::{Ndb, Transaction};
33

44
pub use crate::user_account::UserAccount;
@@ -35,7 +35,7 @@ impl<'a> SimpleProfilePreviewController<'a> {
3535
if let Ok(txn) = Transaction::new(self.ndb) {
3636
let profile = self
3737
.ndb
38-
.get_profile_by_pubkey(&txn, &account.key.public_key().to_bytes());
38+
.get_profile_by_pubkey(&txn, account.key.pubkey.bytes());
3939

4040
if let Ok(profile) = profile {
4141
let preview = SimpleProfilePreview::new(&profile, self.img_cache);
@@ -67,7 +67,7 @@ impl<'a> SimpleProfilePreviewController<'a> {
6767
if let Ok(txn) = Transaction::new(self.ndb) {
6868
let profile = self
6969
.ndb
70-
.get_profile_by_pubkey(&txn, &account.key.public_key().to_bytes());
70+
.get_profile_by_pubkey(&txn, account.key.pubkey.bytes());
7171

7272
if let Ok(profile) = profile {
7373
let preview = SimpleProfilePreview::new(&profile, self.img_cache);
@@ -115,16 +115,16 @@ impl<'a> AccountManager<'a> {
115115

116116
pub fn remove_account(&mut self, index: usize) {
117117
if let Some(account) = self.accounts.get(index) {
118-
self.key_store.remove_key(&account.key);
118+
let _ = self.key_store.remove_key(&account.key);
119119
}
120120
if index < self.accounts.len() {
121121
self.accounts.remove(index);
122122
}
123123
}
124124

125-
pub fn add_account(&'a mut self, key: Keys, ctx: &egui::Context) {
126-
self.key_store.add_key(&key);
127-
let relays = self.relay_generator.generate_relays_for(&key, ctx);
125+
pub fn add_account(&'a mut self, key: FullKeypair, ctx: &egui::Context) {
126+
let _ = self.key_store.add_key(&key);
127+
let relays = self.relay_generator.generate_relays_for(&key.pubkey, ctx);
128128
let account = UserAccount { key, relays };
129129

130130
self.accounts.push(account)

src/key_storage.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use nostr_sdk::Keys;
1+
use enostr::FullKeypair;
22

33
pub enum KeyStorage {
44
None,
@@ -9,19 +9,21 @@ pub enum KeyStorage {
99
}
1010

1111
impl KeyStorage {
12-
pub fn get_keys(&self) -> Result<Vec<Keys>, KeyStorageError> {
12+
pub fn get_keys(&self) -> Result<Vec<FullKeypair>, KeyStorageError> {
1313
match self {
1414
Self::None => Ok(Vec::new()),
1515
}
1616
}
1717

18-
pub fn add_key(&self, key: &Keys) -> Result<(), KeyStorageError> {
18+
pub fn add_key(&self, key: &FullKeypair) -> Result<(), KeyStorageError> {
19+
let _ = key;
1920
match self {
2021
Self::None => Ok(()),
2122
}
2223
}
2324

24-
pub fn remove_key(&self, key: &Keys) -> Result<(), KeyStorageError> {
25+
pub fn remove_key(&self, key: &FullKeypair) -> Result<(), KeyStorageError> {
26+
let _ = key;
2527
match self {
2628
Self::None => Ok(()),
2729
}
@@ -31,16 +33,16 @@ impl KeyStorage {
3133
#[derive(Debug, PartialEq)]
3234
pub enum KeyStorageError<'a> {
3335
Retrieval,
34-
Addition(&'a Keys),
35-
Removal(&'a Keys),
36+
Addition(&'a FullKeypair),
37+
Removal(&'a FullKeypair),
3638
}
3739

3840
impl std::fmt::Display for KeyStorageError<'_> {
3941
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
4042
match self {
4143
Self::Retrieval => write!(f, "Failed to retrieve keys."),
42-
Self::Addition(key) => write!(f, "Failed to add key: {:?}", key.public_key()),
43-
Self::Removal(key) => write!(f, "Failed to remove key: {:?}", key.public_key()),
44+
Self::Addition(key) => write!(f, "Failed to add key: {:?}", key.pubkey),
45+
Self::Removal(key) => write!(f, "Failed to remove key: {:?}", key.pubkey),
4446
}
4547
}
4648
}

src/relay_generation.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::relay_pool_manager::create_wakeup;
2-
use enostr::RelayPool;
3-
use nostr_sdk::Keys;
2+
use enostr::{Pubkey, RelayPool};
43
use tracing::error;
54

65
pub enum RelayGenerator {
@@ -10,7 +9,7 @@ pub enum RelayGenerator {
109
}
1110

1211
impl RelayGenerator {
13-
pub fn generate_relays_for(&self, key: &Keys, ctx: &egui::Context) -> RelayPool {
12+
pub fn generate_relays_for(&self, key: &Pubkey, ctx: &egui::Context) -> RelayPool {
1413
match self {
1514
Self::GossipModel => generate_relays_gossip(key, ctx),
1615
Self::Nip65 => generate_relays_nip65(key, ctx),
@@ -19,11 +18,15 @@ impl RelayGenerator {
1918
}
2019
}
2120

22-
fn generate_relays_gossip(key: &Keys, ctx: &egui::Context) -> RelayPool {
21+
fn generate_relays_gossip(key: &Pubkey, ctx: &egui::Context) -> RelayPool {
22+
let _ = ctx;
23+
let _ = key;
2324
todo!()
2425
}
2526

26-
fn generate_relays_nip65(key: &Keys, ctx: &egui::Context) -> RelayPool {
27+
fn generate_relays_nip65(key: &Pubkey, ctx: &egui::Context) -> RelayPool {
28+
let _ = ctx;
29+
let _ = key;
2730
todo!()
2831
}
2932

src/ui/account_management.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ impl<'a> AccountSelectionWidget<'a> {
292292
// PREVIEWS
293293

294294
mod preview {
295-
use nostr_sdk::{Keys, PublicKey};
295+
use enostr::{FullKeypair, Pubkey};
296296
use nostrdb::{Config, Ndb};
297297

298298
use super::*;
@@ -325,7 +325,10 @@ mod preview {
325325
ACCOUNT_HEXES
326326
.iter()
327327
.map(|account_hex| {
328-
let key = Keys::from_public_key(PublicKey::from_hex(account_hex).unwrap());
328+
let key = FullKeypair::new(
329+
Pubkey::from_hex(account_hex).unwrap(),
330+
FullKeypair::generate().secret_key,
331+
);
329332

330333
UserAccount {
331334
key,

src/ui/profile/preview.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::imgcache::ImageCache;
33
use crate::ui::ProfilePic;
44
use crate::{colors, images, DisplayName};
55
use egui::load::TexturePoll;
6-
use egui::{Frame, Layout, RichText, Sense, Vec2, Widget};
6+
use egui::{Frame, RichText, Sense, Vec2, Widget};
77
use egui_extras::Size;
88
use nostrdb::ProfileRecord;
99

src/user_account.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use enostr::RelayPool;
1+
use enostr::{FullKeypair, RelayPool};
22

33
pub struct UserAccount {
4-
pub key: nostr_sdk::Keys,
4+
pub key: FullKeypair,
55
pub relays: RelayPool,
66
}

0 commit comments

Comments
 (0)