Skip to main content

tari_comms/
types.rs

1//  Copyright 2019 The Tari Project
2//
3//  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
4//  following conditions are met:
5//
6//  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
7//  disclaimer.
8//
9//  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
10//  following disclaimer in the documentation and/or other materials provided with the distribution.
11//
12//  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
13//  products derived from this software without specific prior written permission.
14//
15//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
16//  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17//  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18//  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19//  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
20//  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
21//  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22
23//! Common Tari comms types
24
25use multiaddr::{Multiaddr, Protocol};
26use serde::{Deserialize, Serialize};
27use tari_crypto::{
28    compressed_key::CompressedKey,
29    dhke::DiffieHellmanSharedSecret,
30    hash_domain,
31    keys::PublicKey,
32    ristretto::RistrettoPublicKey,
33    signatures::{CompressedSchnorrSignature, SchnorrSignature},
34};
35use tari_storage::lmdb_store::LMDBStore;
36
37use crate::peer_manager::database::PeerDatabaseSql;
38
39/// Public key type
40pub type CommsPublicKey = CompressedKey<RistrettoPublicKey>;
41pub type UncompressedCommsPublicKey = RistrettoPublicKey;
42pub type CommsSecretKey = <RistrettoPublicKey as PublicKey>::K;
43
44// Diffie-Hellman key exchange type
45pub type CommsDHKE = DiffieHellmanSharedSecret<RistrettoPublicKey>;
46
47/// Comms signature type
48pub type Signature = SchnorrSignature<RistrettoPublicKey, CommsSecretKey>;
49pub type CompressedSignature = CompressedSchnorrSignature<RistrettoPublicKey, CommsSecretKey>;
50
51/// Specify the RNG that should be used for random selection
52pub type CommsRng = rand::rngs::ThreadRng;
53
54/// Datastore and Database used for persistence storage
55pub type CommsDataStore = LMDBStore;
56
57pub type CommsDatabase = PeerDatabaseSql;
58
59/// Specify the address protocol
60#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
61pub enum TransportProtocol {
62    Ipv4,
63    Ipv6,
64    Onion,
65    Memory,
66}
67
68impl TransportProtocol {
69    pub fn get_all() -> Vec<TransportProtocol> {
70        vec![
71            TransportProtocol::Ipv4,
72            TransportProtocol::Ipv6,
73            TransportProtocol::Onion,
74            TransportProtocol::Memory,
75        ]
76    }
77
78    pub fn get_prefix(&self) -> &str {
79        match self {
80            TransportProtocol::Ipv4 => "/ip4",
81            TransportProtocol::Ipv6 => "/ip6",
82            TransportProtocol::Onion => "/onion",
83            TransportProtocol::Memory => "/memory",
84        }
85    }
86}
87
88impl From<Multiaddr> for TransportProtocol {
89    fn from(addr: Multiaddr) -> Self {
90        match addr.iter().next() {
91            Some(Protocol::Ip4(_)) => TransportProtocol::Ipv4,
92            Some(Protocol::Ip6(_)) => TransportProtocol::Ipv6,
93            Some(Protocol::Onion(_, _)) => TransportProtocol::Onion,
94            Some(Protocol::Onion3(_)) => TransportProtocol::Onion,
95            Some(Protocol::Memory(_)) => TransportProtocol::Memory,
96            _ => TransportProtocol::Ipv4,
97        }
98    }
99}
100
101impl From<&Multiaddr> for TransportProtocol {
102    fn from(addr: &Multiaddr) -> Self {
103        match addr.iter().next() {
104            Some(Protocol::Ip4(_)) => TransportProtocol::Ipv4,
105            Some(Protocol::Ip6(_)) => TransportProtocol::Ipv6,
106            Some(Protocol::Onion(_, _)) => TransportProtocol::Onion,
107            Some(Protocol::Onion3(_)) => TransportProtocol::Onion,
108            Some(Protocol::Memory(_)) => TransportProtocol::Memory,
109            _ => TransportProtocol::Ipv4,
110        }
111    }
112}
113
114impl From<&Multiaddr> for &TransportProtocol {
115    fn from(addr: &Multiaddr) -> Self {
116        match addr.iter().next() {
117            Some(Protocol::Ip4(_)) => &TransportProtocol::Ipv4,
118            Some(Protocol::Ip6(_)) => &TransportProtocol::Ipv6,
119            Some(Protocol::Onion(_, _)) => &TransportProtocol::Onion,
120            Some(Protocol::Onion3(_)) => &TransportProtocol::Onion,
121            Some(Protocol::Memory(_)) => &TransportProtocol::Memory,
122            _ => &TransportProtocol::Ipv4,
123        }
124    }
125}
126
127hash_domain!(CommsCoreHashDomain, "com.tari.comms.core", 0);