ant_protocol/messages/
node_id.rs

1// Copyright 2024 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
4// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
5// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
6// KIND, either express or implied. Please review the Licences for the specific language governing
7// permissions and limitations relating to use of the SAFE Network Software.
8
9use libp2p::PeerId;
10use serde::{Deserialize, Serialize};
11use std::fmt::{self, Display};
12
13/// A unique identifier for a node in the network,
14/// by which we can know their location in the xor space.
15#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
16pub struct NodeId(Vec<u8>);
17
18impl NodeId {
19    /// Returns a `NodeId` representation of the `PeerId` by encapsulating its bytes.
20    pub fn from(peer_id: PeerId) -> Self {
21        Self(peer_id.to_bytes())
22    }
23
24    /// Returns this NodeId as bytes
25    pub fn as_bytes(&self) -> Vec<u8> {
26        self.0.to_vec()
27    }
28}
29
30impl Display for NodeId {
31    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32        write!(f, "NodeId({:?})", self.0)
33    }
34}