substrate_stellar_sdk/network.rs
1//! Stellar network passphrases
2
3use lazy_static::lazy_static;
4use sp_std::vec::Vec;
5
6use crate::utils::sha256::{sha256, BinarySha256Hash};
7
8/// A wrapper type for the passphrase of a Stellar network
9///
10/// The SHA-256 hash of this passphrase is used for signing transactions.
11/// This makes sure that a signed transaction is only valid for
12/// a network having the specified passphrase.
13pub struct Network {
14 passphrase: Vec<u8>,
15 id: BinarySha256Hash,
16}
17
18impl Network {
19 /// Construct a new `Network` for the given `passphrase`
20 pub fn new(passphrase: &[u8]) -> Network {
21 let id = sha256(passphrase);
22 let passphrase = passphrase.to_vec();
23 Network { passphrase, id }
24 }
25
26 /// Return the passphrase of the network
27 pub fn get_passphrase(&self) -> &Vec<u8> {
28 &self.passphrase
29 }
30
31 /// Return the SHA-256 hash of the passphrase
32 ///
33 /// This hash is used for signing transactions.
34 pub fn get_id(&self) -> &BinarySha256Hash {
35 &self.id
36 }
37}
38
39lazy_static! {
40 /// The `Network` for the standard test network passphrase
41 ///
42 /// This passphrase is `"Test SDF Network ; September 2015"`.
43 pub static ref TEST_NETWORK: Network = Network::new(b"Test SDF Network ; September 2015");
44
45 /// The `Network` for the standard public network passphrase
46 ///
47 /// This passphrase is `"Public Global Stellar Network ; September 2015"`.
48 pub static ref PUBLIC_NETWORK: Network =
49 Network::new(b"Public Global Stellar Network ; September 2015");
50}