Skip to main content

ferripfs_config/
identity.rs

1// Ported from: kubo/config/identity.go
2// Kubo version: v0.39.0
3// Original: https://github.com/ipfs/kubo/blob/v0.39.0/config/identity.go
4//
5// Original work: Copyright (c) Protocol Labs, Inc.
6// Port: Copyright (c) 2026 ferripfs contributors
7// SPDX-License-Identifier: MIT OR Apache-2.0
8
9//! Identity configuration, containing PeerID and private key.
10
11use serde::{Deserialize, Serialize};
12
13/// Identity configuration section
14#[derive(Debug, Clone, Default, Serialize, Deserialize)]
15#[serde(rename_all = "PascalCase")]
16pub struct Identity {
17    /// The peer ID (base58 or base36 encoded)
18    #[serde(default, rename = "PeerID")]
19    pub peer_id: String,
20
21    /// The private key (base64 encoded protobuf)
22    #[serde(default, skip_serializing_if = "Option::is_none")]
23    pub priv_key: Option<String>,
24}
25
26impl Identity {
27    /// Create a new identity with the given peer ID
28    pub fn new(peer_id: String) -> Self {
29        Self {
30            peer_id,
31            priv_key: None,
32        }
33    }
34
35    /// Create a new identity with peer ID and private key
36    pub fn with_private_key(peer_id: String, priv_key: String) -> Self {
37        Self {
38            peer_id,
39            priv_key: Some(priv_key),
40        }
41    }
42
43    /// Check if the identity has a private key
44    pub fn has_private_key(&self) -> bool {
45        self.priv_key.is_some()
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_identity_serialization() {
55        let id = Identity::new("QmTest123".to_string());
56        let json = serde_json::to_string(&id).unwrap();
57        assert!(json.contains("PeerID"));
58        assert!(json.contains("QmTest123"));
59    }
60
61    #[test]
62    fn test_identity_with_privkey() {
63        let id =
64            Identity::with_private_key("QmTest123".to_string(), "base64encodedkey".to_string());
65        assert!(id.has_private_key());
66    }
67}