1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2023 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::error::{Error, Result};

use bls::{PublicKey, PK_SIZE};
use serde::{Deserialize, Serialize};
use std::{
    fmt::{Debug, Display},
    hash::Hash,
};
use xor_name::{XorName, XOR_NAME_LEN};

/// Address of a Register on the SAFE Network
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub struct RegisterAddress {
    /// User chosen meta, can be anything, the register's name on the network will be the hash of this meta and the owner
    pub(crate) meta: XorName,
    /// Owner of the register
    pub(crate) owner: PublicKey,
}

impl Display for RegisterAddress {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}({:?})", &self.to_hex()[0..6], self.xorname())
    }
}

impl Debug for RegisterAddress {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "RegisterAddress({}) {{ meta: {:?}, owner: {:?} }}",
            &self.to_hex()[0..6],
            self.meta,
            self.owner
        )
    }
}

impl RegisterAddress {
    /// Construct a new `RegisterAddress` given `meta` and `owner`.
    pub fn new(meta: XorName, owner: PublicKey) -> Self {
        Self { meta, owner }
    }

    /// Return the network name of the register.
    /// This is used to locate the register on the network.
    pub fn xorname(&self) -> XorName {
        let mut bytes = vec![];
        bytes.extend_from_slice(&self.meta.0);
        bytes.extend_from_slice(&self.owner.to_bytes());
        XorName::from_content(&bytes)
    }

    /// Serialize this `RegisterAddress` instance to a hex-encoded `String`.
    pub fn to_hex(&self) -> String {
        let mut bytes = vec![];
        bytes.extend_from_slice(&self.meta.0);
        bytes.extend_from_slice(&self.owner.to_bytes());
        hex::encode(bytes)
    }

    /// Deserialize a hex-encoded representation of a `RegisterAddress` to a `RegisterAddress` instance.
    pub fn from_hex(hex: &str) -> Result<Self> {
        let bytes = hex::decode(hex).map_err(|_| Error::HexDeserializeFailed)?;
        let meta_bytes: [u8; XOR_NAME_LEN] = bytes[..XOR_NAME_LEN]
            .try_into()
            .map_err(|_| Error::HexDeserializeFailed)?;
        let meta = XorName(meta_bytes);
        let owner_bytes: [u8; PK_SIZE] = bytes[XOR_NAME_LEN..]
            .try_into()
            .map_err(|_| Error::HexDeserializeFailed)?;
        let owner = PublicKey::from_bytes(owner_bytes).map_err(|_| Error::HexDeserializeFailed)?;
        Ok(Self { meta, owner })
    }

    /// Return the user chosen meta.
    pub fn meta(&self) -> XorName {
        self.meta
    }

    /// Return the owner.
    pub fn owner(&self) -> PublicKey {
        self.owner
    }
}

#[cfg(test)]
mod tests {
    use bls::SecretKey;

    use super::*;

    #[test]
    fn test_register_hex_conversion() {
        let mut rng = rand::thread_rng();
        let owner = SecretKey::random().public_key();
        let meta = XorName::random(&mut rng);
        let addr = RegisterAddress::new(meta, owner);
        let hex = &addr.to_hex();
        let addr2 = RegisterAddress::from_hex(hex).unwrap();

        assert_eq!(addr, addr2);

        let bad_hex = format!("{hex}0");
        let err = RegisterAddress::from_hex(&bad_hex);
        assert_eq!(err, Err(Error::HexDeserializeFailed));
    }
}