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
use crate::crypto;

pub const PUBLIC_PASSPHRASE: &str = "Public Global Stellar Network ; September 2015";
pub const TEST_PASSPHRASE: &str = "Test SDF Network ; September 2015";

/// A Stellar Network.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Network {
    passphrase: String,
}

impl Network {
    /// Create new network with `passphrase`.
    pub fn new(passphrase: String) -> Network {
        Network { passphrase }
    }

    /// Create new network with the same passphrase as SDF public network.
    pub fn public() -> Network {
        Self::new(PUBLIC_PASSPHRASE.to_string())
    }

    /// Create new network with the same passphrase as SDF test network.
    pub fn test() -> Network {
        Self::new(TEST_PASSPHRASE.to_string())
    }

    /// Retrievs the network passphrase.
    pub fn passphrase(&self) -> &str {
        &self.passphrase
    }

    /// Retrievs the network id, which is the hash of the network passphrase.
    pub fn network_id(&self) -> Vec<u8> {
        crypto::hash(self.passphrase.as_bytes())
    }
}

#[cfg(test)]
mod tests {
    use super::Network;

    #[test]
    fn test_public_network_id() {
        let network = Network::public();
        let id = network.network_id();
        let expected_id = vec![
            0x7A, 0xC3, 0x39, 0x97, 0x54, 0x4E, 0x31, 0x75, 0xD2, 0x66, 0xBD, 0x02, 0x24, 0x39,
            0xB2, 0x2C, 0xDB, 0x16, 0x50, 0x8C, 0x01, 0x16, 0x3F, 0x26, 0xE5, 0xCB, 0x2A, 0x3E,
            0x10, 0x45, 0xA9, 0x79,
        ];
        assert_eq!(id, expected_id);
    }

    #[test]
    fn test_test_network_id() {
        let network = Network::test();
        let id = network.network_id();
        let expected_id = vec![
            0xCE, 0xE0, 0x30, 0x2D, 0x59, 0x84, 0x4D, 0x32, 0xBD, 0xCA, 0x91, 0x5C, 0x82, 0x03,
            0xDD, 0x44, 0xB3, 0x3F, 0xBB, 0x7E, 0xDC, 0x19, 0x05, 0x1E, 0xA3, 0x7A, 0xBE, 0xDF,
            0x28, 0xEC, 0xD4, 0x72,
        ];
        assert_eq!(id, expected_id);
    }
}