shuttle_core/
network.rs

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