1use crate::message::Message;
22use chacha20poly1305::Nonce;
23use ed25519_dalek::PUBLIC_KEY_LENGTH;
24use paste::paste;
25use serde::{Deserialize, Serialize};
26use validator::Validate;
27use x25519_dalek::{PublicKey, ReusableSecret};
28
29pub type OnionPubkey = [u8; PUBLIC_KEY_LENGTH];
30pub type Ed25519Pubkey = [u8; 32];
31pub type Ed25519Singature = [u8; 64];
32
33pub type OnionUrl = String;
34pub type Body = String;
35pub type HexPubkey = String;
36pub type HexDomain = String;
37pub type HexSignature = String;
38pub type HexMessage = String;
39pub type HexCipher = String;
40pub type HexNonce = String;
41pub type HexPoW = String;
42pub type HexDiffieHellman = String;
43
44macro_rules! generate_getters {
45 ($struct_name:ident, $( $field:ident : $field_type:ty ),* ) => {
46 impl $struct_name {
47 $(
48 paste! {
49 pub fn [<get_ $field>](&self) -> $field_type {
50 self.$field.clone()
51 }
52 }
53 )*
54 }
55 };
56}
57
58#[derive(Debug, Deserialize, serde::Serialize, Validate)]
59pub struct RequestPubkey {
60 #[validate(length(equal = 64, message = "bytes length should be 64"))]
61 bytes: HexPubkey,
62}
63
64impl RequestPubkey {
65 pub fn new(bytes: HexPubkey) -> Self {
66 RequestPubkey { bytes }
67 }
68}
69
70generate_getters!(
71 RequestPubkey,
72 bytes: HexPubkey
73);
74
75#[derive(Debug, Deserialize, Serialize, Validate)]
76pub struct PoW {
77 #[validate(length(equal = 152, message = "bytes_to_verify length should be 64"))]
79 bytes: HexMessage,
80 #[validate(length(equal = 128, message = "signature length should be 128"))]
81 signature: HexSignature,
82}
83
84impl PoW {
85 pub fn new(bytes: HexMessage, signature: HexSignature) -> Self {
86 PoW { bytes, signature }
87 }
88}
89
90generate_getters!(
91 PoW,
92 bytes: HexMessage,
93 signature: HexSignature
94);
95
96impl Default for PoW {
97 fn default() -> Self {
98 PoW::new(hex::encode([0; 80]), hex::encode([0; 64]))
99 }
100}
101
102#[derive(Debug, Deserialize, Serialize, Validate)]
103pub struct SolvedPoW {
104 #[validate(length(equal = 64, message = "pubkey length should be 64"))]
105 pubkey: HexPubkey,
106 #[validate(length(equal = 64, message = "nonce length should be 64"))]
107 nonce: HexNonce,
108 #[validate(length(equal = 128, message = "signature length should be 128"))]
109 signature: HexSignature,
110}
111
112impl SolvedPoW {
113 pub fn new(pubkey: HexPubkey, nonce: HexNonce, signature: HexSignature) -> Self {
114 SolvedPoW {
115 pubkey,
116 nonce,
117 signature,
118 }
119 }
120}
121
122generate_getters!(
123 SolvedPoW,
124 pubkey: HexPubkey,
125 nonce: HexNonce,
126 signature: HexSignature
127);
128
129#[derive(Debug, Deserialize, Serialize, Validate)]
130pub struct SentMessage {
131 #[validate(length(equal = 64, message = "pubkey length should be 64"))]
132 pubkey: HexPubkey,
133 #[validate(length(max = 6000, message = "chiphertext length should be max 6000"))]
134 chiphertext: HexCipher,
135 #[validate(length(equal = 128, message = "signature length should be 128"))]
136 signature: HexSignature,
137}
138
139impl SentMessage {
140 pub fn new(pubkey: HexPubkey, chiphertext: HexCipher, signature: HexSignature) -> Self {
141 SentMessage {
142 pubkey,
143 chiphertext,
144 signature,
145 }
146 }
147}
148
149generate_getters!(
150 SentMessage,
151 pubkey: HexPubkey,
152 chiphertext: HexCipher,
153 signature: HexSignature
154);
155
156#[derive(Debug, Serialize, Deserialize)]
157pub struct DataCollection {
158 size: usize,
159 items: Vec<Message>,
160}
161
162impl Default for DataCollection {
163 fn default() -> Self {
164 DataCollection::new(0, vec![])
165 }
166}
167
168impl DataCollection {
169 pub fn new(size: usize, items: Vec<Message>) -> Self {
170 DataCollection { size, items }
171 }
172}
173
174generate_getters!(
175 DataCollection,
176 size: usize,
177 items: Vec<Message>
178);
179
180#[derive(Serialize, Deserialize, Debug)]
181pub struct ServerIndex {
182 index: usize,
183}
184
185impl ServerIndex {
186 pub fn new(index: usize) -> Self {
187 ServerIndex { index }
188 }
189}
190
191generate_getters!(
192 ServerIndex,
193 index: usize
194);
195
196impl Default for ServerIndex {
197 fn default() -> Self {
198 ServerIndex::new(0)
199 }
200}
201
202#[derive(Debug, Deserialize, Serialize, Validate)]
203pub struct IndexMsg {
204 index: usize,
205}
206
207impl IndexMsg {
208 pub fn new(index: usize) -> Self {
209 IndexMsg { index }
210 }
211}
212
213generate_getters!(
214 IndexMsg,
215 index: usize
216);
217
218#[derive(Debug, Deserialize, Serialize, Validate)]
219pub struct KeyExchange {
220 #[validate(length(equal = 64, message = "pubkey length should be 64"))]
221 ed25519_pubkey: HexPubkey,
222 #[validate(length(equal = 64, message = "nonce length should be 64"))]
223 nonce: HexNonce,
224 #[validate(length(equal = 152, message = "pow length should be 152"))]
225 pow: HexPoW,
226 #[validate(length(equal = 128, message = "pow_sig length should be 128"))]
227 pow_sig: HexPoW,
228 #[validate(length(equal = 64, message = "diffie_hellman length should be 64"))]
229 diffie_hellman: HexDiffieHellman,
230 #[validate(length(equal = 128, message = "diffie_hellman_sig length should be 128"))]
231 diffie_hellman_sig: HexSignature,
232}
233
234impl KeyExchange {
235 pub fn new(
236 ed25519_pubkey: HexPubkey,
237 nonce: HexNonce,
238 pow: HexPoW,
239 pow_sig: HexPoW,
240 diffie_hellman: HexDiffieHellman,
241 diffie_hellman_sig: HexSignature,
242 ) -> Self {
243 KeyExchange {
244 ed25519_pubkey,
245 nonce,
246 pow,
247 pow_sig,
248 diffie_hellman,
249 diffie_hellman_sig,
250 }
251 }
252}
253
254impl Default for KeyExchange {
255 fn default() -> Self {
256 KeyExchange::new(
257 String::default(),
258 String::default(),
259 String::default(),
260 String::default(),
261 String::default(),
262 String::default(),
263 )
264 }
265}
266
267generate_getters!(
268 KeyExchange,
269 ed25519_pubkey: HexPubkey,
270 nonce: HexNonce,
271 pow: HexPoW,
272 pow_sig: HexPoW,
273 diffie_hellman: HexDiffieHellman,
274 diffie_hellman_sig: HexSignature
275);
276
277pub struct Keypair {
280 bob_diffie_hellman_sk: ReusableSecret,
281 alice_diffie_hellman_pk: PublicKey,
282 shared_nonce: Nonce,
283 nonce_from_pow: HexNonce,
284 unix_timestamp_seconds: u64,
285}
286
287impl Keypair {
288 pub fn new(
289 bob_diffie_hellman_sk: ReusableSecret,
290 alice_diffie_hellman_pk: PublicKey,
291 shared_nonce: Nonce,
292 nonce_from_pow: HexNonce,
293 unix_timestamp_seconds: u64,
294 ) -> Self {
295 Keypair {
296 bob_diffie_hellman_sk,
297 alice_diffie_hellman_pk,
298 shared_nonce,
299 nonce_from_pow,
300 unix_timestamp_seconds,
301 }
302 }
303}
304
305generate_getters!(
306 Keypair,
307 bob_diffie_hellman_sk: ReusableSecret,
308 alice_diffie_hellman_pk: PublicKey,
309 shared_nonce: Nonce,
310 nonce_from_pow: HexNonce,
311 unix_timestamp_seconds: u64
312);