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