pub struct PeerIdentityManager { /* private fields */ }Expand description
Manages the Ed25519 peer identity key with rotation support.
§Example
use std::path::Path;
use ipfrs_network::identity::PeerIdentityManager;
let mut mgr = PeerIdentityManager::load_or_generate(Path::new(".ipfrs/identity.key"))
.expect("identity load/generate");
println!("PeerId: {}", mgr.peer_id());
println!("Rotations so far: {}", mgr.rotation_count());Implementations§
Source§impl PeerIdentityManager
impl PeerIdentityManager
Sourcepub fn load_or_generate(key_path: &Path) -> Result<Self, IdentityError>
pub fn load_or_generate(key_path: &Path) -> Result<Self, IdentityError>
Load the identity keypair from key_path, or generate a fresh Ed25519
keypair and persist it if the file does not exist.
Sourcepub fn rotate(&mut self) -> Result<PeerId, IdentityError>
pub fn rotate(&mut self) -> Result<PeerId, IdentityError>
Rotate the peer identity key.
- Generates a fresh Ed25519 keypair.
- Atomically writes the new keypair to disk (temp-file + rename).
- Moves the current keypair into
previous_keypairs. - Returns the new
PeerId.
Sourcepub fn rotation_count(&self) -> u32
pub fn rotation_count(&self) -> u32
Return how many key rotations have been performed during the lifetime of this manager instance.
Sourcepub fn rotation_history(&self) -> &[RotationRecord]
pub fn rotation_history(&self) -> &[RotationRecord]
Return the complete rotation history recorded since this manager was created.
Sourcepub fn previous_keypairs(&self) -> &[PreviousKeypair]
pub fn previous_keypairs(&self) -> &[PreviousKeypair]
Return the list of previous (retired) keypairs still in the grace-period buffer.
Sourcepub fn keypair(&self) -> &Keypair
pub fn keypair(&self) -> &Keypair
Return a reference to the active keypair for use in the libp2p swarm.
Sourcepub fn export_public_key_pem(&self) -> String
pub fn export_public_key_pem(&self) -> String
Export the current public key as a PEM-encoded SubjectPublicKeyInfo block.
The encoding follows RFC 5480 / RFC 8410: the public key bytes are
wrapped in a DER SubjectPublicKeyInfo structure and then base64-
encoded with standard PEM delimiters.
Format:
-----BEGIN PUBLIC KEY-----
<base64-encoded SubjectPublicKeyInfo DER>
-----END PUBLIC KEY-----Sourcepub fn save(&self) -> Result<(), IdentityError>
pub fn save(&self) -> Result<(), IdentityError>
Save the current keypair to disk atomically.
Writes the protobuf-encoded keypair bytes to a temporary file in the
same directory as key_path, then renames it into place. This
ensures that a crash mid-write cannot leave the key file in a corrupt
state.
Sourcepub fn prune_retired(&mut self, max_age: Duration)
pub fn prune_retired(&mut self, max_age: Duration)
Remove retired keypairs older than max_age from the grace-period
buffer.
Call this periodically (e.g., once per hour) to prevent unbounded memory growth when many rotations have been performed.
Auto Trait Implementations§
impl Freeze for PeerIdentityManager
impl RefUnwindSafe for PeerIdentityManager
impl Send for PeerIdentityManager
impl Sync for PeerIdentityManager
impl Unpin for PeerIdentityManager
impl UnsafeUnpin for PeerIdentityManager
impl UnwindSafe for PeerIdentityManager
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more