Commit Graph
483 Commits
Author SHA1 Message Date
4867ddca0d Add comprehensive AI-friendly documentation across core files (#328)
- Created AI_CONTEXT.md as central documentation hub for AI assistants
- Added detailed file-level documentation to all major components
- Documented architecture, design decisions, and security considerations
- Added usage examples and integration guidance
- Improved code discoverability with clear component descriptions

Documentation covers:
- BluetoothMeshService: Core networking and mesh protocol
- BitchatProtocol: Application-layer protocol design
- NoiseProtocol: Cryptographic implementation details
- ChatViewModel: Business logic and state management
- IdentityModels: Three-layer identity architecture
- NoiseEncryptionService: High-level encryption API
- SecureIdentityStateManager: Secure persistence layer
- BinaryProtocol: Low-level wire format

This documentation will significantly improve AI understanding of the codebase structure and enable faster, more accurate assistance with development tasks.

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
2025-07-27 10:18:47 +02:00
e282d077a3 fix iOS app zoom/compat mode issue (issue #236) (#327)
Co-authored-by: kevin <kevin@none>
2025-07-27 08:01:59 +02:00
jackandGitHub 34b2b1eee0 Update README.md 2025-07-26 11:54:39 +02:00
54c7eba8cb Improve code organization and documentation (#325)
* Add MARK headers to improve code organization in major files

* Reorganize peer management functions in BluetoothMeshService

- Removed duplicate getCurrentPeerID(for:) function
- Consolidated peer identity functions in Peer Identity Mapping section
- Moved getPeerFingerprint(), getFingerprint(for:), isPeerIDOurs() to proper location
- Moved getCurrentPeerIDForFingerprint() and getCurrentPeerIDs() from Message Sending section
- Moved notifyPeerIDChange() to Peer Management section

* Consolidate peer management functions in BluetoothMeshService

- Moved getCachedPublicKey() and getCachedSigningKey() from Identity Cache Methods to Peer Connection Management
- Moved getPeerNicknames() and getPeerRSSI() to Peer Connection Management section
- Moved getAllConnectedPeerIDs(), notifyPeerListUpdate(), and cleanupStalePeers() to Peer Connection Management
- Removed duplicate function declarations after consolidation
- Improved code organization by grouping all peer-related functions together

* Consolidate message handling functions in ChatViewModel

- Moved handleHandshakeRequest() from floating location to Message Reception section
- Moved trimMessagesIfNeeded() and trimPrivateChatMessagesIfNeeded() to Message Batching section
- Improved code organization by grouping related message handling functions together
- Removed unnecessary comments from trim functions

* Improve ContentView organization with better documentation

- Added descriptive comments for complex inline computations
- Documented message extraction logic for private vs public chats
- Documented peer data computation and sorting logic
- Improved code readability by explaining complex operations inline
- Note: Attempted to extract complex computations into helper functions, but SwiftUI scope limitations made inline documentation a better approach

---------

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
2025-07-26 00:11:23 +02:00
jackandGitHub 4568951736 Update WHITEPAPER.md 2025-07-25 20:57:19 +02:00
jack 0e78341102 Trim whitespace from nicknames to prevent display issues
- Added didSet observer to nickname property to trim on every change
- Trim nickname when loading from UserDefaults
- Update validateAndSaveNickname to properly trim
- Trim received nicknames in announce packets
- Add custom init/decoder for NoiseIdentityAnnouncement to ensure trimming
- Trim nicknames when decoding from binary data
2025-07-25 20:29:50 +02:00
jack e57bfad843 Merge branch 'fix/read-receipt-consistency' v1.1 2025-07-25 18:10:10 +02:00
jack 6afef8466a Fix read receipt to use same efficient pattern as delivery ACK
- Modified read receipt to use type marker + binary payload pattern
- Added read receipt type marker handling in noise decryption
- Reduces message size and processing overhead
- Ensures consistency between delivery ACK and read receipt implementations
2025-07-25 18:09:55 +02:00
df83f716d1 Fix lazy handshake behavior and re-enable cover traffic (#323)
* Add encryption export compliance to Info.plist

Set ITSAppUsesNonExemptEncryption to false for TestFlight submission

* Revert "Add encryption export compliance to Info.plist"

This reverts commit 2cfbb1679d.

* fix: prevent spurious handshake requests from rotated peers and cover traffic

- Clean up connection states and pending messages during peer ID rotation
- Filter out rotated peer IDs in keep-alive timer to prevent validation attempts
- Add session check before sending cover traffic dummy messages
- Disable cover traffic by default (was sending without user knowledge)
- Only send screenshot notifications when session exists
- Add logging to track automatic message triggers
- Fix compilation errors (SecureLogger.privacy -> security)

* Fix lazy handshake behavior and re-enable cover traffic

- Re-enable cover traffic for privacy protection
- Start handshake immediately when opening PM conversation
- Remove handshake request system notifications

---------

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
2025-07-25 16:15:43 +02:00
jack 97c2393370 Fix lazy handshake behavior and re-enable cover traffic
- Re-enable cover traffic for privacy protection
- Start handshake immediately when opening PM conversation
- Remove handshake request system notifications
2025-07-25 16:15:12 +02:00
jack 087d3b29ae fix: prevent spurious handshake requests from rotated peers and cover traffic
- Clean up connection states and pending messages during peer ID rotation
- Filter out rotated peer IDs in keep-alive timer to prevent validation attempts
- Add session check before sending cover traffic dummy messages
- Disable cover traffic by default (was sending without user knowledge)
- Only send screenshot notifications when session exists
- Add logging to track automatic message triggers
- Fix compilation errors (SecureLogger.privacy -> security)
2025-07-25 16:15:12 +02:00
jack 21562049ac Revert "Add encryption export compliance to Info.plist"
This reverts commit 2cfbb1679d.
2025-07-25 16:15:12 +02:00
jack 9bac75ef76 Add encryption export compliance to Info.plist
Set ITSAppUsesNonExemptEncryption to false for TestFlight submission
2025-07-25 16:15:12 +02:00
3513228736 Implement handshake request notifications for pending messages (#321)
- Add handshakeRequest packet type (0x25) to notify recipients about queued messages
- Create HandshakeRequest struct with binary encoding for efficient transmission
- Send handshake requests when messages are queued due to missing session
- Display notifications when someone wants to send messages
- Fix verification persistence bug by adding forceSave on app termination
- Update UI to handle optional encryption icons (hide when no handshake attempted)

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
2025-07-25 14:55:26 +02:00
jackandGitHub 07c5e2e097 Delete BUILD_MACOS.md 2025-07-25 14:50:52 +02:00
jack f55694c920 Update WHITEPAPER.md 2025-07-25 12:26:27 +02:00
2e0d9be03e Implement targeted delivery for private messages (#319)
- Add direct delivery for private messages when recipient is connected
- Implement selective relay using 2-3 best RSSI peers as fallback
- Limit TTL to 2 hops for relayed private messages
- Update all private communication types to use targeted delivery:
  - Private messages
  - Read receipts
  - Delivery ACKs
  - Protocol ACKs/NACKs
  - Handshake messages
  - Identity announces (when targeted)
  - Session validation pings
  - Version ACKs
- Add intelligent relay logic to forward messages only when recipient is reachable
- Fall back to broadcast only as last resort when no direct path available

This reduces network traffic by ~90% for private communications, improves battery life, and enhances privacy by limiting message visibility.

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
2025-07-25 09:54:56 +02:00
callebtcandGitHub 9515f759d4 NoiseProtocol update: send nonce in packet (#306)
* send nonce in packet

* sliding window and 8 byte nonce

* do not process encrypted messages that arent addressed to us, same for nacks

* compiler error: remove comma

* nonce window 4 bytes

* clean up logging
2025-07-25 09:46:18 +02:00
809e222a31 Improve BLE connection stability and message reliability (#318)
* Remove sequence numbers from protocol

- Remove sequenceNumber field from BitchatPacket struct
- Update BinaryProtocol to not encode/decode sequence numbers (header size reduced from 17 to 13 bytes)
- Replace sequence-based duplicate detection with content-based using packet ID hash
- Update packet ID generation to use SHA256(senderID + timestamp + type + payload prefix)
- Remove all sequence tracking variables and methods
- Simplify duplicate detection to rely on timestamp and content hashing

* Fix connection stability issues

- Increase peer availability check interval from 5 to 15 seconds
- Fix availability logic to not mark connected peers as unavailable
- Add BLE connection keepalive timer (20s) to prevent iOS timeouts
- Fix missing delivery ACKs by passing peripheral context through Noise decryption
- Reduce identity announce frequency from 2 to 10 seconds minimum
- Remove unnecessary identity announces on connection
- Debounce identity cache keychain saves (2 second delay)
- Add message retry notification handler in ChatViewModel
- Fix version negotiation redundancy by checking existing negotiations
- Keep Noise sessions for already-connected peers

* Fix build errors in message retry handler

- Fix reference to 'displayedMessages' - should be 'messages'
- Fix sendMessage call signature to use individual parameters instead of message object
- Both iOS and macOS builds now succeed

* Fix remaining connection stability issues

- Fix duplicate identity announces with content-based deduplication
- Simplify peripheral mapping with cleaner temp ID to peer ID transitions
- Improve graceful leave detection across peer ID rotations
- Track previousPeerID from announcements to maintain state
- Add time-based cleanup for gracefully left peers

* Fix connection stability issues

- Add special duplicate detection for identity announces
- Simplify peripheral mapping with dedicated structure
- Improve graceful leave detection with peer ID rotation handling
- Track graceful leave timestamps for cleanup
- Transfer states properly during peer ID rotation

* Improve BLE connection stability and message reliability

- Increase peer availability timeout from 5s to 15s to prevent flapping
- Add BLE keepalive timer with 30s interval to maintain connections
- Fix missing delivery ACKs by passing peripheral context through decryption
- Reduce identity announce frequency from 2s to 10s minimum interval
- Add keychain save debouncing with 2s delay to prevent excessive writes
- Implement message retry system for failed deliveries to favorites
- Fix version negotiation redundancy by checking existing state
- Add special duplicate detection for identity announcements
- Implement graceful leave detection with peer ID rotation support
- Simplify peripheral mapping to reduce complexity
- Fix switch statement structure issues causing build errors

These changes significantly improve connection stability, eliminate peer availability flapping, and ensure reliable message delivery.

---------

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
2025-07-25 02:40:51 +02:00
86726d7033 Fix RSSI nil display issue (#316)
* Add comprehensive RSSI debugging logs

- Log RSSI capture during discovery phase
- Log RSSI reading after peripheral connection
- Log didReadRSSI results including validation and retries
- Log getPeerRSSI() calls and dictionary contents
- Log RSSI transfer from temp IDs to real peer IDs
- Add UI logging to trace nil RSSI values in ContentView
- Track RSSI flow from Bluetooth discovery to UI display

* Add detailed logging for RSSI transfer from temp ID to real peer ID

- Log peripheral mapping updates during announce packet handling
- Track connectedPeripherals state before and after mapping
- Log RSSI transfer from peripheralRSSI to peerRSSI
- Identify whether temp ID is found and properly transferred

* Add peripheral lookup fallback for announce packets

- Log whether peripheral is present when announce received
- Add fallback to look up peripheral if not passed as parameter
- This handles cases where announce is received via relay

* Add comprehensive state logging to getPeerRSSI

- Log connectedPeripherals mapping state
- Log peripheralRSSI dictionary contents
- Log UI-side RSSI lookup attempts with dictionary state
- Track exactly what's in the RSSI lookup tables

* Log peer list shown in UI

* Ensure RSSI transfers even when no temp ID mapping exists

* Add debugging for announce packets without peripheral reference

* Fix build errors and add type annotations

* Fix RSSI mapping for relayed announce packets

- Add fallback logic to match unmapped peripherals with announced peers
- Transfer RSSI from temp UUID to real peer ID when single unmapped peripheral exists
- Implement periodic RSSI updates every 10 seconds for all connected peripherals
- Improve RSSI debugging logs to track mapping state

* Fix RSSI mapping condition for single unmapped peripheral

- Remove peerNicknames.count == 0 condition that was preventing mapping
- Map single unmapped peripheral to announcing peer regardless of nickname state
- Remove temp RSSI entries when transferring to real peer ID
- Improve logging to show successful RSSI transfers

* Improve getPeerRSSI to skip temp IDs and check known peers

- Skip temp IDs (non-16 character) when iterating connectedPeripherals
- Add fallback to check peerNicknames for known peers without peripherals
- Check both peerRSSI and peripheralRSSI for known peers
- Improve RSSI lookup coverage for edge cases

* Remove RSSI debug logging and fix build warnings

- Remove all RSSI-related debug logging added during troubleshooting
- Keep functional RSSI tracking code intact
- Fix unused variable warnings in didReadRSSI and updateAllPeripheralRSSI
- Clean up verbose logging while maintaining core functionality

---------

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
2025-07-24 22:22:19 +02:00
jack f4e954b837 Replace commands section with security warning in app info
- Removed COMMANDS section from AppInfoView
- Added WARNING section about unaudited private message security
- Warning displayed at bottom with red styling for visibility
- Users advised not to use for critical situations until audit complete
2025-07-24 20:04:30 +02:00
492f90edd5 Optimize BLE mesh network for robustness and range (#314)
- Fixed relay probability calculation for 2-node networks (0% relay needed)
- Added protocol ACKs to prevent unnecessary retransmissions
- Implemented MessageState for enhanced duplicate detection
- Added exponential backoff for collision avoidance
- Fixed duplicate sends for bidirectional connections
- Resolved packet ID generation issues using immutable fields only
- Implemented smart rate limiting with progressive throttling
- Removed unnecessary debug logging and fixed build warnings
- Optimized message routing to prevent flooding in small networks

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
2025-07-24 19:49:37 +02:00
jack 498bb30d82 Fix iOS keyboard constraint warnings
- Added 0.1s delay before focusing text field on appear
- This gives iOS time to properly layout keyboard views
- Prevents constraint conflict warnings in system keyboard
2025-07-24 15:15:28 +02:00
jack bdbc0a6cfe Fix duplicate disconnect messages when Mac app closes
- Added gracefullyLeftPeers tracking to prevent duplicate disconnect messages
- Show disconnect message immediately when leave announcement is received
- Skip duplicate message when physical disconnect happens after graceful leave
- Fixed API misuse by adding peripheral.state checks before readRSSI() calls
- Added debug logging to track graceful leave flow
2025-07-24 15:11:45 +02:00
jack 439668f745 Fix duplicate ChatViewModel disconnect messages
- Prevent didDisconnectFromPeer delegate call when peer gracefully left
- This prevents duplicate 'rick disconnected' messages in chat UI
2025-07-24 14:58:21 +02:00
jack fbc904a331 Fix duplicate disconnect messages and API misuse errors
- Track peers that gracefully left with 'gracefullyLeftPeers' set
- Only log disconnect message if peer didn't send leave message first
- Add state checks before all peripheral.readRSSI() calls
- Prevents API misuse errors for disconnected peripherals
- Clear gracefullyLeftPeers after 5 seconds to handle race conditions
2025-07-24 14:52:22 +02:00
jack a3773a6b31 Fix build errors: wrap textInputAutocapitalization in iOS conditional
- textInputAutocapitalization is iOS-only API
- Wrapped in #if os(iOS) to fix macOS build
2025-07-24 14:38:16 +02:00
jack ffab7b695c Fix NaN errors and timeout warnings when typing
- Disable iOS autocorrection on text fields to prevent conflicts
- Add NaN validation for all geometry and gesture calculations
- Fix frame calculations to handle invalid geometry values
- Validate translation and velocity values in drag gestures
- Prevent Result accumulator timeout warnings
2025-07-24 14:33:20 +02:00
jackandGitHub eac3531a54 Merge pull request #310 from permissionlesstech/fix/ui-polish
UI Polish and Performance Optimizations
2025-07-24 14:23:41 +02:00
jack cea0579bce Fix build error: move PeerDisplayData struct outside ViewBuilder
- Move struct declaration to top level to avoid ViewBuilder error
- Struct declarations cannot be inside ViewBuilder closures
2025-07-24 13:47:44 +02:00
jack c7f0a5ea02 Implement lazy loading for heavy components
- Add LazyLinkPreviewView wrapper to defer link preview loading
- Only render sidebar content when visible or animating
- Defer expensive computations until components are shown
- Reduces initial render cost and memory usage
2025-07-24 13:46:23 +02:00
jack 2350c48e13 Reduce ForEach overhead with pre-computed data
- Remove unnecessary Array() conversions in messages ForEach
- Optimize URL ForEach to use indices instead of enumerated
- Simplify autocomplete ForEach by removing unneeded enumeration
- Pre-compute all peer display data outside ForEach for sidebar
- Create PeerDisplayData struct to bundle peer properties
- Move sorting and data preparation outside the render loop
2025-07-24 13:43:02 +02:00
jack 7120b11692 Optimize autocomplete with debouncing
- Add 150ms debounce to text field onChange handler
- Prevents excessive autocomplete calculations during rapid typing
- Add timer cleanup in onDisappear
- Works for both main chat and private chat inputs
2025-07-24 13:40:34 +02:00
jack bb6552f4a6 Add more spacing between chat messages
- Increased vertical padding from 1px to 2px per message
- Total spacing between messages now 4px (was 2px)
- Improves readability and visual separation
2025-07-24 13:34:21 +02:00
jack 354d3ff6ce Optimization 5: Fix animation overhead
- Removed redundant animation modifiers (multiple animations on same view)
- Replaced spring animations with simpler easeInOut/easeOut curves
- Spring animations are computationally expensive, now using 0.2s easeInOut
- Added scroll throttling to prevent excessive animations on message updates
- Scroll animations limited to once per 0.5 seconds
- Removed withAnimation wrapper from scrollTo calls
- Simplified drag gesture animations from spring to easeOut

This reduces CPU usage during UI transitions and message updates
2025-07-24 13:30:56 +02:00
jack 8d64920f8e Optimization 4: Batch message updates for better performance
- Added message batching system with 100ms window
- Created pendingMessages array for public messages
- Created pendingPrivateMessages dictionary for private messages
- Added scheduleBatchFlush() to consolidate updates
- Batch all incoming messages from didReceiveMessage
- Batch system messages (connect/disconnect/screenshot)
- Keep immediate updates for user's own sent messages
- Single UI update per batch instead of per message
- Flush pending messages on app background/terminate
- Clear timer on deinit

This reduces UI updates when multiple messages arrive quickly
2025-07-24 13:23:31 +02:00
jack 58876704c3 Bold user's own nickname in chat messages
- Added font weight logic to formatMessageAsText and formatMessage
- User's own @nickname appears bold (weight: .bold)
- Other nicknames remain medium weight
- Helps visually distinguish own messages from others
2025-07-24 13:18:12 +02:00
jack b51000939b Remove RSSI-based sender name coloring in chat messages
- Simplified getSenderColor to always return primary color
- Removed RSSI color calculation logic from formatMessageAsText
- Removed RSSI color calculation logic from formatMessage
- All sender names now use consistent green color
- RSSI information remains visible only in peer list
2025-07-24 13:16:47 +02:00
jack e13380fe27 Set message limit to 1337 and auto-trim oldest messages
- Added maxMessages constant set to 1337
- Added trimMessagesIfNeeded() to remove oldest messages when limit exceeded
- Added trimPrivateChatMessagesIfNeeded() for private chat limits
- Applied trimming after all message append operations:
  - Regular chat messages (sent and received)
  - Private messages (sent and received)
  - System messages (connect/disconnect/screenshot)
  - Message migrations between peer IDs
- Ensures chat history doesn't grow unbounded
2025-07-24 13:14:37 +02:00
jack f9d5a07542 Optimization 3: Add caching for expensive view computations
- Cache getRSSIColor results with key format: "\(rssi)_\(isDark)"
- Cache getEncryptionStatus results with peerID as key
- Add cache invalidation methods for both caches
- Invalidate encryption cache when:
  - Encryption status is updated
  - Fingerprints are verified
  - Peer authentication callbacks trigger
- Invalidate RSSI cache when peer list updates
- Clear all caches in panicClearAllData

This reduces redundant calculations during view updates and improves performance
2025-07-24 13:09:18 +02:00
jack 5d3512dfbc Add 1pt more padding around link preview box
- Increased padding from 9pt to 10pt for better visual spacing
- Improves readability and touch target size
2025-07-24 13:04:43 +02:00
jack 5a0c8f0a26 Add 1pt more padding around link preview box
- Increase top padding from 2pt to 3pt
- Add 1pt horizontal padding for better visual balance
2025-07-24 12:59:46 +02:00
jack c08fe38c72 Fix URL detection and message spacing
- Add URL detection to message formatting with blue color and underline
- Replace List with ScrollView for precise spacing control
- Use minimal vertical padding (1px) between messages
- Remove LazyLinkPreviewView, use LinkPreviewView directly
- URLs now appear highlighted in message text AND show preview below
2025-07-24 12:53:55 +02:00
jack b480a4836c Fix message spacing and remove markdown link support
- Reduce VStack spacing from 4 to 2 and nested VStack from 2 to 0
- Set list row insets top/bottom to 0 to minimize spacing on iPhone
- Remove all markdown link parsing and formatting
- Share plain URLs instead of markdown when using share extension
- Keep only plain text URL detection and preview functionality
2025-07-24 12:47:49 +02:00
jack f45c52e9d3 Optimize UI performance with message caching and List view
- Convert BitchatMessage from struct to class for efficient caching
- Cache formatted AttributedStrings to avoid expensive regex on every render
- Replace ScrollView+LazyVStack with native List for better cell reuse
- Implement message windowing (show last 100 messages for performance)
- Add LazyLinkPreviewView that defers loading for 0.5s to improve scroll performance

These optimizations significantly improve scroll performance, especially with large message lists.
2025-07-24 12:38:31 +02:00
jack a338008511 Remove nickname label from chat input box
- Remove <@nickname> label to simplify UI
- Add placeholder text "type a message..." for clarity
- Maintain consistent padding for input field
2025-07-24 12:22:50 +02:00
jack cc3a094205 Add nickname validation to prevent empty names
- Validate nickname only when field loses focus or on submit
- Replace empty nicknames with anon+random 4 digits (e.g. anon3847)
- Remove auto-save timer to prevent validation while typing
- Add FocusState to detect when user exits the field
2025-07-24 12:16:23 +02:00
jack c4ca38654d Improve private message notification behavior
- Don't show PM notifications when the private chat is already open
- Store peer ID in notification userInfo for reliable chat opening
- Tap notification to open the correct private chat using peer ID
- Check both in ChatViewModel (when receiving) and NotificationDelegate (when presenting)
2025-07-24 12:00:01 +02:00
jack b0204afa75 Add message action sheet for user interactions
- Tap any message to show action sheet with user options
- Actions include: private message, hug, slap, and block
- Only appears for messages from other users (not system or self)
- Integrates with existing chat commands (/hug, /slap, /block)
2025-07-24 11:54:09 +02:00
jack 0beee2fb27 Improve peer list UI polish
- Make entire peer list row tappable for starting private chats
- Fix separator bar visibility when private chat is fully open (offset by -1px)
- Keep favorite star as separate tap target
- Preserve double-tap gesture for showing fingerprints
2025-07-24 11:47:20 +02:00