stellar_base/
network.rs

1//! Stellar Network.
2use crate::crypto;
3
4pub const PUBLIC_PASSPHRASE: &str = "Public Global Stellar Network ; September 2015";
5pub const TEST_PASSPHRASE: &str = "Test SDF Network ; September 2015";
6
7/// A Stellar Network.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct Network {
10    passphrase: String,
11}
12
13impl Network {
14    /// Create new network with `passphrase`.
15    pub fn new(passphrase: String) -> Network {
16        Network { passphrase }
17    }
18
19    /// Create new network with the same passphrase as SDF public network.
20    pub fn new_public() -> Network {
21        Self::new(PUBLIC_PASSPHRASE.to_string())
22    }
23
24    /// Create new network with the same passphrase as SDF test network.
25    pub fn new_test() -> Network {
26        Self::new(TEST_PASSPHRASE.to_string())
27    }
28
29    /// Retrieves the network passphrase.
30    pub fn passphrase(&self) -> &str {
31        &self.passphrase
32    }
33
34    /// Retrieves the network id, which is the hash of the network passphrase.
35    pub fn network_id(&self) -> Vec<u8> {
36        crypto::hash(self.passphrase.as_bytes())
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::Network;
43
44    #[test]
45    fn test_public_network_id() {
46        let network = Network::new_public();
47        let id = network.network_id();
48        let expected_id = vec![
49            0x7A, 0xC3, 0x39, 0x97, 0x54, 0x4E, 0x31, 0x75, 0xD2, 0x66, 0xBD, 0x02, 0x24, 0x39,
50            0xB2, 0x2C, 0xDB, 0x16, 0x50, 0x8C, 0x01, 0x16, 0x3F, 0x26, 0xE5, 0xCB, 0x2A, 0x3E,
51            0x10, 0x45, 0xA9, 0x79,
52        ];
53        assert_eq!(id, expected_id);
54    }
55
56    #[test]
57    fn test_test_network_id() {
58        let network = Network::new_test();
59        let id = network.network_id();
60        let expected_id = vec![
61            0xCE, 0xE0, 0x30, 0x2D, 0x59, 0x84, 0x4D, 0x32, 0xBD, 0xCA, 0x91, 0x5C, 0x82, 0x03,
62            0xDD, 0x44, 0xB3, 0x3F, 0xBB, 0x7E, 0xDC, 0x19, 0x05, 0x1E, 0xA3, 0x7A, 0xBE, 0xDF,
63            0x28, 0xEC, 0xD4, 0x72,
64        ];
65        assert_eq!(id, expected_id);
66    }
67}