2026-01-22 06:39:09 +07:00
|
|
|
use std::fmt;
|
|
|
|
|
|
2026-02-18 09:29:33 -08:00
|
|
|
use crate::crypto::{PrivateKey, PublicKey};
|
2026-01-22 06:39:09 +07:00
|
|
|
|
|
|
|
|
pub struct Identity {
|
2026-02-19 17:25:42 -08:00
|
|
|
name: String,
|
2026-02-18 09:29:33 -08:00
|
|
|
secret: PrivateKey,
|
2026-01-22 06:39:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl fmt::Debug for Identity {
|
|
|
|
|
// Manually implement debug to not reveal secret key material
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
f.debug_struct("Identity")
|
|
|
|
|
.field("public_key", &self.public_key())
|
|
|
|
|
.finish_non_exhaustive()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Identity {
|
2026-02-19 17:25:42 -08:00
|
|
|
pub fn new(name: impl Into<String>) -> Self {
|
2026-01-22 06:39:09 +07:00
|
|
|
Self {
|
2026-02-19 17:25:42 -08:00
|
|
|
name: name.into(),
|
2026-02-18 09:29:33 -08:00
|
|
|
secret: PrivateKey::random(),
|
2026-01-22 06:39:09 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-25 08:45:22 +08:00
|
|
|
pub fn from_secret(name: impl Into<String>, secret: PrivateKey) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
name: name.into(),
|
|
|
|
|
secret,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-22 06:39:09 +07:00
|
|
|
pub fn public_key(&self) -> PublicKey {
|
|
|
|
|
PublicKey::from(&self.secret)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-18 09:29:33 -08:00
|
|
|
pub fn secret(&self) -> &PrivateKey {
|
2026-01-22 06:39:09 +07:00
|
|
|
&self.secret
|
|
|
|
|
}
|
2026-02-19 17:25:42 -08:00
|
|
|
|
|
|
|
|
// Returns an associated name for this Identity.
|
|
|
|
|
// Names are a friendly developer chosen identifier for an Identity which
|
|
|
|
|
// can provide between logging.
|
|
|
|
|
pub fn get_name(&self) -> &str {
|
|
|
|
|
&self.name
|
|
|
|
|
}
|
2026-01-22 06:39:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for Identity {
|
|
|
|
|
fn default() -> Self {
|
2026-02-19 17:25:42 -08:00
|
|
|
Self::new("default")
|
2026-01-22 06:39:09 +07:00
|
|
|
}
|
|
|
|
|
}
|