|
| 1 | +# Ratatoskr Codebase Decomposition |
| 2 | + |
| 3 | +This document maps the logical architecture to the physical code structure of the Monorepo. Use this as a guide to locate features and understand dependency flows. |
| 4 | + |
| 5 | +## 📂 Repository Root |
| 6 | + |
| 7 | +| File/Dir | Purpose | |
| 8 | +| :--- | :--- | |
| 9 | +| `Cargo.toml` | Rust Workspace definition. Manages shared dependencies across Core, Server, and Desktop. | |
| 10 | +| `package.json` | Node.js dependencies for tooling (Lefthook, Prettier) and Frontend. | |
| 11 | +| `lefthook.yml` | Git hooks configuration (Pre-commit checks). | |
| 12 | +| `mise.toml` | Task runner and tool version manager configuration. | |
| 13 | +| `.github/` | CI/CD workflows (GitHub Actions). | |
| 14 | +| `scripts/` | Utility scripts (e.g., `run_testnet.sh` for multi-node simulation). | |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## 🧠 Core Library (`ratatoskr-core`) |
| 19 | +**Role:** The shared brain. Contains all protocol logic, cryptography, and networking. Used by both Client and Relay Server. |
| 20 | + |
| 21 | +### `src/` |
| 22 | +- **`lib.rs`**: Module exports and library initialization. |
| 23 | +- **`models.rs`**: Data structures exchanged over the network and stored in DB. |
| 24 | + - `ChatMessage`: The central object (includes `msg_type`, `ttl`, `reply_to`). |
| 25 | + - `SosPayload`: Emergency signal structure. |
| 26 | +- **`network.rs`**: The P2P Networking Stack (`libp2p`). |
| 27 | + - `RatatoskrBehavior`: Combines `GossipSub` (Chat/SOS) and `Kademlia` (DHT Routing). |
| 28 | + - `NetworkCommand/Event`: Channels for async communication with the UI/Server. |
| 29 | +- **`crypto.rs`**: Cryptographic primitives. |
| 30 | + - ECIES (Anonymous Encryption) implementation. |
| 31 | + - *Future:* Double Ratchet session management. |
| 32 | +- **`key_vault.rs`**: Identity Management. |
| 33 | + - Ed25519 Key generation. |
| 34 | + - BIP-39 Mnemonic recovery logic. |
| 35 | + - Secure file I/O. |
| 36 | +- **`storage.rs`**: Persistence Layer. |
| 37 | + - SQLite connection pool (`sqlx`). |
| 38 | + - `GarbageCollector`: Logic for deleting expired TTL messages. |
| 39 | + - CRUD operations for Contacts and Messages. |
| 40 | +- **`access_control.rs`**: Authorization. |
| 41 | + - `VolunteerCredential` definitions. |
| 42 | + - *Future:* Reputation system logic ("Plague Protocol"). |
| 43 | + |
| 44 | +### `migrations/` |
| 45 | +- SQL files for database schema versioning (e.g., `20240101...init.sql`). |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +## 🖥️ Desktop Client (`ratatoskr-desktop`) |
| 50 | +**Role:** The user-facing application. Wraps Core with a GUI. |
| 51 | + |
| 52 | +### `src-tauri/` (Rust Backend) |
| 53 | +- **`src/lib.rs`**: The Bridge. |
| 54 | + - **Tauri Commands:** Functions callable from JS (`send_message`, `panic_wipe`). |
| 55 | + - **State Management:** Holds `AppState` (Network channels, DB connection). |
| 56 | + - **Background Threads:** Spawns the P2P node and the Event Listener loop. |
| 57 | +- **`tauri.conf.json`**: App configuration (permissions, bundle settings). |
| 58 | + |
| 59 | +### `src/` (Svelte Frontend) |
| 60 | +- **`routes/+page.svelte`**: The Main UI (currently monolithic, planned for split). |
| 61 | + - **Sidebar:** Navigation (Chats, Contacts, Settings). |
| 62 | + - **Chat View:** Message rendering, bubbles, input area. |
| 63 | + - **Settings:** Identity management, Backup, Panic Button. |
| 64 | +- **`lib/`**: (Planned) Shared Svelte components (`MessageBubble.svelte`, `ContactItem.svelte`). |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +## 📡 Relay Server (`ratatoskr-server`) |
| 69 | +**Role:** Infrastructure node. Always-on peer for bootstrapping and routing. |
| 70 | + |
| 71 | +### `src/` |
| 72 | +- **`main.rs`**: Application Entry point. |
| 73 | + - Initializes `ratatoskr-core`. |
| 74 | + - Configures P2P in **Server Mode** (DHT Provider). |
| 75 | + - Logs traffic (SOS signals). |
| 76 | + - *Future:* Blind Mailbox storage implementation. |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +## 🔗 Logical Flow Examples |
| 81 | + |
| 82 | +### 1. Sending a Message |
| 83 | +1. **UI (`+page.svelte`):** User types text -> calls `invoke("send_message")`. |
| 84 | +2. **Tauri (`lib.rs`):** |
| 85 | + - Loads Identity from `KeyVault`. |
| 86 | + - Saves message to SQLite via `Storage`. |
| 87 | + - Sends `NetworkCommand::Broadcast` to the Network Channel. |
| 88 | +3. **Core (`network.rs`):** |
| 89 | + - Picks up command. |
| 90 | + - Publishes to `libp2p` GossipSub topic. |
| 91 | + |
| 92 | +### 2. Receiving a Message |
| 93 | +1. **Core (`network.rs`):** `libp2p` receives packet -> Sends `NetworkEvent::MessageReceived`. |
| 94 | +2. **Tauri (`lib.rs`):** |
| 95 | + - Listener loop catches event. |
| 96 | + - Saves to SQLite (Status: Unread). |
| 97 | + - Emits Tauri Event `msg-received`. |
| 98 | +3. **UI (`+page.svelte`):** |
| 99 | + - `listen()` triggers. |
| 100 | + - Updates `chatMessages` array. |
| 101 | + - DOM updates to show new bubble. |
| 102 | + |
| 103 | +### 3. Panic Wipe |
| 104 | +1. **UI:** User clicks "Destroy". Calls `invoke("delete_identity")`. |
| 105 | +2. **Tauri:** Deletes `identity.key` from disk. |
| 106 | +3. **UI:** Reloads window (`window.location.reload()`), resetting all JS state. |
0 commit comments