libratman 0.7.0

Ratman types, client, and interface library
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
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
// SPDX-FileCopyrightText: 2019-2023 Katharina Fey <kookie@spacekookie.de>
//
// SPDX-License-Identifier: AGPL-3.0-or-later WITH LicenseRef-AppStore

use crate::{
    types::{error::UserError, identifiers::ID_LEN},
    EncodingError, RatmanError,
};
use serde::{
    de::{Deserializer, SeqAccess, Visitor},
    Deserialize, Serialize, Serializer,
};
use std::{
    fmt::{self, Debug, Display, Formatter},
    string::ToString,
};

#[cfg(feature = "metrics")]
use prometheus_client::encoding::text::Encode;

/// A random identifier for the Irdest ecosystem
///
/// Internally an ID is 32 bytes long.  This data can either be:
///
/// 1. A random identifier for any given resource.
///
/// 2. An ed25519 public key, backed by a corresponding private key.
///
/// API functions that _require_ an `Id` to be backed by a private key MUST wrap it via the
/// [`Address`](super::address::Address) type.
///
/// For every consumer that simply wants to identify a unique object, with a reasonable amount of
/// entropy to avoid collisions, this type is sufficient.
///
/// You can generate it via [`random()`](Ident32::random) function, or by hashing a piece of data
/// ([`with_digest()`](Ident32::with_digest)).  The hash function used is blake2.
#[derive(Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)]
pub struct Ident32([u8; ID_LEN]);

impl Debug for Ident32 {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "<ID: {}>", hex::encode_upper(self))
    }
}

impl Display for Ident32 {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}",
            hex::encode_upper(self)
                .as_bytes()
                .chunks(4)
                .map(std::str::from_utf8)
                .collect::<Result<Vec<_>, _>>()
                .unwrap()
                .join("-")
        )
    }
}

#[cfg(feature = "metrics")]
impl Encode for Ident32 {
    fn encode(&self, w: &mut dyn std::io::Write) -> std::io::Result<()> {
        write!(w, "{:}", self)
    }
}

impl Ident32 {
    /// Create an identity from the first 16 bytes of a vector
    ///
    /// This function will panic, if the provided vector isn't long
    /// enough, but extra data will simply be discarded.
    pub fn truncate(bytes: impl AsRef<[u8]>) -> Self {
        let bytes = bytes.as_ref();
        assert!(bytes.len() >= ID_LEN);

        Self(
            bytes
                .into_iter()
                .enumerate()
                .take(ID_LEN)
                .fold([0; ID_LEN], |mut buf, (i, u)| {
                    buf[i] = *u;
                    buf
                }),
        )
    }

    /// Create an identity from a byte slice
    ///
    /// If the slice is not long enough (or too long), an appropriate
    /// error will be returned, instead of panicking.
    pub fn try_from_bytes(buf: &[u8]) -> crate::Result<Self> {
        if buf.len() != ID_LEN {
            return Err(RatmanError::User(UserError::InvalidInput(
                format!("Wrong identifier length {}", buf.len()),
                Some(format!("{ID_LEN}")),
            ))
            .into());
        }

        Ok(Self::from_bytes(buf))
    }

    /// Create an identity from an exactly length-matched byte slice
    ///
    /// This function will panic, if the provided slice isn't exactly
    /// the length of the underlying identity implementation (see
    /// `ID_LEN`)
    pub fn from_bytes(buf: &[u8]) -> Self {
        assert_eq!(buf.len(), ID_LEN);
        Self(
            buf.into_iter()
                .enumerate()
                .fold([0; ID_LEN], |mut buf, (i, u)| {
                    buf[i] = *u;
                    buf
                }),
        )
    }

    pub fn pretty_string(&self) -> String {
        let base = self.to_string();

        let sections = base.split(|c| c == '-').into_iter().collect::<Vec<&str>>();

        let head = &sections[0..2];
        let tail = &sections[14..16];

        format!("[{}:{}::::{}:{}]", head[0], head[1], tail[0], tail[1])
    }

    pub fn from_string(s: &String) -> Self {
        let v: Vec<u8> = s
            .split("-")
            .map(|s| {
                hex::decode(s).expect(
                    "Don't call from_string() on input that was not serialised by to_string()!",
                )
            })
            .collect::<Vec<Vec<u8>>>()
            .into_iter()
            .flatten()
            .collect();
        Self::from_bytes(&v)
    }

    /// Create an un-initialised identifier which is all-zero
    pub fn uninit() -> Self {
        Self([0; 32])
    }

    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }

    /// Copy the contents out as a slice
    #[inline]
    pub fn slice(&self) -> [u8; ID_LEN] {
        self.0
    }

    /// Create an identity using a digest function
    ///
    /// This allows you to pass arbitrary length data which will
    /// result in a precise ID length data output.  The hash function
    /// is the cryptographic [blake2] cipher, so it can be used to
    /// turn secrets into identity information.
    ///
    /// [blake2]: https://blake2.net/
    pub fn with_digest<'vec, V: Into<&'vec Vec<u8>>>(vec: V) -> Self {
        use blake2::{
            digest::{Update, VariableOutput},
            VarBlake2b,
        };

        let mut hasher = VarBlake2b::new(ID_LEN).unwrap();
        hasher.update(vec.into());
        Self::truncate(hasher.finalize_boxed())
    }

    /// Generate a new random Identity
    pub fn random() -> Self {
        use rand::RngCore;
        let mut rng = rand::thread_rng();
        let mut buf = [0; ID_LEN];

        // TODO: replace this with an async generator which always has
        // SOME IDs ready to use and only runs occasionally to re-fill
        // a buffer.  That way we can rely on quick ID access without
        // random latency spikes if we end up generating a few IDs
        // that aren't suitble for being used as IDs.
        loop {
            // IDs are not allowed to have ZERO bytes because
            // otherwise a whole bunch of crap breaks elsewhere.
            rng.fill_bytes(&mut buf);

            // If we can't find a ZERO byte, we break from the loop
            if buf.iter().find(|x| *x == &0).is_none() {
                break;
            }

            trace!("ID generation failed, retrying...");
        }

        Self(buf)
    }

    /// Returns an iterator over the bytes of the identity
    pub fn iter<'a>(&'a self) -> impl Iterator<Item = &'a u8> {
        self.0.iter()
    }

    /// A cmp function which always loops over the full byte array
    ///
    /// This is done to avoid timing attacks based on invalid tokens.
    /// This mechanism isn't currently used as the default
    /// PartialEq/Eq implementation because the `Address` type relies
    /// on fast comparisons, which this does not provide.
    // TODO: can we do this with simd ??  Feels like yes.
    pub fn compare_constant_time(&self, other: &Self) -> bool {
        let mut valid = true;
        let mut ctr = 0;
        while ctr < ID_LEN {
            valid &= self.0[ctr] == other.0[ctr];
            ctr += 1;
        }
        valid
    }
}

/// Implement RAW `From` binary array
impl From<[u8; ID_LEN]> for Ident32 {
    fn from(i: [u8; ID_LEN]) -> Self {
        Self(i)
    }
}

/// Implement RAW `From` binary (reference) array
impl From<&[u8; ID_LEN]> for Ident32 {
    fn from(i: &[u8; ID_LEN]) -> Self {
        Self(i.clone())
    }
}

/// Implement binary array `From` RAW
impl From<Ident32> for [u8; ID_LEN] {
    fn from(i: Ident32) -> Self {
        i.0
    }
}

/// Implement binary array `From` RAW reference
impl From<&Ident32> for [u8; ID_LEN] {
    fn from(i: &Ident32) -> Self {
        i.0.clone()
    }
}

/// Implement RAW identity to binary array reference
impl AsRef<[u8]> for Ident32 {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

/// Iterator for iterating over `Identity`
pub struct Iter {
    index: usize,
    ident: Ident32,
}

impl Iterator for Iter {
    type Item = u8;
    fn next(&mut self) -> Option<Self::Item> {
        let ret = self.ident.0.get(self.index).map(|byte| *byte);
        self.index += 1;
        ret
    }
}

impl IntoIterator for Ident32 {
    type Item = u8;
    type IntoIter = Iter;
    fn into_iter(self) -> Self::IntoIter {
        Iter {
            index: 0,
            ident: self,
        }
    }
}

impl Serialize for Ident32 {
    fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        if ser.is_human_readable() {
            ser.serialize_str(&self.to_string())
        } else {
            ser.serialize_bytes(&self.0)
        }
    }
}

impl<'de> Deserialize<'de> for Ident32 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        use serde::de::Error;

        struct IdentityVisitor;

        impl IdentityVisitor {
            fn from_str<E: Error>(v: &str) -> Result<Ident32, E> {
                let v: Vec<u8> = v
                    .split("-")
                    .map(|s| hex::decode(s).map_err(|e| E::custom(e)))
                    // I don't like this way of propagating errors up but the alternative
                    // is a for loop which i also don't like
                    .collect::<Result<Vec<Vec<u8>>, E>>()?
                    .into_iter()
                    .flatten()
                    .collect();

                Self::from_bytes(&v)
            }

            fn from_bytes<E: Error, V: AsRef<[u8]>>(v: V) -> Result<Ident32, E> {
                let v = v.as_ref();
                if v.len() != ID_LEN {
                    return Err(E::custom(format!(
                        "Expected {} bytes, got {}",
                        ID_LEN,
                        v.len()
                    )));
                }

                Ok(Ident32(v.iter().enumerate().take(ID_LEN).fold(
                    [0; ID_LEN],
                    |mut buf, (i, u)| {
                        buf[i] = *u;
                        buf
                    },
                )))
            }
        }

        impl<'de> Visitor<'de> for IdentityVisitor {
            type Value = Ident32;

            fn expecting(&self, f: &mut Formatter) -> fmt::Result {
                write!(
                    f,
                    "Either a {l} byte array or a hex string representing {l} bytes",
                    l = ID_LEN
                )
            }

            fn visit_borrowed_str<E: Error>(self, v: &'de str) -> Result<Self::Value, E> {
                Self::from_str(v)
            }

            fn visit_string<E: Error>(self, v: String) -> Result<Self::Value, E> {
                Self::from_str(&v)
            }

            fn visit_borrowed_bytes<E: Error>(self, v: &'de [u8]) -> Result<Self::Value, E> {
                Self::from_bytes(v)
            }

            fn visit_byte_buf<E: Error>(self, v: Vec<u8>) -> Result<Self::Value, E> {
                Self::from_bytes(v)
            }

            fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
            where
                A: SeqAccess<'de>,
            {
                let mut v = Vec::new();
                while let Some(b) = seq.next_element::<u8>()? {
                    v.push(b);
                }

                Self::from_bytes(v)
            }
        }

        if deserializer.is_human_readable() {
            deserializer.deserialize_str(IdentityVisitor)
        } else {
            deserializer.deserialize_bytes(IdentityVisitor)
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use bincode;
    use serde_json;

    #[test]
    fn json_serde() {
        let s = b"Yes, we will make total destroy.";
        let i = Ident32::truncate(&s.to_vec());
        let v = serde_json::to_string(&i).unwrap();
        assert_eq!(
            v,
            "\"5965-732C-2077-6520-7769-6C6C-206D-616B-6520-746F-7461-6C20-6465-7374-726F-792E\""
        );
        let i2 = serde_json::from_str(&v).unwrap();
        assert_eq!(i, i2);
    }

    #[test]
    fn bincode_serde() {
        let s = b"Yes, we will make total destroy.";
        let i = Ident32::truncate(&s.to_vec());
        let v: Vec<u8> = bincode::serialize(&i).unwrap();
        assert_eq!(
            v,
            vec![
                32, 0, 0, 0, 0, 0, 0, 0, 89, 101, 115, 44, 32, 119, 101, 32, 119, 105, 108, 108,
                32, 109, 97, 107, 101, 32, 116, 111, 116, 97, 108, 32, 100, 101, 115, 116, 114,
                111, 121, 46
            ],
        );
        let i2 = bincode::deserialize(&v).unwrap();
        assert_eq!(i, i2);
    }
}

impl TryFrom<&[u8]> for Ident32 {
    type Error = RatmanError;
    fn try_from(value: &[u8]) -> core::result::Result<Self, Self::Error> {
        Self::try_from_bytes(value)
    }
}

impl TryFrom<&str> for Ident32 {
    type Error = RatmanError;
    fn try_from(values: &str) -> core::result::Result<Self, Self::Error> {
        Ident32::try_from_bytes(
            values
                .split("-")
                .map(|chunk| {
                    let h = hex::decode(chunk)
                        .map_err(EncodingError::from)
                        .map_err(RatmanError::from);
                    h
                })
                .collect::<crate::Result<Vec<Vec<u8>>>>()?
                .into_iter()
                .flatten()
                .collect::<Vec<u8>>()
                .as_slice(),
        )
    }
}