communitas-core 0.1.21

Core business logic for Communitas - PQC collaboration with virtual disks
Documentation
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# Communitas Core

Core business logic library for the Communitas P2P collaboration platform.

## Overview

Communitas Core is a Rust library that provides all the essential functionality for building decentralized collaboration applications. It's used by both the desktop application and headless daemon, providing a consistent API without any UI dependencies.

## Features

- **Post-Quantum Cryptography**: ML-DSA signatures and ML-KEM key exchange
- **Four-Word Addressing**: Human-readable identity system (e.g., "ocean-forest-moon-star")
- **Gossip Overlay Network**: Distributed P2P communication layer
- **CRDT Document Sync**: Conflict-free collaborative editing with Yrs
- **Encrypted Storage**: Platform-specific secure credential storage (keyring)
- **Presence Service**: Real-time user availability tracking
- **Message Synchronization**: Reliable message delivery with retries
- **Authentication**: Passkey-based authentication with platform integration
- **Storage Management**: Virtual disks with content addressing (BLAKE3)

## Architecture

### Core Components

```
communitas-core/
├── auth_service.rs          # Authentication and session management
├── core_context.rs          # Main application context
├── crdt.rs                  # CRDT document operations
├── doc_replicator.rs        # Document replication logic
├── encrypted_storage/       # Secure credential storage
├── gossip/                  # P2P gossip overlay
│   ├── context.rs          # Gossip network context
│   ├── coordinator.rs      # Message coordination
│   ├── crdt_sync.rs        # CRDT synchronization
│   ├── groups.rs           # Group management
│   ├── identity.rs         # Identity operations
│   ├── membership.rs       # Membership tracking
│   ├── presence.rs         # Presence detection
│   ├── pubsub.rs          # Publish-subscribe messaging
│   ├── rendezvous.rs      # Peer rendezvous
│   ├── transport.rs       # Network transport
│   └── types.rs           # Shared types
├── identity.rs             # Identity management and four-word addressing
├── keystore.rs            # Cryptographic key storage
├── local_storage.rs       # Local data persistence
├── message_sync.rs        # Message synchronization
├── presence_service.rs    # Presence tracking service
├── security/              # Security primitives
└── storage/               # Storage abstractions
```

## Quick Start

### As a Library

Add to your `Cargo.toml`:

```toml
[dependencies]
communitas-core = { path = "../communitas-core" }
tokio = { version = "1.39", features = ["full"] }
```

### Basic Usage

```rust
use communitas_core::{CoreContext, AuthService, GossipContext};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize core context
    let core_ctx = CoreContext::new("./data").await?;

    // Create identity with four-word address
    let auth = AuthService::new(core_ctx.clone()).await?;
    let session = auth.register(
        "ocean-forest-moon-star",
        "Alice",
        "Desktop-01",
        "secure-password"
    ).await?;

    println!("Created identity: {}", session.four_words);
    println!("Entity ID: {}", session.entity_id);

    // Initialize gossip network
    let gossip = GossipContext::new(core_ctx).await?;
    gossip.start().await?;

    // Send message to channel
    gossip.publish(
        "channel-id",
        "Hello, World!".as_bytes(),
        vec!["recipient-four-words"]
    ).await?;

    Ok(())
}
```

## Authentication System

### Four-Word Identity

Every user, organization, and entity has a human-readable four-word identity:

```rust
use communitas_core::identity::{generate_id_words, validate_id_words};

// Generate new identity
let four_words = generate_id_words()?;
// → "ocean-forest-moon-star"

// Validate identity
let is_valid = validate_id_words(&["ocean", "forest", "moon", "star"])?;
// → true

// Connect from words to IP addresses (optional)
use communitas_core::identity::conn_from_words;
let connection_info = conn_from_words(&four_words)?;
```

### Authentication Flow

```rust
use communitas_core::{AuthService, CoreContext};

let core_ctx = CoreContext::new("./data").await?;
let auth = AuthService::new(core_ctx).await?;

// Register new user
let session = auth.register(
    "ocean-forest-moon-star",
    "Alice",
    "Desktop-01",
    "secure-password"
).await?;

// Login existing user
let session = auth.login(
    "ocean-forest-moon-star",
    "secure-password"
).await?;

// Logout
auth.logout().await?;
```

## Gossip Network

### Network Initialization

```rust
use communitas_core::{GossipContext, CoreContext};

let core_ctx = CoreContext::new("./data").await?;
let gossip = GossipContext::new(core_ctx).await?;

// Start gossip network
gossip.start().await?;

// Join a topic
gossip.subscribe("general-chat").await?;

// Publish message
gossip.publish(
    "general-chat",
    b"Hello everyone!",
    vec![] // Empty = broadcast to all subscribers
).await?;
```

### Peer Discovery

```rust
// Discover peers via rendezvous
let peers = gossip.discover_peers("general-chat").await?;

for peer in peers {
    println!("Found peer: {:?}", peer);
}

// Check online presence
let is_online = gossip.is_online("ocean-forest-moon-star").await?;
```

## CRDT Document Collaboration

### Collaborative Editing

```rust
use communitas_core::crdt::{CrdtDocument, CrdtOperation};

// Create shared document
let doc = CrdtDocument::new("doc-123")?;

// Apply local edit
doc.insert(0, "Hello ")?;
doc.insert(6, "World!")?;

// Get operations for sync
let ops = doc.get_operations_since(0)?;

// Apply remote operations
for op in remote_ops {
    doc.apply_operation(op)?;
}

// Get current text
let text = doc.to_string()?;
```

### Document Replication

```rust
use communitas_core::doc_replicator::DocReplicator;

let replicator = DocReplicator::new(gossip_ctx.clone()).await?;

// Replicate document across peers
replicator.replicate_doc("doc-123").await?;

// Subscribe to document updates
replicator.subscribe_doc("doc-123", |update| {
    println!("Document updated: {:?}", update);
}).await?;
```

## Encrypted Storage

### Passkey Storage

```rust
use communitas_core::encrypted_storage::PasskeyStorage;

let storage = PasskeyStorage::new()?;

// Store passkey
storage.store_passkey(
    "user@example.com",
    "service-name",
    &passkey_bytes
).await?;

// Retrieve passkey
let passkey = storage.get_passkey(
    "user@example.com",
    "service-name"
).await?;

// Delete passkey
storage.delete_passkey(
    "user@example.com",
    "service-name"
).await?;
```

### Platform Integration

The encrypted storage system uses platform-specific credential managers:
- **macOS**: Keychain
- **Windows**: Windows Credential Manager
- **Linux**: Secret Service API (libsecret)

## Storage Management

### Virtual Disks

```rust
use communitas_core::storage::{VirtualDisk, DiskType};

// Create virtual disk for entity
let disk = VirtualDisk::new(
    entity_id,
    DiskType::Private
).await?;

// Write file
disk.write(
    "/docs/readme.md",
    b"# Project Documentation"
).await?;

// Read file
let content = disk.read("/docs/readme.md").await?;

// List files
let files = disk.list("/docs").await?;
```

### Content Addressing

```rust
use communitas_core::storage::ContentAddressed;

// Store content-addressed data
let hash = storage.store_content(b"Hello World!").await?;
// → BLAKE3 hash

// Retrieve by hash
let content = storage.get_content(&hash).await?;
```

## Message Synchronization

### Reliable Messaging

```rust
use communitas_core::message_sync::MessageSync;

let sync = MessageSync::new(gossip_ctx.clone()).await?;

// Send message with automatic retry
sync.send_reliable(
    "recipient-four-words",
    "channel-id",
    b"Important message"
).await?;

// Receive messages
let messages = sync.receive_since(last_timestamp).await?;

for msg in messages {
    println!("From: {}, Content: {:?}", msg.sender, msg.content);
}
```

## Presence Service

### Online Status Tracking

```rust
use communitas_core::presence_service::PresenceService;

let presence = PresenceService::new(gossip_ctx.clone()).await?;

// Set own status
presence.set_status("online", "Working on docs").await?;

// Check peer status
let status = presence.get_status("ocean-forest-moon-star").await?;
println!("Status: {:?}", status);

// Subscribe to presence changes
presence.subscribe(|event| {
    match event {
        PresenceEvent::Online(peer) => println!("{} came online", peer),
        PresenceEvent::Offline(peer) => println!("{} went offline", peer),
        PresenceEvent::StatusChange(peer, status) => {
            println!("{} changed status to {}", peer, status)
        }
    }
}).await?;
```

## Security

### Post-Quantum Cryptography

All cryptographic operations use post-quantum algorithms:

```rust
use communitas_core::security::{PqcSigner, PqcVerifier};

// Create ML-DSA signer
let signer = PqcSigner::new()?;

// Sign data
let signature = signer.sign(b"data to sign")?;

// Verify signature
let verifier = PqcVerifier::from_public_key(&public_key)?;
let is_valid = verifier.verify(b"data to sign", &signature)?;
```

### Key Exchange

```rust
use communitas_core::security::{PqcKeyExchange, SharedSecret};

// Alice generates keypair
let alice_kex = PqcKeyExchange::new()?;
let alice_public = alice_kex.public_key();

// Bob generates keypair and creates shared secret
let bob_kex = PqcKeyExchange::new()?;
let bob_public = bob_kex.public_key();
let bob_shared = bob_kex.derive_shared_secret(&alice_public)?;

// Alice creates shared secret
let alice_shared = alice_kex.derive_shared_secret(&bob_public)?;

assert_eq!(alice_shared, bob_shared);
```

## Development

### Building

```bash
cargo build -p communitas-core
cargo build -p communitas-core --release
```

### Testing

```bash
# Run all tests
cargo test -p communitas-core

# Run with logging
RUST_LOG=debug cargo test -p communitas-core

# Run specific test
cargo test -p communitas-core --test integration_test

# Run property-based tests
cargo test -p communitas-core --features proptest
```

### Linting

```bash
# Strict linting (enforces no panics in production)
cargo clippy -p communitas-core --all-features -- \
  -D clippy::panic \
  -D clippy::unwrap_used \
  -D clippy::expect_used

# Format code
cargo fmt -p communitas-core
```

## Configuration

### Environment Variables

| Variable | Description | Default |
|----------|-------------|---------|
| `COMMUNITAS_DATA_DIR` | Data storage directory | `~/.communitas` |
| `COMMUNITAS_LOG_LEVEL` | Logging level | `info` |
| `RUST_LOG` | Detailed logging filter | - |

### Configuration File

Core can be configured via TOML:

```toml
[identity]
four_words = "ocean-forest-moon-star"
display_name = "Alice"
device_name = "Desktop-01"

[network]
bootstrap_nodes = ["bootstrap.communitas.network:8080"]
enable_mdns = true
port = 8080

[storage]
data_dir = "~/.communitas"
cache_size_mb = 500

[security]
enable_pqc = true
key_rotation_days = 90

[logging]
level = "info"
format = "json"
```

## API Documentation

Full API documentation is available via rustdoc:

```bash
cargo doc -p communitas-core --open
```

## Performance Characteristics

- **Message Latency**: <100ms local, <500ms remote
- **Storage Operations**: <100ms for content-addressed reads
- **CRDT Sync**: <200ms for typical document operations
- **Memory Usage**: ~50MB baseline, scales with active documents
- **CPU Usage**: <5% idle, scales with network activity

## Security Considerations

1. **Zero-Panics Policy**: Production code forbids `unwrap()`, `expect()`, and `panic!`
2. **Post-Quantum Ready**: All cryptography uses ML-DSA and ML-KEM
3. **Secure Storage**: Platform-specific credential managers
4. **End-to-End Encryption**: All messages encrypted by default
5. **Forward Secrecy**: Perfect forward secrecy for all sessions
6. **No Trusted Third Parties**: Fully decentralized architecture

## Troubleshooting

### Common Issues

**Identity Generation Fails**
```bash
# Check four-word-networking dictionary
cargo test -p communitas-core -- identity::tests
```

**Gossip Network Won't Start**
```bash
# Check port availability
lsof -i :8080

# Enable debug logging
RUST_LOG=communitas_core::gossip=debug cargo run
```

**Storage Errors**
```bash
# Check permissions
ls -la ~/.communitas/

# Reset storage (development only)
rm -rf ~/.communitas/
```

**Authentication Issues**
```bash
# Check keyring access
cargo test -p communitas-core -- auth_service::tests

# Clear credentials (macOS)
security delete-generic-password -s "communitas"
```

## Contributing

See [../../docs/development/contributing.md](../../docs/development/contributing.md)

## License

Dual-licensed under AGPL-3.0-or-later and commercial license.

## See Also

- [Communitas Desktop]../communitas-desktop/README.md - Desktop application using this core
- [Communitas Headless]../communitas-headless/README.md - Headless daemon using this core
- [Architecture Documentation]../../docs/architecture/ - System architecture details
- [API Reference]../../docs/api/ - Complete API documentation