You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: Update chat example to add a nonce (#324) (#325)
* docs: Update chat example to add a nonce (#324)
* Update src/app/docs/examples/gossip-chat/page.mdx
Co-authored-by: Philipp Krüger <[email protected]>
---------
Co-authored-by: Philipp Krüger <[email protected]>
Copy file name to clipboardExpand all lines: src/app/docs/examples/gossip-chat/page.mdx
+47-18Lines changed: 47 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -184,6 +184,9 @@ And let's write a `Message::Message` that has a `String` with the actual chat me
184
184
185
185
Also, we want each of those messages to include the `NodeId` of the sender. In an actual application, we would encode and decode the messages with keypairs to ensure that everyone who sends a message is actually who they say they are. For more on that, check out our more robust chat example that exists in the [`iroh-gossip`](https://github.com/n0-computer/iroh-gossip/blob/main/examples/chat.rs) repo.
186
186
187
+
In addition, the nature of the gossip protocol could potentially cause messages to be sent multiple times. This is done intentionally, to ensure at-least-once delivery of each message to all nodes. This behavior is unexpected in most app contexts, so iroh will internally deduplicate messages based on the hash of their contents.
188
+
In this case, if someone sends re-sends a message they already sent, it will be ignored by the other peeres. To circumvent this, each message should include a piece of unique data to prevent this deduplication. This can be done in a number of ways - we will use a [cryptographic nonce](https://en.wikipedia.org/wiki/Cryptographic_nonce).
189
+
187
190
We need to add crates that will allow us to serialize our new message types as bytes and deserialize bytes as our message type.
188
191
189
192
`serde` stands for `Serialize/Deserialize`. `serde-json` lets us easily encode and decode to the json format, but we can choose other formats. E.g., in the `iroh-gossip` example, we use `postcard`.
@@ -205,7 +208,13 @@ use serde::{Deserialize, Serialize};
205
208
206
209
// add the message code to the bottom
207
210
#[derive(Debug, Serialize, Deserialize)]
208
-
enumMessage {
211
+
structMessage {
212
+
body:MessageBody,
213
+
nonce: [u8; 16],
214
+
}
215
+
216
+
#[derive(Debug, Serialize, Deserialize)]
217
+
enumMessageBody {
209
218
AboutMe { from:NodeId, name:String },
210
219
Message { from:NodeId, text:String },
211
220
}
@@ -215,6 +224,13 @@ impl Message {
215
224
serde_json::from_slice(bytes).map_err(Into::into)
216
225
}
217
226
227
+
pubfnnew(body:MessageBody) ->Self {
228
+
Self {
229
+
body,
230
+
nonce:rand::random(),
231
+
}
232
+
}
233
+
218
234
pubfnto_vec(&self) ->Vec<u8> {
219
235
serde_json::to_vec(self).expect("serde_json::to_vec is infallible")
0 commit comments