@@ -88,10 +88,29 @@ use super::{Answer, Offer};
8888/// ## Usage
8989///
9090/// ```rust
91- /// use mina_p2p::webrtc::{ConnectionAuth, Offer, Answer};
91+ /// use mina_p2p::webrtc::{ConnectionAuth, Offer, Answer, Host};
92+ /// use mina_p2p::identity::SecretKey;
93+ /// use rand::thread_rng;
94+ ///
95+ /// let sk_a = SecretKey::rand();
96+ /// let sk_b = SecretKey::rand();
97+ /// let offer = Offer {
98+ /// sdp: "v=0\r\no=- 0 0 IN IP4 127.0.0.1".into(),
99+ /// chain_id: mina_core::DEVNET_CHAIN_ID,
100+ /// identity_pub_key: sk_a.public_key(),
101+ /// target_peer_id: sk_b.public_key().peer_id(),
102+ /// host: Host::Domain("localhost".into()),
103+ /// listen_port: Some(8080),
104+ /// };
105+ /// let answer = Answer {
106+ /// sdp: "v=0\r\no=- 1 1 IN IP4 127.0.0.1".into(),
107+ /// identity_pub_key: sk_b.public_key(),
108+ /// target_peer_id: sk_a.public_key().peer_id(),
109+ /// };
92110///
93111/// let connection_auth = ConnectionAuth::new(&offer, &answer);
94- /// let encrypted_auth = connection_auth.encrypt(&my_secret_key, &peer_public_key, rng)?;
112+ /// let encrypted_auth = connection_auth.encrypt(&sk_a, &sk_b.public_key(), thread_rng());
113+ /// assert!(encrypted_auth.is_some());
95114/// ```
96115#[ derive( Serialize , Deserialize , Debug , Eq , PartialEq , Clone ) ]
97116pub struct ConnectionAuth ( Vec < u8 > ) ;
@@ -124,9 +143,32 @@ pub struct ConnectionAuth(Vec<u8>);
124143/// ## Example
125144///
126145/// ```rust
146+ /// use mina_p2p::webrtc::{ConnectionAuth, ConnectionAuthEncrypted, Offer, Answer, Host};
147+ /// use mina_p2p::identity::SecretKey;
148+ /// use rand::thread_rng;
149+ ///
150+ /// let sk_a = SecretKey::rand();
151+ /// let sk_b = SecretKey::rand();
152+ /// let offer = Offer {
153+ /// sdp: "v=0\r\no=- 0 0 IN IP4 127.0.0.1".into(),
154+ /// chain_id: mina_core::DEVNET_CHAIN_ID,
155+ /// identity_pub_key: sk_a.public_key(),
156+ /// target_peer_id: sk_b.public_key().peer_id(),
157+ /// host: Host::Domain("localhost".into()),
158+ /// listen_port: Some(8080),
159+ /// };
160+ /// let answer = Answer {
161+ /// sdp: "v=0\r\no=- 1 1 IN IP4 127.0.0.1".into(),
162+ /// identity_pub_key: sk_b.public_key(),
163+ /// target_peer_id: sk_a.public_key().peer_id(),
164+ /// };
165+ ///
166+ /// let auth = ConnectionAuth::new(&offer, &answer);
167+ /// let encrypted_auth = auth.encrypt(&sk_a, &sk_b.public_key(), thread_rng()).unwrap();
168+ ///
127169/// // After receiving encrypted authentication data
128- /// let decrypted_auth = encrypted_auth.decrypt(&my_secret_key , &peer_public_key)? ;
129- /// // Verify that the decrypted data matches expected values
170+ /// let decrypted_auth = encrypted_auth.decrypt(&sk_b , &sk_a.public_key()) ;
171+ /// assert!(decrypted_auth.is_some());
130172/// ```
131173#[ derive( Debug , Clone ) ]
132174pub struct ConnectionAuthEncrypted ( Box < [ u8 ; 92 ] > ) ;
@@ -158,7 +200,24 @@ impl ConnectionAuth {
158200 /// # Example
159201 ///
160202 /// ```rust
161- /// use mina_p2p::webrtc::ConnectionAuth;
203+ /// use mina_p2p::webrtc::{ConnectionAuth, Offer, Answer, Host};
204+ /// use mina_p2p::identity::SecretKey;
205+ ///
206+ /// let sk_a = SecretKey::rand();
207+ /// let sk_b = SecretKey::rand();
208+ /// let offer = Offer {
209+ /// sdp: "v=0\r\no=- 0 0 IN IP4 127.0.0.1".into(),
210+ /// chain_id: mina_core::DEVNET_CHAIN_ID,
211+ /// identity_pub_key: sk_a.public_key(),
212+ /// target_peer_id: sk_b.public_key().peer_id(),
213+ /// host: Host::Domain("localhost".into()),
214+ /// listen_port: Some(8080),
215+ /// };
216+ /// let answer = Answer {
217+ /// sdp: "v=0\r\no=- 1 1 IN IP4 127.0.0.1".into(),
218+ /// identity_pub_key: sk_b.public_key(),
219+ /// target_peer_id: sk_a.public_key().peer_id(),
220+ /// };
162221 ///
163222 /// let auth = ConnectionAuth::new(&offer, &answer);
164223 /// // Use auth for connection verification
@@ -196,10 +255,28 @@ impl ConnectionAuth {
196255 /// # Example
197256 ///
198257 /// ```rust
258+ /// use mina_p2p::webrtc::{ConnectionAuth, Offer, Answer, Host};
259+ /// use mina_p2p::identity::SecretKey;
199260 /// use rand::thread_rng;
200261 ///
201- /// let mut rng = thread_rng();
202- /// let encrypted_auth = connection_auth.encrypt(&my_secret_key, &peer_public_key, &mut rng);
262+ /// let sk_a = SecretKey::rand();
263+ /// let sk_b = SecretKey::rand();
264+ /// let offer = Offer {
265+ /// sdp: "v=0\r\no=- 0 0 IN IP4 127.0.0.1".into(),
266+ /// chain_id: mina_core::DEVNET_CHAIN_ID,
267+ /// identity_pub_key: sk_a.public_key(),
268+ /// target_peer_id: sk_b.public_key().peer_id(),
269+ /// host: Host::Domain("localhost".into()),
270+ /// listen_port: Some(8080),
271+ /// };
272+ /// let answer = Answer {
273+ /// sdp: "v=0\r\no=- 1 1 IN IP4 127.0.0.1".into(),
274+ /// identity_pub_key: sk_b.public_key(),
275+ /// target_peer_id: sk_a.public_key().peer_id(),
276+ /// };
277+ ///
278+ /// let connection_auth = ConnectionAuth::new(&offer, &answer);
279+ /// let encrypted_auth = connection_auth.encrypt(&sk_a, &sk_b.public_key(), thread_rng());
203280 ///
204281 /// if let Some(encrypted) = encrypted_auth {
205282 /// // Send encrypted authentication data to peer
@@ -254,8 +331,31 @@ impl ConnectionAuthEncrypted {
254331 /// # Example
255332 ///
256333 /// ```rust
334+ /// use mina_p2p::webrtc::{ConnectionAuth, Offer, Answer, Host};
335+ /// use mina_p2p::identity::SecretKey;
336+ /// use rand::thread_rng;
337+ ///
338+ /// let sk_a = SecretKey::rand();
339+ /// let sk_b = SecretKey::rand();
340+ /// let offer = Offer {
341+ /// sdp: "v=0\r\no=- 0 0 IN IP4 127.0.0.1".into(),
342+ /// chain_id: mina_core::DEVNET_CHAIN_ID,
343+ /// identity_pub_key: sk_a.public_key(),
344+ /// target_peer_id: sk_b.public_key().peer_id(),
345+ /// host: Host::Domain("localhost".into()),
346+ /// listen_port: Some(8080),
347+ /// };
348+ /// let answer = Answer {
349+ /// sdp: "v=0\r\no=- 1 1 IN IP4 127.0.0.1".into(),
350+ /// identity_pub_key: sk_b.public_key(),
351+ /// target_peer_id: sk_a.public_key().peer_id(),
352+ /// };
353+ ///
354+ /// let auth = ConnectionAuth::new(&offer, &answer);
355+ /// let encrypted_auth = auth.encrypt(&sk_a, &sk_b.public_key(), thread_rng()).unwrap();
356+ ///
257357 /// // After receiving encrypted authentication data from peer
258- /// if let Some(decrypted_auth) = encrypted_auth.decrypt(&my_secret_key , &peer_public_key ) {
358+ /// if let Some(decrypted_auth) = encrypted_auth.decrypt(&sk_b , &sk_a.public_key() ) {
259359 /// // Authentication successful, proceed with connection
260360 /// println!("Peer authentication verified");
261361 /// } else {
0 commit comments