ferripfs-config 0.1.0

IPFS node configuration types, compatible with Kubo config format
Documentation
// Ported from: kubo/config/identity.go
// Kubo version: v0.39.0
// Original: https://github.com/ipfs/kubo/blob/v0.39.0/config/identity.go
//
// Original work: Copyright (c) Protocol Labs, Inc.
// Port: Copyright (c) 2026 ferripfs contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Identity configuration, containing PeerID and private key.

use serde::{Deserialize, Serialize};

/// Identity configuration section
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Identity {
    /// The peer ID (base58 or base36 encoded)
    #[serde(default, rename = "PeerID")]
    pub peer_id: String,

    /// The private key (base64 encoded protobuf)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub priv_key: Option<String>,
}

impl Identity {
    /// Create a new identity with the given peer ID
    pub fn new(peer_id: String) -> Self {
        Self {
            peer_id,
            priv_key: None,
        }
    }

    /// Create a new identity with peer ID and private key
    pub fn with_private_key(peer_id: String, priv_key: String) -> Self {
        Self {
            peer_id,
            priv_key: Some(priv_key),
        }
    }

    /// Check if the identity has a private 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());
    }
}