use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Identity {
#[serde(default, rename = "PeerID")]
pub peer_id: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub priv_key: Option<String>,
}
impl Identity {
pub fn new(peer_id: String) -> Self {
Self {
peer_id,
priv_key: None,
}
}
pub fn with_private_key(peer_id: String, priv_key: String) -> Self {
Self {
peer_id,
priv_key: Some(priv_key),
}
}
pub fn has_private_key(&self) -> bool {
self.priv_key.is_some()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_identity_serialization() {
let id = Identity::new("QmTest123".to_string());
let json = serde_json::to_string(&id).unwrap();
assert!(json.contains("PeerID"));
assert!(json.contains("QmTest123"));
}
#[test]
fn test_identity_with_privkey() {
let id =
Identity::with_private_key("QmTest123".to_string(), "base64encodedkey".to_string());
assert!(id.has_private_key());
}
}