nym_statistics_common/
lib.rs

1// Copyright 2024 - Nym Technologies SA <contact@nymtech.net>
2// SPDX-License-Identifier: Apache-2.0
3
4//! Nym Statistics
5//!
6//! This crate contains basic statistics utilities and abstractions to be re-used and
7//! applied throughout both the client and gateway implementations.
8
9#![warn(clippy::expect_used)]
10#![warn(clippy::unwrap_used)]
11#![warn(clippy::todo)]
12#![warn(clippy::dbg_macro)]
13
14use nym_crypto::asymmetric::ed25519;
15use sha2::{Digest, Sha256};
16
17/// Client specific statistics interfaces and events.
18pub mod clients;
19/// Statistics related errors.
20pub mod error;
21/// Gateway specific statistics interfaces and events.
22pub mod gateways;
23/// Statistics reporting abstractions and implementations.
24pub mod report;
25/// Statistics related types.
26pub mod types;
27
28const CLIENT_ID_PREFIX: &str = "client_stats_id";
29const VPN_CLIENT_ID_PREFIX: &str = "vpnclient_stats_id";
30
31pub fn generate_client_stats_id(id_key: ed25519::PublicKey) -> String {
32    generate_stats_id(CLIENT_ID_PREFIX, id_key.to_base58_string())
33}
34
35pub fn generate_vpn_client_stats_id<M: AsRef<[u8]>>(seed: M) -> String {
36    generate_stats_id(VPN_CLIENT_ID_PREFIX, seed)
37}
38
39fn generate_stats_id<M: AsRef<[u8]>>(prefix: &str, id_seed: M) -> String {
40    let mut hasher = sha2::Sha256::new();
41    hasher.update(prefix);
42    hasher.update(&id_seed);
43    let output = hasher.finalize();
44    format!("{output:x}")
45}
46
47pub fn hash_identifier<M: AsRef<[u8]>>(identifier: M) -> String {
48    format!("{:x}", Sha256::digest(identifier))
49}