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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

//! # Discovered user table
//!
//! This table contains all users known to this node.

use libp2p::{identity::PublicKey, PeerId};
use prost::Message;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha512};
use state::Storage;
use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::sync::RwLock;

use super::router_net_proto;
use super::table::RoutingTable;
use crate::node::user_accounts::UserAccounts;
use crate::rpc::Rpc;
use crate::services::group::group_id::GroupId;
use crate::storage::database::DbUsers;
use crate::utilities::qaul_id::QaulId;

/// Import protobuf users RPC message definition generated by
/// the rust module prost-build.
pub mod proto {
    include!("qaul.rpc.users.rs");
}

/// Import protobuf router_net_info definitions
pub mod proto_net {
    include!("qaul.net.router_net_info.rs");
}

/// mutable state of users table
static USERS: Storage<RwLock<Users>> = Storage::new();

/// implementation of all known users for routing references
pub struct Users {
    /// the BTreeMap key is the 8 byte qaul ID (q8id)
    pub users: BTreeMap<Vec<u8>, User>,
}

impl Users {
    /// Initialize the router::users::Users module
    /// this module is automatically initialized
    /// when the router module is initialized
    pub fn init() {
        {
            // create users table and save it to state
            let users = Users {
                users: BTreeMap::new(),
            };
            USERS.set(RwLock::new(users));
        }

        // fill user table with users from data base
        let tree = DbUsers::get_tree();
        let mut users = USERS.get().write().unwrap();
        // iterate over all values in db
        for res in tree.iter() {
            if let Ok((_vec, user)) = res {
                // encode values from bytes
                let q8id = QaulId::bytes_to_q8id(user.id.clone());
                let id = PeerId::from_bytes(&user.id).unwrap();
                let key = PublicKey::from_protobuf_encoding(&user.key).unwrap();
                // fill result into user table
                users.users.insert(
                    q8id,
                    User {
                        id,
                        key,
                        name: user.name,
                        verified: user.verified,
                        blocked: user.blocked,
                    },
                );
            }
        }
    }

    /// add a new user
    ///
    /// This user will be added to the users list in memory and to the data base
    pub fn add(id: PeerId, key: PublicKey, name: String, verified: bool, blocked: bool) {
        // save user to the data base
        DbUsers::add_user(UserData {
            id: id.to_bytes(),
            key: key.clone().to_protobuf_encoding(),
            name: name.clone(),
            verified,
            blocked,
        });

        // add user to the users table
        let q8id = QaulId::to_q8id(id.clone());
        let mut users = USERS.get().write().unwrap();
        users.users.insert(
            q8id,
            User {
                id,
                key,
                name,
                verified,
                blocked,
            },
        );
    }

    /// add a new user to the users list, and check whether the
    /// User ID matches the public key
    /// and save it to the data base
    pub fn add_with_check(id: PeerId, key: PublicKey, name: String) {
        // check if user is valid
        if id != key.clone().to_peer_id() {
            log::error!("user id & key do not match {}", id.to_base58());
            return;
        }

        // check if user already exists
        {
            let q8id = QaulId::to_q8id(id.clone());
            let users = USERS.get().read().unwrap();

            // check if user already exists
            if users.users.contains_key(&q8id) {
                return;
            }
        }
        // add user
        Self::add(id, key, name, false, false);
    }

    /// check missed users from ids
    pub fn get_missed_ids(ids: &Vec<Vec<u8>>) -> Vec<Vec<u8>> {
        let mut res: Vec<Vec<u8>> = vec![];
        let users = USERS.get().read().unwrap();
        for id in ids {
            if !users.users.contains_key(id) {
                res.push(id.clone());
            }
        }
        return res;
    }

    /// get the public key of a known user
    pub fn get_pub_key(user_id: &PeerId) -> Option<PublicKey> {
        // get q8id
        let q8id = QaulId::to_q8id(user_id.to_owned());

        // get public key
        Self::get_pub_key_by_q8id(&q8id)
    }

    /// get the public key of a known user by it's q8id
    pub fn get_pub_key_by_q8id(q8id: &Vec<u8>) -> Option<PublicKey> {
        let store = USERS.get().read().unwrap();
        let result = store.users.get(q8id);
        match result {
            Some(user) => Some(user.key.clone()),
            None => None,
        }
    }

    /// get user by q8id
    pub fn get_user_id_by_q8id(q8id: Vec<u8>) -> Option<PeerId> {
        let store = USERS.get().read().unwrap();

        if let Some(user) = store.users.get(&q8id) {
            return Some(user.id);
        }

        None
    }

    /// create and send the user info table for the
    /// RouterInfo message which is sent regularly to neighbours
    ///
    /// This is a wrapper function for the PeerIds for the function
    /// `get_user_info_table_by_q8ids(q8ids)`
    pub fn _get_user_info_table_by_ids(ids: &Vec<PeerId>) -> router_net_proto::UserInfoTable {
        let mut q8ids: Vec<Vec<u8>> = Vec::new();
        for id in ids {
            // convert qaul ID to q8id
            let q8id = QaulId::to_q8id(id.to_owned());
            q8ids.push(q8id);
        }

        Self::get_user_info_table_by_q8ids(&q8ids)
    }

    /// create and send the user info table for the
    /// RouterInfo message which is sent regularly to neighbours
    pub fn get_user_info_table_by_q8ids(q8ids: &Vec<Vec<u8>>) -> router_net_proto::UserInfoTable {
        let store = USERS.get().read().unwrap();
        let mut users = router_net_proto::UserInfoTable { info: Vec::new() };

        for q8id in q8ids {
            if let Some(value) = store.users.get(q8id) {
                let user_info = router_net_proto::UserInfo {
                    id: value.id.to_bytes(),
                    key: value.key.clone().to_protobuf_encoding(),
                    name: value.name.clone(),
                };
                users.info.push(user_info);
            }
        }
        users
    }

    /// add new users from the received bytes of a UserInfoTable
    pub fn add_user_info_table(users: &Vec<router_net_proto::UserInfo>) {
        // loop through it and add it to the users list
        for value in users {
            let id_result = PeerId::from_bytes(&value.id);
            let key_result = PublicKey::from_protobuf_encoding(&value.key);

            if let (Ok(id), Ok(key)) = (id_result, key_result) {
                Self::add_with_check(id, key, value.name.clone());
            }
        }
    }

    fn compare(a: &[u8], b: &[u8]) -> Ordering {
        for (ai, bi) in a.iter().zip(b.iter()) {
            match ai.cmp(&bi) {
                Ordering::Equal => continue,
                ord => return ord,
            }
        }

        // if every single element was equal, compare length
        a.len().cmp(&b.len())
    }

    /// get security number
    fn get_security_number(my_user: &PeerId, user_id: &Vec<u8>) -> Result<Vec<u8>, String> {
        let q8id = QaulId::bytes_to_q8id(user_id.clone());
        let q8id_my = QaulId::to_q8id(my_user.clone());

        // find user from users
        let users = USERS.get().read().unwrap();
        if !users.users.contains_key(&q8id) {
            return Err("user no exists".to_string());
        }

        if !users.users.contains_key(&q8id_my) {
            return Err("my user is not existed".to_string());
        }
        let mut key1 = users
            .users
            .get(&q8id_my)
            .unwrap()
            .key
            .to_protobuf_encoding();
        let mut key2 = users.users.get(&q8id).unwrap().key.to_protobuf_encoding();

        // merge two keys
        let mut data: Vec<u8> = vec![];
        match Self::compare(&key1, &key2) {
            Ordering::Less => {
                data.append(&mut key1);
                data.append(&mut key2);
            }
            _ => {
                data.append(&mut key2);
                data.append(&mut key1);
            }
        }

        let mut key_data = data.clone();
        data.clear();

        for _ in 0..5200 {
            data.append(&mut key_data);
            let hash = Sha512::digest(&data);
            let mut hash_vec = hash[..64].to_vec();
            data.clear();
            data.append(&mut hash_vec);
        }
        Ok(data[..16].to_vec())
    }

    /// Process incoming RPC request messages
    pub fn rpc(data: Vec<u8>, user_id: Vec<u8>) {
        let account_id = PeerId::from_bytes(&user_id).unwrap();

        match proto::Users::decode(&data[..]) {
            Ok(users) => {
                match users.message {
                    Some(proto::users::Message::UserRequest(_user_request)) => {
                        // get users store
                        let users = USERS.get().read().unwrap();

                        // create empty user list
                        let mut user_list = proto::UserList { user: Vec::new() };

                        // get user account
                        if let Some(account) = UserAccounts::get_default_user() {
                            // get online users
                            let online_users = super::RoutingTable::get_online_users_info();

                            // fill them into the list
                            for (id, user) in &users.users {
                                // get RPC key values
                                let (_key_type, key_base58) =
                                    Self::get_protobuf_public_key(user.key.clone());

                                // create group id
                                let group_id =
                                    GroupId::from_peers(&account.id, &user.id).to_bytes();

                                let mut connectivity: i32 = 0;
                                let mut connections: Vec<proto::RoutingTableConnection> =
                                    Vec::new();

                                if let Some(entries) = online_users.get(id) {
                                    for entry in entries {
                                        connections.push(proto::RoutingTableConnection {
                                            module: entry.module.as_int(),
                                            hop_count: entry.hc as u32,
                                            rtt: entry.rtt,
                                            via: entry.node.to_bytes(),
                                        });
                                    }
                                    connectivity = 1;
                                }

                                // create user entry message
                                let user_entry = proto::UserEntry {
                                    name: user.name.clone(),
                                    id: user.id.to_bytes(),
                                    group_id,
                                    key_base58,
                                    connectivity: connectivity,
                                    verified: user.verified,
                                    blocked: user.blocked,
                                    connections,
                                };

                                // add entry to list
                                user_list.user.push(user_entry);
                            }
                        }

                        // create message
                        let proto_message = proto::Users {
                            message: Some(proto::users::Message::UserList(user_list)),
                        };

                        // encode message
                        let mut buf = Vec::with_capacity(proto_message.encoded_len());
                        proto_message
                            .encode(&mut buf)
                            .expect("Vec<u8> provides capacity as needed");

                        // send message
                        Rpc::send_message(
                            buf,
                            crate::rpc::proto::Modules::Users.into(),
                            "".to_string(),
                            Vec::new(),
                        );
                    }
                    Some(proto::users::Message::UserOnlineRequest(_user_online_request)) => {
                        // get users store
                        let users = USERS.get().read().unwrap();

                        // get all online user ids by passing last_sent=0
                        let online_user_ids = RoutingTable::get_online_user_ids(0);

                        // create empty user list
                        let mut user_list = proto::UserList { user: Vec::new() };

                        // get user account
                        if let Some(account) = UserAccounts::get_default_user() {
                            // get online uses info
                            let online_users = super::RoutingTable::get_online_users_info();
                            // fill them into the list
                            for id in &online_user_ids {
                                if let Some(user) = users.users.get(id) {
                                    // get RPC key values
                                    let (_key_type, key_base58) =
                                        Self::get_protobuf_public_key(user.key.clone());

                                    // create group id
                                    let group_id =
                                        GroupId::from_peers(&account.id, &user.id).to_bytes();

                                    let mut connectivity: i32 = 0;
                                    let mut connections: Vec<proto::RoutingTableConnection> =
                                        vec![];
                                    if online_users.contains_key(id) {
                                        connectivity = 1;
                                        let cnns = online_users.get(id).unwrap();
                                        for cnn in cnns {
                                            connections.push(proto::RoutingTableConnection {
                                                module: cnn.module.as_int(),
                                                hop_count: cnn.hc as u32,
                                                rtt: cnn.rtt,
                                                via: cnn.node.to_bytes(),
                                            });
                                        }
                                    }

                                    // create user entry message
                                    let user_entry = proto::UserEntry {
                                        name: user.name.clone(),
                                        id: user.id.to_bytes(),
                                        group_id,
                                        key_base58,
                                        connectivity,
                                        verified: user.verified,
                                        blocked: user.blocked,
                                        connections,
                                    };

                                    // add entry to list
                                    user_list.user.push(user_entry);
                                }
                            }
                        }

                        // create message
                        let proto_message = proto::Users {
                            message: Some(proto::users::Message::UserList(user_list)),
                        };

                        // encode message
                        let mut buf = Vec::with_capacity(proto_message.encoded_len());
                        proto_message
                            .encode(&mut buf)
                            .expect("Vec<u8> provides capacity as needed");

                        // send message
                        Rpc::send_message(
                            buf,
                            crate::rpc::proto::Modules::Users.into(),
                            "".to_string(),
                            Vec::new(),
                        );
                    }
                    Some(proto::users::Message::UserUpdate(updated_user)) => {
                        log::trace!("UserUpdate protobuf RPC message");

                        // create user id from bytes
                        if let Ok(user_id) = PeerId::from_bytes(&updated_user.id) {
                            // get users store
                            let mut users = USERS.get().write().unwrap();

                            let q8id = QaulId::to_q8id(user_id);

                            // search for user in list and update entry
                            match users.users.get_mut(&q8id) {
                                Some(user_result) => {
                                    let user = User {
                                        id: user_id,
                                        key: user_result.key.clone(),
                                        name: user_result.name.clone(),
                                        verified: updated_user.verified,
                                        blocked: updated_user.blocked,
                                    };

                                    // update list
                                    *user_result = user;

                                    // save to data base
                                    DbUsers::add_user(UserData {
                                        id: user_id.to_bytes(),
                                        key: user_result.key.clone().to_protobuf_encoding(),
                                        name: user_result.name.clone(),
                                        verified: updated_user.verified,
                                        blocked: updated_user.blocked,
                                    });
                                }
                                None => {
                                    log::error!("updated user is unknown: {}", user_id.to_base58())
                                }
                            }
                        } else {
                            log::error!("PeerId couldn't be created");
                        }
                    }
                    Some(proto::users::Message::SecurityNumberRequest(secure_req)) => {
                        match Self::get_security_number(&account_id, &secure_req.user_id) {
                            Ok(x) => {
                                let mut security_number_blocks: Vec<u32> = vec![];
                                for i in 0..x.len() / 2 {
                                    let number = x[i * 2] as u32 + (x[i * 2 + 1] as u32 * 256);
                                    security_number_blocks.push(number);
                                }

                                // create message
                                let proto_message = proto::Users {
                                    message: Some(proto::users::Message::SecurityNumberResponse(
                                        proto::SecurityNumberResponse {
                                            user_id: secure_req.user_id.clone(),
                                            security_hash: x.clone(),
                                            security_number_blocks,
                                        },
                                    )),
                                };

                                // encode message
                                let mut buf = Vec::with_capacity(proto_message.encoded_len());
                                proto_message
                                    .encode(&mut buf)
                                    .expect("Vec<u8> provides capacity as needed");

                                // send message
                                Rpc::send_message(
                                    buf,
                                    crate::rpc::proto::Modules::Users.into(),
                                    "".to_string(),
                                    Vec::new(),
                                );
                            }
                            Err(error) => {
                                log::error!("security number error: {}", error);
                            }
                        }
                    }
                    _ => {}
                }
            }
            Err(error) => {
                log::error!("{:?}", error);
            }
        }
    }

    /// create the qaul RPC definitions of a public key
    ///
    /// Returns a tuple with the key type & the base58 encoded
    /// (key_type: String, key_base58: String)
    pub fn get_protobuf_public_key(key: PublicKey) -> (String, String) {
        // extract values
        let key_type: String;
        let key_base58: String;

        match key {
            PublicKey::Ed25519(key) => {
                key_type = "Ed25519".to_owned();
                key_base58 = bs58::encode(key.encode()).into_string();
            }
            _ => {
                key_type = "UNDEFINED".to_owned();
                key_base58 = "UNDEFINED".to_owned();
            }
        }

        (key_type, key_base58)
    }
}

/// user structure
pub struct User {
    pub id: PeerId,
    pub key: PublicKey,
    pub name: String,
    pub verified: bool,
    pub blocked: bool,
}

/// user structure for storing it in the data base
#[derive(Serialize, Deserialize, Clone)]
pub struct UserData {
    pub id: Vec<u8>,
    pub key: Vec<u8>,
    pub name: String,
    pub verified: bool,
    pub blocked: bool,
}