-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhisper.rs
More file actions
318 lines (267 loc) · 9.84 KB
/
Copy pathwhisper.rs
File metadata and controls
318 lines (267 loc) · 9.84 KB
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
use binary_codec::{FromBytes, ToBytes};
use serde::{Deserialize, Serialize};
use serde_with::base64::{Base64, UrlSafe};
use serde_with::formats::Unpadded;
use serde_with::serde_as;
use crate::core::BucketId;
use crate::core::PlabbleDateTime;
use crate::crypto::algorithm::CryptoSignature;
use crate::network::node_info::NodeInfo;
use crate::packets::body::bucket::{BucketQuery, PutRequestBody};
use crate::packets::body::post::PostRequestBody;
/// Whisper metadata for conflict resolving
///
/// If the version is higher: accept
/// If the version is equal: accept if timestamp is lower (first come first serve)
/// If the version is lower: reject
/// If the version is equal and timestamp is equal: accept if node ID is higher (to break ties)
#[serde_as]
#[derive(FromBytes, ToBytes, Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct WhisperMetadata {
/// If applicable, indicates the keys in the message are in binary format (String)
#[toggles("binary_keys")]
pub binary_keys: bool,
/// Whether the message has a "from" field (some messages may be anonymous)
#[toggles("has_from")]
pub has_from: bool,
/// Node ID of the sender (same as certificate ID)
#[serde_as(as = "Option<Base64<UrlSafe, Unpadded>>")]
#[toggled_by = "has_from"]
pub from: Option<[u8; 16]>,
/// Version number for conflict resolution
#[dyn_int]
pub version: u32,
/// Message timestamp (when it was sent)
pub timestamp: PlabbleDateTime,
/// Signatures by the sender to ensure authenticity and integrity of the message
#[multi_enum]
pub signatures: Vec<CryptoSignature>,
}
/// Whisper request body, used for server<->server messaging
#[repr(u8)]
#[serde_as]
#[derive(Debug, FromBytes, ToBytes, Serialize, Deserialize, PartialEq, Clone)]
#[no_discriminator]
pub enum WhisperRequestBody {
/// Ping other nodes to check if they are alive (with random number)
Ping(u8) = 0,
/// Broadcasted when a new node appears in the network
Hello(NodeInfo) = 1,
/// Asking the network who is a specific node id
WhoIs(#[serde_as(as = "Base64<UrlSafe, Unpadded>")] [u8; 16]) = 2,
/// Telling other nodes about a new bucket
NewBucket {
bucket: PostRequestBody,
meta: WhisperMetadata,
} = 3,
/// Telling other nodes to change the content of a slot in the bucket (with conflict resolution)
PutSlot {
id: BucketId,
slots: PutRequestBody,
meta: WhisperMetadata,
} = 4,
/// Telling other nodes to delete a slot in the bucket (with conflict resolution)
DeleteSlot {
id: BucketId,
query: BucketQuery,
meta: WhisperMetadata,
} = 5,
// 6-15 are reserved for future use
}
/// Whisper response body, used for server<->server messaging
#[repr(u8)]
#[derive(Debug, FromBytes, ToBytes, Serialize, Deserialize, PartialEq, Clone)]
#[no_discriminator]
pub enum WhisperResponseBody {
/// Response to a ping request, with the same random number (should match)
Pong(u8) = 0,
/// Response to a hello message
Hello = 1,
/// Response to a WhoIs request
WhoIs(Option<NodeInfo>) = 2,
/// Acknowledgment for a new bucket message (true if accepted, false if rejected)
NewBucketAck(bool) = 3,
/// Acknowledgment for a put slot message (true if accepted, false if rejected)
PutSlotAck(bool) = 4,
/// Acknowledgment for a delete slot message (true if accepted, false if rejected)
DeleteSlotAck(bool) = 5,
// 6-15 are reserved for future use
}
#[cfg(test)]
mod tests {
use binary_codec::{BinaryDeserializer, BinarySerializer};
use crate::{
core::{BucketId, PlabbleDateTime},
crypto::algorithm::CryptoSignature,
packets::{
body::{
post::PostRequestBody,
request_body::PlabbleRequestBody,
whisper::{WhisperMetadata, WhisperRequestBody},
},
header::{request_header::PlabbleRequestHeader, type_and_flags::RequestPacketType},
request::PlabbleRequestPacket,
response::PlabbleResponsePacket,
},
};
#[test]
fn can_serialize_and_deserialize_ping() {
let request: PlabbleRequestPacket = toml::from_str(
r#"
version = 1
[header]
packet_type = "Whisper"
[body]
Ping = 42
"#,
)
.unwrap();
assert_eq!(
request.header.packet_type,
RequestPacketType::Whisper { whisper_type: 0 }
);
let bytes = request.to_bytes(None).unwrap();
assert_eq!(bytes[1], 0b0000_1001);
assert_eq!("01092a", hex::encode(&bytes));
let deserialized = PlabbleRequestPacket::from_bytes(&bytes, None).unwrap();
assert_eq!(request, deserialized);
let response: PlabbleResponsePacket = toml::from_str(
r#"
version = 1
[header]
packet_type = "Whisper"
request_counter = 7
[body]
Pong = 42
"#,
)
.unwrap();
let bytes = response.to_bytes(None).unwrap();
assert_eq!(bytes[1], 0b0000_1001);
assert_eq!("010900072a", hex::encode(&bytes));
let deserialized = PlabbleResponsePacket::from_bytes(&bytes, None).unwrap();
assert_eq!(response, deserialized);
}
#[test]
fn can_serialize_and_deserialize_hello() {
let req: PlabbleRequestPacket = toml::from_str(
r#"
version = 1
[header]
packet_type = "Whisper"
[body.Hello]
id = "AQEBAQEBAQEBAQEBAQEBAQ"
address.V4 = "127.0.0.1:1234"
last_seen = "2161-02-07T06:28:15Z"
[[body.Hello.verification_keys]]
Ed25519 = "yX8-B6lkBke5guSLzDWbasSLRQ524mUq7YezQz4YeVU"
"#,
)
.unwrap();
let vkey: &str = "c97f3e07a9640647b982e48bcc359b6ac48b450e76e2652aed87b3433e187955";
assert_eq!(
req.header.packet_type,
RequestPacketType::Whisper { whisper_type: 1 }
);
let bytes = req.to_bytes(None).unwrap();
assert_eq!(bytes[1], 0b0001_1001);
assert_eq!(
format!(
// 0119 header + whisper type 1
// 16x 01 ID
// 00 IPv4
// 7f000001 127.0.0.1
// 04d2 port 1234
// ffffffff timestamp
// 31 default crypto settings
"0119{}007f00000104d2ffffffff31{}",
"01".repeat(16),
vkey
),
hex::encode(&bytes)
);
let deserialized = PlabbleRequestPacket::from_bytes(&bytes, None).unwrap();
assert_eq!(req, deserialized);
}
#[test]
fn can_serialize_and_deserialize_whois_request() {
let req: PlabbleRequestPacket = toml::from_str(
r#"
version = 1
[header]
packet_type = "Whisper"
[body]
WhoIs = "AQEBAQEBAQEBAQEBAQEBAQ"
"#,
)
.unwrap();
assert_eq!(
req.header.packet_type,
RequestPacketType::Whisper { whisper_type: 2 }
);
let bytes = req.to_bytes(None).unwrap();
assert_eq!(bytes[1], 0b0010_1001);
assert_eq!("012901010101010101010101010101010101", hex::encode(&bytes));
let deserialized = PlabbleRequestPacket::from_bytes(&bytes, None).unwrap();
assert_eq!(req, deserialized);
}
#[test]
fn can_serialize_and_deserialize_new_bucket() {
let body = WhisperRequestBody::NewBucket {
bucket: PostRequestBody {
id: BucketId::parse("#test").unwrap(),
settings: Default::default(),
range: None,
},
meta: WhisperMetadata {
binary_keys: false,
has_from: false,
from: None,
version: 7,
timestamp: PlabbleDateTime::new(0),
signatures: vec![CryptoSignature::Ed25519([0u8; 64])],
},
};
let req = PlabbleRequestPacket {
base: Default::default(),
header: PlabbleRequestHeader::new(RequestPacketType::Whisper { whisper_type: 3 }, None),
body: PlabbleRequestBody::Whisper(body),
};
let req_toml: PlabbleRequestPacket = toml::from_str(
r#"
version = 1
[header]
packet_type = "Whisper"
[body.NewBucket.bucket]
id = "RKiZXdULZlegN6eDkwRTWw"
[body.NewBucket.meta]
binary_keys = false
has_from = false
version = 7
timestamp = "2025-01-01T00:00:00Z"
[[body.NewBucket.meta.signatures]]
Ed25519 = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
"#,
)
.unwrap();
assert_eq!(req, req_toml);
let bytes = req.to_bytes(None).unwrap();
assert_eq!(bytes[1], 0b0011_1001);
assert_eq!(
format!(
"0139{}{}{}{}{}{}",
"44a8995dd50b6657a037a7839304535b", // bucket ID
"21f80100", // default bucket settings
"00", // whisper metadata flags
"07", // version
"00000000", // timestamp
"00".repeat(64) // signature
),
hex::encode(&bytes)
);
let deserialized = PlabbleRequestPacket::from_bytes(&bytes, None).unwrap();
assert_eq!(req, deserialized);
}
// TODO: add tests for PutSlot and DeleteSlot, also for responses and other missing
// but they are not that interesting for all those structures are already tested in other places
}