* Refactor BitChat for improved robustness and performance Major refactoring to simplify architecture and fix critical issues: Architecture Improvements: - Replace complex BluetoothMeshService with simplified SimplifiedBluetoothService - Consolidate message routing and peer management into unified services - Remove redundant caching layers and optimize performance Bug Fixes: - Fix critical BLE peer mapping corruption in mesh networks - Fix encrypted message routing failures in multi-peer scenarios - Fix app freezes and Main Thread Checker warnings - Fix BLE message delivery in dual-role connections - Fix favorite toggle UI not updating instantly - Fix Nostr offline messaging with 24-hour message filtering Features: - Add command processor for chat commands - Add autocomplete service for mentions and commands - Improve private chat management with dedicated service - Add unified peer service for consistent state management Performance: - Optimize BLE reconnection speed - Reduce excessive logging throughout codebase - Improve message deduplication efficiency - Optimize UI updates and state management * Improvements refactor robust (#441) * remove unused code * remove more * TLV for announcement * restore * restore * restore? * messages tlv too (#442) * Fix Nostr notification and read receipt issues, add TLV encoding, cleanup unused code ## Notification & Read Receipt Fixes - Fixed toolbar notification icon appearing incorrectly on app restart for already-read messages - Fixed read receipts being incorrectly deleted on startup when privateChats was empty - Fixed messages not being marked as read when opening chat for first time - Fixed senderPeerID not being updated during message consolidation - Added startup phase logic to block old messages (>30s) while allowing recent ones - Fixed unread status checking across all storage locations (ephemeral, stable Noise keys, temporary Nostr IDs) ## TLV Encoding Implementation - Implemented Type-Length-Value encoding for private message payloads - Added PrivateMessagePacket struct with TLV encode/decode methods - Enhanced message structure for better extensibility and robustness ## Code Cleanup - Removed unused PeerStateManager class and related dependencies - Removed dead protocol types (DeliveryAck, ProtocolAck/Nack, NoiseIdentityAnnouncement) - Cleaned up BitchatDelegate by removing unused methods - Removed excessive debug logging throughout ChatViewModel - Added test-only ProtocolNack helper for integration tests ## Technical Details - Messages stored under three ID types: ephemeral peer IDs, stable Noise key hexes, temporary Nostr IDs - Fixed cleanupOldReadReceipts() to skip when privateChats is empty or during startup - Updated message consolidation to properly update senderPeerID - Restored NIP-17 timestamp randomization (±15 minutes) for privacy --------- Co-authored-by: jack <jackjackbits@users.noreply.github.com> Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com>
18 KiB
AI Context for BitChat
This document provides essential context for AI assistants working on the BitChat codebase. Read this first to understand the project's architecture, design decisions, and key concepts.
Project Overview
BitChat is a decentralized, peer-to-peer messaging application that works over Bluetooth mesh networks without requiring internet connectivity, servers, or phone numbers. It's designed for scenarios where traditional communication infrastructure is unavailable or untrusted.
Key Features
- Bluetooth Mesh Networking: Multi-hop message relay over BLE
- Privacy-First Design: No accounts, no persistent identifiers
- End-to-End Encryption: Uses Noise Protocol Framework for private messages
- IRC-Style Commands: Familiar
/msg,/whointerface - Cross-Platform: Native iOS and macOS support
- Nostr Integration: Seamless fallback for mutual favorites when out of Bluetooth range
- Hybrid Transport: Automatic switching between Bluetooth and Nostr
Architecture Overview
┌─────────────────────────────────────────────────────────────────┐
│ User Interface │
│ (ContentView, ChatViewModel) │
└─────────────────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────────────┐
│ Application Services │
│ (MessageRetryService, DeliveryTracker, NotificationService) │
└─────────────────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────────────┐
│ Message Router │
│ (Transport selection, Favorites integration) │
└─────────────────────────────────────────────────────────────────┘
│ │
┌───────────────────────────────┐ ┌────────────────────────────────┐
│ Security Layer │ │ Nostr Protocol Layer │
│ (NoiseEncryptionService, │ │ (NostrProtocol, NIP-17, │
│ SecureIdentityStateManager) │ │ NostrRelayManager) │
└───────────────────────────────┘ └────────────────────────────────┘
│ │
┌───────────────────────────────┐ ┌────────────────────────────────┐
│ Protocol Layer │ │ Transport │
│ (BitchatProtocol, Binary- │ │ (WebSocket to Nostr │
│ Protocol, NoiseProtocol) │ │ relay servers) │
└───────────────────────────────┘ └────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────────────┐
│ Bluetooth Transport Layer │
│ (SimplifiedBluetoothService) │
└─────────────────────────────────────────────────────────────────┘
Core Components
1. SimplifiedBluetoothService (Transport Layer)
- Location:
bitchat/Services/SimplifiedBluetoothService.swift - Purpose: Manages BLE connections and implements mesh networking
- Key Responsibilities:
- Peer discovery (scanning and advertising simultaneously)
- Connection management (acts as both central and peripheral)
- Message routing and relay
- Version negotiation with peers
- Automatic reconnection and topology management
2. BitchatProtocol (Protocol Layer)
- Location:
bitchat/Protocols/BitchatProtocol.swift - Purpose: Defines the application-level messaging protocol
- Key Features:
- Binary packet format for efficiency
- Message types: Chat, Announcement, PrivateMessage, etc.
- TTL-based routing (max 7 hops)
- Message deduplication via unique IDs
- Privacy features: padding, timing obfuscation
3. NoiseProtocol Implementation
- Locations:
bitchat/Noise/NoiseProtocol.swift- Core protocol implementationbitchat/Noise/NoiseSession.swift- Session managementbitchat/Services/NoiseEncryptionService.swift- High-level encryption API
- Purpose: Provides end-to-end encryption for private messages
- Implementation Details:
- Uses Noise_XX_25519_AESGCM_SHA256 pattern
- Mutual authentication via static keys
- Forward secrecy via ephemeral keys
- Integrated with identity management
4. Identity System
- Location:
bitchat/Identity/ - Three-Layer Model:
- Ephemeral Identity: Short-lived, rotates frequently
- Cryptographic Identity: Long-term Noise static keypair
- Social Identity: User-chosen nickname and metadata
- Trust Levels: Untrusted → Verified → Trusted → Blocked
5. ChatViewModel
- Location:
bitchat/ViewModels/ChatViewModel.swift - Purpose: Central state management and business logic
- Responsibilities:
- Message handling and batching
- Command processing (/msg, /who, etc.)
- UI state management
- Private chat coordination
6. Nostr Integration
- Locations:
bitchat/Nostr/NostrProtocol.swift- NIP-17 private message implementationbitchat/Nostr/NostrRelayManager.swift- WebSocket relay connectionsbitchat/Nostr/NostrIdentity.swift- Nostr key managementbitchat/Services/MessageRouter.swift- Transport selection logic
- Purpose: Enables communication with mutual favorites when out of Bluetooth range
- Key Features:
- NIP-17 gift-wrapped private messages for metadata privacy
- Automatic relay connection management
- Seamless transport switching between Bluetooth and Nostr
- Integrated with favorites system for mutual authentication
7. MessageRouter
- Location:
bitchat/Services/MessageRouter.swift - Purpose: Intelligent routing between Bluetooth mesh and Nostr transports
- Transport Selection Logic:
- Always prefer Bluetooth mesh when peer is connected
- Use Nostr for mutual favorites when peer is offline
- Fail gracefully when no transport is available
- Message Types Routed:
- Regular chat messages
- Favorite/unfavorite notifications
- Delivery acknowledgments
- Read receipts
Key Design Decisions
1. Protocol Design
- Binary Protocol: Chosen for efficiency over BLE's limited bandwidth
- No JSON: Reduces parsing overhead and message size
- Custom Framing: Handles BLE's 512-byte MTU limitations
2. Security Architecture
- Noise Protocol: Industry-standard, well-analyzed framework
- XX Pattern: Provides mutual authentication and forward secrecy
- No Long-Term Identifiers: Enhances privacy and deniability
3. Mesh Networking
- TTL-Based Routing: Prevents infinite loops in mesh
- Bloom Filters: Efficient duplicate detection
4. Privacy Features
- Message Padding: Obscures message length
- Timing Obfuscation: Randomized delays
- Emergency Wipe: Triple-tap to clear all data
5. Nostr Integration
- NIP-17 Gift Wraps: Maximum metadata privacy
- Ephemeral Keys: Each message uses unique ephemeral keys
- Mutual Favorites Only: Requires bidirectional trust
- Transport Abstraction: Users don't need to know about Nostr
Code Organization
Services (/bitchat/Services/)
Application-level services that coordinate between layers:
SimplifiedBluetoothService: Core networkingNoiseEncryptionService: Encryption coordinationMessageRetryService: Reliability layerDeliveryTracker: Acknowledgment handlingNotificationService: System notifications
Protocols (/bitchat/Protocols/)
Protocol definitions and implementations:
BitchatProtocol: Application protocolBinaryProtocol: Low-level encodingBinaryEncodingUtils: Helper functions
Noise (/bitchat/Noise/)
Noise Protocol Framework implementation:
NoiseProtocol: Core cryptographic operationsNoiseSession: Session state managementNoiseHandshakeCoordinator: Handshake orchestrationNoiseSecurityConsiderations: Security validations
Views & ViewModels
MVVM architecture for UI:
ContentView: Main chat interfaceChatViewModel: Business logic and state- Supporting views for settings, identity, etc.
Nostr Protocol Implementation
Overview
BitChat integrates Nostr as a secondary transport for communicating with mutual favorites when Bluetooth connectivity is unavailable. This integration is transparent to users - messages automatically route through Nostr when needed.
NIP-17 Private Direct Messages
BitChat implements NIP-17 (Private Direct Messages) for metadata-private communication:
-
Gift Wrap Structure:
Gift Wrap (kind 1059) → Seal (kind 13) → Rumor (kind 1)- Rumor: The actual message content (unsigned)
- Seal: Encrypted rumor, hides sender identity
- Gift Wrap: Double-encrypted, tagged for recipient
-
Ephemeral Keys:
- Each message uses TWO ephemeral key pairs
- Seal uses one ephemeral key
- Gift wrap uses a different ephemeral key
- Provides sender anonymity and forward secrecy
-
Timestamp Randomization:
- ±1 minute randomization (reduced from NIP-17's ±15 minutes)
- Prevents timing correlation attacks
- Configurable in
NostrProtocol.randomizedTimestamp()
Favorites Integration
The Nostr transport is only available for mutual favorites:
-
Favorite Establishment:
- User favorites a peer via
/favcommand - Favorite notification sent via Bluetooth (if connected)
- Peer's Nostr public key exchanged during favorite process
- Stored in
FavoritesPersistenceService
- User favorites a peer via
-
Mutual Requirement:
- Both peers must favorite each other
- Prevents spam and unwanted Nostr messages
- Enforced by
MessageRoutertransport selection
-
Nostr Key Management:
- Derived from Noise static key using BIP-32
- Path:
m/44'/1237'/0'/0/0(1237 = "NOSTR" in decimal) - Consistent npub across app reinstalls
- Keys never leave the device
Message Routing Logic
MessageRouter automatically selects transport:
if peerAvailableOnMesh {
transport = .bluetoothMesh // Always prefer mesh
} else if isMutualFavorite {
transport = .nostr // Use Nostr for offline favorites
} else {
throw MessageRouterError.peerNotReachable
}
Relay Configuration
Default relays (hardcoded for reliability):
wss://relay.damus.iowss://relay.primal.netwss://offchain.pubwss://nostr21.com
Relay selection criteria:
- Geographic distribution
- High uptime
- No authentication required
- Support for ephemeral events
Message Format
Structured content for different message types:
- Chat:
MSG:<messageID>:<content> - Favorite:
FAVORITED:<senderNpub>orUNFAVORITED:<senderNpub> - Delivery ACK:
DELIVERED:<messageID> - Read Receipt:
READ:<base64EncodedReceipt>
Implementation Details
-
NostrRelayManager:
- Manages WebSocket connections to relays
- Handles reconnection logic
- Processes EVENT, EOSE, OK, NOTICE messages
- Implements NIP-01 relay protocol
-
NostrProtocol:
- Implements NIP-17 encryption/decryption
- Handles gift wrap creation/unwrapping
- Manages ephemeral key generation
- Provides Schnorr signatures
-
ProcessedMessagesService:
- Prevents duplicate message processing
- Tracks last subscription timestamp
- Persists across app launches
- 30-day retention window
Security Considerations
-
Metadata Protection:
- Sender identity hidden via ephemeral keys
- Recipient only visible in gift wrap p-tag
- Timing correlation prevented via randomization
- Message content double-encrypted
-
Relay Trust:
- Relays cannot read message content
- Relays can see recipient pubkey (gift wrap)
- Relays cannot determine sender
- Multiple relays used for redundancy
-
Key Hygiene:
- Ephemeral keys used once and discarded
- Static Nostr key derived from Noise key
- No key reuse between messages
- Keys cleared from memory after use
Debugging Nostr Issues
-
Check relay connections:
- Look for "Connected to Nostr relay" in logs
- Verify WebSocket state in NostrRelayManager
- Check for relay errors/notices
-
Verify gift wrap creation:
- Enable debug logging in NostrProtocol
- Check ephemeral key generation
- Verify encryption steps
-
Message delivery:
- Check ProcessedMessagesService for duplicates
- Verify subscription filters
- Look for EVENT messages in relay responses
Development Guidelines
1. Security First
- Never log sensitive data (keys, message content)
- Use
SecureLoggerfor security-aware logging - Validate all inputs from network
- Follow principle of least privilege
2. Performance Considerations
- BLE has limited bandwidth (~20KB/s practical)
- Minimize protocol overhead
- Batch operations where possible
- Use compression for large messages
3. Testing
- Unit tests for protocol logic
- Integration tests for service interactions
- End-to-end tests for user flows
- Mock objects for BLE testing
4. Error Handling
- Graceful degradation for network issues
- Clear error messages for users
- Automatic retry with backoff
- Never expose internal errors
Common Tasks
Adding a New Message Type
- Define in
MessageTypeenum inBitchatProtocol.swift - Implement encoding/decoding logic
- Add handling in
ChatViewModel - Update UI if needed
- Add tests
Implementing a New Command
- Add to
ChatViewModel.processCommand() - Define any new message types needed
- Implement command logic
- Add autocomplete support
- Update help text
Debugging Bluetooth Issues
- Check
SimplifiedBluetoothServicelogs - Verify peer states and connections
- Monitor characteristic updates
- Use Bluetooth debugging tools
Working with Nostr Transport
- Verify mutual favorite status in
FavoritesPersistenceService - Check Nostr key derivation in
NostrIdentity - Monitor relay connections in
NostrRelayManager - Test gift wrap encryption/decryption
- Verify transport selection in
MessageRouter
Adding Nostr Features
- Understand NIP-17 gift wrap structure
- Maintain ephemeral key hygiene
- Test with multiple relays
- Preserve metadata privacy
- Handle relay disconnections gracefully
Security Threat Model
Assumptions
- Adversaries can intercept all Bluetooth traffic
- Devices may be compromised
- No trusted infrastructure available
Protections
- End-to-end encryption for private messages
- Message authentication via HMAC
- Forward secrecy via ephemeral keys
- Deniability through lack of signatures
Limitations
- Public messages are unencrypted by design
- Metadata (who talks to whom) partially visible
- Timing attacks possible on mesh network
- No protection against flooding/spam (yet)
Performance Optimizations
Implemented
- LZ4 compression for messages
- Adaptive duty cycling for battery
- Connection caching and reuse
- Bloom filters for deduplication
Future Improvements
- Protocol buffer encoding
- Better mesh routing algorithms
- Predictive pre-connection
- Smarter retransmission
Troubleshooting Guide
Common Issues
- Peers not discovering: Check Bluetooth permissions, ensure app is in foreground
- Messages not delivering: Verify mesh connectivity, check TTL values
- Handshake failures: Ensure identity state is consistent, check key storage
- Performance issues: Monitor connection count, check for message loops
External Dependencies
Swift Packages
- CryptoKit: Apple's crypto framework
- Network.framework: For future internet support
- No third-party dependencies (by design)
System Requirements
- iOS 14.0+ / macOS 11.0+
- Bluetooth LE hardware
- ~50MB storage for app + data
Future Roadmap
Planned Features
- Internet bridging for hybrid networks
- Group chat with forward secrecy
- Voice messages with Opus codec
- File transfer support
Architecture Evolution
- Plugin system for transports
- Modular protocol stack
- Cross-platform core library
- Federation between networks
Quick Start for AI Assistants
- Understand the layers: Transport → Protocol → Security → Services → UI
- Follow the data flow: BLE/Nostr → Binary/JSON → Protocol → ViewModel → View
- Respect security boundaries: Never mix trusted and untrusted data
- Test thoroughly: This is critical infrastructure for users
- Ask about design decisions: Many choices have non-obvious reasons
- Dual Transport: Remember that messages can flow over Bluetooth OR Nostr
- Favorites System: Nostr only works between mutual favorites
When in doubt, prioritize security and privacy over features. BitChat users depend on this app in situations where traditional communication has failed them.