How gracelium64/crypt works
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: index.ts API route boundary · ChatView.tsx Chat workflow · messages.ts Message orchestration · providers.service.ts Provider gateway
Components 21
React client
- Browser user
- App routing & UI · Authenticated chat, contacts, settings, linking, and provider workflows.
frontendReactJs/src/App.tsx - Session state
frontendReactJs/src/context/AuthProvider.tsx - Protected pages
frontendReactJs/src/layouts/ProtectedLayout.tsx - Chat workflow
frontendReactJs/src/pages/ChatView.tsx - E2E key & crypto · ECDH, AES-GCM, and wrapped private-key synchronization.
frontendReactJs/src/lib/crypto.ts - API client
frontendReactJs/src/lib/api.ts - Conversation sync · Socket updates with polling and reconnect catch-up.
frontendReactJs/src/hooks/useRealtime.ts
Backend service
- Express & Socket.IO
backend/src/server.ts - API route boundary · Routes apply auth, authorization, rate limits, validation, and error handling.
backend/src/routes/index.ts - Accounts & public keys · JWT-gated account access plus public-key registration and lookup.
backend/src/controllers/keys.ts - Message orchestration · Persists participant fan-out copies, reads timelines, and emits updates.
backend/src/controllers/messages.ts - Realtime publisher
backend/src/services/realtime.service.ts - Provider linking · Associates external identities with Crypt accounts.
backend/src/controllers/link.ts - Provider gateway · Routes provider send/webhook flows and guards stale accounts.
backend/src/services/providers.service.ts - Telegram integration · Supports Telegram session/login operations and bot fallback.
backend/src/services/telegram-mtproto.service.ts - Media handling · Handles validated uploads and inbound provider media.
backend/src/services/media.service.ts - At-rest crypto · Protects selected persisted server-side fields independently of E2E ciphertext.
backend/src/services/crypto.service.ts
External infrastructure
- MongoDB · Stores accounts, keys, messages, links, provider connections, and Telegram sessions.
backend/src/db/connect.ts - Telegram & WhatsApp
- Cloudinary
Connections 27
- 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