ant_protocol/storage/address/
graph.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 bls::PublicKey;
10use serde::{Deserialize, Serialize};
11use xor_name::XorName;
12
13use super::AddressParseError;
14
15/// Address of a [`crate::storage::graph::GraphEntry`]
16/// It is derived from the owner's unique public key
17#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize, Debug)]
18pub struct GraphEntryAddress(PublicKey);
19
20impl GraphEntryAddress {
21    /// Create a new [`GraphEntryAddress`]
22    pub fn new(owner: PublicKey) -> Self {
23        Self(owner)
24    }
25
26    /// Return the network name of the scratchpad.
27    /// This is used to locate the scratchpad on the network.
28    pub fn xorname(&self) -> XorName {
29        XorName::from_content(&self.0.to_bytes())
30    }
31
32    /// Return the owner.
33    pub fn owner(&self) -> &PublicKey {
34        &self.0
35    }
36
37    /// Serialize this [`GraphEntryAddress`] into a hex-encoded string.
38    pub fn to_hex(&self) -> String {
39        hex::encode(self.0.to_bytes())
40    }
41
42    /// Parse a hex-encoded string into a [`GraphEntryAddress`].
43    pub fn from_hex(hex: &str) -> Result<Self, AddressParseError> {
44        let owner = PublicKey::from_hex(hex)?;
45        Ok(Self(owner))
46    }
47}
48
49impl std::fmt::Display for GraphEntryAddress {
50    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51        write!(f, "{}", &self.to_hex())
52    }
53}