# gracelium64/crypt architecture

> Architecture diagram of the GitHub repository gracelium64/crypt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/gracelium64/crypt
- Repository: https://github.com/gracelium64/crypt

## Overview

#### 1. System Purpose and Entry Points
Crypt Companion is a secure messaging companion MVP: users authenticate, manage encryption keys, connect Telegram/WhatsApp providers, link contacts, and view/send synchronized messages. The backend starts the Express/Socket.IO service and mounts API/webhook routes at `backend/src/server.ts`; the browser client boots through `frontendReactJs/src/main.tsx` and application routing/UI through `frontendReactJs/src/App.tsx`.

#### 2. HTTP API and Request Boundary
Routes divide the API into auth, keys, links, messages, uploads, provider connections, Telegram, WhatsApp/provider, and administrative operations. Controllers implement route behavior, while schemas validate request inputs before business/data operations. Authentication, authorization, rate limiting, admin checks, and centralized error/not-found handling form the API boundary.
- `backend/src/routes/index.ts`
- `backend/src/controllers/messages.ts`
- `backend/src/middleware/authenticate.ts`

#### 3. Identity, Accounts, and Key Lifecycle
JWT-based account access gates user-owned resources. Public-key registration and lookup support recipient resolution; frontend key management and cryptographic functions support ECDH P-256 exchange, AES-GCM message encryption, and PBKDF2-wrapped private-key synchronization. Server-side crypto separately protects selected persisted fields, so frontend E2E ciphertext and backend at-rest protection are distinct concerns.
- `backend/src/models/account.ts`
- `backend/src/controllers/keys.ts`
- `backend/src/services/crypto.service.ts`
- `frontendReactJs/src/components/KeyManager.tsx`
- `frontendReactJs/src/lib/crypto.ts`

#### 4. Messaging, Persistence, and Realtime Flow
Outbound UI actions call the API service layer; backend message logic persists fan-out copies so both communicating parties retain a Crypt timeline regardless of provider delivery path. Reads derive conversations from stored messages, decrypt server-at-rest payloads on read paths, and publish realtime events. Socket.IO updates are supplemented by frontend polling/reconnect catch-up behavior.
- `backend/src/controllers/messages.ts`
- `backend/src/models/message.ts`
- `backend/src/services/realtime.service.ts`
- `frontendReactJs/src/hooks/useConversations.ts`
- `frontendReactJs/src/hooks/useRealtime.ts`

#### 5. Provider Integration and Linking
Provider connections bind external identities to Crypt accounts. Link flows establish associations, then provider-specific controllers/services receive or send messages and participate in fan-out persistence. Telegram supports MTProto session/login operations and a bot fallback; WhatsApp is represented through provider routes/webhooks. The account-validation guard on provider connections prevents stale/deleted accounts from receiving fan-out traffic.
- `backend/src/controllers/link.ts`
- `backend/src/controllers/telegram.ts`
- `backend/src/services/telegram-mtproto.service.ts`
- `backend/src/services/providers.service.ts`
- `backend/src/models/providerConnection.ts`

#### 6. Media and External Infrastructure
Multipart uploads are validated and delegated to media handling, including Cloudinary-hosted attachments and inbound WhatsApp media. MongoDB is the system of record for accounts, keys, messages, links, provider connections, and Telegram sessions. Required operational boundaries include MongoDB connectivity, JWT signing, AES key material, CORS origin configuration, and deployed backend/frontend hosting.
- `backend/src/controllers/uploads.ts`
- `backend/src/services/media.service.ts`
- `backend/src/db/connect.ts`
- `backend/env.example`

#### 7. Frontend Application Structure
The React/Vite client keeps authenticated session state in an auth context, restricts application pages through a protected layout, and organizes primary workflows around chats, contact discovery, settings, linking, and provider connection panels. Hooks isolate API, send, provider, link, conversation, and realtime coordination from page components.
- `frontendReactJs/src/context/AuthProvider.tsx`
- `frontendReactJs/src/layouts/ProtectedLayout.tsx`
- `frontendReactJs/src/pages/ChatView.tsx`
- `frontendReactJs/src/lib/api.ts`

Main entry points: [`backend/src/routes/index.ts`](https://github.com/gracelium64/crypt/blob/main/backend/src/routes/index.ts) (API route boundary), [`frontendReactJs/src/pages/ChatView.tsx`](https://github.com/gracelium64/crypt/blob/main/frontendReactJs/src/pages/ChatView.tsx) (Chat workflow), [`backend/src/controllers/messages.ts`](https://github.com/gracelium64/crypt/blob/main/backend/src/controllers/messages.ts) (Message orchestration), [`backend/src/services/providers.service.ts`](https://github.com/gracelium64/crypt/blob/main/backend/src/services/providers.service.ts) (Provider gateway)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["React client"]
  node_browser(("Browser user<br/>client"))
  node_frontend_app["App routing &amp; UI<br/>React application<br/>[App.tsx]"]
  node_auth_context["Session state<br/>auth context<br/>[AuthProvider.tsx]"]
  node_protected_layout["Protected pages<br/>route guard"]
  node_chat_view["Chat workflow<br/>chat page<br/>[ChatView.tsx]"]
  node_client_crypto{{"E2E key &amp; crypto<br/>browser cryptography<br/>[crypto.ts]"}}
  node_client_api["API client<br/>HTTP service<br/>[api.ts]"]
  node_client_realtime["Conversation sync<br/>realtime hooks<br/>[useRealtime.ts]"]
end

subgraph group_backend["Backend service"]
  node_server["Express &amp; Socket.IO<br/>service entry point<br/>[server.ts]"]
  node_api_routes["API route boundary<br/>routes and middleware<br/>[index.ts]"]
  node_identity_keys["Accounts &amp; public keys<br/>identity controllers<br/>[keys.ts]"]
  node_messages["Message orchestration<br/>message controller<br/>[messages.ts]"]
  node_realtime["Realtime publisher<br/>Socket.IO service"]
  node_provider_linking["Provider linking<br/>link controller<br/>[link.ts]"]
  node_provider_gateway["Provider gateway<br/>provider service"]
  node_telegram["Telegram integration<br/>MTProto service"]
  node_media["Media handling<br/>media service<br/>[media.service.ts]"]
  node_at_rest_crypto{{"At-rest crypto<br/>server cryptography<br/>[crypto.service.ts]"}}
end

subgraph group_integrations["External infrastructure"]
  node_mongodb[("MongoDB<br/>system of record<br/>[connect.ts]")]
  node_provider_networks["Telegram &amp; WhatsApp<br/>provider platforms"]
  node_cloudinary["Cloudinary<br/>media host"]
end

node_browser -->|"uses"| node_frontend_app
node_frontend_app -->|"reads session"| node_auth_context
node_frontend_app -->|"routes through"| node_protected_layout
node_protected_layout -->|"permits"| node_chat_view
node_chat_view -->|"encrypts/decrypts"| node_client_crypto
node_chat_view -->|"calls"| node_client_api
node_chat_view -->|"subscribes"| node_client_realtime
node_client_api -->|"HTTP API"| node_server
node_client_realtime -->|"Socket.IO"| node_server
node_server -->|"mounts"| node_api_routes
node_api_routes -->|"auth and key requests"| node_identity_keys
node_api_routes -->|"message requests"| node_messages
node_api_routes -->|"link requests"| node_provider_linking
node_api_routes -->|"provider routes/webhooks"| node_provider_gateway
node_api_routes -->|"upload requests"| node_media
node_identity_keys -->|"accounts and keys"| node_mongodb
node_messages -->|"protects fields"| node_at_rest_crypto
node_messages -->|"fan-out messages"| node_mongodb
node_messages -->|"publishes events"| node_realtime
node_realtime -->|"emits sockets"| node_server
node_provider_linking -->|"stores associations"| node_mongodb
node_provider_gateway -->|"validates connections"| node_mongodb
node_provider_gateway -->|"Telegram flow"| node_telegram
node_provider_gateway -->|"sends and receives"| node_provider_networks
node_telegram -->|"MTProto or bot"| node_provider_networks
node_media -->|"stores attachments"| node_cloudinary
node_media -->|"retrieves WhatsApp media"| node_provider_networks

click node_frontend_app "https://github.com/gracelium64/crypt/blob/main/frontendReactJs/src/App.tsx"
click node_auth_context "https://github.com/gracelium64/crypt/blob/main/frontendReactJs/src/context/AuthProvider.tsx"
click node_protected_layout "https://github.com/gracelium64/crypt/blob/main/frontendReactJs/src/layouts/ProtectedLayout.tsx"
click node_chat_view "https://github.com/gracelium64/crypt/blob/main/frontendReactJs/src/pages/ChatView.tsx"
click node_client_crypto "https://github.com/gracelium64/crypt/blob/main/frontendReactJs/src/lib/crypto.ts"
click node_client_api "https://github.com/gracelium64/crypt/blob/main/frontendReactJs/src/lib/api.ts"
click node_client_realtime "https://github.com/gracelium64/crypt/blob/main/frontendReactJs/src/hooks/useRealtime.ts"
click node_server "https://github.com/gracelium64/crypt/blob/main/backend/src/server.ts"
click node_api_routes "https://github.com/gracelium64/crypt/blob/main/backend/src/routes/index.ts"
click node_identity_keys "https://github.com/gracelium64/crypt/blob/main/backend/src/controllers/keys.ts"
click node_messages "https://github.com/gracelium64/crypt/blob/main/backend/src/controllers/messages.ts"
click node_realtime "https://github.com/gracelium64/crypt/blob/main/backend/src/services/realtime.service.ts"
click node_provider_linking "https://github.com/gracelium64/crypt/blob/main/backend/src/controllers/link.ts"
click node_provider_gateway "https://github.com/gracelium64/crypt/blob/main/backend/src/services/providers.service.ts"
click node_telegram "https://github.com/gracelium64/crypt/blob/main/backend/src/services/telegram-mtproto.service.ts"
click node_media "https://github.com/gracelium64/crypt/blob/main/backend/src/services/media.service.ts"
click node_at_rest_crypto "https://github.com/gracelium64/crypt/blob/main/backend/src/services/crypto.service.ts"
click node_mongodb "https://github.com/gracelium64/crypt/blob/main/backend/src/db/connect.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_browser,node_frontend_app,node_auth_context,node_protected_layout,node_chat_view,node_client_crypto,node_client_api,node_client_realtime toneBlue
class node_server,node_api_routes,node_identity_keys,node_messages,node_realtime,node_provider_linking,node_provider_gateway,node_telegram,node_media,node_at_rest_crypto toneAmber
class node_mongodb,node_provider_networks,node_cloudinary toneMint
```

## Components

### React client

- **Browser user**: client
- [App routing & UI](https://github.com/gracelium64/crypt/blob/main/frontendReactJs/src/App.tsx): React application · Authenticated chat, contacts, settings, linking, and provider workflows. · `frontendReactJs/src/App.tsx`
- [Session state](https://github.com/gracelium64/crypt/blob/main/frontendReactJs/src/context/AuthProvider.tsx): auth context · `frontendReactJs/src/context/AuthProvider.tsx`
- [Protected pages](https://github.com/gracelium64/crypt/blob/main/frontendReactJs/src/layouts/ProtectedLayout.tsx): route guard · `frontendReactJs/src/layouts/ProtectedLayout.tsx`
- [Chat workflow](https://github.com/gracelium64/crypt/blob/main/frontendReactJs/src/pages/ChatView.tsx): chat page · `frontendReactJs/src/pages/ChatView.tsx`
- [E2E key & crypto](https://github.com/gracelium64/crypt/blob/main/frontendReactJs/src/lib/crypto.ts): browser cryptography · ECDH, AES-GCM, and wrapped private-key synchronization. · `frontendReactJs/src/lib/crypto.ts`
- [API client](https://github.com/gracelium64/crypt/blob/main/frontendReactJs/src/lib/api.ts): HTTP service · `frontendReactJs/src/lib/api.ts`
- [Conversation sync](https://github.com/gracelium64/crypt/blob/main/frontendReactJs/src/hooks/useRealtime.ts): realtime hooks · Socket updates with polling and reconnect catch-up. · `frontendReactJs/src/hooks/useRealtime.ts`

### Backend service

- [Express & Socket.IO](https://github.com/gracelium64/crypt/blob/main/backend/src/server.ts): service entry point · `backend/src/server.ts`
- [API route boundary](https://github.com/gracelium64/crypt/blob/main/backend/src/routes/index.ts): routes and middleware · Routes apply auth, authorization, rate limits, validation, and error handling. · `backend/src/routes/index.ts`
- [Accounts & public keys](https://github.com/gracelium64/crypt/blob/main/backend/src/controllers/keys.ts): identity controllers · JWT-gated account access plus public-key registration and lookup. · `backend/src/controllers/keys.ts`
- [Message orchestration](https://github.com/gracelium64/crypt/blob/main/backend/src/controllers/messages.ts): message controller · Persists participant fan-out copies, reads timelines, and emits updates. · `backend/src/controllers/messages.ts`
- [Realtime publisher](https://github.com/gracelium64/crypt/blob/main/backend/src/services/realtime.service.ts): Socket.IO service · `backend/src/services/realtime.service.ts`
- [Provider linking](https://github.com/gracelium64/crypt/blob/main/backend/src/controllers/link.ts): link controller · Associates external identities with Crypt accounts. · `backend/src/controllers/link.ts`
- [Provider gateway](https://github.com/gracelium64/crypt/blob/main/backend/src/services/providers.service.ts): provider service · Routes provider send/webhook flows and guards stale accounts. · `backend/src/services/providers.service.ts`
- [Telegram integration](https://github.com/gracelium64/crypt/blob/main/backend/src/services/telegram-mtproto.service.ts): MTProto service · Supports Telegram session/login operations and bot fallback. · `backend/src/services/telegram-mtproto.service.ts`
- [Media handling](https://github.com/gracelium64/crypt/blob/main/backend/src/services/media.service.ts): media service · Handles validated uploads and inbound provider media. · `backend/src/services/media.service.ts`
- [At-rest crypto](https://github.com/gracelium64/crypt/blob/main/backend/src/services/crypto.service.ts): server cryptography · Protects selected persisted server-side fields independently of E2E ciphertext. · `backend/src/services/crypto.service.ts`

### External infrastructure

- [MongoDB](https://github.com/gracelium64/crypt/blob/main/backend/src/db/connect.ts): system of record · Stores accounts, keys, messages, links, provider connections, and Telegram sessions. · `backend/src/db/connect.ts`
- **Telegram & WhatsApp**: provider platforms
- **Cloudinary**: media host

## Connections

- Browser user → App routing & UI: uses
- App routing & UI → Session state: reads session
- App routing & UI → Protected pages: routes through
- Protected pages → Chat workflow: permits
- Chat workflow → E2E key & crypto: encrypts/decrypts
- Chat workflow → API client: calls
- Chat workflow → Conversation sync: subscribes
- API client → Express & Socket.IO: HTTP API
- Conversation sync → Express & Socket.IO: Socket.IO
- Express & Socket.IO → API route boundary: mounts
- API route boundary → Accounts & public keys: auth and key requests
- API route boundary → Message orchestration: message requests
- API route boundary → Provider linking: link requests
- API route boundary → Provider gateway: provider routes/webhooks
- API route boundary → Media handling: upload requests
- Accounts & public keys → MongoDB: accounts and keys
- Message orchestration → At-rest crypto: protects fields
- Message orchestration → MongoDB: fan-out messages
- Message orchestration → Realtime publisher: publishes events
- Realtime publisher → Express & Socket.IO: emits sockets
- Provider linking → MongoDB: stores associations
- Provider gateway → MongoDB: validates connections
- Provider gateway → Telegram integration: Telegram flow
- Provider gateway → Telegram & WhatsApp: sends and receives
- Telegram integration → Telegram & WhatsApp: MTProto or bot
- Media handling → Cloudinary: stores attachments
- Media handling → Telegram & WhatsApp: retrieves WhatsApp media
