# Building OFFChat: Zero-Infrastructure P2P Encrypted Mesh Communication Over BLE & Wi-Fi Direct
In natural disaster emergencies, internet blackouts, or remote expedition environments, cellular towers and internet gateways are frequently disabled. Traditional messaging architectures rely on centralized servers and DNS resolution.
**OFFChat** was engineered to solve this problem: a zero-infrastructure, self-healing peer-to-peer (P2P) mesh messaging system using Bluetooth Low Energy (BLE) and Wi-Fi Direct with cryptographic end-to-end security.
---
## 1. Network Topology: Store-and-Forward Epidemic Routing
Unlike conventional IP routing, an ad-hoc mobile mesh cannot maintain continuous static route tables because nodes move unpredictably. We adopted an **Optimized Epidemic Store-and-Forward Protocol**:
``` [Node A] --(BLE Peer Discovery)--> [Node B] | | | (Carried in local SQLite buffer) | (Relayed over Wi-Fi Direct) v v [Node C] <------------------------- [Node D] (Destination Node) ```
### Packet Structure (Binary Protocol Header) Each packet carries a fixed 48-byte cryptographically signed header:
``` +-------------------+-------------------+-------------------+-------------------+ | Magic (4 bytes) | Packet UUID (16B) | Sender PK (32B) | Recipient PK (32B)| +-------------------+-------------------+-------------------+-------------------+ | Hop Count (1 byte)| TTL (1 byte) | Timestamp (8B) | Payload HMAC (32B)| +-------------------+-------------------+-------------------+-------------------+ | Encrypted Payload (Variable) | +-------------------------------------------------------------------------------+ ```
---
## 2. Cryptographic Security Architecture
A distributed mesh inherently routes packets through untrusted intermediary nodes. Therefore, intermediaries must be mathematically incapable of reading, modifying, or forging messages.
### The Cryptographic Stack: 1. **Identity & Key Exchange**: Curve25519 (X25519) keypairs generated on local device enclave. 2. **Forward Secrecy**: Signal-style **Double Ratchet Algorithm** ensuring that compromised keys cannot decrypt past message history. 3. **Authenticated Encryption**: ChaCha20-Poly1305 AEAD providing confidentiality and tamper-evident authentication.
```typescript // Cryptographic Message Packaging import { box, randomBytes } from "tweetnacl";
export function encryptMeshPacket( plaintext: Uint8Array, recipientPublicKey: Uint8Array, senderPrivateKey: Uint8Array ): { nonce: Uint8Array; cipherText: Uint8Array } { const nonce = randomBytes(box.nonceLength); // 24 bytes const cipherText = box(plaintext, nonce, recipientPublicKey, senderPrivateKey); return { nonce, cipherText }; } ```
---
## 3. Battery Conservation & Radio Duty Cycling
Continuous BLE advertising and Wi-Fi Direct scanning drain a smartphone battery within 4 hours. We designed a dynamic adaptive duty cycle scheduler:
| State | BLE Advertise | BLE Scan | Wi-Fi Direct Search | Battery Drain | | :--- | :--- | :--- | :--- | :--- | | **Active Chatting** | 100ms interval | Continuous | Connected P2P Group | ~4.2% / hour | | **Idle Background** | 1200ms interval | 2.5s every 30s | Disabled until handshaked | **~0.6% / hour** | | **Emergency Beacon** | 2000ms burst | 1.0s every 60s | Standby | **~0.3% / hour** |
> [!TIP] > Grouping neighboring devices into Wi-Fi Direct "Autonomous Group Owners" (GO) based on remaining battery level ensures the most energy-resilient device orchestrates local file transfers.