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
use bytes::Bytes;
use smallvec::SmallVec;
use tl_proto::{BoxedConstructor, BoxedWrapper, TlRead, TlWrite};

use super::{adnl, HashRef};

#[derive(TlRead)]
#[tl(boxed)]
pub enum ValueResult<'tl> {
    #[tl(id = 0xe40cf774)]
    ValueFound(BoxedWrapper<Value<'tl>>),
    #[tl(id = 0xa2620568)]
    ValueNotFound(NodesOwned),
}

#[derive(TlWrite)]
#[tl(boxed)]
pub enum ValueResultOwned {
    #[tl(id = 0xe40cf774)]
    ValueFound(BoxedWrapper<ValueOwned>),
    #[tl(id = 0xa2620568)]
    ValueNotFound(NodesOwned),
}

#[derive(TlWrite, TlRead)]
pub struct Nodes<'tl> {
    pub nodes: SmallVec<[Node<'tl>; 5]>,
}

impl BoxedConstructor for Nodes<'_> {
    const TL_ID: u32 = 0x7974a0be;
}

#[derive(TlWrite, TlRead)]
pub struct NodesOwned {
    pub nodes: Vec<NodeOwned>,
}

impl BoxedConstructor for NodesOwned {
    const TL_ID: u32 = Nodes::TL_ID;
}

#[derive(Debug, Copy, Clone, TlWrite, TlRead)]
pub struct Node<'tl> {
    pub id: everscale_crypto::tl::PublicKey<'tl>,
    pub addr_list: adnl::AddressList,
    pub version: u32,
    pub signature: &'tl [u8],
}

impl BoxedConstructor for Node<'_> {
    const TL_ID: u32 = 0x84533248;
}

impl Node<'_> {
    pub fn as_equivalent_owned(&self) -> NodeOwned {
        NodeOwned {
            id: self.id.as_equivalent_owned(),
            addr_list: self.addr_list,
            version: self.version,
            signature: self.signature.to_vec().into(),
        }
    }
}

#[derive(Debug, Clone, TlWrite, TlRead)]
pub struct NodeOwned {
    pub id: everscale_crypto::tl::PublicKeyOwned,
    pub addr_list: adnl::AddressList,
    pub version: u32,
    pub signature: Bytes,
}

impl BoxedConstructor for NodeOwned {
    const TL_ID: u32 = Node::TL_ID;
}

impl NodeOwned {
    pub fn as_equivalent_ref(&self) -> Node {
        Node {
            id: self.id.as_equivalent_ref(),
            addr_list: self.addr_list,
            version: self.version,
            signature: &self.signature,
        }
    }
}

#[derive(Debug, Copy, Clone, TlWrite, TlRead)]
pub struct Value<'tl> {
    pub key: KeyDescription<'tl>,
    pub value: &'tl [u8],
    pub ttl: u32,
    pub signature: &'tl [u8],
}

impl BoxedConstructor for Value<'_> {
    const TL_ID: u32 = 0x90ad27cb;
}

impl Value<'_> {
    pub fn as_equivalent_owned(&self) -> ValueOwned {
        ValueOwned {
            key: self.key.as_equivalent_owned(),
            value: self.value.to_vec().into(),
            ttl: self.ttl,
            signature: self.signature.to_vec().into(),
        }
    }
}

#[derive(Debug, Clone, TlWrite, TlRead)]
pub struct ValueOwned {
    pub key: KeyDescriptionOwned,
    pub value: Bytes,
    pub ttl: u32,
    pub signature: Bytes,
}

impl BoxedConstructor for ValueOwned {
    const TL_ID: u32 = Value::TL_ID;
}

impl ValueOwned {
    pub fn as_equivalent_ref(&self) -> Value {
        Value {
            key: self.key.as_equivalent_ref(),
            value: &self.value,
            ttl: self.ttl,
            signature: &self.signature,
        }
    }
}

#[derive(Debug, Copy, Clone, TlWrite, TlRead)]
pub struct KeyDescription<'tl> {
    pub key: Key<'tl>,
    pub id: everscale_crypto::tl::PublicKey<'tl>,
    pub update_rule: UpdateRule,
    pub signature: &'tl [u8],
}

impl BoxedConstructor for KeyDescription<'_> {
    const TL_ID: u32 = 0x281d4e05;
}

impl KeyDescription<'_> {
    pub fn as_equivalent_owned(&self) -> KeyDescriptionOwned {
        KeyDescriptionOwned {
            key: self.key.as_equivalent_owned(),
            id: self.id.as_equivalent_owned(),
            update_rule: self.update_rule,
            signature: self.signature.to_vec().into(),
        }
    }
}

#[derive(Debug, Clone, TlWrite, TlRead)]
pub struct KeyDescriptionOwned {
    pub key: KeyOwned,
    pub id: everscale_crypto::tl::PublicKeyOwned,
    pub update_rule: UpdateRule,
    pub signature: Bytes,
}

impl BoxedConstructor for KeyDescriptionOwned {
    const TL_ID: u32 = KeyDescription::TL_ID;
}

impl KeyDescriptionOwned {
    pub fn as_equivalent_ref(&self) -> KeyDescription<'_> {
        KeyDescription {
            key: self.key.as_equivalent_ref(),
            id: self.id.as_equivalent_ref(),
            update_rule: self.update_rule,
            signature: &self.signature,
        }
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, TlWrite, TlRead)]
pub struct Key<'tl> {
    #[tl(size_hint = 32)]
    pub id: HashRef<'tl>,
    pub name: &'tl [u8],
    pub idx: u32,
}

impl BoxedConstructor for Key<'_> {
    const TL_ID: u32 = 0xf667de8f;
}

impl Key<'_> {
    pub fn as_equivalent_owned(&self) -> KeyOwned {
        KeyOwned {
            id: *self.id,
            name: self.name.to_vec().into(),
            idx: self.idx,
        }
    }
}

#[derive(Debug, Clone, TlWrite, TlRead)]
pub struct KeyOwned {
    #[tl(size_hint = 32)]
    pub id: [u8; 32],
    pub name: Bytes,
    pub idx: u32,
}

impl BoxedConstructor for KeyOwned {
    const TL_ID: u32 = Key::TL_ID;
}

impl KeyOwned {
    pub fn as_equivalent_ref(&self) -> Key {
        Key {
            id: &self.id,
            name: &self.name,
            idx: self.idx,
        }
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, TlWrite, TlRead)]
#[tl(boxed)]
pub enum UpdateRule {
    #[tl(id = 0x61578e14, size_hint = 0)]
    Anybody,
    #[tl(id = 0x26779383, size_hint = 0)]
    OverlayNodes,
    #[tl(id = 0xcc9f31f7, size_hint = 0)]
    Signature,
}

#[derive(Copy, Clone, TlWrite, TlRead)]
#[tl(boxed, id = 0x5a8aef81, size_hint = 8)]
pub struct Pong {
    pub random_id: u64,
}

#[derive(Copy, Clone, TlWrite, TlRead)]
#[tl(boxed, id = 0x7026fb08, size_hint = 0)]
pub struct Stored;