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
// Copyright 2020 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0

use libp2p::{mplex::MplexConfig, pnet::PreSharedKey};
use multihash::{Blake2b256, Hasher};

lazy_static::lazy_static! {
    /// Mainnet config for mina p2p network
    pub static ref MAINNET_CONFIG : TransportConfig<'static> = TransportConfig
    {
        rendezvous_string: b"/coda/0.0.1/5f704cc0c82e0ed70e873f0893d7e06f148524e3f0bdae2afb02e7819a0c24d1",
        ..Default::default()
    };
}

/// Configuration type for [super::TransportBuilder]
#[derive(Clone, Debug)]
pub struct TransportConfig<'a> {
    /// Rendezvous string for configuring private network
    pub rendezvous_string: &'a [u8],
    /// Protocol name for configuring libp2p multiplexer
    pub mplex_protocol_name: &'static [u8],
}

impl<'a> TransportConfig<'a> {
    /// Gets [PreSharedKey] from the [TransportConfig] instance
    pub fn get_shared_key(&self) -> PreSharedKey {
        let mut hasher = Blake2b256::default();
        hasher.update(self.rendezvous_string);
        let hash = hasher.finalize();
        let mut psk_fixed: [u8; 32] = Default::default();
        psk_fixed.copy_from_slice(hash.as_ref());
        PreSharedKey::new(psk_fixed)
    }

    /// Gets [MplexConfig] from the [TransportConfig] instance
    pub fn get_mplex_config(&self) -> MplexConfig {
        let mut config = MplexConfig::new();
        config.set_protocol_name(self.mplex_protocol_name);
        config
    }
}

impl Default for TransportConfig<'_> {
    fn default() -> Self {
        Self {
            rendezvous_string: b"",
            mplex_protocol_name: b"/coda/mplex/1.0.0",
        }
    }
}