diff options
author | hozan23 <hozan23@karyontech.net> | 2024-06-24 19:25:57 +0200 |
---|---|---|
committer | hozan23 <hozan23@karyontech.net> | 2024-06-24 19:25:57 +0200 |
commit | cc6b474b0d35f5fa3f00a742b1c0e18a9a1a25a3 (patch) | |
tree | 48d6e9d5cdaa515127da1b8575550be25559fd9a /p2p | |
parent | e3d1f4fd91a5f077fda8a1976e194c378ee166d0 (diff) |
p2p: use base64 to encode PeerID to string
Diffstat (limited to 'p2p')
-rw-r--r-- | p2p/Cargo.toml | 28 | ||||
-rw-r--r-- | p2p/src/error.rs | 10 | ||||
-rw-r--r-- | p2p/src/peer/peer_id.rs | 29 |
3 files changed, 44 insertions, 23 deletions
diff --git a/p2p/Cargo.toml b/p2p/Cargo.toml index 9dd43d5..7eee02a 100644 --- a/p2p/Cargo.toml +++ b/p2p/Cargo.toml @@ -27,18 +27,26 @@ karyon_net = { workspace = true, default-features = false, features = [ "udp", ] } -async-trait = "0.1.77" -async-channel = "2.3.0" -futures-util = { version = "0.3.5", features = [ - "alloc", -], default-features = false } + log = "0.4.21" chrono = "0.4.35" -bincode = { version = "2.0.0-rc.3", features = ["derive"] } rand = "0.8.5" thiserror = "1.0.58" semver = "1.0.22" sha2 = "0.10.8" +parking_lot = "0.12.2" + +# encode/decode +bincode = { version = "2.0.0-rc.3", features = ["derive"] } +base64 = "0.22.1" +serde = { version = "1.0.197", features = ["derive"], optional = true } + +# async +async-trait = "0.1.77" +async-channel = "2.3.0" +futures-util = { version = "0.3.5", features = [ + "alloc", +], default-features = false } # tls rcgen = "0.12.1" @@ -50,11 +58,6 @@ futures-rustls = { version = "0.25.1", features = [ tokio-rustls = { version = "0.26.0", features = ["aws-lc-rs"], optional = true } rustls-pki-types = "1.7.0" -# serde -serde = { version = "1.0.197", features = ["derive"], optional = true } - -parking_lot = "0.12.2" - [dev-dependencies] async-std = "1.12.0" clap = { version = "4.5.2", features = ["derive"] } @@ -62,6 +65,3 @@ ctrlc = "3.4.4" easy-parallel = "3.3.1" env_logger = "0.11.3" smol = "2.0.0" -karyon_jsonrpc = { workspace = true, features = ["ws", "smol"] } -serde_json = "1.0.117" -serde = { version = "1.0.202", features = ["derive"] } diff --git a/p2p/src/error.rs b/p2p/src/error.rs index 97b7b7f..a490b57 100644 --- a/p2p/src/error.rs +++ b/p2p/src/error.rs @@ -11,8 +11,11 @@ pub enum Error { #[error("Unsupported protocol error: {0}")] UnsupportedProtocol(String), - #[error("Try from public key Error: {0}")] - TryFromPublicKey(&'static str), + #[error("PeerID try from PublicKey Error")] + PeerIDTryFromPublicKey, + + #[error("PeerID try from String Error")] + PeerIDTryFromString, #[error("Invalid message error: {0}")] InvalidMsg(String), @@ -24,6 +27,9 @@ pub enum Error { ParseIntError(#[from] std::num::ParseIntError), #[error(transparent)] + ParseIntError2(#[from] base64::DecodeError), + + #[error(transparent)] ParseFloatError(#[from] std::num::ParseFloatError), #[error(transparent)] diff --git a/p2p/src/peer/peer_id.rs b/p2p/src/peer/peer_id.rs index a769c86..145c199 100644 --- a/p2p/src/peer/peer_id.rs +++ b/p2p/src/peer/peer_id.rs @@ -1,3 +1,4 @@ +use base64::{engine::general_purpose::STANDARD, Engine}; use bincode::{Decode, Encode}; use rand::{rngs::OsRng, RngCore}; use sha2::{Digest, Sha256}; @@ -12,16 +13,12 @@ use crate::Error; /// Represents a unique identifier for a peer. #[derive(Clone, Debug, Eq, PartialEq, Hash, Decode, Encode)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", serde(into = "String"))] pub struct PeerID(pub [u8; 32]); impl std::fmt::Display for PeerID { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - let id = self.0[0..8] - .iter() - .map(|b| format!("{:x}", b)) - .collect::<Vec<String>>() - .join(""); - + let id = STANDARD.encode(self.0); write!(f, "{}", id) } } @@ -48,6 +45,24 @@ impl From<[u8; 32]> for PeerID { } } +impl From<PeerID> for String { + fn from(pid: PeerID) -> Self { + pid.to_string() + } +} + +impl TryFrom<String> for PeerID { + type Error = Error; + + fn try_from(i: String) -> Result<Self, Self::Error> { + let result: [u8; 32] = STANDARD + .decode(i)? + .try_into() + .map_err(|_| Error::PeerIDTryFromString)?; + Ok(PeerID(result)) + } +} + impl TryFrom<PublicKey> for PeerID { type Error = Error; @@ -55,7 +70,7 @@ impl TryFrom<PublicKey> for PeerID { let pk: [u8; 32] = pk .as_bytes() .try_into() - .map_err(|_| Error::TryFromPublicKey("Failed to convert public key to [u8;32]"))?; + .map_err(|_| Error::PeerIDTryFromPublicKey)?; Ok(PeerID(pk)) } |