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
//! # Gun.rs - A Real-time, Decentralized, Offline-First Graph Database
//!
//! Gun.rs is a Rust implementation of the [Gun.js](https://gun.eco/) protocol, providing a
//! real-time, decentralized, offline-first graph database. It enables peer-to-peer data
//! synchronization without requiring a central server.
//!
//! ## Features
//!
//! - **Real-time Synchronization**: Automatic data synchronization with connected peers
//! - **Decentralized**: P2P mesh networking - no central server required
//! - **Offline-First**: Works locally and syncs when connected
//! - **Graph Database**: Store data as a graph with nodes and relationships
//! - **Conflict Resolution**: Automatic conflict resolution using state timestamps (HAM algorithm)
//! - **Cryptographic Security**: BLS signatures for message authentication
//! - **Pluggable Storage**: Memory, file-based (localStorage-like), or Sled database
//! - **WebRTC Support**: Direct peer-to-peer connections with NAT traversal
//! - **WebSocket Support**: Relay server connections for NAT traversal
//!
//! ## Quick Start
//!
//! ```rust,no_run
//! use gun::{Gun, GunOptions};
//! use chia_bls::{SecretKey, PublicKey};
//! use serde_json::json;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Generate BLS key pair
//! let secret_key = SecretKey::from_seed(&[0u8; 32]);
//! let public_key = secret_key.public_key();
//!
//! // Create a local Gun instance
//! let gun = Gun::new(secret_key, public_key);
//!
//! // Store data
//! gun.get("user").put(json!({
//! "name": "Alice",
//! "age": 30
//! })).await?;
//!
//! // Read data once
//! gun.get("user").once(|data, _key| {
//! println!("User: {:?}", data);
//! }).await?;
//!
//! // Subscribe to updates
//! gun.get("user").on(|data, _key| {
//! println!("User updated: {:?}", data);
//! });
//!
//! // Connect to peers (optional)
//! let options = GunOptions {
//! peers: vec!["ws://relay.example.com/gun".to_string()],
//! ..Default::default()
//! };
//! let gun_with_peers = Gun::with_options(secret_key, public_key, options).await?;
//!
//! Ok(())
//! }
//! ```
//!
//! ## Architecture
//!
//! ### Core Components
//!
//! - **[`Gun`](gun::Gun)**: Main entry point - creates and manages Gun instances
//! - **[`Chain`](chain::Chain)**: Fluent API for data operations (get, put, on, once, map, set)
//! - **[`GunCore`](core::GunCore)**: Core engine managing graph, state, events, and storage
//! - **[`Graph`](graph::Graph)**: In-memory graph storage with conflict resolution
//! - **[`Mesh`](dam::Mesh)**: DAM protocol implementation for P2P message routing
//! - **[`Storage`](storage::Storage)**: Pluggable storage backend trait
//!
//! ### Data Model
//!
//! Gun uses a graph data model where:
//! - **Nodes**: Identified by a unique "soul" (UUID-like string)
//! - **Properties**: Key-value pairs on nodes
//! - **References**: Links between nodes using soul references `{"#": "soul_id"}`
//! - **State**: Timestamps for conflict resolution
//!
//! ### Conflict Resolution
//!
//! Gun uses the HAM (Hypothetical Amnesia Machine) algorithm:
//! - Each property update gets a state timestamp
//! - Higher state wins in conflicts
//! - Automatic merge of non-conflicting updates
//!
//! ### Network Protocol
//!
//! Gun uses the DAM (Directed Acyclic Mesh) protocol:
//! - Message deduplication to prevent loops
//! - Automatic routing through peer mesh
//! - Support for WebSocket relays and WebRTC direct connections
//!
//! ## Examples
//!
//! See the `examples/` directory for comprehensive examples including:
//! - Basic operations (put, get, once, on)
//! - Chain operations (get, put, back, map, set)
//! - Network synchronization
//! - Storage backends
//! - WebRTC connections
//! - Certificate-based security (SEA)
//!
//! ## Security
//!
//! Gun.rs supports cryptographic security through:
//! - **BLS Signatures**: All messages are signed and verified
//! - **SEA (Security, Encryption, Authorization)**: Certificate-based access control
//! - **Message Predicates**: Custom filtering logic for incoming messages
//!
//! ## Performance
//!
//! - In-memory graph for fast reads
//! - Persistent storage options for durability
//! - Message batching and deduplication
//! - Efficient conflict resolution algorithm
//!
//! ## Compatibility
//!
//! Gun.rs aims to be compatible with Gun.js:
//! - Same protocol and message format
//! - Compatible graph structure
//! - Interoperable with JavaScript peers
//!
//! ## License
//!
//! See the LICENSE file for license information.
//!
//! ## Links
//!
//! - [Gun.js Documentation](https://gun.eco/docs)
//! - [Gun.js GitHub](https://github.com/amark/gun)
pub use Chain;
pub use GunError;
pub use ;
pub use *;
pub use MessagePredicate;
pub use valid;
pub use ;
pub use ;