Documentation
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
use std::{
    fmt::{self, Debug, Display, Formatter},
    str::FromStr,
};

use anyhow::Context;
use argon2::Argon2;
use arrayref::array_ref;
use base32::Alphabet;
use base64::{engine::general_purpose, Engine as _};
use bytemuck::{Pod, Zeroable};
use bytes::Bytes;
use rand::Rng;
use serde::{Deserialize, Serialize};
use thiserror::Error;

/// Derive a key from a human-readable seed. Uses Argon2.
pub fn kdf_from_human(human: &str, salt: &str) -> [u8; 32] {
    let mut output_key_material = [0u8; 32];
    Argon2::default()
        .hash_password_into(human.as_bytes(), salt.as_bytes(), &mut output_key_material)
        .unwrap();
    output_key_material
}

#[derive(Error, Debug, Deserialize, Serialize)]
pub enum VerifyError {
    #[error("The signature is corrupt")]
    SignatureCorrupt,

    #[error("The signature mismatches")]
    SignatureMismatch,
}
/// The public half of an "identity" on the network.
///
/// Underlying representation is a Ed25519 public key.
#[derive(Serialize, Debug, Deserialize, Clone, Copy, PartialEq, PartialOrd, Ord, Eq)]
pub struct HavenIdentityPublic([u8; 32]);

impl TryFrom<Vec<u8>> for HavenIdentityPublic {
    type Error = Vec<u8>;

    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
        Ok(Self(value.try_into()?))
    }
}

impl AsRef<[u8]> for HavenIdentityPublic {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

impl HavenIdentityPublic {
    /// Verifies a message supposedly signed by this key.
    pub fn verify(&self, msg: &[u8], sig: &[u8]) -> Result<(), VerifyError> {
        let pk = ed25519_consensus::VerificationKeyBytes::from(self.0);
        let pk = ed25519_consensus::VerificationKey::try_from(pk)
            .map_err(|_| VerifyError::SignatureCorrupt)?;
        let sig: [u8; 64] = sig.try_into().map_err(|_| VerifyError::SignatureCorrupt)?;
        let sig = ed25519_consensus::Signature::from(sig);

        pk.verify(&sig, msg)
            .map_err(|_| VerifyError::SignatureMismatch)
    }

    /// The hash-based fingerprint of this identity.
    pub fn fingerprint(&self) -> HavenFingerprint {
        let hash = blake3::keyed_hash(b"fingerprint_____________________", &self.0);
        HavenFingerprint::from_bytes(array_ref![hash.as_bytes(), 0, 20])
    }
}

/// The secret half of an "identity" on the network.
///
/// Underlying representation is a Ed25519 "seed".
#[derive(Serialize, Deserialize, Clone, PartialEq, PartialOrd, Ord, Eq, Copy, Hash, Debug)]
pub struct HavenIdentitySecret([u8; 32]);

impl FromStr for HavenIdentitySecret {
    type Err = base64::DecodeError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let decoded = general_purpose::STANDARD.decode(s)?;
        if decoded.len() == 32 {
            let mut array = [0u8; 32];
            array.copy_from_slice(&decoded);
            Ok(HavenIdentitySecret(array))
        } else {
            Err(base64::DecodeError::InvalidLength)
        }
    }
}

impl HavenIdentitySecret {
    /// Derive an identity from a human-readable seed.
    pub fn from_seed(id_seed: &str) -> Self {
        HavenIdentitySecret::from_bytes(&kdf_from_human(id_seed, "identity_kdf_salt"))
    }

    /// Generates a new random secret identity.
    pub fn generate() -> Self {
        Self(rand::thread_rng().gen())
    }

    /// Returns the public half of this secret identity.
    pub fn public(&self) -> HavenIdentityPublic {
        let sk = ed25519_consensus::SigningKey::from(self.0);
        HavenIdentityPublic(sk.verification_key().to_bytes())
    }

    /// Signs a message, returning a signature.
    pub fn sign(&self, msg: &[u8]) -> Bytes {
        let sk = ed25519_consensus::SigningKey::from(self.0);
        sk.sign(msg).to_bytes().to_vec().into()
    }

    /// Convert from bytes representation
    pub fn from_bytes(b: &[u8; 32]) -> Self {
        Self(*b)
    }

    /// View as bytes representation
    pub fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }
}

/// A haven fingerprint is used to uniquely identify Earendil havens
#[repr(C)]
#[derive(
    Clone, Copy, PartialEq, PartialOrd, Ord, Eq, Hash, Serialize, Deserialize, Pod, Zeroable,
)]
pub struct HavenFingerprint([u8; 20]);

impl Display for HavenFingerprint {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let b64 = base32::encode(Alphabet::Crockford, &self.0).to_lowercase();
        write!(f, "{}", b64)
    }
}

impl Debug for HavenFingerprint {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let b64 = base32::encode(Alphabet::Crockford, &self.0).to_lowercase();
        write!(f, "{}", b64)
    }
}

impl FromStr for HavenFingerprint {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let bytes = base32::decode(Alphabet::Crockford, s).context("could not decode base32")?;
        if bytes.len() == 20 {
            let mut arr = [0u8; 20];
            arr.copy_from_slice(&bytes);
            Ok(HavenFingerprint(arr))
        } else {
            Err(anyhow::anyhow!("Invalid haven fingerprint length"))
        }
    }
}

impl HavenFingerprint {
    /// Convert from bytes representation
    pub fn from_bytes(b: &[u8; 20]) -> Self {
        Self(*b)
    }

    /// View as bytes representation
    pub fn as_bytes(&self) -> &[u8; 20] {
        &self.0
    }
}

#[derive(Copy, Clone, Deserialize, Serialize, Hash, Debug, PartialEq, PartialOrd, Ord, Eq)]
pub struct HavenEndpoint {
    pub fingerprint: HavenFingerprint,
    pub port: u16,
}

impl HavenEndpoint {
    pub fn new(fingerprint: HavenFingerprint, port: u16) -> Self {
        Self { fingerprint, port }
    }
}

impl Display for HavenEndpoint {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}", self.fingerprint, self.port)
    }
}

impl FromStr for HavenEndpoint {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let parts: Vec<&str> = s.split(':').collect();
        if parts.len() != 2 {
            return Err(anyhow::anyhow!("invalid haven endpoint format"));
        }
        let fingerprint = HavenFingerprint::from_str(parts[0])?;
        let port = u16::from_str(parts[1])?;
        Ok(HavenEndpoint::new(fingerprint, port))
    }
}

/// The public half of a "relay identity" on the network.
///
/// Underlying representation is a Ed25519 public key.
#[derive(Serialize, Debug, Deserialize, Clone, Copy, PartialEq, PartialOrd, Ord, Eq)]
pub struct RelayIdentityPublic([u8; 32]);

impl TryFrom<Vec<u8>> for RelayIdentityPublic {
    type Error = Vec<u8>;

    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
        Ok(Self(value.try_into()?))
    }
}

impl AsRef<[u8]> for RelayIdentityPublic {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

impl RelayIdentityPublic {
    /// Verifies a message supposedly signed by this key.
    pub fn verify(&self, msg: &[u8], sig: &[u8]) -> Result<(), VerifyError> {
        let pk = ed25519_consensus::VerificationKeyBytes::from(self.0);
        let pk = ed25519_consensus::VerificationKey::try_from(pk)
            .map_err(|_| VerifyError::SignatureCorrupt)?;
        let sig: [u8; 64] = sig.try_into().map_err(|_| VerifyError::SignatureCorrupt)?;
        let sig = ed25519_consensus::Signature::from(sig);

        pk.verify(&sig, msg)
            .map_err(|_| VerifyError::SignatureMismatch)
    }

    /// The hash-based fingerprint of this identity.
    pub fn fingerprint(&self) -> RelayFingerprint {
        let hash = blake3::keyed_hash(b"fingerprint_____________________", &self.0);
        RelayFingerprint::from_bytes(hash.as_bytes())
    }
}

/// The secret half of a "relay identity" on the network.
///
/// Underlying representation is a Ed25519 "seed".
#[derive(Serialize, Deserialize, Clone, PartialEq, PartialOrd, Ord, Eq, Copy, Hash, Debug)]
pub struct RelayIdentitySecret([u8; 32]);

impl FromStr for RelayIdentitySecret {
    type Err = base64::DecodeError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let decoded = general_purpose::STANDARD.decode(s)?;
        if decoded.len() == 32 {
            let mut array = [0u8; 32];
            array.copy_from_slice(&decoded);
            Ok(RelayIdentitySecret(array))
        } else {
            Err(base64::DecodeError::InvalidLength)
        }
    }
}

impl RelayIdentitySecret {
    /// Derive an identity from a human-readable seed.
    pub fn from_seed(id_seed: &str) -> Self {
        RelayIdentitySecret::from_bytes(&kdf_from_human(id_seed, "identity_kdf_salt"))
    }

    /// Generates a new random secret identity.
    pub fn generate() -> Self {
        Self(rand::thread_rng().gen())
    }

    /// Returns the public half of this secret identity.
    pub fn public(&self) -> RelayIdentityPublic {
        let sk = ed25519_consensus::SigningKey::from(self.0);
        RelayIdentityPublic(sk.verification_key().to_bytes())
    }

    /// Signs a message, returning a signature.
    pub fn sign(&self, msg: &[u8]) -> Bytes {
        let sk = ed25519_consensus::SigningKey::from(self.0);
        sk.sign(msg).to_bytes().to_vec().into()
    }

    /// Convert from bytes representation
    pub fn from_bytes(b: &[u8; 32]) -> Self {
        Self(*b)
    }

    /// View as bytes representation
    pub fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }
}

/// An Earendil node fingerprint, uniquely identifying a relay.
#[repr(C)]
#[derive(
    Clone, Copy, PartialEq, PartialOrd, Ord, Eq, Hash, Serialize, Deserialize, Pod, Zeroable,
)]
pub struct RelayFingerprint([u8; 32]);
impl Display for RelayFingerprint {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let hex_str = hex::encode(self.0).to_lowercase(); // Convert bytes to hex string
        write!(f, "{}", hex_str)
    }
}

impl Debug for RelayFingerprint {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let hex_str = hex::encode(self.0).to_lowercase(); // Convert bytes to hex string
        write!(f, "{}", hex_str)
    }
}

impl FromStr for RelayFingerprint {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let bytes = hex::decode(s).context("could not decode hex")?;
        if bytes.len() == 32 {
            let mut arr = [0u8; 32];
            arr.copy_from_slice(&bytes);
            Ok(RelayFingerprint(arr))
        } else {
            Err(anyhow::anyhow!("Invalid relay fingerprint length"))
        }
    }
}

impl RelayFingerprint {
    /// Convert from bytes representation
    pub fn from_bytes(b: &[u8; 32]) -> Self {
        Self(*b)
    }

    /// View as bytes representation
    pub fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }
}

#[repr(C)]
#[derive(
    Clone, Copy, PartialEq, PartialOrd, Ord, Eq, Hash, Serialize, Deserialize, Pod, Zeroable,
)]
pub struct AnonEndpoint(pub [u8; 16]);

impl AnonEndpoint {
    pub fn random() -> Self {
        let new_anon_id: [u8; 16] = rand::thread_rng().gen();
        AnonEndpoint(new_anon_id)
    }
}

impl Display for AnonEndpoint {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let hex_string = hex::encode(self.0);
        write!(f, "ANON-{}", hex_string)
    }
}

impl Debug for AnonEndpoint {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let hex_string = hex::encode(self.0);
        write!(f, "ANON-{}", hex_string)
    }
}

#[derive(Copy, Clone, Deserialize, Serialize, Hash, Debug, PartialEq, PartialOrd, Ord, Eq)]
pub struct RelayEndpoint {
    pub fingerprint: RelayFingerprint,
    pub dock: u32,
}

impl RelayEndpoint {
    pub fn new(fingerprint: RelayFingerprint, dock: u32) -> Self {
        Self { fingerprint, dock }
    }
}

impl Display for RelayEndpoint {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}", self.fingerprint, self.dock)
    }
}

impl FromStr for RelayEndpoint {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let parts: Vec<&str> = s.split(':').collect();
        if parts.len() != 2 {
            return Err(anyhow::anyhow!("invalid relay endpoint format"));
        }
        let fingerprint = RelayFingerprint::from_str(parts[0])?;
        let dock = u32::from_str(parts[1])?;
        Ok(RelayEndpoint::new(fingerprint, dock))
    }
}

pub type ClientId = u64;

#[derive(Clone, Copy, Eq, PartialEq, Debug, Hash)]
pub enum RemoteId {
    Relay(RelayFingerprint),
    Anon(AnonEndpoint),
}