migrate AccountManagementView to enostr Keypair

Signed-off-by: kernelkind <kernelkind@gmail.com>
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
kernelkind
2024-05-14 16:23:45 -04:00
committed by William Casarin
parent bb25fd4ae1
commit 95f8623c41
7 changed files with 51 additions and 25 deletions

View File

@@ -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 {

View File

@@ -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)

View File

@@ -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),
}
}
}

View File

@@ -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!()
}

View File

@@ -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,

View File

@@ -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;

View File

@@ -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,
}