Skip to main content

rings_core/
lib.rs

1//! Rings: Chord based P2P implementation over WebRTC and ElGamal.
2//! --------------
3//! - [Chord](crate::dht::PeerRing) is a structured p2p network based on Chord protocol and expanded with secp256k1 based Did support.
4//! - [ElGamal](crate::ecc::elgamal) provides End2End encryption based on Chord Did.
5//! - [Swarm](crate::swarm) is a module for managing all transports.
6//! - [Connection](rings_transport::core::transport::ConnectionInterface) is used for connection handshaking, which supports all platforms, including browser (wasm) and native runtime.
7
8//! # Connection
9//!
10//! There are three phases when node A connects to node B. So-called join a DHT Ring.
11//!
12//! 1. Handshake
13//! - Node A create a new transport via `swarm.new_transport()` and generate the handshake SDP
14//!   with `transport.get_handshake_info(session_sk, offer)` and send it to node B.
15//! - Node B accept the offer with `transport.register_remote_info(offer)` and response with Answer via
16//!   `transport.get_handshake_info(session_sk, offer)`.
17//! - Node A accept the answer and wait until the connection creation.
18//! 2. Join Ring
19//! - After the connection creation, node A will ask node B for a successor.
20//!   (A successor is the closest node on the Ring for node A.)
21//!   If node B know another node X could be the successor of node A, it will respond with the `Did` of node X.
22//! 3. E2e encrypt
23//! - After joining Ring, should encrypt all direct messages with the ElGamal algorithm.
24//!
25//! # MessagePayload
26//!
27//! MSRP over WebRTC Data Channel is Published in Jan/2021, which is based on The Message Session Relay Protocol and its extension.
28//!
29//! To implement MSRP over WebRTC, it's necessary to handle SCTP transport of peer connection (text data channel based on SCTP).
30//! For webrtc-rs, there is already an implementation. But unfortunately, it is not widely supported in the browser environment.
31//! (We can modify web-sys to support it, it may be easy, but it still won't work in Firefox.)
32//! So the best solution for message relay is to create a similar protocol to MSRP.
33//!
34//! The message relay protocol is similar to MSRP(RFC8873). All relay messages should have path data fields: `path`, `destination`.
35//! If a message is sent from A to Z over Ring when a relay node X got the message, the path data of the relay message may look like this:
36//!
37//! ```txt
38//! path:[A, B, C, D] destination: Z
39//! ```
40//!
41//! Node X must append itself to the `path` list: `path[A, B, C, X]`.
42//!
43//! When node Z receives the relay message, node Z can respond without any query on DHT -- Just reverse the path.
44//!
45//! # ECDSA Session
46//!
47//! To avoid too frequent signing, and keep the private key safe, we implemented a session protocol for signing/encrypting and verifying messages.
48//!
49//! ECDSA Session is based on secp256k1.
50//! - ECDSA Session is based on secp256k1.
51//!   ECDSA Session creates a temporary secret key with one-time signing auth.
52//! - To create a ECDSA Session, we should generate the unsign_info with our pubkey (Address).
53//!   `SessionSk::gen_unsign_info(addr, ..)`, it will return the msg needs for signing, and a temporary private key.
54//! - Then we can sign the auth message via some web3 provider like metamask or just with a raw private key, and create the SessionManger with
55//!   `SessionSk::new(sig, auth_info, temp_key)`.
56
57//! # WASM Supported
58//! ```shell
59//! cargo build -p rings-core --target=wasm32-unknown-unknown --features wasm --no-default-features
60//! ```
61#![cfg_attr(target_arch = "wasm32", allow(clippy::arc_with_non_send_sync))]
62#![cfg_attr(
63    test,
64    allow(
65        clippy::expect_used,
66        clippy::indexing_slicing,
67        clippy::panic,
68        clippy::unwrap_used
69    )
70)]
71
72pub mod algebra;
73pub mod dht;
74pub mod ecc;
75pub mod error;
76pub mod macros;
77pub mod message;
78pub mod prelude;
79pub mod session;
80pub mod storage;
81pub mod swarm;
82#[cfg(test)]
83mod tests;
84pub mod utils;
85pub use async_trait::async_trait;
86pub use futures;
87pub mod chunk;
88pub mod consts;
89pub mod inspect;
90pub mod measure;