mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-24 22:45:19 +00:00
Implement Nostr NIP-17 for offline messaging and performance optimizations (#358)
* Implement Nostr NIP-17 integration for offline mutual favorite messaging - Add Nostr relay connectivity and NIP-17 gift-wrapped private messages - Implement dual transport system: Bluetooth mesh + Nostr relays - Add favorites persistence with mutual detection and Nostr key exchange - Support offline messaging for mutual favorites via Nostr relays - Handle peer identity rotation with automatic favorite key updates - Fix UI to show all favorites (online and offline) in peer list - Add proper message routing based on peer availability - Update peer list icons: 📶 for mesh, 🌐 for Nostr, 🌙 for one-sided - Fix toolbar display for offline peers in private chat view - Add network entitlements for macOS and iOS - Implement automatic noise key updates when peers reconnect * Implement Nostr NIP-17 for private messaging between mutual favorites - Add support for NIP-17 gift-wrapped private messages with double encryption - Enable private messaging via Nostr when mutual favorites are offline - Fix peer reconnection issues: users now stay in private chat when peer reconnects - Fix read receipt delivery: send pending receipts when peer comes back online - Add message ID tracking through Nostr transport for proper delivery acknowledgments - Update peer noise key mapping when peers reconnect with different IDs - Check for Nostr messages when app becomes active - Implement 7-day message retrieval window for better reliability * Fix private message UI refresh and adjust PEOPLE header spacing - Fix UI not updating when receiving private messages on mesh - Add immediate batch processing for messages in active chat - Force UI update when viewing current chat peer - Ensure real-time message display without navigation - Reduce PEOPLE header spacing from 16 to 12 points for tighter UI * Fix build errors and unused value warnings in Nostr favorites integration * Implement read receipts via Nostr - Added sendReadReceipt method to MessageRouter to send receipts via mesh or Nostr - Added handleReadReceipt to process incoming read receipts from Nostr - Made ReadReceipt.readerID mutable to allow updates - Added missing notification names and error cases - Uncommented and enabled read receipt handling in ChatViewModel - Read receipts now work seamlessly via both mesh and Nostr transports * Fix ReadReceipt initialization - use correct constructor * Implement persistent message deduplication for Nostr - Added ProcessedMessagesService to track messages across app restarts - Store processed message IDs and last timestamp in UserDefaults - Skip already processed messages when receiving from Nostr - Adjust subscription filter to use smart timestamp (last processed or 24h) - Prevents duplicate messages when reconnecting to Nostr relays * Fix peer list UI not updating to Nostr mode on disconnect - Remove peer from peerNicknames when connection state changes to disconnected - Ensures UI properly reflects peer disconnection state - Peer list now correctly shows Nostr mode (🌐) when peer walks out of range * Update peer count to include Nostr peers and improve UI indicators - Peer count now shows total peers including those available via Nostr - Count appears purple when only Nostr peers are connected - Private message header shows purple globe icon for Nostr transport - Consistent visual language for Nostr connectivity across the app * Improve RSSI real-time updates and fix UI flashing - Reduce RSSI update timer from 10s to 5s and per-peripheral from 5s to 3s - Add RSSI change detection with 2 dBm threshold for responsive updates - Always update previous RSSI values to fix gradual change detection bug - Trigger RSSI read on peer authentication for immediate status - Fix UI flashing 'nobody around' by removing array clearing on updates - Add proper cleanup of RSSI tracking on disconnect and peer rotation * Fix favorite nickname updates and peer list filtering - Add updateNickname method to FavoritesPersistenceService to update nicknames while preserving favorite status - Update announce handler to check for existing favorites and update their nicknames - Remove dead BluetoothMeshService+PublicAPI.swift file - Move sendFavoriteNotification to main BluetoothMeshService - Fix peer list to only show connected peers and user's favorites (not peers who favorite the user) - Remove UI logic for showing peers who favorite us but we don't favorite back * Remove dead code and fix ghost connections Phase 1 - Remove abandoned peer ID rotation code: - Remove previousPeerID property and rotationGracePeriod constant - Remove grace period logic from isPeerIDOurs() - Remove previousPeerID handling from announce packets - Pass nil for previousPeerID in identity announcements Phase 2 - Fix ghost connections from relayed packets: - CRITICAL FIX: Only add peers to activePeers if they have a peripheral connection - Check for peripheral connection before marking peer as active - Prevents ghost connections when announce packets are relayed - Log warning when rejecting relayed announce without peripheral Phase 3 - Begin consolidating redundant peer tracking: - Create new PeerSession class to unify peer data in one place - Add helper methods for PeerSession management - Integrate PeerSession into announce packet handling - Update authentication state changes to use PeerSession - Update peripheral mapping and RSSI to sync with PeerSession - Update disconnect and leave handling to update PeerSession - Add consolidated getter methods for peer info This fixes the issue where peers appeared connected without actually having a Bluetooth connection, and begins the migration to a cleaner single-source-of-truth peer tracking system. * Fix multiple connect messages on peer restart - Move hasPeripheralConnection check outside sync block to fix scope issue - Add debug logging to track connect message conditions - Ensure connect messages only show on first connection or reconnection with peripheral * Optimize RSSI updates for better battery life - Add app state tracking to BluetoothMeshService - Only update RSSI when app is in foreground and peer list is visible - Add setPeerListVisible method to control RSSI updates - Remove individual periodic RSSI updates in favor of centralized timer - Update ContentView to notify mesh service of peer list visibility changes - Improve battery efficiency by avoiding unnecessary RSSI reads * Initialize peer list visibility state on view appear - Ensure RSSI timer state is properly initialized when view loads - Call setPeerListVisible with initial showSidebar value * Fix duplicate peers and multiple disconnect messages - Fixed duplicate peer entries when relay-connected by adding relay-connected peers to connectedNicknames set - Added deduplication logic for disconnect messages with 2-second window to prevent multiple disconnect notifications for same peer - Added cleanup for old disconnect notification tracking to prevent memory growth * Fix peer count indicator color logic - Show green for any mesh peer (direct Bluetooth or relay connected) - Show purple only for Nostr-only peers (no mesh connections) - Show red only when no peers are reachable at all - Fixed to use meshPeerCount instead of viewModel.isConnected which only checked direct connections * Fix relay connection issues and peripheral mapping cleanup - Fixed relay-connected peers being marked as directly connected when receiving identity announce - Added proper cleanup of temp peripheral mappings when discovering real peer ID - Fixed disconnect notification deduplication cleanup - Improved debug logging to show actual connection state (direct/relay/nostr/offline) - Added debug logging for relay connection detection - Fixed compiler warning about unused variable * Fix Unknown peer disconnect notifications and disable faulty relay detection - Add check to prevent disconnect notifications for Unknown peers that never announced - Disable relay connection detection until proper relay tracking is implemented - In a 2-peer network, peers should never show as relay-connected * Fix RSSI updates and peer visibility after reconnection - Add updatePeers() call in didUpdatePeerList to refresh RSSI values in UI - Track version hello times to better detect direct connections - Allow peers to be marked active if recent version hello received - Fix thread safety for version hello tracking - Clean up old version hello times to prevent memory leaks * Remove RSSI tracking completely and replace with radio icon for mesh connections * Fix build errors after RSSI removal - Add missing peripheralID declaration in didDiscover delegate method - Remove obsolete setPeerListVisible calls from ContentView * Center private message header elements using ZStack layout - Replace HStack with ZStack for perfect centering - Globe/nick/lock cluster now always centered regardless of button sizes - Back and favorite buttons positioned in overlay HStack * Fix private chat view showing Unknown when peer reconnects with new ID - Update FavoritesPersistenceService to notify with both old and new keys - Handle peer ID changes in ChatViewModel to migrate private chat data - Update selectedPrivateChatPeer when favorite's noise key changes - Maintain chat history and unread status across peer ID changes * Fix read receipts after peer reconnection and replace nos.lol relay - Updated sendReadReceipt to resolve current peer ID when peers reconnect with new IDs - Enhanced MessageRouter to check favorites for current noise keys - Replaced nos.lol relay with relay.snort.social to avoid PoW requirements * Fix message routing to use Nostr when peers are disconnected Changed message routing logic to check actual peer connection status using isPeerConnected() instead of just checking if peer exists in nickname list. This ensures that offline mutual favorites correctly route messages through Nostr instead of attempting Bluetooth handshakes. Also added safety check to prevent starting private chat with ourselves. * Add debug logging for Nostr timestamp randomization Added logging to track the random offset being applied to Nostr event timestamps to debug why messages appear 8-9 minutes in the future. * Fix Nostr timestamp issue by reducing randomization range Temporarily reduced the timestamp randomization from +/-15 minutes to +/-1 minute to address messages appearing 8-9 minutes in the future. Added detailed UTC/local time logging to help debug the issue. The random offset should have been evenly distributed but was consistently showing positive offsets. This change mitigates the issue while we investigate the root cause. * Fix message routing for offline favorites and reduce Nostr timestamp randomization - Fix transport selection to properly detect disconnected peers using isPeerConnected() - Change from checking peer nicknames to checking actual connection status - Reduce Nostr timestamp randomization from ±15 minutes to ±1 minute - Add detailed timestamp logging for debugging * Improve PM header UI and encryption status display - Show transport icons (radio/link/globe) in PM header matching peer list - Always show lock icon if noise session ever established (no handshake icon) - Change verified icon from shield to checkmark seal - Use consistent green color (textColor) for PM header and encryption icons * Update AI_CONTEXT.md with comprehensive Nostr implementation details - Add Nostr and MessageRouter to architecture diagram - Document NIP-17 gift wrap implementation - Explain favorites integration and mutual requirement - Detail message routing logic and transport selection - Add security considerations and debugging tips - Update common tasks with Nostr-specific guidance * Fix data consistency issues in favorites, chat migration, and bloom filter - Fix favorites deduplication to use public key instead of nickname Prevents losing favorites when multiple peers use same nickname - Fix private chat migration to use fingerprints instead of nicknames Prevents merging unrelated conversations that share nicknames Fallback to nickname matching only for legacy data without fingerprints - Fix bloom filter reset to preserve messages from last 10 minutes Prevents duplicate message processing after bloom filter resets Keeps processedMessages for 10 minutes while bloom filter resets every 5 * Add mutual favorites internet messaging to app info * Remove excessive debug/info logging for production readiness - Removed ~140 debug/info level logs across core services - Preserved critical logs: errors, warnings, security events, state changes - Kept logs for: peer join/leave, favorite status, mutual relationships - Cleaned up verbose logging in: Bluetooth mesh, Nostr, message routing - Improved performance by reducing log I/O overhead 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Implement performance optimizations and fix build warnings - Add UI update debouncing (50ms) to prevent excessive SwiftUI refreshes - Implement memory bounds for processedMessages with LRU eviction - Add encryption queue cleanup for disconnected peers - Optimize peer lookups from O(n) to O(1) with indexed dictionary - Fix multiple compiler warnings (unused variables, missing break statements) - Optimize peer counting with single-pass reduce operation - Fix ViewBuilder control flow issue in ContentView - Fix Dictionary initialization type mismatches with Array wrapper * Add TTL-based cleanup for Noise handshake sessions - Add session TTL (5 minutes) and max session limit (50) to NoiseHandshakeCoordinator - Clean up old established sessions to prevent unbounded memory growth - Move handshake cleanup timer out of DEBUG conditional for production use - Run cleanup every 60 seconds in production (vs 30s in debug) - Clean up crypto state immediately on peer disconnect - Prevents memory leaks from accumulating Noise sessions * Pre-compute and store fingerprints in PeerSession for O(1) lookups - Store fingerprint in PeerSession when peer authenticates - Update getPeerFingerprint() and getFingerprint() to check PeerSession first - Replace all noiseService.getPeerFingerprint() calls with optimized version - Eliminates repeated SHA256 calculations during message processing - Improves performance for favorite checks and encryption status updates * Implement exponential backoff for Nostr relay connections - Add reconnection tracking fields to Relay struct (attempts, timing) - Replace fixed 5-second delay with exponential backoff (1s → 2s → 4s... max 5min) - Stop reconnection attempts after 10 failures to prevent infinite retries - Reset attempt counter on successful connection - Add utility methods: retryConnection(), getRelayStatuses(), resetAllConnections() - DNS failures still bypass retry logic as before - Improves battery life and reduces server load from constant reconnection attempts --------- Co-authored-by: jack <jackjackbits@users.noreply.github.com> Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
+208
-10
@@ -13,6 +13,8 @@ BitChat is a decentralized, peer-to-peer messaging application that works over B
|
||||
- **Store & Forward**: Messages cached for offline peers
|
||||
- **IRC-Style Commands**: Familiar `/msg`, `/who` interface
|
||||
- **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
|
||||
|
||||
@@ -28,17 +30,24 @@ BitChat is a decentralized, peer-to-peer messaging application that works over B
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ Security Layer │
|
||||
│ (NoiseEncryptionService, SecureIdentityStateManager) │
|
||||
│ 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) │
|
||||
└───────────────────────────────┘ └────────────────────────────────┘
|
||||
│
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ Protocol Layer │
|
||||
│ (BitchatProtocol, BinaryProtocol, NoiseProtocol) │
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
┌─────────────────────────────────────────────────────────────────┐
|
||||
│ Transport Layer │
|
||||
│ Bluetooth Transport Layer │
|
||||
│ (BluetoothMeshService) │
|
||||
└─────────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
@@ -94,6 +103,32 @@ BitChat is a decentralized, peer-to-peer messaging application that works over B
|
||||
- UI state management
|
||||
- Private chat coordination
|
||||
|
||||
### 6. Nostr Integration
|
||||
- **Locations**:
|
||||
- `bitchat/Nostr/NostrProtocol.swift` - NIP-17 private message implementation
|
||||
- `bitchat/Nostr/NostrRelayManager.swift` - WebSocket relay connections
|
||||
- `bitchat/Nostr/NostrIdentity.swift` - Nostr key management
|
||||
- `bitchat/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**:
|
||||
1. Always prefer Bluetooth mesh when peer is connected
|
||||
2. Use Nostr for mutual favorites when peer is offline
|
||||
3. 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
|
||||
@@ -117,6 +152,12 @@ BitChat is a decentralized, peer-to-peer messaging application that works over B
|
||||
- **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/`)
|
||||
@@ -146,6 +187,147 @@ MVVM architecture for UI:
|
||||
- `ChatViewModel`: 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:
|
||||
|
||||
1. **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
|
||||
|
||||
2. **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
|
||||
|
||||
3. **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:
|
||||
|
||||
1. **Favorite Establishment**:
|
||||
- User favorites a peer via `/fav` command
|
||||
- Favorite notification sent via Bluetooth (if connected)
|
||||
- Peer's Nostr public key exchanged during favorite process
|
||||
- Stored in `FavoritesPersistenceService`
|
||||
|
||||
2. **Mutual Requirement**:
|
||||
- Both peers must favorite each other
|
||||
- Prevents spam and unwanted Nostr messages
|
||||
- Enforced by `MessageRouter` transport selection
|
||||
|
||||
3. **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:
|
||||
|
||||
```swift
|
||||
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.io`
|
||||
- `wss://relay.primal.net`
|
||||
- `wss://offchain.pub`
|
||||
- `wss://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>` or `UNFAVORITED:<senderNpub>`
|
||||
- Delivery ACK: `DELIVERED:<messageID>`
|
||||
- Read Receipt: `READ:<base64EncodedReceipt>`
|
||||
|
||||
### Implementation Details
|
||||
|
||||
1. **NostrRelayManager**:
|
||||
- Manages WebSocket connections to relays
|
||||
- Handles reconnection logic
|
||||
- Processes EVENT, EOSE, OK, NOTICE messages
|
||||
- Implements NIP-01 relay protocol
|
||||
|
||||
2. **NostrProtocol**:
|
||||
- Implements NIP-17 encryption/decryption
|
||||
- Handles gift wrap creation/unwrapping
|
||||
- Manages ephemeral key generation
|
||||
- Provides Schnorr signatures
|
||||
|
||||
3. **ProcessedMessagesService**:
|
||||
- Prevents duplicate message processing
|
||||
- Tracks last subscription timestamp
|
||||
- Persists across app launches
|
||||
- 30-day retention window
|
||||
|
||||
### Security Considerations
|
||||
|
||||
1. **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
|
||||
|
||||
2. **Relay Trust**:
|
||||
- Relays cannot read message content
|
||||
- Relays can see recipient pubkey (gift wrap)
|
||||
- Relays cannot determine sender
|
||||
- Multiple relays used for redundancy
|
||||
|
||||
3. **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
|
||||
|
||||
1. **Check relay connections**:
|
||||
- Look for "Connected to Nostr relay" in logs
|
||||
- Verify WebSocket state in NostrRelayManager
|
||||
- Check for relay errors/notices
|
||||
|
||||
2. **Verify gift wrap creation**:
|
||||
- Enable debug logging in NostrProtocol
|
||||
- Check ephemeral key generation
|
||||
- Verify encryption steps
|
||||
|
||||
3. **Message delivery**:
|
||||
- Check ProcessedMessagesService for duplicates
|
||||
- Verify subscription filters
|
||||
- Look for EVENT messages in relay responses
|
||||
|
||||
## Development Guidelines
|
||||
|
||||
### 1. Security First
|
||||
@@ -194,6 +376,20 @@ MVVM architecture for UI:
|
||||
3. Monitor characteristic updates
|
||||
4. Use Bluetooth debugging tools
|
||||
|
||||
### Working with Nostr Transport
|
||||
1. Verify mutual favorite status in `FavoritesPersistenceService`
|
||||
2. Check Nostr key derivation in `NostrIdentity`
|
||||
3. Monitor relay connections in `NostrRelayManager`
|
||||
4. Test gift wrap encryption/decryption
|
||||
5. Verify transport selection in `MessageRouter`
|
||||
|
||||
### Adding Nostr Features
|
||||
1. Understand NIP-17 gift wrap structure
|
||||
2. Maintain ephemeral key hygiene
|
||||
3. Test with multiple relays
|
||||
4. Preserve metadata privacy
|
||||
5. Handle relay disconnections gracefully
|
||||
|
||||
## Security Threat Model
|
||||
|
||||
### Assumptions
|
||||
@@ -266,9 +462,11 @@ MVVM architecture for UI:
|
||||
## Quick Start for AI Assistants
|
||||
|
||||
1. **Understand the layers**: Transport → Protocol → Security → Services → UI
|
||||
2. **Follow the data flow**: BLE → Binary → Protocol → ViewModel → View
|
||||
2. **Follow the data flow**: BLE/Nostr → Binary/JSON → Protocol → ViewModel → View
|
||||
3. **Respect security boundaries**: Never mix trusted and untrusted data
|
||||
4. **Test thoroughly**: This is critical infrastructure for users
|
||||
5. **Ask about design decisions**: Many choices have non-obvious reasons
|
||||
6. **Dual Transport**: Remember that messages can flow over Bluetooth OR Nostr
|
||||
7. **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.
|
||||
@@ -46,6 +46,26 @@
|
||||
04B6BA4F2E2038A70090FE39 /* NoiseEncryptionService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04B6BA4D2E2038A70090FE39 /* NoiseEncryptionService.swift */; };
|
||||
04B6BA552E203D6C0090FE39 /* FingerprintView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04B6BA532E203D6C0090FE39 /* FingerprintView.swift */; };
|
||||
04B6BA572E203D6C0090FE39 /* FingerprintView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04B6BA532E203D6C0090FE39 /* FingerprintView.swift */; };
|
||||
04E363362E3800310048E624 /* P256K in Frameworks */ = {isa = PBXBuildFile; productRef = 04F127E72E37EBCD00FFBA8D /* P256K */; };
|
||||
04E363372E3800310048E624 /* P256K in Frameworks */ = {isa = PBXBuildFile; productRef = 04F127E82E37EBDB00FFBA8D /* P256K */; };
|
||||
04F127E42E37EBAA00FFBA8D /* NostrIdentity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F127E22E37EBAA00FFBA8D /* NostrIdentity.swift */; };
|
||||
04F127E52E37EBAA00FFBA8D /* NostrIdentity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F127E22E37EBAA00FFBA8D /* NostrIdentity.swift */; };
|
||||
04F127F22E37EEB800FFBA8D /* NostrProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F127F02E37EEB800FFBA8D /* NostrProtocol.swift */; };
|
||||
04F127F32E37EEB800FFBA8D /* NostrRelayManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F127F12E37EEB800FFBA8D /* NostrRelayManager.swift */; };
|
||||
04F127F42E37EEB800FFBA8D /* NostrProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F127F02E37EEB800FFBA8D /* NostrProtocol.swift */; };
|
||||
04F127F52E37EEB800FFBA8D /* NostrRelayManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F127F12E37EEB800FFBA8D /* NostrRelayManager.swift */; };
|
||||
04F127F82E37EEEE00FFBA8D /* MessageRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F127F72E37EEEE00FFBA8D /* MessageRouter.swift */; };
|
||||
04F127F92E37EEEE00FFBA8D /* FavoritesPersistenceService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F127F62E37EEEE00FFBA8D /* FavoritesPersistenceService.swift */; };
|
||||
04F127FA2E37EEEE00FFBA8D /* MessageRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F127F72E37EEEE00FFBA8D /* MessageRouter.swift */; };
|
||||
04F127FB2E37EEEE00FFBA8D /* FavoritesPersistenceService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F127F62E37EEEE00FFBA8D /* FavoritesPersistenceService.swift */; };
|
||||
04F127FE2E37EF3D00FFBA8D /* BitchatPeer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F127FC2E37EF3D00FFBA8D /* BitchatPeer.swift */; };
|
||||
04F127FF2E37EF3D00FFBA8D /* BitchatPeer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F127FC2E37EF3D00FFBA8D /* BitchatPeer.swift */; };
|
||||
04F128062E37F10000FFBA8D /* PeerSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F128072E37F10000FFBA8D /* PeerSession.swift */; };
|
||||
04F128082E37F10000FFBA8D /* PeerSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F128072E37F10000FFBA8D /* PeerSession.swift */; };
|
||||
04F128012E37FF4E00FFBA8D /* libsecp256k1 in Frameworks */ = {isa = PBXBuildFile; productRef = 04F128002E37FF4E00FFBA8D /* libsecp256k1 */; };
|
||||
04F128032E37FFB900FFBA8D /* libsecp256k1 in Frameworks */ = {isa = PBXBuildFile; productRef = 04F128022E37FFB900FFBA8D /* libsecp256k1 */; };
|
||||
04F128042E37F00000FFBA8D /* ProcessedMessagesService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F127FF2E37F00000FFBA8D /* ProcessedMessagesService.swift */; };
|
||||
04F128052E37F00000FFBA8D /* ProcessedMessagesService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 04F127FF2E37F00000FFBA8D /* ProcessedMessagesService.swift */; };
|
||||
0FBC81FF78CF4711B78E092A /* IdentityModels.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E2446380E7A44E49A35B664 /* IdentityModels.swift */; };
|
||||
10E68BB889356219189E38EC /* BitchatApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF625BB3AD919322C01A46B2 /* BitchatApp.swift */; };
|
||||
17901751FD8010AFC8E750F2 /* bitchatShareExtension.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = 61F92EBA29C47C0FCC482F1F /* bitchatShareExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
|
||||
@@ -142,6 +162,14 @@
|
||||
04B6BA432E2035530090FE39 /* NoiseSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoiseSession.swift; sourceTree = "<group>"; };
|
||||
04B6BA4D2E2038A70090FE39 /* NoiseEncryptionService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoiseEncryptionService.swift; sourceTree = "<group>"; };
|
||||
04B6BA532E203D6C0090FE39 /* FingerprintView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FingerprintView.swift; sourceTree = "<group>"; };
|
||||
04F127E22E37EBAA00FFBA8D /* NostrIdentity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NostrIdentity.swift; sourceTree = "<group>"; };
|
||||
04F127F02E37EEB800FFBA8D /* NostrProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NostrProtocol.swift; sourceTree = "<group>"; };
|
||||
04F127F12E37EEB800FFBA8D /* NostrRelayManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NostrRelayManager.swift; sourceTree = "<group>"; };
|
||||
04F127F62E37EEEE00FFBA8D /* FavoritesPersistenceService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FavoritesPersistenceService.swift; sourceTree = "<group>"; };
|
||||
04F127F72E37EEEE00FFBA8D /* MessageRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageRouter.swift; sourceTree = "<group>"; };
|
||||
04F127FC2E37EF3D00FFBA8D /* BitchatPeer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BitchatPeer.swift; sourceTree = "<group>"; };
|
||||
04F128072E37F10000FFBA8D /* PeerSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PeerSession.swift; sourceTree = "<group>"; };
|
||||
04F127FF2E37F00000FFBA8D /* ProcessedMessagesService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProcessedMessagesService.swift; sourceTree = "<group>"; };
|
||||
12B9C3EDF3BC73D3BC106DA4 /* DeliveryTracker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeliveryTracker.swift; sourceTree = "<group>"; };
|
||||
136696FC4436A02D98CE6A77 /* KeychainManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KeychainManager.swift; sourceTree = "<group>"; };
|
||||
229F17B68CFF7AB1BC91C847 /* BitchatProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BitchatProtocol.swift; sourceTree = "<group>"; };
|
||||
@@ -174,6 +202,27 @@
|
||||
FF7AF93D874001FBD94C8306 /* bitchat-macOS.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "bitchat-macOS.entitlements"; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
04F127E92E37EBEF00FFBA8D /* Frameworks */ = {
|
||||
isa = PBXFrameworksBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
04E363362E3800310048E624 /* P256K in Frameworks */,
|
||||
04F128012E37FF4E00FFBA8D /* libsecp256k1 in Frameworks */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
04F127ED2E37EBFF00FFBA8D /* Frameworks */ = {
|
||||
isa = PBXFrameworksBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
04E363372E3800310048E624 /* P256K in Frameworks */,
|
||||
04F128032E37FFB900FFBA8D /* libsecp256k1 in Frameworks */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXFrameworksBuildPhase section */
|
||||
|
||||
/* Begin PBXGroup section */
|
||||
04636BC62E30BE5100FBCFA8 /* EndToEnd */ = {
|
||||
isa = PBXGroup;
|
||||
@@ -237,6 +286,32 @@
|
||||
path = Noise;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
04F127E32E37EBAA00FFBA8D /* Nostr */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
04F127F02E37EEB800FFBA8D /* NostrProtocol.swift */,
|
||||
04F127F12E37EEB800FFBA8D /* NostrRelayManager.swift */,
|
||||
04F127E22E37EBAA00FFBA8D /* NostrIdentity.swift */,
|
||||
);
|
||||
path = Nostr;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
04F127EA2E37EBF300FFBA8D /* Frameworks */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
);
|
||||
name = Frameworks;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
04F127FD2E37EF3D00FFBA8D /* Models */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
04F127FC2E37EF3D00FFBA8D /* BitchatPeer.swift */,
|
||||
04F128072E37F10000FFBA8D /* PeerSession.swift */,
|
||||
);
|
||||
path = Models;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
18198ED912AAF495D8AF7763 = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
@@ -244,6 +319,7 @@
|
||||
A2E8C336FA1ADBEC03261DFD /* bitchatShareExtension */,
|
||||
C3D98EB3E1B455E321F519F4 /* bitchatTests */,
|
||||
9F37F9F2C353B58AC809E93B /* Products */,
|
||||
04F127EA2E37EBF300FFBA8D /* Frameworks */,
|
||||
);
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
@@ -258,8 +334,10 @@
|
||||
95F16C3A4A5621C74461D8D3 /* LaunchScreen.storyboard */,
|
||||
ADD53BCDA233C02E53458926 /* Protocols */,
|
||||
04B6BA442E2035530090FE39 /* Noise */,
|
||||
04F127E32E37EBAA00FFBA8D /* Nostr */,
|
||||
D98A3186D7E4C72E35BDF7FE /* Services */,
|
||||
6078981E5A3646BC84CC6DB4 /* Identity */,
|
||||
04F127FD2E37EF3D00FFBA8D /* Models */,
|
||||
9A78348821A7D3374607D4E3 /* Utils */,
|
||||
45BB7D87CAE42A8C0447D909 /* ViewModels */,
|
||||
A55126E93155456CAA8D6656 /* Views */,
|
||||
@@ -356,6 +434,9 @@
|
||||
D98A3186D7E4C72E35BDF7FE /* Services */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
04F127F62E37EEEE00FFBA8D /* FavoritesPersistenceService.swift */,
|
||||
04F127F72E37EEEE00FFBA8D /* MessageRouter.swift */,
|
||||
04F127FF2E37F00000FFBA8D /* ProcessedMessagesService.swift */,
|
||||
04B6BA4D2E2038A70090FE39 /* NoiseEncryptionService.swift */,
|
||||
D5C3D880FF8AE1673B20E1E3 /* BluetoothMeshService.swift */,
|
||||
12B9C3EDF3BC73D3BC106DA4 /* DeliveryTracker.swift */,
|
||||
@@ -375,6 +456,7 @@
|
||||
buildPhases = (
|
||||
137ABE739BF20ACDDF8CC605 /* Sources */,
|
||||
0214973A876129753D39EB47 /* Resources */,
|
||||
04F127ED2E37EBFF00FFBA8D /* Frameworks */,
|
||||
);
|
||||
buildRules = (
|
||||
);
|
||||
@@ -382,6 +464,8 @@
|
||||
);
|
||||
name = bitchat_macOS;
|
||||
packageProductDependencies = (
|
||||
04F127E82E37EBDB00FFBA8D /* P256K */,
|
||||
04F128022E37FFB900FFBA8D /* libsecp256k1 */,
|
||||
);
|
||||
productName = bitchat_macOS;
|
||||
productReference = 8F3A7C058C2C8E1A06C8CF8B /* bitchat.app */;
|
||||
@@ -447,6 +531,7 @@
|
||||
4E49E34F00154C051AE90FED /* Sources */,
|
||||
CD6E8F32BC38357473954F97 /* Resources */,
|
||||
B6C356449BAE4E0F650565D1 /* Embed Foundation Extensions */,
|
||||
04F127E92E37EBEF00FFBA8D /* Frameworks */,
|
||||
);
|
||||
buildRules = (
|
||||
);
|
||||
@@ -455,6 +540,8 @@
|
||||
);
|
||||
name = bitchat_iOS;
|
||||
packageProductDependencies = (
|
||||
04F127E72E37EBCD00FFBA8D /* P256K */,
|
||||
04F128002E37FF4E00FFBA8D /* libsecp256k1 */,
|
||||
);
|
||||
productName = bitchat_iOS;
|
||||
productReference = 96D0D41CA19EE5A772AA8434 /* bitchat.app */;
|
||||
@@ -501,6 +588,9 @@
|
||||
);
|
||||
mainGroup = 18198ED912AAF495D8AF7763;
|
||||
minimizedProjectReferenceProxies = 1;
|
||||
packageReferences = (
|
||||
04F127E62E37EBCD00FFBA8D /* XCRemoteSwiftPackageReference "swift-secp256k1" */,
|
||||
);
|
||||
projectDirPath = "";
|
||||
projectRoot = "";
|
||||
targets = (
|
||||
@@ -556,22 +646,30 @@
|
||||
04B6BA462E2035530090FE39 /* NoiseSession.swift in Sources */,
|
||||
04B6BA472E2035530090FE39 /* NoiseProtocol.swift in Sources */,
|
||||
7A50E2F04A3515A7E90EEAE4 /* BluetoothMeshService.swift in Sources */,
|
||||
04F127F42E37EEB800FFBA8D /* NostrProtocol.swift in Sources */,
|
||||
04F127F52E37EEB800FFBA8D /* NostrRelayManager.swift in Sources */,
|
||||
D450CF41F207BDE1A1AAA56E /* ChatViewModel.swift in Sources */,
|
||||
B0CA7796B2B2AC2B33F84548 /* CompressionUtil.swift in Sources */,
|
||||
92D34E7A07C990C8A815B0CE /* ContentView.swift in Sources */,
|
||||
04B6BA4F2E2038A70090FE39 /* NoiseEncryptionService.swift in Sources */,
|
||||
8DCEEA289EF7C49E7CD38B08 /* DeliveryTracker.swift in Sources */,
|
||||
FB8819B4C84FAFEF5C36B216 /* KeychainManager.swift in Sources */,
|
||||
04F127E52E37EBAA00FFBA8D /* NostrIdentity.swift in Sources */,
|
||||
04636BBA2E2FAA1700FBCFA8 /* BinaryEncodingUtils.swift in Sources */,
|
||||
04AD0B4E2E25B9580002A40A /* IdentityModels.swift in Sources */,
|
||||
04AD0B4F2E25B9580002A40A /* SecureIdentityStateManager.swift in Sources */,
|
||||
31D147471B9F4E2815352DDA /* LinkPreviewView.swift in Sources */,
|
||||
04F127FE2E37EF3D00FFBA8D /* BitchatPeer.swift in Sources */,
|
||||
04F128062E37F10000FFBA8D /* PeerSession.swift in Sources */,
|
||||
04B6BA572E203D6C0090FE39 /* FingerprintView.swift in Sources */,
|
||||
04636BED2E30CD4A00FBCFA8 /* NoiseHandshakeCoordinator.swift in Sources */,
|
||||
C99763A4761567F587D21688 /* MessageRetryService.swift in Sources */,
|
||||
749D8CF8A362B6CD0786782D /* NotificationService.swift in Sources */,
|
||||
04636BBF2E2FCA8A00FBCFA8 /* SecureLogger.swift in Sources */,
|
||||
0245710AEAA58AD0A1425234 /* OptimizedBloomFilter.swift in Sources */,
|
||||
04F127F82E37EEEE00FFBA8D /* MessageRouter.swift in Sources */,
|
||||
04F127F92E37EEEE00FFBA8D /* FavoritesPersistenceService.swift in Sources */,
|
||||
04F128042E37F00000FFBA8D /* ProcessedMessagesService.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
@@ -589,22 +687,30 @@
|
||||
04B6BA4A2E2035530090FE39 /* NoiseSession.swift in Sources */,
|
||||
04B6BA4B2E2035530090FE39 /* NoiseProtocol.swift in Sources */,
|
||||
D948085736ED8E736C1DE3B0 /* BluetoothMeshService.swift in Sources */,
|
||||
04F127F22E37EEB800FFBA8D /* NostrProtocol.swift in Sources */,
|
||||
04F127F32E37EEB800FFBA8D /* NostrRelayManager.swift in Sources */,
|
||||
7576A357B278E5733E9D9F33 /* ChatViewModel.swift in Sources */,
|
||||
7DCA0DBCB8884E3B31C7BCE3 /* CompressionUtil.swift in Sources */,
|
||||
1D9674FA5F998503831DC281 /* ContentView.swift in Sources */,
|
||||
04B6BA4E2E2038A70090FE39 /* NoiseEncryptionService.swift in Sources */,
|
||||
CD0AE423F03AC52BAFC16834 /* DeliveryTracker.swift in Sources */,
|
||||
8F737CE0435792CC2AD65FCB /* KeychainManager.swift in Sources */,
|
||||
04F127E42E37EBAA00FFBA8D /* NostrIdentity.swift in Sources */,
|
||||
04636BBC2E2FAA1700FBCFA8 /* BinaryEncodingUtils.swift in Sources */,
|
||||
0FBC81FF78CF4711B78E092A /* IdentityModels.swift in Sources */,
|
||||
1AF9F9036DEE42408D557A87 /* SecureIdentityStateManager.swift in Sources */,
|
||||
7A5B1AB5642FEC168E917949 /* LinkPreviewView.swift in Sources */,
|
||||
04F127FF2E37EF3D00FFBA8D /* BitchatPeer.swift in Sources */,
|
||||
04F128082E37F10000FFBA8D /* PeerSession.swift in Sources */,
|
||||
04B6BA552E203D6C0090FE39 /* FingerprintView.swift in Sources */,
|
||||
04636BEE2E30CD4A00FBCFA8 /* NoiseHandshakeCoordinator.swift in Sources */,
|
||||
CEAE115C9C3EB3C4ED82F128 /* MessageRetryService.swift in Sources */,
|
||||
61C81ED5F679D5E973EE0C07 /* NotificationService.swift in Sources */,
|
||||
04636BC02E2FCA8A00FBCFA8 /* SecureLogger.swift in Sources */,
|
||||
1F48A8CEEE9399D1EBD08F0C /* OptimizedBloomFilter.swift in Sources */,
|
||||
04F127FA2E37EEEE00FFBA8D /* MessageRouter.swift in Sources */,
|
||||
04F127FB2E37EEEE00FFBA8D /* FavoritesPersistenceService.swift in Sources */,
|
||||
04F128052E37F00000FFBA8D /* ProcessedMessagesService.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
@@ -744,7 +850,7 @@
|
||||
"@executable_path/Frameworks",
|
||||
"@executable_path/../../Frameworks",
|
||||
);
|
||||
MARKETING_VERSION = 1.1.1;
|
||||
MARKETING_VERSION = 1.2.0;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = chat.bitchat.ShareExtension;
|
||||
SDKROOT = iphoneos;
|
||||
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
|
||||
@@ -776,7 +882,7 @@
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
);
|
||||
MARKETING_VERSION = 1.1.1;
|
||||
MARKETING_VERSION = 1.2.0;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = chat.bitchat;
|
||||
PRODUCT_NAME = bitchat;
|
||||
SDKROOT = iphoneos;
|
||||
@@ -832,7 +938,7 @@
|
||||
"$(inherited)",
|
||||
"@executable_path/Frameworks",
|
||||
);
|
||||
MARKETING_VERSION = 1.1.1;
|
||||
MARKETING_VERSION = 1.2.0;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = chat.bitchat;
|
||||
PRODUCT_NAME = bitchat;
|
||||
SDKROOT = iphoneos;
|
||||
@@ -858,11 +964,14 @@
|
||||
DEAD_CODE_STRIPPING = YES;
|
||||
ENABLE_PREVIEWS = YES;
|
||||
INFOPLIST_FILE = bitchat/Info.plist;
|
||||
INFOPLIST_KEY_CFBundleDisplayName = bitchat;
|
||||
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking";
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/../Frameworks",
|
||||
);
|
||||
MACOSX_DEPLOYMENT_TARGET = 13.0;
|
||||
MARKETING_VERSION = 1.2.0;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = chat.bitchat;
|
||||
PRODUCT_NAME = bitchat;
|
||||
REGISTER_APP_GROUPS = YES;
|
||||
@@ -945,11 +1054,14 @@
|
||||
DEAD_CODE_STRIPPING = YES;
|
||||
ENABLE_PREVIEWS = YES;
|
||||
INFOPLIST_FILE = bitchat/Info.plist;
|
||||
INFOPLIST_KEY_CFBundleDisplayName = bitchat;
|
||||
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking";
|
||||
LD_RUNPATH_SEARCH_PATHS = (
|
||||
"$(inherited)",
|
||||
"@executable_path/../Frameworks",
|
||||
);
|
||||
MACOSX_DEPLOYMENT_TARGET = 13.0;
|
||||
MARKETING_VERSION = 1.2.0;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = chat.bitchat;
|
||||
PRODUCT_NAME = bitchat;
|
||||
REGISTER_APP_GROUPS = YES;
|
||||
@@ -1042,7 +1154,7 @@
|
||||
"@executable_path/Frameworks",
|
||||
"@executable_path/../../Frameworks",
|
||||
);
|
||||
MARKETING_VERSION = 1.1.1;
|
||||
MARKETING_VERSION = 1.2.0;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = chat.bitchat.ShareExtension;
|
||||
SDKROOT = iphoneos;
|
||||
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator";
|
||||
@@ -1112,6 +1224,40 @@
|
||||
defaultConfigurationName = Debug;
|
||||
};
|
||||
/* End XCConfigurationList section */
|
||||
|
||||
/* Begin XCRemoteSwiftPackageReference section */
|
||||
04F127E62E37EBCD00FFBA8D /* XCRemoteSwiftPackageReference "swift-secp256k1" */ = {
|
||||
isa = XCRemoteSwiftPackageReference;
|
||||
repositoryURL = "https://github.com/21-DOT-DEV/swift-secp256k1";
|
||||
requirement = {
|
||||
kind = upToNextMajorVersion;
|
||||
minimumVersion = 0.21.1;
|
||||
};
|
||||
};
|
||||
/* End XCRemoteSwiftPackageReference section */
|
||||
|
||||
/* Begin XCSwiftPackageProductDependency section */
|
||||
04F127E72E37EBCD00FFBA8D /* P256K */ = {
|
||||
isa = XCSwiftPackageProductDependency;
|
||||
package = 04F127E62E37EBCD00FFBA8D /* XCRemoteSwiftPackageReference "swift-secp256k1" */;
|
||||
productName = P256K;
|
||||
};
|
||||
04F127E82E37EBDB00FFBA8D /* P256K */ = {
|
||||
isa = XCSwiftPackageProductDependency;
|
||||
package = 04F127E62E37EBCD00FFBA8D /* XCRemoteSwiftPackageReference "swift-secp256k1" */;
|
||||
productName = P256K;
|
||||
};
|
||||
04F128002E37FF4E00FFBA8D /* libsecp256k1 */ = {
|
||||
isa = XCSwiftPackageProductDependency;
|
||||
package = 04F127E62E37EBCD00FFBA8D /* XCRemoteSwiftPackageReference "swift-secp256k1" */;
|
||||
productName = libsecp256k1;
|
||||
};
|
||||
04F128022E37FFB900FFBA8D /* libsecp256k1 */ = {
|
||||
isa = XCSwiftPackageProductDependency;
|
||||
package = 04F127E62E37EBCD00FFBA8D /* XCRemoteSwiftPackageReference "swift-secp256k1" */;
|
||||
productName = libsecp256k1;
|
||||
};
|
||||
/* End XCSwiftPackageProductDependency section */
|
||||
};
|
||||
rootObject = 475D96681D0EA0AE57A4E06E /* Project object */;
|
||||
}
|
||||
|
||||
@@ -43,6 +43,13 @@ struct BitchatApp: App {
|
||||
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
|
||||
// Check for shared content when app becomes active
|
||||
checkForSharedContent()
|
||||
// Notify MessageRouter to check for Nostr messages
|
||||
NotificationCenter.default.post(name: .appDidBecomeActive, object: nil)
|
||||
}
|
||||
#elseif os(macOS)
|
||||
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
|
||||
// Notify MessageRouter to check for Nostr messages
|
||||
NotificationCenter.default.post(name: .appDidBecomeActive, object: nil)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -54,5 +54,18 @@
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
<string>UIInterfaceOrientationPortraitUpsideDown</string>
|
||||
</array>
|
||||
<key>NSAppTransportSecurity</key>
|
||||
<dict>
|
||||
<key>NSAllowsArbitraryLoads</key>
|
||||
<false/>
|
||||
<key>NSAllowsArbitraryLoadsForMedia</key>
|
||||
<false/>
|
||||
<key>NSAllowsArbitraryLoadsInWebContent</key>
|
||||
<false/>
|
||||
<key>NSAllowsLocalNetworking</key>
|
||||
<true/>
|
||||
</dict>
|
||||
<key>NSLocalNetworkUsageDescription</key>
|
||||
<string>bitchat uses local network access to discover and connect with other bitchat users on your network.</string>
|
||||
</dict>
|
||||
</plist>
|
||||
|
||||
@@ -0,0 +1,336 @@
|
||||
import Foundation
|
||||
import CoreBluetooth
|
||||
|
||||
/// Represents a peer in the BitChat network with all associated metadata
|
||||
struct BitchatPeer: Identifiable, Equatable {
|
||||
let id: String // Hex-encoded peer ID
|
||||
let noisePublicKey: Data
|
||||
let nickname: String
|
||||
let lastSeen: Date
|
||||
let isConnected: Bool
|
||||
|
||||
// Favorite-related properties
|
||||
var favoriteStatus: FavoritesPersistenceService.FavoriteRelationship?
|
||||
|
||||
// Nostr identity (if known)
|
||||
var nostrPublicKey: String?
|
||||
|
||||
// Connection state
|
||||
enum ConnectionState {
|
||||
case bluetoothConnected
|
||||
case relayConnected // Connected via mesh relay (another peer)
|
||||
case nostrAvailable // Mutual favorite, reachable via Nostr
|
||||
case offline // Not connected via any transport
|
||||
}
|
||||
|
||||
var connectionState: ConnectionState {
|
||||
if isConnected {
|
||||
return .bluetoothConnected
|
||||
} else if isRelayConnected {
|
||||
return .relayConnected
|
||||
} else if favoriteStatus?.isMutual == true {
|
||||
// Mutual favorites can communicate via Nostr when offline
|
||||
return .nostrAvailable
|
||||
} else {
|
||||
return .offline
|
||||
}
|
||||
}
|
||||
|
||||
var isRelayConnected: Bool = false // Set by PeerManager based on session state
|
||||
|
||||
var isFavorite: Bool {
|
||||
favoriteStatus?.isFavorite ?? false
|
||||
}
|
||||
|
||||
var isMutualFavorite: Bool {
|
||||
favoriteStatus?.isMutual ?? false
|
||||
}
|
||||
|
||||
var theyFavoritedUs: Bool {
|
||||
favoriteStatus?.theyFavoritedUs ?? false
|
||||
}
|
||||
|
||||
// Display helpers
|
||||
var displayName: String {
|
||||
nickname.isEmpty ? String(id.prefix(8)) : nickname
|
||||
}
|
||||
|
||||
var statusIcon: String {
|
||||
switch connectionState {
|
||||
case .bluetoothConnected:
|
||||
return "📻" // Radio icon for mesh connection
|
||||
case .relayConnected:
|
||||
return "🔗" // Chain link for relay connection
|
||||
case .nostrAvailable:
|
||||
return "🌐" // Purple globe for Nostr
|
||||
case .offline:
|
||||
if theyFavoritedUs && !isFavorite {
|
||||
return "🌙" // Crescent moon - they favorited us but we didn't reciprocate
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize from mesh service data
|
||||
init(
|
||||
id: String,
|
||||
noisePublicKey: Data,
|
||||
nickname: String,
|
||||
lastSeen: Date = Date(),
|
||||
isConnected: Bool = false,
|
||||
isRelayConnected: Bool = false
|
||||
) {
|
||||
self.id = id
|
||||
self.noisePublicKey = noisePublicKey
|
||||
self.nickname = nickname
|
||||
self.lastSeen = lastSeen
|
||||
self.isConnected = isConnected
|
||||
self.isRelayConnected = isRelayConnected
|
||||
|
||||
// Load favorite status - will be set later by the manager
|
||||
self.favoriteStatus = nil
|
||||
self.nostrPublicKey = nil
|
||||
}
|
||||
|
||||
static func == (lhs: BitchatPeer, rhs: BitchatPeer) -> Bool {
|
||||
lhs.id == rhs.id
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Peer Manager
|
||||
|
||||
/// Manages the collection of peers and their states
|
||||
@MainActor
|
||||
class PeerManager: ObservableObject {
|
||||
@Published var peers: [BitchatPeer] = []
|
||||
@Published var favorites: [BitchatPeer] = []
|
||||
@Published var mutualFavorites: [BitchatPeer] = []
|
||||
|
||||
private let meshService: BluetoothMeshService
|
||||
private let favoritesService = FavoritesPersistenceService.shared
|
||||
|
||||
init(meshService: BluetoothMeshService) {
|
||||
self.meshService = meshService
|
||||
updatePeers()
|
||||
|
||||
// Listen for updates
|
||||
NotificationCenter.default.addObserver(
|
||||
self,
|
||||
selector: #selector(handleFavoriteChanged),
|
||||
name: .favoriteStatusChanged,
|
||||
object: nil
|
||||
)
|
||||
}
|
||||
|
||||
@objc private func handleFavoriteChanged() {
|
||||
SecureLogger.log("⭐ Favorite status changed notification received, updating peers",
|
||||
category: SecureLogger.session, level: .debug)
|
||||
updatePeers()
|
||||
}
|
||||
|
||||
deinit {
|
||||
NotificationCenter.default.removeObserver(self)
|
||||
}
|
||||
|
||||
func updatePeers() {
|
||||
// Reduce log verbosity - only log when count changes
|
||||
let previousCount = peers.count
|
||||
|
||||
// Get current mesh peers
|
||||
let meshPeers = meshService.getPeerNicknames()
|
||||
|
||||
// Build peer list
|
||||
var allPeers: [BitchatPeer] = []
|
||||
var connectedNicknames: Set<String> = []
|
||||
|
||||
// Add connected mesh peers (only if actually connected or relay connected)
|
||||
for (peerID, nickname) in meshPeers {
|
||||
guard let noiseKey = Data(hexString: peerID) else { continue }
|
||||
|
||||
// Safety check: Never add our own peer ID
|
||||
if peerID == meshService.myPeerID {
|
||||
SecureLogger.log("⚠️ Skipping self peer ID \(peerID) in peer list",
|
||||
category: SecureLogger.session, level: .warning)
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if this peer is actually connected (not just known via relay)
|
||||
let isConnected = meshService.isPeerConnected(peerID)
|
||||
let isKnown = meshService.isPeerKnown(peerID)
|
||||
// In a mesh network, a peer can only be relay-connected if:
|
||||
// 1. We know about them (have received announce)
|
||||
// 2. We're not directly connected
|
||||
// 3. There are other peers that could relay (mesh peer count > 2)
|
||||
// For now, disable relay detection until we have proper relay tracking
|
||||
let isRelayConnected = false
|
||||
|
||||
// Debug logging for relay connection detection
|
||||
if isKnown && !isConnected {
|
||||
SecureLogger.log("Peer \(nickname) (\(peerID)): isConnected=\(isConnected), isKnown=\(isKnown), isRelayConnected=\(isRelayConnected)",
|
||||
category: SecureLogger.session, level: .debug)
|
||||
}
|
||||
|
||||
// Skip disconnected peers unless they're favorites (handled later)
|
||||
if !isConnected && !isRelayConnected {
|
||||
continue
|
||||
}
|
||||
|
||||
if isConnected || isRelayConnected {
|
||||
connectedNicknames.insert(nickname)
|
||||
}
|
||||
|
||||
var peer = BitchatPeer(
|
||||
id: peerID,
|
||||
noisePublicKey: noiseKey,
|
||||
nickname: nickname,
|
||||
isConnected: isConnected,
|
||||
isRelayConnected: isRelayConnected
|
||||
)
|
||||
// Set favorite status - check both by current noise key and by nickname
|
||||
if let favoriteStatus = favoritesService.getFavoriteStatus(for: noiseKey) {
|
||||
peer.favoriteStatus = favoriteStatus
|
||||
peer.nostrPublicKey = favoriteStatus.peerNostrPublicKey
|
||||
} else {
|
||||
// Check if we have a favorite for this nickname (peer may have reconnected with new ID)
|
||||
let favoriteByNickname = favoritesService.favorites.values.first { $0.peerNickname == nickname }
|
||||
if let favorite = favoriteByNickname {
|
||||
SecureLogger.log("🔄 Found favorite for '\(nickname)' by nickname, updating noise key",
|
||||
category: SecureLogger.session, level: .info)
|
||||
// Update the favorite's noise key to match the current connection
|
||||
favoritesService.updateNoisePublicKey(from: favorite.peerNoisePublicKey, to: noiseKey, peerNickname: nickname)
|
||||
// Get the updated favorite with the new key
|
||||
peer.favoriteStatus = favoritesService.getFavoriteStatus(for: noiseKey)
|
||||
peer.nostrPublicKey = peer.favoriteStatus?.peerNostrPublicKey ?? favorite.peerNostrPublicKey
|
||||
}
|
||||
}
|
||||
allPeers.append(peer)
|
||||
}
|
||||
|
||||
// Add offline favorites (only those not currently connected/relay-connected AND that we actively favorite)
|
||||
SecureLogger.log("📋 Processing \(favoritesService.favorites.count) favorite relationships (connected/relay nicknames: \(connectedNicknames))",
|
||||
category: SecureLogger.session, level: .info)
|
||||
|
||||
for (favoriteKey, favorite) in favoritesService.favorites {
|
||||
let favoriteID = favorite.peerNoisePublicKey.hexEncodedString()
|
||||
|
||||
// Skip if this peer is already connected or relay-connected (by nickname)
|
||||
if connectedNicknames.contains(favorite.peerNickname) {
|
||||
SecureLogger.log(" - Skipping '\(favorite.peerNickname)' (key: \(favoriteKey.hexEncodedString())) - already connected/relay-connected",
|
||||
category: SecureLogger.session, level: .debug)
|
||||
continue
|
||||
}
|
||||
|
||||
// Only add peers that WE favorite (not just ones who favorite us)
|
||||
if !favorite.isFavorite {
|
||||
SecureLogger.log(" - Skipping '\(favorite.peerNickname)' - we don't favorite them (they favorite us: \(favorite.theyFavoritedUs))",
|
||||
category: SecureLogger.session, level: .debug)
|
||||
continue
|
||||
}
|
||||
|
||||
// Add this favorite as an offline peer
|
||||
SecureLogger.log(" - Adding offline favorite '\(favorite.peerNickname)' (key: \(favoriteKey.hexEncodedString()), ID: \(favoriteID), mutual: \(favorite.isMutual))",
|
||||
category: SecureLogger.session, level: .info)
|
||||
|
||||
var peer = BitchatPeer(
|
||||
id: favoriteID,
|
||||
noisePublicKey: favorite.peerNoisePublicKey,
|
||||
nickname: favorite.peerNickname,
|
||||
isConnected: false
|
||||
)
|
||||
// Set favorite status
|
||||
peer.favoriteStatus = favorite
|
||||
peer.nostrPublicKey = favorite.peerNostrPublicKey
|
||||
allPeers.append(peer)
|
||||
}
|
||||
|
||||
// Filter out "Unknown" peers unless they are favorites or have a favorite relationship
|
||||
allPeers = allPeers.filter { peer in
|
||||
!(peer.displayName == "Unknown" && peer.favoriteStatus == nil)
|
||||
}
|
||||
|
||||
// Sort: Connected first (direct then relay), then favorites, then alphabetical
|
||||
allPeers.sort { lhs, rhs in
|
||||
// Direct connections first
|
||||
if lhs.isConnected != rhs.isConnected {
|
||||
return lhs.isConnected
|
||||
}
|
||||
// Then relay connections
|
||||
if lhs.isRelayConnected != rhs.isRelayConnected {
|
||||
return lhs.isRelayConnected
|
||||
}
|
||||
// Then favorites
|
||||
if lhs.isFavorite != rhs.isFavorite {
|
||||
return lhs.isFavorite
|
||||
}
|
||||
// Finally alphabetical
|
||||
return lhs.displayName < rhs.displayName
|
||||
}
|
||||
|
||||
// Single pass to compute all subsets and counts
|
||||
var favorites: [BitchatPeer] = []
|
||||
var mutualFavorites: [BitchatPeer] = []
|
||||
var connectedCount = 0
|
||||
var offlineCount = 0
|
||||
|
||||
for peer in allPeers {
|
||||
if peer.isFavorite {
|
||||
favorites.append(peer)
|
||||
}
|
||||
if peer.isMutualFavorite {
|
||||
mutualFavorites.append(peer)
|
||||
}
|
||||
if peer.isConnected {
|
||||
connectedCount += 1
|
||||
} else {
|
||||
offlineCount += 1
|
||||
}
|
||||
}
|
||||
|
||||
self.peers = allPeers
|
||||
self.favorites = favorites
|
||||
self.mutualFavorites = mutualFavorites
|
||||
|
||||
// Always log favorites debug info when there are favorites
|
||||
if favoritesService.favorites.count > 0 {
|
||||
SecureLogger.log("📊 Peer list update: \(allPeers.count) total (\(connectedCount) connected, \(offlineCount) offline), \(favorites.count) favorites, \(mutualFavorites.count) mutual",
|
||||
category: SecureLogger.session, level: .info)
|
||||
|
||||
// Log each peer's status
|
||||
for peer in allPeers {
|
||||
// Use the actual statusIcon from the peer which accounts for relay connections
|
||||
let statusIcon: String
|
||||
switch peer.connectionState {
|
||||
case .bluetoothConnected:
|
||||
statusIcon = "🟢"
|
||||
case .relayConnected:
|
||||
statusIcon = "🔗"
|
||||
case .nostrAvailable:
|
||||
statusIcon = "🌐"
|
||||
case .offline:
|
||||
statusIcon = "🔴"
|
||||
}
|
||||
let favoriteIcon = peer.isMutualFavorite ? "💕" : (peer.isFavorite ? "⭐" : (peer.theyFavoritedUs ? "🌙" : ""))
|
||||
SecureLogger.log(" \(statusIcon) \(peer.displayName) (ID: \(peer.id.prefix(8))...) \(favoriteIcon)",
|
||||
category: SecureLogger.session, level: .debug)
|
||||
}
|
||||
} else if previousCount != allPeers.count {
|
||||
// Only log non-favorite updates if count changed
|
||||
SecureLogger.log("✅ Updated peer list: \(allPeers.count) total peers",
|
||||
category: SecureLogger.session, level: .info)
|
||||
}
|
||||
}
|
||||
|
||||
func toggleFavorite(_ peer: BitchatPeer) {
|
||||
if peer.isFavorite {
|
||||
favoritesService.removeFavorite(peerNoisePublicKey: peer.noisePublicKey)
|
||||
} else {
|
||||
favoritesService.addFavorite(
|
||||
peerNoisePublicKey: peer.noisePublicKey,
|
||||
peerNostrPublicKey: peer.nostrPublicKey,
|
||||
peerNickname: peer.nickname
|
||||
)
|
||||
}
|
||||
updatePeers()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import Foundation
|
||||
import CoreBluetooth
|
||||
|
||||
/// Unified model for tracking all peer session data
|
||||
/// Consolidates multiple redundant data structures into a single source of truth
|
||||
class PeerSession {
|
||||
// Core identification
|
||||
let peerID: String
|
||||
var nickname: String
|
||||
|
||||
// Bluetooth connection
|
||||
var peripheral: CBPeripheral?
|
||||
var peripheralID: String?
|
||||
var characteristic: CBCharacteristic?
|
||||
|
||||
// Authentication and encryption
|
||||
var isAuthenticated: Bool = false
|
||||
var hasEstablishedNoiseSession: Bool = false
|
||||
var fingerprint: String?
|
||||
|
||||
// Connection state
|
||||
var isConnected: Bool = false
|
||||
var lastSeen: Date
|
||||
|
||||
// Protocol state
|
||||
var hasAnnounced: Bool = false
|
||||
var hasReceivedAnnounce: Bool = false
|
||||
var isActivePeer: Bool = false
|
||||
|
||||
// Message tracking
|
||||
var lastMessageSent: Date?
|
||||
var lastMessageReceived: Date?
|
||||
var pendingMessages: [String] = []
|
||||
|
||||
// Connection timing
|
||||
var lastConnectionTime: Date?
|
||||
var lastSuccessfulMessageTime: Date?
|
||||
var lastHeardFromPeer: Date?
|
||||
|
||||
// Availability tracking
|
||||
var isAvailable: Bool = false
|
||||
|
||||
// Identity binding
|
||||
var identityBinding: PeerIdentityBinding?
|
||||
|
||||
init(peerID: String, nickname: String = "Unknown") {
|
||||
self.peerID = peerID
|
||||
self.nickname = nickname
|
||||
self.lastSeen = Date()
|
||||
}
|
||||
|
||||
/// Update Bluetooth connection info
|
||||
func updateBluetoothConnection(peripheral: CBPeripheral?, characteristic: CBCharacteristic?) {
|
||||
self.peripheral = peripheral
|
||||
self.peripheralID = peripheral?.identifier.uuidString
|
||||
self.characteristic = characteristic
|
||||
self.isConnected = (peripheral?.state == .connected)
|
||||
if isConnected {
|
||||
self.lastSeen = Date()
|
||||
}
|
||||
}
|
||||
|
||||
/// Update authentication state
|
||||
func updateAuthenticationState(authenticated: Bool, noiseSession: Bool) {
|
||||
self.isAuthenticated = authenticated
|
||||
self.hasEstablishedNoiseSession = noiseSession
|
||||
if authenticated {
|
||||
self.isActivePeer = true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Check if session is stale
|
||||
var isStale: Bool {
|
||||
// Consider stale if not seen for more than 5 minutes and not connected
|
||||
return !isConnected && Date().timeIntervalSince(lastSeen) > 300
|
||||
}
|
||||
|
||||
/// Get display status for UI
|
||||
var displayStatus: String {
|
||||
if isConnected {
|
||||
if isAuthenticated {
|
||||
return "🟢" // Connected and authenticated
|
||||
} else {
|
||||
return "🟡" // Connected but not authenticated
|
||||
}
|
||||
} else {
|
||||
return "🔴" // Not connected
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,8 @@ class NoiseHandshakeCoordinator {
|
||||
private let handshakeTimeout: TimeInterval = 10.0
|
||||
private let retryDelay: TimeInterval = 2.0
|
||||
private let minTimeBetweenHandshakes: TimeInterval = 1.0 // Reduced from 5.0 for faster recovery
|
||||
private let establishedSessionTTL: TimeInterval = 300.0 // 5 minutes - sessions older than this can be cleaned up
|
||||
private let maxEstablishedSessions = 50 // Limit total established sessions
|
||||
|
||||
// Track handshake messages to detect duplicates
|
||||
private var processedHandshakeMessages: Set<Data> = []
|
||||
@@ -220,11 +222,12 @@ class NoiseHandshakeCoordinator {
|
||||
}
|
||||
}
|
||||
|
||||
/// Clean up stale handshake states
|
||||
/// Clean up stale handshake states and old established sessions
|
||||
func cleanupStaleHandshakes(staleTimeout: TimeInterval = 30.0) -> [String] {
|
||||
return handshakeQueue.sync {
|
||||
let now = Date()
|
||||
var stalePeerIDs: [String] = []
|
||||
var establishedSessions: [(peerID: String, since: Date)] = []
|
||||
|
||||
for (peerID, state) in handshakeStates {
|
||||
var isStale = false
|
||||
@@ -242,6 +245,13 @@ class NoiseHandshakeCoordinator {
|
||||
if now > timeout {
|
||||
isStale = true
|
||||
}
|
||||
case .established(let since):
|
||||
// Track established sessions for potential cleanup
|
||||
establishedSessions.append((peerID, since))
|
||||
// Clean up very old established sessions
|
||||
if now.timeIntervalSince(since) > establishedSessionTTL {
|
||||
isStale = true
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
@@ -253,11 +263,30 @@ class NoiseHandshakeCoordinator {
|
||||
}
|
||||
}
|
||||
|
||||
// If we have too many established sessions, clean up the oldest ones
|
||||
if establishedSessions.count > maxEstablishedSessions {
|
||||
// Sort by age (oldest first)
|
||||
let sortedSessions = establishedSessions.sorted { $0.since < $1.since }
|
||||
let sessionsToRemove = sortedSessions.count - maxEstablishedSessions
|
||||
|
||||
for i in 0..<sessionsToRemove {
|
||||
let peerID = sortedSessions[i].peerID
|
||||
stalePeerIDs.append(peerID)
|
||||
SecureLogger.log("Removing old established session for \(peerID) to maintain session limit",
|
||||
category: SecureLogger.handshake, level: .info)
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up stale states
|
||||
for peerID in stalePeerIDs {
|
||||
handshakeStates.removeValue(forKey: peerID)
|
||||
}
|
||||
|
||||
if !stalePeerIDs.isEmpty {
|
||||
SecureLogger.log("Cleaned up \(stalePeerIDs.count) stale handshake states",
|
||||
category: SecureLogger.handshake, level: .info)
|
||||
}
|
||||
|
||||
return stalePeerIDs
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,265 @@
|
||||
import Foundation
|
||||
import CryptoKit
|
||||
import P256K
|
||||
|
||||
// Keychain helper for secure storage
|
||||
struct KeychainHelper {
|
||||
static func save(key: String, data: Data, service: String) {
|
||||
let query: [String: Any] = [
|
||||
kSecClass as String: kSecClassGenericPassword,
|
||||
kSecAttrService as String: service,
|
||||
kSecAttrAccount as String: key,
|
||||
kSecValueData as String: data
|
||||
]
|
||||
|
||||
SecItemDelete(query as CFDictionary)
|
||||
SecItemAdd(query as CFDictionary, nil)
|
||||
}
|
||||
|
||||
static func load(key: String, service: String) -> Data? {
|
||||
let query: [String: Any] = [
|
||||
kSecClass as String: kSecClassGenericPassword,
|
||||
kSecAttrService as String: service,
|
||||
kSecAttrAccount as String: key,
|
||||
kSecReturnData as String: true
|
||||
]
|
||||
|
||||
var result: AnyObject?
|
||||
let status = SecItemCopyMatching(query as CFDictionary, &result)
|
||||
|
||||
guard status == errSecSuccess else { return nil }
|
||||
return result as? Data
|
||||
}
|
||||
|
||||
static func delete(key: String, service: String) {
|
||||
let query: [String: Any] = [
|
||||
kSecClass as String: kSecClassGenericPassword,
|
||||
kSecAttrService as String: service,
|
||||
kSecAttrAccount as String: key
|
||||
]
|
||||
|
||||
SecItemDelete(query as CFDictionary)
|
||||
}
|
||||
}
|
||||
|
||||
/// Manages Nostr identity (secp256k1 keypair) for NIP-17 private messaging
|
||||
struct NostrIdentity: Codable {
|
||||
let privateKey: Data
|
||||
let publicKey: Data
|
||||
let npub: String // Bech32-encoded public key
|
||||
let createdAt: Date
|
||||
|
||||
/// Memberwise initializer
|
||||
init(privateKey: Data, publicKey: Data, npub: String, createdAt: Date) {
|
||||
self.privateKey = privateKey
|
||||
self.publicKey = publicKey
|
||||
self.npub = npub
|
||||
self.createdAt = createdAt
|
||||
}
|
||||
|
||||
/// Generate a new Nostr identity
|
||||
static func generate() throws -> NostrIdentity {
|
||||
// Generate Schnorr key for Nostr
|
||||
let schnorrKey = try P256K.Schnorr.PrivateKey()
|
||||
let xOnlyPubkey = Data(schnorrKey.xonly.bytes)
|
||||
let npub = try Bech32.encode(hrp: "npub", data: xOnlyPubkey)
|
||||
|
||||
return NostrIdentity(
|
||||
privateKey: schnorrKey.dataRepresentation,
|
||||
publicKey: xOnlyPubkey, // Store x-only public key
|
||||
npub: npub,
|
||||
createdAt: Date()
|
||||
)
|
||||
}
|
||||
|
||||
/// Initialize from existing private key data
|
||||
init(privateKeyData: Data) throws {
|
||||
let schnorrKey = try P256K.Schnorr.PrivateKey(dataRepresentation: privateKeyData)
|
||||
let xOnlyPubkey = Data(schnorrKey.xonly.bytes)
|
||||
|
||||
self.privateKey = privateKeyData
|
||||
self.publicKey = xOnlyPubkey
|
||||
self.npub = try Bech32.encode(hrp: "npub", data: xOnlyPubkey)
|
||||
self.createdAt = Date()
|
||||
}
|
||||
|
||||
/// Get signing key for event signatures
|
||||
func signingKey() throws -> P256K.Signing.PrivateKey {
|
||||
try P256K.Signing.PrivateKey(dataRepresentation: privateKey)
|
||||
}
|
||||
|
||||
/// Get Schnorr signing key for Nostr event signatures
|
||||
func schnorrSigningKey() throws -> P256K.Schnorr.PrivateKey {
|
||||
try P256K.Schnorr.PrivateKey(dataRepresentation: privateKey)
|
||||
}
|
||||
|
||||
/// Get hex-encoded public key (for Nostr events)
|
||||
var publicKeyHex: String {
|
||||
// Public key is already stored as x-only (32 bytes)
|
||||
return publicKey.hexEncodedString()
|
||||
}
|
||||
}
|
||||
|
||||
/// Bridge between Noise and Nostr identities
|
||||
struct NostrIdentityBridge {
|
||||
private static let keychainService = "chat.bitchat.nostr"
|
||||
private static let currentIdentityKey = "nostr-current-identity"
|
||||
|
||||
/// Get or create the current Nostr identity
|
||||
static func getCurrentNostrIdentity() throws -> NostrIdentity? {
|
||||
// Check if we already have a Nostr identity
|
||||
if let existingData = KeychainHelper.load(key: currentIdentityKey, service: keychainService),
|
||||
let identity = try? JSONDecoder().decode(NostrIdentity.self, from: existingData) {
|
||||
return identity
|
||||
}
|
||||
|
||||
// Generate new Nostr identity
|
||||
let nostrIdentity = try NostrIdentity.generate()
|
||||
|
||||
// Store it
|
||||
let data = try JSONEncoder().encode(nostrIdentity)
|
||||
KeychainHelper.save(key: currentIdentityKey, data: data, service: keychainService)
|
||||
|
||||
return nostrIdentity
|
||||
}
|
||||
|
||||
/// Associate a Nostr identity with a Noise public key (for favorites)
|
||||
static func associateNostrIdentity(_ nostrPubkey: String, with noisePublicKey: Data) {
|
||||
let key = "nostr-noise-\(noisePublicKey.base64EncodedString())"
|
||||
if let data = nostrPubkey.data(using: .utf8) {
|
||||
KeychainHelper.save(key: key, data: data, service: keychainService)
|
||||
}
|
||||
}
|
||||
|
||||
/// Get Nostr public key associated with a Noise public key
|
||||
static func getNostrPublicKey(for noisePublicKey: Data) -> String? {
|
||||
let key = "nostr-noise-\(noisePublicKey.base64EncodedString())"
|
||||
guard let data = KeychainHelper.load(key: key, service: keychainService),
|
||||
let pubkey = String(data: data, encoding: .utf8) else {
|
||||
return nil
|
||||
}
|
||||
return pubkey
|
||||
}
|
||||
}
|
||||
|
||||
// Bech32 encoding for Nostr (minimal implementation)
|
||||
enum Bech32 {
|
||||
private static let charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
|
||||
private static let generator = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3]
|
||||
|
||||
static func encode(hrp: String, data: Data) throws -> String {
|
||||
let values = convertBits(from: 8, to: 5, pad: true, data: Array(data))
|
||||
let checksum = createChecksum(hrp: hrp, values: values)
|
||||
let combined = values + checksum
|
||||
|
||||
return hrp + "1" + combined.map {
|
||||
let index = charset.index(charset.startIndex, offsetBy: Int($0))
|
||||
return String(charset[index])
|
||||
}.joined()
|
||||
}
|
||||
|
||||
static func decode(_ bech32String: String) throws -> (hrp: String, data: Data) {
|
||||
// Find the last occurrence of '1'
|
||||
guard let separatorIndex = bech32String.lastIndex(of: "1") else {
|
||||
throw Bech32Error.invalidFormat
|
||||
}
|
||||
|
||||
let hrp = String(bech32String[..<separatorIndex])
|
||||
let dataString = String(bech32String[bech32String.index(after: separatorIndex)...])
|
||||
|
||||
// Convert characters to values
|
||||
var values = [UInt8]()
|
||||
for char in dataString {
|
||||
guard let index = charset.firstIndex(of: char) else {
|
||||
throw Bech32Error.invalidCharacter
|
||||
}
|
||||
values.append(UInt8(charset.distance(from: charset.startIndex, to: index)))
|
||||
}
|
||||
|
||||
// Verify checksum
|
||||
guard values.count >= 6 else {
|
||||
throw Bech32Error.invalidChecksum
|
||||
}
|
||||
|
||||
let payloadValues = Array(values.dropLast(6))
|
||||
let checksum = Array(values.suffix(6))
|
||||
let expectedChecksum = createChecksum(hrp: hrp, values: payloadValues)
|
||||
|
||||
guard checksum == expectedChecksum else {
|
||||
throw Bech32Error.invalidChecksum
|
||||
}
|
||||
|
||||
// Convert back to bytes
|
||||
let bytes = convertBits(from: 5, to: 8, pad: false, data: payloadValues)
|
||||
return (hrp: hrp, data: Data(bytes))
|
||||
}
|
||||
|
||||
enum Bech32Error: Error {
|
||||
case invalidFormat
|
||||
case invalidCharacter
|
||||
case invalidChecksum
|
||||
}
|
||||
|
||||
private static func convertBits(from: Int, to: Int, pad: Bool, data: [UInt8]) -> [UInt8] {
|
||||
var acc = 0
|
||||
var bits = 0
|
||||
var result = [UInt8]()
|
||||
let maxv = (1 << to) - 1
|
||||
|
||||
for value in data {
|
||||
acc = (acc << from) | Int(value)
|
||||
bits += from
|
||||
|
||||
while bits >= to {
|
||||
bits -= to
|
||||
result.append(UInt8((acc >> bits) & maxv))
|
||||
}
|
||||
}
|
||||
|
||||
if pad && bits > 0 {
|
||||
result.append(UInt8((acc << (to - bits)) & maxv))
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
private static func createChecksum(hrp: String, values: [UInt8]) -> [UInt8] {
|
||||
let checksumValues = hrpExpand(hrp) + values + [0, 0, 0, 0, 0, 0]
|
||||
let polymod = polymod(checksumValues) ^ 1
|
||||
var checksum = [UInt8]()
|
||||
|
||||
for i in 0..<6 {
|
||||
checksum.append(UInt8((polymod >> (5 * (5 - i))) & 31))
|
||||
}
|
||||
|
||||
return checksum
|
||||
}
|
||||
|
||||
private static func hrpExpand(_ hrp: String) -> [UInt8] {
|
||||
var result = [UInt8]()
|
||||
for c in hrp {
|
||||
result.append(UInt8(c.asciiValue! >> 5))
|
||||
}
|
||||
result.append(0)
|
||||
for c in hrp {
|
||||
result.append(UInt8(c.asciiValue! & 31))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private static func polymod(_ values: [UInt8]) -> Int {
|
||||
var chk = 1
|
||||
for value in values {
|
||||
let b = chk >> 25
|
||||
chk = (chk & 0x1ffffff) << 5 ^ Int(value)
|
||||
for i in 0..<5 {
|
||||
if (b >> i) & 1 == 1 {
|
||||
chk ^= generator[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return chk
|
||||
}
|
||||
}
|
||||
|
||||
// Data hex encoding extension moved to BinaryEncodingUtils.swift to avoid duplication
|
||||
@@ -0,0 +1,560 @@
|
||||
import Foundation
|
||||
import CryptoKit
|
||||
import P256K
|
||||
|
||||
// Note: This file depends on Data extension from BinaryEncodingUtils.swift
|
||||
// Make sure BinaryEncodingUtils.swift is included in the target
|
||||
|
||||
/// NIP-17 Protocol Implementation for Private Direct Messages
|
||||
struct NostrProtocol {
|
||||
|
||||
/// Nostr event kinds
|
||||
enum EventKind: Int {
|
||||
case metadata = 0
|
||||
case textNote = 1
|
||||
case seal = 13 // NIP-17 sealed event
|
||||
case giftWrap = 1059 // NIP-17 gift wrap
|
||||
case ephemeralEvent = 20000
|
||||
}
|
||||
|
||||
/// Create a NIP-17 private message
|
||||
static func createPrivateMessage(
|
||||
content: String,
|
||||
recipientPubkey: String,
|
||||
senderIdentity: NostrIdentity
|
||||
) throws -> NostrEvent {
|
||||
|
||||
// Creating private message
|
||||
|
||||
// 1. Create the rumor (unsigned event)
|
||||
let rumor = NostrEvent(
|
||||
pubkey: senderIdentity.publicKeyHex,
|
||||
createdAt: Date(),
|
||||
kind: .textNote,
|
||||
tags: [],
|
||||
content: content
|
||||
)
|
||||
|
||||
// 2. Create ephemeral key for this message
|
||||
let ephemeralKey = try P256K.Schnorr.PrivateKey()
|
||||
let ephemeralPubkey = Data(ephemeralKey.xonly.bytes).hexEncodedString()
|
||||
// Created ephemeral key for seal
|
||||
|
||||
// 3. Seal the rumor (encrypt to recipient)
|
||||
let sealedEvent = try createSeal(
|
||||
rumor: rumor,
|
||||
recipientPubkey: recipientPubkey,
|
||||
senderKey: ephemeralKey
|
||||
)
|
||||
|
||||
// 4. Gift wrap the sealed event (encrypt to recipient again)
|
||||
let giftWrap = try createGiftWrap(
|
||||
seal: sealedEvent,
|
||||
recipientPubkey: recipientPubkey,
|
||||
senderKey: ephemeralKey
|
||||
)
|
||||
|
||||
// Created gift wrap
|
||||
|
||||
return giftWrap
|
||||
}
|
||||
|
||||
/// Decrypt a received NIP-17 message
|
||||
static func decryptPrivateMessage(
|
||||
giftWrap: NostrEvent,
|
||||
recipientIdentity: NostrIdentity
|
||||
) throws -> (content: String, senderPubkey: String) {
|
||||
|
||||
// Starting decryption
|
||||
|
||||
// 1. Unwrap the gift wrap
|
||||
let seal: NostrEvent
|
||||
do {
|
||||
seal = try unwrapGiftWrap(
|
||||
giftWrap: giftWrap,
|
||||
recipientKey: recipientIdentity.schnorrSigningKey()
|
||||
)
|
||||
// Successfully unwrapped gift wrap
|
||||
} catch {
|
||||
SecureLogger.log("❌ Failed to unwrap gift wrap: \(error)",
|
||||
category: SecureLogger.session, level: .error)
|
||||
throw error
|
||||
}
|
||||
|
||||
// 2. Open the seal
|
||||
let rumor: NostrEvent
|
||||
do {
|
||||
rumor = try openSeal(
|
||||
seal: seal,
|
||||
recipientKey: recipientIdentity.schnorrSigningKey()
|
||||
)
|
||||
// Successfully opened seal
|
||||
} catch {
|
||||
SecureLogger.log("❌ Failed to open seal: \(error)",
|
||||
category: SecureLogger.session, level: .error)
|
||||
throw error
|
||||
}
|
||||
|
||||
return (content: rumor.content, senderPubkey: rumor.pubkey)
|
||||
}
|
||||
|
||||
// MARK: - Private Methods
|
||||
|
||||
private static func createSeal(
|
||||
rumor: NostrEvent,
|
||||
recipientPubkey: String,
|
||||
senderKey: P256K.Schnorr.PrivateKey
|
||||
) throws -> NostrEvent {
|
||||
|
||||
let rumorJSON = try rumor.jsonString()
|
||||
let encrypted = try encrypt(
|
||||
plaintext: rumorJSON,
|
||||
recipientPubkey: recipientPubkey,
|
||||
senderKey: senderKey
|
||||
)
|
||||
|
||||
let seal = NostrEvent(
|
||||
pubkey: Data(senderKey.xonly.bytes).hexEncodedString(),
|
||||
createdAt: randomizedTimestamp(),
|
||||
kind: .seal,
|
||||
tags: [],
|
||||
content: encrypted
|
||||
)
|
||||
|
||||
// Convert to P256K.Signing.PrivateKey for signing (temporary until we update sign method)
|
||||
let signingKey = try P256K.Signing.PrivateKey(dataRepresentation: senderKey.dataRepresentation)
|
||||
return try seal.sign(with: signingKey)
|
||||
}
|
||||
|
||||
private static func createGiftWrap(
|
||||
seal: NostrEvent,
|
||||
recipientPubkey: String,
|
||||
senderKey: P256K.Schnorr.PrivateKey // This is the ephemeral key used for the seal
|
||||
) throws -> NostrEvent {
|
||||
|
||||
let sealJSON = try seal.jsonString()
|
||||
|
||||
// Create new ephemeral key for gift wrap
|
||||
let wrapKey = try P256K.Schnorr.PrivateKey()
|
||||
// Creating gift wrap with ephemeral key
|
||||
|
||||
// Encrypt the seal with the new ephemeral key (not the seal's key)
|
||||
let encrypted = try encrypt(
|
||||
plaintext: sealJSON,
|
||||
recipientPubkey: recipientPubkey,
|
||||
senderKey: wrapKey // Use the gift wrap ephemeral key
|
||||
)
|
||||
|
||||
let giftWrap = NostrEvent(
|
||||
pubkey: Data(wrapKey.xonly.bytes).hexEncodedString(),
|
||||
createdAt: randomizedTimestamp(),
|
||||
kind: .giftWrap,
|
||||
tags: [["p", recipientPubkey]], // Tag recipient
|
||||
content: encrypted
|
||||
)
|
||||
|
||||
// Convert to P256K.Signing.PrivateKey for signing (temporary until we update sign method)
|
||||
let signingKey = try P256K.Signing.PrivateKey(dataRepresentation: wrapKey.dataRepresentation)
|
||||
return try giftWrap.sign(with: signingKey)
|
||||
}
|
||||
|
||||
private static func unwrapGiftWrap(
|
||||
giftWrap: NostrEvent,
|
||||
recipientKey: P256K.Schnorr.PrivateKey
|
||||
) throws -> NostrEvent {
|
||||
|
||||
// Unwrapping gift wrap
|
||||
|
||||
let decrypted = try decrypt(
|
||||
ciphertext: giftWrap.content,
|
||||
senderPubkey: giftWrap.pubkey,
|
||||
recipientKey: recipientKey
|
||||
)
|
||||
|
||||
guard let data = decrypted.data(using: .utf8),
|
||||
let sealDict = try JSONSerialization.jsonObject(with: data) as? [String: Any] else {
|
||||
throw NostrError.invalidEvent
|
||||
}
|
||||
|
||||
let seal = try NostrEvent(from: sealDict)
|
||||
// Unwrapped seal
|
||||
|
||||
return seal
|
||||
}
|
||||
|
||||
private static func openSeal(
|
||||
seal: NostrEvent,
|
||||
recipientKey: P256K.Schnorr.PrivateKey
|
||||
) throws -> NostrEvent {
|
||||
|
||||
let decrypted = try decrypt(
|
||||
ciphertext: seal.content,
|
||||
senderPubkey: seal.pubkey,
|
||||
recipientKey: recipientKey
|
||||
)
|
||||
|
||||
guard let data = decrypted.data(using: .utf8),
|
||||
let rumorDict = try JSONSerialization.jsonObject(with: data) as? [String: Any] else {
|
||||
throw NostrError.invalidEvent
|
||||
}
|
||||
|
||||
return try NostrEvent(from: rumorDict)
|
||||
}
|
||||
|
||||
// MARK: - Encryption (NIP-44 style)
|
||||
|
||||
private static func encrypt(
|
||||
plaintext: String,
|
||||
recipientPubkey: String,
|
||||
senderKey: P256K.Schnorr.PrivateKey
|
||||
) throws -> String {
|
||||
|
||||
guard let recipientPubkeyData = Data(hexString: recipientPubkey) else {
|
||||
throw NostrError.invalidPublicKey
|
||||
}
|
||||
|
||||
let senderPubkey = Data(senderKey.xonly.bytes).hexEncodedString()
|
||||
// Encrypting message
|
||||
|
||||
// Derive shared secret
|
||||
let sharedSecret = try deriveSharedSecret(
|
||||
privateKey: senderKey,
|
||||
publicKey: recipientPubkeyData
|
||||
)
|
||||
|
||||
// Derived shared secret
|
||||
|
||||
// Generate nonce
|
||||
let nonce = AES.GCM.Nonce()
|
||||
|
||||
// Encrypt
|
||||
let sealed = try AES.GCM.seal(
|
||||
plaintext.data(using: .utf8)!,
|
||||
using: SymmetricKey(data: sharedSecret),
|
||||
nonce: nonce
|
||||
)
|
||||
|
||||
// Combine nonce + ciphertext + tag
|
||||
var result = Data()
|
||||
result.append(nonce.withUnsafeBytes { Data($0) })
|
||||
result.append(sealed.ciphertext)
|
||||
result.append(sealed.tag)
|
||||
|
||||
return result.base64EncodedString()
|
||||
}
|
||||
|
||||
private static func decrypt(
|
||||
ciphertext: String,
|
||||
senderPubkey: String,
|
||||
recipientKey: P256K.Schnorr.PrivateKey
|
||||
) throws -> String {
|
||||
|
||||
// Decrypting message
|
||||
|
||||
guard let data = Data(base64Encoded: ciphertext),
|
||||
let senderPubkeyData = Data(hexString: senderPubkey) else {
|
||||
SecureLogger.log("❌ Invalid ciphertext or sender pubkey format",
|
||||
category: SecureLogger.session, level: .error)
|
||||
throw NostrError.invalidCiphertext
|
||||
}
|
||||
|
||||
// Ciphertext data parsed
|
||||
|
||||
// Extract components
|
||||
let nonceData = data.prefix(12)
|
||||
let ciphertextData = data.dropFirst(12).dropLast(16)
|
||||
let tagData = data.suffix(16)
|
||||
|
||||
// Components parsed
|
||||
|
||||
// Derive shared secret - try with default Y coordinate first
|
||||
var sharedSecret: Data
|
||||
var decrypted: Data? = nil
|
||||
|
||||
do {
|
||||
sharedSecret = try deriveSharedSecret(
|
||||
privateKey: recipientKey,
|
||||
publicKey: senderPubkeyData
|
||||
)
|
||||
// Derived shared secret with first Y coordinate
|
||||
|
||||
// Try to decrypt
|
||||
let sealedBox = try AES.GCM.SealedBox(
|
||||
nonce: AES.GCM.Nonce(data: nonceData),
|
||||
ciphertext: ciphertextData,
|
||||
tag: tagData
|
||||
)
|
||||
|
||||
do {
|
||||
decrypted = try AES.GCM.open(
|
||||
sealedBox,
|
||||
using: SymmetricKey(data: sharedSecret)
|
||||
)
|
||||
// AES-GCM decryption successful
|
||||
} catch {
|
||||
// AES-GCM decryption failed, trying alternate
|
||||
|
||||
// If the sender pubkey is x-only (32 bytes), try the other Y coordinate
|
||||
if senderPubkeyData.count == 32 {
|
||||
// Trying alternate Y coordinate
|
||||
|
||||
// Force deriveSharedSecret to use odd Y by manipulating the data
|
||||
var altPubkey = Data()
|
||||
altPubkey.append(0x03) // Force odd Y
|
||||
altPubkey.append(senderPubkeyData)
|
||||
|
||||
sharedSecret = try deriveSharedSecretDirect(
|
||||
privateKey: recipientKey,
|
||||
publicKey: altPubkey
|
||||
)
|
||||
|
||||
decrypted = try AES.GCM.open(
|
||||
sealedBox,
|
||||
using: SymmetricKey(data: sharedSecret)
|
||||
)
|
||||
// AES-GCM decryption successful with alternate Y
|
||||
} else {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
SecureLogger.log("❌ Failed to derive shared secret or decrypt: \(error)",
|
||||
category: SecureLogger.session, level: .error)
|
||||
throw error
|
||||
}
|
||||
|
||||
guard let finalDecrypted = decrypted else {
|
||||
throw NostrError.encryptionFailed
|
||||
}
|
||||
|
||||
return String(data: finalDecrypted, encoding: .utf8) ?? ""
|
||||
}
|
||||
|
||||
private static func deriveSharedSecret(
|
||||
privateKey: P256K.Schnorr.PrivateKey,
|
||||
publicKey: Data
|
||||
) throws -> Data {
|
||||
// Deriving shared secret
|
||||
|
||||
// Convert Schnorr private key to KeyAgreement private key
|
||||
let keyAgreementPrivateKey = try P256K.KeyAgreement.PrivateKey(
|
||||
dataRepresentation: privateKey.dataRepresentation
|
||||
)
|
||||
|
||||
// Create KeyAgreement public key from the public key data
|
||||
// For ECDH, we need the full 33-byte compressed public key (with 0x02 or 0x03 prefix)
|
||||
var fullPublicKey = Data()
|
||||
if publicKey.count == 32 { // X-only key, need to add prefix
|
||||
// For x-only keys in Nostr/Bitcoin, we need to try both possible Y coordinates
|
||||
// First try with even Y (0x02 prefix)
|
||||
fullPublicKey.append(0x02)
|
||||
fullPublicKey.append(publicKey)
|
||||
// Trying with even Y coordinate
|
||||
} else {
|
||||
fullPublicKey = publicKey
|
||||
}
|
||||
|
||||
// Try to create public key, if it fails with even Y, try odd Y
|
||||
let keyAgreementPublicKey: P256K.KeyAgreement.PublicKey
|
||||
do {
|
||||
keyAgreementPublicKey = try P256K.KeyAgreement.PublicKey(
|
||||
dataRepresentation: fullPublicKey,
|
||||
format: .compressed
|
||||
)
|
||||
} catch {
|
||||
if publicKey.count == 32 {
|
||||
// Try with odd Y (0x03 prefix)
|
||||
// Even Y failed, trying odd Y
|
||||
fullPublicKey = Data()
|
||||
fullPublicKey.append(0x03)
|
||||
fullPublicKey.append(publicKey)
|
||||
keyAgreementPublicKey = try P256K.KeyAgreement.PublicKey(
|
||||
dataRepresentation: fullPublicKey,
|
||||
format: .compressed
|
||||
)
|
||||
} else {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
// Perform ECDH
|
||||
let sharedSecret = try keyAgreementPrivateKey.sharedSecretFromKeyAgreement(
|
||||
with: keyAgreementPublicKey,
|
||||
format: .compressed
|
||||
)
|
||||
|
||||
// Convert SharedSecret to Data
|
||||
let sharedSecretData = sharedSecret.withUnsafeBytes { Data($0) }
|
||||
// ECDH shared secret derived
|
||||
|
||||
// Derive key using HKDF for NIP-44 v2
|
||||
let derivedKey = HKDF<CryptoKit.SHA256>.deriveKey(
|
||||
inputKeyMaterial: SymmetricKey(data: sharedSecretData),
|
||||
salt: "nip44-v2".data(using: .utf8)!,
|
||||
info: Data(),
|
||||
outputByteCount: 32
|
||||
)
|
||||
|
||||
let result = derivedKey.withUnsafeBytes { Data($0) }
|
||||
// Final derived key ready
|
||||
return result
|
||||
}
|
||||
|
||||
// Direct version that doesn't try to add prefixes
|
||||
private static func deriveSharedSecretDirect(
|
||||
privateKey: P256K.Schnorr.PrivateKey,
|
||||
publicKey: Data
|
||||
) throws -> Data {
|
||||
// Direct shared secret calculation
|
||||
|
||||
// Convert Schnorr private key to KeyAgreement private key
|
||||
let keyAgreementPrivateKey = try P256K.KeyAgreement.PrivateKey(
|
||||
dataRepresentation: privateKey.dataRepresentation
|
||||
)
|
||||
|
||||
// Use the public key as-is (should already have prefix)
|
||||
let keyAgreementPublicKey = try P256K.KeyAgreement.PublicKey(
|
||||
dataRepresentation: publicKey,
|
||||
format: .compressed
|
||||
)
|
||||
|
||||
// Perform ECDH
|
||||
let sharedSecret = try keyAgreementPrivateKey.sharedSecretFromKeyAgreement(
|
||||
with: keyAgreementPublicKey,
|
||||
format: .compressed
|
||||
)
|
||||
|
||||
// Convert SharedSecret to Data
|
||||
let sharedSecretData = sharedSecret.withUnsafeBytes { Data($0) }
|
||||
|
||||
// Derive key using HKDF for NIP-44 v2
|
||||
let derivedKey = HKDF<CryptoKit.SHA256>.deriveKey(
|
||||
inputKeyMaterial: SymmetricKey(data: sharedSecretData),
|
||||
salt: "nip44-v2".data(using: .utf8)!,
|
||||
info: Data(),
|
||||
outputByteCount: 32
|
||||
)
|
||||
|
||||
return derivedKey.withUnsafeBytes { Data($0) }
|
||||
}
|
||||
|
||||
private static func randomizedTimestamp() -> Date {
|
||||
// Add random offset to current time for privacy
|
||||
// TEMPORARY: Reduced range to debug timestamp issue
|
||||
let offset = TimeInterval.random(in: -60...60) // +/- 1 minute (was +/- 15 minutes)
|
||||
let now = Date()
|
||||
let randomized = now.addingTimeInterval(offset)
|
||||
|
||||
// Log with explicit UTC and local time for debugging
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
|
||||
formatter.timeZone = TimeZone(abbreviation: "UTC")
|
||||
let nowUTC = formatter.string(from: now)
|
||||
let randomizedUTC = formatter.string(from: randomized)
|
||||
|
||||
formatter.timeZone = TimeZone.current
|
||||
let nowLocal = formatter.string(from: now)
|
||||
let randomizedLocal = formatter.string(from: randomized)
|
||||
|
||||
// Timestamp randomized for privacy
|
||||
|
||||
return randomized
|
||||
}
|
||||
}
|
||||
|
||||
/// Nostr Event structure
|
||||
struct NostrEvent: Codable {
|
||||
var id: String
|
||||
let pubkey: String
|
||||
let created_at: Int
|
||||
let kind: Int
|
||||
let tags: [[String]]
|
||||
let content: String
|
||||
var sig: String?
|
||||
|
||||
init(
|
||||
pubkey: String,
|
||||
createdAt: Date,
|
||||
kind: NostrProtocol.EventKind,
|
||||
tags: [[String]],
|
||||
content: String
|
||||
) {
|
||||
self.pubkey = pubkey
|
||||
self.created_at = Int(createdAt.timeIntervalSince1970)
|
||||
self.kind = kind.rawValue
|
||||
self.tags = tags
|
||||
self.content = content
|
||||
self.sig = nil
|
||||
self.id = "" // Will be set during signing
|
||||
}
|
||||
|
||||
init(from dict: [String: Any]) throws {
|
||||
guard let pubkey = dict["pubkey"] as? String,
|
||||
let createdAt = dict["created_at"] as? Int,
|
||||
let kind = dict["kind"] as? Int,
|
||||
let tags = dict["tags"] as? [[String]],
|
||||
let content = dict["content"] as? String else {
|
||||
throw NostrError.invalidEvent
|
||||
}
|
||||
|
||||
self.id = dict["id"] as? String ?? ""
|
||||
self.pubkey = pubkey
|
||||
self.created_at = createdAt
|
||||
self.kind = kind
|
||||
self.tags = tags
|
||||
self.content = content
|
||||
self.sig = dict["sig"] as? String
|
||||
}
|
||||
|
||||
func sign(with key: P256K.Signing.PrivateKey) throws -> NostrEvent {
|
||||
let (eventId, eventIdHash) = try calculateEventId()
|
||||
|
||||
// Convert to Schnorr key for Nostr signing
|
||||
let schnorrKey = try P256K.Schnorr.PrivateKey(dataRepresentation: key.dataRepresentation)
|
||||
|
||||
// Sign with Schnorr
|
||||
var messageBytes = [UInt8](eventIdHash)
|
||||
var auxRand = [UInt8](repeating: 0, count: 32) // Zero auxiliary randomness for deterministic signing
|
||||
let schnorrSignature = try schnorrKey.signature(message: &messageBytes, auxiliaryRand: &auxRand)
|
||||
|
||||
let signatureHex = schnorrSignature.dataRepresentation.hexEncodedString()
|
||||
|
||||
var signed = self
|
||||
signed.id = eventId
|
||||
signed.sig = signatureHex
|
||||
return signed
|
||||
}
|
||||
|
||||
private func calculateEventId() throws -> (String, Data) {
|
||||
let serialized = [
|
||||
0,
|
||||
pubkey,
|
||||
created_at,
|
||||
kind,
|
||||
tags,
|
||||
content
|
||||
] as [Any]
|
||||
|
||||
let data = try JSONSerialization.data(withJSONObject: serialized, options: [.withoutEscapingSlashes])
|
||||
let hash = CryptoKit.SHA256.hash(data: data)
|
||||
let hashData = Data(hash)
|
||||
let hashHex = hash.compactMap { String(format: "%02x", $0) }.joined()
|
||||
return (hashHex, hashData)
|
||||
}
|
||||
|
||||
func jsonString() throws -> String {
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = [.withoutEscapingSlashes]
|
||||
let data = try encoder.encode(self)
|
||||
return String(data: data, encoding: .utf8) ?? ""
|
||||
}
|
||||
}
|
||||
|
||||
enum NostrError: Error {
|
||||
case invalidPublicKey
|
||||
case invalidPrivateKey
|
||||
case invalidEvent
|
||||
case invalidCiphertext
|
||||
case signingFailed
|
||||
case encryptionFailed
|
||||
}
|
||||
@@ -0,0 +1,552 @@
|
||||
import Foundation
|
||||
import Network
|
||||
import Combine
|
||||
|
||||
/// Manages WebSocket connections to Nostr relays
|
||||
@MainActor
|
||||
class NostrRelayManager: ObservableObject {
|
||||
static let shared = NostrRelayManager()
|
||||
|
||||
struct Relay: Identifiable {
|
||||
let id = UUID()
|
||||
let url: String
|
||||
var isConnected: Bool = false
|
||||
var lastError: Error?
|
||||
var lastConnectedAt: Date?
|
||||
var messagesSent: Int = 0
|
||||
var messagesReceived: Int = 0
|
||||
var reconnectAttempts: Int = 0
|
||||
var lastDisconnectedAt: Date?
|
||||
var nextReconnectTime: Date?
|
||||
}
|
||||
|
||||
// Default relay list (can be customized)
|
||||
private static let defaultRelays = [
|
||||
"wss://relay.damus.io",
|
||||
"wss://relay.primal.net",
|
||||
"wss://offchain.pub",
|
||||
"wss://nostr21.com"
|
||||
// For local testing, you can add: "ws://localhost:8080"
|
||||
]
|
||||
|
||||
@Published private(set) var relays: [Relay] = []
|
||||
@Published private(set) var isConnected = false
|
||||
|
||||
private var connections: [String: URLSessionWebSocketTask] = [:]
|
||||
private var subscriptions: [String: Set<String>] = [:] // relay URL -> subscription IDs
|
||||
private var messageHandlers: [String: (NostrEvent) -> Void] = [:]
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
|
||||
// Message queue for reliability
|
||||
private var messageQueue: [(event: NostrEvent, relayUrls: [String])] = []
|
||||
private let messageQueueLock = NSLock()
|
||||
|
||||
// Exponential backoff configuration
|
||||
private let initialBackoffInterval: TimeInterval = 1.0 // Start with 1 second
|
||||
private let maxBackoffInterval: TimeInterval = 300.0 // Max 5 minutes
|
||||
private let backoffMultiplier: Double = 2.0 // Double each time
|
||||
private let maxReconnectAttempts = 10 // Stop after 10 attempts
|
||||
|
||||
// Reconnection timer
|
||||
private var reconnectionTimer: Timer?
|
||||
|
||||
init() {
|
||||
// Initialize with default relays
|
||||
self.relays = Self.defaultRelays.map { Relay(url: $0) }
|
||||
}
|
||||
|
||||
/// Connect to all configured relays
|
||||
func connect() {
|
||||
for relay in relays {
|
||||
connectToRelay(relay.url)
|
||||
}
|
||||
}
|
||||
|
||||
/// Disconnect from all relays
|
||||
func disconnect() {
|
||||
for (_, task) in connections {
|
||||
task.cancel(with: .goingAway, reason: nil)
|
||||
}
|
||||
connections.removeAll()
|
||||
updateConnectionStatus()
|
||||
}
|
||||
|
||||
/// Send an event to specified relays (or all if none specified)
|
||||
func sendEvent(_ event: NostrEvent, to relayUrls: [String]? = nil) {
|
||||
let targetRelays = relayUrls ?? relays.map { $0.url }
|
||||
|
||||
// Add to queue for reliability
|
||||
messageQueueLock.lock()
|
||||
messageQueue.append((event, targetRelays))
|
||||
messageQueueLock.unlock()
|
||||
|
||||
// Attempt immediate send
|
||||
for relayUrl in targetRelays {
|
||||
if let connection = connections[relayUrl] {
|
||||
sendToRelay(event: event, connection: connection, relayUrl: relayUrl)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Subscribe to events matching a filter
|
||||
func subscribe(
|
||||
filter: NostrFilter,
|
||||
id: String = UUID().uuidString,
|
||||
handler: @escaping (NostrEvent) -> Void
|
||||
) {
|
||||
messageHandlers[id] = handler
|
||||
|
||||
let req = NostrRequest.subscribe(id: id, filters: [filter])
|
||||
|
||||
do {
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = .sortedKeys // For consistent output
|
||||
let message = try encoder.encode(req)
|
||||
guard let messageString = String(data: message, encoding: .utf8) else {
|
||||
SecureLogger.log("❌ Failed to encode subscription request", category: SecureLogger.session, level: .error)
|
||||
return
|
||||
}
|
||||
|
||||
// Sending subscription to relays
|
||||
// Filter JSON prepared
|
||||
// Full filter JSON logged
|
||||
|
||||
// Send subscription to all connected relays
|
||||
for (relayUrl, connection) in connections {
|
||||
connection.send(.string(messageString)) { error in
|
||||
if let error = error {
|
||||
SecureLogger.log("❌ Failed to send subscription to \(relayUrl): \(error)",
|
||||
category: SecureLogger.session, level: .error)
|
||||
} else {
|
||||
// Subscription sent successfully
|
||||
Task { @MainActor in
|
||||
var subs = self.subscriptions[relayUrl] ?? Set<String>()
|
||||
subs.insert(id)
|
||||
self.subscriptions[relayUrl] = subs
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if connections.isEmpty {
|
||||
SecureLogger.log("⚠️ No relay connections available for subscription",
|
||||
category: SecureLogger.session, level: .warning)
|
||||
}
|
||||
} catch {
|
||||
SecureLogger.log("❌ Failed to encode subscription request: \(error)",
|
||||
category: SecureLogger.session, level: .error)
|
||||
}
|
||||
}
|
||||
|
||||
/// Unsubscribe from a subscription
|
||||
func unsubscribe(id: String) {
|
||||
messageHandlers.removeValue(forKey: id)
|
||||
|
||||
let req = NostrRequest.close(id: id)
|
||||
let message = try? JSONEncoder().encode(req)
|
||||
|
||||
guard let messageData = message,
|
||||
let messageString = String(data: messageData, encoding: .utf8) else { return }
|
||||
|
||||
// Send unsubscribe to all relays
|
||||
for (relayUrl, connection) in connections {
|
||||
if subscriptions[relayUrl]?.contains(id) == true {
|
||||
connection.send(.string(messageString)) { _ in
|
||||
Task { @MainActor in
|
||||
self.subscriptions[relayUrl]?.remove(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private Methods
|
||||
|
||||
private func connectToRelay(_ urlString: String) {
|
||||
guard let url = URL(string: urlString) else {
|
||||
SecureLogger.log("Invalid relay URL: \(urlString)", category: SecureLogger.session, level: .warning)
|
||||
return
|
||||
}
|
||||
|
||||
// Attempting to connect to Nostr relay
|
||||
|
||||
let session = URLSession(configuration: .default)
|
||||
let task = session.webSocketTask(with: url)
|
||||
|
||||
connections[urlString] = task
|
||||
task.resume()
|
||||
|
||||
// Start receiving messages
|
||||
receiveMessage(from: task, relayUrl: urlString)
|
||||
|
||||
// Send initial ping to verify connection
|
||||
task.sendPing { [weak self] error in
|
||||
DispatchQueue.main.async {
|
||||
if error == nil {
|
||||
// Successfully connected to Nostr relay
|
||||
self?.updateRelayStatus(urlString, isConnected: true)
|
||||
SecureLogger.log("Successfully connected to Nostr relay \(urlString)",
|
||||
category: SecureLogger.session, level: .info)
|
||||
} else {
|
||||
SecureLogger.log("Failed to connect to Nostr relay \(urlString): \(error?.localizedDescription ?? "Unknown error")",
|
||||
category: SecureLogger.session, level: .error)
|
||||
self?.updateRelayStatus(urlString, isConnected: false, error: error)
|
||||
// Trigger disconnection handler for proper backoff
|
||||
self?.handleDisconnection(relayUrl: urlString, error: error ?? NSError(domain: "NostrRelay", code: -1, userInfo: nil))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func receiveMessage(from task: URLSessionWebSocketTask, relayUrl: String) {
|
||||
task.receive { [weak self] result in
|
||||
guard let self = self else { return }
|
||||
|
||||
switch result {
|
||||
case .success(let message):
|
||||
switch message {
|
||||
case .string(let text):
|
||||
Task { @MainActor in
|
||||
self.handleMessage(text, from: relayUrl)
|
||||
}
|
||||
case .data(let data):
|
||||
if let text = String(data: data, encoding: .utf8) {
|
||||
Task { @MainActor in
|
||||
self.handleMessage(text, from: relayUrl)
|
||||
}
|
||||
}
|
||||
@unknown default:
|
||||
break
|
||||
}
|
||||
|
||||
// Continue receiving
|
||||
Task { @MainActor in
|
||||
self.receiveMessage(from: task, relayUrl: relayUrl)
|
||||
}
|
||||
|
||||
case .failure(let error):
|
||||
DispatchQueue.main.async {
|
||||
self.handleDisconnection(relayUrl: relayUrl, error: error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func handleMessage(_ message: String, from relayUrl: String) {
|
||||
guard let data = message.data(using: .utf8) else { return }
|
||||
|
||||
do {
|
||||
// Try to decode as an array first
|
||||
if let array = try JSONSerialization.jsonObject(with: data) as? [Any],
|
||||
array.count >= 2,
|
||||
let type = array[0] as? String {
|
||||
|
||||
// Received message from relay
|
||||
|
||||
switch type {
|
||||
case "EVENT":
|
||||
if array.count >= 3,
|
||||
let subId = array[1] as? String,
|
||||
let eventDict = array[2] as? [String: Any] {
|
||||
|
||||
let event = try NostrEvent(from: eventDict)
|
||||
|
||||
// Processing event
|
||||
|
||||
DispatchQueue.main.async {
|
||||
// Update relay stats
|
||||
if let index = self.relays.firstIndex(where: { $0.url == relayUrl }) {
|
||||
self.relays[index].messagesReceived += 1
|
||||
}
|
||||
|
||||
// Call handler
|
||||
if let handler = self.messageHandlers[subId] {
|
||||
handler(event)
|
||||
} else {
|
||||
SecureLogger.log("⚠️ No handler for subscription \(subId)",
|
||||
category: SecureLogger.session, level: .warning)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case "EOSE":
|
||||
if array.count >= 2,
|
||||
let _ = array[1] as? String {
|
||||
// End of stored events
|
||||
}
|
||||
|
||||
case "OK":
|
||||
if array.count >= 3,
|
||||
let eventId = array[1] as? String,
|
||||
let success = array[2] as? Bool {
|
||||
let reason = array.count >= 4 ? (array[3] as? String ?? "no reason given") : "no reason given"
|
||||
if !success {
|
||||
SecureLogger.log("📮 Event \(eventId) rejected by relay: \(reason)",
|
||||
category: SecureLogger.session, level: .error)
|
||||
}
|
||||
}
|
||||
|
||||
case "NOTICE":
|
||||
if array.count >= 2,
|
||||
let notice = array[1] as? String {
|
||||
SecureLogger.log("📢 Relay notice: \(notice)",
|
||||
category: SecureLogger.session, level: .info)
|
||||
}
|
||||
|
||||
default:
|
||||
break // Unknown message type
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
SecureLogger.log("Failed to parse Nostr message: \(error)", category: SecureLogger.session, level: .error)
|
||||
}
|
||||
}
|
||||
|
||||
private func sendToRelay(event: NostrEvent, connection: URLSessionWebSocketTask, relayUrl: String) {
|
||||
let req = NostrRequest.event(event)
|
||||
|
||||
do {
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = .sortedKeys
|
||||
let data = try encoder.encode(req)
|
||||
let message = String(data: data, encoding: .utf8) ?? ""
|
||||
|
||||
// Sending event to relay
|
||||
// Event JSON prepared
|
||||
|
||||
connection.send(.string(message)) { [weak self] error in
|
||||
DispatchQueue.main.async {
|
||||
if let error = error {
|
||||
SecureLogger.log("❌ Failed to send event to \(relayUrl): \(error)",
|
||||
category: SecureLogger.session, level: .error)
|
||||
} else {
|
||||
// Update relay stats
|
||||
if let index = self?.relays.firstIndex(where: { $0.url == relayUrl }) {
|
||||
self?.relays[index].messagesSent += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
SecureLogger.log("Failed to encode event: \(error)", category: SecureLogger.session, level: .error)
|
||||
}
|
||||
}
|
||||
|
||||
private func updateRelayStatus(_ url: String, isConnected: Bool, error: Error? = nil) {
|
||||
if let index = relays.firstIndex(where: { $0.url == url }) {
|
||||
relays[index].isConnected = isConnected
|
||||
relays[index].lastError = error
|
||||
if isConnected {
|
||||
relays[index].lastConnectedAt = Date()
|
||||
relays[index].reconnectAttempts = 0 // Reset on successful connection
|
||||
relays[index].nextReconnectTime = nil
|
||||
} else {
|
||||
relays[index].lastDisconnectedAt = Date()
|
||||
}
|
||||
}
|
||||
updateConnectionStatus()
|
||||
}
|
||||
|
||||
private func updateConnectionStatus() {
|
||||
isConnected = relays.contains { $0.isConnected }
|
||||
}
|
||||
|
||||
private func handleDisconnection(relayUrl: String, error: Error) {
|
||||
connections.removeValue(forKey: relayUrl)
|
||||
subscriptions.removeValue(forKey: relayUrl)
|
||||
updateRelayStatus(relayUrl, isConnected: false, error: error)
|
||||
|
||||
// Check if this is a DNS error
|
||||
let errorDescription = error.localizedDescription.lowercased()
|
||||
if errorDescription.contains("hostname could not be found") ||
|
||||
errorDescription.contains("dns") {
|
||||
// Only log once for DNS failures
|
||||
if relays.first(where: { $0.url == relayUrl })?.lastError == nil {
|
||||
SecureLogger.log("Nostr relay DNS failure for \(relayUrl) - not retrying", category: SecureLogger.session, level: .warning)
|
||||
}
|
||||
// Mark relay as permanently failed
|
||||
if let index = relays.firstIndex(where: { $0.url == relayUrl }) {
|
||||
relays[index].lastError = error
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Implement exponential backoff for non-DNS errors
|
||||
guard let index = relays.firstIndex(where: { $0.url == relayUrl }) else { return }
|
||||
|
||||
relays[index].reconnectAttempts += 1
|
||||
|
||||
// Stop attempting after max attempts
|
||||
if relays[index].reconnectAttempts >= maxReconnectAttempts {
|
||||
SecureLogger.log("Max reconnection attempts (\(maxReconnectAttempts)) reached for \(relayUrl)",
|
||||
category: SecureLogger.session, level: .warning)
|
||||
return
|
||||
}
|
||||
|
||||
// Calculate backoff interval
|
||||
let backoffInterval = min(
|
||||
initialBackoffInterval * pow(backoffMultiplier, Double(relays[index].reconnectAttempts - 1)),
|
||||
maxBackoffInterval
|
||||
)
|
||||
|
||||
let nextReconnectTime = Date().addingTimeInterval(backoffInterval)
|
||||
relays[index].nextReconnectTime = nextReconnectTime
|
||||
|
||||
SecureLogger.log("Scheduling reconnection to \(relayUrl) in \(Int(backoffInterval))s (attempt \(relays[index].reconnectAttempts))",
|
||||
category: SecureLogger.session, level: .info)
|
||||
|
||||
// Schedule reconnection with exponential backoff
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + backoffInterval) { [weak self] in
|
||||
guard let self = self else { return }
|
||||
|
||||
// Check if we should still reconnect (relay might have been removed)
|
||||
if self.relays.contains(where: { $0.url == relayUrl }) {
|
||||
self.connectToRelay(relayUrl)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Public Utility Methods
|
||||
|
||||
/// Manually retry connection to a specific relay
|
||||
func retryConnection(to relayUrl: String) {
|
||||
guard let index = relays.firstIndex(where: { $0.url == relayUrl }) else { return }
|
||||
|
||||
// Reset reconnection attempts
|
||||
relays[index].reconnectAttempts = 0
|
||||
relays[index].nextReconnectTime = nil
|
||||
|
||||
// Disconnect if connected
|
||||
if let connection = connections[relayUrl] {
|
||||
connection.cancel(with: .goingAway, reason: nil)
|
||||
connections.removeValue(forKey: relayUrl)
|
||||
}
|
||||
|
||||
// Attempt immediate reconnection
|
||||
connectToRelay(relayUrl)
|
||||
}
|
||||
|
||||
/// Get detailed status for all relays
|
||||
func getRelayStatuses() -> [(url: String, isConnected: Bool, reconnectAttempts: Int, nextReconnectTime: Date?)] {
|
||||
return relays.map { relay in
|
||||
(url: relay.url,
|
||||
isConnected: relay.isConnected,
|
||||
reconnectAttempts: relay.reconnectAttempts,
|
||||
nextReconnectTime: relay.nextReconnectTime)
|
||||
}
|
||||
}
|
||||
|
||||
/// Reset all relay connections
|
||||
func resetAllConnections() {
|
||||
disconnect()
|
||||
|
||||
// Reset all relay states
|
||||
for index in relays.indices {
|
||||
relays[index].reconnectAttempts = 0
|
||||
relays[index].nextReconnectTime = nil
|
||||
relays[index].lastError = nil
|
||||
}
|
||||
|
||||
// Reconnect
|
||||
connect()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Nostr Protocol Types
|
||||
|
||||
enum NostrRequest: Encodable {
|
||||
case event(NostrEvent)
|
||||
case subscribe(id: String, filters: [NostrFilter])
|
||||
case close(id: String)
|
||||
|
||||
func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.unkeyedContainer()
|
||||
|
||||
switch self {
|
||||
case .event(let event):
|
||||
try container.encode("EVENT")
|
||||
try container.encode(event)
|
||||
|
||||
case .subscribe(let id, let filters):
|
||||
try container.encode("REQ")
|
||||
try container.encode(id)
|
||||
for filter in filters {
|
||||
try container.encode(filter)
|
||||
}
|
||||
|
||||
case .close(let id):
|
||||
try container.encode("CLOSE")
|
||||
try container.encode(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct NostrFilter: Encodable {
|
||||
var ids: [String]?
|
||||
var authors: [String]?
|
||||
var kinds: [Int]?
|
||||
var since: Int?
|
||||
var until: Int?
|
||||
var limit: Int?
|
||||
|
||||
// Tag filters - stored internally but encoded specially
|
||||
fileprivate var tagFilters: [String: [String]]?
|
||||
|
||||
init() {
|
||||
// Default initializer
|
||||
}
|
||||
|
||||
// Custom encoding to handle tag filters properly
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case ids, authors, kinds, since, until, limit
|
||||
}
|
||||
|
||||
func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: DynamicCodingKey.self)
|
||||
|
||||
// Encode standard fields
|
||||
if let ids = ids { try container.encode(ids, forKey: DynamicCodingKey(stringValue: "ids")) }
|
||||
if let authors = authors { try container.encode(authors, forKey: DynamicCodingKey(stringValue: "authors")) }
|
||||
if let kinds = kinds { try container.encode(kinds, forKey: DynamicCodingKey(stringValue: "kinds")) }
|
||||
if let since = since { try container.encode(since, forKey: DynamicCodingKey(stringValue: "since")) }
|
||||
if let until = until { try container.encode(until, forKey: DynamicCodingKey(stringValue: "until")) }
|
||||
if let limit = limit { try container.encode(limit, forKey: DynamicCodingKey(stringValue: "limit")) }
|
||||
|
||||
// Encode tag filters with # prefix
|
||||
if let tagFilters = tagFilters {
|
||||
for (tag, values) in tagFilters {
|
||||
try container.encode(values, forKey: DynamicCodingKey(stringValue: "#\(tag)"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// For NIP-17 gift wraps
|
||||
static func giftWrapsFor(pubkey: String, since: Date? = nil) -> NostrFilter {
|
||||
var filter = NostrFilter()
|
||||
filter.kinds = [1059] // Gift wrap kind
|
||||
filter.since = since?.timeIntervalSince1970.toInt()
|
||||
filter.tagFilters = ["p": [pubkey]]
|
||||
filter.limit = 100 // Add a reasonable limit
|
||||
return filter
|
||||
}
|
||||
}
|
||||
|
||||
// Dynamic coding key for tag filters
|
||||
private struct DynamicCodingKey: CodingKey {
|
||||
var stringValue: String
|
||||
var intValue: Int? { nil }
|
||||
|
||||
init(stringValue: String) {
|
||||
self.stringValue = stringValue
|
||||
}
|
||||
|
||||
init?(intValue: Int) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
private extension TimeInterval {
|
||||
func toInt() -> Int {
|
||||
return Int(self)
|
||||
}
|
||||
}
|
||||
@@ -162,6 +162,10 @@ enum MessageType: UInt8 {
|
||||
case systemValidation = 0x24 // Session validation ping
|
||||
case handshakeRequest = 0x25 // Request handshake for pending messages
|
||||
|
||||
// Favorite system messages
|
||||
case favorited = 0x30 // Peer favorited us
|
||||
case unfavorited = 0x31 // Peer unfavorited us
|
||||
|
||||
var description: String {
|
||||
switch self {
|
||||
case .announce: return "announce"
|
||||
@@ -183,6 +187,8 @@ enum MessageType: UInt8 {
|
||||
case .protocolNack: return "protocolNack"
|
||||
case .systemValidation: return "systemValidation"
|
||||
case .handshakeRequest: return "handshakeRequest"
|
||||
case .favorited: return "favorited"
|
||||
case .unfavorited: return "unfavorited"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -369,7 +375,7 @@ struct DeliveryAck: Codable {
|
||||
struct ReadReceipt: Codable {
|
||||
let originalMessageID: String
|
||||
let receiptID: String
|
||||
let readerID: String // Who read it
|
||||
var readerID: String // Who read it
|
||||
let readerNickname: String
|
||||
let timestamp: Date
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,415 @@
|
||||
import Foundation
|
||||
import Combine
|
||||
|
||||
/// Manages persistent favorite relationships between peers
|
||||
@MainActor
|
||||
class FavoritesPersistenceService: ObservableObject {
|
||||
|
||||
struct FavoriteRelationship: Codable {
|
||||
let peerNoisePublicKey: Data
|
||||
let peerNostrPublicKey: String?
|
||||
let peerNickname: String
|
||||
let isFavorite: Bool
|
||||
let theyFavoritedUs: Bool
|
||||
let favoritedAt: Date
|
||||
let lastUpdated: Date
|
||||
|
||||
var isMutual: Bool {
|
||||
isFavorite && theyFavoritedUs
|
||||
}
|
||||
}
|
||||
|
||||
private static let storageKey = "chat.bitchat.favorites"
|
||||
private static let keychainService = "chat.bitchat.favorites"
|
||||
|
||||
@Published private(set) var favorites: [Data: FavoriteRelationship] = [:] // Noise pubkey -> relationship
|
||||
@Published private(set) var mutualFavorites: Set<Data> = []
|
||||
|
||||
private let userDefaults = UserDefaults.standard
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
|
||||
static let shared = FavoritesPersistenceService()
|
||||
|
||||
private init() {
|
||||
loadFavorites()
|
||||
|
||||
// Update mutual favorites when favorites change
|
||||
$favorites
|
||||
.map { favorites in
|
||||
Set(favorites.compactMap { $0.value.isMutual ? $0.key : nil })
|
||||
}
|
||||
.assign(to: &$mutualFavorites)
|
||||
}
|
||||
|
||||
/// Add or update a favorite
|
||||
func addFavorite(
|
||||
peerNoisePublicKey: Data,
|
||||
peerNostrPublicKey: String? = nil,
|
||||
peerNickname: String
|
||||
) {
|
||||
SecureLogger.log("⭐️ Adding favorite: \(peerNickname) (\(peerNoisePublicKey.hexEncodedString()))",
|
||||
category: SecureLogger.session, level: .info)
|
||||
|
||||
let existing = favorites[peerNoisePublicKey]
|
||||
|
||||
let relationship = FavoriteRelationship(
|
||||
peerNoisePublicKey: peerNoisePublicKey,
|
||||
peerNostrPublicKey: peerNostrPublicKey ?? existing?.peerNostrPublicKey,
|
||||
peerNickname: peerNickname,
|
||||
isFavorite: true,
|
||||
theyFavoritedUs: existing?.theyFavoritedUs ?? false,
|
||||
favoritedAt: existing?.favoritedAt ?? Date(),
|
||||
lastUpdated: Date()
|
||||
)
|
||||
|
||||
// Log if this creates a mutual favorite
|
||||
if relationship.isMutual {
|
||||
SecureLogger.log("💕 Mutual favorite relationship established with \(peerNickname)!",
|
||||
category: SecureLogger.session, level: .info)
|
||||
}
|
||||
|
||||
favorites[peerNoisePublicKey] = relationship
|
||||
saveFavorites()
|
||||
|
||||
// Notify observers
|
||||
NotificationCenter.default.post(
|
||||
name: .favoriteStatusChanged,
|
||||
object: nil,
|
||||
userInfo: ["peerPublicKey": peerNoisePublicKey]
|
||||
)
|
||||
}
|
||||
|
||||
/// Remove a favorite
|
||||
func removeFavorite(peerNoisePublicKey: Data) {
|
||||
guard let existing = favorites[peerNoisePublicKey] else { return }
|
||||
|
||||
SecureLogger.log("⭐️ Removing favorite: \(existing.peerNickname) (\(peerNoisePublicKey.hexEncodedString()))",
|
||||
category: SecureLogger.session, level: .info)
|
||||
|
||||
// If they still favorite us, keep the record but mark us as not favoriting
|
||||
if existing.theyFavoritedUs {
|
||||
let updated = FavoriteRelationship(
|
||||
peerNoisePublicKey: existing.peerNoisePublicKey,
|
||||
peerNostrPublicKey: existing.peerNostrPublicKey,
|
||||
peerNickname: existing.peerNickname,
|
||||
isFavorite: false,
|
||||
theyFavoritedUs: true,
|
||||
favoritedAt: existing.favoritedAt,
|
||||
lastUpdated: Date()
|
||||
)
|
||||
favorites[peerNoisePublicKey] = updated
|
||||
// Keeping record - they still favorite us
|
||||
} else {
|
||||
// Neither side favorites, remove completely
|
||||
favorites.removeValue(forKey: peerNoisePublicKey)
|
||||
// Completely removed from favorites
|
||||
}
|
||||
|
||||
saveFavorites()
|
||||
|
||||
// Notify observers
|
||||
NotificationCenter.default.post(
|
||||
name: .favoriteStatusChanged,
|
||||
object: nil,
|
||||
userInfo: ["peerPublicKey": peerNoisePublicKey]
|
||||
)
|
||||
}
|
||||
|
||||
/// Update when we learn a peer favorited/unfavorited us
|
||||
func updatePeerFavoritedUs(
|
||||
peerNoisePublicKey: Data,
|
||||
favorited: Bool,
|
||||
peerNickname: String? = nil,
|
||||
peerNostrPublicKey: String? = nil
|
||||
) {
|
||||
let existing = favorites[peerNoisePublicKey]
|
||||
let displayName = peerNickname ?? existing?.peerNickname ?? "Unknown"
|
||||
|
||||
SecureLogger.log("📨 Received favorite notification: \(displayName) \(favorited ? "favorited" : "unfavorited") us",
|
||||
category: SecureLogger.session, level: .info)
|
||||
|
||||
let relationship = FavoriteRelationship(
|
||||
peerNoisePublicKey: peerNoisePublicKey,
|
||||
peerNostrPublicKey: peerNostrPublicKey ?? existing?.peerNostrPublicKey,
|
||||
peerNickname: displayName,
|
||||
isFavorite: existing?.isFavorite ?? false,
|
||||
theyFavoritedUs: favorited,
|
||||
favoritedAt: existing?.favoritedAt ?? Date(),
|
||||
lastUpdated: Date()
|
||||
)
|
||||
|
||||
if !relationship.isFavorite && !relationship.theyFavoritedUs {
|
||||
// Neither side favorites, remove completely
|
||||
favorites.removeValue(forKey: peerNoisePublicKey)
|
||||
// Removed - neither side favorites anymore
|
||||
} else {
|
||||
favorites[peerNoisePublicKey] = relationship
|
||||
|
||||
// Check if this creates a mutual favorite
|
||||
if relationship.isMutual {
|
||||
SecureLogger.log("💕 Mutual favorite relationship established with \(displayName)!",
|
||||
category: SecureLogger.session, level: .info)
|
||||
}
|
||||
}
|
||||
|
||||
saveFavorites()
|
||||
|
||||
// Notify observers
|
||||
NotificationCenter.default.post(
|
||||
name: .favoriteStatusChanged,
|
||||
object: nil,
|
||||
userInfo: ["peerPublicKey": peerNoisePublicKey]
|
||||
)
|
||||
}
|
||||
|
||||
/// Check if a peer is favorited by us
|
||||
func isFavorite(_ peerNoisePublicKey: Data) -> Bool {
|
||||
favorites[peerNoisePublicKey]?.isFavorite ?? false
|
||||
}
|
||||
|
||||
/// Check if we have a mutual favorite relationship
|
||||
func isMutualFavorite(_ peerNoisePublicKey: Data) -> Bool {
|
||||
favorites[peerNoisePublicKey]?.isMutual ?? false
|
||||
}
|
||||
|
||||
/// Get favorite status for a peer
|
||||
func getFavoriteStatus(for peerNoisePublicKey: Data) -> FavoriteRelationship? {
|
||||
favorites[peerNoisePublicKey]
|
||||
}
|
||||
|
||||
/// Update Nostr public key for a peer
|
||||
func updateNostrPublicKey(for peerNoisePublicKey: Data, nostrPubkey: String) {
|
||||
guard let existing = favorites[peerNoisePublicKey] else { return }
|
||||
|
||||
let updated = FavoriteRelationship(
|
||||
peerNoisePublicKey: existing.peerNoisePublicKey,
|
||||
peerNostrPublicKey: nostrPubkey,
|
||||
peerNickname: existing.peerNickname,
|
||||
isFavorite: existing.isFavorite,
|
||||
theyFavoritedUs: existing.theyFavoritedUs,
|
||||
favoritedAt: existing.favoritedAt,
|
||||
lastUpdated: Date()
|
||||
)
|
||||
|
||||
favorites[peerNoisePublicKey] = updated
|
||||
saveFavorites()
|
||||
}
|
||||
|
||||
/// Update nickname for an existing favorite
|
||||
func updateNickname(for peerNoisePublicKey: Data, newNickname: String) {
|
||||
guard let existing = favorites[peerNoisePublicKey] else { return }
|
||||
|
||||
// Skip if nickname hasn't changed
|
||||
if existing.peerNickname == newNickname { return }
|
||||
|
||||
// Updating nickname for favorite
|
||||
|
||||
let updated = FavoriteRelationship(
|
||||
peerNoisePublicKey: existing.peerNoisePublicKey,
|
||||
peerNostrPublicKey: existing.peerNostrPublicKey,
|
||||
peerNickname: newNickname,
|
||||
isFavorite: existing.isFavorite,
|
||||
theyFavoritedUs: existing.theyFavoritedUs,
|
||||
favoritedAt: existing.favoritedAt,
|
||||
lastUpdated: Date()
|
||||
)
|
||||
|
||||
favorites[peerNoisePublicKey] = updated
|
||||
saveFavorites()
|
||||
|
||||
// Notify observers
|
||||
NotificationCenter.default.post(
|
||||
name: .favoriteStatusChanged,
|
||||
object: nil,
|
||||
userInfo: ["peerPublicKey": peerNoisePublicKey]
|
||||
)
|
||||
}
|
||||
|
||||
/// Update noise public key when peer reconnects with new ID
|
||||
func updateNoisePublicKey(from oldKey: Data, to newKey: Data, peerNickname: String) {
|
||||
guard let existing = favorites[oldKey] else {
|
||||
SecureLogger.log("⚠️ Cannot update noise key - no favorite found for \(oldKey.hexEncodedString())",
|
||||
category: SecureLogger.session, level: .warning)
|
||||
return
|
||||
}
|
||||
|
||||
// Check if we already have a favorite with the new key
|
||||
if favorites[newKey] != nil {
|
||||
SecureLogger.log("⚠️ Favorite already exists with new key \(newKey.hexEncodedString()), removing old entry",
|
||||
category: SecureLogger.session, level: .warning)
|
||||
favorites.removeValue(forKey: oldKey)
|
||||
saveFavorites()
|
||||
return
|
||||
}
|
||||
|
||||
// Updating noise public key
|
||||
|
||||
// Remove old entry
|
||||
favorites.removeValue(forKey: oldKey)
|
||||
|
||||
// Add with new key
|
||||
let updated = FavoriteRelationship(
|
||||
peerNoisePublicKey: newKey,
|
||||
peerNostrPublicKey: existing.peerNostrPublicKey,
|
||||
peerNickname: peerNickname,
|
||||
isFavorite: existing.isFavorite,
|
||||
theyFavoritedUs: existing.theyFavoritedUs,
|
||||
favoritedAt: existing.favoritedAt,
|
||||
lastUpdated: Date()
|
||||
)
|
||||
|
||||
favorites[newKey] = updated
|
||||
saveFavorites()
|
||||
|
||||
// Notify observers with both old and new keys
|
||||
NotificationCenter.default.post(
|
||||
name: .favoriteStatusChanged,
|
||||
object: nil,
|
||||
userInfo: [
|
||||
"peerPublicKey": newKey,
|
||||
"oldPeerPublicKey": oldKey,
|
||||
"isKeyUpdate": true
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
/// Get all favorites (including non-mutual)
|
||||
func getAllFavorites() -> [FavoriteRelationship] {
|
||||
favorites.values.filter { $0.isFavorite }
|
||||
}
|
||||
|
||||
/// Get only mutual favorites
|
||||
func getMutualFavorites() -> [FavoriteRelationship] {
|
||||
favorites.values.filter { $0.isMutual }
|
||||
}
|
||||
|
||||
/// Get all favorite relationships (including where they favorited us)
|
||||
func getAllRelationships() -> [FavoriteRelationship] {
|
||||
Array(favorites.values)
|
||||
}
|
||||
|
||||
/// Clear all favorites - used for panic mode
|
||||
func clearAllFavorites() {
|
||||
SecureLogger.log("🧹 Clearing all favorites (panic mode)", category: SecureLogger.session, level: .warning)
|
||||
|
||||
favorites.removeAll()
|
||||
saveFavorites()
|
||||
|
||||
// Delete from keychain directly
|
||||
KeychainHelper.delete(
|
||||
key: Self.storageKey,
|
||||
service: Self.keychainService
|
||||
)
|
||||
|
||||
// Post notification for UI update
|
||||
NotificationCenter.default.post(name: .favoriteStatusChanged, object: nil)
|
||||
}
|
||||
|
||||
// MARK: - Persistence
|
||||
|
||||
private func saveFavorites() {
|
||||
let relationships = Array(favorites.values)
|
||||
// Saving favorite relationships to keychain
|
||||
|
||||
do {
|
||||
let encoder = JSONEncoder()
|
||||
let data = try encoder.encode(relationships)
|
||||
|
||||
// Store in keychain for security
|
||||
KeychainHelper.save(
|
||||
key: Self.storageKey,
|
||||
data: data,
|
||||
service: Self.keychainService
|
||||
)
|
||||
|
||||
// Successfully saved favorites
|
||||
} catch {
|
||||
SecureLogger.log("Failed to save favorites: \(error)", category: SecureLogger.session, level: .error)
|
||||
}
|
||||
}
|
||||
|
||||
private func loadFavorites() {
|
||||
// Loading favorites from keychain
|
||||
|
||||
guard let data = KeychainHelper.load(
|
||||
key: Self.storageKey,
|
||||
service: Self.keychainService
|
||||
) else {
|
||||
SecureLogger.log("📭 No existing favorites found in keychain", category: SecureLogger.session, level: .info)
|
||||
return
|
||||
}
|
||||
|
||||
do {
|
||||
let decoder = JSONDecoder()
|
||||
let relationships = try decoder.decode([FavoriteRelationship].self, from: data)
|
||||
|
||||
SecureLogger.log("✅ Loaded \(relationships.count) favorite relationships",
|
||||
category: SecureLogger.session, level: .info)
|
||||
|
||||
// Log Nostr public key info
|
||||
for relationship in relationships {
|
||||
if relationship.peerNostrPublicKey == nil {
|
||||
SecureLogger.log("⚠️ No Nostr public key stored for '\(relationship.peerNickname)'",
|
||||
category: SecureLogger.session, level: .warning)
|
||||
}
|
||||
}
|
||||
|
||||
// Convert to dictionary, cleaning up duplicates by public key (not nickname)
|
||||
var seenPublicKeys: [Data: FavoriteRelationship] = [:]
|
||||
var cleanedRelationships: [FavoriteRelationship] = []
|
||||
|
||||
for relationship in relationships {
|
||||
// Check for duplicates by public key (the actual unique identifier)
|
||||
if let existing = seenPublicKeys[relationship.peerNoisePublicKey] {
|
||||
SecureLogger.log("⚠️ Duplicate favorite found for public key \(relationship.peerNoisePublicKey.hexEncodedString()) - nicknames: '\(existing.peerNickname)' vs '\(relationship.peerNickname)'",
|
||||
category: SecureLogger.session, level: .warning)
|
||||
|
||||
// Keep the most recent or most complete relationship
|
||||
if relationship.lastUpdated > existing.lastUpdated ||
|
||||
(relationship.peerNostrPublicKey != nil && existing.peerNostrPublicKey == nil) {
|
||||
// Replace with newer/more complete entry
|
||||
seenPublicKeys[relationship.peerNoisePublicKey] = relationship
|
||||
cleanedRelationships.removeAll { $0.peerNoisePublicKey == relationship.peerNoisePublicKey }
|
||||
cleanedRelationships.append(relationship)
|
||||
}
|
||||
} else {
|
||||
seenPublicKeys[relationship.peerNoisePublicKey] = relationship
|
||||
cleanedRelationships.append(relationship)
|
||||
}
|
||||
}
|
||||
|
||||
// If we cleaned up duplicates, save the cleaned list
|
||||
if cleanedRelationships.count < relationships.count {
|
||||
// Cleaned up duplicates
|
||||
|
||||
// Clear and rebuild favorites dictionary
|
||||
favorites.removeAll()
|
||||
for relationship in cleanedRelationships {
|
||||
favorites[relationship.peerNoisePublicKey] = relationship
|
||||
}
|
||||
|
||||
// Save cleaned favorites
|
||||
saveFavorites()
|
||||
|
||||
// Notify that favorites have been cleaned up (synchronously since we're already on main actor)
|
||||
NotificationCenter.default.post(name: .favoriteStatusChanged, object: nil)
|
||||
} else {
|
||||
// No duplicates, just populate normally
|
||||
for relationship in cleanedRelationships {
|
||||
favorites[relationship.peerNoisePublicKey] = relationship
|
||||
}
|
||||
}
|
||||
|
||||
// Log loaded relationships
|
||||
// Loaded relationships successfully
|
||||
} catch {
|
||||
SecureLogger.log("Failed to load favorites: \(error)", category: SecureLogger.session, level: .error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Notification Names
|
||||
|
||||
extension Notification.Name {
|
||||
static let favoriteStatusChanged = Notification.Name("FavoriteStatusChanged")
|
||||
}
|
||||
@@ -0,0 +1,671 @@
|
||||
import Foundation
|
||||
import Combine
|
||||
|
||||
/// Routes messages through the appropriate transport (Bluetooth mesh or Nostr)
|
||||
@MainActor
|
||||
class MessageRouter: ObservableObject {
|
||||
|
||||
enum Transport {
|
||||
case bluetoothMesh
|
||||
case nostr
|
||||
}
|
||||
|
||||
enum DeliveryStatus {
|
||||
case pending
|
||||
case sent
|
||||
case delivered
|
||||
case failed(Error)
|
||||
}
|
||||
|
||||
struct RoutedMessage {
|
||||
let id: String
|
||||
let content: String
|
||||
let recipientNoisePublicKey: Data
|
||||
let transport: Transport
|
||||
let timestamp: Date
|
||||
var status: DeliveryStatus
|
||||
}
|
||||
|
||||
@Published private(set) var pendingMessages: [String: RoutedMessage] = [:]
|
||||
|
||||
private let meshService: BluetoothMeshService
|
||||
private let nostrRelay: NostrRelayManager
|
||||
private let favoritesService: FavoritesPersistenceService
|
||||
private let processedMessagesService = ProcessedMessagesService.shared
|
||||
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
private let messageDeduplication = LRUCache<String, Date>(maxSize: 1000)
|
||||
|
||||
init(
|
||||
meshService: BluetoothMeshService,
|
||||
nostrRelay: NostrRelayManager
|
||||
) {
|
||||
self.meshService = meshService
|
||||
self.nostrRelay = nostrRelay
|
||||
self.favoritesService = FavoritesPersistenceService.shared
|
||||
|
||||
setupBindings()
|
||||
}
|
||||
|
||||
/// Send a message to a peer, automatically selecting the best transport
|
||||
func sendMessage(
|
||||
_ content: String,
|
||||
to recipientNoisePublicKey: Data,
|
||||
preferredTransport: Transport? = nil,
|
||||
messageId: String? = nil
|
||||
) async throws {
|
||||
|
||||
let finalMessageId = messageId ?? UUID().uuidString
|
||||
|
||||
// Check if peer is available on mesh (actually connected, not just known)
|
||||
let recipientHexID = recipientNoisePublicKey.hexEncodedString()
|
||||
let peerAvailableOnMesh = meshService.isPeerConnected(recipientHexID)
|
||||
|
||||
// Check if this is a mutual favorite
|
||||
let isMutualFavorite = favoritesService.isMutualFavorite(recipientNoisePublicKey)
|
||||
|
||||
// Determine transport
|
||||
let transport: Transport
|
||||
if let preferred = preferredTransport {
|
||||
transport = preferred
|
||||
} else if peerAvailableOnMesh {
|
||||
// Always prefer mesh when available
|
||||
transport = .bluetoothMesh
|
||||
} else if isMutualFavorite {
|
||||
// Use Nostr for mutual favorites when not on mesh
|
||||
transport = .nostr
|
||||
} else {
|
||||
throw MessageRouterError.peerNotReachable
|
||||
}
|
||||
|
||||
// Create routed message
|
||||
let routedMessage = RoutedMessage(
|
||||
id: finalMessageId,
|
||||
content: content,
|
||||
recipientNoisePublicKey: recipientNoisePublicKey,
|
||||
transport: transport,
|
||||
timestamp: Date(),
|
||||
status: .pending
|
||||
)
|
||||
|
||||
pendingMessages[finalMessageId] = routedMessage
|
||||
|
||||
// Route based on transport
|
||||
switch transport {
|
||||
case .bluetoothMesh:
|
||||
try await sendViaMesh(routedMessage)
|
||||
|
||||
case .nostr:
|
||||
try await sendViaNostr(routedMessage)
|
||||
}
|
||||
}
|
||||
|
||||
/// Send a favorite/unfavorite notification
|
||||
func sendFavoriteNotification(
|
||||
to recipientNoisePublicKey: Data,
|
||||
isFavorite: Bool
|
||||
) async throws {
|
||||
|
||||
// messageType is used for logging below
|
||||
// let messageType: MessageType = isFavorite ? .favorited : .unfavorited
|
||||
let recipientHexID = recipientNoisePublicKey.hexEncodedString()
|
||||
let action = isFavorite ? "favorite" : "unfavorite"
|
||||
|
||||
SecureLogger.log("📤 Sending \(action) notification to \(recipientHexID)",
|
||||
category: SecureLogger.session, level: .info)
|
||||
|
||||
// Try mesh first
|
||||
if meshService.getPeerNicknames()[recipientHexID] != nil {
|
||||
SecureLogger.log("📡 Sending \(action) notification via Bluetooth mesh",
|
||||
category: SecureLogger.session, level: .info)
|
||||
|
||||
// Send via mesh as a system message
|
||||
meshService.sendFavoriteNotification(to: recipientHexID, isFavorite: isFavorite)
|
||||
|
||||
} else if let favoriteStatus = favoritesService.getFavoriteStatus(for: recipientNoisePublicKey),
|
||||
let recipientNostrPubkey = favoriteStatus.peerNostrPublicKey {
|
||||
|
||||
SecureLogger.log("🌐 Sending \(action) notification via Nostr to \(favoriteStatus.peerNickname)",
|
||||
category: SecureLogger.session, level: .info)
|
||||
|
||||
// Send via Nostr as a special message
|
||||
guard let senderIdentity = try? NostrIdentityBridge.getCurrentNostrIdentity() else {
|
||||
throw MessageRouterError.noNostrIdentity
|
||||
}
|
||||
|
||||
// Include our npub in the content
|
||||
let content = isFavorite ? "FAVORITED:\(senderIdentity.npub)" : "UNFAVORITED:\(senderIdentity.npub)"
|
||||
let event = try NostrProtocol.createPrivateMessage(
|
||||
content: content,
|
||||
recipientPubkey: recipientNostrPubkey,
|
||||
senderIdentity: senderIdentity
|
||||
)
|
||||
|
||||
nostrRelay.sendEvent(event)
|
||||
} else {
|
||||
SecureLogger.log("⚠️ Cannot send \(action) notification - peer not reachable via mesh or Nostr",
|
||||
category: SecureLogger.session, level: .warning)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private Methods
|
||||
|
||||
private func sendViaMesh(_ message: RoutedMessage) async throws {
|
||||
// Send the message through mesh - using sendPrivateMessage for now
|
||||
let recipientHexID = message.recipientNoisePublicKey.hexEncodedString()
|
||||
if let recipientNickname = meshService.getPeerNicknames()[recipientHexID] {
|
||||
meshService.sendPrivateMessage(message.content, to: recipientHexID, recipientNickname: recipientNickname, messageID: message.id)
|
||||
}
|
||||
|
||||
// Update status
|
||||
pendingMessages[message.id]?.status = .sent
|
||||
}
|
||||
|
||||
private func sendViaNostr(_ message: RoutedMessage) async throws {
|
||||
// Get recipient's Nostr public key
|
||||
let favoriteStatus = favoritesService.getFavoriteStatus(for: message.recipientNoisePublicKey)
|
||||
|
||||
// Looking up Nostr key for recipient
|
||||
|
||||
if favoriteStatus != nil {
|
||||
// Found favorite relationship
|
||||
} else {
|
||||
SecureLogger.log("❌ No favorite relationship found",
|
||||
category: SecureLogger.session, level: .error)
|
||||
}
|
||||
|
||||
guard let favoriteStatus = favoriteStatus,
|
||||
let recipientNostrPubkey = favoriteStatus.peerNostrPublicKey else {
|
||||
throw MessageRouterError.noNostrPublicKey
|
||||
}
|
||||
|
||||
// Get sender's Nostr identity
|
||||
guard let senderIdentity = try NostrIdentityBridge.getCurrentNostrIdentity() else {
|
||||
throw MessageRouterError.noNostrIdentity
|
||||
}
|
||||
|
||||
// Create NIP-17 encrypted message with structured content
|
||||
let structuredContent = "MSG:\(message.id):\(message.content)"
|
||||
let event = try NostrProtocol.createPrivateMessage(
|
||||
content: structuredContent,
|
||||
recipientPubkey: recipientNostrPubkey,
|
||||
senderIdentity: senderIdentity
|
||||
)
|
||||
|
||||
// Created gift wrap event
|
||||
|
||||
// Send via relay
|
||||
nostrRelay.sendEvent(event)
|
||||
|
||||
// Update status
|
||||
pendingMessages[message.id]?.status = .sent
|
||||
}
|
||||
|
||||
private func setupBindings() {
|
||||
// Monitor Nostr messages
|
||||
setupNostrMessageHandling()
|
||||
|
||||
// Clean up old pending messages periodically
|
||||
Timer.publish(every: 60, on: .main, in: .common)
|
||||
.autoconnect()
|
||||
.sink { [weak self] _ in
|
||||
self?.cleanupOldMessages()
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
// Listen for app becoming active to check for messages
|
||||
NotificationCenter.default.publisher(for: .appDidBecomeActive)
|
||||
.sink { [weak self] _ in
|
||||
self?.checkForNostrMessages()
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
private func setupNostrMessageHandling() {
|
||||
guard let currentIdentity = try? NostrIdentityBridge.getCurrentNostrIdentity() else {
|
||||
SecureLogger.log("⚠️ No Nostr identity available for initial setup", category: SecureLogger.session, level: .warning)
|
||||
return
|
||||
}
|
||||
|
||||
SecureLogger.log("🚀 Setting up Nostr message handling for \(currentIdentity.npub)",
|
||||
category: SecureLogger.session, level: .info)
|
||||
|
||||
// Connect to relays if not already connected
|
||||
if !nostrRelay.isConnected {
|
||||
nostrRelay.connect()
|
||||
|
||||
// Wait for connections to establish before subscribing
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) { [weak self] in
|
||||
self?.subscribeToNostrMessages()
|
||||
}
|
||||
} else {
|
||||
// Already connected, subscribe immediately
|
||||
subscribeToNostrMessages()
|
||||
}
|
||||
}
|
||||
|
||||
/// Check for Nostr messages when app becomes active
|
||||
func checkForNostrMessages() {
|
||||
// Checking for Nostr messages
|
||||
|
||||
guard (try? NostrIdentityBridge.getCurrentNostrIdentity()) != nil else {
|
||||
SecureLogger.log("⚠️ No Nostr identity available for message check", category: SecureLogger.session, level: .warning)
|
||||
return
|
||||
}
|
||||
|
||||
// Ensure we're connected to relays first
|
||||
if !nostrRelay.isConnected {
|
||||
// Connecting to Nostr relays
|
||||
nostrRelay.connect()
|
||||
|
||||
// Wait a bit for connections to establish before subscribing
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { [weak self] in
|
||||
self?.subscribeToNostrMessages()
|
||||
}
|
||||
} else {
|
||||
// Already connected, subscribe immediately
|
||||
subscribeToNostrMessages()
|
||||
}
|
||||
}
|
||||
|
||||
private func subscribeToNostrMessages() {
|
||||
guard let currentIdentity = try? NostrIdentityBridge.getCurrentNostrIdentity() else { return }
|
||||
|
||||
// Subscribing to Nostr messages
|
||||
// Full pubkey recorded
|
||||
// Pubkey length verified
|
||||
|
||||
// Unsubscribe existing subscription to refresh
|
||||
nostrRelay.unsubscribe(id: "router-messages")
|
||||
|
||||
// Create a new subscription for recent messages
|
||||
let sinceDate = processedMessagesService.getSubscriptionSinceDate()
|
||||
let filter = NostrFilter.giftWrapsFor(
|
||||
pubkey: currentIdentity.publicKeyHex,
|
||||
since: sinceDate
|
||||
)
|
||||
|
||||
// Subscribing to messages since date
|
||||
|
||||
// Subscribing to gift wraps
|
||||
|
||||
nostrRelay.subscribe(filter: filter, id: "router-messages") { [weak self] event in
|
||||
// Received Nostr event
|
||||
self?.handleNostrMessage(event)
|
||||
}
|
||||
}
|
||||
|
||||
private func handleNostrMessage(_ giftWrap: NostrEvent) {
|
||||
// Check if we've already processed this event
|
||||
if processedMessagesService.isMessageProcessed(giftWrap.id) {
|
||||
// Skipping already processed event
|
||||
return
|
||||
}
|
||||
|
||||
// Attempting to decrypt gift wrap
|
||||
// Full event ID recorded
|
||||
// Event timestamp recorded
|
||||
// Event tags recorded
|
||||
|
||||
// Decrypt the message
|
||||
guard let currentIdentity = try? NostrIdentityBridge.getCurrentNostrIdentity() else {
|
||||
SecureLogger.log("❌ No current Nostr identity available",
|
||||
category: SecureLogger.session, level: .error)
|
||||
return
|
||||
}
|
||||
|
||||
// Check if this event is actually tagged for us
|
||||
let ourPubkey = currentIdentity.publicKeyHex
|
||||
let isTaggedForUs = giftWrap.tags.contains { tag in
|
||||
tag.count >= 2 && tag[0] == "p" && tag[1] == ourPubkey
|
||||
}
|
||||
|
||||
if !isTaggedForUs {
|
||||
SecureLogger.log("⚠️ Gift wrap not tagged for us! Our pubkey: \(ourPubkey.prefix(8))..., Event tags: \(giftWrap.tags)",
|
||||
category: SecureLogger.session, level: .warning)
|
||||
return
|
||||
}
|
||||
|
||||
do {
|
||||
let (content, senderPubkey) = try NostrProtocol.decryptPrivateMessage(
|
||||
giftWrap: giftWrap,
|
||||
recipientIdentity: currentIdentity
|
||||
)
|
||||
|
||||
SecureLogger.log("✅ Successfully decrypted message from \(senderPubkey.prefix(8))...: \(content)",
|
||||
category: SecureLogger.session, level: .info)
|
||||
|
||||
// Mark this event as processed to avoid duplicates on app restart
|
||||
let eventTimestamp = Date(timeIntervalSince1970: TimeInterval(giftWrap.created_at))
|
||||
processedMessagesService.markMessageAsProcessed(giftWrap.id, timestamp: eventTimestamp)
|
||||
|
||||
// Check for deduplication within current session
|
||||
let messageHash = "\(senderPubkey)-\(content)-\(giftWrap.created_at)"
|
||||
if messageDeduplication.get(messageHash) != nil {
|
||||
return // Already processed in this session
|
||||
}
|
||||
messageDeduplication.set(messageHash, value: Date())
|
||||
|
||||
// Handle special messages
|
||||
if content.hasPrefix("FAVORITED") || content.hasPrefix("UNFAVORITED") {
|
||||
let parts = content.split(separator: ":")
|
||||
let isFavorite = parts.first == "FAVORITED"
|
||||
let nostrNpub = parts.count > 1 ? String(parts[1]) : nil
|
||||
handleFavoriteNotification(from: senderPubkey, isFavorite: isFavorite, nostrNpub: nostrNpub)
|
||||
return
|
||||
}
|
||||
|
||||
// Handle delivery acknowledgments
|
||||
if content.hasPrefix("DELIVERED:") {
|
||||
let parts = content.split(separator: ":")
|
||||
if parts.count > 1 {
|
||||
let messageId = String(parts[1])
|
||||
handleDeliveryAcknowledgment(messageId: messageId, from: senderPubkey)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Handle read receipts
|
||||
if content.hasPrefix("READ:") {
|
||||
let parts = content.split(separator: ":", maxSplits: 1)
|
||||
if parts.count > 1 {
|
||||
let receiptDataString = String(parts[1])
|
||||
if let receiptData = Data(base64Encoded: receiptDataString),
|
||||
let receipt = ReadReceipt.fromBinaryData(receiptData) {
|
||||
handleReadReceipt(receipt, from: senderPubkey)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Find the sender's Noise public key
|
||||
guard let senderNoiseKey = findNoisePublicKey(for: senderPubkey) else { return }
|
||||
|
||||
// Parse structured message content
|
||||
var messageId = UUID().uuidString
|
||||
var messageContent = content
|
||||
|
||||
if content.hasPrefix("MSG:") {
|
||||
let parts = content.split(separator: ":", maxSplits: 2)
|
||||
if parts.count >= 3 {
|
||||
messageId = String(parts[1])
|
||||
messageContent = String(parts[2])
|
||||
}
|
||||
}
|
||||
|
||||
// Create a BitchatMessage and inject into the stream
|
||||
let chatMessage = BitchatMessage(
|
||||
id: messageId,
|
||||
sender: favoritesService.getFavoriteStatus(for: senderNoiseKey)?.peerNickname ?? "Unknown",
|
||||
content: messageContent,
|
||||
timestamp: Date(timeIntervalSince1970: TimeInterval(giftWrap.created_at)),
|
||||
isRelay: false,
|
||||
originalSender: nil,
|
||||
isPrivate: true,
|
||||
recipientNickname: nil,
|
||||
senderPeerID: senderNoiseKey.hexEncodedString(),
|
||||
mentions: nil,
|
||||
deliveryStatus: .delivered(to: "nostr", at: Date())
|
||||
)
|
||||
|
||||
// Post notification for ChatViewModel to handle
|
||||
NotificationCenter.default.post(
|
||||
name: .nostrMessageReceived,
|
||||
object: nil,
|
||||
userInfo: ["message": chatMessage]
|
||||
)
|
||||
|
||||
// Send delivery acknowledgment back to sender
|
||||
sendDeliveryAcknowledgment(for: chatMessage.id, to: senderPubkey)
|
||||
|
||||
} catch {
|
||||
SecureLogger.log("❌ Failed to decrypt gift wrap: \(error)",
|
||||
category: SecureLogger.session, level: .error)
|
||||
}
|
||||
}
|
||||
|
||||
private func handleFavoriteNotification(from nostrPubkey: String, isFavorite: Bool, nostrNpub: String? = nil) {
|
||||
// Find the sender's Noise public key
|
||||
guard let senderNoiseKey = findNoisePublicKey(for: nostrPubkey) else { return }
|
||||
|
||||
// Update favorites service - nostrPubkey is already the hex public key
|
||||
favoritesService.updatePeerFavoritedUs(
|
||||
peerNoisePublicKey: senderNoiseKey,
|
||||
favorited: isFavorite,
|
||||
peerNostrPublicKey: nostrPubkey
|
||||
)
|
||||
|
||||
// Post notification for UI update
|
||||
NotificationCenter.default.post(
|
||||
name: .favoriteStatusChanged,
|
||||
object: nil,
|
||||
userInfo: [
|
||||
"peerPublicKey": senderNoiseKey,
|
||||
"isFavorite": isFavorite
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
private func findNoisePublicKey(for nostrPubkey: String) -> Data? {
|
||||
// Search through favorites for matching Nostr pubkey
|
||||
for (noiseKey, relationship) in favoritesService.favorites {
|
||||
if relationship.peerNostrPublicKey == nostrPubkey {
|
||||
return noiseKey
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
private func handleDeliveryAcknowledgment(messageId: String, from senderPubkey: String) {
|
||||
SecureLogger.log("✅ Received delivery acknowledgment for message \(messageId) from \(senderPubkey)",
|
||||
category: SecureLogger.session, level: .info)
|
||||
|
||||
// Find the sender's Noise public key
|
||||
guard let senderNoiseKey = findNoisePublicKey(for: senderPubkey) else { return }
|
||||
|
||||
// Post notification for ChatViewModel to update delivery status
|
||||
NotificationCenter.default.post(
|
||||
name: .messageDeliveryAcknowledged,
|
||||
object: nil,
|
||||
userInfo: [
|
||||
"messageId": messageId,
|
||||
"senderNoiseKey": senderNoiseKey
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
private func handleReadReceipt(_ receipt: ReadReceipt, from senderPubkey: String) {
|
||||
SecureLogger.log("📖 Received read receipt for message \(receipt.originalMessageID) from \(senderPubkey)",
|
||||
category: SecureLogger.session, level: .info)
|
||||
|
||||
// Find the sender's Noise public key
|
||||
guard let senderNoiseKey = findNoisePublicKey(for: senderPubkey) else { return }
|
||||
let senderHexID = senderNoiseKey.hexEncodedString()
|
||||
|
||||
// Update the receipt with the correct sender ID
|
||||
var updatedReceipt = receipt
|
||||
updatedReceipt.readerID = senderHexID
|
||||
|
||||
// Post notification for ChatViewModel to process
|
||||
NotificationCenter.default.post(
|
||||
name: .readReceiptReceived,
|
||||
object: nil,
|
||||
userInfo: ["receipt": updatedReceipt]
|
||||
)
|
||||
}
|
||||
|
||||
func sendReadReceipt(
|
||||
for originalMessageID: String,
|
||||
to recipientNoisePublicKey: Data,
|
||||
preferredTransport: Transport? = nil
|
||||
) async throws {
|
||||
SecureLogger.log("📖 Sending read receipt for message \(originalMessageID)",
|
||||
category: SecureLogger.session, level: .info)
|
||||
|
||||
// Get nickname from delegate or use default
|
||||
let nickname = (meshService.delegate as? ChatViewModel)?.nickname ?? "Anonymous"
|
||||
|
||||
// Create read receipt
|
||||
let receipt = ReadReceipt(
|
||||
originalMessageID: originalMessageID,
|
||||
readerID: meshService.myPeerID,
|
||||
readerNickname: nickname
|
||||
)
|
||||
|
||||
// Encode receipt
|
||||
let receiptData = receipt.toBinaryData()
|
||||
let content = "READ:\(receiptData.base64EncodedString())"
|
||||
|
||||
// Check if peer is connected via mesh (mesh takes precedence)
|
||||
let recipientHexID = recipientNoisePublicKey.hexEncodedString()
|
||||
|
||||
// First check if the peer is currently connected with the given ID
|
||||
var actualRecipientHexID = recipientHexID
|
||||
var actualRecipientNoiseKey = recipientNoisePublicKey
|
||||
|
||||
// Always check if they reconnected with a new ID, even if preferredTransport is specified
|
||||
if let favoriteStatus = favoritesService.getFavoriteStatus(for: recipientNoisePublicKey) {
|
||||
let peerNickname = favoriteStatus.peerNickname
|
||||
|
||||
// Search through all current peers to find one with the same nickname
|
||||
for (currentPeerID, currentNickname) in meshService.getPeerNicknames() {
|
||||
if currentNickname == peerNickname,
|
||||
currentPeerID != recipientHexID,
|
||||
let currentNoiseKey = Data(hexString: currentPeerID) {
|
||||
SecureLogger.log("🔄 Found updated peer ID for \(peerNickname): \(recipientHexID) -> \(currentPeerID)",
|
||||
category: SecureLogger.session, level: .info)
|
||||
actualRecipientHexID = currentPeerID
|
||||
actualRecipientNoiseKey = currentNoiseKey
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// If still not found in connected peers, check all favorites for the current key
|
||||
if meshService.getPeerNicknames()[actualRecipientHexID] == nil {
|
||||
// Search through all favorites to find the current noise key for this nickname
|
||||
for (noiseKey, relationship) in favoritesService.favorites {
|
||||
if relationship.peerNickname == peerNickname && relationship.peerNostrPublicKey != nil {
|
||||
SecureLogger.log("🔄 Using current favorite key for \(peerNickname): \(recipientHexID) -> \(noiseKey.hexEncodedString())",
|
||||
category: SecureLogger.session, level: .info)
|
||||
actualRecipientHexID = noiseKey.hexEncodedString()
|
||||
actualRecipientNoiseKey = noiseKey
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let isConnectedOnMesh = meshService.isPeerConnected(actualRecipientHexID)
|
||||
|
||||
if isConnectedOnMesh && preferredTransport != .nostr {
|
||||
// Send via mesh
|
||||
SecureLogger.log("📡 Sending read receipt via mesh to \(actualRecipientHexID)",
|
||||
category: SecureLogger.session, level: .debug)
|
||||
meshService.sendReadReceipt(receipt, to: actualRecipientHexID)
|
||||
} else {
|
||||
// Send via Nostr
|
||||
SecureLogger.log("🌐 Sending read receipt via Nostr to \(actualRecipientHexID)",
|
||||
category: SecureLogger.session, level: .debug)
|
||||
|
||||
// Get recipient's Nostr public key using the actual current noise key
|
||||
let favoriteStatus = favoritesService.getFavoriteStatus(for: actualRecipientNoiseKey)
|
||||
guard let recipientNostrPubkey = favoriteStatus?.peerNostrPublicKey else {
|
||||
SecureLogger.log("❌ Cannot send read receipt - no Nostr key for recipient",
|
||||
category: SecureLogger.session, level: .error)
|
||||
throw MessageRouterError.noNostrKey
|
||||
}
|
||||
|
||||
guard let senderIdentity = try? NostrIdentityBridge.getCurrentNostrIdentity() else {
|
||||
SecureLogger.log("⚠️ No Nostr identity available for read receipt",
|
||||
category: SecureLogger.session, level: .warning)
|
||||
throw MessageRouterError.noIdentity
|
||||
}
|
||||
|
||||
// Create read receipt message
|
||||
guard let event = try? NostrProtocol.createPrivateMessage(
|
||||
content: content,
|
||||
recipientPubkey: recipientNostrPubkey,
|
||||
senderIdentity: senderIdentity
|
||||
) else {
|
||||
SecureLogger.log("❌ Failed to create read receipt",
|
||||
category: SecureLogger.session, level: .error)
|
||||
throw MessageRouterError.encryptionFailed
|
||||
}
|
||||
|
||||
// Send via relay
|
||||
nostrRelay.sendEvent(event)
|
||||
}
|
||||
}
|
||||
|
||||
private func sendDeliveryAcknowledgment(for messageId: String, to recipientNostrPubkey: String) {
|
||||
SecureLogger.log("📤 Sending delivery acknowledgment for message \(messageId)",
|
||||
category: SecureLogger.session, level: .debug)
|
||||
|
||||
guard let senderIdentity = try? NostrIdentityBridge.getCurrentNostrIdentity() else {
|
||||
SecureLogger.log("⚠️ No Nostr identity available for acknowledgment",
|
||||
category: SecureLogger.session, level: .warning)
|
||||
return
|
||||
}
|
||||
|
||||
// Create acknowledgment message
|
||||
let content = "DELIVERED:\(messageId)"
|
||||
guard let event = try? NostrProtocol.createPrivateMessage(
|
||||
content: content,
|
||||
recipientPubkey: recipientNostrPubkey,
|
||||
senderIdentity: senderIdentity
|
||||
) else {
|
||||
SecureLogger.log("❌ Failed to create delivery acknowledgment",
|
||||
category: SecureLogger.session, level: .error)
|
||||
return
|
||||
}
|
||||
|
||||
// Send via relay
|
||||
nostrRelay.sendEvent(event)
|
||||
}
|
||||
|
||||
private func cleanupOldMessages() {
|
||||
let cutoff = Date().addingTimeInterval(-300) // 5 minutes
|
||||
pendingMessages = pendingMessages.filter { $0.value.timestamp > cutoff }
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Errors
|
||||
|
||||
enum MessageRouterError: LocalizedError {
|
||||
case peerNotReachable
|
||||
case noNostrPublicKey
|
||||
case noNostrIdentity
|
||||
case transportFailed
|
||||
case noNostrKey
|
||||
case noIdentity
|
||||
case encryptionFailed
|
||||
|
||||
var errorDescription: String? {
|
||||
switch self {
|
||||
case .peerNotReachable:
|
||||
return "Peer is not reachable via mesh or Nostr"
|
||||
case .noNostrPublicKey:
|
||||
return "Peer's Nostr public key is unknown"
|
||||
case .noNostrIdentity:
|
||||
return "No Nostr identity available"
|
||||
case .transportFailed:
|
||||
return "Failed to send message"
|
||||
case .noNostrKey:
|
||||
return "No Nostr key available for recipient"
|
||||
case .noIdentity:
|
||||
return "No identity available"
|
||||
case .encryptionFailed:
|
||||
return "Failed to encrypt message"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Notification Names
|
||||
|
||||
extension Notification.Name {
|
||||
static let nostrMessageReceived = Notification.Name("NostrMessageReceived")
|
||||
static let messageDeliveryAcknowledged = Notification.Name("MessageDeliveryAcknowledged")
|
||||
static let readReceiptReceived = Notification.Name("ReadReceiptReceived")
|
||||
static let appDidBecomeActive = Notification.Name("AppDidBecomeActive")
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ enum EncryptionStatus: Equatable {
|
||||
case .noiseSecured:
|
||||
return "lock.fill" // Changed from "lock" to "lock.fill" for filled lock
|
||||
case .noiseVerified:
|
||||
return "lock.shield.fill" // Changed to filled version for consistency
|
||||
return "checkmark.seal.fill" // Verified badge
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
import Foundation
|
||||
|
||||
/// Service to track processed messages across app restarts to prevent duplicates
|
||||
@MainActor
|
||||
final class ProcessedMessagesService {
|
||||
static let shared = ProcessedMessagesService()
|
||||
|
||||
private let userDefaults = UserDefaults.standard
|
||||
private let processedMessagesKey = "ProcessedNostrMessages"
|
||||
private let lastProcessedTimestampKey = "LastProcessedNostrTimestamp"
|
||||
private let maxStoredMessages = 1000 // Keep last 1000 message IDs
|
||||
|
||||
private var processedMessageIDs: Set<String> = []
|
||||
private var lastProcessedTimestamp: Date?
|
||||
|
||||
private init() {
|
||||
loadProcessedMessages()
|
||||
}
|
||||
|
||||
/// Check if a message has already been processed
|
||||
func isMessageProcessed(_ messageID: String) -> Bool {
|
||||
return processedMessageIDs.contains(messageID)
|
||||
}
|
||||
|
||||
/// Mark a message as processed
|
||||
func markMessageAsProcessed(_ messageID: String, timestamp: Date) {
|
||||
processedMessageIDs.insert(messageID)
|
||||
|
||||
// Update last processed timestamp if this message is newer
|
||||
if let lastTimestamp = lastProcessedTimestamp {
|
||||
if timestamp > lastTimestamp {
|
||||
lastProcessedTimestamp = timestamp
|
||||
}
|
||||
} else {
|
||||
lastProcessedTimestamp = timestamp
|
||||
}
|
||||
|
||||
// Trim if we have too many stored IDs
|
||||
if processedMessageIDs.count > maxStoredMessages {
|
||||
trimOldestMessages()
|
||||
}
|
||||
|
||||
saveProcessedMessages()
|
||||
}
|
||||
|
||||
/// Get the timestamp to use for Nostr subscription filters
|
||||
func getSubscriptionSinceDate() -> Date {
|
||||
// If we have a last processed timestamp, use it minus a small buffer
|
||||
if let lastTimestamp = lastProcessedTimestamp {
|
||||
// Go back 1 hour before last processed message for safety
|
||||
return lastTimestamp.addingTimeInterval(-3600)
|
||||
}
|
||||
|
||||
// Default: look back 24 hours on first run
|
||||
return Date().addingTimeInterval(-86400)
|
||||
}
|
||||
|
||||
/// Clear all processed messages (useful for debugging)
|
||||
func clearProcessedMessages() {
|
||||
processedMessageIDs.removeAll()
|
||||
lastProcessedTimestamp = nil
|
||||
saveProcessedMessages()
|
||||
}
|
||||
|
||||
// MARK: - Private Methods
|
||||
|
||||
private func loadProcessedMessages() {
|
||||
if let data = userDefaults.data(forKey: processedMessagesKey),
|
||||
let decoded = try? JSONDecoder().decode([String].self, from: data) {
|
||||
processedMessageIDs = Set(decoded)
|
||||
}
|
||||
|
||||
if let timestampInterval = userDefaults.object(forKey: lastProcessedTimestampKey) as? TimeInterval {
|
||||
lastProcessedTimestamp = Date(timeIntervalSince1970: timestampInterval)
|
||||
}
|
||||
|
||||
SecureLogger.log("📋 Loaded \(processedMessageIDs.count) processed message IDs, last timestamp: \(lastProcessedTimestamp?.description ?? "nil")",
|
||||
category: SecureLogger.session, level: .info)
|
||||
}
|
||||
|
||||
private func saveProcessedMessages() {
|
||||
// Convert Set to Array for encoding
|
||||
let messageArray = Array(processedMessageIDs)
|
||||
if let encoded = try? JSONEncoder().encode(messageArray) {
|
||||
userDefaults.set(encoded, forKey: processedMessagesKey)
|
||||
}
|
||||
|
||||
if let timestamp = lastProcessedTimestamp {
|
||||
userDefaults.set(timestamp.timeIntervalSince1970, forKey: lastProcessedTimestampKey)
|
||||
}
|
||||
|
||||
userDefaults.synchronize()
|
||||
}
|
||||
|
||||
private func trimOldestMessages() {
|
||||
// Since we don't track insertion order, we'll just keep the most recent N messages
|
||||
// In a production app, you might want to track timestamps for each message
|
||||
let excess = processedMessageIDs.count - maxStoredMessages
|
||||
if excess > 0 {
|
||||
// Remove random excess messages (not ideal, but simple)
|
||||
for _ in 0..<excess {
|
||||
if let first = processedMessageIDs.first {
|
||||
processedMessageIDs.remove(first)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+1001
-155
File diff suppressed because it is too large
Load Diff
@@ -26,8 +26,9 @@ struct AppInfoView: View {
|
||||
static let offlineComm = ("wifi.slash", "offline communication", "works without internet using Bluetooth low energy")
|
||||
static let encryption = ("lock.shield", "end-to-end encryption", "private messages encrypted with noise protocol")
|
||||
static let extendedRange = ("antenna.radiowaves.left.and.right", "extended range", "messages relay through peers, going the distance")
|
||||
static let favorites = ("star.fill", "favorites", "get notified when your favorite people join")
|
||||
static let mentions = ("at", "mentions", "use @nickname to notify specific people")
|
||||
static let favorites = ("star.fill", "favorites", "get notified when your favorite people join")
|
||||
static let mutualFavorites = ("globe", "mutual favorites", "private message each other via nostr when out of mesh range")
|
||||
}
|
||||
|
||||
enum Privacy {
|
||||
@@ -130,6 +131,10 @@ struct AppInfoView: View {
|
||||
title: Strings.Features.favorites.1,
|
||||
description: Strings.Features.favorites.2)
|
||||
|
||||
FeatureRow(icon: Strings.Features.mutualFavorites.0,
|
||||
title: Strings.Features.mutualFavorites.1,
|
||||
description: Strings.Features.mutualFavorites.2)
|
||||
|
||||
FeatureRow(icon: Strings.Features.mentions.0,
|
||||
title: Strings.Features.mentions.1,
|
||||
description: Strings.Features.mentions.2)
|
||||
|
||||
+190
-87
@@ -14,11 +14,12 @@ import SwiftUI
|
||||
struct PeerDisplayData: Identifiable {
|
||||
let id: String
|
||||
let displayName: String
|
||||
let rssi: Int?
|
||||
let isFavorite: Bool
|
||||
let isMe: Bool
|
||||
let hasUnreadMessages: Bool
|
||||
let encryptionStatus: EncryptionStatus
|
||||
let connectionState: BitchatPeer.ConnectionState
|
||||
let isMutualFavorite: Bool
|
||||
}
|
||||
|
||||
// MARK: - Lazy Link Preview
|
||||
@@ -593,11 +594,11 @@ struct ContentView: View {
|
||||
|
||||
// Rooms and People list
|
||||
ScrollView {
|
||||
VStack(alignment: .leading, spacing: 12) {
|
||||
VStack(alignment: .leading, spacing: 6) {
|
||||
// People section
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
// Show appropriate header based on context
|
||||
if !viewModel.connectedPeers.isEmpty {
|
||||
if !viewModel.allPeers.isEmpty {
|
||||
HStack(spacing: 4) {
|
||||
Image(systemName: "person.2.fill")
|
||||
.font(.system(size: 10))
|
||||
@@ -607,40 +608,33 @@ struct ContentView: View {
|
||||
}
|
||||
.foregroundColor(secondaryTextColor)
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.top, 12)
|
||||
}
|
||||
|
||||
if viewModel.connectedPeers.isEmpty {
|
||||
if viewModel.allPeers.isEmpty {
|
||||
Text("nobody around...")
|
||||
.font(.system(size: 14, design: .monospaced))
|
||||
.foregroundColor(secondaryTextColor)
|
||||
.padding(.horizontal)
|
||||
.padding(.top, 12)
|
||||
} else {
|
||||
// Extract peer data for display
|
||||
let peerNicknames = viewModel.meshService.getPeerNicknames()
|
||||
let peerRSSI = viewModel.meshService.getPeerRSSI()
|
||||
let myPeerID = viewModel.meshService.myPeerID
|
||||
|
||||
// Show all connected peers
|
||||
let peersToShow: [String] = viewModel.connectedPeers
|
||||
let _ = print("ContentView: Showing \(peersToShow.count) peers: \(peersToShow.joined(separator: ", "))")
|
||||
|
||||
// Show all peers (connected and favorites)
|
||||
// Pre-compute peer data outside ForEach to reduce overhead
|
||||
let peerData = peersToShow.map { peerID in
|
||||
let rssiValue = peerRSSI[peerID]?.intValue
|
||||
if rssiValue == nil {
|
||||
print("ContentView: No RSSI for peer \(peerID) in dictionary with \(peerRSSI.count) entries")
|
||||
print("ContentView: peerRSSI keys: \(peerRSSI.keys.joined(separator: ", "))")
|
||||
} else {
|
||||
print("ContentView: RSSI for peer \(peerID) is \(rssiValue!)")
|
||||
}
|
||||
let peerData = viewModel.allPeers.map { peer in
|
||||
// Get current myPeerID for each peer to avoid stale values
|
||||
let currentMyPeerID = viewModel.meshService.myPeerID
|
||||
return PeerDisplayData(
|
||||
id: peerID,
|
||||
displayName: peerID == myPeerID ? viewModel.nickname : (peerNicknames[peerID] ?? "anon\(peerID.prefix(4))"),
|
||||
rssi: rssiValue,
|
||||
isFavorite: viewModel.isFavorite(peerID: peerID),
|
||||
isMe: peerID == myPeerID,
|
||||
hasUnreadMessages: viewModel.unreadPrivateMessages.contains(peerID),
|
||||
encryptionStatus: viewModel.getEncryptionStatus(for: peerID)
|
||||
id: peer.id,
|
||||
displayName: peer.id == currentMyPeerID ? viewModel.nickname : peer.nickname,
|
||||
isFavorite: peer.favoriteStatus?.isFavorite ?? false,
|
||||
isMe: peer.id == currentMyPeerID,
|
||||
hasUnreadMessages: viewModel.unreadPrivateMessages.contains(peer.id),
|
||||
encryptionStatus: viewModel.getEncryptionStatus(for: peer.id),
|
||||
connectionState: peer.connectionState,
|
||||
isMutualFavorite: peer.favoriteStatus?.isMutual ?? false
|
||||
)
|
||||
}.sorted { (peer1: PeerDisplayData, peer2: PeerDisplayData) in
|
||||
// Sort: favorites first, then alphabetically by nickname
|
||||
@@ -651,7 +645,7 @@ struct ContentView: View {
|
||||
}
|
||||
|
||||
ForEach(peerData) { peer in
|
||||
HStack(spacing: 8) {
|
||||
HStack(spacing: 4) {
|
||||
// Signal strength indicator or unread message icon
|
||||
if peer.isMe {
|
||||
Image(systemName: "person.fill")
|
||||
@@ -663,17 +657,42 @@ struct ContentView: View {
|
||||
.font(.system(size: 12))
|
||||
.foregroundColor(Color.orange)
|
||||
.accessibilityLabel("Unread message from \(peer.displayName)")
|
||||
} else if let rssi = peer.rssi {
|
||||
Image(systemName: "circle.fill")
|
||||
.font(.system(size: 8))
|
||||
.foregroundColor(viewModel.getRSSIColor(rssi: rssi, colorScheme: colorScheme))
|
||||
.accessibilityLabel("Signal strength: \(rssi > -60 ? "excellent" : rssi > -70 ? "good" : rssi > -80 ? "fair" : "poor")")
|
||||
} else {
|
||||
// No RSSI data available
|
||||
Image(systemName: "circle")
|
||||
.font(.system(size: 8))
|
||||
.foregroundColor(Color.secondary.opacity(0.5))
|
||||
.accessibilityLabel("Signal strength: unknown")
|
||||
// Connection state indicator
|
||||
switch peer.connectionState {
|
||||
case .bluetoothConnected:
|
||||
// Radio icon for mesh connection
|
||||
Image(systemName: "dot.radiowaves.left.and.right")
|
||||
.font(.system(size: 10))
|
||||
.foregroundColor(textColor)
|
||||
.accessibilityLabel("Connected via mesh")
|
||||
case .relayConnected:
|
||||
// Chain link for relay connection
|
||||
Image(systemName: "link")
|
||||
.font(.system(size: 10))
|
||||
.foregroundColor(Color.blue)
|
||||
.accessibilityLabel("Connected via relay")
|
||||
case .nostrAvailable:
|
||||
// Purple globe for mutual favorites reachable via Nostr
|
||||
Image(systemName: "globe")
|
||||
.font(.system(size: 10))
|
||||
.foregroundColor(.purple)
|
||||
.accessibilityLabel("Available via Nostr")
|
||||
case .offline:
|
||||
if peer.isFavorite {
|
||||
// Crescent moon for non-mutual favorites
|
||||
Image(systemName: "moon.fill")
|
||||
.font(.system(size: 10))
|
||||
.foregroundColor(Color.secondary.opacity(0.5))
|
||||
.accessibilityLabel("Favorite - Offline")
|
||||
} else {
|
||||
// Offline indicator for non-favorites (shouldn't happen since we only show favorites when offline)
|
||||
Image(systemName: "circle")
|
||||
.font(.system(size: 8))
|
||||
.foregroundColor(Color.secondary.opacity(0.3))
|
||||
.accessibilityLabel("Offline")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Peer name
|
||||
@@ -688,13 +707,13 @@ struct ContentView: View {
|
||||
} else {
|
||||
Text(peer.displayName)
|
||||
.font(.system(size: 14, design: .monospaced))
|
||||
.foregroundColor(peerNicknames[peer.id] != nil ? textColor : secondaryTextColor)
|
||||
.foregroundColor(peer.isFavorite || peerNicknames[peer.id] != nil ? textColor : secondaryTextColor)
|
||||
|
||||
// Encryption status icon (after peer name)
|
||||
if let icon = peer.encryptionStatus.icon {
|
||||
Image(systemName: icon)
|
||||
.font(.system(size: 10))
|
||||
.foregroundColor(peer.encryptionStatus == .noiseVerified ? Color.green :
|
||||
.foregroundColor(peer.encryptionStatus == .noiseVerified ? textColor :
|
||||
peer.encryptionStatus == .noiseSecured ? textColor :
|
||||
peer.encryptionStatus == .noiseHandshaking ? Color.orange :
|
||||
Color.red)
|
||||
@@ -716,10 +735,11 @@ struct ContentView: View {
|
||||
}
|
||||
}
|
||||
.padding(.horizontal)
|
||||
.padding(.vertical, 8)
|
||||
.padding(.vertical, 4)
|
||||
.contentShape(Rectangle())
|
||||
.onTapGesture {
|
||||
if !peer.isMe && peerNicknames[peer.id] != nil {
|
||||
if !peer.isMe {
|
||||
// Allow tapping on any peer (connected or offline favorite)
|
||||
viewModel.startPrivateChat(with: peer.id)
|
||||
withAnimation(.easeInOut(duration: 0.2)) {
|
||||
showSidebar = false
|
||||
@@ -737,7 +757,7 @@ struct ContentView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.vertical, 8)
|
||||
.id(viewModel.allPeers.map { "\($0.id)-\($0.isConnected)" }.joined())
|
||||
}
|
||||
|
||||
Spacer()
|
||||
@@ -863,18 +883,35 @@ struct ContentView: View {
|
||||
.accessibilityLabel("Unread private messages")
|
||||
}
|
||||
|
||||
let otherPeersCount = viewModel.connectedPeers.filter { $0 != viewModel.meshService.myPeerID }.count
|
||||
// Single pass to count both metrics
|
||||
let peerCounts = viewModel.allPeers.reduce(into: (others: 0, mesh: 0)) { counts, peer in
|
||||
guard peer.id != viewModel.meshService.myPeerID else { return }
|
||||
|
||||
let isMeshConnected = peer.isConnected || peer.isRelayConnected
|
||||
if isMeshConnected {
|
||||
counts.mesh += 1
|
||||
counts.others += 1
|
||||
} else if peer.isMutualFavorite {
|
||||
counts.others += 1
|
||||
}
|
||||
}
|
||||
|
||||
let otherPeersCount = peerCounts.others
|
||||
let meshPeerCount = peerCounts.mesh
|
||||
|
||||
// Purple only if we have peers but none are reachable via mesh (only via Nostr)
|
||||
let isNostrOnly = otherPeersCount > 0 && meshPeerCount == 0
|
||||
|
||||
HStack(spacing: 4) {
|
||||
// People icon with count
|
||||
Image(systemName: "person.2.fill")
|
||||
.font(.system(size: 11))
|
||||
.accessibilityLabel("\(otherPeersCount) connected \(otherPeersCount == 1 ? "person" : "people")")
|
||||
.accessibilityLabel("\(otherPeersCount) \(otherPeersCount == 1 ? "person" : "people")")
|
||||
Text("\(otherPeersCount)")
|
||||
.font(.system(size: 12, design: .monospaced))
|
||||
.accessibilityHidden(true)
|
||||
}
|
||||
.foregroundColor(viewModel.isConnected ? textColor : Color.red)
|
||||
.foregroundColor(isNostrOnly ? Color.purple : (meshPeerCount > 0 ? textColor : Color.red))
|
||||
}
|
||||
.onTapGesture {
|
||||
withAnimation(.easeInOut(duration: 0.2)) {
|
||||
@@ -890,73 +927,139 @@ struct ContentView: View {
|
||||
|
||||
private var privateHeaderView: some View {
|
||||
Group {
|
||||
if let privatePeerID = viewModel.selectedPrivateChatPeer,
|
||||
let privatePeerNick = viewModel.meshService.getPeerNicknames()[privatePeerID] {
|
||||
HStack {
|
||||
Button(action: {
|
||||
withAnimation(.easeInOut(duration: 0.2)) {
|
||||
showPrivateChat = false
|
||||
viewModel.endPrivateChat()
|
||||
}
|
||||
}) {
|
||||
HStack(spacing: 4) {
|
||||
Image(systemName: "chevron.left")
|
||||
.font(.system(size: 12))
|
||||
Text("back")
|
||||
.font(.system(size: 14, design: .monospaced))
|
||||
}
|
||||
.foregroundColor(textColor)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.accessibilityLabel("Back to main chat")
|
||||
|
||||
Spacer()
|
||||
|
||||
if let privatePeerID = viewModel.selectedPrivateChatPeer {
|
||||
privateHeaderContent(for: privatePeerID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private func privateHeaderContent(for privatePeerID: String) -> some View {
|
||||
// Try to resolve to current peer ID if this is an old one
|
||||
// resolveCurrentPeerID not implemented
|
||||
let currentPeerID: String = privatePeerID
|
||||
|
||||
let peer = viewModel.getPeer(byID: currentPeerID)
|
||||
let privatePeerNick = peer?.displayName ??
|
||||
viewModel.meshService.getPeerNicknames()[currentPeerID] ??
|
||||
FavoritesPersistenceService.shared.getFavoriteStatus(for: Data(hexString: privatePeerID) ?? Data())?.peerNickname ??
|
||||
// getFavoriteStatusByNostrKey not implemented
|
||||
// FavoritesPersistenceService.shared.getFavoriteStatusByNostrKey(privatePeerID)?.peerNickname ??
|
||||
"Unknown"
|
||||
let isNostrAvailable: Bool = {
|
||||
guard let connectionState = peer?.connectionState else {
|
||||
// Check if we can reach this peer via Nostr even if not in allPeers
|
||||
if let noiseKey = Data(hexString: privatePeerID),
|
||||
let favoriteStatus = FavoritesPersistenceService.shared.getFavoriteStatus(for: noiseKey),
|
||||
favoriteStatus.isMutual {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
switch connectionState {
|
||||
case .nostrAvailable:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}()
|
||||
|
||||
ZStack {
|
||||
// Center content - always perfectly centered
|
||||
Button(action: {
|
||||
viewModel.showFingerprint(for: privatePeerID)
|
||||
}) {
|
||||
HStack(spacing: 6) {
|
||||
// Show transport icon based on connection state (like peer list)
|
||||
if let connectionState = peer?.connectionState {
|
||||
switch connectionState {
|
||||
case .bluetoothConnected:
|
||||
// Radio icon for mesh connection
|
||||
Image(systemName: "dot.radiowaves.left.and.right")
|
||||
.font(.system(size: 14))
|
||||
.foregroundColor(textColor)
|
||||
.accessibilityLabel("Connected via mesh")
|
||||
case .relayConnected:
|
||||
// Chain link for relay connection
|
||||
Image(systemName: "link")
|
||||
.font(.system(size: 14))
|
||||
.foregroundColor(Color.blue)
|
||||
.accessibilityLabel("Connected via relay")
|
||||
case .nostrAvailable:
|
||||
// Purple globe for Nostr
|
||||
Image(systemName: "globe")
|
||||
.font(.system(size: 14))
|
||||
.foregroundColor(.purple)
|
||||
.accessibilityLabel("Available via Nostr")
|
||||
case .offline:
|
||||
// Should not happen for PM header, but handle gracefully
|
||||
EmptyView()
|
||||
}
|
||||
} else if isNostrAvailable {
|
||||
// Fallback to Nostr if peer not in list but is mutual favorite
|
||||
Image(systemName: "globe")
|
||||
.font(.system(size: 14))
|
||||
.foregroundColor(.purple)
|
||||
.accessibilityLabel("Available via Nostr")
|
||||
}
|
||||
|
||||
Text("\(privatePeerNick)")
|
||||
.font(.system(size: 16, weight: .medium, design: .monospaced))
|
||||
.foregroundColor(Color.orange)
|
||||
.foregroundColor(textColor)
|
||||
|
||||
// Dynamic encryption status icon
|
||||
let encryptionStatus = viewModel.getEncryptionStatus(for: privatePeerID)
|
||||
if let icon = encryptionStatus.icon {
|
||||
Image(systemName: icon)
|
||||
.font(.system(size: 14))
|
||||
.foregroundColor(encryptionStatus == .noiseVerified ? Color.green :
|
||||
encryptionStatus == .noiseSecured ? Color.orange :
|
||||
.foregroundColor(encryptionStatus == .noiseVerified ? textColor :
|
||||
encryptionStatus == .noiseSecured ? textColor :
|
||||
Color.red)
|
||||
.accessibilityLabel("Encryption status: \(encryptionStatus == .noiseVerified ? "verified" : encryptionStatus == .noiseSecured ? "secured" : "not encrypted")")
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
.accessibilityLabel("Private chat with \(privatePeerNick)")
|
||||
.accessibilityHint("Tap to view encryption fingerprint")
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
|
||||
Spacer()
|
||||
|
||||
// Favorite button
|
||||
Button(action: {
|
||||
viewModel.toggleFavorite(peerID: privatePeerID)
|
||||
}) {
|
||||
Image(systemName: viewModel.isFavorite(peerID: privatePeerID) ? "star.fill" : "star")
|
||||
.font(.system(size: 16))
|
||||
.foregroundColor(viewModel.isFavorite(peerID: privatePeerID) ? Color.yellow : textColor)
|
||||
// Left and right buttons positioned with HStack
|
||||
HStack {
|
||||
Button(action: {
|
||||
withAnimation(.easeInOut(duration: 0.2)) {
|
||||
showPrivateChat = false
|
||||
viewModel.endPrivateChat()
|
||||
}
|
||||
}) {
|
||||
HStack(spacing: 4) {
|
||||
Image(systemName: "chevron.left")
|
||||
.font(.system(size: 12))
|
||||
Text("back")
|
||||
.font(.system(size: 14, design: .monospaced))
|
||||
}
|
||||
.foregroundColor(textColor)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.accessibilityLabel("Back to main chat")
|
||||
|
||||
Spacer()
|
||||
|
||||
// Favorite button
|
||||
Button(action: {
|
||||
viewModel.toggleFavorite(peerID: privatePeerID)
|
||||
}) {
|
||||
Image(systemName: viewModel.isFavorite(peerID: privatePeerID) ? "star.fill" : "star")
|
||||
.font(.system(size: 16))
|
||||
.foregroundColor(viewModel.isFavorite(peerID: privatePeerID) ? Color.yellow : textColor)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.accessibilityLabel(viewModel.isFavorite(peerID: privatePeerID) ? "Remove from favorites" : "Add to favorites")
|
||||
.accessibilityHint("Double tap to toggle favorite status")
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.accessibilityLabel(viewModel.isFavorite(peerID: privatePeerID) ? "Remove from favorites" : "Add to favorites")
|
||||
.accessibilityHint("Double tap to toggle favorite status")
|
||||
}
|
||||
.frame(height: 44)
|
||||
.padding(.horizontal, 12)
|
||||
.background(backgroundColor.opacity(0.95))
|
||||
} else {
|
||||
EmptyView()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,5 +10,7 @@
|
||||
</array>
|
||||
<key>com.apple.security.device.bluetooth</key>
|
||||
<true/>
|
||||
<key>com.apple.security.network.client</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -365,8 +365,7 @@ final class PrivateChatE2ETests: XCTestCase {
|
||||
timestamp: packet.timestamp,
|
||||
payload: encrypted,
|
||||
signature: packet.signature,
|
||||
ttl: packet.ttl,
|
||||
sequenceNumber: 1
|
||||
ttl: packet.ttl
|
||||
)
|
||||
self.bob.simulateIncomingPacket(encryptedPacket)
|
||||
} catch {
|
||||
|
||||
@@ -122,8 +122,7 @@ final class PublicChatE2ETests: XCTestCase {
|
||||
timestamp: packet.timestamp,
|
||||
payload: relayPayload,
|
||||
signature: packet.signature,
|
||||
ttl: packet.ttl - 1,
|
||||
sequenceNumber: 1
|
||||
ttl: packet.ttl - 1
|
||||
)
|
||||
|
||||
// Simulate relay to Charlie
|
||||
@@ -451,8 +450,7 @@ final class PublicChatE2ETests: XCTestCase {
|
||||
timestamp: packet.timestamp,
|
||||
payload: relayPayload,
|
||||
signature: packet.signature,
|
||||
ttl: packet.ttl - 1,
|
||||
sequenceNumber: 1
|
||||
ttl: packet.ttl - 1
|
||||
)
|
||||
|
||||
// Relay to next hops
|
||||
|
||||
@@ -197,8 +197,7 @@ final class IntegrationTests: XCTestCase {
|
||||
timestamp: packet.timestamp,
|
||||
payload: encrypted,
|
||||
signature: packet.signature,
|
||||
ttl: packet.ttl,
|
||||
sequenceNumber: 1
|
||||
ttl: packet.ttl
|
||||
)
|
||||
self.nodes["Bob"]!.simulateIncomingPacket(encPacket)
|
||||
}
|
||||
@@ -699,8 +698,7 @@ final class IntegrationTests: XCTestCase {
|
||||
timestamp: packet.timestamp,
|
||||
payload: encrypted,
|
||||
signature: packet.signature,
|
||||
ttl: packet.ttl,
|
||||
sequenceNumber: 1
|
||||
ttl: packet.ttl
|
||||
)
|
||||
self.nodes["Bob"]!.simulateIncomingPacket(encPacket)
|
||||
}
|
||||
@@ -807,8 +805,7 @@ final class IntegrationTests: XCTestCase {
|
||||
timestamp: packet.timestamp,
|
||||
payload: relayPayload,
|
||||
signature: packet.signature,
|
||||
ttl: packet.ttl - 1,
|
||||
sequenceNumber: 1
|
||||
ttl: packet.ttl - 1
|
||||
)
|
||||
|
||||
for hop in nextHops {
|
||||
|
||||
@@ -73,8 +73,7 @@ class MockBluetoothMeshService: BluetoothMeshService {
|
||||
timestamp: UInt64(Date().timeIntervalSince1970 * 1000),
|
||||
payload: payload,
|
||||
signature: nil,
|
||||
ttl: 3,
|
||||
sequenceNumber: 1
|
||||
ttl: 3
|
||||
)
|
||||
|
||||
sentMessages.append((message, packet))
|
||||
@@ -112,8 +111,7 @@ class MockBluetoothMeshService: BluetoothMeshService {
|
||||
timestamp: UInt64(Date().timeIntervalSince1970 * 1000),
|
||||
payload: payload,
|
||||
signature: nil,
|
||||
ttl: 3,
|
||||
sequenceNumber: 1
|
||||
ttl: 3
|
||||
)
|
||||
|
||||
sentMessages.append((message, packet))
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
//
|
||||
// NostrProtocolTests.swift
|
||||
// bitchatTests
|
||||
//
|
||||
// Tests for NIP-17 gift-wrapped private messages
|
||||
//
|
||||
|
||||
import XCTest
|
||||
import CryptoKit
|
||||
@testable import bitchat
|
||||
|
||||
final class NostrProtocolTests: XCTestCase {
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
// Enable secure logging for tests
|
||||
SecureLogger.enableLogging()
|
||||
}
|
||||
|
||||
func testNIP17MessageRoundTrip() throws {
|
||||
// Create sender and recipient identities
|
||||
let sender = try NostrIdentity.generate()
|
||||
let recipient = try NostrIdentity.generate()
|
||||
|
||||
print("Sender pubkey: \(sender.publicKeyHex)")
|
||||
print("Recipient pubkey: \(recipient.publicKeyHex)")
|
||||
|
||||
// Create a test message
|
||||
let originalContent = "Hello from NIP-17 test!"
|
||||
|
||||
// Create encrypted gift wrap
|
||||
let giftWrap = try NostrProtocol.createPrivateMessage(
|
||||
content: originalContent,
|
||||
recipientPubkey: recipient.publicKeyHex,
|
||||
senderIdentity: sender
|
||||
)
|
||||
|
||||
print("Gift wrap created with ID: \(giftWrap.id)")
|
||||
print("Gift wrap pubkey: \(giftWrap.pubkey)")
|
||||
|
||||
// Decrypt the gift wrap
|
||||
let (decryptedContent, senderPubkey) = try NostrProtocol.decryptPrivateMessage(
|
||||
giftWrap: giftWrap,
|
||||
recipientIdentity: recipient
|
||||
)
|
||||
|
||||
// Verify
|
||||
XCTAssertEqual(decryptedContent, originalContent)
|
||||
XCTAssertEqual(senderPubkey, sender.publicKeyHex)
|
||||
|
||||
print("✅ Successfully decrypted message: '\(decryptedContent)' from \(senderPubkey)")
|
||||
}
|
||||
|
||||
func testGiftWrapUsesUniqueEphemeralKeys() throws {
|
||||
// Create identities
|
||||
let sender = try NostrIdentity.generate()
|
||||
let recipient = try NostrIdentity.generate()
|
||||
|
||||
// Create two messages
|
||||
let message1 = try NostrProtocol.createPrivateMessage(
|
||||
content: "Message 1",
|
||||
recipientPubkey: recipient.publicKeyHex,
|
||||
senderIdentity: sender
|
||||
)
|
||||
|
||||
let message2 = try NostrProtocol.createPrivateMessage(
|
||||
content: "Message 2",
|
||||
recipientPubkey: recipient.publicKeyHex,
|
||||
senderIdentity: sender
|
||||
)
|
||||
|
||||
// Gift wrap pubkeys should be different (unique ephemeral keys)
|
||||
XCTAssertNotEqual(message1.pubkey, message2.pubkey)
|
||||
print("Message 1 gift wrap pubkey: \(message1.pubkey)")
|
||||
print("Message 2 gift wrap pubkey: \(message2.pubkey)")
|
||||
|
||||
// Both should decrypt successfully
|
||||
let (content1, _) = try NostrProtocol.decryptPrivateMessage(
|
||||
giftWrap: message1,
|
||||
recipientIdentity: recipient
|
||||
)
|
||||
let (content2, _) = try NostrProtocol.decryptPrivateMessage(
|
||||
giftWrap: message2,
|
||||
recipientIdentity: recipient
|
||||
)
|
||||
|
||||
XCTAssertEqual(content1, "Message 1")
|
||||
XCTAssertEqual(content2, "Message 2")
|
||||
}
|
||||
|
||||
func testDecryptionFailsWithWrongRecipient() throws {
|
||||
let sender = try NostrIdentity.generate()
|
||||
let recipient = try NostrIdentity.generate()
|
||||
let wrongRecipient = try NostrIdentity.generate()
|
||||
|
||||
// Create message for recipient
|
||||
let giftWrap = try NostrProtocol.createPrivateMessage(
|
||||
content: "Secret message",
|
||||
recipientPubkey: recipient.publicKeyHex,
|
||||
senderIdentity: sender
|
||||
)
|
||||
|
||||
// Try to decrypt with wrong recipient
|
||||
XCTAssertThrowsError(try NostrProtocol.decryptPrivateMessage(
|
||||
giftWrap: giftWrap,
|
||||
recipientIdentity: wrongRecipient
|
||||
)) { error in
|
||||
print("Expected error when decrypting with wrong key: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -55,8 +55,7 @@ class TestHelpers {
|
||||
recipientID: String? = nil,
|
||||
payload: Data = "test payload".data(using: .utf8)!,
|
||||
signature: Data? = nil,
|
||||
ttl: UInt8 = 3,
|
||||
sequenceNumber: UInt32 = 1
|
||||
ttl: UInt8 = 3
|
||||
) -> BitchatPacket {
|
||||
return BitchatPacket(
|
||||
type: type,
|
||||
@@ -65,8 +64,7 @@ class TestHelpers {
|
||||
timestamp: UInt64(Date().timeIntervalSince1970 * 1000),
|
||||
payload: payload,
|
||||
signature: signature,
|
||||
ttl: ttl,
|
||||
sequenceNumber: sequenceNumber
|
||||
ttl: ttl
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user