Thomas Eizinger 135942d319
chore: enforce unreachable_pub lint
The `unreachable_pub` lint makes us aware of uses of `pub` that are not actually reachable from the crate root. This is considered good because it means reading a `pub` somewhere means it is actually public API. Some of our crates are quite large and keeping their entire API surface in your head is difficult.

We should strive for most items being `pub(crate)`. This lint helps us enforce that.

Pull-Request: #3735.
2023-04-26 07:31:56 +00:00

46 lines
1.1 KiB
Rust

use base64::prelude::*;
use serde::{Deserialize, Serialize};
use std::error::Error;
use std::path::Path;
use libp2p_identity::Keypair;
use libp2p_identity::PeerId;
#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub(crate) struct Config {
pub(crate) identity: Identity,
}
impl Config {
pub(crate) fn from_file(path: &Path) -> Result<Self, Box<dyn Error>> {
Ok(serde_json::from_str(&std::fs::read_to_string(path)?)?)
}
pub(crate) fn from_key_material(
peer_id: PeerId,
keypair: &Keypair,
) -> Result<Self, Box<dyn Error>> {
let priv_key = BASE64_STANDARD.encode(keypair.to_protobuf_encoding()?);
let peer_id = peer_id.to_base58();
Ok(Self {
identity: Identity { peer_id, priv_key },
})
}
}
#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub(crate) struct Identity {
#[serde(rename = "PeerID")]
pub(crate) peer_id: String,
pub(crate) priv_key: String,
}
impl zeroize::Zeroize for Config {
fn zeroize(&mut self) {
self.identity.peer_id.zeroize();
self.identity.priv_key.zeroize();
}
}