use core::fmt::{Debug, Display, Formatter};
use crate::{
DiscoKeyPair, MachineKeyPair, MachinePrivateKey, NetworkLockKeyPair, NetworkLockPrivateKey,
NodeKeyPair, NodePrivateKey,
};
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PersistState {
pub machine_key: MachinePrivateKey,
pub network_lock_key: NetworkLockPrivateKey,
pub node_key: NodePrivateKey,
}
impl From<&NodeState> for PersistState {
fn from(value: &NodeState) -> Self {
Self {
node_key: value.node_keys.private,
machine_key: value.machine_keys.private,
network_lock_key: value.network_lock_keys.private,
}
}
}
impl From<NodeState> for PersistState {
fn from(value: NodeState) -> Self {
Self::from(&value)
}
}
impl Default for PersistState {
fn default() -> Self {
Self {
machine_key: MachinePrivateKey::random(),
network_lock_key: NetworkLockPrivateKey::random(),
node_key: NodePrivateKey::random(),
}
}
}
#[derive(Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct NodeState {
pub disco_keys: DiscoKeyPair,
pub machine_keys: MachineKeyPair,
pub network_lock_keys: NetworkLockKeyPair,
pub node_keys: NodeKeyPair,
}
impl Debug for NodeState {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("NodeState")
.field(&self.machine_keys.public)
.field(&self.node_keys.public)
.field(&self.disco_keys.public)
.field(&self.network_lock_keys.public)
.finish()
}
}
impl Display for NodeState {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
Debug::fmt(self, f)
}
}
impl NodeState {
pub fn generate() -> Self {
Default::default()
}
}
impl From<&PersistState> for NodeState {
fn from(value: &PersistState) -> Self {
Self {
disco_keys: Default::default(),
node_keys: value.node_key.into(),
machine_keys: value.machine_key.into(),
network_lock_keys: value.network_lock_key.into(),
}
}
}
impl From<PersistState> for NodeState {
fn from(value: PersistState) -> Self {
Self::from(&value)
}
}