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
use holochain_serialized_bytes::prelude::*;
use std::sync::Arc;

/// Key refs represent shared secrets stored in the keystore.
/// They can either be user-specified, or auto-generated at time of
/// secret creation, or ingestion.
#[derive(Debug, Clone, SerializedBytes)]
pub struct XSalsa20Poly1305KeyRef(Arc<[u8]>);

impl serde::Serialize for XSalsa20Poly1305KeyRef {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_bytes(&self.0)
    }
}

impl<'de> serde::Deserialize<'de> for XSalsa20Poly1305KeyRef {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(serde::Deserialize)]
        #[serde(transparent)]
        struct I(#[serde(with = "serde_bytes")] Vec<u8>);
        let inner: I = serde::Deserialize::deserialize(deserializer)?;
        Ok(Self(inner.0.into_boxed_slice().into()))
    }
}

impl std::ops::Deref for XSalsa20Poly1305KeyRef {
    type Target = [u8];

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

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

impl std::borrow::Borrow<[u8]> for XSalsa20Poly1305KeyRef {
    fn borrow(&self) -> &[u8] {
        &self.0
    }
}

impl PartialEq for XSalsa20Poly1305KeyRef {
    fn eq(&self, other: &Self) -> bool {
        use subtle::ConstantTimeEq;
        self.0.ct_eq(&other.0).into()
    }
}

impl Eq for XSalsa20Poly1305KeyRef {}

impl From<&[u8]> for XSalsa20Poly1305KeyRef {
    #[inline(always)]
    fn from(b: &[u8]) -> Self {
        b.to_vec().into()
    }
}

impl<const N: usize> From<[u8; N]> for XSalsa20Poly1305KeyRef {
    #[inline(always)]
    fn from(b: [u8; N]) -> Self {
        b.to_vec().into()
    }
}

impl From<&Vec<u8>> for XSalsa20Poly1305KeyRef {
    #[inline(always)]
    fn from(b: &Vec<u8>) -> Self {
        b.clone().into()
    }
}

impl From<Vec<u8>> for XSalsa20Poly1305KeyRef {
    #[inline(always)]
    fn from(b: Vec<u8>) -> Self {
        b.into_boxed_slice().into()
    }
}

impl From<Box<[u8]>> for XSalsa20Poly1305KeyRef {
    #[inline(always)]
    fn from(b: Box<[u8]>) -> Self {
        Self(b.into())
    }
}