Start of private key code

This commit is contained in:
Mike Dilger 2022-12-21 09:01:53 +13:00
parent 7c2e0b0c73
commit 974f1fb09a
2 changed files with 24 additions and 2 deletions

View File

@ -5,6 +5,7 @@ use crate::event_related::EventRelated;
use nostr_proto::{Event, EventKind, Id, Metadata, PublicKey, PublicKeyHex, Tag, Unixtime};
use rusqlite::Connection;
use std::collections::HashMap;
use std::sync::atomic::AtomicBool;
use tokio::sync::{broadcast, mpsc, Mutex};
use tracing::info;
@ -34,6 +35,10 @@ pub struct Globals {
/// All nostr people records currently loaded into memory, keyed by pubkey
pub people: Mutex<HashMap<PublicKey, DbPerson>>,
/// Whether or not we have a saved private key and need the password to unlock it
#[allow(dead_code)]
pub need_password: AtomicBool,
}
lazy_static! {
@ -53,6 +58,7 @@ lazy_static! {
events: Mutex::new(HashMap::new()),
event_relateds: Mutex::new(HashMap::new()),
people: Mutex::new(HashMap::new()),
need_password: AtomicBool::new(false),
}
};
}

View File

@ -2,12 +2,12 @@ mod minion;
mod relay_picker;
use crate::comms::BusMessage;
use crate::db::{DbEvent, DbPerson, DbPersonRelay, DbRelay};
use crate::db::{DbEvent, DbPerson, DbPersonRelay, DbRelay, DbSetting};
use crate::error::Error;
use crate::globals::GLOBALS;
use crate::settings::Settings;
use minion::Minion;
use nostr_proto::{Event, PublicKey, PublicKeyHex, Unixtime, Url};
use nostr_proto::{Event, PrivateKey, PublicKey, PublicKeyHex, Unixtime, Url};
use relay_picker::{BestRelay, RelayPicker};
use std::collections::HashMap;
use tokio::sync::broadcast::Sender;
@ -21,6 +21,8 @@ pub struct Overlord {
from_minions: UnboundedReceiver<BusMessage>,
minions: task::JoinSet<()>,
minions_task_url: HashMap<task::Id, Url>,
#[allow(dead_code)]
private_key: Option<PrivateKey>, // note that PrivateKey already zeroizes on drop
}
impl Overlord {
@ -32,6 +34,7 @@ impl Overlord {
from_minions,
minions: task::JoinSet::new(),
minions_task_url: HashMap::new(),
private_key: None,
}
}
@ -63,6 +66,19 @@ impl Overlord {
// Load settings
self.settings = Settings::load().await?;
// Check for a private key
if DbSetting::fetch_setting("user_private_key")
.await?
.is_some()
{
// We don't bother loading the value just yet because we don't have
// the password.
info!("Saved private key found. Will need a password to unlock.");
GLOBALS
.need_password
.store(true, std::sync::atomic::Ordering::Relaxed);
}
// FIXME - if this needs doing, it should be done dynamically as
// new people are encountered, not batch-style on startup.
// Create a person record for every person seen, possibly autofollow