mirror of
https://github.com/mikedilger/gossip.git
synced 2024-09-19 19:46:50 +00:00
Signer
This commit is contained in:
parent
06f6eebc57
commit
ef7849e659
@ -30,6 +30,9 @@ pub enum Error {
|
|||||||
#[error("Nostr: {0}")]
|
#[error("Nostr: {0}")]
|
||||||
Nostr(#[from] nostr_types::Error),
|
Nostr(#[from] nostr_types::Error),
|
||||||
|
|
||||||
|
#[error("No private key available.")]
|
||||||
|
NoPrivateKey,
|
||||||
|
|
||||||
#[error("Image: {0}")]
|
#[error("Image: {0}")]
|
||||||
Image(#[from] image::error::ImageError),
|
Image(#[from] image::error::ImageError),
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ use crate::db::{DbEvent, DbPerson, DbPersonRelay, DbRelay};
|
|||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use crate::relationship::Relationship;
|
use crate::relationship::Relationship;
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
|
use crate::signer::Signer;
|
||||||
use async_recursion::async_recursion;
|
use async_recursion::async_recursion;
|
||||||
use nostr_types::{Event, EventKind, Id, IdHex, PublicKey, PublicKeyHex, Unixtime, Url};
|
use nostr_types::{Event, EventKind, Id, IdHex, PublicKey, PublicKeyHex, Unixtime, Url};
|
||||||
use rusqlite::Connection;
|
use rusqlite::Connection;
|
||||||
@ -55,6 +56,9 @@ pub struct Globals {
|
|||||||
|
|
||||||
/// Settings
|
/// Settings
|
||||||
pub settings: RwLock<Settings>,
|
pub settings: RwLock<Settings>,
|
||||||
|
|
||||||
|
/// Signer
|
||||||
|
pub signer: RwLock<Signer>,
|
||||||
}
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
@ -79,6 +83,7 @@ lazy_static! {
|
|||||||
relays: RwLock::new(HashMap::new()),
|
relays: RwLock::new(HashMap::new()),
|
||||||
shutting_down: AtomicBool::new(false),
|
shutting_down: AtomicBool::new(false),
|
||||||
settings: RwLock::new(Settings::default()),
|
settings: RwLock::new(Settings::default()),
|
||||||
|
signer: RwLock::new(Signer::default()),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ mod overlord;
|
|||||||
mod process;
|
mod process;
|
||||||
mod relationship;
|
mod relationship;
|
||||||
mod settings;
|
mod settings;
|
||||||
|
mod signer;
|
||||||
mod ui;
|
mod ui;
|
||||||
|
|
||||||
use crate::comms::BusMessage;
|
use crate::comms::BusMessage;
|
||||||
|
67
src/signer.rs
Normal file
67
src/signer.rs
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
use crate::error::Error;
|
||||||
|
use nostr_types::{EncryptedPrivateKey, Event, KeySecurity, PreEvent, PrivateKey, PublicKey};
|
||||||
|
|
||||||
|
pub enum Signer {
|
||||||
|
Fresh,
|
||||||
|
Encrypted(EncryptedPrivateKey),
|
||||||
|
Ready(PrivateKey),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Signer {
|
||||||
|
fn default() -> Signer {
|
||||||
|
Signer::Fresh
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Signer {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn load_encrypted_private_key(&mut self, epk: EncryptedPrivateKey) {
|
||||||
|
*self = Signer::Encrypted(epk);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn unlock_encrypted_private_key(&mut self, pass: &str) -> Result<(), Error> {
|
||||||
|
if let Signer::Encrypted(epk) = self {
|
||||||
|
*self = Signer::Ready(epk.decrypt(pass)?);
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(Error::NoPrivateKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn is_loaded(&self) -> bool {
|
||||||
|
matches!(self, Signer::Encrypted(_)) || matches!(self, Signer::Ready(_))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn is_ready(&self) -> bool {
|
||||||
|
matches!(self, Signer::Ready(_))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn public_key(&self) -> Option<PublicKey> {
|
||||||
|
if let Signer::Ready(pk) = self {
|
||||||
|
Some(pk.public_key())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn key_security(&self) -> Option<KeySecurity> {
|
||||||
|
if let Signer::Ready(pk) = self {
|
||||||
|
Some(pk.key_security())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn sign_preevent(&self, preevent: PreEvent) -> Result<Event, Error> {
|
||||||
|
match self {
|
||||||
|
Signer::Ready(pk) => Ok(Event::new(preevent, pk)?),
|
||||||
|
_ => Err(Error::NoPrivateKey),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user