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
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//  Copyright (C) 2018  The Duniter Project Developers.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! Implements the Documents of DUNP (DUniter Network Protocol).

#![cfg_attr(feature = "strict", deny(warnings))]
#![deny(
    missing_debug_implementations,
    missing_copy_implementations,
    trivial_casts,
    trivial_numeric_casts,
    unsafe_code,
    unstable_features,
    unused_import_braces
)]

#[macro_use]
extern crate pest_derive;
#[cfg(test)]
#[macro_use]
extern crate pretty_assertions;
#[macro_use]
extern crate serde_derive;

pub mod network_endpoint;
pub mod network_head;
pub mod network_head_v2;
pub mod network_head_v3;
pub mod network_peer;

use crate::network_head::NetworkHead;
use crate::network_head_v3::NetworkHeadV3;
use crate::network_peer::PeerCard;
use crate::network_peer::PeerCardV11;
use dubp_documents::{TextDocumentParseError, TextDocumentParser};
use dup_crypto::hashs::*;
use dup_crypto::keys::*;
use pest::iterators::Pair;
use pest::Parser;
use std::fmt::{Display, Error, Formatter};

#[derive(Parser)]
#[grammar = "network_documents.pest"]
/// Parser for network documents
struct NetworkDocsParser;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
/// Network document
pub enum NetworkDocument {
    /// Peer
    Peer(Box<PeerCard>),
    /// Head
    Head(NetworkHead),
}

impl TextDocumentParser<Rule> for NetworkDocument {
    type DocumentType = NetworkDocument;

    fn parse(doc: &str) -> Result<NetworkDocument, TextDocumentParseError> {
        match NetworkDocsParser::parse(Rule::network_document, doc) {
            Ok(mut net_doc_pairs) => Ok(NetworkDocument::from_pest_pair(
                net_doc_pairs.next().unwrap().into_inner().next().unwrap(),
            )), // get and unwrap the `network_document` rule; never fails
            Err(pest_error) => Err(TextDocumentParseError::PestError(format!("{}", pest_error))),
        }
    }
    fn from_pest_pair(pair: Pair<Rule>) -> NetworkDocument {
        match pair.as_rule() {
            Rule::peer_v11 => {
                NetworkDocument::Peer(Box::new(PeerCard::V11(PeerCardV11::from_pest_pair(pair))))
            }
            Rule::head_v3 => NetworkDocument::Head(NetworkHead::V3(Box::new(
                NetworkHeadV3::from_pest_pair(pair),
            ))),
            _ => panic!("unexpected rule: {:?}", pair.as_rule()), // Grammar ensures that we never reach this line
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
/// ParseError
pub enum ParseError {
    /// Pest grammar error
    PestError(String),
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
/// Random identifier with which several Duniter nodes with the same network keypair can be differentiated
pub struct NodeId(pub u32);

impl Default for NodeId {
    fn default() -> NodeId {
        NodeId(0)
    }
}

impl Display for NodeId {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        write!(f, "{:x}", self.0)
    }
}

impl<'a> From<&'a str> for NodeId {
    fn from(source: &'a str) -> NodeId {
        NodeId(u32::from_str_radix(source, 16).expect("Fail to parse NodeId"))
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
/// Complete identifier of a duniter node.
pub struct NodeFullId(pub NodeId, pub PubKey);

impl Default for NodeFullId {
    fn default() -> NodeFullId {
        NodeFullId(
            NodeId::default(),
            PubKey::Ed25519(
                ed25519::PublicKey::from_base58("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
                    .unwrap(),
            ),
        )
    }
}

impl Display for NodeFullId {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        write!(f, "{}-{}", self.0, self.1)
    }
}

impl NodeFullId {
    /// Compute sha256 hash
    pub fn sha256(&self) -> Hash {
        Hash::compute(format!("{}", self).as_bytes())
    }
    /// To human string
    pub fn to_human_string(&self) -> String {
        let mut pubkey_string = self.1.to_string();
        pubkey_string.truncate(8);
        format!("{:8x}-{:8}", (self.0).0, pubkey_string)
    }
}

#[cfg(test)]
mod tests {
    use super::network_endpoint::*;
    use super::*;

    pub fn keypair1() -> ed25519::KeyPair {
        ed25519::KeyPairFromSaltedPasswordGenerator::with_default_parameters().generate(
            "JhxtHB7UcsDbA9wMSyMKXUzBZUQvqVyB32KwzS9SWoLkjrUhHV".as_bytes(),
            "JhxtHB7UcsDbA9wMSyMKXUzBZUQvqVyB32KwzS9SWoLkjrUhHV_".as_bytes(),
        )
    }

    #[test]
    fn parse_endpoint() {
        let issuer = PubKey::Ed25519(
            ed25519::PublicKey::from_base58("D9D2zaJoWYWveii1JRYLVK3J4Z7ZH3QczoKrnQeiM6mx")
                .unwrap(),
        );
        let node_id = NodeId(u32::from_str_radix("c1c39a0a", 16).unwrap());
        let full_id = NodeFullId(node_id, issuer);
        assert_eq!(
            EndpointV1::parse_from_raw("WS2P c1c39a0a i3.ifee.fr 80 /ws2p", issuer, 0, 0),
            Ok(EndpointV1 {
                issuer,
                api: NetworkEndpointApi(String::from("WS2P")),
                node_id: Some(node_id),
                hash_full_id: Some(full_id.sha256()),
                host: String::from("i3.ifee.fr"),
                port: 80,
                path: Some(String::from("ws2p")),
                raw_endpoint: String::from("WS2P c1c39a0a i3.ifee.fr 80 /ws2p"),
                last_check: 0,
                status: 0,
            })
        );
    }

    #[test]
    fn parse_endpoint2() {
        let issuer = PubKey::Ed25519(
            ed25519::PublicKey::from_base58("5gJYnQp8v7bWwk7EWRoL8vCLof1r3y9c6VDdnGSM1GLv")
                .unwrap(),
        );
        let node_id = NodeId(u32::from_str_radix("cb06a19b", 16).unwrap());
        let full_id = NodeFullId(node_id, issuer);
        assert_eq!(
            EndpointV1::parse_from_raw("WS2P cb06a19b g1.imirhil.fr 53012", issuer, 0, 0),
            Ok(EndpointV1 {
                issuer,
                api: NetworkEndpointApi(String::from("WS2P")),
                node_id: Some(node_id),
                hash_full_id: Some(full_id.sha256()),
                host: String::from("g1.imirhil.fr"),
                port: 53012,
                path: None,
                raw_endpoint: String::from("WS2P cb06a19b g1.imirhil.fr 53012"),
                last_check: 0,
                status: 0,
            })
        );
    }
}