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
extern crate core;

pub mod ed25519;

#[cfg(feature = "tl-proto")]
pub mod tl {
    use std::ops::Deref;

    /// Public key which is used in protocol
    #[derive(Debug, Copy, Clone, Eq, PartialEq, tl_proto::TlRead, tl_proto::TlWrite)]
    #[tl(boxed)]
    pub enum PublicKey<'tl> {
        #[tl(id = 0x4813b4c6, size_hint = 32)]
        Ed25519 { key: &'tl [u8; 32] },
        #[tl(id = 0x34ba45cb)]
        Overlay { name: &'tl [u8] },
        #[tl(id = 0x2dbcadd4, size_hint = 32)]
        Aes { key: &'tl [u8; 32] },
        #[tl(id = 0xb61f450a)]
        Unencoded { data: &'tl [u8] },
    }

    impl PublicKey<'_> {
        pub fn as_equivalent_owned(&self) -> PublicKeyOwned {
            match self {
                &Self::Ed25519 { key } => PublicKeyOwned::Ed25519 { key: *key },
                Self::Overlay { name } => PublicKeyOwned::Overlay {
                    name: name.to_vec(),
                },
                &Self::Aes { key } => PublicKeyOwned::Aes { key: *key },
                Self::Unencoded { data } => PublicKeyOwned::Unencoded {
                    data: data.to_vec(),
                },
            }
        }
    }

    /// Public key which is used in protocol. Owned version
    #[derive(Debug, Clone, Eq, PartialEq, tl_proto::TlRead, tl_proto::TlWrite)]
    #[tl(boxed)]
    pub enum PublicKeyOwned {
        #[tl(id = 0x4813b4c6, size_hint = 32)]
        Ed25519 { key: [u8; 32] },
        #[tl(id = 0x34ba45cb)]
        Overlay { name: Vec<u8> },
        #[tl(id = 0x2dbcadd4, size_hint = 32)]
        Aes { key: [u8; 32] },
        #[tl(id = 0xb61f450a)]
        Unencoded { data: Vec<u8> },
    }

    impl PublicKeyOwned {
        pub fn as_equivalent_ref(&self) -> PublicKey<'_> {
            match self {
                Self::Ed25519 { key } => PublicKey::Ed25519 { key },
                Self::Overlay { name } => PublicKey::Overlay {
                    name: name.as_slice(),
                },
                Self::Aes { key } => PublicKey::Aes { key },
                Self::Unencoded { data } => PublicKey::Unencoded {
                    data: data.as_slice(),
                },
            }
        }
    }

    #[derive(Debug, Copy, Clone, Eq, PartialEq)]
    pub struct Signature<'a>(pub &'a [u8; 64]);

    impl Signature<'_> {
        #[inline(always)]
        pub fn as_equivalent_owned(&self) -> SignatureOwned {
            SignatureOwned(*self.0)
        }
    }

    impl Deref for Signature<'_> {
        type Target = [u8; 64];

        #[inline(always)]
        fn deref(&self) -> &Self::Target {
            self.0
        }
    }

    impl tl_proto::TlWrite for Signature<'_> {
        type Repr = tl_proto::Bare;

        #[inline(always)]
        fn max_size_hint(&self) -> usize {
            68 // 1 byte len + 64 bytes data + 3 bytes alignment
        }

        #[inline(always)]
        fn write_to<P: tl_proto::TlPacket>(&self, packet: &mut P) {
            <&[u8]>::write_to(&self.0.as_slice(), packet);
        }
    }

    impl<'a> tl_proto::TlRead<'a> for Signature<'a> {
        type Repr = tl_proto::Bare;

        #[inline(always)]
        fn read_from(packet: &'a [u8], offset: &mut usize) -> tl_proto::TlResult<Self> {
            <&'a [u8]>::read_from(packet, offset)?
                .try_into()
                .map(Self)
                .map_err(|_| tl_proto::TlError::UnexpectedEof)
        }
    }

    #[derive(Debug, Copy, Clone, Eq, PartialEq)]
    #[repr(transparent)]
    pub struct SignatureOwned(pub [u8; 64]);

    impl SignatureOwned {
        #[inline(always)]
        pub fn as_equivalent_ref(&self) -> Signature {
            Signature(&self.0)
        }
    }

    impl Deref for SignatureOwned {
        type Target = [u8; 64];

        #[inline(always)]
        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }

    impl tl_proto::TlWrite for SignatureOwned {
        type Repr = tl_proto::Bare;

        #[inline(always)]
        fn max_size_hint(&self) -> usize {
            68 // 1 byte len + 64 bytes data + 3 bytes alignment
        }

        #[inline(always)]
        fn write_to<P: tl_proto::TlPacket>(&self, packet: &mut P) {
            <&[u8]>::write_to(&self.0.as_slice(), packet);
        }
    }

    impl<'a> tl_proto::TlRead<'a> for SignatureOwned {
        type Repr = tl_proto::Bare;

        #[inline(always)]
        fn read_from(packet: &'a [u8], offset: &mut usize) -> tl_proto::TlResult<Self> {
            <&'a [u8]>::read_from(packet, offset)?
                .try_into()
                .map(Self)
                .map_err(|_| tl_proto::TlError::UnexpectedEof)
        }
    }
}