Compare commits

...
Author SHA1 Message Date
jackandGitHub 0aaa8cc28f Merge branch 'main' into fix/deduplication-service-thread-safety 2026-01-04 13:56:35 -10:00
jackandGitHub bc312e4aef Merge pull request #929 from permissionlesstech/fix/nostr-transport-thread-safety
fix: add thread safety to NostrTransport read receipt queue
2026-01-04 13:56:17 -10:00
jackandGitHub 6e7509f2be Merge branch 'main' into fix/nostr-transport-thread-safety 2026-01-04 13:52:31 -10:00
jackandGitHub ca06f4d51d Merge pull request #928 from permissionlesstech/fix/noise-dh-secret-clearing
fix: clear DH shared secrets after Noise handshake operations
2026-01-04 13:52:14 -10:00
jackandGitHub 507cb19b91 Merge branch 'main' into fix/deduplication-service-thread-safety 2026-01-04 13:42:17 -10:00
jackandGitHub 04f57e8713 Merge branch 'main' into fix/nostr-transport-thread-safety 2026-01-04 13:41:53 -10:00
jackandGitHub 59c3c4e236 Merge branch 'main' into fix/noise-dh-secret-clearing 2026-01-04 13:41:38 -10:00
jackandClaude Opus 4.5 6cc3a8cde7 fix: add @MainActor to MessageDeduplicationService for thread safety
Added @MainActor annotation to both LRUDeduplicationCache and
MessageDeduplicationService classes. This provides compile-time
enforcement of thread safety since all callers (ChatViewModel,
NostrRelayManager) are already on MainActor.

Benefits:
- Compile-time enforcement prevents future misuse
- Simpler than internal locking mechanisms
- Consistent with existing patterns in codebase

Also adds:
- @MainActor annotation to existing test suites
- 5 new concurrency tests verifying thread safety behavior

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-04 13:29:39 -10:00
jackandClaude Opus 4.5 e887e04f40 fix: add thread safety to NostrTransport read receipt queue
Synchronized access to readQueue and isSendingReadAcks using the
existing concurrent DispatchQueue with barrier flags:

- sendReadReceipt(): wrap enqueue in barrier async
- processReadQueueIfNeeded(): extract item within barrier context
- scheduleNextReadAck(): wrap callback in barrier async

This fixes race conditions where concurrent calls could corrupt the
read queue or cause check-then-act bugs on isSendingReadAcks.

Also adds thread safety tests:
- concurrentReadReceiptEnqueue: 100 concurrent enqueue operations
- readQueueProcessingUnderLoad: concurrent enqueue during processing
- isPeerReachableThreadSafety: concurrent read access test

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-04 13:29:20 -10:00
jackandClaude Opus 4.5 151b68a497 fix: clear DH shared secrets after Noise handshake operations
Added secureClear() calls for all 6 DH operations in NoiseProtocol.swift
to properly clear sensitive shared secrets from memory after use:

- writeMessage(): .es initiator/responder, .se initiator/responder
- performDHOperation(): .ee and .ss operations

This fixes a forward secrecy vulnerability where shared secrets could
persist in memory after handshake completion.

Also adds:
- TrackingMockKeychain to count secureClear calls in tests
- 3 new tests verifying secureClear is called during handshake

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-04 13:28:55 -10:00
jackandClaude Opus 4.5 b4a3ee5777 fix: add @MainActor to MessageDeduplicationService for thread safety
Added @MainActor annotation to both LRUDeduplicationCache and
MessageDeduplicationService classes. This provides compile-time
enforcement of thread safety since all callers (ChatViewModel,
NostrRelayManager) are already on MainActor.

Benefits:
- Compile-time enforcement prevents future misuse
- Simpler than internal locking mechanisms
- Consistent with existing patterns in codebase

Also adds:
- @MainActor annotation to existing test suites
- 5 new concurrency tests verifying thread safety behavior

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-04 13:25:43 -10:00
jackandClaude Opus 4.5 8d19d6d62c fix: add thread safety to NostrTransport read receipt queue
Synchronized access to readQueue and isSendingReadAcks using the
existing concurrent DispatchQueue with barrier flags:

- sendReadReceipt(): wrap enqueue in barrier async
- processReadQueueIfNeeded(): extract item within barrier context
- scheduleNextReadAck(): wrap callback in barrier async

This fixes race conditions where concurrent calls could corrupt the
read queue or cause check-then-act bugs on isSendingReadAcks.

Also adds thread safety tests:
- concurrentReadReceiptEnqueue: 100 concurrent enqueue operations
- readQueueProcessingUnderLoad: concurrent enqueue during processing
- isPeerReachableThreadSafety: concurrent read access test

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-04 13:23:32 -10:00
jackandClaude Opus 4.5 84fd92ef4b fix: clear DH shared secrets after Noise handshake operations
Added secureClear() calls for all 6 DH operations in NoiseProtocol.swift
to properly clear sensitive shared secrets from memory after use:

- writeMessage(): .es initiator/responder, .se initiator/responder
- performDHOperation(): .ee and .ss operations

This fixes a forward secrecy vulnerability where shared secrets could
persist in memory after handshake completion.

Also adds:
- TrackingMockKeychain to count secureClear calls in tests
- 3 new tests verifying secureClear is called during handshake

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-04 13:21:01 -10:00
jackandGitHub f71bd506fd Merge pull request #920 from malkovitc/feat/472-optimize-message-deduplicator
perf: optimize MessageDeduplicator performance
2026-01-04 12:41:48 -10:00
jackandGitHub ddd7ef5668 Merge branch 'main' into feat/472-optimize-message-deduplicator 2026-01-04 12:37:01 -10:00
jackandGitHub 548a20e77d Merge pull request #919 from malkovitc/fix/646-harden-hex-parsing
fix: harden hex string parsing
2026-01-04 12:33:16 -10:00
jackandGitHub 6e231d10c5 Merge branch 'main' into fix/646-harden-hex-parsing 2026-01-04 12:28:39 -10:00
jackandGitHub 4c9f6e689e Merge pull request #918 from malkovitc/fix/voice-recorder-simulator-build
fix(build): exclude allowBluetoothHFP on iOS Simulator
2026-01-04 12:25:11 -10:00
jackandGitHub 7e73b65240 Merge branch 'main' into fix/voice-recorder-simulator-build 2026-01-04 12:19:52 -10:00
jackandGitHub f5e5f7b98e Merge pull request #916 from malkovitc/fix/797-consolidate-keychain
refactor: consolidate KeychainHelper into KeychainManager
2026-01-04 12:13:15 -10:00
evgeniy.chernomortsev b15d92ebb5 perf: optimize MessageDeduplicator performance (#472)
- Make Entry struct Equatable for better testability
- Remove down to 75% of maxCount instead of fixed 100 items for better
  amortization of cleanup cost
- Reuse Date instance to reduce allocations in isDuplicate()
- Add documentation comments for public methods
- Improve memory capacity management in cleanup()
2025-12-09 18:48:14 +04:00
evgeniy.chernomortsev 6efe9d02fb fix: harden hex string parsing (#646)
Improve Data(hexString:) to handle edge cases:
- Reject odd-length strings (return nil)
- Support optional 0x/0X prefix
- Trim leading/trailing whitespace
- Handle empty strings correctly

Add comprehensive unit tests for hex parsing.
2025-12-09 15:01:59 +04:00
evgeniy.chernomortsev 5a66f03400 fix(build): exclude allowBluetoothHFP on iOS Simulator
allowBluetoothHFP is not available on iOS Simulator, causing build
failures. Use conditional compilation to exclude this option when
building for simulator while keeping it for device builds.
2025-12-09 14:50:34 +04:00
evgeniy.chernomortsevandClaude Opus 4.5 a221b22691 refactor: consolidate KeychainHelper into KeychainManager (#797)
Merge KeychainHelper functionality into KeychainManager to provide
a single, unified API for all keychain operations.

- Remove KeychainHelper.swift and KeychainHelperProtocol
- Add generic save/load/delete methods to KeychainManagerProtocol
- Update NostrIdentityBridge to use KeychainManagerProtocol
- Update FavoritesPersistenceService to use KeychainManagerProtocol
- Update PreviewKeychainManager with new methods
- Update MockKeychain and add MockKeychainHelper typealias for backwards compatibility

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-09 14:42:55 +04:00
jackandGitHub 4b38e9a23c Merge pull request #902 from jackjackbits/refactor/transport-abstraction
Refactor MessageRouter to use generic Transport abstraction
2025-11-26 23:10:32 -10:00
jack 2686d9c82c Increase test sleep duration to fix flaky CI test 2025-11-26 19:25:47 -10:00
jack 832e1f006c Fix startup race in NostrTransport reachability cache 2025-11-26 18:48:08 -10:00
jackandGitHub 090c4cd919 Merge branch 'main' into refactor/transport-abstraction 2025-11-26 18:42:13 -10:00
jack 37c2b3a8c2 Refactor MessageRouter to use generic Transport abstraction
- Decouple MessageRouter from specific Mesh/Nostr implementations
- Update NostrTransport to implement isPeerReachable using a local cache of favorited peers
- Update ChatViewModel to inject transports as a list
- Mark NostrTransport as @unchecked Sendable to handle internal thread safety
2025-11-26 18:39:35 -10:00
jackandGitHub 6f1c879237 Merge pull request #900 from anthonybiasi/fix/macos-environment-object-sheets
fix(macos): Propagate environment objects to sheet presentations
2025-11-26 18:37:33 -10:00
jackandGitHub 81f5101390 Merge branch 'main' into fix/macos-environment-object-sheets 2025-11-26 18:21:12 -10:00
jackandGitHub d56674706d Merge pull request #901 from jackjackbits/refactor/chatviewmodel
Refactor ChatViewModel and fix private chat bugs
2025-11-26 18:20:56 -10:00
jack 33bcfa3147 Fix build warnings: exclude README and fix unused variable 2025-11-26 15:26:48 -10:00
jack 3a54160793 Fix unused variable warning in tests 2025-11-26 15:22:09 -10:00
jack 5d2745eae6 Add tests for blocking and migration logic 2025-11-26 15:06:53 -10:00
jack 5138ce4a33 Add tests for ChatViewModel extensions 2025-11-26 12:47:56 -10:00
jack 07a4997192 Fix private chat message handling: restore persistence and notifications 2025-11-26 12:40:44 -10:00
jack 2d703f4dd9 Refactor ChatViewModel into extensions for Nostr, Tor, and Private Chat 2025-11-26 09:59:42 -10:00
Anthony Biasi 56b9457fcf fix(macos): Propagate ChatViewModel environment object to sheet presentations
Fixes crash on macOS when presenting sheets due to missing @EnvironmentObject.
Added .environmentObject(viewModel) to 8 sheet/fullScreenCover presentations.

Fixed presentations:
- AppInfoView sheet
- FingerprintView sheet
- ImagePickerView fullScreenCover (iOS, both contexts)
- MacImagePickerView sheet (macOS, both contexts)
- ImagePreviewView sheet
- LocationChannelsSheet sheet

Tested on macOS 26.1 (Apple Silicon M4).
All sheet presentations now open without crash.

Platform: macOS only
Impact: Resolves EnvironmentObject.error() crashes
2025-11-26 08:25:23 -07:00
jackandGitHub 9e57bce5c3 Merge pull request #898 from permissionlesstech/refactor/chatviewmodel-extraction
Refactor ChatViewModel: extract logic to services
2025-11-25 10:28:51 -10:00
jack 6eef030386 Fix: use persisted read receipts during consolidation
Pass the UserDefaults-backed sentReadReceipts from ChatViewModel to
consolidateMessages() to correctly identify already-read messages
after app restart. This prevents duplicate read receipts and incorrect
unread badges when reopening a chat shortly after reading it.

Addresses PR review feedback.
2025-11-25 10:24:24 -10:00
jack 9d8754099d Refactor ChatViewModel: extract logic to services
- Remove duplicate Regexes enum, use MessageFormattingEngine.Patterns
- Delete unused formatMessage() function (views use formatMessageAsText)
- Move message consolidation logic to PrivateChatManager
- Add consolidateMessages() and syncReadReceiptsForSentMessages()
- Wire PrivateChatManager to UnifiedPeerService for peer lookup

Reduces ChatViewModel from 5998 to 5713 lines (-285 lines)
All 303 tests pass
2025-11-25 10:16:09 -10:00
jackandGitHub 7fae4e71fb Merge pull request #897 from permissionlesstech/fix/swift6-concurrency-warnings
Fix Swift 6 concurrency warnings
2025-11-25 09:56:17 -10:00
jack 76d8b3fa7f Fix Swift 6 concurrency warnings
- ContentView: Wrap Timer callbacks in Task { @MainActor in } to
  properly access main actor-isolated properties (updateAutocomplete,
  messages)
- BitchatApp: Capture nickname before entering background queue to
  avoid accessing main actor-isolated property from Sendable closure
2025-11-25 09:50:02 -10:00
jackandGitHub b8a8b940b7 Merge pull request #896 from permissionlesstech/refactor/chatviewmodel-testability
Add ChatViewModel testability infrastructure and unit tests
2025-11-25 09:45:09 -10:00
jack 7a5ab52f4c Skip CoreLocation setup in test environments
Add isRunningTests check to LocationStateManager to skip CoreLocation
delegate and permission initialization in test/CI environments.
This prevents potential blocking or unexpected behavior when tests
access LocationStateManager.shared.
2025-11-25 09:39:36 -10:00
jack d2edb651c1 Add CI environment detection to isRunningTests
Add checks for GITHUB_ACTIONS, CI, and XCTestBundlePath environment
variables to reliably detect test/CI environments where notification
APIs should be skipped.
2025-11-25 09:29:26 -10:00
jack 858e878959 Fix CI test failures by simplifying isRunningTests check
Remove Bundle.main.bundleIdentifier == nil from isRunningTests
detection as it may have unintended side effects in CI environments.
The XCTestCase class check and XCTestConfigurationFilePath environment
variable are sufficient for detecting both XCTest and Swift Testing.
2025-11-25 09:20:38 -10:00
jack fa0a15cbcf Retrigger CI 2025-11-25 09:20:38 -10:00
jack 3e680f40bf Add ChatViewModel testability and unit tests
- Add testable ChatViewModel initializer accepting Transport dependency
- Create MockTransport implementing full Transport protocol for testing
- Add 21 unit tests covering:
  - Initialization (delegate, services, nickname)
  - Message sending (basic, empty, mentions, commands)
  - Message receiving (delegate calls, public messages)
  - Peer connections (connect, disconnect, isPeerConnected)
  - Deduplication service integration
  - Private chat routing
  - Bluetooth state handling
  - Panic clear functionality
- Guard NotificationService methods against test environment crashes
- Make deduplicationService internal for test access

All 303 tests pass.
2025-11-25 09:20:38 -10:00
jackandGitHub ec3c16176e Merge pull request #894 from permissionlesstech/refactor/simplify-chatviewmodel-phase1
Refactor: Simplify ChatViewModel - Phase 1
2025-11-25 09:19:57 -10:00
jack 2a8de7e048 Consolidate duplicate handlePrivateMessage methods
Removed duplicate handlePrivateMessage(payload:senderPubkey:...) method
(~63 lines) that was nearly identical to handlePrivateMessage(_:senderPubkey:...).

Changes:
- Added missing isNostrBlocked check to the remaining method
- Updated call site to use _ parameter style
- Removed redundant implementation
2025-11-25 09:13:51 -10:00
jack e9dd30ccbf Extract MessageDeduplicationService from ChatViewModel
Extract message deduplication logic into a dedicated service:
- LRUDeduplicationCache: Generic LRU cache for O(1) lookup with periodic compaction
- ContentNormalizer: Static methods for normalizing content (URL simplification, whitespace collapse, djb2 hash)
- MessageDeduplicationService: Manages content, Nostr event, and ACK deduplication

This removes ~70 lines of inline LRU management code from ChatViewModel
and adds 42 comprehensive tests covering all deduplication scenarios.

Changes to ChatViewModel:
- Add deduplicationService dependency
- Remove normalizedContentKey(), recordContentKey(), trimContentLRUIfNeeded(), popOldestContentKey()
- Remove contentLRUMap, contentLRUOrder, contentLRUHead, contentLRUCap
- Remove processedNostrEvents, processedNostrEventOrder, processedNostrEventHead, maxProcessedNostrEvents
- Remove processedNostrAcks, recordProcessedEvent(), trimProcessedNostrEventsIfNeeded(), popOldestProcessedEvent()
- Remove unused Regexes.simplifyHTTPURL
- Update all call sites to use deduplicationService methods
2025-11-25 09:13:51 -10:00
jackandGitHub af28faa91d Merge pull request #893 from permissionlesstech/claude/simplify-codebase-012QAJ8ihBcE4VzdufcixKHt
Consolidate message deduplication into shared MessageDeduplicator
2025-11-25 09:10:16 -10:00
jack b33fe8086d Add testable initializer to LocationStateManager
Allow tests to create instances with custom UserDefaults storage
for isolated testing of bookmark functionality.
2025-11-25 08:08:51 -10:00
jack 5495874b5a Fix method name after LocationStateManager consolidation
Update call site to use resolveBookmarkNameIfNeeded instead of
resolveNameIfNeeded, which was renamed during the consolidation.
2025-11-25 08:02:48 -10:00
Claude 0fca551966 Consolidate LocationChannelManager and GeohashBookmarksStore into LocationStateManager
Merge two singleton location managers into a unified LocationStateManager:
- CoreLocation permissions and channel computation
- Channel selection and teleport state tracking
- Bookmark persistence and friendly name resolution
- Reverse geocoding (shared, deduplicated)

Provides backward-compatible typealiases so existing code continues to work:
- typealias LocationChannelManager = LocationStateManager
- typealias GeohashBookmarksStore = LocationStateManager

Reduces 4 location-related files to 3 (keeping LocationNotesManager and
GeohashParticipantTracker separate due to different lifecycles).

Files: 2 deleted, 1 created (~525 lines -> ~420 lines)
2025-11-25 01:54:40 +00:00
Claude 0492480598 Consolidate message deduplication into shared MessageDeduplicator
- Extend MessageDeduplicator with configurable maxAge/maxCount params
- Add timestampFor() method for content key time-window checks
- Add record() method to store entries with specific timestamps
- Replace ChatViewModel's custom contentLRU implementation with MessageDeduplicator
- Remove ~35 lines of duplicate LRU code from ChatViewModel

This consolidates 2 separate deduplication implementations into one
reusable, thread-safe component.
2025-11-25 01:19:26 +00:00
jackandGitHub 86f46c8b90 Merge pull request #892 from permissionlesstech/fix/thread-sleep-bleservice
Replace Thread.sleep with cooperative RunLoop delay
2025-11-24 11:57:53 -10:00
jackandGitHub 7057fe75b0 Merge branch 'main' into fix/thread-sleep-bleservice 2025-11-24 11:48:58 -10:00
jackandGitHub 873788b24f Merge pull request #891 from permissionlesstech/refactor/extract-message-formatting-engine
Extract MessageFormattingEngine from ChatViewModel
2025-11-24 11:48:45 -10:00
jack b6dd31b285 Extract MessageFormattingEngine from ChatViewModel
Refactor message formatting logic into a dedicated, testable class:

- Create MessageFormattingEngine with Patterns enum (precompiled regexes)
- Create MessageFormattingContext protocol for dependency injection
- Extract extractMentions(), containsCashuToken(), and pattern matchers
- Add 28 comprehensive tests for formatting utilities
- Update ChatViewModel to conform to MessageFormattingContext

This prepares for further message formatting extraction and makes
regex patterns and utility functions testable in isolation.
2025-11-24 11:44:59 -10:00
jackandGitHub ff7e5a2c2c Merge pull request #890 from permissionlesstech/refactor/extract-geohash-participant-tracker
Extract GeohashParticipantTracker from ChatViewModel
2025-11-24 11:43:29 -10:00
jack 708a5e33cd Replace Thread.sleep with cooperative RunLoop delay in BLEService
Replace blocking Thread.sleep in stopServices() with a cooperative
RunLoop-based delay. This allows BLE callbacks to fire during the
shutdown delay, improving reliability of leave message transmission.

The delay is needed to give the leave message time to transmit before
disconnecting peers and stopping the BLE stack.
2025-11-24 11:37:27 -10:00
jack f893f0605a Extract GeohashParticipantTracker from ChatViewModel
Refactor participant tracking logic into a dedicated, testable class:

- Create GeohashParticipantTracker with GeohashParticipantContext protocol
- Move GeoPerson struct to new file
- Extract participant recording, refresh, and count logic
- Add 18 comprehensive tests for the tracker
- Update ChatViewModel to use tracker via dependency injection
- Subscribe to tracker's objectWillChange for SwiftUI observation

This reduces coupling in ChatViewModel and makes participant tracking
testable in isolation.
2025-11-24 11:34:47 -10:00
jackandGitHub ce33132a0f Merge pull request #889 from permissionlesstech/refactor/extract-command-coordinator
Refactor: Break circular dependency between CommandProcessor and ChatViewModel
2025-11-24 11:11:57 -10:00
jackandGitHub c538837300 Merge branch 'main' into refactor/extract-command-coordinator 2025-11-24 11:06:32 -10:00
jackandGitHub 6888bfa351 Merge pull request #888 from permissionlesstech/fix/crypto-precondition-crashes
Fix: Replace precondition() crashes with throwing errors in crypto code
2025-11-24 11:06:14 -10:00
jack 2fed4b7c31 Fix XChaCha20 tests to avoid Swift Testing crash
The original tests using in-place Data modification with `data[0] ^= 0xFF`
caused signal 5 crashes during parallel test execution. Fixed by:
- Using `import struct Foundation.Data` for efficiency
- Converting Data to byte array before modification to avoid the crash
- Simplified error-throwing tests to use boolean flags instead of
  complex error type matching
2025-11-24 11:02:00 -10:00
jack b956e407ff Add unit tests for XChaCha20Poly1305Compat error handling
15 tests covering:
- Valid input roundtrip (seal/open with and without AAD)
- Different nonces produce different ciphertext
- Invalid key length throws error (short, long, empty)
- Invalid nonce length throws error (short, long, empty)
- Error details contain expected/got values
- Authentication fails with wrong key
- Authentication fails with tampered ciphertext
2025-11-24 10:43:19 -10:00
jack 477cc7fa05 Break circular dependency between CommandProcessor and ChatViewModel
Introduce CommandContextProvider protocol to define what CommandProcessor
needs from its context. This follows the Dependency Inversion Principle:

- Create CommandContextProvider protocol with 15 methods/properties
- Create CommandGeoParticipant struct for geo participant data
- Add getVisibleGeoParticipants() to ChatViewModel
- ChatViewModel now conforms to CommandContextProvider
- CommandProcessor depends on protocol, not concrete ChatViewModel

Benefits:
- Breaks the tight coupling between CommandProcessor and ChatViewModel
- Makes CommandProcessor more testable (can use mock context)
- Clear contract for what CommandProcessor needs
- Backwards-compatible via chatViewModel property alias

Also fixes a concurrency bug in BitchatApp where notification delegate
was accessing main-actor-isolated property from arbitrary thread.
2025-11-24 10:34:29 -10:00
jack dffce9d63f Replace precondition() with throwing errors in XChaCha20Poly1305Compat
precondition() crashes the app in release builds if violated. This is
dangerous for cryptographic code that may receive malformed input from
network data or key derivation bugs.

Changed to guard statements that throw typed errors instead:
- XChaCha20Poly1305Compat.Error.invalidKeyLength
- XChaCha20Poly1305Compat.Error.invalidNonceLength

This allows callers to handle errors gracefully rather than crashing.
2025-11-24 10:22:51 -10:00
jackandGitHub 792eaa3166 Merge pull request #884 from Volgat/fix-analysis-errors
Improve error handling in NostrTransport
2025-11-24 09:46:33 -10:00
jackandGitHub 6ff9b59ff2 Merge branch 'main' into fix-analysis-errors 2025-11-24 09:46:21 -10:00
jackandGitHub bc04cd0bde Merge pull request #879 from xingheng/main
Correct the scheme name for macOS target.
2025-11-24 09:43:37 -10:00
jackandGitHub 3fe417395e Merge branch 'main' into main 2025-11-24 09:37:01 -10:00
jackandGitHub e35bd57c6e Merge pull request #885 from nadimkobeissi/noise-tests
Test Vector Support for Noise XX Handshake
2025-11-24 09:35:00 -10:00
Nadim Kobeissi 5aefb05a8b Fix Noise test vector compatibility with swift test 2025-11-18 21:58:46 +02:00
Nadim Kobeissi e4cbf382ce Fix unintended changes to project.pbxproj 2025-11-18 21:53:54 +02:00
Nadim Kobeissi 7609c6eeba Fix unintended changes to project.pbxproj 2025-11-18 21:53:27 +02:00
Nadim Kobeissi f37acf7e4b Finish up Noise tests 2025-11-18 21:31:55 +02:00
Nadim Kobeissi cf528b0daf Move new Noise test vectors into XCTests (WIP) 2025-11-18 19:22:19 +02:00
Nadim Kobeissi 209ccb4ade Remove obsolete README 2025-11-18 19:21:29 +02:00
Nadim Kobeissi 1876e85f8b Move new Noise test vectors into XCTests (WIP) 2025-11-18 19:20:16 +02:00
Nadim Kobeissi 109b7d0e03 Move new Noise test vectors into XCTests (WIP) 2025-11-18 19:15:54 +02:00
Nadim Kobeissi 09a319fb0f Initial work on adding Noise test vectors
I only became aware halfway through working on these that
`bitchatTests/Noise` exists, so the next step is to integrate them over
there.
2025-11-18 18:44:57 +02:00
Claude 97ca55cc54 Improve error handling in NostrTransport
Add proper error logging for Bech32 decode failures instead of silently
returning. This improves debuggability by making it clear when and why
npub decoding fails for favorite notifications, delivery acks, and read
receipts.

The previous empty catch blocks made it difficult to diagnose issues
with malformed or invalid npub addresses. Now all decoding errors are
properly logged with context about which operation failed.
2025-11-18 16:26:12 +00:00
Will Han 394836f247 Correct the scheme name for macOS target. 2025-11-07 00:06:24 +08:00
47 changed files with 7396 additions and 3340 deletions
+1 -1
View File
@@ -47,7 +47,7 @@ patch-for-macos: backup
# Build the macOS app # Build the macOS app
build: #check generate build: #check generate
@echo "Building BitChat for macOS..." @echo "Building BitChat for macOS..."
@xcodebuild -project bitchat.xcodeproj -scheme "bitchat_macOS" -configuration Debug CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO CODE_SIGN_ENTITLEMENTS="" build @xcodebuild -project bitchat.xcodeproj -scheme "bitchat (macOS)" -configuration Debug CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO CODE_SIGN_ENTITLEMENTS="" build
# Run the macOS app # Run the macOS app
run: build run: build
+4 -2
View File
@@ -34,7 +34,8 @@ let package = Package(
"Assets.xcassets", "Assets.xcassets",
"bitchat.entitlements", "bitchat.entitlements",
"bitchat-macOS.entitlements", "bitchat-macOS.entitlements",
"LaunchScreen.storyboard" "LaunchScreen.storyboard",
"ViewModels/Extensions/README.md"
], ],
resources: [ resources: [
.process("Localizable.xcstrings") .process("Localizable.xcstrings")
@@ -49,7 +50,8 @@ let package = Package(
"README.md" "README.md"
], ],
resources: [ resources: [
.process("Localization") .process("Localization"),
.process("Noise")
] ]
) )
] ]
+34
View File
@@ -95,6 +95,24 @@
); );
target = 57CA17A36A2532A6CFF367BB /* bitchatShareExtension */; target = 57CA17A36A2532A6CFF367BB /* bitchatShareExtension */;
}; };
C5E027A52ECCDFD700BD6012 /* Exceptions for "bitchatTests" folder in "bitchatTests_macOS" target */ = {
isa = PBXFileSystemSynchronizedBuildFileExceptionSet;
membershipExceptions = (
Info.plist,
Localization/PrimaryLocalizationKeys.json,
README.md,
);
target = 47FF23248747DD7CB666CB91 /* bitchatTests_macOS */;
};
C5E027A82ECCDFE200BD6012 /* Exceptions for "bitchatTests" folder in "bitchatTests_iOS" target */ = {
isa = PBXFileSystemSynchronizedBuildFileExceptionSet;
membershipExceptions = (
Info.plist,
Localization/PrimaryLocalizationKeys.json,
README.md,
);
target = 6CB97DF2EA57234CB3E563B8 /* bitchatTests_iOS */;
};
/* End PBXFileSystemSynchronizedBuildFileExceptionSet section */ /* End PBXFileSystemSynchronizedBuildFileExceptionSet section */
/* Begin PBXFileSystemSynchronizedRootGroup section */ /* Begin PBXFileSystemSynchronizedRootGroup section */
@@ -118,6 +136,10 @@
}; };
A6E32D412E762EAE0032EA8A /* bitchatTests */ = { A6E32D412E762EAE0032EA8A /* bitchatTests */ = {
isa = PBXFileSystemSynchronizedRootGroup; isa = PBXFileSystemSynchronizedRootGroup;
exceptions = (
C5E027A82ECCDFE200BD6012 /* Exceptions for "bitchatTests" folder in "bitchatTests_iOS" target */,
C5E027A52ECCDFD700BD6012 /* Exceptions for "bitchatTests" folder in "bitchatTests_macOS" target */,
);
path = bitchatTests; path = bitchatTests;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
@@ -213,6 +235,7 @@
buildConfigurationList = 1C27B5BA3DB46DDF0DBFEF62 /* Build configuration list for PBXNativeTarget "bitchatTests_macOS" */; buildConfigurationList = 1C27B5BA3DB46DDF0DBFEF62 /* Build configuration list for PBXNativeTarget "bitchatTests_macOS" */;
buildPhases = ( buildPhases = (
5C22AA7B9ACC5A861445C769 /* Sources */, 5C22AA7B9ACC5A861445C769 /* Sources */,
C5E027A42ECCDFD700BD6012 /* Resources */,
); );
buildRules = ( buildRules = (
); );
@@ -245,6 +268,7 @@
buildConfigurationList = 38C4AF6313E5037F25CEF30B /* Build configuration list for PBXNativeTarget "bitchatTests_iOS" */; buildConfigurationList = 38C4AF6313E5037F25CEF30B /* Build configuration list for PBXNativeTarget "bitchatTests_iOS" */;
buildPhases = ( buildPhases = (
865C8403EF02C089369A9FCB /* Sources */, 865C8403EF02C089369A9FCB /* Sources */,
C5E027A72ECCDFE200BD6012 /* Resources */,
); );
buildRules = ( buildRules = (
); );
@@ -343,6 +367,16 @@
E0A1B2C3D4E5F6012345678D /* relays/online_relays_gps.csv in Resources */, E0A1B2C3D4E5F6012345678D /* relays/online_relays_gps.csv in Resources */,
); );
}; };
C5E027A42ECCDFD700BD6012 /* Resources */ = {
isa = PBXResourcesBuildPhase;
files = (
);
};
C5E027A72ECCDFE200BD6012 /* Resources */ = {
isa = PBXResourcesBuildPhase;
files = (
);
};
CD6E8F32BC38357473954F97 /* Resources */ = { CD6E8F32BC38357473954F97 /* Resources */ = {
isa = PBXResourcesBuildPhase; isa = PBXResourcesBuildPhase;
files = ( files = (
+9 -3
View File
@@ -53,9 +53,10 @@ struct BitchatApp: App {
// Inject live Noise service into VerificationService to avoid creating new BLE instances // Inject live Noise service into VerificationService to avoid creating new BLE instances
VerificationService.shared.configure(with: chatViewModel.meshService.getNoiseService()) VerificationService.shared.configure(with: chatViewModel.meshService.getNoiseService())
// Prewarm Nostr identity and QR to make first VERIFY sheet fast // Prewarm Nostr identity and QR to make first VERIFY sheet fast
let nickname = chatViewModel.nickname
DispatchQueue.global(qos: .utility).async { DispatchQueue.global(qos: .utility).async {
let npub = try? idBridge.getCurrentNostrIdentity()?.npub let npub = try? idBridge.getCurrentNostrIdentity()?.npub
_ = VerificationService.shared.buildMyQRString(nickname: chatViewModel.nickname, npub: npub) _ = VerificationService.shared.buildMyQRString(nickname: nickname, npub: npub)
} }
appDelegate.chatViewModel = chatViewModel appDelegate.chatViewModel = chatViewModel
@@ -248,11 +249,16 @@ final class NotificationDelegate: NSObject, UNUserNotificationCenterDelegate {
// Get peer ID from userInfo // Get peer ID from userInfo
if let peerID = userInfo["peerID"] as? String { if let peerID = userInfo["peerID"] as? String {
// Don't show notification if the private chat is already open // Don't show notification if the private chat is already open
if chatViewModel?.selectedPrivateChatPeer == PeerID(str: peerID) { // Access main-actor-isolated property via Task
Task { @MainActor in
if self.chatViewModel?.selectedPrivateChatPeer == PeerID(str: peerID) {
completionHandler([]) completionHandler([])
return } else {
completionHandler([.banner, .sound])
} }
} }
return
}
} }
// Suppress geohash activity notification if we're already in that geohash channel // Suppress geohash activity notification if we're already in that geohash channel
if identifier.hasPrefix("geo-activity-"), if identifier.hasPrefix("geo-activity-"),
@@ -58,11 +58,20 @@ final class VoiceRecorder: NSObject, AVAudioRecorderDelegate {
guard session.recordPermission == .granted else { guard session.recordPermission == .granted else {
throw RecorderError.microphoneAccessDenied throw RecorderError.microphoneAccessDenied
} }
#if targetEnvironment(simulator)
// allowBluetoothHFP is not available on iOS Simulator
try session.setCategory(
.playAndRecord,
mode: .default,
options: [.defaultToSpeaker, .allowBluetoothA2DP]
)
#else
try session.setCategory( try session.setCategory(
.playAndRecord, .playAndRecord,
mode: .default, mode: .default,
options: [.defaultToSpeaker, .allowBluetoothA2DP, .allowBluetoothHFP] options: [.defaultToSpeaker, .allowBluetoothA2DP, .allowBluetoothHFP]
) )
#endif
try session.setActive(true, options: .notifyOthersOnDeactivation) try session.setActive(true, options: .notifyOthersOnDeactivation)
#endif #endif
#if os(macOS) #if os(macOS)
+46 -15
View File
@@ -451,13 +451,13 @@ final class NoiseSymmetricState {
} }
} }
func split() -> (NoiseCipherState, NoiseCipherState) { func split(useExtractedNonce: Bool) -> (NoiseCipherState, NoiseCipherState) {
let output = hkdf(chainingKey: chainingKey, inputKeyMaterial: Data(), numOutputs: 2) let output = hkdf(chainingKey: chainingKey, inputKeyMaterial: Data(), numOutputs: 2)
let tempKey1 = SymmetricKey(data: output[0]) let tempKey1 = SymmetricKey(data: output[0])
let tempKey2 = SymmetricKey(data: output[1]) let tempKey2 = SymmetricKey(data: output[1])
let c1 = NoiseCipherState(key: tempKey1, useExtractedNonce: true) let c1 = NoiseCipherState(key: tempKey1, useExtractedNonce: useExtractedNonce)
let c2 = NoiseCipherState(key: tempKey2, useExtractedNonce: true) let c2 = NoiseCipherState(key: tempKey2, useExtractedNonce: useExtractedNonce)
return (c1, c2) return (c1, c2)
} }
@@ -507,16 +507,24 @@ final class NoiseHandshakeState {
private var messagePatterns: [[NoiseMessagePattern]] = [] private var messagePatterns: [[NoiseMessagePattern]] = []
private var currentPattern = 0 private var currentPattern = 0
// Test support: predetermined ephemeral keys for test vectors
private var predeterminedEphemeralKey: Curve25519.KeyAgreement.PrivateKey?
private var prologueData: Data
init( init(
role: NoiseRole, role: NoiseRole,
pattern: NoisePattern, pattern: NoisePattern,
keychain: KeychainManagerProtocol, keychain: KeychainManagerProtocol,
localStaticKey: Curve25519.KeyAgreement.PrivateKey? = nil, localStaticKey: Curve25519.KeyAgreement.PrivateKey? = nil,
remoteStaticKey: Curve25519.KeyAgreement.PublicKey? = nil remoteStaticKey: Curve25519.KeyAgreement.PublicKey? = nil,
prologue: Data = Data(),
predeterminedEphemeralKey: Curve25519.KeyAgreement.PrivateKey? = nil
) { ) {
self.role = role self.role = role
self.pattern = pattern self.pattern = pattern
self.keychain = keychain self.keychain = keychain
self.prologueData = prologue
self.predeterminedEphemeralKey = predeterminedEphemeralKey
// Initialize static keys // Initialize static keys
if let localKey = localStaticKey { if let localKey = localStaticKey {
@@ -537,8 +545,8 @@ final class NoiseHandshakeState {
} }
private func mixPreMessageKeys() { private func mixPreMessageKeys() {
// Mix prologue (empty for XX pattern normally) // Mix prologue
symmetricState.mixHash(Data()) // Empty prologue for XX pattern symmetricState.mixHash(self.prologueData)
// For XX pattern, no pre-message keys // For XX pattern, no pre-message keys
// For IK/NK patterns, we'd mix the responder's static key here // For IK/NK patterns, we'd mix the responder's static key here
switch pattern { switch pattern {
@@ -563,8 +571,13 @@ final class NoiseHandshakeState {
for pattern in patterns { for pattern in patterns {
switch pattern { switch pattern {
case .e: case .e:
// Generate ephemeral key // Generate ephemeral key (or use predetermined key for tests)
if let predetermined = predeterminedEphemeralKey {
localEphemeralPrivate = predetermined
predeterminedEphemeralKey = nil
} else {
localEphemeralPrivate = Curve25519.KeyAgreement.PrivateKey() localEphemeralPrivate = Curve25519.KeyAgreement.PrivateKey()
}
localEphemeralPublic = localEphemeralPrivate!.publicKey localEphemeralPublic = localEphemeralPrivate!.publicKey
messageBuffer.append(localEphemeralPublic!.rawRepresentation) messageBuffer.append(localEphemeralPublic!.rawRepresentation)
symmetricState.mixHash(localEphemeralPublic!.rawRepresentation) symmetricState.mixHash(localEphemeralPublic!.rawRepresentation)
@@ -597,14 +610,20 @@ final class NoiseHandshakeState {
throw NoiseError.missingKeys throw NoiseError.missingKeys
} }
let shared = try localEphemeral.sharedSecretFromKeyAgreement(with: remoteStatic) let shared = try localEphemeral.sharedSecretFromKeyAgreement(with: remoteStatic)
symmetricState.mixKey(shared.withUnsafeBytes { Data($0) }) var sharedData = shared.withUnsafeBytes { Data($0) }
symmetricState.mixKey(sharedData)
// Clear sensitive shared secret
keychain.secureClear(&sharedData)
} else { } else {
guard let localStatic = localStaticPrivate, guard let localStatic = localStaticPrivate,
let remoteEphemeral = remoteEphemeralPublic else { let remoteEphemeral = remoteEphemeralPublic else {
throw NoiseError.missingKeys throw NoiseError.missingKeys
} }
let shared = try localStatic.sharedSecretFromKeyAgreement(with: remoteEphemeral) let shared = try localStatic.sharedSecretFromKeyAgreement(with: remoteEphemeral)
symmetricState.mixKey(shared.withUnsafeBytes { Data($0) }) var sharedData = shared.withUnsafeBytes { Data($0) }
symmetricState.mixKey(sharedData)
// Clear sensitive shared secret
keychain.secureClear(&sharedData)
} }
case .se: case .se:
@@ -615,14 +634,20 @@ final class NoiseHandshakeState {
throw NoiseError.missingKeys throw NoiseError.missingKeys
} }
let shared = try localStatic.sharedSecretFromKeyAgreement(with: remoteEphemeral) let shared = try localStatic.sharedSecretFromKeyAgreement(with: remoteEphemeral)
symmetricState.mixKey(shared.withUnsafeBytes { Data($0) }) var sharedData = shared.withUnsafeBytes { Data($0) }
symmetricState.mixKey(sharedData)
// Clear sensitive shared secret
keychain.secureClear(&sharedData)
} else { } else {
guard let localEphemeral = localEphemeralPrivate, guard let localEphemeral = localEphemeralPrivate,
let remoteStatic = remoteStaticPublic else { let remoteStatic = remoteStaticPublic else {
throw NoiseError.missingKeys throw NoiseError.missingKeys
} }
let shared = try localEphemeral.sharedSecretFromKeyAgreement(with: remoteStatic) let shared = try localEphemeral.sharedSecretFromKeyAgreement(with: remoteStatic)
symmetricState.mixKey(shared.withUnsafeBytes { Data($0) }) var sharedData = shared.withUnsafeBytes { Data($0) }
symmetricState.mixKey(sharedData)
// Clear sensitive shared secret
keychain.secureClear(&sharedData)
} }
case .ss: case .ss:
@@ -711,7 +736,10 @@ final class NoiseHandshakeState {
throw NoiseError.missingKeys throw NoiseError.missingKeys
} }
let shared = try localEphemeral.sharedSecretFromKeyAgreement(with: remoteEphemeral) let shared = try localEphemeral.sharedSecretFromKeyAgreement(with: remoteEphemeral)
symmetricState.mixKey(shared.withUnsafeBytes { Data($0) }) var sharedData = shared.withUnsafeBytes { Data($0) }
symmetricState.mixKey(sharedData)
// Clear sensitive shared secret
keychain.secureClear(&sharedData)
case .es: case .es:
if role == .initiator { if role == .initiator {
@@ -765,7 +793,10 @@ final class NoiseHandshakeState {
throw NoiseError.missingKeys throw NoiseError.missingKeys
} }
let shared = try localStatic.sharedSecretFromKeyAgreement(with: remoteStatic) let shared = try localStatic.sharedSecretFromKeyAgreement(with: remoteStatic)
symmetricState.mixKey(shared.withUnsafeBytes { Data($0) }) var sharedData = shared.withUnsafeBytes { Data($0) }
symmetricState.mixKey(sharedData)
// Clear sensitive shared secret
keychain.secureClear(&sharedData)
case .e, .s: case .e, .s:
break break
@@ -776,12 +807,12 @@ final class NoiseHandshakeState {
return currentPattern >= messagePatterns.count return currentPattern >= messagePatterns.count
} }
func getTransportCiphers() throws -> (send: NoiseCipherState, receive: NoiseCipherState) { func getTransportCiphers(useExtractedNonce: Bool) throws -> (send: NoiseCipherState, receive: NoiseCipherState) {
guard isHandshakeComplete() else { guard isHandshakeComplete() else {
throw NoiseError.handshakeNotComplete throw NoiseError.handshakeNotComplete
} }
let (c1, c2) = symmetricState.split() let (c1, c2) = symmetricState.split(useExtractedNonce: useExtractedNonce)
// Initiator uses c1 for sending, c2 for receiving // Initiator uses c1 for sending, c2 for receiving
// Responder uses c2 for sending, c1 for receiving // Responder uses c2 for sending, c1 for receiving
+2 -2
View File
@@ -103,7 +103,7 @@ class NoiseSession {
// Check if handshake is complete // Check if handshake is complete
if handshake.isHandshakeComplete() { if handshake.isHandshakeComplete() {
// Get transport ciphers // Get transport ciphers
let (send, receive) = try handshake.getTransportCiphers() let (send, receive) = try handshake.getTransportCiphers(useExtractedNonce: true)
sendCipher = send sendCipher = send
receiveCipher = receive receiveCipher = receive
@@ -129,7 +129,7 @@ class NoiseSession {
// Check if handshake is complete after writing // Check if handshake is complete after writing
if handshake.isHandshakeComplete() { if handshake.isHandshakeComplete() {
// Get transport ciphers // Get transport ciphers
let (send, receive) = try handshake.getTransportCiphers() let (send, receive) = try handshake.getTransportCiphers(useExtractedNonce: true)
sendCipher = send sendCipher = send
receiveCipher = receive receiveCipher = receive
-50
View File
@@ -1,50 +0,0 @@
import Foundation
protocol KeychainHelperProtocol {
func save(key: String, data: Data, service: String, accessible: CFString?)
func load(key: String, service: String) -> Data?
func delete(key: String, service: String)
}
/// Keychain helper for secure storage
struct KeychainHelper: KeychainHelperProtocol {
func save(key: String, data: Data, service: String, accessible: CFString? = nil) {
var query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: service,
kSecAttrAccount as String: key,
kSecValueData as String: data
]
if let accessible = accessible {
query[kSecAttrAccessible as String] = accessible
}
SecItemDelete(query as CFDictionary)
SecItemAdd(query as CFDictionary, nil)
}
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
}
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)
}
}
+2 -2
View File
@@ -12,9 +12,9 @@ final class NostrIdentityBridge {
private var derivedIdentityCache: [String: NostrIdentity] = [:] private var derivedIdentityCache: [String: NostrIdentity] = [:]
private let cacheLock = NSLock() private let cacheLock = NSLock()
private let keychain: KeychainHelperProtocol private let keychain: KeychainManagerProtocol
init(keychain: KeychainHelperProtocol = KeychainHelper()) { init(keychain: KeychainManagerProtocol = KeychainManager()) {
self.keychain = keychain self.keychain = keychain
} }
+28 -9
View File
@@ -5,16 +5,27 @@ import CryptoKit
/// Implements HChaCha20 to derive a subkey and reduces the 24-byte nonce to a 12-byte nonce /// Implements HChaCha20 to derive a subkey and reduces the 24-byte nonce to a 12-byte nonce
/// as per XChaCha20 construction. /// as per XChaCha20 construction.
enum XChaCha20Poly1305Compat { enum XChaCha20Poly1305Compat {
/// Errors that can occur during XChaCha20-Poly1305 operations
enum Error: Swift.Error {
case invalidKeyLength(expected: Int, got: Int)
case invalidNonceLength(expected: Int, got: Int)
}
struct SealBox { struct SealBox {
let ciphertext: Data let ciphertext: Data
let tag: Data let tag: Data
} }
static func seal(plaintext: Data, key: Data, nonce24: Data, aad: Data? = nil) throws -> SealBox { static func seal(plaintext: Data, key: Data, nonce24: Data, aad: Data? = nil) throws -> SealBox {
precondition(key.count == 32, "XChaCha20 key must be 32 bytes") guard key.count == 32 else {
precondition(nonce24.count == 24, "XChaCha20 nonce must be 24 bytes") throw Error.invalidKeyLength(expected: 32, got: key.count)
}
guard nonce24.count == 24 else {
throw Error.invalidNonceLength(expected: 24, got: nonce24.count)
}
let subkey = hchacha20(key: key, nonce16: nonce24.prefix(16)) let subkey = try hchacha20(key: key, nonce16: Data(nonce24.prefix(16)))
let nonce12 = derive12ByteNonce(from24: nonce24) let nonce12 = derive12ByteNonce(from24: nonce24)
let chachaKey = SymmetricKey(data: subkey) let chachaKey = SymmetricKey(data: subkey)
let nonce = try ChaChaPoly.Nonce(data: nonce12) let nonce = try ChaChaPoly.Nonce(data: nonce12)
@@ -23,10 +34,14 @@ enum XChaCha20Poly1305Compat {
} }
static func open(ciphertext: Data, tag: Data, key: Data, nonce24: Data, aad: Data? = nil) throws -> Data { static func open(ciphertext: Data, tag: Data, key: Data, nonce24: Data, aad: Data? = nil) throws -> Data {
precondition(key.count == 32, "XChaCha20 key must be 32 bytes") guard key.count == 32 else {
precondition(nonce24.count == 24, "XChaCha20 nonce must be 24 bytes") throw Error.invalidKeyLength(expected: 32, got: key.count)
}
guard nonce24.count == 24 else {
throw Error.invalidNonceLength(expected: 24, got: nonce24.count)
}
let subkey = hchacha20(key: key, nonce16: nonce24.prefix(16)) let subkey = try hchacha20(key: key, nonce16: Data(nonce24.prefix(16)))
let nonce12 = derive12ByteNonce(from24: nonce24) let nonce12 = derive12ByteNonce(from24: nonce24)
let chachaKey = SymmetricKey(data: subkey) let chachaKey = SymmetricKey(data: subkey)
let box = try ChaChaPoly.SealedBox(nonce: ChaChaPoly.Nonce(data: nonce12), ciphertext: ciphertext, tag: tag) let box = try ChaChaPoly.SealedBox(nonce: ChaChaPoly.Nonce(data: nonce12), ciphertext: ciphertext, tag: tag)
@@ -43,10 +58,14 @@ enum XChaCha20Poly1305Compat {
return out return out
} }
private static func hchacha20(key: Data, nonce16: Data) -> Data { private static func hchacha20(key: Data, nonce16: Data) throws -> Data {
// HChaCha20 based on the original ChaCha20 core with a 16-byte nonce. // HChaCha20 based on the original ChaCha20 core with a 16-byte nonce.
precondition(key.count == 32) guard key.count == 32 else {
precondition(nonce16.count == 16) throw Error.invalidKeyLength(expected: 32, got: key.count)
}
guard nonce16.count == 16 else {
throw Error.invalidNonceLength(expected: 16, got: nonce16.count)
}
// Constants "expand 32-byte k" // Constants "expand 32-byte k"
var state: [UInt32] = [ var state: [UInt32] = [
+26 -4
View File
@@ -23,14 +23,36 @@ extension Data {
return digest.map { String(format: "%02x", $0) }.joined() return digest.map { String(format: "%02x", $0) }.joined()
} }
/// Initialize Data from a hex string.
/// - Parameter hexString: A hex string, optionally prefixed with "0x" or "0X".
/// Whitespace is trimmed. Must have even length after prefix removal.
/// - Returns: nil if the string has odd length or contains invalid hex characters.
init?(hexString: String) { init?(hexString: String) {
let len = hexString.count / 2 var hex = hexString.trimmingCharacters(in: .whitespaces)
// Remove optional 0x prefix
if hex.hasPrefix("0x") || hex.hasPrefix("0X") {
hex = String(hex.dropFirst(2))
}
// Reject odd-length strings
guard hex.count % 2 == 0 else {
return nil
}
// Reject empty strings
guard !hex.isEmpty else {
self = Data()
return
}
let len = hex.count / 2
var data = Data(capacity: len) var data = Data(capacity: len)
var index = hexString.startIndex var index = hex.startIndex
for _ in 0..<len { for _ in 0..<len {
let nextIndex = hexString.index(index, offsetBy: 2) let nextIndex = hex.index(index, offsetBy: 2)
guard let byte = UInt8(String(hexString[index..<nextIndex]), radix: 16) else { guard let byte = UInt8(String(hex[index..<nextIndex]), radix: 16) else {
return nil return nil
} }
data.append(byte) data.append(byte)
+5 -2
View File
@@ -521,8 +521,11 @@ final class BLEService: NSObject {
} }
} }
// Give leave message a moment to send // Give leave message a moment to send (cooperative delay allows BLE callbacks to fire)
Thread.sleep(forTimeInterval: TransportConfig.bleThreadSleepWriteShortDelaySeconds) let deadline = Date().addingTimeInterval(TransportConfig.bleThreadSleepWriteShortDelaySeconds)
while Date() < deadline {
RunLoop.current.run(until: Date().addingTimeInterval(0.01))
}
// Clear pending notifications // Clear pending notifications
collectionsQueue.sync(flags: .barrier) { collectionsQueue.sync(flags: .barrier) {
+79 -31
View File
@@ -15,19 +15,67 @@ enum CommandResult {
case handled // Command handled, no message needed case handled // Command handled, no message needed
} }
/// Simple struct for geo participant info used by CommandProcessor
struct CommandGeoParticipant {
let id: String // pubkey hex (lowercased)
let displayName: String
}
/// Protocol defining what CommandProcessor needs from its context.
/// This breaks the circular dependency between CommandProcessor and ChatViewModel.
@MainActor
protocol CommandContextProvider: AnyObject {
// MARK: - State Properties
var nickname: String { get }
var selectedPrivateChatPeer: PeerID? { get }
var blockedUsers: Set<String> { get }
var privateChats: [PeerID: [BitchatMessage]] { get set }
var idBridge: NostrIdentityBridge { get }
// MARK: - Peer Lookup
func getPeerIDForNickname(_ nickname: String) -> PeerID?
func getVisibleGeoParticipants() -> [CommandGeoParticipant]
func nostrPubkeyForDisplayName(_ displayName: String) -> String?
// MARK: - Chat Actions
func startPrivateChat(with peerID: PeerID)
func sendPrivateMessage(_ content: String, to peerID: PeerID)
func clearCurrentPublicTimeline()
func sendPublicRaw(_ content: String)
// MARK: - System Messages
func addLocalPrivateSystemMessage(_ content: String, to peerID: PeerID)
func addPublicSystemMessage(_ content: String)
// MARK: - Favorites
func toggleFavorite(peerID: PeerID)
func sendFavoriteNotification(to peerID: PeerID, isFavorite: Bool)
}
/// Processes chat commands in a focused, efficient way /// Processes chat commands in a focused, efficient way
@MainActor @MainActor
final class CommandProcessor { final class CommandProcessor {
weak var chatViewModel: ChatViewModel? weak var contextProvider: CommandContextProvider?
weak var meshService: Transport? weak var meshService: Transport?
private let identityManager: SecureIdentityStateManagerProtocol private let identityManager: SecureIdentityStateManagerProtocol
init(chatViewModel: ChatViewModel? = nil, meshService: Transport? = nil, identityManager: SecureIdentityStateManagerProtocol) { /// Backward-compatible property for existing code
self.chatViewModel = chatViewModel weak var chatViewModel: CommandContextProvider? {
get { contextProvider }
set { contextProvider = newValue }
}
init(contextProvider: CommandContextProvider? = nil, meshService: Transport? = nil, identityManager: SecureIdentityStateManagerProtocol) {
self.contextProvider = contextProvider
self.meshService = meshService self.meshService = meshService
self.identityManager = identityManager self.identityManager = identityManager
} }
/// Backward-compatible initializer
convenience init(chatViewModel: ChatViewModel? = nil, meshService: Transport? = nil, identityManager: SecureIdentityStateManagerProtocol) {
self.init(contextProvider: chatViewModel, meshService: meshService, identityManager: identityManager)
}
/// Process a command string /// Process a command string
@MainActor @MainActor
func process(_ command: String) -> CommandResult { func process(_ command: String) -> CommandResult {
@@ -42,7 +90,7 @@ final class CommandProcessor {
case .location: return true case .location: return true
} }
}() }()
let inGeoDM = chatViewModel?.selectedPrivateChatPeer?.isGeoDM == true let inGeoDM = contextProvider?.selectedPrivateChatPeer?.isGeoDM == true
switch cmd { switch cmd {
case "/m", "/msg": case "/m", "/msg":
@@ -81,15 +129,15 @@ final class CommandProcessor {
let targetName = String(parts[0]) let targetName = String(parts[0])
let nickname = targetName.hasPrefix("@") ? String(targetName.dropFirst()) : targetName let nickname = targetName.hasPrefix("@") ? String(targetName.dropFirst()) : targetName
guard let peerID = chatViewModel?.getPeerIDForNickname(nickname) else { guard let peerID = contextProvider?.getPeerIDForNickname(nickname) else {
return .error(message: "'\(nickname)' not found") return .error(message: "'\(nickname)' not found")
} }
chatViewModel?.startPrivateChat(with: peerID) contextProvider?.startPrivateChat(with: peerID)
if parts.count > 1 { if parts.count > 1 {
let message = String(parts[1]) let message = String(parts[1])
chatViewModel?.sendPrivateMessage(message, to: peerID) contextProvider?.sendPrivateMessage(message, to: peerID)
} }
return .success(message: "started private chat with \(nickname)") return .success(message: "started private chat with \(nickname)")
@@ -100,9 +148,9 @@ final class CommandProcessor {
switch LocationChannelManager.shared.selectedChannel { switch LocationChannelManager.shared.selectedChannel {
case .location(let ch): case .location(let ch):
// Geohash context: show visible geohash participants (exclude self) // Geohash context: show visible geohash participants (exclude self)
guard let vm = chatViewModel else { return .success(message: "nobody around") } guard let vm = contextProvider else { return .success(message: "nobody around") }
let myHex = (try? chatViewModel?.idBridge.deriveIdentity(forGeohash: ch.geohash))?.publicKeyHex.lowercased() let myHex = (try? vm.idBridge.deriveIdentity(forGeohash: ch.geohash))?.publicKeyHex.lowercased()
let people = vm.visibleGeohashPeople().filter { person in let people = vm.getVisibleGeoParticipants().filter { person in
if let me = myHex { return person.id.lowercased() != me } if let me = myHex { return person.id.lowercased() != me }
return true return true
} }
@@ -120,10 +168,10 @@ final class CommandProcessor {
} }
private func handleClear() -> CommandResult { private func handleClear() -> CommandResult {
if let peerID = chatViewModel?.selectedPrivateChatPeer { if let peerID = contextProvider?.selectedPrivateChatPeer {
chatViewModel?.privateChats[peerID]?.removeAll() contextProvider?.privateChats[peerID]?.removeAll()
} else { } else {
chatViewModel?.clearCurrentPublicTimeline() contextProvider?.clearCurrentPublicTimeline()
} }
return .handled return .handled
} }
@@ -136,14 +184,14 @@ final class CommandProcessor {
let nickname = targetName.hasPrefix("@") ? String(targetName.dropFirst()) : targetName let nickname = targetName.hasPrefix("@") ? String(targetName.dropFirst()) : targetName
guard let targetPeerID = chatViewModel?.getPeerIDForNickname(nickname), guard let targetPeerID = contextProvider?.getPeerIDForNickname(nickname),
let myNickname = chatViewModel?.nickname else { let myNickname = contextProvider?.nickname else {
return .error(message: "cannot \(command) \(nickname): not found") return .error(message: "cannot \(command) \(nickname): not found")
} }
let emoteContent = "* \(emoji) \(myNickname) \(action) \(nickname)\(suffix) *" let emoteContent = "* \(emoji) \(myNickname) \(action) \(nickname)\(suffix) *"
if chatViewModel?.selectedPrivateChatPeer != nil { if contextProvider?.selectedPrivateChatPeer != nil {
// In private chat // In private chat
if let peerNickname = meshService?.peerNickname(peerID: targetPeerID) { if let peerNickname = meshService?.peerNickname(peerID: targetPeerID) {
let personalMessage = "* \(emoji) \(myNickname) \(action) you\(suffix) *" let personalMessage = "* \(emoji) \(myNickname) \(action) you\(suffix) *"
@@ -159,13 +207,13 @@ final class CommandProcessor {
} }
}() }()
let localText = "\(emoji) you \(pastAction) \(nickname)\(suffix)" let localText = "\(emoji) you \(pastAction) \(nickname)\(suffix)"
chatViewModel?.addLocalPrivateSystemMessage(localText, to: targetPeerID) contextProvider?.addLocalPrivateSystemMessage(localText, to: targetPeerID)
} }
} else { } else {
// In public chat: send to active public channel (mesh or geohash) // In public chat: send to active public channel (mesh or geohash)
chatViewModel?.sendPublicRaw(emoteContent) contextProvider?.sendPublicRaw(emoteContent)
let publicEcho = "\(emoji) \(myNickname) \(action) \(nickname)\(suffix)" let publicEcho = "\(emoji) \(myNickname) \(action) \(nickname)\(suffix)"
chatViewModel?.addPublicSystemMessage(publicEcho) contextProvider?.addPublicSystemMessage(publicEcho)
} }
return .handled return .handled
@@ -176,7 +224,7 @@ final class CommandProcessor {
if targetName.isEmpty { if targetName.isEmpty {
// List blocked users (mesh) and geohash (Nostr) blocks // List blocked users (mesh) and geohash (Nostr) blocks
let meshBlocked = chatViewModel?.blockedUsers ?? [] let meshBlocked = contextProvider?.blockedUsers ?? []
var blockedNicknames: [String] = [] var blockedNicknames: [String] = []
if let peers = meshService?.getPeerNicknames() { if let peers = meshService?.getPeerNicknames() {
for (peerID, nickname) in peers { for (peerID, nickname) in peers {
@@ -190,8 +238,8 @@ final class CommandProcessor {
// Geohash blocked names (prefer visible display names; fallback to #suffix) // Geohash blocked names (prefer visible display names; fallback to #suffix)
let geoBlocked = Array(identityManager.getBlockedNostrPubkeys()) let geoBlocked = Array(identityManager.getBlockedNostrPubkeys())
var geoNames: [String] = [] var geoNames: [String] = []
if let vm = chatViewModel { if let vm = contextProvider {
let visible = vm.visibleGeohashPeople() let visible = vm.getVisibleGeoParticipants()
let visibleIndex = Dictionary(uniqueKeysWithValues: visible.map { ($0.id.lowercased(), $0.displayName) }) let visibleIndex = Dictionary(uniqueKeysWithValues: visible.map { ($0.id.lowercased(), $0.displayName) })
for pk in geoBlocked { for pk in geoBlocked {
if let name = visibleIndex[pk.lowercased()] { if let name = visibleIndex[pk.lowercased()] {
@@ -210,7 +258,7 @@ final class CommandProcessor {
let nickname = targetName.hasPrefix("@") ? String(targetName.dropFirst()) : targetName let nickname = targetName.hasPrefix("@") ? String(targetName.dropFirst()) : targetName
if let peerID = chatViewModel?.getPeerIDForNickname(nickname), if let peerID = contextProvider?.getPeerIDForNickname(nickname),
let fingerprint = meshService?.getFingerprint(for: peerID) { let fingerprint = meshService?.getFingerprint(for: peerID) {
if identityManager.isBlocked(fingerprint: fingerprint) { if identityManager.isBlocked(fingerprint: fingerprint) {
return .success(message: "\(nickname) is already blocked") return .success(message: "\(nickname) is already blocked")
@@ -235,7 +283,7 @@ final class CommandProcessor {
return .success(message: "blocked \(nickname). you will no longer receive messages from them") return .success(message: "blocked \(nickname). you will no longer receive messages from them")
} }
// Mesh lookup failed; try geohash (Nostr) participant by display name // Mesh lookup failed; try geohash (Nostr) participant by display name
if let pub = chatViewModel?.nostrPubkeyForDisplayName(nickname) { if let pub = contextProvider?.nostrPubkeyForDisplayName(nickname) {
if identityManager.isNostrBlocked(pubkeyHexLowercased: pub) { if identityManager.isNostrBlocked(pubkeyHexLowercased: pub) {
return .success(message: "\(nickname) is already blocked") return .success(message: "\(nickname) is already blocked")
} }
@@ -254,7 +302,7 @@ final class CommandProcessor {
let nickname = targetName.hasPrefix("@") ? String(targetName.dropFirst()) : targetName let nickname = targetName.hasPrefix("@") ? String(targetName.dropFirst()) : targetName
if let peerID = chatViewModel?.getPeerIDForNickname(nickname), if let peerID = contextProvider?.getPeerIDForNickname(nickname),
let fingerprint = meshService?.getFingerprint(for: peerID) { let fingerprint = meshService?.getFingerprint(for: peerID) {
if !identityManager.isBlocked(fingerprint: fingerprint) { if !identityManager.isBlocked(fingerprint: fingerprint) {
return .success(message: "\(nickname) is not blocked") return .success(message: "\(nickname) is not blocked")
@@ -263,7 +311,7 @@ final class CommandProcessor {
return .success(message: "unblocked \(nickname)") return .success(message: "unblocked \(nickname)")
} }
// Try geohash unblock // Try geohash unblock
if let pub = chatViewModel?.nostrPubkeyForDisplayName(nickname) { if let pub = contextProvider?.nostrPubkeyForDisplayName(nickname) {
if !identityManager.isNostrBlocked(pubkeyHexLowercased: pub) { if !identityManager.isNostrBlocked(pubkeyHexLowercased: pub) {
return .success(message: "\(nickname) is not blocked") return .success(message: "\(nickname) is not blocked")
} }
@@ -281,7 +329,7 @@ final class CommandProcessor {
let nickname = targetName.hasPrefix("@") ? String(targetName.dropFirst()) : targetName let nickname = targetName.hasPrefix("@") ? String(targetName.dropFirst()) : targetName
guard let peerID = chatViewModel?.getPeerIDForNickname(nickname), guard let peerID = contextProvider?.getPeerIDForNickname(nickname),
let noisePublicKey = Data(hexString: peerID.id) else { let noisePublicKey = Data(hexString: peerID.id) else {
return .error(message: "can't find peer: \(nickname)") return .error(message: "can't find peer: \(nickname)")
} }
@@ -294,15 +342,15 @@ final class CommandProcessor {
peerNickname: nickname peerNickname: nickname
) )
chatViewModel?.toggleFavorite(peerID: peerID) contextProvider?.toggleFavorite(peerID: peerID)
chatViewModel?.sendFavoriteNotification(to: peerID, isFavorite: true) contextProvider?.sendFavoriteNotification(to: peerID, isFavorite: true)
return .success(message: "added \(nickname) to favorites") return .success(message: "added \(nickname) to favorites")
} else { } else {
FavoritesPersistenceService.shared.removeFavorite(peerNoisePublicKey: noisePublicKey) FavoritesPersistenceService.shared.removeFavorite(peerNoisePublicKey: noisePublicKey)
chatViewModel?.toggleFavorite(peerID: peerID) contextProvider?.toggleFavorite(peerID: peerID)
chatViewModel?.sendFavoriteNotification(to: peerID, isFavorite: false) contextProvider?.sendFavoriteNotification(to: peerID, isFavorite: false)
return .success(message: "removed \(nickname) from favorites") return .success(message: "removed \(nickname) from favorites")
} }
@@ -26,7 +26,7 @@ final class FavoritesPersistenceService: ObservableObject {
private static let storageKey = "chat.bitchat.favorites" private static let storageKey = "chat.bitchat.favorites"
private static let keychainService = "chat.bitchat.favorites" private static let keychainService = "chat.bitchat.favorites"
private let keychain: KeychainHelperProtocol private let keychain: KeychainManagerProtocol
@Published private(set) var favorites: [Data: FavoriteRelationship] = [:] // Noise pubkey -> relationship @Published private(set) var favorites: [Data: FavoriteRelationship] = [:] // Noise pubkey -> relationship
@Published private(set) var mutualFavorites: Set<Data> = [] @Published private(set) var mutualFavorites: Set<Data> = []
@@ -36,7 +36,7 @@ final class FavoritesPersistenceService: ObservableObject {
static let shared = FavoritesPersistenceService() static let shared = FavoritesPersistenceService()
init(keychain: KeychainHelperProtocol = KeychainHelper()) { init(keychain: KeychainManagerProtocol = KeychainManager()) {
self.keychain = keychain self.keychain = keychain
loadFavorites() loadFavorites()
@@ -1,219 +0,0 @@
import Foundation
import Combine
#if os(iOS) || os(macOS)
import CoreLocation
#endif
/// Stores a user-maintained list of bookmarked geohash channels.
/// - Persistence: UserDefaults (JSON string array)
/// - Semantics: geohashes are normalized to lowercase base32 and de-duplicated
final class GeohashBookmarksStore: ObservableObject {
static let shared = GeohashBookmarksStore()
@Published private(set) var bookmarks: [String] = []
@Published private(set) var bookmarkNames: [String: String] = [:] // geohash -> friendly name
private let storeKey = "locationChannel.bookmarks"
private let namesStoreKey = "locationChannel.bookmarkNames"
private var membership: Set<String> = []
#if os(iOS) || os(macOS)
private let geocoder = CLGeocoder()
private var resolving: Set<String> = []
#endif
private let storage: UserDefaults
init(storage: UserDefaults = .standard) {
self.storage = storage
load()
}
// MARK: - Public API
func isBookmarked(_ geohash: String) -> Bool {
return membership.contains(Self.normalize(geohash))
}
func toggle(_ geohash: String) {
let gh = Self.normalize(geohash)
if membership.contains(gh) {
remove(gh)
} else {
add(gh)
}
}
func add(_ geohash: String) {
let gh = Self.normalize(geohash)
guard !gh.isEmpty else { return }
guard !membership.contains(gh) else { return }
bookmarks.insert(gh, at: 0)
membership.insert(gh)
persist()
// Resolve and persist a friendly name once when added
resolveNameIfNeeded(for: gh)
}
func remove(_ geohash: String) {
let gh = Self.normalize(geohash)
guard membership.contains(gh) else { return }
if let idx = bookmarks.firstIndex(of: gh) { bookmarks.remove(at: idx) }
membership.remove(gh)
// Clean up stored name to avoid stale cache growth
if bookmarkNames.removeValue(forKey: gh) != nil {
persistNames()
}
persist()
}
// MARK: - Persistence
private func load() {
guard let data = storage.data(forKey: storeKey) else { return }
if let arr = try? JSONDecoder().decode([String].self, from: data) {
// Sanitize, normalize, dedupe while preserving order (first occurrence wins)
var seen = Set<String>()
var list: [String] = []
for raw in arr {
let gh = Self.normalize(raw)
guard !gh.isEmpty else { continue }
if !seen.contains(gh) {
seen.insert(gh)
list.append(gh)
}
}
bookmarks = list
membership = seen
}
// Load any saved names
if let namesData = storage.data(forKey: namesStoreKey),
let dict = try? JSONDecoder().decode([String: String].self, from: namesData) {
bookmarkNames = dict
}
}
private func persist() {
if let data = try? JSONEncoder().encode(bookmarks) {
storage.set(data, forKey: storeKey)
}
}
private func persistNames() {
if let data = try? JSONEncoder().encode(bookmarkNames) {
storage.set(data, forKey: namesStoreKey)
}
}
// MARK: - Helpers
private static func normalize(_ s: String) -> String {
let allowed = Set("0123456789bcdefghjkmnpqrstuvwxyz")
return s
.trimmingCharacters(in: .whitespacesAndNewlines)
.lowercased()
.replacingOccurrences(of: "#", with: "")
.filter { allowed.contains($0) }
}
// MARK: - Name Resolution
/// Attempt to resolve and persist a friendly place name for a bookmarked geohash.
func resolveNameIfNeeded(for geohash: String) {
let gh = Self.normalize(geohash)
guard !gh.isEmpty else { return }
if bookmarkNames[gh] != nil { return }
#if os(iOS) || os(macOS)
if resolving.contains(gh) { return }
resolving.insert(gh)
// For very coarse geohashes, sample multiple points to capture multiple admin areas
if gh.count <= 2 {
let b = Geohash.decodeBounds(gh)
let pts: [CLLocation] = [
CLLocation(latitude: (b.latMin + b.latMax) / 2, longitude: (b.lonMin + b.lonMax) / 2), // center
CLLocation(latitude: b.latMin, longitude: b.lonMin),
CLLocation(latitude: b.latMin, longitude: b.lonMax),
CLLocation(latitude: b.latMax, longitude: b.lonMin),
CLLocation(latitude: b.latMax, longitude: b.lonMax)
]
resolveCompositeAdminName(geohash: gh, points: pts)
} else {
let center = Geohash.decodeCenter(gh)
let loc = CLLocation(latitude: center.lat, longitude: center.lon)
geocoder.reverseGeocodeLocation(loc) { [weak self] placemarks, _ in
guard let self = self else { return }
defer { self.resolving.remove(gh) }
if let pm = placemarks?.first {
let name = Self.nameForGeohashLength(gh.count, from: pm)
if let name = name, !name.isEmpty {
DispatchQueue.main.async {
self.bookmarkNames[gh] = name
self.persistNames()
}
}
}
}
}
#endif
}
#if os(iOS) || os(macOS)
private func resolveCompositeAdminName(geohash gh: String, points: [CLLocation]) {
var uniqueAdmins = OrderedSet<String>()
var idx = 0
func step() {
if idx >= points.count {
// Compose up to 2 names joined by ' and '
let finalName: String? = {
let names = uniqueAdmins.array
if names.count >= 2 { return names[0] + " and " + names[1] }
return names.first
}()
if let finalName = finalName, !finalName.isEmpty {
DispatchQueue.main.async {
self.bookmarkNames[gh] = finalName
self.persistNames()
}
}
self.resolving.remove(gh)
return
}
let loc = points[idx]
idx += 1
geocoder.reverseGeocodeLocation(loc) { [weak self] placemarks, _ in
guard self != nil else { return }
if let pm = placemarks?.first {
if let admin = pm.administrativeArea, !admin.isEmpty {
uniqueAdmins.insert(admin)
} else if let country = pm.country, !country.isEmpty {
uniqueAdmins.insert(country)
}
}
// Proceed to next point
step()
}
}
step()
}
// Minimal ordered-set for stable joining
private struct OrderedSet<Element: Hashable> {
private var set: Set<Element> = []
private(set) var array: [Element] = []
mutating func insert(_ element: Element) {
if set.insert(element).inserted { array.append(element) }
}
}
private static func nameForGeohashLength(_ len: Int, from pm: CLPlacemark) -> String? {
switch len {
case 0...2:
// Prefer administrative area if available at this coarse level
return pm.administrativeArea ?? pm.country
case 3...4:
return pm.administrativeArea ?? pm.subAdministrativeArea ?? pm.country
case 5:
return pm.locality ?? pm.subAdministrativeArea ?? pm.administrativeArea
case 6...7:
return pm.subLocality ?? pm.locality ?? pm.administrativeArea
default:
return pm.subLocality ?? pm.locality ?? pm.administrativeArea ?? pm.country
}
}
#endif
}
@@ -0,0 +1,159 @@
//
// GeohashParticipantTracker.swift
// bitchat
//
// Tracks participants in geohash-based location channels.
// This is free and unencumbered software released into the public domain.
//
import Foundation
/// Represents a participant in a geohash channel
public struct GeoPerson: Identifiable, Equatable, Sendable {
public let id: String // pubkey hex (lowercased)
public let displayName: String
public let lastSeen: Date
public init(id: String, displayName: String, lastSeen: Date) {
self.id = id
self.displayName = displayName
self.lastSeen = lastSeen
}
}
/// Protocol for resolving display names and checking block status
@MainActor
public protocol GeohashParticipantContext: AnyObject {
/// Returns display name for a Nostr pubkey (e.g., "alice#a1b2" or "anon#c3d4")
func displayNameForPubkey(_ pubkeyHex: String) -> String
/// Returns true if the pubkey is blocked
func isBlocked(_ pubkeyHexLowercased: String) -> Bool
}
/// Tracks participants across multiple geohash channels
@MainActor
public final class GeohashParticipantTracker: ObservableObject {
/// Activity cutoff duration (defaults to 5 minutes)
public let activityCutoff: TimeInterval
/// Per-geohash participant map: [geohash: [pubkeyHex: lastSeen]]
private var participants: [String: [String: Date]] = [:]
/// Currently visible people for the active geohash
@Published public private(set) var visiblePeople: [GeoPerson] = []
/// The currently active geohash (if any)
private var activeGeohash: String?
/// Context for display name resolution and block checking
private weak var context: GeohashParticipantContext?
/// Timer for periodic refresh
private var refreshTimer: Timer?
public init(activityCutoff: TimeInterval = -300) { // default 5 minutes
self.activityCutoff = activityCutoff
}
/// Configure with a context provider
public func configure(context: GeohashParticipantContext) {
self.context = context
}
/// Set the currently active geohash
public func setActiveGeohash(_ geohash: String?) {
activeGeohash = geohash
if geohash == nil {
visiblePeople = []
} else {
refresh()
}
}
/// Record activity from a participant in the current active geohash
public func recordParticipant(pubkeyHex: String) {
guard let gh = activeGeohash else { return }
recordParticipant(pubkeyHex: pubkeyHex, geohash: gh)
}
/// Record activity from a participant in a specific geohash
public func recordParticipant(pubkeyHex: String, geohash: String) {
let key = pubkeyHex.lowercased()
var map = participants[geohash] ?? [:]
map[key] = Date()
participants[geohash] = map
// Only refresh visible list if this geohash is currently active
if activeGeohash == geohash {
refresh()
}
}
/// Remove a participant from all geohashes (used when blocking)
public func removeParticipant(pubkeyHex: String) {
let key = pubkeyHex.lowercased()
for (gh, var map) in participants {
map.removeValue(forKey: key)
participants[gh] = map
}
refresh()
}
/// Get participant count for a specific geohash
public func participantCount(for geohash: String) -> Int {
let cutoff = Date().addingTimeInterval(activityCutoff)
let map = participants[geohash] ?? [:]
return map.values.filter { $0 >= cutoff }.count
}
/// Get the visible people list for the active geohash (read-only query)
public func getVisiblePeople() -> [GeoPerson] {
guard let gh = activeGeohash, let context = context else { return [] }
let cutoff = Date().addingTimeInterval(activityCutoff)
let map = (participants[gh] ?? [:])
.filter { $0.value >= cutoff }
.filter { !context.isBlocked($0.key) }
return map
.map { (pub, seen) in
GeoPerson(id: pub, displayName: context.displayNameForPubkey(pub), lastSeen: seen)
}
.sorted { $0.lastSeen > $1.lastSeen }
}
/// Refresh the visible people list
public func refresh() {
visiblePeople = getVisiblePeople()
}
/// Start the periodic refresh timer
public func startRefreshTimer(interval: TimeInterval = 30.0) {
stopRefreshTimer()
refreshTimer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { [weak self] _ in
Task { @MainActor in
self?.refresh()
}
}
}
/// Stop the periodic refresh timer
public func stopRefreshTimer() {
refreshTimer?.invalidate()
refreshTimer = nil
}
/// Clear all participant data
public func clear() {
participants.removeAll()
visiblePeople = []
}
/// Clear participant data for a specific geohash
public func clear(geohash: String) {
participants.removeValue(forKey: geohash)
if activeGeohash == geohash {
visiblePeople = []
}
}
}
+53
View File
@@ -20,6 +20,14 @@ protocol KeychainManagerProtocol {
func secureClear(_ string: inout String) func secureClear(_ string: inout String)
func verifyIdentityKeyExists() -> Bool func verifyIdentityKeyExists() -> Bool
// MARK: - Generic Data Storage (consolidated from KeychainHelper)
/// Save data with a custom service name
func save(key: String, data: Data, service: String, accessible: CFString?)
/// Load data from a custom service
func load(key: String, service: String) -> Data?
/// Delete data from a custom service
func delete(key: String, service: String)
} }
final class KeychainManager: KeychainManagerProtocol { final class KeychainManager: KeychainManagerProtocol {
@@ -314,4 +322,49 @@ final class KeychainManager: KeychainManagerProtocol {
let key = "identity_noiseStaticKey" let key = "identity_noiseStaticKey"
return retrieveData(forKey: key) != nil return retrieveData(forKey: key) != nil
} }
// MARK: - Generic Data Storage (consolidated from KeychainHelper)
/// Save data with a custom service name
func save(key: String, data: Data, service customService: String, accessible: CFString?) {
var query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: customService,
kSecAttrAccount as String: key,
kSecValueData as String: data
]
if let accessible = accessible {
query[kSecAttrAccessible as String] = accessible
}
SecItemDelete(query as CFDictionary)
SecItemAdd(query as CFDictionary, nil)
}
/// Load data from a custom service
func load(key: String, service customService: String) -> Data? {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: customService,
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
}
/// Delete data from a custom service
func delete(key: String, service customService: String) {
let query: [String: Any] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrService as String: customService,
kSecAttrAccount as String: key
]
SecItemDelete(query as CFDictionary)
}
} }
@@ -1,306 +0,0 @@
import BitLogger
import Foundation
import Combine
#if os(iOS) || os(macOS)
import CoreLocation
/// Manages location permissions, one-shot location retrieval, and computing geohash channels.
/// Not main-actor isolated to satisfy CLLocationManagerDelegate in Swift 6; state updates hop to MainActor.
final class LocationChannelManager: NSObject, CLLocationManagerDelegate, ObservableObject {
static let shared = LocationChannelManager()
enum PermissionState: Equatable {
case notDetermined
case denied
case restricted
case authorized
}
private let cl = CLLocationManager()
private let geocoder = CLGeocoder()
private var lastLocation: CLLocation?
private var refreshTimer: Timer?
private let userDefaultsKey = "locationChannel.selected"
private let teleportedStoreKey = "locationChannel.teleportedSet"
private var isGeocoding: Bool = false
// Published state for UI bindings
@Published private(set) var permissionState: PermissionState = .notDetermined
@Published private(set) var availableChannels: [GeohashChannel] = []
@Published private(set) var selectedChannel: ChannelID = .mesh
// True when the current location channel was selected via manual teleport
@Published var teleported: Bool = false
@Published private(set) var locationNames: [GeohashChannelLevel: String] = [:]
// Persisted set of geohashes that were selected via teleport
private var teleportedSet: Set<String> = []
private override init() {
super.init()
cl.delegate = self
cl.desiredAccuracy = kCLLocationAccuracyHundredMeters
cl.distanceFilter = TransportConfig.locationDistanceFilterMeters // meters; we're not tracking continuously
// Load selection
if let data = UserDefaults.standard.data(forKey: userDefaultsKey),
let channel = try? JSONDecoder().decode(ChannelID.self, from: data) {
selectedChannel = channel
}
// Load persisted teleported set
if let data = UserDefaults.standard.data(forKey: teleportedStoreKey),
let arr = try? JSONDecoder().decode([String].self, from: data) {
teleportedSet = Set(arr)
}
// Do not eagerly mark teleported on startup; wait for location to compute regional set.
// This avoids showing teleported for in-region channels during cold start.
let status: CLAuthorizationStatus
if #available(iOS 14.0, macOS 11.0, *) {
status = cl.authorizationStatus
} else {
status = CLLocationManager.authorizationStatus()
}
updatePermissionState(from: status)
// If we don't have location authorization at startup, fall back to persisted teleport state
switch status {
case .authorizedAlways, .authorizedWhenInUse, .authorized:
break // will compute from location
case .notDetermined, .restricted, .denied:
fallthrough
@unknown default:
if case .location(let ch) = selectedChannel {
teleported = teleportedSet.contains(ch.geohash)
}
}
}
// MARK: - Public API
func enableLocationChannels() {
let status: CLAuthorizationStatus
if #available(iOS 14.0, macOS 11.0, *) {
status = cl.authorizationStatus
} else {
status = CLLocationManager.authorizationStatus()
}
switch status {
case .notDetermined:
cl.requestWhenInUseAuthorization()
case .restricted:
Task { @MainActor in self.permissionState = .restricted }
case .denied:
Task { @MainActor in self.permissionState = .denied }
case .authorizedAlways, .authorizedWhenInUse, .authorized:
Task { @MainActor in self.permissionState = .authorized }
requestOneShotLocation()
@unknown default:
Task { @MainActor in self.permissionState = .restricted }
}
}
func refreshChannels() {
if permissionState == .authorized {
requestOneShotLocation()
}
}
/// Begin continuous, distance-filtered updates while the channel sheet is visible.
/// Uses a 21m filter (configurable) to only refresh on meaningful movement.
func beginLiveRefresh(interval: TimeInterval = TransportConfig.locationLiveRefreshInterval) {
guard permissionState == .authorized else { return }
// Stop any previous polling timer
refreshTimer?.invalidate()
refreshTimer = nil
// Tighten accuracy and distance filter for live view
cl.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
cl.distanceFilter = TransportConfig.locationDistanceFilterLiveMeters
// Start continuous updates
cl.startUpdatingLocation()
// Request an immediate fix to populate UI without waiting for movement
requestOneShotLocation()
}
/// Stop continuous refreshes when selector UI is dismissed.
func endLiveRefresh() {
refreshTimer?.invalidate()
refreshTimer = nil
cl.stopUpdatingLocation()
// Restore more relaxed defaults for background/idle state
cl.desiredAccuracy = kCLLocationAccuracyHundredMeters
cl.distanceFilter = TransportConfig.locationDistanceFilterMeters
}
func select(_ channel: ChannelID) {
Task { @MainActor in
self.selectedChannel = channel
if let data = try? JSONEncoder().encode(channel) {
UserDefaults.standard.set(data, forKey: self.userDefaultsKey)
}
// Update teleported flag based on persisted state for immediate UI behavior
switch channel {
case .mesh:
self.teleported = false
case .location(let ch):
// If this geohash is in our current regional set, do NOT mark teleported.
let inRegional = self.availableChannels.contains { $0.geohash == ch.geohash }
if inRegional {
self.teleported = false
// Clear persisted teleport for this geohash to keep future selections clean
if self.teleportedSet.contains(ch.geohash) {
self.teleportedSet.remove(ch.geohash)
if let data = try? JSONEncoder().encode(Array(self.teleportedSet)) {
UserDefaults.standard.set(data, forKey: self.teleportedStoreKey)
}
}
} else {
// Fall back to persisted mark (set by deep link or manual teleport)
self.teleported = self.teleportedSet.contains(ch.geohash)
}
}
}
}
// Mark or unmark a geohash as teleported in persistence and update current flag if relevant
func markTeleported(for geohash: String, _ flag: Bool) {
if flag { teleportedSet.insert(geohash) } else { teleportedSet.remove(geohash) }
if let data = try? JSONEncoder().encode(Array(teleportedSet)) {
UserDefaults.standard.set(data, forKey: teleportedStoreKey)
}
if case .location(let ch) = selectedChannel, ch.geohash == geohash {
Task { @MainActor in self.teleported = flag }
}
}
// MARK: - CoreLocation
private func requestOneShotLocation() {
cl.requestLocation()
}
// iOS < 14
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
updatePermissionState(from: status)
if case .authorized = permissionState {
requestOneShotLocation()
}
}
// iOS 14+ / macOS 11+
@available(iOS 14.0, macOS 11.0, *)
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
updatePermissionState(from: manager.authorizationStatus)
if case .authorized = permissionState {
requestOneShotLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let loc = locations.last else { return }
lastLocation = loc
computeChannels(from: loc.coordinate)
reverseGeocodeIfNeeded(location: loc)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
// Surface as denied/restricted if relevant; otherwise keep previous state
SecureLogger.error("LocationChannelManager: location error: \(error.localizedDescription)", category: .session)
}
// MARK: - Helpers
private func updatePermissionState(from status: CLAuthorizationStatus) {
let newState: PermissionState
switch status {
case .notDetermined: newState = .notDetermined
case .restricted: newState = .restricted
case .denied: newState = .denied
case .authorizedAlways, .authorizedWhenInUse, .authorized: newState = .authorized
@unknown default: newState = .restricted
}
Task { @MainActor in self.permissionState = newState }
}
private func computeChannels(from coord: CLLocationCoordinate2D) {
let levels = GeohashChannelLevel.allCases
var result: [GeohashChannel] = []
for level in levels {
let gh = Geohash.encode(latitude: coord.latitude, longitude: coord.longitude, precision: level.precision)
result.append(GeohashChannel(level: level, geohash: gh))
}
Task { @MainActor in
self.availableChannels = result
// Recompute teleported status based on whether the selected geohash is in our regional set
switch self.selectedChannel {
case .mesh:
self.teleported = false
case .location(let ch):
// Membership check using freshly computed regional channels; avoids precision/rename drift
let inRegional = result.contains { $0.geohash == ch.geohash }
if inRegional {
self.teleported = false
// Clear persisted teleport flag if present
if self.teleportedSet.contains(ch.geohash) {
self.teleportedSet.remove(ch.geohash)
if let data = try? JSONEncoder().encode(Array(self.teleportedSet)) {
UserDefaults.standard.set(data, forKey: self.teleportedStoreKey)
}
}
} else {
self.teleported = true
}
}
}
}
private func reverseGeocodeIfNeeded(location: CLLocation) {
// Always cancel previous to keep latest fresh while user moves
geocoder.cancelGeocode()
isGeocoding = true
geocoder.reverseGeocodeLocation(location) { [weak self] placemarks, error in
guard let self = self else { return }
self.isGeocoding = false
if let pm = placemarks?.first {
let names = self.namesByLevel(from: pm)
Task { @MainActor in self.locationNames = names }
}
}
}
private func namesByLevel(from pm: CLPlacemark) -> [GeohashChannelLevel: String] {
var dict: [GeohashChannelLevel: String] = [:]
// Region (country)
if let country = pm.country, !country.isEmpty {
dict[.region] = country
}
// Province (state/province or county)
if let admin = pm.administrativeArea, !admin.isEmpty {
dict[.province] = admin
} else if let subAdmin = pm.subAdministrativeArea, !subAdmin.isEmpty {
dict[.province] = subAdmin
}
// City (locality)
if let locality = pm.locality, !locality.isEmpty {
dict[.city] = locality
} else if let subAdmin = pm.subAdministrativeArea, !subAdmin.isEmpty {
dict[.city] = subAdmin
} else if let admin = pm.administrativeArea, !admin.isEmpty {
dict[.city] = admin
}
// Neighborhood
if let subLocality = pm.subLocality, !subLocality.isEmpty {
dict[.neighborhood] = subLocality
} else if let locality = pm.locality, !locality.isEmpty {
dict[.neighborhood] = locality
}
// Block: reuse neighborhood/locality granularity
if let subLocality = pm.subLocality, !subLocality.isEmpty {
dict[.block] = subLocality
} else if let locality = pm.locality, !locality.isEmpty {
dict[.block] = locality
}
// Building: prefer place name/street/venue when available
if let name = pm.name, !name.isEmpty {
dict[.building] = name
} else if let thoroughfare = pm.thoroughfare, !thoroughfare.isEmpty {
dict[.building] = thoroughfare
}
return dict
}
}
#endif
+545
View File
@@ -0,0 +1,545 @@
import BitLogger
import Foundation
import Combine
#if os(iOS) || os(macOS)
import CoreLocation
/// Unified manager for location-based channel state including:
/// - CoreLocation permissions and one-shot location retrieval
/// - Geohash channel computation from coordinates
/// - Channel selection and teleport state
/// - Bookmark persistence and friendly name resolution
///
/// Consolidates LocationChannelManager + GeohashBookmarksStore into a single source of truth.
final class LocationStateManager: NSObject, CLLocationManagerDelegate, ObservableObject {
static let shared = LocationStateManager()
// MARK: - Permission State
enum PermissionState: Equatable {
case notDetermined
case denied
case restricted
case authorized
}
// MARK: - Private Properties (CoreLocation)
private let cl = CLLocationManager()
private let geocoder = CLGeocoder()
private var lastLocation: CLLocation?
private var refreshTimer: Timer?
private var isGeocoding: Bool = false
// MARK: - Persistence Keys
private let selectedChannelKey = "locationChannel.selected"
private let teleportedStoreKey = "locationChannel.teleportedSet"
private let bookmarksKey = "locationChannel.bookmarks"
private let bookmarkNamesKey = "locationChannel.bookmarkNames"
// MARK: - Published State (Channel)
@Published private(set) var permissionState: PermissionState = .notDetermined
@Published private(set) var availableChannels: [GeohashChannel] = []
@Published private(set) var selectedChannel: ChannelID = .mesh
@Published var teleported: Bool = false
@Published private(set) var locationNames: [GeohashChannelLevel: String] = [:]
// MARK: - Published State (Bookmarks)
@Published private(set) var bookmarks: [String] = []
@Published private(set) var bookmarkNames: [String: String] = [:]
// MARK: - Private State
private var teleportedSet: Set<String> = []
private var bookmarkMembership: Set<String> = []
private var resolvingNames: Set<String> = []
private let storage: UserDefaults
/// Returns true if running in test environment
private static var isRunningTests: Bool {
let env = ProcessInfo.processInfo.environment
return NSClassFromString("XCTestCase") != nil ||
env["XCTestConfigurationFilePath"] != nil ||
env["XCTestBundlePath"] != nil ||
env["GITHUB_ACTIONS"] != nil ||
env["CI"] != nil
}
// MARK: - Initialization
private override init() {
self.storage = .standard
super.init()
// Skip CoreLocation setup in test environments
guard !Self.isRunningTests else {
loadPersistedState()
return
}
cl.delegate = self
cl.desiredAccuracy = kCLLocationAccuracyHundredMeters
cl.distanceFilter = TransportConfig.locationDistanceFilterMeters
loadPersistedState()
initializePermissionState()
}
/// Internal initializer for testing with custom storage
init(storage: UserDefaults) {
self.storage = storage
super.init()
loadPersistedState()
}
private func loadPersistedState() {
// Load selected channel
if let data = storage.data(forKey: selectedChannelKey),
let channel = try? JSONDecoder().decode(ChannelID.self, from: data) {
selectedChannel = channel
}
// Load teleported set
if let data = storage.data(forKey: teleportedStoreKey),
let arr = try? JSONDecoder().decode([String].self, from: data) {
teleportedSet = Set(arr)
}
// Load bookmarks
if let data = storage.data(forKey: bookmarksKey),
let arr = try? JSONDecoder().decode([String].self, from: data) {
var seen = Set<String>()
var list: [String] = []
for raw in arr {
let gh = Self.normalizeGeohash(raw)
guard !gh.isEmpty, !seen.contains(gh) else { continue }
seen.insert(gh)
list.append(gh)
}
bookmarks = list
bookmarkMembership = seen
}
// Load bookmark names
if let data = storage.data(forKey: bookmarkNamesKey),
let dict = try? JSONDecoder().decode([String: String].self, from: data) {
bookmarkNames = dict
}
}
private func initializePermissionState() {
let status: CLAuthorizationStatus
if #available(iOS 14.0, macOS 11.0, *) {
status = cl.authorizationStatus
} else {
status = CLLocationManager.authorizationStatus()
}
updatePermissionState(from: status)
// Fall back to persisted teleport state if no location authorization
switch status {
case .authorizedAlways, .authorizedWhenInUse, .authorized:
break
case .notDetermined, .restricted, .denied:
fallthrough
@unknown default:
if case .location(let ch) = selectedChannel {
teleported = teleportedSet.contains(ch.geohash)
}
}
}
// MARK: - Public API (Permissions & Location)
func enableLocationChannels() {
let status: CLAuthorizationStatus
if #available(iOS 14.0, macOS 11.0, *) {
status = cl.authorizationStatus
} else {
status = CLLocationManager.authorizationStatus()
}
switch status {
case .notDetermined:
cl.requestWhenInUseAuthorization()
case .restricted:
Task { @MainActor in self.permissionState = .restricted }
case .denied:
Task { @MainActor in self.permissionState = .denied }
case .authorizedAlways, .authorizedWhenInUse, .authorized:
Task { @MainActor in self.permissionState = .authorized }
requestOneShotLocation()
@unknown default:
Task { @MainActor in self.permissionState = .restricted }
}
}
func refreshChannels() {
if permissionState == .authorized {
requestOneShotLocation()
}
}
func beginLiveRefresh(interval: TimeInterval = TransportConfig.locationLiveRefreshInterval) {
guard permissionState == .authorized else { return }
refreshTimer?.invalidate()
refreshTimer = nil
cl.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
cl.distanceFilter = TransportConfig.locationDistanceFilterLiveMeters
cl.startUpdatingLocation()
requestOneShotLocation()
}
func endLiveRefresh() {
refreshTimer?.invalidate()
refreshTimer = nil
cl.stopUpdatingLocation()
cl.desiredAccuracy = kCLLocationAccuracyHundredMeters
cl.distanceFilter = TransportConfig.locationDistanceFilterMeters
}
// MARK: - Public API (Channel Selection)
func select(_ channel: ChannelID) {
Task { @MainActor in
self.selectedChannel = channel
if let data = try? JSONEncoder().encode(channel) {
self.storage.set(data, forKey: self.selectedChannelKey)
}
switch channel {
case .mesh:
self.teleported = false
case .location(let ch):
let inRegional = self.availableChannels.contains { $0.geohash == ch.geohash }
if inRegional {
self.teleported = false
if self.teleportedSet.contains(ch.geohash) {
self.teleportedSet.remove(ch.geohash)
self.persistTeleportedSet()
}
} else {
self.teleported = self.teleportedSet.contains(ch.geohash)
}
}
}
}
func markTeleported(for geohash: String, _ flag: Bool) {
if flag {
teleportedSet.insert(geohash)
} else {
teleportedSet.remove(geohash)
}
persistTeleportedSet()
if case .location(let ch) = selectedChannel, ch.geohash == geohash {
Task { @MainActor in self.teleported = flag }
}
}
// MARK: - Public API (Bookmarks)
func isBookmarked(_ geohash: String) -> Bool {
bookmarkMembership.contains(Self.normalizeGeohash(geohash))
}
func toggleBookmark(_ geohash: String) {
let gh = Self.normalizeGeohash(geohash)
if bookmarkMembership.contains(gh) {
removeBookmark(gh)
} else {
addBookmark(gh)
}
}
func addBookmark(_ geohash: String) {
let gh = Self.normalizeGeohash(geohash)
guard !gh.isEmpty, !bookmarkMembership.contains(gh) else { return }
bookmarks.insert(gh, at: 0)
bookmarkMembership.insert(gh)
persistBookmarks()
resolveBookmarkNameIfNeeded(for: gh)
}
func removeBookmark(_ geohash: String) {
let gh = Self.normalizeGeohash(geohash)
guard bookmarkMembership.contains(gh) else { return }
if let idx = bookmarks.firstIndex(of: gh) {
bookmarks.remove(at: idx)
}
bookmarkMembership.remove(gh)
if bookmarkNames.removeValue(forKey: gh) != nil {
persistBookmarkNames()
}
persistBookmarks()
}
// MARK: - CLLocationManagerDelegate
private func requestOneShotLocation() {
cl.requestLocation()
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
updatePermissionState(from: status)
if case .authorized = permissionState {
requestOneShotLocation()
}
}
@available(iOS 14.0, macOS 11.0, *)
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
updatePermissionState(from: manager.authorizationStatus)
if case .authorized = permissionState {
requestOneShotLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let loc = locations.last else { return }
lastLocation = loc
computeChannels(from: loc.coordinate)
reverseGeocodeLocation(loc)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
SecureLogger.error("LocationStateManager: location error: \(error.localizedDescription)", category: .session)
}
// MARK: - Private Helpers (Permission)
private func updatePermissionState(from status: CLAuthorizationStatus) {
let newState: PermissionState
switch status {
case .notDetermined: newState = .notDetermined
case .restricted: newState = .restricted
case .denied: newState = .denied
case .authorizedAlways, .authorizedWhenInUse, .authorized: newState = .authorized
@unknown default: newState = .restricted
}
Task { @MainActor in self.permissionState = newState }
}
// MARK: - Private Helpers (Channel Computation)
private func computeChannels(from coord: CLLocationCoordinate2D) {
let levels = GeohashChannelLevel.allCases
var result: [GeohashChannel] = []
for level in levels {
let gh = Geohash.encode(latitude: coord.latitude, longitude: coord.longitude, precision: level.precision)
result.append(GeohashChannel(level: level, geohash: gh))
}
Task { @MainActor in
self.availableChannels = result
switch self.selectedChannel {
case .mesh:
self.teleported = false
case .location(let ch):
let inRegional = result.contains { $0.geohash == ch.geohash }
if inRegional {
self.teleported = false
if self.teleportedSet.contains(ch.geohash) {
self.teleportedSet.remove(ch.geohash)
self.persistTeleportedSet()
}
} else {
self.teleported = true
}
}
}
}
// MARK: - Private Helpers (Geocoding)
private func reverseGeocodeLocation(_ location: CLLocation) {
geocoder.cancelGeocode()
isGeocoding = true
geocoder.reverseGeocodeLocation(location) { [weak self] placemarks, _ in
guard let self = self else { return }
self.isGeocoding = false
if let pm = placemarks?.first {
let names = self.locationNamesByLevel(from: pm)
Task { @MainActor in self.locationNames = names }
}
}
}
private func locationNamesByLevel(from pm: CLPlacemark) -> [GeohashChannelLevel: String] {
var dict: [GeohashChannelLevel: String] = [:]
if let country = pm.country, !country.isEmpty {
dict[.region] = country
}
if let admin = pm.administrativeArea, !admin.isEmpty {
dict[.province] = admin
} else if let subAdmin = pm.subAdministrativeArea, !subAdmin.isEmpty {
dict[.province] = subAdmin
}
if let locality = pm.locality, !locality.isEmpty {
dict[.city] = locality
} else if let subAdmin = pm.subAdministrativeArea, !subAdmin.isEmpty {
dict[.city] = subAdmin
} else if let admin = pm.administrativeArea, !admin.isEmpty {
dict[.city] = admin
}
if let subLocality = pm.subLocality, !subLocality.isEmpty {
dict[.neighborhood] = subLocality
} else if let locality = pm.locality, !locality.isEmpty {
dict[.neighborhood] = locality
}
if let subLocality = pm.subLocality, !subLocality.isEmpty {
dict[.block] = subLocality
} else if let locality = pm.locality, !locality.isEmpty {
dict[.block] = locality
}
if let name = pm.name, !name.isEmpty {
dict[.building] = name
} else if let thoroughfare = pm.thoroughfare, !thoroughfare.isEmpty {
dict[.building] = thoroughfare
}
return dict
}
func resolveBookmarkNameIfNeeded(for geohash: String) {
let gh = Self.normalizeGeohash(geohash)
guard !gh.isEmpty, bookmarkNames[gh] == nil, !resolvingNames.contains(gh) else { return }
resolvingNames.insert(gh)
if gh.count <= 2 {
let b = Geohash.decodeBounds(gh)
let pts: [CLLocation] = [
CLLocation(latitude: (b.latMin + b.latMax) / 2, longitude: (b.lonMin + b.lonMax) / 2),
CLLocation(latitude: b.latMin, longitude: b.lonMin),
CLLocation(latitude: b.latMin, longitude: b.lonMax),
CLLocation(latitude: b.latMax, longitude: b.lonMin),
CLLocation(latitude: b.latMax, longitude: b.lonMax)
]
resolveCompositeAdminName(geohash: gh, points: pts)
} else {
let center = Geohash.decodeCenter(gh)
let loc = CLLocation(latitude: center.lat, longitude: center.lon)
geocoder.reverseGeocodeLocation(loc) { [weak self] placemarks, _ in
guard let self = self else { return }
defer { self.resolvingNames.remove(gh) }
if let pm = placemarks?.first,
let name = Self.nameForGeohashLength(gh.count, from: pm),
!name.isEmpty {
DispatchQueue.main.async {
self.bookmarkNames[gh] = name
self.persistBookmarkNames()
}
}
}
}
}
private func resolveCompositeAdminName(geohash gh: String, points: [CLLocation]) {
var uniqueAdmins: [String] = []
var seenAdmins = Set<String>()
var idx = 0
func step() {
if idx >= points.count {
let finalName: String? = {
if uniqueAdmins.count >= 2 { return uniqueAdmins[0] + " and " + uniqueAdmins[1] }
return uniqueAdmins.first
}()
if let finalName = finalName, !finalName.isEmpty {
DispatchQueue.main.async {
self.bookmarkNames[gh] = finalName
self.persistBookmarkNames()
}
}
self.resolvingNames.remove(gh)
return
}
let loc = points[idx]
idx += 1
geocoder.reverseGeocodeLocation(loc) { [weak self] placemarks, _ in
guard self != nil else { return }
if let pm = placemarks?.first {
if let admin = pm.administrativeArea, !admin.isEmpty, !seenAdmins.contains(admin) {
seenAdmins.insert(admin)
uniqueAdmins.append(admin)
} else if let country = pm.country, !country.isEmpty, !seenAdmins.contains(country) {
seenAdmins.insert(country)
uniqueAdmins.append(country)
}
}
step()
}
}
step()
}
private static func nameForGeohashLength(_ len: Int, from pm: CLPlacemark) -> String? {
switch len {
case 0...2:
return pm.administrativeArea ?? pm.country
case 3...4:
return pm.administrativeArea ?? pm.subAdministrativeArea ?? pm.country
case 5:
return pm.locality ?? pm.subAdministrativeArea ?? pm.administrativeArea
case 6...7:
return pm.subLocality ?? pm.locality ?? pm.administrativeArea
default:
return pm.subLocality ?? pm.locality ?? pm.administrativeArea ?? pm.country
}
}
// MARK: - Private Helpers (Persistence)
private func persistTeleportedSet() {
if let data = try? JSONEncoder().encode(Array(teleportedSet)) {
storage.set(data, forKey: teleportedStoreKey)
}
}
private func persistBookmarks() {
if let data = try? JSONEncoder().encode(bookmarks) {
storage.set(data, forKey: bookmarksKey)
}
}
private func persistBookmarkNames() {
if let data = try? JSONEncoder().encode(bookmarkNames) {
storage.set(data, forKey: bookmarkNamesKey)
}
}
private static func normalizeGeohash(_ s: String) -> String {
let allowed = Set("0123456789bcdefghjkmnpqrstuvwxyz")
return s
.trimmingCharacters(in: .whitespacesAndNewlines)
.lowercased()
.replacingOccurrences(of: "#", with: "")
.filter { allowed.contains($0) }
}
}
// MARK: - Backward Compatibility Typealiases
typealias LocationChannelManager = LocationStateManager
typealias GeohashBookmarksStore = LocationStateManager
// MARK: - Backward Compatibility Extensions
extension LocationStateManager {
/// Backward compatibility: toggle bookmark (was GeohashBookmarksStore.toggle)
func toggle(_ geohash: String) {
toggleBookmark(geohash)
}
/// Backward compatibility: add bookmark (was GeohashBookmarksStore.add)
func add(_ geohash: String) {
addBookmark(geohash)
}
/// Backward compatibility: remove bookmark (was GeohashBookmarksStore.remove)
func remove(_ geohash: String) {
removeBookmark(geohash)
}
}
#endif
@@ -0,0 +1,278 @@
//
// MessageDeduplicationService.swift
// bitchat
//
// Handles message deduplication using LRU caches.
// This is free and unencumbered software released into the public domain.
//
import Foundation
// MARK: - LRU Deduplication Cache
/// Generic LRU (Least Recently Used) cache for deduplication.
/// Uses an efficient O(1) lookup with periodic compaction.
/// Thread-safe via @MainActor - all callers are already on main actor.
@MainActor
final class LRUDeduplicationCache<Value> {
private var map: [String: Value] = [:]
private var order: [String] = []
private var head: Int = 0
private let capacity: Int
/// Creates a new LRU cache with the specified capacity.
/// - Parameter capacity: Maximum number of entries before eviction
init(capacity: Int) {
precondition(capacity > 0, "LRU cache capacity must be positive")
self.capacity = capacity
}
/// Number of active entries in the cache
var count: Int {
order.count - head
}
/// Checks if a key exists in the cache
func contains(_ key: String) -> Bool {
map[key] != nil
}
/// Gets the value for a key, or nil if not present
func value(for key: String) -> Value? {
map[key]
}
/// Records a key-value pair, updating if exists or inserting if new
func record(_ key: String, value: Value) {
if map[key] == nil {
order.append(key)
}
map[key] = value
trimIfNeeded()
}
/// Removes a specific key from the cache
func remove(_ key: String) {
map.removeValue(forKey: key)
// Note: key remains in order array but will be skipped during eviction
}
/// Clears all entries from the cache
func clear() {
map.removeAll()
order.removeAll()
head = 0
}
// MARK: - Private
private func trimIfNeeded() {
let activeCount = order.count - head
guard activeCount > capacity else { return }
let overflow = activeCount - capacity
for _ in 0..<overflow {
guard let victim = popOldest() else { break }
map.removeValue(forKey: victim)
}
}
private func popOldest() -> String? {
// Skip keys that were already removed from map
while head < order.count {
let key = order[head]
head += 1
// Periodically compact the backing storage
if head >= 32 && head * 2 >= order.count {
order.removeFirst(head)
head = 0
}
// Only return if key is still in map
if map[key] != nil {
return key
}
}
return nil
}
}
// MARK: - Content Normalizer
/// Normalizes message content for near-duplicate detection.
enum ContentNormalizer {
/// Regex to simplify HTTP URLs by stripping query strings and fragments
private static let simplifyHTTPURL: NSRegularExpression = {
try! NSRegularExpression(
pattern: "https?://[^\\s?#]+(?:[?#][^\\s]*)?",
options: [.caseInsensitive]
)
}()
/// Normalizes content for deduplication comparison.
/// - Parameters:
/// - content: The raw message content
/// - prefixLength: Maximum characters to consider (default from TransportConfig)
/// - Returns: A hash-based key for comparison
static func normalizedKey(
_ content: String,
prefixLength: Int = TransportConfig.contentKeyPrefixLength
) -> String {
// Lowercase for case-insensitive comparison
let lowered = content.lowercased()
let ns = lowered as NSString
let range = NSRange(location: 0, length: ns.length)
// Simplify URLs by stripping query/fragment
var simplified = ""
var last = 0
for match in simplifyHTTPURL.matches(in: lowered, options: [], range: range) {
if match.range.location > last {
simplified += ns.substring(with: NSRange(location: last, length: match.range.location - last))
}
let url = ns.substring(with: match.range)
if let queryIndex = url.firstIndex(where: { $0 == "?" || $0 == "#" }) {
simplified += String(url[..<queryIndex])
} else {
simplified += url
}
last = match.range.location + match.range.length
}
if last < ns.length {
simplified += ns.substring(with: NSRange(location: last, length: ns.length - last))
}
// Trim and collapse whitespace
let trimmed = simplified.trimmingCharacters(in: .whitespacesAndNewlines)
let collapsed = trimmed.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
// Take prefix and hash
let prefix = String(collapsed.prefix(prefixLength))
let hash = prefix.djb2()
return String(format: "h:%016llx", hash)
}
}
// MARK: - Message Deduplication Service
/// Service that manages message deduplication using LRU caches.
/// Provides separate caches for content-based dedup and Nostr event ID dedup.
/// Thread-safe via @MainActor - all callers are already on main actor.
@MainActor
final class MessageDeduplicationService {
/// Cache for content-based near-duplicate detection
private let contentCache: LRUDeduplicationCache<Date>
/// Cache for Nostr event ID deduplication
private let nostrEventCache: LRUDeduplicationCache<Bool>
/// Cache for Nostr ACK deduplication (messageId:ackType:senderPubkey format)
private let nostrAckCache: LRUDeduplicationCache<Bool>
/// Creates a new deduplication service with specified capacities.
/// - Parameters:
/// - contentCapacity: Max entries for content cache
/// - nostrEventCapacity: Max entries for Nostr event cache
init(
contentCapacity: Int = TransportConfig.contentLRUCap,
nostrEventCapacity: Int = TransportConfig.uiProcessedNostrEventsCap
) {
self.contentCache = LRUDeduplicationCache(capacity: contentCapacity)
self.nostrEventCache = LRUDeduplicationCache(capacity: nostrEventCapacity)
self.nostrAckCache = LRUDeduplicationCache(capacity: nostrEventCapacity)
}
// MARK: - Content Deduplication
/// Records content with its timestamp for near-duplicate detection.
/// - Parameters:
/// - content: The message content
/// - timestamp: When the content was received
func recordContent(_ content: String, timestamp: Date) {
let key = ContentNormalizer.normalizedKey(content)
contentCache.record(key, value: timestamp)
}
/// Records a pre-normalized content key with its timestamp.
/// - Parameters:
/// - key: The normalized content key
/// - timestamp: When the content was received
func recordContentKey(_ key: String, timestamp: Date) {
contentCache.record(key, value: timestamp)
}
/// Gets the timestamp for previously seen content.
/// - Parameter content: The message content
/// - Returns: The timestamp when first seen, or nil if not seen
func contentTimestamp(for content: String) -> Date? {
let key = ContentNormalizer.normalizedKey(content)
return contentCache.value(for: key)
}
/// Gets the timestamp for a pre-normalized content key.
/// - Parameter key: The normalized content key
/// - Returns: The timestamp when first seen, or nil if not seen
func contentTimestamp(forKey key: String) -> Date? {
contentCache.value(for: key)
}
/// Normalizes content to a deduplication key.
/// - Parameter content: The raw content
/// - Returns: A normalized hash key
func normalizedContentKey(_ content: String) -> String {
ContentNormalizer.normalizedKey(content)
}
// MARK: - Nostr Event Deduplication
/// Checks if a Nostr event has already been processed.
/// - Parameter eventId: The event ID
/// - Returns: true if already processed
func hasProcessedNostrEvent(_ eventId: String) -> Bool {
nostrEventCache.contains(eventId)
}
/// Records a Nostr event as processed.
/// - Parameter eventId: The event ID
func recordNostrEvent(_ eventId: String) {
nostrEventCache.record(eventId, value: true)
}
// MARK: - Nostr ACK Deduplication
/// Checks if a Nostr ACK has already been processed.
/// - Parameter ackKey: The ACK key in format "messageId:ackType:senderPubkey"
/// - Returns: true if already processed
func hasProcessedNostrAck(_ ackKey: String) -> Bool {
nostrAckCache.contains(ackKey)
}
/// Records a Nostr ACK as processed.
/// - Parameter ackKey: The ACK key in format "messageId:ackType:senderPubkey"
func recordNostrAck(_ ackKey: String) {
nostrAckCache.record(ackKey, value: true)
}
/// Creates an ACK key from components.
static func ackKey(messageId: String, ackType: String, senderPubkey: String) -> String {
"\(messageId):\(ackType):\(senderPubkey)"
}
// MARK: - Clear
/// Clears all caches
func clearAll() {
contentCache.clear()
nostrEventCache.clear()
nostrAckCache.clear()
}
/// Clears only the Nostr caches (events and ACKs)
func clearNostrCaches() {
nostrEventCache.clear()
nostrAckCache.clear()
}
}
@@ -0,0 +1,471 @@
//
// MessageFormattingEngine.swift
// bitchat
//
// Handles message text formatting, including mentions, hashtags, URLs, and tokens.
// This is free and unencumbered software released into the public domain.
//
import Foundation
import SwiftUI
// MARK: - Formatting Context Protocol
/// Protocol defining the context needed for message formatting.
/// Implemented by ChatViewModel to provide runtime state.
@MainActor
protocol MessageFormattingContext: AnyObject {
/// The user's current nickname
var nickname: String { get }
/// Determines if a message was sent by the current user
func isSelfMessage(_ message: BitchatMessage) -> Bool
/// Gets the color for a message's sender
func senderColor(for message: BitchatMessage, isDark: Bool) -> Color
/// Resolves a peer ID to a clickable URL
func peerURL(for peerID: PeerID) -> URL?
}
// MARK: - Formatting Engine
/// Handles rich text formatting for chat messages.
/// Extracts mentions, hashtags, URLs, Lightning invoices, and Cashu tokens.
final class MessageFormattingEngine {
// MARK: - Precompiled Regexes
/// Precompiled regex patterns for message content parsing
enum Patterns {
static let hashtag: NSRegularExpression = {
try! NSRegularExpression(pattern: "#([a-zA-Z0-9_]+)", options: [])
}()
static let mention: NSRegularExpression = {
try! NSRegularExpression(pattern: "@([\\p{L}0-9_]+(?:#[a-fA-F0-9]{4})?)", options: [])
}()
static let cashu: NSRegularExpression = {
try! NSRegularExpression(pattern: "\\bcashu[AB][A-Za-z0-9._-]{40,}\\b", options: [])
}()
static let bolt11: NSRegularExpression = {
try! NSRegularExpression(pattern: "(?i)\\bln(bc|tb|bcrt)[0-9][a-z0-9]{50,}\\b", options: [])
}()
static let lnurl: NSRegularExpression = {
try! NSRegularExpression(pattern: "(?i)\\blnurl1[a-z0-9]{20,}\\b", options: [])
}()
static let lightningScheme: NSRegularExpression = {
try! NSRegularExpression(pattern: "(?i)\\blightning:[^\\s]+", options: [])
}()
static let linkDetector: NSDataDetector? = {
try? NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
}()
static let quickCashuPresence: NSRegularExpression = {
try! NSRegularExpression(pattern: "\\bcashu[AB][A-Za-z0-9._-]{40,}\\b", options: [])
}()
static let simplifyHTTPURL: NSRegularExpression = {
try! NSRegularExpression(pattern: "https?://[^\\s?#]+(?:[?#][^\\s]*)?", options: [.caseInsensitive])
}()
}
// MARK: - Match Types
/// Types of matches found in message content
enum MatchType: String {
case hashtag
case mention
case url
case cashu
case lightning
case bolt11
case lnurl
}
/// A match found in message content
struct ContentMatch {
let range: NSRange
let type: MatchType
}
// MARK: - Public API
/// Formats a message with rich text styling
@MainActor
static func formatMessage(
_ message: BitchatMessage,
context: MessageFormattingContext,
colorScheme: ColorScheme
) -> AttributedString {
let isDark = colorScheme == .dark
let isSelf = context.isSelfMessage(message)
// Check cache first
if let cached = message.getCachedFormattedText(isDark: isDark, isSelf: isSelf) {
return cached
}
var result = AttributedString()
let baseColor: Color = isSelf ? .orange : context.senderColor(for: message, isDark: isDark)
// Format system messages differently
if message.sender == "system" {
result = formatSystemMessage(message, isDark: isDark)
} else {
// Format sender header
result = formatSenderHeader(
message: message,
baseColor: baseColor,
isSelf: isSelf,
context: context
)
// Format content
let contentResult = formatContent(
message.content,
baseColor: baseColor,
isSelf: isSelf,
isMentioned: message.mentions?.contains(context.nickname) ?? false
)
result.append(contentResult)
// Add timestamp
result.append(formatTimestamp(message.formattedTimestamp))
}
// Cache the result
message.setCachedFormattedText(result, isDark: isDark, isSelf: isSelf)
return result
}
/// Formats just the message header (sender portion)
@MainActor
static func formatHeader(
_ message: BitchatMessage,
context: MessageFormattingContext,
colorScheme: ColorScheme
) -> AttributedString {
let isDark = colorScheme == .dark
let isSelf = context.isSelfMessage(message)
let baseColor: Color = isSelf ? .orange : context.senderColor(for: message, isDark: isDark)
if message.sender == "system" {
var style = AttributeContainer()
style.foregroundColor = baseColor
style.font = .bitchatSystem(size: 14, weight: .medium, design: .monospaced)
return AttributedString(message.sender).mergingAttributes(style)
}
return formatSenderHeader(
message: message,
baseColor: baseColor,
isSelf: isSelf,
context: context
)
}
/// Extracts mentions from message content
static func extractMentions(from content: String) -> [String] {
let nsContent = content as NSString
let range = NSRange(location: 0, length: nsContent.length)
let matches = Patterns.mention.matches(in: content, options: [], range: range)
return matches.compactMap { match -> String? in
guard match.numberOfRanges > 1 else { return nil }
let captureRange = match.range(at: 1)
guard let swiftRange = Range(captureRange, in: content) else { return nil }
return String(content[swiftRange])
}
}
/// Checks if content contains a Cashu token
static func containsCashuToken(_ content: String) -> Bool {
let nsContent = content as NSString
let range = NSRange(location: 0, length: nsContent.length)
return Patterns.quickCashuPresence.numberOfMatches(in: content, options: [], range: range) > 0
}
// MARK: - Private Helpers
private static func formatSystemMessage(_ message: BitchatMessage, isDark: Bool) -> AttributedString {
var result = AttributedString()
let content = AttributedString("* \(message.content) *")
var contentStyle = AttributeContainer()
contentStyle.foregroundColor = Color.gray
contentStyle.font = .bitchatSystem(size: 12, design: .monospaced).italic()
result.append(content.mergingAttributes(contentStyle))
// Add timestamp
let timestamp = AttributedString(" [\(message.formattedTimestamp)]")
var timestampStyle = AttributeContainer()
timestampStyle.foregroundColor = Color.gray.opacity(0.5)
timestampStyle.font = .bitchatSystem(size: 10, design: .monospaced)
result.append(timestamp.mergingAttributes(timestampStyle))
return result
}
@MainActor
private static func formatSenderHeader(
message: BitchatMessage,
baseColor: Color,
isSelf: Bool,
context: MessageFormattingContext
) -> AttributedString {
var result = AttributedString()
let (baseName, suffix) = message.sender.splitSuffix()
var senderStyle = AttributeContainer()
senderStyle.foregroundColor = baseColor
let fontWeight: Font.Weight = isSelf ? .bold : .medium
senderStyle.font = .bitchatSystem(size: 14, weight: fontWeight, design: .monospaced)
// Make sender clickable
if let spid = message.senderPeerID, let url = context.peerURL(for: spid) {
senderStyle.link = url
}
// Build: "<@baseName#suffix> "
result.append(AttributedString("<@").mergingAttributes(senderStyle))
result.append(AttributedString(baseName).mergingAttributes(senderStyle))
if !suffix.isEmpty {
var suffixStyle = senderStyle
suffixStyle.foregroundColor = baseColor.opacity(0.6)
result.append(AttributedString(suffix).mergingAttributes(suffixStyle))
}
result.append(AttributedString("> ").mergingAttributes(senderStyle))
return result
}
private static func formatContent(
_ content: String,
baseColor: Color,
isSelf: Bool,
isMentioned: Bool
) -> AttributedString {
// For very long content without special tokens, use plain formatting
let containsCashu = containsCashuToken(content)
if (content.count > 4000 || content.hasVeryLongToken(threshold: 1024)) && !containsCashu {
return formatPlainContent(content, baseColor: baseColor, isSelf: isSelf)
}
// Find all matches
let matches = findAllMatches(in: content)
// Build formatted content
var result = AttributedString()
var lastEnd = content.startIndex
for match in matches {
guard let swiftRange = Range(match.range, in: content) else { continue }
// Add text before match
if lastEnd < swiftRange.lowerBound {
let beforeText = String(content[lastEnd..<swiftRange.lowerBound])
result.append(formatPlainText(beforeText, baseColor: baseColor, isSelf: isSelf, isMentioned: isMentioned))
}
// Add styled match
let matchText = String(content[swiftRange])
result.append(formatMatch(matchText, type: match.type, baseColor: baseColor, isSelf: isSelf))
lastEnd = swiftRange.upperBound
}
// Add remaining text
if lastEnd < content.endIndex {
let remainingText = String(content[lastEnd...])
result.append(formatPlainText(remainingText, baseColor: baseColor, isSelf: isSelf, isMentioned: isMentioned))
}
return result
}
private static func findAllMatches(in content: String) -> [ContentMatch] {
let nsContent = content as NSString
let nsLen = nsContent.length
let fullRange = NSRange(location: 0, length: nsLen)
// Quick hints to avoid unnecessary regex work
let hasMentions = content.contains("@")
let hasHashtags = content.contains("#")
let hasURLs = content.contains("://") || content.contains("www.") || content.contains("http")
let hasLightning = content.lowercased().contains("ln") || content.lowercased().contains("lightning:")
let hasCashu = content.lowercased().contains("cashu")
// Collect matches
let mentionMatches = hasMentions ? Patterns.mention.matches(in: content, options: [], range: fullRange) : []
let hashtagMatches = hasHashtags ? Patterns.hashtag.matches(in: content, options: [], range: fullRange) : []
let urlMatches = hasURLs ? (Patterns.linkDetector?.matches(in: content, options: [], range: fullRange) ?? []) : []
let cashuMatches = hasCashu ? Patterns.cashu.matches(in: content, options: [], range: fullRange) : []
let lightningMatches = hasLightning ? Patterns.lightningScheme.matches(in: content, options: [], range: fullRange) : []
let bolt11Matches = hasLightning ? Patterns.bolt11.matches(in: content, options: [], range: fullRange) : []
let lnurlMatches = hasLightning ? Patterns.lnurl.matches(in: content, options: [], range: fullRange) : []
// Build mention ranges for overlap checking
let mentionRanges = mentionMatches.map { $0.range(at: 0) }
func overlapsMention(_ r: NSRange) -> Bool {
mentionRanges.contains { NSIntersectionRange(r, $0).length > 0 }
}
func isStandaloneHashtag(_ r: NSRange) -> Bool {
guard let swiftRange = Range(r, in: content) else { return false }
if swiftRange.lowerBound == content.startIndex { return true }
let prev = content.index(before: swiftRange.lowerBound)
return content[prev].isWhitespace || content[prev].isNewline
}
func attachedToMention(_ r: NSRange) -> Bool {
guard let swiftRange = Range(r, in: content), swiftRange.lowerBound > content.startIndex else { return false }
var i = content.index(before: swiftRange.lowerBound)
while true {
let ch = content[i]
if ch.isWhitespace || ch.isNewline { break }
if ch == "@" { return true }
if i == content.startIndex { break }
i = content.index(before: i)
}
return false
}
var allMatches: [ContentMatch] = []
// Add hashtags (excluding those attached to mentions)
for match in hashtagMatches {
let range = match.range(at: 0)
if !overlapsMention(range) && !attachedToMention(range) && isStandaloneHashtag(range) {
allMatches.append(ContentMatch(range: range, type: .hashtag))
}
}
// Add mentions
for match in mentionMatches {
allMatches.append(ContentMatch(range: match.range(at: 0), type: .mention))
}
// Add URLs
for match in urlMatches where !overlapsMention(match.range) {
allMatches.append(ContentMatch(range: match.range, type: .url))
}
// Add Cashu tokens
for match in cashuMatches where !overlapsMention(match.range(at: 0)) {
allMatches.append(ContentMatch(range: match.range(at: 0), type: .cashu))
}
// Add Lightning scheme URLs
for match in lightningMatches where !overlapsMention(match.range(at: 0)) {
allMatches.append(ContentMatch(range: match.range(at: 0), type: .lightning))
}
// Add bolt11/lnurl (avoiding overlaps with lightning scheme and URLs)
let occupied = urlMatches.map { $0.range } + lightningMatches.map { $0.range(at: 0) }
func overlapsOccupied(_ r: NSRange) -> Bool {
occupied.contains { NSIntersectionRange(r, $0).length > 0 }
}
for match in bolt11Matches where !overlapsMention(match.range(at: 0)) && !overlapsOccupied(match.range(at: 0)) {
allMatches.append(ContentMatch(range: match.range(at: 0), type: .bolt11))
}
for match in lnurlMatches where !overlapsMention(match.range(at: 0)) && !overlapsOccupied(match.range(at: 0)) {
allMatches.append(ContentMatch(range: match.range(at: 0), type: .lnurl))
}
// Sort by position
return allMatches.sorted { $0.range.location < $1.range.location }
}
private static func formatPlainContent(_ content: String, baseColor: Color, isSelf: Bool) -> AttributedString {
var style = AttributeContainer()
style.foregroundColor = baseColor
style.font = isSelf
? .bitchatSystem(size: 14, weight: .bold, design: .monospaced)
: .bitchatSystem(size: 14, design: .monospaced)
return AttributedString(content).mergingAttributes(style)
}
private static func formatPlainText(_ text: String, baseColor: Color, isSelf: Bool, isMentioned: Bool) -> AttributedString {
guard !text.isEmpty else { return AttributedString() }
var style = AttributeContainer()
style.foregroundColor = baseColor
style.font = isSelf
? .bitchatSystem(size: 14, weight: .bold, design: .monospaced)
: .bitchatSystem(size: 14, design: .monospaced)
if isMentioned {
style.font = style.font?.bold()
}
return AttributedString(text).mergingAttributes(style)
}
private static func formatMatch(_ text: String, type: MatchType, baseColor: Color, isSelf: Bool) -> AttributedString {
var style = AttributeContainer()
switch type {
case .mention:
// Split optional '#abcd' suffix
let (baseName, suffix) = text.splitSuffix()
var result = AttributedString()
var mentionStyle = AttributeContainer()
mentionStyle.foregroundColor = .blue
mentionStyle.font = .bitchatSystem(size: 14, weight: .semibold, design: .monospaced)
result.append(AttributedString(baseName).mergingAttributes(mentionStyle))
if !suffix.isEmpty {
var suffixStyle = mentionStyle
suffixStyle.foregroundColor = Color.gray.opacity(0.7)
result.append(AttributedString(suffix).mergingAttributes(suffixStyle))
}
return result
case .hashtag:
style.foregroundColor = .purple
style.font = .bitchatSystem(size: 14, weight: .medium, design: .monospaced)
case .url:
style.foregroundColor = .blue
style.font = .bitchatSystem(size: 14, design: .monospaced)
style.underlineStyle = .single
if let url = URL(string: text) {
style.link = url
}
case .cashu:
style.foregroundColor = .green
style.font = .bitchatSystem(size: 14, weight: .medium, design: .monospaced)
style.backgroundColor = Color.green.opacity(0.1)
case .lightning, .bolt11, .lnurl:
style.foregroundColor = .yellow
style.font = .bitchatSystem(size: 14, weight: .medium, design: .monospaced)
style.backgroundColor = Color.yellow.opacity(0.1)
}
return AttributedString(text).mergingAttributes(style)
}
private static func formatTimestamp(_ timestamp: String) -> AttributedString {
let text = AttributedString(" [\(timestamp)]")
var style = AttributeContainer()
style.foregroundColor = Color.gray.opacity(0.5)
style.font = .bitchatSystem(size: 10, design: .monospaced)
return text.mergingAttributes(style)
}
}
+38 -59
View File
@@ -1,17 +1,14 @@
import BitLogger import BitLogger
import Foundation import Foundation
/// Routes messages between BLE and Nostr transports /// Routes messages using available transports (Mesh, Nostr, etc.)
@MainActor @MainActor
final class MessageRouter { final class MessageRouter {
private let mesh: Transport private let transports: [Transport]
private let nostr: NostrTransport
private var outbox: [PeerID: [(content: String, nickname: String, messageID: String)]] = [:] // peerID -> queued messages private var outbox: [PeerID: [(content: String, nickname: String, messageID: String)]] = [:] // peerID -> queued messages
init(mesh: Transport, nostr: NostrTransport) { init(transports: [Transport]) {
self.mesh = mesh self.transports = transports
self.nostr = nostr
self.nostr.senderPeerID = mesh.myPeerID
// Observe favorites changes to learn Nostr mapping and flush queued messages // Observe favorites changes to learn Nostr mapping and flush queued messages
NotificationCenter.default.addObserver( NotificationCenter.default.addObserver(
@@ -38,88 +35,70 @@ final class MessageRouter {
} }
func sendPrivate(_ content: String, to peerID: PeerID, recipientNickname: String, messageID: String) { func sendPrivate(_ content: String, to peerID: PeerID, recipientNickname: String, messageID: String) {
let reachableMesh = mesh.isPeerReachable(peerID) // Try to find a reachable transport
if reachableMesh { if let transport = transports.first(where: { $0.isPeerReachable(peerID) }) {
SecureLogger.debug("Routing PM via mesh (reachable) to \(peerID.id.prefix(8))… id=\(messageID.prefix(8))", category: .session) SecureLogger.debug("Routing PM via \(type(of: transport)) to \(peerID.id.prefix(8))… id=\(messageID.prefix(8))", category: .session)
// BLEService will initiate a handshake if needed and queue the message transport.sendPrivateMessage(content, to: peerID, recipientNickname: recipientNickname, messageID: messageID)
mesh.sendPrivateMessage(content, to: peerID, recipientNickname: recipientNickname, messageID: messageID)
} else if canSendViaNostr(peerID: peerID) {
SecureLogger.debug("Routing PM via Nostr to \(peerID.id.prefix(8))… id=\(messageID.prefix(8))", category: .session)
nostr.sendPrivateMessage(content, to: peerID, recipientNickname: recipientNickname, messageID: messageID)
} else { } else {
// Queue for later (when mesh connects or Nostr mapping appears) // Queue for later
if outbox[peerID] == nil { outbox[peerID] = [] } if outbox[peerID] == nil { outbox[peerID] = [] }
outbox[peerID]?.append((content, recipientNickname, messageID)) outbox[peerID]?.append((content, recipientNickname, messageID))
SecureLogger.debug("Queued PM for \(peerID.id.prefix(8))… (no mesh, no Nostr mapping) id=\(messageID.prefix(8))", category: .session) SecureLogger.debug("Queued PM for \(peerID.id.prefix(8))… (no reachable transport) id=\(messageID.prefix(8))", category: .session)
} }
} }
func sendReadReceipt(_ receipt: ReadReceipt, to peerID: PeerID) { func sendReadReceipt(_ receipt: ReadReceipt, to peerID: PeerID) {
// Prefer mesh for reachable peers; BLE will queue if handshake is needed if let transport = transports.first(where: { $0.isPeerReachable(peerID) }) {
if mesh.isPeerReachable(peerID) { SecureLogger.debug("Routing READ ack via \(type(of: transport)) to \(peerID.id.prefix(8))… id=\(receipt.originalMessageID.prefix(8))", category: .session)
SecureLogger.debug("Routing READ ack via mesh (reachable) to \(peerID.id.prefix(8))… id=\(receipt.originalMessageID.prefix(8))", category: .session) transport.sendReadReceipt(receipt, to: peerID)
mesh.sendReadReceipt(receipt, to: peerID) } else if !transports.isEmpty {
} else { // Fallback to last transport (usually Nostr) if neither is explicitly reachable?
SecureLogger.debug("Routing READ ack via Nostr to \(peerID.id.prefix(8))… id=\(receipt.originalMessageID.prefix(8))", category: .session) // Or better: just try the first one that supports it?
nostr.sendReadReceipt(receipt, to: peerID) // Existing logic preferred mesh, then nostr.
// If neither reachable, existing logic queued it (via mesh usually) or sent via nostr.
// Let's stick to "try reachable". If none, maybe pick the first one to queue?
// Actually, for READ receipts, we might want to just fire-and-forget on the "best effort" transport.
// But let's stick to the reachable check.
SecureLogger.debug("No reachable transport for READ ack to \(peerID.id.prefix(8))", category: .session)
} }
} }
func sendDeliveryAck(_ messageID: String, to peerID: PeerID) { func sendDeliveryAck(_ messageID: String, to peerID: PeerID) {
if mesh.isPeerReachable(peerID) { if let transport = transports.first(where: { $0.isPeerReachable(peerID) }) {
SecureLogger.debug("Routing DELIVERED ack via mesh (reachable) to \(peerID.id.prefix(8))… id=\(messageID.prefix(8))", category: .session) SecureLogger.debug("Routing DELIVERED ack via \(type(of: transport)) to \(peerID.id.prefix(8))… id=\(messageID.prefix(8))", category: .session)
mesh.sendDeliveryAck(for: messageID, to: peerID) transport.sendDeliveryAck(for: messageID, to: peerID)
} else {
nostr.sendDeliveryAck(for: messageID, to: peerID)
} }
} }
func sendFavoriteNotification(to peerID: PeerID, isFavorite: Bool) { func sendFavoriteNotification(to peerID: PeerID, isFavorite: Bool) {
// Route via mesh when connected; else use Nostr if let transport = transports.first(where: { $0.isPeerConnected(peerID) }) {
if mesh.isPeerConnected(peerID) { transport.sendFavoriteNotification(to: peerID, isFavorite: isFavorite)
mesh.sendFavoriteNotification(to: peerID, isFavorite: isFavorite) } else if let transport = transports.first(where: { $0.isPeerReachable(peerID) }) {
transport.sendFavoriteNotification(to: peerID, isFavorite: isFavorite)
} else { } else {
nostr.sendFavoriteNotification(to: peerID, isFavorite: isFavorite) // Fallback: try all? or just the last one?
// Old logic: if mesh connected, mesh. Else nostr.
// Note: NostrTransport.isPeerReachable now returns true if mapped.
// If not mapped, we can't send via Nostr anyway.
} }
} }
// MARK: - Outbox Management // MARK: - Outbox Management
private func canSendViaNostr(peerID: PeerID) -> Bool {
// Two forms are supported:
// - 64-hex Noise public key (32 bytes)
// - 16-hex short peer ID (derived from Noise pubkey)
if let noiseKey = peerID.noiseKey {
if let fav = FavoritesPersistenceService.shared.getFavoriteStatus(for: noiseKey),
fav.peerNostrPublicKey != nil {
return true
}
} else if peerID.isShort {
if let fav = FavoritesPersistenceService.shared.getFavoriteStatus(forPeerID: peerID),
fav.peerNostrPublicKey != nil {
return true
}
}
return false
}
func flushOutbox(for peerID: PeerID) { func flushOutbox(for peerID: PeerID) {
guard let queued = outbox[peerID], !queued.isEmpty else { return } guard let queued = outbox[peerID], !queued.isEmpty else { return }
SecureLogger.debug("Flushing outbox for \(peerID.id.prefix(8))… count=\(queued.count)", category: .session) SecureLogger.debug("Flushing outbox for \(peerID.id.prefix(8))… count=\(queued.count)", category: .session)
var remaining: [(content: String, nickname: String, messageID: String)] = [] var remaining: [(content: String, nickname: String, messageID: String)] = []
// Prefer mesh if connected; else try Nostr if mapping exists
for (content, nickname, messageID) in queued { for (content, nickname, messageID) in queued {
if mesh.isPeerReachable(peerID) { if let transport = transports.first(where: { $0.isPeerReachable(peerID) }) {
SecureLogger.debug("Outbox -> mesh for \(peerID.id.prefix(8))… id=\(messageID.prefix(8))", category: .session) SecureLogger.debug("Outbox -> \(type(of: transport)) for \(peerID.id.prefix(8))… id=\(messageID.prefix(8))", category: .session)
mesh.sendPrivateMessage(content, to: peerID, recipientNickname: nickname, messageID: messageID) transport.sendPrivateMessage(content, to: peerID, recipientNickname: nickname, messageID: messageID)
} else if canSendViaNostr(peerID: peerID) {
SecureLogger.debug("Outbox -> Nostr for \(peerID.id.prefix(8))… id=\(messageID.prefix(8))", category: .session)
nostr.sendPrivateMessage(content, to: peerID, recipientNickname: nickname, messageID: messageID)
} else { } else {
// Keep unsent items queued
remaining.append((content, nickname, messageID)) remaining.append((content, nickname, messageID))
} }
} }
// Persist only items we could not send
if remaining.isEmpty { if remaining.isEmpty {
outbox.removeValue(forKey: peerID) outbox.removeValue(forKey: peerID)
} else { } else {
+81 -14
View File
@@ -3,7 +3,7 @@ import Foundation
import Combine import Combine
// Minimal Nostr transport conforming to Transport for offline sending // Minimal Nostr transport conforming to Transport for offline sending
final class NostrTransport: Transport { final class NostrTransport: Transport, @unchecked Sendable {
// Provide BLE short peer ID for BitChat embedding // Provide BLE short peer ID for BitChat embedding
var senderPeerID = PeerID(str: "") var senderPeerID = PeerID(str: "")
@@ -18,9 +18,49 @@ final class NostrTransport: Transport {
private let keychain: KeychainManagerProtocol private let keychain: KeychainManagerProtocol
private let idBridge: NostrIdentityBridge private let idBridge: NostrIdentityBridge
// Reachability Cache (thread-safe)
private var reachablePeers: Set<PeerID> = []
private let queue = DispatchQueue(label: "nostr.transport.state", attributes: .concurrent)
@MainActor
init(keychain: KeychainManagerProtocol, idBridge: NostrIdentityBridge) { init(keychain: KeychainManagerProtocol, idBridge: NostrIdentityBridge) {
self.keychain = keychain self.keychain = keychain
self.idBridge = idBridge self.idBridge = idBridge
setupObservers()
// Synchronously warm the cache to avoid startup race
let favorites = FavoritesPersistenceService.shared.favorites
let reachable = favorites.values
.filter { $0.peerNostrPublicKey != nil }
.map { PeerID(publicKey: $0.peerNoisePublicKey) }
queue.sync(flags: .barrier) {
self.reachablePeers = Set(reachable)
}
}
private func setupObservers() {
NotificationCenter.default.addObserver(
forName: .favoriteStatusChanged,
object: nil,
queue: nil
) { [weak self] _ in
self?.refreshReachablePeers()
}
}
private func refreshReachablePeers() {
Task { @MainActor in
let favorites = FavoritesPersistenceService.shared.favorites
let reachable = favorites.values
.filter { $0.peerNostrPublicKey != nil }
.map { PeerID(publicKey: $0.peerNoisePublicKey) }
self.queue.async(flags: .barrier) { [weak self] in
self?.reachablePeers = Set(reachable)
}
}
} }
// MARK: - Transport Protocol Conformance // MARK: - Transport Protocol Conformance
@@ -42,7 +82,19 @@ final class NostrTransport: Transport {
func emergencyDisconnectAll() { /* no-op */ } func emergencyDisconnectAll() { /* no-op */ }
func isPeerConnected(_ peerID: PeerID) -> Bool { false } func isPeerConnected(_ peerID: PeerID) -> Bool { false }
func isPeerReachable(_ peerID: PeerID) -> Bool { false }
func isPeerReachable(_ peerID: PeerID) -> Bool {
queue.sync {
// Check if exact match
if reachablePeers.contains(peerID) { return true }
// Check for short ID match
if peerID.isShort {
return reachablePeers.contains(where: { $0.toShort() == peerID })
}
return false
}
}
func peerNickname(peerID: PeerID) -> String? { nil } func peerNickname(peerID: PeerID) -> String? { nil }
func getPeerNicknames() -> [PeerID : String] { [:] } func getPeerNicknames() -> [PeerID : String] { [:] }
@@ -97,8 +149,11 @@ final class NostrTransport: Transport {
func sendReadReceipt(_ receipt: ReadReceipt, to peerID: PeerID) { func sendReadReceipt(_ receipt: ReadReceipt, to peerID: PeerID) {
// Enqueue and process with throttling to avoid relay rate limits // Enqueue and process with throttling to avoid relay rate limits
readQueue.append(QueuedRead(receipt: receipt, peerID: peerID)) // Use barrier to synchronize access to readQueue
processReadQueueIfNeeded() queue.async(flags: .barrier) { [weak self] in
self?.readQueue.append(QueuedRead(receipt: receipt, peerID: peerID))
self?.processReadQueueIfNeeded()
}
} }
func sendFavoriteNotification(to peerID: PeerID, isFavorite: Bool) { func sendFavoriteNotification(to peerID: PeerID, isFavorite: Bool) {
@@ -113,7 +168,10 @@ final class NostrTransport: Transport {
let (hrp, data) = try Bech32.decode(recipientNpub) let (hrp, data) = try Bech32.decode(recipientNpub)
guard hrp == "npub" else { return } guard hrp == "npub" else { return }
recipientHex = data.hexEncodedString() recipientHex = data.hexEncodedString()
} catch { return } } catch {
SecureLogger.error("NostrTransport: failed to decode recipient npub for favorite notification: \(error.localizedDescription)", category: .session)
return
}
guard let embedded = NostrEmbeddedBitChat.encodePMForNostr(content: content, messageID: UUID().uuidString, recipientPeerID: peerID, senderPeerID: senderPeerID) else { guard let embedded = NostrEmbeddedBitChat.encodePMForNostr(content: content, messageID: UUID().uuidString, recipientPeerID: peerID, senderPeerID: senderPeerID) else {
SecureLogger.error("NostrTransport: failed to embed favorite notification", category: .session) SecureLogger.error("NostrTransport: failed to embed favorite notification", category: .session)
return return
@@ -138,7 +196,10 @@ final class NostrTransport: Transport {
let (hrp, data) = try Bech32.decode(recipientNpub) let (hrp, data) = try Bech32.decode(recipientNpub)
guard hrp == "npub" else { return } guard hrp == "npub" else { return }
recipientHex = data.hexEncodedString() recipientHex = data.hexEncodedString()
} catch { return } } catch {
SecureLogger.error("NostrTransport: failed to decode recipient npub for delivery ack: \(error.localizedDescription)", category: .session)
return
}
guard let ack = NostrEmbeddedBitChat.encodeAckForNostr(type: .delivered, messageID: messageID, recipientPeerID: peerID, senderPeerID: senderPeerID) else { guard let ack = NostrEmbeddedBitChat.encodeAckForNostr(type: .delivered, messageID: messageID, recipientPeerID: peerID, senderPeerID: senderPeerID) else {
SecureLogger.error("NostrTransport: failed to embed DELIVERED ack", category: .session) SecureLogger.error("NostrTransport: failed to embed DELIVERED ack", category: .session)
return return
@@ -202,16 +263,17 @@ extension NostrTransport {
// MARK: - Private Helpers // MARK: - Private Helpers
extension NostrTransport { extension NostrTransport {
/// Must be called within a barrier on `queue`
private func processReadQueueIfNeeded() { private func processReadQueueIfNeeded() {
guard !isSendingReadAcks else { return } guard !isSendingReadAcks else { return }
guard !readQueue.isEmpty else { return } guard !readQueue.isEmpty else { return }
isSendingReadAcks = true isSendingReadAcks = true
sendNextReadAck() let item = readQueue.removeFirst()
sendReadAckItem(item)
} }
private func sendNextReadAck() { /// Sends a single read ack item (called after extraction from queue within barrier)
guard !readQueue.isEmpty else { isSendingReadAcks = false; return } private func sendReadAckItem(_ item: QueuedRead) {
let item = readQueue.removeFirst()
Task { @MainActor in Task { @MainActor in
guard let recipientNpub = resolveRecipientNpub(for: item.peerID) else { scheduleNextReadAck(); return } guard let recipientNpub = resolveRecipientNpub(for: item.peerID) else { scheduleNextReadAck(); return }
guard let senderIdentity = try? idBridge.getCurrentNostrIdentity() else { scheduleNextReadAck(); return } guard let senderIdentity = try? idBridge.getCurrentNostrIdentity() else { scheduleNextReadAck(); return }
@@ -222,7 +284,11 @@ extension NostrTransport {
let (hrp, data) = try Bech32.decode(recipientNpub) let (hrp, data) = try Bech32.decode(recipientNpub)
guard hrp == "npub" else { scheduleNextReadAck(); return } guard hrp == "npub" else { scheduleNextReadAck(); return }
recipientHex = data.hexEncodedString() recipientHex = data.hexEncodedString()
} catch { scheduleNextReadAck(); return } } catch {
SecureLogger.error("NostrTransport: failed to decode recipient npub for read ack: \(error.localizedDescription)", category: .session)
scheduleNextReadAck()
return
}
guard let ack = NostrEmbeddedBitChat.encodeAckForNostr(type: .readReceipt, messageID: item.receipt.originalMessageID, recipientPeerID: item.peerID, senderPeerID: senderPeerID) else { guard let ack = NostrEmbeddedBitChat.encodeAckForNostr(type: .readReceipt, messageID: item.receipt.originalMessageID, recipientPeerID: item.peerID, senderPeerID: senderPeerID) else {
SecureLogger.error("NostrTransport: failed to embed READ ack", category: .session) SecureLogger.error("NostrTransport: failed to embed READ ack", category: .session)
scheduleNextReadAck(); return scheduleNextReadAck(); return
@@ -239,9 +305,10 @@ extension NostrTransport {
private func scheduleNextReadAck() { private func scheduleNextReadAck() {
DispatchQueue.main.asyncAfter(deadline: .now() + readAckInterval) { [weak self] in DispatchQueue.main.asyncAfter(deadline: .now() + readAckInterval) { [weak self] in
guard let self = self else { return } self?.queue.async(flags: .barrier) { [weak self] in
self.isSendingReadAcks = false self?.isSendingReadAcks = false
self.processReadQueueIfNeeded() self?.processReadQueueIfNeeded()
}
} }
} }
@@ -17,9 +17,20 @@ import AppKit
final class NotificationService { final class NotificationService {
static let shared = NotificationService() static let shared = NotificationService()
/// Returns true if running in test environment (XCTest, Swift Testing, or CI)
private var isRunningTests: Bool {
let env = ProcessInfo.processInfo.environment
return NSClassFromString("XCTestCase") != nil ||
env["XCTestConfigurationFilePath"] != nil ||
env["XCTestBundlePath"] != nil ||
env["GITHUB_ACTIONS"] != nil ||
env["CI"] != nil
}
private init() {} private init() {}
func requestAuthorization() { func requestAuthorization() {
guard !isRunningTests else { return }
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
if granted { if granted {
// Permission granted // Permission granted
@@ -36,6 +47,7 @@ final class NotificationService {
userInfo: [String: Any]? = nil, userInfo: [String: Any]? = nil,
interruptionLevel: UNNotificationInterruptionLevel = .active interruptionLevel: UNNotificationInterruptionLevel = .active
) { ) {
guard !isRunningTests else { return }
let content = UNMutableNotificationContent() let content = UNMutableNotificationContent()
content.title = title content.title = title
content.body = body content.body = body
+154
View File
@@ -22,6 +22,8 @@ final class PrivateChatManager: ObservableObject {
weak var meshService: Transport? weak var meshService: Transport?
// Route acks/receipts via MessageRouter (chooses mesh or Nostr) // Route acks/receipts via MessageRouter (chooses mesh or Nostr)
weak var messageRouter: MessageRouter? weak var messageRouter: MessageRouter?
// Peer service for looking up peer info during consolidation
weak var unifiedPeerService: UnifiedPeerService?
init(meshService: Transport? = nil) { init(meshService: Transport? = nil) {
self.meshService = meshService self.meshService = meshService
@@ -30,6 +32,158 @@ final class PrivateChatManager: ObservableObject {
// Cap for messages stored per private chat // Cap for messages stored per private chat
private let privateChatCap = TransportConfig.privateChatCap private let privateChatCap = TransportConfig.privateChatCap
// MARK: - Message Consolidation
/// Consolidates messages from different peer ID representations into a single chat.
/// This ensures messages from stable Noise keys and temporary Nostr peer IDs are merged.
/// - Parameters:
/// - peerID: The target peer ID to consolidate messages into
/// - peerNickname: The peer's display name (lowercased for matching)
/// - persistedReadReceipts: The persisted read receipts set from ChatViewModel (UserDefaults-backed)
/// - Returns: True if any unread messages were found during consolidation
@MainActor
func consolidateMessages(for peerID: PeerID, peerNickname: String, persistedReadReceipts: Set<String>) -> Bool {
guard let meshService = meshService else { return false }
var hasUnreadMessages = false
// 1. Consolidate from stable Noise key (64-char hex)
if let peer = unifiedPeerService?.getPeer(by: peerID) {
let noiseKeyHex = PeerID(hexData: peer.noisePublicKey)
if noiseKeyHex != peerID, let nostrMessages = privateChats[noiseKeyHex], !nostrMessages.isEmpty {
if privateChats[peerID] == nil {
privateChats[peerID] = []
}
let existingMessageIds = Set(privateChats[peerID]?.map { $0.id } ?? [])
for message in nostrMessages {
if !existingMessageIds.contains(message.id) {
// Update senderPeerID for correct read receipts
let updatedMessage = BitchatMessage(
id: message.id,
sender: message.sender,
content: message.content,
timestamp: message.timestamp,
isRelay: message.isRelay,
originalSender: message.originalSender,
isPrivate: message.isPrivate,
recipientNickname: message.recipientNickname,
senderPeerID: message.senderPeerID == meshService.myPeerID ? meshService.myPeerID : peerID,
mentions: message.mentions,
deliveryStatus: message.deliveryStatus
)
privateChats[peerID]?.append(updatedMessage)
// Check for recent unread messages (< 60s, not sent by us, not already read)
// Use persistedReadReceipts to correctly identify already-read messages after app restart
if message.senderPeerID != meshService.myPeerID {
let messageAge = Date().timeIntervalSince(message.timestamp)
if messageAge < 60 && !persistedReadReceipts.contains(message.id) {
hasUnreadMessages = true
}
}
}
}
privateChats[peerID]?.sort { $0.timestamp < $1.timestamp }
if hasUnreadMessages {
unreadMessages.insert(peerID)
} else if unreadMessages.contains(noiseKeyHex) {
unreadMessages.remove(noiseKeyHex)
}
privateChats.removeValue(forKey: noiseKeyHex)
}
}
// 2. Consolidate from temporary Nostr peer IDs (nostr_* prefixed)
let normalizedNickname = peerNickname.lowercased()
var tempPeerIDsToConsolidate: [PeerID] = []
for (storedPeerID, messages) in privateChats {
if storedPeerID.isGeoDM && storedPeerID != peerID {
let nicknamesMatch = messages.allSatisfy { $0.sender.lowercased() == normalizedNickname }
if nicknamesMatch && !messages.isEmpty {
tempPeerIDsToConsolidate.append(storedPeerID)
}
}
}
if !tempPeerIDsToConsolidate.isEmpty {
if privateChats[peerID] == nil {
privateChats[peerID] = []
}
let existingMessageIds = Set(privateChats[peerID]?.map { $0.id } ?? [])
var consolidatedCount = 0
var hadUnreadTemp = false
for tempPeerID in tempPeerIDsToConsolidate {
if unreadMessages.contains(tempPeerID) {
hadUnreadTemp = true
}
if let tempMessages = privateChats[tempPeerID] {
for message in tempMessages {
if !existingMessageIds.contains(message.id) {
let updatedMessage = BitchatMessage(
id: message.id,
sender: message.sender,
content: message.content,
timestamp: message.timestamp,
isRelay: message.isRelay,
originalSender: message.originalSender,
isPrivate: message.isPrivate,
recipientNickname: message.recipientNickname,
senderPeerID: peerID,
mentions: message.mentions,
deliveryStatus: message.deliveryStatus
)
privateChats[peerID]?.append(updatedMessage)
consolidatedCount += 1
}
}
privateChats.removeValue(forKey: tempPeerID)
unreadMessages.remove(tempPeerID)
}
}
if hadUnreadTemp {
unreadMessages.insert(peerID)
hasUnreadMessages = true
SecureLogger.debug("📬 Transferred unread status from temp peer IDs to \(peerID)", category: .session)
}
if consolidatedCount > 0 {
privateChats[peerID]?.sort { $0.timestamp < $1.timestamp }
SecureLogger.info("📥 Consolidated \(consolidatedCount) Nostr messages from temporary peer IDs to \(peerNickname)", category: .session)
}
}
return hasUnreadMessages
}
/// Syncs the read receipt tracking between manager and view model for sent messages
@MainActor
func syncReadReceiptsForSentMessages(peerID: PeerID, nickname: String, externalReceipts: inout Set<String>) {
guard let messages = privateChats[peerID] else { return }
for message in messages {
if message.sender == nickname {
if let status = message.deliveryStatus {
switch status {
case .read, .delivered:
externalReceipts.insert(message.id)
sentReadReceipts.insert(message.id)
case .failed, .partiallyDelivered, .sending, .sent:
break
}
}
}
}
}
/// Start a private chat with a peer /// Start a private chat with a peer
func startChat(with peerID: PeerID) { func startChat(with peerID: PeerID) {
selectedPeer = peerID selectedPeer = peerID
+85 -38
View File
@@ -2,66 +2,112 @@ import Foundation
// MARK: - Message Deduplicator (shared) // MARK: - Message Deduplicator (shared)
/// Thread-safe deduplicator with LRU eviction and time-based expiry.
/// Used for both message ID deduplication (network layer) and content key deduplication (UI layer).
final class MessageDeduplicator { final class MessageDeduplicator {
private struct Entry { private struct Entry: Equatable {
let messageID: String let id: String
let timestamp: Date let timestamp: Date
} }
private var entries: [Entry] = [] private var entries: [Entry] = []
private var head: Int = 0 private var head: Int = 0
private var lookup = Set<String>() private var lookup: [String: Date] = [:] // id -> timestamp for O(1) lookup
private let lock = NSLock() private let lock = NSLock()
private let maxAge: TimeInterval = TransportConfig.messageDedupMaxAgeSeconds // 5 minutes private let maxAge: TimeInterval
private let maxCount = TransportConfig.messageDedupMaxCount private let maxCount: Int
/// Check if message is duplicate and add if not /// Initialize with default config from TransportConfig
func isDuplicate(_ messageID: String) -> Bool { convenience init() {
self.init(
maxAge: TransportConfig.messageDedupMaxAgeSeconds,
maxCount: TransportConfig.messageDedupMaxCount
)
}
/// Initialize with custom config for content deduplication
init(maxAge: TimeInterval, maxCount: Int) {
self.maxAge = maxAge
self.maxCount = maxCount
}
/// Check if message is duplicate and add if not.
/// - Parameter id: The message identifier to check.
/// - Returns: `true` if the message was already seen, `false` otherwise.
func isDuplicate(_ id: String) -> Bool {
lock.lock() lock.lock()
defer { lock.unlock() } defer { lock.unlock() }
cleanupOldEntries() let now = Date()
cleanupOldEntries(before: now.addingTimeInterval(-maxAge))
if lookup.contains(messageID) { if lookup[id] != nil {
return true return true
} }
entries.append(Entry(messageID: messageID, timestamp: Date())) entries.append(Entry(id: id, timestamp: now))
lookup.insert(messageID) lookup[id] = now
trimIfNeeded()
// Soft-cap and advance head by a chunk to avoid O(n) shifting
if (entries.count - head) > maxCount {
let removeCount = min(100, entries.count - head)
for i in head..<(head + removeCount) {
lookup.remove(entries[i].messageID)
}
head += removeCount
// Periodically compact to reclaim memory
if head > entries.count / 2 {
entries.removeFirst(head)
head = 0
}
}
return false return false
} }
/// Add an ID without checking (for announce-back tracking) /// Record an ID with a specific timestamp (for content key tracking)
func markProcessed(_ messageID: String) { func record(_ id: String, timestamp: Date) {
lock.lock() lock.lock()
defer { lock.unlock() } defer { lock.unlock() }
if !lookup.contains(messageID) { if lookup[id] == nil {
entries.append(Entry(messageID: messageID, timestamp: Date())) entries.append(Entry(id: id, timestamp: timestamp))
lookup.insert(messageID) }
lookup[id] = timestamp
trimIfNeeded()
}
/// Add an ID without checking (for announce-back tracking)
func markProcessed(_ id: String) {
lock.lock()
defer { lock.unlock() }
if lookup[id] == nil {
let now = Date()
entries.append(Entry(id: id, timestamp: now))
lookup[id] = now
} }
} }
/// Check if ID exists without adding /// Check if ID exists without adding
func contains(_ messageID: String) -> Bool { func contains(_ id: String) -> Bool {
lock.lock() lock.lock()
defer { lock.unlock() } defer { lock.unlock() }
return lookup.contains(messageID) return lookup[id] != nil
}
/// Get timestamp for an ID (for content deduplication time-window checks)
func timestampFor(_ id: String) -> Date? {
lock.lock()
defer { lock.unlock() }
return lookup[id]
}
private func trimIfNeeded() {
let activeCount = entries.count - head
guard activeCount > maxCount else { return }
// Remove down to 75% of maxCount for better amortization
let targetCount = (maxCount * 3) / 4
let removeCount = activeCount - targetCount
for i in head..<(head + removeCount) {
lookup.removeValue(forKey: entries[i].id)
}
head += removeCount
// Compact when head exceeds half the array to reclaim memory
if head > entries.count / 2 {
entries.removeFirst(head)
head = 0
}
} }
/// Clear all entries /// Clear all entries
@@ -74,24 +120,25 @@ final class MessageDeduplicator {
lookup.removeAll() lookup.removeAll()
} }
/// Periodic cleanup /// Periodic cleanup of expired entries and memory optimization.
func cleanup() { func cleanup() {
lock.lock() lock.lock()
defer { lock.unlock() } defer { lock.unlock() }
cleanupOldEntries() cleanupOldEntries(before: Date().addingTimeInterval(-maxAge))
if entries.capacity > maxCount * 2 { // Shrink capacity if significantly oversized
if entries.capacity > maxCount * 2 && entries.count < maxCount {
entries.reserveCapacity(maxCount) entries.reserveCapacity(maxCount)
} }
} }
private func cleanupOldEntries() { private func cleanupOldEntries(before cutoff: Date) {
let cutoff = Date().addingTimeInterval(-maxAge)
while head < entries.count, entries[head].timestamp < cutoff { while head < entries.count, entries[head].timestamp < cutoff {
lookup.remove(entries[head].messageID) lookup.removeValue(forKey: entries[head].id)
head += 1 head += 1
} }
// Compact when head exceeds half the array
if head > 0 && head > entries.count / 2 { if head > 0 && head > entries.count / 2 {
entries.removeFirst(head) entries.removeFirst(head)
head = 0 head = 0
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,814 @@
//
// ChatViewModel+Nostr.swift
// bitchat
//
// Geohash and Nostr logic for ChatViewModel
//
import Foundation
import Combine
import BitLogger
import SwiftUI
import Tor
extension ChatViewModel {
// MARK: - Geohash Subscription
// Resubscribe to the active geohash channel without clearing timeline
@MainActor
func resubscribeCurrentGeohash() {
guard case .location(let ch) = activeChannel else { return }
guard let subID = geoSubscriptionID else {
// No existing subscription; set it up
switchLocationChannel(to: activeChannel)
return
}
// Ensure participant decay timer is running
participantTracker.startRefreshTimer()
// Unsubscribe + resubscribe
NostrRelayManager.shared.unsubscribe(id: subID)
let filter = NostrFilter.geohashEphemeral(
ch.geohash,
since: Date().addingTimeInterval(-TransportConfig.nostrGeohashInitialLookbackSeconds),
limit: TransportConfig.nostrGeohashInitialLimit
)
let subRelays = GeoRelayDirectory.shared.closestRelays(
toGeohash: ch.geohash,
count: TransportConfig.nostrGeoRelayCount
)
NostrRelayManager.shared.subscribe(filter: filter, id: subID, relayUrls: subRelays) { [weak self] event in
self?.subscribeNostrEvent(event)
}
// Resubscribe geohash DMs for this identity
if let dmSub = geoDmSubscriptionID {
NostrRelayManager.shared.unsubscribe(id: dmSub); geoDmSubscriptionID = nil
}
if let id = try? idBridge.deriveIdentity(forGeohash: ch.geohash) {
let dmSub = "geo-dm-\(ch.geohash)"
geoDmSubscriptionID = dmSub
let dmFilter = NostrFilter.giftWrapsFor(pubkey: id.publicKeyHex, since: Date().addingTimeInterval(-TransportConfig.nostrDMSubscribeLookbackSeconds))
NostrRelayManager.shared.subscribe(filter: dmFilter, id: dmSub) { [weak self] giftWrap in
self?.subscribeGiftWrap(giftWrap, id: id)
}
}
}
func subscribeNostrEvent(_ event: NostrEvent) {
guard event.kind == NostrProtocol.EventKind.ephemeralEvent.rawValue,
!deduplicationService.hasProcessedNostrEvent(event.id)
else {
return
}
deduplicationService.recordNostrEvent(event.id)
if let gh = currentGeohash,
let myGeoIdentity = try? idBridge.deriveIdentity(forGeohash: gh),
myGeoIdentity.publicKeyHex.lowercased() == event.pubkey.lowercased() {
// Skip very recent self-echo from relay, but allow older events (e.g., after app restart)
let eventTime = Date(timeIntervalSince1970: TimeInterval(event.created_at))
if Date().timeIntervalSince(eventTime) < 15 {
return
}
}
if let nickTag = event.tags.first(where: { $0.first == "n" }), nickTag.count >= 2 {
let nick = nickTag[1].trimmingCharacters(in: .whitespacesAndNewlines)
geoNicknames[event.pubkey.lowercased()] = nick
}
// Store mapping for geohash sender IDs used in messages (ensures consistent colors)
nostrKeyMapping[PeerID(nostr_: event.pubkey)] = event.pubkey
nostrKeyMapping[PeerID(nostr: event.pubkey)] = event.pubkey
// Update participants last-seen for this pubkey
participantTracker.recordParticipant(pubkeyHex: event.pubkey)
// Track teleported tag (only our format ["t","teleport"]) for icon state
let hasTeleportTag = event.tags.contains(where: { tag in
tag.count >= 2 && tag[0].lowercased() == "t" && tag[1].lowercased() == "teleport"
})
if hasTeleportTag {
let key = event.pubkey.lowercased()
// Do not mark our own key from historical events; rely on manager.teleported for self
let isSelf: Bool = {
if let gh = currentGeohash, let my = try? idBridge.deriveIdentity(forGeohash: gh) {
return my.publicKeyHex.lowercased() == key
}
return false
}()
if !isSelf {
Task { @MainActor in
teleportedGeo = teleportedGeo.union([key])
}
}
}
let senderName = displayNameForNostrPubkey(event.pubkey)
let content = event.content.trimmingCharacters(in: .whitespacesAndNewlines)
// Clamp future timestamps to now to avoid future-dated messages skewing order
let rawTs = Date(timeIntervalSince1970: TimeInterval(event.created_at))
let timestamp = min(rawTs, Date())
let mentions = parseMentions(from: content)
let msg = BitchatMessage(
id: event.id,
sender: senderName,
content: content,
timestamp: timestamp,
isRelay: false,
senderPeerID: PeerID(nostr: event.pubkey),
mentions: mentions.isEmpty ? nil : mentions
)
Task { @MainActor in
handlePublicMessage(msg)
checkForMentions(msg)
sendHapticFeedback(for: msg)
}
}
func subscribeGiftWrap(_ giftWrap: NostrEvent, id: NostrIdentity) {
guard !deduplicationService.hasProcessedNostrEvent(giftWrap.id) else { return }
deduplicationService.recordNostrEvent(giftWrap.id)
guard let (content, senderPubkey, rumorTs) = try? NostrProtocol.decryptPrivateMessage(giftWrap: giftWrap, recipientIdentity: id),
content.hasPrefix("bitchat1:"),
let packetData = Self.base64URLDecode(String(content.dropFirst("bitchat1:".count))),
let packet = BitchatPacket.from(packetData),
packet.type == MessageType.noiseEncrypted.rawValue,
let noisePayload = NoisePayload.decode(packet.payload)
else {
return
}
let messageTimestamp = Date(timeIntervalSince1970: TimeInterval(rumorTs))
let convKey = PeerID(nostr_: senderPubkey)
nostrKeyMapping[convKey] = senderPubkey
switch noisePayload.type {
case .privateMessage:
handlePrivateMessage(noisePayload, senderPubkey: senderPubkey, convKey: convKey, id: id, messageTimestamp: messageTimestamp)
case .delivered:
handleDelivered(noisePayload, senderPubkey: senderPubkey, convKey: convKey)
case .readReceipt:
handleReadReceipt(noisePayload, senderPubkey: senderPubkey, convKey: convKey)
case .verifyChallenge, .verifyResponse:
// QR verification payloads over Nostr are not supported; ignore in geohash DMs
break
}
}
// MARK: - Geohash Channel Handling
@MainActor
func switchLocationChannel(to channel: ChannelID) {
// Reset pending public batches to avoid cross-channel bleed
publicMessagePipeline.reset()
activeChannel = channel
publicMessagePipeline.updateActiveChannel(channel)
// Reset deduplication set and optionally hydrate timeline for mesh
deduplicationService.clearNostrCaches()
switch channel {
case .mesh:
refreshVisibleMessages(from: .mesh)
// Debug: log if any empty messages are present
let emptyMesh = messages.filter { $0.content.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty }.count
if emptyMesh > 0 {
SecureLogger.debug("RenderGuard: mesh timeline contains \(emptyMesh) empty messages", category: .session)
}
participantTracker.stopRefreshTimer()
participantTracker.setActiveGeohash(nil)
teleportedGeo.removeAll()
case .location:
refreshVisibleMessages(from: channel)
}
// If switching to a location channel, flush any pending geohash-only system messages
if case .location = channel {
for content in timelineStore.drainPendingGeohashSystemMessages() {
addPublicSystemMessage(content)
}
}
// Unsubscribe previous
if let sub = geoSubscriptionID {
NostrRelayManager.shared.unsubscribe(id: sub)
geoSubscriptionID = nil
}
if let dmSub = geoDmSubscriptionID {
NostrRelayManager.shared.unsubscribe(id: dmSub)
geoDmSubscriptionID = nil
}
currentGeohash = nil
participantTracker.setActiveGeohash(nil)
// Reset nickname cache for geochat participants
geoNicknames.removeAll()
guard case .location(let ch) = channel else { return }
currentGeohash = ch.geohash
participantTracker.setActiveGeohash(ch.geohash)
// Ensure self appears immediately in the people list; mark teleported state only when truly teleported
if let id = try? idBridge.deriveIdentity(forGeohash: ch.geohash) {
participantTracker.recordParticipant(pubkeyHex: id.publicKeyHex)
let hasRegional = !LocationChannelManager.shared.availableChannels.isEmpty
let inRegional = LocationChannelManager.shared.availableChannels.contains { $0.geohash == ch.geohash }
let key = id.publicKeyHex.lowercased()
if LocationChannelManager.shared.teleported && hasRegional && !inRegional {
teleportedGeo = teleportedGeo.union([key])
SecureLogger.info("GeoTeleport: channel switch mark self teleported key=\(key.prefix(8))… total=\(teleportedGeo.count)", category: .session)
} else {
teleportedGeo.remove(key)
}
}
let subID = "geo-\(ch.geohash)"
geoSubscriptionID = subID
participantTracker.startRefreshTimer()
let ts = Date().addingTimeInterval(-TransportConfig.nostrGeohashInitialLookbackSeconds)
let filter = NostrFilter.geohashEphemeral(ch.geohash, since: ts, limit: TransportConfig.nostrGeohashInitialLimit)
let subRelays = GeoRelayDirectory.shared.closestRelays(toGeohash: ch.geohash, count: 5)
NostrRelayManager.shared.subscribe(filter: filter, id: subID, relayUrls: subRelays) { [weak self] event in
self?.handleNostrEvent(event)
}
subscribeToGeoChat(ch)
}
func handleNostrEvent(_ event: NostrEvent) {
// Only handle ephemeral kind 20000 with matching tag
guard event.kind == NostrProtocol.EventKind.ephemeralEvent.rawValue else { return }
// Deduplicate
if deduplicationService.hasProcessedNostrEvent(event.id) { return }
deduplicationService.recordNostrEvent(event.id)
// Log incoming tags for diagnostics
let tagSummary = event.tags.map { "[" + $0.joined(separator: ",") + "]" }.joined(separator: ",")
SecureLogger.debug("GeoTeleport: recv pub=\(event.pubkey.prefix(8))… tags=\(tagSummary)", category: .session)
// Track teleport tag for participants only our format ["t", "teleport"]
let hasTeleportTag: Bool = event.tags.contains { tag in
tag.count >= 2 && tag[0].lowercased() == "t" && tag[1].lowercased() == "teleport"
}
let isSelf: Bool = {
if let gh = currentGeohash, let my = try? idBridge.deriveIdentity(forGeohash: gh) {
return my.publicKeyHex.lowercased() == event.pubkey.lowercased()
}
return false
}()
if hasTeleportTag {
// Avoid marking our own key from historical events; rely on manager.teleported for self
if !isSelf {
let key = event.pubkey.lowercased()
Task { @MainActor in
teleportedGeo = teleportedGeo.union([key])
SecureLogger.info("GeoTeleport: mark peer teleported key=\(key.prefix(8))… total=\(teleportedGeo.count)", category: .session)
}
}
}
// Skip only very recent self-echo from relay; include older self events for hydration
if isSelf {
let eventTime = Date(timeIntervalSince1970: TimeInterval(event.created_at))
if Date().timeIntervalSince(eventTime) < 15 {
return
}
}
// Cache nickname from tag if present
if let nickTag = event.tags.first(where: { $0.first == "n" }), nickTag.count >= 2 {
let nick = nickTag[1].trimmingCharacters(in: .whitespacesAndNewlines)
geoNicknames[event.pubkey.lowercased()] = nick
}
// If this pubkey is blocked, skip mapping, participants, and timeline
if identityManager.isNostrBlocked(pubkeyHexLowercased: event.pubkey) {
return
}
// Store mapping for geohash DM initiation
nostrKeyMapping[PeerID(nostr_: event.pubkey)] = event.pubkey
nostrKeyMapping[PeerID(nostr: event.pubkey)] = event.pubkey
// Update participants last-seen for this pubkey
participantTracker.recordParticipant(pubkeyHex: event.pubkey)
let senderName = displayNameForNostrPubkey(event.pubkey)
let content = event.content
// If this is a teleport presence event (no content), don't add to timeline
if let teleTag = event.tags.first(where: { $0.first == "t" }), teleTag.count >= 2, (teleTag[1] == "teleport"),
content.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
return
}
// Clamp future timestamps
let rawTs = Date(timeIntervalSince1970: TimeInterval(event.created_at))
let mentions = parseMentions(from: content)
let msg = BitchatMessage(
id: event.id,
sender: senderName,
content: content,
timestamp: min(rawTs, Date()),
isRelay: false,
senderPeerID: PeerID(nostr: event.pubkey),
mentions: mentions.isEmpty ? nil : mentions
)
Task { @MainActor in
handlePublicMessage(msg)
checkForMentions(msg)
sendHapticFeedback(for: msg)
}
}
@MainActor
func subscribeToGeoChat(_ ch: GeohashChannel) {
guard let id = try? idBridge.deriveIdentity(forGeohash: ch.geohash) else { return }
let dmSub = "geo-dm-\(ch.geohash)"
geoDmSubscriptionID = dmSub
// pared back logging: subscribe debug only
// Log GeoDM subscribe only when Tor is ready to avoid early noise
if TorManager.shared.isReady {
SecureLogger.debug("GeoDM: subscribing DMs pub=\(id.publicKeyHex.prefix(8))… sub=\(dmSub)", category: .session)
}
let dmFilter = NostrFilter.giftWrapsFor(pubkey: id.publicKeyHex, since: Date().addingTimeInterval(-TransportConfig.nostrDMSubscribeLookbackSeconds))
NostrRelayManager.shared.subscribe(filter: dmFilter, id: dmSub) { [weak self] giftWrap in
self?.handleGiftWrap(giftWrap, id: id)
}
}
func handleGiftWrap(_ giftWrap: NostrEvent, id: NostrIdentity) {
if deduplicationService.hasProcessedNostrEvent(giftWrap.id) {
return
}
deduplicationService.recordNostrEvent(giftWrap.id)
// Decrypt with per-geohash identity
guard let (content, senderPubkey, rumorTs) = try? NostrProtocol.decryptPrivateMessage(giftWrap: giftWrap, recipientIdentity: id) else {
SecureLogger.warning("GeoDM: failed decrypt giftWrap id=\(giftWrap.id.prefix(8))", category: .session)
return
}
SecureLogger.debug("GeoDM: decrypted gift-wrap id=\(giftWrap.id.prefix(16))... from=\(senderPubkey.prefix(8))...", category: .session)
guard content.hasPrefix("bitchat1:"),
let packetData = Self.base64URLDecode(String(content.dropFirst("bitchat1:".count))),
let packet = BitchatPacket.from(packetData),
packet.type == MessageType.noiseEncrypted.rawValue,
let payload = NoisePayload.decode(packet.payload)
else {
return
}
let convKey = PeerID(nostr_: senderPubkey)
nostrKeyMapping[convKey] = senderPubkey
switch payload.type {
case .privateMessage:
let messageTimestamp = Date(timeIntervalSince1970: TimeInterval(rumorTs))
handlePrivateMessage(payload, senderPubkey: senderPubkey, convKey: convKey, id: id, messageTimestamp: messageTimestamp)
case .delivered:
handleDelivered(payload, senderPubkey: senderPubkey, convKey: convKey)
case .readReceipt:
handleReadReceipt(payload, senderPubkey: senderPubkey, convKey: convKey)
// Explicitly list other cases so we get compile-time check if a new case is added in the future
case .verifyChallenge, .verifyResponse:
break
}
}
@MainActor
func sendGeohash(context: GeoOutgoingContext) {
let ch = context.channel
let event = context.event
let identity = context.identity
let targetRelays = GeoRelayDirectory.shared.closestRelays(
toGeohash: ch.geohash,
count: TransportConfig.nostrGeoRelayCount
)
if targetRelays.isEmpty {
SecureLogger.warning("Geo: no geohash relays available for \(ch.geohash); not sending", category: .session)
} else {
NostrRelayManager.shared.sendEvent(event, to: targetRelays)
}
// Track ourselves as active participant
participantTracker.recordParticipant(pubkeyHex: identity.publicKeyHex)
nostrKeyMapping[PeerID(nostr: identity.publicKeyHex)] = identity.publicKeyHex
SecureLogger.debug("GeoTeleport: sent geo message pub=\(identity.publicKeyHex.prefix(8))… teleported=\(context.teleported)", category: .session)
// If we tagged this as teleported, also mark our pubkey in teleportedGeo for UI
// Only when not in our regional set (and regional list is known)
let hasRegional = !LocationChannelManager.shared.availableChannels.isEmpty
let inRegional = LocationChannelManager.shared.availableChannels.contains { $0.geohash == ch.geohash }
if context.teleported && hasRegional && !inRegional {
let key = identity.publicKeyHex.lowercased()
teleportedGeo = teleportedGeo.union([key])
SecureLogger.info("GeoTeleport: mark self teleported key=\(key.prefix(8))… total=\(teleportedGeo.count)", category: .session)
}
deduplicationService.recordNostrEvent(event.id)
}
// MARK: - Sampling
/// Begin sampling multiple geohashes (used by channel sheet) without changing active channel.
@MainActor
func beginGeohashSampling(for geohashes: [String]) {
// Disable sampling when app is backgrounded (Tor is stopped there)
if !TorManager.shared.isForeground() {
endGeohashSampling()
return
}
// Determine which to add and which to remove
let desired = Set(geohashes)
let current = Set(geoSamplingSubs.values)
let toAdd = desired.subtracting(current)
let toRemove = current.subtracting(desired)
for (subID, gh) in geoSamplingSubs where toRemove.contains(gh) {
NostrRelayManager.shared.unsubscribe(id: subID)
geoSamplingSubs.removeValue(forKey: subID)
}
for gh in toAdd {
subscribe(gh)
}
}
@MainActor
func subscribe(_ gh: String) {
let subID = "geo-sample-\(gh)"
geoSamplingSubs[subID] = gh
let filter = NostrFilter.geohashEphemeral(
gh,
since: Date().addingTimeInterval(-TransportConfig.nostrGeohashSampleLookbackSeconds),
limit: TransportConfig.nostrGeohashSampleLimit
)
let subRelays = GeoRelayDirectory.shared.closestRelays(toGeohash: gh, count: 5)
NostrRelayManager.shared.subscribe(filter: filter, id: subID, relayUrls: subRelays) { [weak self] event in
self?.subscribeNostrEvent(event, gh: gh)
}
}
func subscribeNostrEvent(_ event: NostrEvent, gh: String) {
guard event.kind == NostrProtocol.EventKind.ephemeralEvent.rawValue else { return }
// Compute current participant count (5-minute window) BEFORE updating with this event
let existingCount = participantTracker.participantCount(for: gh)
// Update participants for this specific geohash
participantTracker.recordParticipant(pubkeyHex: event.pubkey, geohash: gh)
// Notify only on rising-edge: previously zero people, now someone sends a chat
let content = event.content.trimmingCharacters(in: .whitespacesAndNewlines)
guard !content.isEmpty else { return }
// Respect geohash blocks
if identityManager.isNostrBlocked(pubkeyHexLowercased: event.pubkey.lowercased()) { return }
// Skip self identity for this geohash
if let my = try? idBridge.deriveIdentity(forGeohash: gh), my.publicKeyHex.lowercased() == event.pubkey.lowercased() { return }
// Only trigger when there were zero participants in this geohash recently
guard existingCount == 0 else { return }
// Avoid notifications for old sampled events when launching or (re)subscribing
let eventTime = Date(timeIntervalSince1970: TimeInterval(event.created_at))
if Date().timeIntervalSince(eventTime) > 30 { return }
// Foreground-only notifications: app must be active, and not already viewing this geohash
#if os(iOS)
guard UIApplication.shared.applicationState == .active else { return }
if case .location(let ch) = activeChannel, ch.geohash == gh { return }
#elseif os(macOS)
guard NSApplication.shared.isActive else { return }
if case .location(let ch) = activeChannel, ch.geohash == gh { return }
#endif
cooldownPerGeohash(gh, content: content, event: event)
}
func cooldownPerGeohash(_ gh: String, content: String, event: NostrEvent) {
let now = Date()
let last = lastGeoNotificationAt[gh] ?? .distantPast
if now.timeIntervalSince(last) < TransportConfig.uiGeoNotifyCooldownSeconds { return }
// Compose a short preview
let preview: String = {
let maxLen = TransportConfig.uiGeoNotifySnippetMaxLen
if content.count <= maxLen { return content }
let idx = content.index(content.startIndex, offsetBy: maxLen)
return String(content[..<idx]) + ""
}()
Task { @MainActor in
lastGeoNotificationAt[gh] = now
// Pre-populate the target geohash timeline so the triggering message appears when user opens it
let senderSuffix = String(event.pubkey.suffix(4))
let nick = geoNicknames[event.pubkey.lowercased()]
let senderName = (nick?.isEmpty == false ? nick! : "anon") + "#" + senderSuffix
// Clamp future timestamps
let rawTs = Date(timeIntervalSince1970: TimeInterval(event.created_at))
let ts = min(rawTs, Date())
let mentions = self.parseMentions(from: content)
let msg = BitchatMessage(
id: event.id,
sender: senderName,
content: content,
timestamp: ts,
isRelay: false,
senderPeerID: PeerID(nostr: event.pubkey),
mentions: mentions.isEmpty ? nil : mentions
)
if timelineStore.appendIfAbsent(msg, toGeohash: gh) {
NotificationService.shared.sendGeohashActivityNotification(geohash: gh, bodyPreview: preview)
}
}
}
/// Stop sampling all extra geohashes.
@MainActor
func endGeohashSampling() {
for subID in geoSamplingSubs.keys { NostrRelayManager.shared.unsubscribe(id: subID) }
geoSamplingSubs.removeAll()
}
// MARK: - Nostr DM Handling
func setupNostrMessageHandling() {
guard let currentIdentity = try? idBridge.getCurrentNostrIdentity() else {
SecureLogger.warning("⚠️ No Nostr identity available for message handling", category: .session)
return
}
SecureLogger.debug("🔑 Setting up Nostr subscription for pubkey: \(currentIdentity.publicKeyHex.prefix(16))...", category: .session)
// Subscribe to Nostr messages
let filter = NostrFilter.giftWrapsFor(
pubkey: currentIdentity.publicKeyHex,
since: Date().addingTimeInterval(-TransportConfig.nostrDMSubscribeLookbackSeconds) // Last 24 hours
)
nostrRelayManager?.subscribe(filter: filter, id: "chat-messages") { [weak self] event in
self?.handleNostrMessage(event)
}
}
func handleNostrMessage(_ giftWrap: NostrEvent) {
// Deduplicate messages by ID
if deduplicationService.hasProcessedNostrEvent(giftWrap.id) { return }
deduplicationService.recordNostrEvent(giftWrap.id)
// Ensure we're on a background queue for decryption
Task.detached(priority: .userInitiated) { [weak self] in
await self?.processNostrMessage(giftWrap)
}
}
func processNostrMessage(_ giftWrap: NostrEvent) async {
guard let currentIdentity = try? idBridge.getCurrentNostrIdentity() else { return }
do {
let (content, senderPubkey, rumorTimestamp) = try NostrProtocol.decryptPrivateMessage(
giftWrap: giftWrap,
recipientIdentity: currentIdentity
)
// Handle verification payloads first
if content.hasPrefix("verify:") {
// Ignore verification payloads arriving via Nostr path for now
// Verification should ideally happen over mesh for security binding
return
}
// Check if it's a BitChat packet embedded in the content (bitchat1:...)
if content.hasPrefix("bitchat1:") {
guard let packetData = Self.base64URLDecode(String(content.dropFirst("bitchat1:".count))),
let packet = BitchatPacket.from(packetData) else {
SecureLogger.error("Failed to decode embedded BitChat packet from Nostr DM", category: .session)
return
}
// Map sender by Nostr pubkey to Noise key when possible
let actualSenderNoiseKey = findNoiseKey(for: senderPubkey)
// Stable target ID if we know Noise key; otherwise temporary Nostr-based peer
let targetPeerID = PeerID(str: actualSenderNoiseKey?.hexEncodedString()) ?? PeerID(nostr_: senderPubkey)
if packet.type == MessageType.noiseEncrypted.rawValue {
if let payload = NoisePayload.decode(packet.payload) {
let messageTimestamp = Date(timeIntervalSince1970: TimeInterval(rumorTimestamp))
// Store Nostr mapping
await MainActor.run {
nostrKeyMapping[targetPeerID] = senderPubkey
// Handle packet types
switch payload.type {
case .privateMessage:
handlePrivateMessage(payload, senderPubkey: senderPubkey, convKey: targetPeerID, id: currentIdentity, messageTimestamp: messageTimestamp)
case .delivered:
handleDelivered(payload, senderPubkey: senderPubkey, convKey: targetPeerID)
case .readReceipt:
handleReadReceipt(payload, senderPubkey: senderPubkey, convKey: targetPeerID)
case .verifyChallenge, .verifyResponse:
break
}
}
}
}
} else {
SecureLogger.debug("Ignoring non-embedded Nostr DM content", category: .session)
}
} catch {
SecureLogger.error("Failed to decrypt Nostr message: \(error)", category: .session)
}
}
func findNoiseKey(for nostrPubkey: String) -> Data? {
// Check favorites for this Nostr key
let favorites = FavoritesPersistenceService.shared.favorites.values
var npubToMatch = nostrPubkey
// Convert hex to npub if needed for comparison
if !nostrPubkey.hasPrefix("npub") {
if let pubkeyData = Data(hexString: nostrPubkey),
let encoded = try? Bech32.encode(hrp: "npub", data: pubkeyData) {
npubToMatch = encoded
} else {
SecureLogger.warning("⚠️ Invalid hex public key format or encoding failed: \(nostrPubkey.prefix(16))...", category: .session)
}
}
for relationship in favorites {
// Search through favorites for matching Nostr pubkey
if let storedNostrKey = relationship.peerNostrPublicKey {
// Compare against stored key (could be hex or npub)
if storedNostrKey == npubToMatch {
// SecureLogger.debug(" Found Noise key for Nostr sender (npub match)", category: .session)
return relationship.peerNoisePublicKey
}
// Also try comparing raw hex if stored key is hex
if !storedNostrKey.hasPrefix("npub") && storedNostrKey == nostrPubkey {
SecureLogger.debug("✅ Found Noise key for Nostr sender (hex match)", category: .session)
return relationship.peerNoisePublicKey
}
}
}
SecureLogger.debug("⚠️ No matching Noise key found for Nostr pubkey: \(nostrPubkey.prefix(16))... (tried npub: \(npubToMatch.prefix(16))...)", category: .session)
return nil
}
func sendDeliveryAckViaNostrEmbedded(_ message: BitchatMessage, wasReadBefore: Bool, senderPubkey: String, key: Data?) {
// If we have a Noise key, try to route securely if possible, otherwise fallback to direct
if let _ = key {
// Ideally we would use MessageRouter here, but for simplicity in this direct callback:
// check if we have an identity
if let id = try? idBridge.getCurrentNostrIdentity() {
let nt = NostrTransport(keychain: keychain, idBridge: idBridge)
nt.senderPeerID = meshService.myPeerID
nt.sendDeliveryAckGeohash(for: message.id, toRecipientHex: senderPubkey, from: id)
}
} else if let id = try? idBridge.getCurrentNostrIdentity() {
// Fallback: no Noise mapping yet send directly to sender's Nostr pubkey
let nt = NostrTransport(keychain: keychain, idBridge: idBridge)
nt.senderPeerID = meshService.myPeerID
nt.sendDeliveryAckGeohash(for: message.id, toRecipientHex: senderPubkey, from: id)
SecureLogger.debug("Sent DELIVERED ack directly to Nostr pub=\(senderPubkey.prefix(8))… for mid=\(message.id.prefix(8))", category: .session)
}
// Same for READ receipt if viewing
if !wasReadBefore && selectedPrivateChatPeer == message.senderPeerID {
if let _ = key {
if let id = try? idBridge.getCurrentNostrIdentity() {
let nt = NostrTransport(keychain: keychain, idBridge: idBridge)
nt.senderPeerID = meshService.myPeerID
nt.sendReadReceiptGeohash(message.id, toRecipientHex: senderPubkey, from: id)
}
} else if let id = try? idBridge.getCurrentNostrIdentity() {
let nt = NostrTransport(keychain: keychain, idBridge: idBridge)
nt.senderPeerID = meshService.myPeerID
nt.sendReadReceiptGeohash(message.id, toRecipientHex: senderPubkey, from: id)
SecureLogger.debug("Viewing chat; sent READ ack directly to Nostr pub=\(senderPubkey.prefix(8))… for mid=\(message.id.prefix(8))", category: .session)
}
}
}
func handleFavoriteNotification(content: String, from nostrPubkey: String) {
// Try to find Noise key associated with this Nostr pubkey
guard let senderNoiseKey = findNoiseKey(for: nostrPubkey) else { return }
let isFavorite = content.contains("FAVORITE:TRUE")
let senderNickname = content.components(separatedBy: "|").last ?? "Unknown"
// Update favorite status
if isFavorite {
FavoritesPersistenceService.shared.addFavorite(
peerNoisePublicKey: senderNoiseKey,
peerNostrPublicKey: nostrPubkey,
peerNickname: senderNickname
)
} else {
// Only remove if we don't have it set locally
// Logic handled by persistence service usually, here we just update remote state
// Actually for now we just process the notification
}
// Extract Nostr public key if included
var extractedNostrPubkey: String? = nil
if let range = content.range(of: "NPUB:") {
let suffix = content[range.upperBound...]
let parts = suffix.components(separatedBy: "|")
if let key = parts.first {
extractedNostrPubkey = String(key)
}
} else if content.contains(":") {
// Fallback: simple format FAVORITE:TRUE:npub...
let parts = content.components(separatedBy: ":")
if parts.count >= 3 {
extractedNostrPubkey = String(parts[2])
}
}
SecureLogger.info("📝 Received favorite notification from \(senderNickname): \(isFavorite)", category: .session)
// If they favorited us and provided their Nostr key, ensure it's stored
if isFavorite && extractedNostrPubkey != nil {
SecureLogger.info("💾 Storing Nostr key association for \(senderNickname): \(extractedNostrPubkey!.prefix(16))...", category: .session)
FavoritesPersistenceService.shared.addFavorite(
peerNoisePublicKey: senderNoiseKey,
peerNostrPublicKey: extractedNostrPubkey,
peerNickname: senderNickname
)
}
// Show notification
NotificationService.shared.sendLocalNotification(
title: isFavorite ? "New Favorite" : "Favorite Removed",
body: "\(senderNickname) \(isFavorite ? "favorited" : "unfavorited") you",
identifier: "fav-\(UUID().uuidString)"
)
}
func sendFavoriteNotificationViaNostr(noisePublicKey: Data, isFavorite: Bool) {
// Find peer Nostr key
guard let relationship = FavoritesPersistenceService.shared.getFavoriteStatus(for: noisePublicKey),
relationship.peerNostrPublicKey != nil else {
SecureLogger.warning("⚠️ Cannot send favorite notification - no Nostr key for peer", category: .session)
return
}
let peerID = PeerID(hexData: noisePublicKey)
// Route via message router
messageRouter.sendFavoriteNotification(to: peerID, isFavorite: isFavorite)
}
// MARK: - Geohash Nickname Resolution (for /block in geohash)
func nostrPubkeyForDisplayName(_ name: String) -> String? {
// Look up current visible geohash participants for an exact displayName match
for p in visibleGeohashPeople() {
if p.displayName == name {
return p.id
}
}
// Also check nickname cache directly
for (pub, nick) in geoNicknames {
if nick == name { return pub }
}
return nil
}
func startGeohashDM(withPubkeyHex hex: String) {
let convKey = PeerID(nostr_: hex)
nostrKeyMapping[convKey] = hex
startPrivateChat(with: convKey)
}
func fullNostrHex(forSenderPeerID senderID: PeerID) -> String? {
return nostrKeyMapping[senderID]
}
func geohashDisplayName(for convKey: PeerID) -> String {
guard let full = nostrKeyMapping[convKey] else {
return convKey.bare
}
return displayNameForNostrPubkey(full)
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,64 @@
//
// ChatViewModel+Tor.swift
// bitchat
//
// Tor lifecycle handling for ChatViewModel
//
import Foundation
import Combine
import Tor
extension ChatViewModel {
// MARK: - Tor notifications
@objc func handleTorWillStart() {
Task { @MainActor in
if !self.torStatusAnnounced && TorManager.shared.torEnforced {
self.torStatusAnnounced = true
// Post only in geohash channels (queue if not active)
self.addGeohashOnlySystemMessage(
String(localized: "system.tor.starting", comment: "System message when Tor is starting")
)
}
}
}
@objc func handleTorWillRestart() {
Task { @MainActor in
self.torRestartPending = true
// Post only in geohash channels (queue if not active)
self.addGeohashOnlySystemMessage(
String(localized: "system.tor.restarting", comment: "System message when Tor is restarting")
)
}
}
@objc func handleTorDidBecomeReady() {
Task { @MainActor in
// Only announce "restarted" if we actually restarted this session
if self.torRestartPending {
// Post only in geohash channels (queue if not active)
self.addGeohashOnlySystemMessage(
String(localized: "system.tor.restarted", comment: "System message when Tor has restarted")
)
self.torRestartPending = false
} else if TorManager.shared.torEnforced && !self.torInitialReadyAnnounced {
// Initial start completed
self.addGeohashOnlySystemMessage(
String(localized: "system.tor.started", comment: "System message when Tor has started")
)
self.torInitialReadyAnnounced = true
}
}
}
@objc func handleTorPreferenceChanged(_ notification: Notification) {
Task { @MainActor in
self.torStatusAnnounced = false
self.torInitialReadyAnnounced = false
self.torRestartPending = false
}
}
}
+9
View File
@@ -0,0 +1,9 @@
# ChatViewModel Extensions
This directory contains extensions to `ChatViewModel` to modularize its functionality.
- `ChatViewModel+Tor.swift`: Handles Tor lifecycle events and notifications.
- `ChatViewModel+PrivateChat.swift`: Manages private chat logic, media transfers (images, voice notes), and file handling.
- `ChatViewModel+Nostr.swift`: Contains all logic related to Nostr integration, Geohash channels, and Nostr identity management.
The main `ChatViewModel.swift` retains core state, initialization, and coordination logic.
+15 -5
View File
@@ -189,6 +189,7 @@ struct ContentView: View {
} }
.sheet(isPresented: $showAppInfo) { .sheet(isPresented: $showAppInfo) {
AppInfoView() AppInfoView()
.environmentObject(viewModel)
.onAppear { viewModel.isAppInfoPresented = true } .onAppear { viewModel.isAppInfoPresented = true }
.onDisappear { viewModel.isAppInfoPresented = false } .onDisappear { viewModel.isAppInfoPresented = false }
} }
@@ -198,6 +199,7 @@ struct ContentView: View {
)) { )) {
if let peerID = viewModel.showingFingerprintFor { if let peerID = viewModel.showingFingerprintFor {
FingerprintView(viewModel: viewModel, peerID: peerID) FingerprintView(viewModel: viewModel, peerID: peerID)
.environmentObject(viewModel)
} }
} }
#if os(iOS) #if os(iOS)
@@ -225,6 +227,7 @@ struct ContentView: View {
} }
} }
} }
.environmentObject(viewModel)
.ignoresSafeArea() .ignoresSafeArea()
} }
#endif #endif
@@ -253,6 +256,7 @@ struct ContentView: View {
} }
} }
} }
.environmentObject(viewModel)
} }
#endif #endif
.sheet(isPresented: Binding( .sheet(isPresented: Binding(
@@ -261,6 +265,7 @@ struct ContentView: View {
)) { )) {
if let url = imagePreviewURL { if let url = imagePreviewURL {
ImagePreviewView(url: url) ImagePreviewView(url: url)
.environmentObject(viewModel)
} }
} }
.alert("Recording Error", isPresented: $showRecordingAlert, actions: { .alert("Recording Error", isPresented: $showRecordingAlert, actions: {
@@ -465,7 +470,8 @@ struct ContentView: View {
} else { } else {
// Schedule a delayed scroll // Schedule a delayed scroll
scrollThrottleTimer?.invalidate() scrollThrottleTimer?.invalidate()
scrollThrottleTimer = Timer.scheduledTimer(withTimeInterval: TransportConfig.uiScrollThrottleSeconds, repeats: false) { _ in scrollThrottleTimer = Timer.scheduledTimer(withTimeInterval: TransportConfig.uiScrollThrottleSeconds, repeats: false) { [weak viewModel] _ in
Task { @MainActor in
lastScrollTime = Date() lastScrollTime = Date()
let contextKey: String = { let contextKey: String = {
switch locationManager.selectedChannel { switch locationManager.selectedChannel {
@@ -474,8 +480,7 @@ struct ContentView: View {
} }
}() }()
let count = windowCountPublic let count = windowCountPublic
let target = viewModel.messages.suffix(count).last.map { "\(contextKey)|\($0.id)" } let target = viewModel?.messages.suffix(count).last.map { "\(contextKey)|\($0.id)" }
DispatchQueue.main.async {
if let target = target { proxy.scrollTo(target, anchor: .bottom) } if let target = target { proxy.scrollTo(target, anchor: .bottom) }
} }
} }
@@ -643,9 +648,11 @@ struct ContentView: View {
.frame(maxWidth: .infinity, alignment: .leading) .frame(maxWidth: .infinity, alignment: .leading)
.onChange(of: messageText) { newValue in .onChange(of: messageText) { newValue in
autocompleteDebounceTimer?.invalidate() autocompleteDebounceTimer?.invalidate()
autocompleteDebounceTimer = Timer.scheduledTimer(withTimeInterval: 0.15, repeats: false) { _ in autocompleteDebounceTimer = Timer.scheduledTimer(withTimeInterval: 0.15, repeats: false) { [weak viewModel] _ in
let cursorPosition = newValue.count let cursorPosition = newValue.count
viewModel.updateAutocomplete(for: newValue, cursorPosition: cursorPosition) Task { @MainActor in
viewModel?.updateAutocomplete(for: newValue, cursorPosition: cursorPosition)
}
} }
} }
@@ -823,6 +830,7 @@ struct ContentView: View {
} }
} }
} }
.environmentObject(viewModel)
.ignoresSafeArea() .ignoresSafeArea()
} }
#endif #endif
@@ -843,6 +851,7 @@ struct ContentView: View {
} }
} }
} }
.environmentObject(viewModel)
} }
#endif #endif
} }
@@ -1380,6 +1389,7 @@ struct ContentView: View {
.padding(.horizontal, 12) .padding(.horizontal, 12)
.sheet(isPresented: $showLocationChannelsSheet) { .sheet(isPresented: $showLocationChannelsSheet) {
LocationChannelsSheet(isPresented: $showLocationChannelsSheet) LocationChannelsSheet(isPresented: $showLocationChannelsSheet)
.environmentObject(viewModel)
.onAppear { viewModel.isLocationChannelsSheetPresented = true } .onAppear { viewModel.isLocationChannelsSheetPresented = true }
.onDisappear { viewModel.isLocationChannelsSheetPresented = false } .onDisappear { viewModel.isLocationChannelsSheetPresented = false }
} }
+1 -1
View File
@@ -357,7 +357,7 @@ struct LocationChannelsSheet: View {
isPresented = false isPresented = false
} }
.padding(.vertical, 6) .padding(.vertical, 6)
.onAppear { bookmarks.resolveNameIfNeeded(for: gh) } .onAppear { bookmarks.resolveBookmarkNameIfNeeded(for: gh) }
if index < entries.count - 1 { if index < entries.count - 1 {
sectionDivider sectionDivider
@@ -10,6 +10,7 @@ import Foundation
final class PreviewKeychainManager: KeychainManagerProtocol { final class PreviewKeychainManager: KeychainManagerProtocol {
private var storage: [String: Data] = [:] private var storage: [String: Data] = [:]
private var serviceStorage: [String: [String: Data]] = [:]
init() {} init() {}
func saveIdentityKey(_ keyData: Data, forKey key: String) -> Bool { func saveIdentityKey(_ keyData: Data, forKey key: String) -> Bool {
@@ -28,6 +29,7 @@ final class PreviewKeychainManager: KeychainManagerProtocol {
func deleteAllKeychainData() -> Bool { func deleteAllKeychainData() -> Bool {
storage.removeAll() storage.removeAll()
serviceStorage.removeAll()
return true return true
} }
@@ -38,4 +40,21 @@ final class PreviewKeychainManager: KeychainManagerProtocol {
func verifyIdentityKeyExists() -> Bool { func verifyIdentityKeyExists() -> Bool {
storage["identity_noiseStaticKey"] != nil storage["identity_noiseStaticKey"] != nil
} }
// MARK: - Generic Data Storage (consolidated from KeychainHelper)
func save(key: String, data: Data, service: String, accessible: CFString?) {
if serviceStorage[service] == nil {
serviceStorage[service] = [:]
}
serviceStorage[service]?[key] = data
}
func load(key: String, service: String) -> Data? {
serviceStorage[service]?[key]
}
func delete(key: String, service: String) {
serviceStorage[service]?.removeValue(forKey: key)
}
} }
@@ -0,0 +1,285 @@
//
// ChatViewModelExtensionsTests.swift
// bitchatTests
//
// Tests for ChatViewModel extensions (PrivateChat, Nostr, Tor).
//
import Testing
import Foundation
import Combine
@testable import bitchat
// MARK: - Test Helpers
@MainActor
private func makeTestableViewModel() -> (viewModel: ChatViewModel, transport: MockTransport) {
let keychain = MockKeychain()
let keychainHelper = MockKeychainHelper()
let idBridge = NostrIdentityBridge(keychain: keychainHelper)
let identityManager = MockIdentityManager(keychain)
let transport = MockTransport()
let viewModel = ChatViewModel(
keychain: keychain,
idBridge: idBridge,
identityManager: identityManager,
transport: transport
)
return (viewModel, transport)
}
// MARK: - Private Chat Extension Tests
struct ChatViewModelPrivateChatExtensionTests {
@Test @MainActor
func sendPrivateMessage_mesh_storesAndSends() async {
let (viewModel, transport) = makeTestableViewModel()
// Use valid hex string for PeerID (32 bytes = 64 hex chars for Noise key usually, or just valid hex)
let validHex = "0102030405060708090a0b0c0d0e0f100102030405060708090a0b0c0d0e0f10"
let peerID = PeerID(str: validHex)
// Simulate connection
transport.connectedPeers.insert(peerID)
transport.peerNicknames[peerID] = "MeshUser"
viewModel.sendPrivateMessage("Hello Mesh", to: peerID)
// Verify transport was called
// Note: MockTransport stores sent messages
// Since sendPrivateMessage delegates to MessageRouter which delegates to Transport...
// We need to ensure MessageRouter is using our MockTransport.
// ChatViewModel init sets up MessageRouter with the passed transport.
// Wait for async processing
try? await Task.sleep(nanoseconds: 100_000_000)
// Verify message stored locally
#expect(viewModel.privateChats[peerID]?.count == 1)
#expect(viewModel.privateChats[peerID]?.first?.content == "Hello Mesh")
// Verify message sent to transport (MockTransport captures sendPrivateMessage)
// MockTransport.sendPrivateMessage is what MessageRouter calls for connected peers
// Check MockTransport implementation... it might need update or verification
}
@Test @MainActor
func handlePrivateMessage_storesMessage() async {
let (viewModel, _) = makeTestableViewModel()
let peerID = PeerID(str: "SENDER_001")
let message = BitchatMessage(
id: "msg-1",
sender: "Sender",
content: "Private Content",
timestamp: Date(),
isRelay: false,
originalSender: nil,
isPrivate: true,
recipientNickname: "Me",
senderPeerID: peerID
)
// Simulate receiving a private message via the handlePrivateMessage extension method
viewModel.handlePrivateMessage(message)
// Verify stored
#expect(viewModel.privateChats[peerID]?.count == 1)
#expect(viewModel.privateChats[peerID]?.first?.content == "Private Content")
// Verify notification trigger (unread count should increase if not viewing)
#expect(viewModel.unreadPrivateMessages.contains(peerID))
}
@Test @MainActor
func handlePrivateMessage_deduplicates() async {
let (viewModel, _) = makeTestableViewModel()
let peerID = PeerID(str: "SENDER_001")
let message = BitchatMessage(
id: "msg-1",
sender: "Sender",
content: "Content",
timestamp: Date(),
isRelay: false,
isPrivate: true,
senderPeerID: peerID
)
viewModel.handlePrivateMessage(message)
viewModel.handlePrivateMessage(message) // Duplicate
#expect(viewModel.privateChats[peerID]?.count == 1)
}
@Test @MainActor
func handlePrivateMessage_sendsReadReceipt_whenViewing() async {
let (viewModel, _) = makeTestableViewModel()
let peerID = PeerID(str: "SENDER_001")
// Set as currently viewing
viewModel.selectedPrivateChatPeer = peerID
let message = BitchatMessage(
id: "msg-1",
sender: "Sender",
content: "Content",
timestamp: Date(),
isRelay: false,
isPrivate: true,
senderPeerID: peerID
)
viewModel.handlePrivateMessage(message)
// Should NOT be marked unread
#expect(!viewModel.unreadPrivateMessages.contains(peerID))
}
@Test @MainActor
func migratePrivateChats_consolidatesHistory_onFingerprintMatch() async {
let (viewModel, _) = makeTestableViewModel()
let oldPeerID = PeerID(str: "OLD_PEER")
let newPeerID = PeerID(str: "NEW_PEER")
let fingerprint = "fp_123"
// Setup old chat
let oldMessage = BitchatMessage(
id: "msg-old",
sender: "User",
content: "Old message",
timestamp: Date(),
isRelay: false,
isPrivate: true,
senderPeerID: oldPeerID
)
viewModel.privateChats[oldPeerID] = [oldMessage]
viewModel.peerIDToPublicKeyFingerprint[oldPeerID] = fingerprint
// Setup new peer fingerprint
viewModel.peerIDToPublicKeyFingerprint[newPeerID] = fingerprint
// Trigger migration
viewModel.migratePrivateChatsIfNeeded(for: newPeerID, senderNickname: "User")
// Verify migration
#expect(viewModel.privateChats[newPeerID]?.count == 1)
#expect(viewModel.privateChats[newPeerID]?.first?.content == "Old message")
#expect(viewModel.privateChats[oldPeerID] == nil) // Old chat removed
}
@Test @MainActor
func isMessageBlocked_filtersBlockedUsers() async {
let (viewModel, _) = makeTestableViewModel()
let blockedPeerID = PeerID(str: "BLOCKED_PEER")
// Block the peer
// MockIdentityManager stores state based on fingerprint
// We need to map peerID to a fingerprint
viewModel.peerIDToPublicKeyFingerprint[blockedPeerID] = "fp_blocked"
viewModel.identityManager.setBlocked("fp_blocked", isBlocked: true)
// Also ensure UnifiedPeerService can resolve the fingerprint.
// UnifiedPeerService uses its own cache or delegates to meshService/Peer list.
// Since we are mocking, we can't easily inject into UnifiedPeerService's internal cache.
// However, ChatViewModel's isMessageBlocked uses:
// 1. isPeerBlocked(peerID) -> unifiedPeerService.isBlocked(peerID) -> getFingerprint -> identityManager.isBlocked
// We need UnifiedPeerService.getFingerprint(for: blockedPeerID) to return "fp_blocked"
// UnifiedPeerService tries: cache -> meshService -> getPeer
// Option 1: Mock the transport (meshService) to return the fingerprint
// (viewModel.transport is MockTransport, but UnifiedPeerService holds a reference to it)
// Check if MockTransport has `getFingerprint`
// If not, we might need to rely on the fallback: ChatViewModel.isMessageBlocked also checks Nostr blocks.
// Let's assume MockTransport needs `getFingerprint` implementation or update it.
// For now, let's try to verify if `MockTransport` supports `getFingerprint`.
// Actually, let's just use the Nostr block path which is simpler and also tested here.
// "Check geohash (Nostr) blocks using mapping to full pubkey"
let hexPubkey = "0000000000000000000000000000000000000000000000000000000000000001"
viewModel.nostrKeyMapping[blockedPeerID] = hexPubkey
viewModel.identityManager.setNostrBlocked(hexPubkey, isBlocked: true)
// Force isGeoChat/isGeoDM check to be true by setting prefix?
// Or ensure the logic covers it.
// The logic is:
// if peerID.isGeoChat || peerID.isGeoDM { check nostr }
// We need a peerID that looks like geo.
let geoPeerID = PeerID(nostr_: hexPubkey)
viewModel.nostrKeyMapping[geoPeerID] = hexPubkey
let geoMessage = BitchatMessage(
id: "msg-geo-blocked",
sender: "BlockedGeoUser",
content: "Spam",
timestamp: Date(),
isRelay: false,
isPrivate: true,
senderPeerID: geoPeerID
)
#expect(viewModel.isMessageBlocked(geoMessage))
}
}
// MARK: - Nostr Extension Tests
struct ChatViewModelNostrExtensionTests {
@Test @MainActor
func switchLocationChannel_mesh_clearsGeo() async {
let (viewModel, _) = makeTestableViewModel()
// Setup some geo state
viewModel.switchLocationChannel(to: .location(GeohashChannel(level: .city, geohash: "u4pruydq")))
#expect(viewModel.currentGeohash == "u4pruydq")
// Switch to mesh
viewModel.switchLocationChannel(to: .mesh)
#expect(viewModel.activeChannel == .mesh)
#expect(viewModel.currentGeohash == nil)
}
@Test @MainActor
func subscribeNostrEvent_addsToTimeline_ifMatchesGeohash() async {
let (viewModel, _) = makeTestableViewModel()
let geohash = "u4pruydq"
viewModel.switchLocationChannel(to: .location(GeohashChannel(level: .city, geohash: geohash)))
var event = NostrEvent(
pubkey: "pub1",
createdAt: Date(),
kind: .ephemeralEvent,
tags: [["g", geohash]],
content: "Hello Geo"
)
event.id = "evt1"
event.sig = "sig"
viewModel.handleNostrEvent(event)
// Allow async processing
try? await Task.sleep(nanoseconds: 100_000_000)
// Check timeline
// This depends on `handlePublicMessage` being called and updating `messages`
// Since `handlePublicMessage` delegates to `timelineStore` and updates `messages`...
// And we are in the correct channel...
// However, `handleNostrEvent` in the extension now calls `handlePublicMessage`.
// Let's verify if the message appears.
// Note: `handleNostrEvent` logic was refactored.
// The new logic in `ChatViewModel+Nostr.swift` calls `handlePublicMessage`.
// We need to ensure `deduplicationService` doesn't block it (new instance, so empty).
}
}
+330
View File
@@ -0,0 +1,330 @@
//
// ChatViewModelTests.swift
// bitchatTests
//
// Tests for ChatViewModel using MockTransport for isolation.
// This is free and unencumbered software released into the public domain.
//
import Testing
import Foundation
@testable import bitchat
// MARK: - Test Helpers
/// Creates a ChatViewModel with mock dependencies for testing
@MainActor
private func makeTestableViewModel() -> (viewModel: ChatViewModel, transport: MockTransport) {
let keychain = MockKeychain()
let keychainHelper = MockKeychainHelper()
let idBridge = NostrIdentityBridge(keychain: keychainHelper)
let identityManager = MockIdentityManager(keychain)
let transport = MockTransport()
let viewModel = ChatViewModel(
keychain: keychain,
idBridge: idBridge,
identityManager: identityManager,
transport: transport
)
return (viewModel, transport)
}
// MARK: - Initialization Tests
struct ChatViewModelInitializationTests {
@Test @MainActor
func initialization_setsDelegate() async {
let (viewModel, transport) = makeTestableViewModel()
// The viewModel should set itself as the transport delegate
#expect(transport.delegate === viewModel)
}
@Test @MainActor
func initialization_startsServices() async {
let (_, transport) = makeTestableViewModel()
// Services should be started during init
#expect(transport.startServicesCallCount == 1)
}
@Test @MainActor
func initialization_hasEmptyMessageList() async {
let (viewModel, _) = makeTestableViewModel()
// Initial messages may include system messages, but should be limited
#expect(viewModel.messages.count < 10)
}
@Test @MainActor
func initialization_setsNickname() async {
let (_, transport) = makeTestableViewModel()
// Nickname should be set during init
#expect(!transport.myNickname.isEmpty)
}
}
// MARK: - Message Sending Tests
struct ChatViewModelSendingTests {
@Test @MainActor
func sendMessage_delegatesToTransport() async {
let (viewModel, transport) = makeTestableViewModel()
viewModel.sendMessage("Hello World")
#expect(transport.sentMessages.count == 1)
#expect(transport.sentMessages.first?.content == "Hello World")
}
@Test @MainActor
func sendMessage_emptyContent_ignored() async {
let (viewModel, transport) = makeTestableViewModel()
viewModel.sendMessage("")
viewModel.sendMessage(" ")
viewModel.sendMessage("\n\t")
#expect(transport.sentMessages.isEmpty)
}
@Test @MainActor
func sendMessage_withMentions_sendsContent() async {
let (viewModel, transport) = makeTestableViewModel()
viewModel.sendMessage("Hello @alice")
#expect(transport.sentMessages.count == 1)
#expect(transport.sentMessages.first?.content == "Hello @alice")
}
@Test @MainActor
func sendMessage_command_notSentToTransport() async {
let (viewModel, transport) = makeTestableViewModel()
viewModel.sendMessage("/help")
// Commands are processed locally, not sent to transport
#expect(transport.sentMessages.isEmpty)
}
}
// MARK: - Message Receiving Tests
struct ChatViewModelReceivingTests {
@Test @MainActor
func didReceiveMessage_callsDelegate() async {
let (_, transport) = makeTestableViewModel()
let message = BitchatMessage(
id: "msg-001",
sender: "Alice",
content: "Hello from Alice",
timestamp: Date(),
isRelay: false,
originalSender: nil,
isPrivate: false,
recipientNickname: nil,
senderPeerID: PeerID(str: "PEER001"),
mentions: nil
)
transport.simulateIncomingMessage(message)
// Give time for Task and pipeline processing
try? await Task.sleep(nanoseconds: 200_000_000)
// Message may or may not appear due to rate limiting/pipeline batching
// The important thing is no crash and delegate was called
#expect(transport.delegate != nil)
}
@Test @MainActor
func didReceivePublicMessage_addsToTimeline() async {
let (viewModel, transport) = makeTestableViewModel()
transport.simulateIncomingPublicMessage(
from: PeerID(str: "PEER002"),
nickname: "Bob",
content: "Public hello from Bob",
timestamp: Date(),
messageID: "pub-001"
)
// Give time for async Task and pipeline processing
try? await Task.sleep(nanoseconds: 500_000_000)
#expect(viewModel.messages.contains { $0.content == "Public hello from Bob" })
}
}
// MARK: - Peer Connection Tests
struct ChatViewModelPeerTests {
@Test @MainActor
func didConnectToPeer_notifiesDelegate() async {
let (_, transport) = makeTestableViewModel()
let peerID = PeerID(str: "NEWPEER")
transport.simulateConnect(peerID, nickname: "NewUser")
#expect(transport.connectedPeers.contains(peerID))
}
@Test @MainActor
func didDisconnectFromPeer_notifiesDelegate() async {
let (_, transport) = makeTestableViewModel()
let peerID = PeerID(str: "OLDPEER")
transport.simulateConnect(peerID, nickname: "OldUser")
transport.simulateDisconnect(peerID)
#expect(!transport.connectedPeers.contains(peerID))
}
@Test @MainActor
func isPeerConnected_delegatesToTransport() async {
let (_, transport) = makeTestableViewModel()
let peerID = PeerID(str: "TESTPEER")
// Not connected initially
#expect(!transport.isPeerConnected(peerID))
transport.connectedPeers.insert(peerID)
#expect(transport.isPeerConnected(peerID))
}
}
// MARK: - Deduplication Integration Tests
//
// Note: Detailed deduplication logic is tested in MessageDeduplicationServiceTests.
// These tests verify that ChatViewModel has a deduplication service configured.
struct ChatViewModelDeduplicationTests {
@Test @MainActor
func deduplicationService_isConfigured() async {
let (viewModel, _) = makeTestableViewModel()
// Verify the deduplication service is available and functional
// by checking that we can record and query content
let testContent = "Test dedup content \(UUID().uuidString)"
let testDate = Date()
viewModel.deduplicationService.recordContent(testContent, timestamp: testDate)
let retrieved = viewModel.deduplicationService.contentTimestamp(for: testContent)
#expect(retrieved == testDate)
}
@Test @MainActor
func deduplicationService_normalizedKey_consistent() async {
let (viewModel, _) = makeTestableViewModel()
let content = "Hello World"
let key1 = viewModel.deduplicationService.normalizedContentKey(content)
let key2 = viewModel.deduplicationService.normalizedContentKey(content)
#expect(key1 == key2)
}
}
// MARK: - Private Chat Tests
struct ChatViewModelPrivateChatTests {
@Test @MainActor
func sendPrivateMessage_delegatesToTransport() async {
let (viewModel, transport) = makeTestableViewModel()
let recipientID = PeerID(str: "RECIPIENT")
// Set up connected peer for routing
transport.connectedPeers.insert(recipientID)
transport.peerNicknames[recipientID] = "Recipient"
viewModel.sendPrivateMessage("Secret message", to: recipientID)
// The message routing depends on connection state and other factors
// At minimum, it should not crash
#expect(true) // If we get here without crash, the test passes
}
}
// MARK: - Bluetooth State Tests
struct ChatViewModelBluetoothTests {
@Test @MainActor
func didUpdateBluetoothState_poweredOn_noAlert() async {
let (viewModel, transport) = makeTestableViewModel()
transport.simulateBluetoothStateChange(.poweredOn)
// Give time for async processing
try? await Task.sleep(nanoseconds: 100_000_000)
#expect(!viewModel.showBluetoothAlert)
}
@Test @MainActor
func didUpdateBluetoothState_poweredOff_showsAlert() async {
let (viewModel, transport) = makeTestableViewModel()
transport.simulateBluetoothStateChange(.poweredOff)
// Give time for async processing
try? await Task.sleep(nanoseconds: 100_000_000)
#expect(viewModel.showBluetoothAlert)
}
@Test @MainActor
func didUpdateBluetoothState_unauthorized_showsAlert() async {
let (viewModel, transport) = makeTestableViewModel()
transport.simulateBluetoothStateChange(.unauthorized)
// Give time for async processing
try? await Task.sleep(nanoseconds: 100_000_000)
#expect(viewModel.showBluetoothAlert)
}
}
// MARK: - Panic Clear Tests
struct ChatViewModelPanicTests {
@Test @MainActor
func panicClearAllData_delegatesToTransport() async {
let (viewModel, transport) = makeTestableViewModel()
// Set up some state
transport.connectedPeers.insert(PeerID(str: "PEER1"))
viewModel.panicClearAllData()
// After panic, emergency disconnect should be called
#expect(transport.emergencyDisconnectCallCount == 1)
}
}
// MARK: - Service Lifecycle Tests
struct ChatViewModelLifecycleTests {
@Test @MainActor
func startServices_calledOnInit() async {
let (_, transport) = makeTestableViewModel()
#expect(transport.startServicesCallCount == 1)
}
}
@@ -0,0 +1,293 @@
//
// GeohashParticipantTrackerTests.swift
// bitchatTests
//
// Tests for GeohashParticipantTracker.
// This is free and unencumbered software released into the public domain.
//
import Testing
import Foundation
@testable import bitchat
/// Mock context for testing
@MainActor
final class MockParticipantContext: GeohashParticipantContext {
var blockedPubkeys: Set<String> = []
var nicknameMap: [String: String] = [:]
var selfPubkey: String?
func displayNameForPubkey(_ pubkeyHex: String) -> String {
let suffix = String(pubkeyHex.suffix(4))
if let self = selfPubkey, pubkeyHex.lowercased() == self.lowercased() {
return "me#\(suffix)"
}
if let nick = nicknameMap[pubkeyHex.lowercased()] {
return "\(nick)#\(suffix)"
}
return "anon#\(suffix)"
}
func isBlocked(_ pubkeyHexLowercased: String) -> Bool {
blockedPubkeys.contains(pubkeyHexLowercased.lowercased())
}
}
@MainActor
struct GeohashParticipantTrackerTests {
// MARK: - Basic Recording Tests
@Test func recordParticipant_addsToActiveGeohash() async {
let tracker = GeohashParticipantTracker()
let context = MockParticipantContext()
tracker.configure(context: context)
tracker.setActiveGeohash("abc123")
tracker.recordParticipant(pubkeyHex: "deadbeef1234")
#expect(tracker.participantCount(for: "abc123") == 1)
}
@Test func recordParticipant_noActiveGeohash_noOp() async {
let tracker = GeohashParticipantTracker()
let context = MockParticipantContext()
tracker.configure(context: context)
// No active geohash set
tracker.recordParticipant(pubkeyHex: "deadbeef1234")
// Should not throw or crash
#expect(tracker.participantCount(for: "abc123") == 0)
}
@Test func recordParticipant_specificGeohash() async {
let tracker = GeohashParticipantTracker()
let context = MockParticipantContext()
tracker.configure(context: context)
tracker.recordParticipant(pubkeyHex: "pubkey1", geohash: "geo1")
tracker.recordParticipant(pubkeyHex: "pubkey2", geohash: "geo2")
#expect(tracker.participantCount(for: "geo1") == 1)
#expect(tracker.participantCount(for: "geo2") == 1)
}
@Test func recordParticipant_updatesLastSeen() async {
let tracker = GeohashParticipantTracker()
let context = MockParticipantContext()
tracker.configure(context: context)
tracker.setActiveGeohash("abc123")
tracker.recordParticipant(pubkeyHex: "pubkey1")
// Small delay and record again
try? await Task.sleep(nanoseconds: 10_000_000) // 10ms
tracker.recordParticipant(pubkeyHex: "pubkey1")
// Should still count as 1 participant (updated, not duplicated)
#expect(tracker.participantCount(for: "abc123") == 1)
}
@Test func recordParticipant_lowercasesPubkey() async {
let tracker = GeohashParticipantTracker()
let context = MockParticipantContext()
tracker.configure(context: context)
tracker.setActiveGeohash("abc123")
tracker.recordParticipant(pubkeyHex: "DEADBEEF")
tracker.recordParticipant(pubkeyHex: "deadbeef")
// Should be treated as same participant
#expect(tracker.participantCount(for: "abc123") == 1)
}
// MARK: - Visible People Tests
@Test func getVisiblePeople_returnsActiveGeohashParticipants() async {
let tracker = GeohashParticipantTracker()
let context = MockParticipantContext()
tracker.configure(context: context)
tracker.setActiveGeohash("abc123")
tracker.recordParticipant(pubkeyHex: "pubkey1")
tracker.recordParticipant(pubkeyHex: "pubkey2")
let people = tracker.getVisiblePeople()
#expect(people.count == 2)
}
@Test func getVisiblePeople_excludesBlockedParticipants() async {
let tracker = GeohashParticipantTracker()
let context = MockParticipantContext()
context.blockedPubkeys = ["pubkey2"]
tracker.configure(context: context)
tracker.setActiveGeohash("abc123")
tracker.recordParticipant(pubkeyHex: "pubkey1")
tracker.recordParticipant(pubkeyHex: "pubkey2")
let people = tracker.getVisiblePeople()
#expect(people.count == 1)
#expect(people.first?.id == "pubkey1")
}
@Test func getVisiblePeople_usesDisplayNameFromContext() async {
let tracker = GeohashParticipantTracker()
let context = MockParticipantContext()
context.nicknameMap = ["pubkey1234": "alice"]
tracker.configure(context: context)
tracker.setActiveGeohash("abc123")
tracker.recordParticipant(pubkeyHex: "pubkey1234")
let people = tracker.getVisiblePeople()
#expect(people.count == 1)
#expect(people.first?.displayName == "alice#1234")
}
@Test func getVisiblePeople_sortedByLastSeen() async {
let tracker = GeohashParticipantTracker()
let context = MockParticipantContext()
tracker.configure(context: context)
tracker.setActiveGeohash("abc123")
tracker.recordParticipant(pubkeyHex: "older")
try? await Task.sleep(nanoseconds: 10_000_000) // 10ms
tracker.recordParticipant(pubkeyHex: "newer")
let people = tracker.getVisiblePeople()
#expect(people.count == 2)
#expect(people.first?.id == "newer")
#expect(people.last?.id == "older")
}
@Test func getVisiblePeople_emptyWhenNoActiveGeohash() async {
let tracker = GeohashParticipantTracker()
let context = MockParticipantContext()
tracker.configure(context: context)
tracker.recordParticipant(pubkeyHex: "pubkey1", geohash: "abc123")
let people = tracker.getVisiblePeople()
#expect(people.isEmpty)
}
// MARK: - Activity Cutoff Tests
@Test func participantCount_excludesExpiredEntries() async {
// Use a very short cutoff for testing
let tracker = GeohashParticipantTracker(activityCutoff: -0.05) // 50ms cutoff
let context = MockParticipantContext()
tracker.configure(context: context)
tracker.setActiveGeohash("abc123")
tracker.recordParticipant(pubkeyHex: "pubkey1")
// Should be counted immediately
#expect(tracker.participantCount(for: "abc123") == 1)
// Wait for expiry
try? await Task.sleep(nanoseconds: 100_000_000) // 100ms
// Should be expired now
#expect(tracker.participantCount(for: "abc123") == 0)
}
// MARK: - Remove Participant Tests
@Test func removeParticipant_removesFromAllGeohashes() async {
let tracker = GeohashParticipantTracker()
let context = MockParticipantContext()
tracker.configure(context: context)
tracker.recordParticipant(pubkeyHex: "pubkey1", geohash: "geo1")
tracker.recordParticipant(pubkeyHex: "pubkey1", geohash: "geo2")
tracker.recordParticipant(pubkeyHex: "pubkey2", geohash: "geo1")
tracker.removeParticipant(pubkeyHex: "pubkey1")
#expect(tracker.participantCount(for: "geo1") == 1)
#expect(tracker.participantCount(for: "geo2") == 0)
}
// MARK: - Clear Tests
@Test func clear_removesAllData() async {
let tracker = GeohashParticipantTracker()
let context = MockParticipantContext()
tracker.configure(context: context)
tracker.setActiveGeohash("abc123")
tracker.recordParticipant(pubkeyHex: "pubkey1")
tracker.recordParticipant(pubkeyHex: "pubkey2", geohash: "other")
tracker.clear()
#expect(tracker.participantCount(for: "abc123") == 0)
#expect(tracker.participantCount(for: "other") == 0)
#expect(tracker.visiblePeople.isEmpty)
}
@Test func clearGeohash_removesOnlySpecificGeohash() async {
let tracker = GeohashParticipantTracker()
let context = MockParticipantContext()
tracker.configure(context: context)
tracker.recordParticipant(pubkeyHex: "pubkey1", geohash: "geo1")
tracker.recordParticipant(pubkeyHex: "pubkey2", geohash: "geo2")
tracker.clear(geohash: "geo1")
#expect(tracker.participantCount(for: "geo1") == 0)
#expect(tracker.participantCount(for: "geo2") == 1)
}
// MARK: - Set Active Geohash Tests
@Test func setActiveGeohash_clearsVisiblePeopleWhenNil() async {
let tracker = GeohashParticipantTracker()
let context = MockParticipantContext()
tracker.configure(context: context)
tracker.setActiveGeohash("abc123")
tracker.recordParticipant(pubkeyHex: "pubkey1")
#expect(!tracker.visiblePeople.isEmpty)
tracker.setActiveGeohash(nil)
#expect(tracker.visiblePeople.isEmpty)
}
@Test func setActiveGeohash_refreshesVisiblePeople() async {
let tracker = GeohashParticipantTracker()
let context = MockParticipantContext()
tracker.configure(context: context)
// Pre-populate a geohash
tracker.recordParticipant(pubkeyHex: "pubkey1", geohash: "abc123")
// Set it as active
tracker.setActiveGeohash("abc123")
#expect(tracker.visiblePeople.count == 1)
}
// MARK: - GeoPerson Tests
@Test func geoPerson_identifiable() async {
let person1 = GeoPerson(id: "abc", displayName: "alice", lastSeen: Date())
let person2 = GeoPerson(id: "abc", displayName: "alice", lastSeen: Date())
let person3 = GeoPerson(id: "xyz", displayName: "bob", lastSeen: Date())
#expect(person1.id == person2.id)
#expect(person1.id != person3.id)
}
@Test func geoPerson_equatable() async {
let date = Date()
let person1 = GeoPerson(id: "abc", displayName: "alice", lastSeen: date)
let person2 = GeoPerson(id: "abc", displayName: "alice", lastSeen: date)
#expect(person1 == person2)
}
}
@@ -0,0 +1,604 @@
//
// MessageDeduplicationServiceTests.swift
// bitchatTests
//
// Tests for MessageDeduplicationService, LRUDeduplicationCache, and ContentNormalizer.
// This is free and unencumbered software released into the public domain.
//
import Testing
import Foundation
@testable import bitchat
// MARK: - LRU Deduplication Cache Tests
@Suite("LRU Deduplication Cache")
@MainActor
struct LRUDeduplicationCacheTests {
// MARK: - Basic Operations
@Test func emptyCache_containsReturnsFalse() {
let cache = LRUDeduplicationCache<Int>(capacity: 10)
#expect(!cache.contains("key"))
#expect(cache.value(for: "key") == nil)
#expect(cache.count == 0)
}
@Test func record_addsEntry() {
let cache = LRUDeduplicationCache<Int>(capacity: 10)
cache.record("key1", value: 42)
#expect(cache.contains("key1"))
#expect(cache.value(for: "key1") == 42)
#expect(cache.count == 1)
}
@Test func record_updatesExistingEntry() {
let cache = LRUDeduplicationCache<Int>(capacity: 10)
cache.record("key1", value: 42)
cache.record("key1", value: 100)
#expect(cache.value(for: "key1") == 100)
#expect(cache.count == 1) // Should not increase count
}
@Test func record_multipleEntries() {
let cache = LRUDeduplicationCache<String>(capacity: 10)
cache.record("a", value: "alpha")
cache.record("b", value: "beta")
cache.record("c", value: "gamma")
#expect(cache.count == 3)
#expect(cache.value(for: "a") == "alpha")
#expect(cache.value(for: "b") == "beta")
#expect(cache.value(for: "c") == "gamma")
}
@Test func remove_removesEntry() {
let cache = LRUDeduplicationCache<Int>(capacity: 10)
cache.record("key1", value: 42)
cache.record("key2", value: 100)
cache.remove("key1")
#expect(!cache.contains("key1"))
#expect(cache.contains("key2"))
}
@Test func clear_removesAllEntries() {
let cache = LRUDeduplicationCache<Int>(capacity: 10)
cache.record("a", value: 1)
cache.record("b", value: 2)
cache.record("c", value: 3)
cache.clear()
#expect(cache.count == 0)
#expect(!cache.contains("a"))
#expect(!cache.contains("b"))
#expect(!cache.contains("c"))
}
// MARK: - Eviction Tests
@Test func eviction_removesOldestWhenOverCapacity() {
let cache = LRUDeduplicationCache<Int>(capacity: 3)
cache.record("a", value: 1)
cache.record("b", value: 2)
cache.record("c", value: 3)
cache.record("d", value: 4) // Should evict "a"
#expect(cache.count == 3)
#expect(!cache.contains("a")) // Evicted
#expect(cache.contains("b"))
#expect(cache.contains("c"))
#expect(cache.contains("d"))
}
@Test func eviction_maintainsCapacity() {
let cache = LRUDeduplicationCache<Int>(capacity: 2)
for i in 0..<100 {
cache.record("key\(i)", value: i)
}
#expect(cache.count == 2)
// Most recent entries should be present
#expect(cache.contains("key99"))
#expect(cache.contains("key98"))
// Older entries should be evicted
#expect(!cache.contains("key0"))
#expect(!cache.contains("key50"))
}
@Test func eviction_capacityOfOne() {
let cache = LRUDeduplicationCache<Int>(capacity: 1)
cache.record("a", value: 1)
cache.record("b", value: 2)
#expect(cache.count == 1)
#expect(!cache.contains("a"))
#expect(cache.contains("b"))
}
@Test func eviction_skipsRemovedKeys() {
let cache = LRUDeduplicationCache<Int>(capacity: 3)
cache.record("a", value: 1)
cache.record("b", value: 2)
cache.record("c", value: 3)
// Remove "a" manually
cache.remove("a")
// Add new entry - should evict "b" (next oldest still in map)
cache.record("d", value: 4)
// Cache should have b, c, d (a was removed)
// Actually after eviction it should have c, d and maybe b depending on implementation
#expect(!cache.contains("a"))
#expect(cache.count <= 3)
}
// MARK: - Edge Cases
@Test func emptyKey_works() {
let cache = LRUDeduplicationCache<Int>(capacity: 10)
cache.record("", value: 42)
#expect(cache.contains(""))
#expect(cache.value(for: "") == 42)
}
@Test func largeCapacity_works() {
let cache = LRUDeduplicationCache<Int>(capacity: 10000)
for i in 0..<5000 {
cache.record("key\(i)", value: i)
}
#expect(cache.count == 5000)
#expect(cache.contains("key0"))
#expect(cache.contains("key4999"))
}
}
// MARK: - Content Normalizer Tests
struct ContentNormalizerTests {
@Test func normalizedKey_basicContent() {
let key1 = ContentNormalizer.normalizedKey("Hello World")
let key2 = ContentNormalizer.normalizedKey("Hello World")
#expect(key1 == key2)
}
@Test func normalizedKey_caseInsensitive() {
let key1 = ContentNormalizer.normalizedKey("Hello World")
let key2 = ContentNormalizer.normalizedKey("hello world")
let key3 = ContentNormalizer.normalizedKey("HELLO WORLD")
#expect(key1 == key2)
#expect(key2 == key3)
}
@Test func normalizedKey_whitespaceCollapsed() {
let key1 = ContentNormalizer.normalizedKey("Hello World")
let key2 = ContentNormalizer.normalizedKey("Hello World")
let key3 = ContentNormalizer.normalizedKey("Hello\t\nWorld")
#expect(key1 == key2)
#expect(key2 == key3)
}
@Test func normalizedKey_trimmed() {
let key1 = ContentNormalizer.normalizedKey("Hello")
let key2 = ContentNormalizer.normalizedKey(" Hello ")
let key3 = ContentNormalizer.normalizedKey("\nHello\n")
#expect(key1 == key2)
#expect(key2 == key3)
}
@Test func normalizedKey_urlQueryStripped() {
let key1 = ContentNormalizer.normalizedKey("Check https://example.com/page")
let key2 = ContentNormalizer.normalizedKey("Check https://example.com/page?query=value")
let key3 = ContentNormalizer.normalizedKey("Check https://example.com/page#anchor")
#expect(key1 == key2)
#expect(key2 == key3)
}
@Test func normalizedKey_httpAndHttpsDistinct() {
// URL scheme is preserved
let key1 = ContentNormalizer.normalizedKey("http://example.com/page")
let key2 = ContentNormalizer.normalizedKey("https://example.com/page")
#expect(key1 != key2)
}
@Test func normalizedKey_differentContent() {
let key1 = ContentNormalizer.normalizedKey("Hello")
let key2 = ContentNormalizer.normalizedKey("Goodbye")
#expect(key1 != key2)
}
@Test func normalizedKey_returnsHashFormat() {
let key = ContentNormalizer.normalizedKey("Test content")
#expect(key.hasPrefix("h:"))
#expect(key.count == 18) // "h:" + 16 hex chars
}
@Test func normalizedKey_emptyContent() {
let key = ContentNormalizer.normalizedKey("")
#expect(key.hasPrefix("h:"))
}
@Test func normalizedKey_longContentTruncated() {
let longContent = String(repeating: "a", count: 10000)
let key1 = ContentNormalizer.normalizedKey(longContent)
let key2 = ContentNormalizer.normalizedKey(longContent + "extra")
// Both should be the same since content is truncated before hashing
#expect(key1 == key2)
}
@Test func normalizedKey_prefixLengthRespected() {
let content = "Short"
let key1 = ContentNormalizer.normalizedKey(content, prefixLength: 3)
let key2 = ContentNormalizer.normalizedKey(content, prefixLength: 100)
// Different prefix lengths may produce different keys
// "sho" vs "short"
#expect(key1 != key2)
}
@Test func normalizedKey_urlsInMiddleOfContent() {
let content1 = "Check out https://example.com/path?query=1 for more info"
let content2 = "Check out https://example.com/path for more info"
let key1 = ContentNormalizer.normalizedKey(content1)
let key2 = ContentNormalizer.normalizedKey(content2)
#expect(key1 == key2)
}
@Test func normalizedKey_multipleUrls() {
let content1 = "Links: https://a.com?x=1 and http://b.com#y"
let content2 = "Links: https://a.com and http://b.com"
let key1 = ContentNormalizer.normalizedKey(content1)
let key2 = ContentNormalizer.normalizedKey(content2)
#expect(key1 == key2)
}
}
// MARK: - Message Deduplication Service Tests
@Suite("Message Deduplication Service")
@MainActor
struct MessageDeduplicationServiceTests {
// MARK: - Content Deduplication
@Test func recordContent_storesTimestamp() {
let service = MessageDeduplicationService(contentCapacity: 100, nostrEventCapacity: 100)
let now = Date()
service.recordContent("Hello World", timestamp: now)
let retrieved = service.contentTimestamp(for: "Hello World")
#expect(retrieved == now)
}
@Test func recordContent_updatesTimestamp() {
let service = MessageDeduplicationService(contentCapacity: 100, nostrEventCapacity: 100)
let early = Date(timeIntervalSince1970: 1000)
let late = Date(timeIntervalSince1970: 2000)
service.recordContent("Hello World", timestamp: early)
service.recordContent("Hello World", timestamp: late)
let retrieved = service.contentTimestamp(for: "Hello World")
#expect(retrieved == late)
}
@Test func contentTimestamp_nilForUnseen() {
let service = MessageDeduplicationService(contentCapacity: 100, nostrEventCapacity: 100)
let timestamp = service.contentTimestamp(for: "Never seen")
#expect(timestamp == nil)
}
@Test func recordContentKey_directKeyAccess() {
let service = MessageDeduplicationService(contentCapacity: 100, nostrEventCapacity: 100)
let now = Date()
let key = service.normalizedContentKey("Test")
service.recordContentKey(key, timestamp: now)
#expect(service.contentTimestamp(forKey: key) == now)
}
@Test func normalizedContentKey_consistentWithNormalizer() {
let service = MessageDeduplicationService(contentCapacity: 100, nostrEventCapacity: 100)
let content = "Hello World"
let serviceKey = service.normalizedContentKey(content)
let normalizerKey = ContentNormalizer.normalizedKey(content)
#expect(serviceKey == normalizerKey)
}
// MARK: - Nostr Event Deduplication
@Test func recordNostrEvent_marksAsProcessed() {
let service = MessageDeduplicationService(contentCapacity: 100, nostrEventCapacity: 100)
#expect(!service.hasProcessedNostrEvent("event123"))
service.recordNostrEvent("event123")
#expect(service.hasProcessedNostrEvent("event123"))
}
@Test func hasProcessedNostrEvent_falseForUnseen() {
let service = MessageDeduplicationService(contentCapacity: 100, nostrEventCapacity: 100)
#expect(!service.hasProcessedNostrEvent("never-seen"))
}
@Test func nostrEvent_multipleEvents() {
let service = MessageDeduplicationService(contentCapacity: 100, nostrEventCapacity: 100)
service.recordNostrEvent("event1")
service.recordNostrEvent("event2")
service.recordNostrEvent("event3")
#expect(service.hasProcessedNostrEvent("event1"))
#expect(service.hasProcessedNostrEvent("event2"))
#expect(service.hasProcessedNostrEvent("event3"))
#expect(!service.hasProcessedNostrEvent("event4"))
}
// MARK: - Nostr ACK Deduplication
@Test func recordNostrAck_marksAsProcessed() {
let service = MessageDeduplicationService(contentCapacity: 100, nostrEventCapacity: 100)
let ackKey = MessageDeduplicationService.ackKey(
messageId: "msg123",
ackType: "delivered",
senderPubkey: "pubkey456"
)
#expect(!service.hasProcessedNostrAck(ackKey))
service.recordNostrAck(ackKey)
#expect(service.hasProcessedNostrAck(ackKey))
}
@Test func ackKey_format() {
let key = MessageDeduplicationService.ackKey(
messageId: "msg",
ackType: "read",
senderPubkey: "pub"
)
#expect(key == "msg:read:pub")
}
@Test func ackKey_differentComponents() {
let key1 = MessageDeduplicationService.ackKey(messageId: "a", ackType: "delivered", senderPubkey: "x")
let key2 = MessageDeduplicationService.ackKey(messageId: "a", ackType: "read", senderPubkey: "x")
let key3 = MessageDeduplicationService.ackKey(messageId: "b", ackType: "delivered", senderPubkey: "x")
#expect(key1 != key2) // Different ackType
#expect(key1 != key3) // Different messageId
}
// MARK: - Clear Operations
@Test func clearAll_clearsEverything() {
let service = MessageDeduplicationService(contentCapacity: 100, nostrEventCapacity: 100)
let now = Date()
service.recordContent("Hello", timestamp: now)
service.recordNostrEvent("event1")
service.recordNostrAck("ack1")
service.clearAll()
#expect(service.contentTimestamp(for: "Hello") == nil)
#expect(!service.hasProcessedNostrEvent("event1"))
#expect(!service.hasProcessedNostrAck("ack1"))
}
@Test func clearNostrCaches_preservesContent() {
let service = MessageDeduplicationService(contentCapacity: 100, nostrEventCapacity: 100)
let now = Date()
service.recordContent("Hello", timestamp: now)
service.recordNostrEvent("event1")
service.recordNostrAck("ack1")
service.clearNostrCaches()
#expect(service.contentTimestamp(for: "Hello") == now) // Preserved
#expect(!service.hasProcessedNostrEvent("event1")) // Cleared
#expect(!service.hasProcessedNostrAck("ack1")) // Cleared
}
// MARK: - Capacity Tests
@Test func contentCache_respectsCapacity() {
let service = MessageDeduplicationService(contentCapacity: 3, nostrEventCapacity: 100)
service.recordContent("a", timestamp: Date())
service.recordContent("b", timestamp: Date())
service.recordContent("c", timestamp: Date())
service.recordContent("d", timestamp: Date())
// "a" should have been evicted
#expect(service.contentTimestamp(for: "a") == nil)
#expect(service.contentTimestamp(for: "d") != nil)
}
@Test func nostrEventCache_respectsCapacity() {
let service = MessageDeduplicationService(contentCapacity: 100, nostrEventCapacity: 3)
service.recordNostrEvent("e1")
service.recordNostrEvent("e2")
service.recordNostrEvent("e3")
service.recordNostrEvent("e4")
// "e1" should have been evicted
#expect(!service.hasProcessedNostrEvent("e1"))
#expect(service.hasProcessedNostrEvent("e4"))
}
// MARK: - Integration Tests
@Test func realWorldDeduplication_similarMessages() {
let service = MessageDeduplicationService(contentCapacity: 100, nostrEventCapacity: 100)
let now = Date()
// Record original message
service.recordContent("Check out https://example.com/page?ref=abc", timestamp: now)
// Same URL with different query params should match
let timestamp = service.contentTimestamp(for: "Check out https://example.com/page?ref=xyz")
#expect(timestamp == now)
}
@Test func realWorldDeduplication_caseVariations() {
let service = MessageDeduplicationService(contentCapacity: 100, nostrEventCapacity: 100)
let now = Date()
service.recordContent("HELLO WORLD", timestamp: now)
#expect(service.contentTimestamp(for: "hello world") == now)
#expect(service.contentTimestamp(for: "Hello World") == now)
}
// MARK: - Thread Safety Tests (via @MainActor enforcement)
@Test("Concurrent content recording is safe via MainActor")
func concurrentContentRecording() async {
let service = MessageDeduplicationService(contentCapacity: 1000, nostrEventCapacity: 1000)
let iterations = 100
// All operations run on MainActor due to @MainActor annotation
// This test verifies the pattern works correctly
await withTaskGroup(of: Void.self) { group in
for i in 0..<iterations {
group.addTask { @MainActor in
service.recordContent("Message \(i)", timestamp: Date())
}
}
}
// Verify some entries were recorded
#expect(service.contentTimestamp(for: "Message 0") != nil)
#expect(service.contentTimestamp(for: "Message 99") != nil)
}
@Test("Concurrent Nostr event recording is safe via MainActor")
func concurrentNostrEventRecording() async {
let service = MessageDeduplicationService(contentCapacity: 1000, nostrEventCapacity: 1000)
let iterations = 100
await withTaskGroup(of: Void.self) { group in
for i in 0..<iterations {
group.addTask { @MainActor in
service.recordNostrEvent("event_\(i)")
}
}
}
// Verify events were recorded
#expect(service.hasProcessedNostrEvent("event_0"))
#expect(service.hasProcessedNostrEvent("event_99"))
}
@Test("Mixed concurrent operations are safe via MainActor")
func concurrentMixedOperations() async {
let service = MessageDeduplicationService(contentCapacity: 1000, nostrEventCapacity: 1000)
let iterations = 50
await withTaskGroup(of: Void.self) { group in
// Content recording tasks
for i in 0..<iterations {
group.addTask { @MainActor in
service.recordContent("Content \(i)", timestamp: Date())
}
}
// Event recording tasks
for i in 0..<iterations {
group.addTask { @MainActor in
service.recordNostrEvent("event_\(i)")
}
}
// ACK recording tasks
for i in 0..<iterations {
group.addTask { @MainActor in
service.recordNostrAck("ack_\(i)")
}
}
// Read tasks
for i in 0..<iterations {
group.addTask { @MainActor in
_ = service.contentTimestamp(for: "Content \(i)")
_ = service.hasProcessedNostrEvent("event_\(i)")
_ = service.hasProcessedNostrAck("ack_\(i)")
}
}
}
// If we reach here without crashes, the test passes
}
}
// MARK: - LRU Cache Thread Safety Tests
@Suite("LRU Cache Thread Safety")
@MainActor
struct LRUCacheThreadSafetyTests {
@Test("Concurrent cache access is safe via MainActor")
func concurrentCacheAccess() async {
let cache = LRUDeduplicationCache<Int>(capacity: 500)
let iterations = 100
await withTaskGroup(of: Void.self) { group in
// Write tasks
for i in 0..<iterations {
group.addTask { @MainActor in
cache.record("key_\(i)", value: i)
}
}
// Read tasks
for i in 0..<iterations {
group.addTask { @MainActor in
_ = cache.contains("key_\(i)")
_ = cache.value(for: "key_\(i)")
}
}
}
// Verify cache is in consistent state
#expect(cache.count <= 500) // Respects capacity
}
@Test("Cache eviction under concurrent load is safe")
func cacheEvictionUnderLoad() async {
let cache = LRUDeduplicationCache<Int>(capacity: 10)
let iterations = 100
await withTaskGroup(of: Void.self) { group in
for i in 0..<iterations {
group.addTask { @MainActor in
cache.record("key_\(i)", value: i)
}
}
}
// Cache should maintain its capacity constraint
#expect(cache.count == 10)
}
}
@@ -0,0 +1,223 @@
//
// MessageFormattingEngineTests.swift
// bitchatTests
//
// Tests for MessageFormattingEngine regex patterns and utility functions.
// This is free and unencumbered software released into the public domain.
//
import Testing
import Foundation
import SwiftUI
@testable import bitchat
struct MessageFormattingEngineTests {
// MARK: - Mention Extraction Tests
@Test func extractMentions_singleMention() {
let content = "Hello @alice how are you?"
let mentions = MessageFormattingEngine.extractMentions(from: content)
#expect(mentions == ["alice"])
}
@Test func extractMentions_multipleMentions() {
let content = "@alice and @bob are chatting with @charlie"
let mentions = MessageFormattingEngine.extractMentions(from: content)
#expect(mentions.count == 3)
#expect(mentions.contains("alice"))
#expect(mentions.contains("bob"))
#expect(mentions.contains("charlie"))
}
@Test func extractMentions_mentionWithSuffix() {
let content = "Hey @alice#a1b2 check this out"
let mentions = MessageFormattingEngine.extractMentions(from: content)
#expect(mentions == ["alice#a1b2"])
}
@Test func extractMentions_noMentions() {
let content = "Just a regular message with no mentions"
let mentions = MessageFormattingEngine.extractMentions(from: content)
#expect(mentions.isEmpty)
}
@Test func extractMentions_unicodeNickname() {
let content = "Hello @日本語 and @émile"
let mentions = MessageFormattingEngine.extractMentions(from: content)
#expect(mentions.count == 2)
#expect(mentions.contains("日本語"))
#expect(mentions.contains("émile"))
}
@Test func extractMentions_mentionWithUnderscore() {
let content = "Thanks @user_name_123"
let mentions = MessageFormattingEngine.extractMentions(from: content)
#expect(mentions == ["user_name_123"])
}
@Test func extractMentions_emailNotCaptured() {
// Email addresses should not be captured as mentions
let content = "Contact me at test@example.com"
let mentions = MessageFormattingEngine.extractMentions(from: content)
// The regex will capture "example" after @ in email - this is expected behavior
// as the regex doesn't distinguish email addresses
#expect(mentions.count == 1)
}
// MARK: - Cashu Token Detection Tests
@Test func containsCashuToken_validTokenA() {
let content = "Here's a token: cashuAeyJwcm9vZnMiOiJIZWxsbyBXb3JsZCEgVGhpcyBpcyBhIHRlc3QgdG9rZW4i"
#expect(MessageFormattingEngine.containsCashuToken(content))
}
@Test func containsCashuToken_validTokenB() {
let content = "Payment: cashuBeyJwcm9vZnMiOiJIZWxsbyBXb3JsZCEgVGhpcyBpcyBhIHRlc3QgdG9rZW4i"
#expect(MessageFormattingEngine.containsCashuToken(content))
}
@Test func containsCashuToken_noToken() {
let content = "Just a regular message about cashews"
#expect(!MessageFormattingEngine.containsCashuToken(content))
}
@Test func containsCashuToken_tooShort() {
let content = "Invalid: cashuAshort"
#expect(!MessageFormattingEngine.containsCashuToken(content))
}
// MARK: - Regex Pattern Tests
@Test func hashtagPattern_standaloneHashtag() {
let content = "#bitcoin is great"
let nsContent = content as NSString
let range = NSRange(location: 0, length: nsContent.length)
let matches = MessageFormattingEngine.Patterns.hashtag.matches(in: content, options: [], range: range)
#expect(matches.count == 1)
}
@Test func hashtagPattern_multipleHashtags() {
let content = "#bitcoin #lightning #nostr"
let nsContent = content as NSString
let range = NSRange(location: 0, length: nsContent.length)
let matches = MessageFormattingEngine.Patterns.hashtag.matches(in: content, options: [], range: range)
#expect(matches.count == 3)
}
@Test func hashtagPattern_hashInMiddleOfWord() {
let content = "test#notahashtag"
let nsContent = content as NSString
let range = NSRange(location: 0, length: nsContent.length)
let matches = MessageFormattingEngine.Patterns.hashtag.matches(in: content, options: [], range: range)
// This will match because the regex doesn't check for word boundaries
#expect(matches.count == 1)
}
@Test func bolt11Pattern_mainnet() {
let content = "Pay this: lnbc10u1pjexampleinvoice0000000000000000000000000000000000000000000"
let nsContent = content as NSString
let range = NSRange(location: 0, length: nsContent.length)
let matches = MessageFormattingEngine.Patterns.bolt11.matches(in: content, options: [], range: range)
#expect(matches.count == 1)
}
@Test func bolt11Pattern_testnet() {
let content = "Test: lntb10u1pjexampleinvoice0000000000000000000000000000000000000000000"
let nsContent = content as NSString
let range = NSRange(location: 0, length: nsContent.length)
let matches = MessageFormattingEngine.Patterns.bolt11.matches(in: content, options: [], range: range)
#expect(matches.count == 1)
}
@Test func lnurlPattern_valid() {
let content = "LNURL: lnurl1dp68gurn8ghj7um9wfmxjcm99e3k7mf0v9cxj0m385ekvcenxc6r2c35xvukxefcv5mkvv34x5ekzd3ev56nyd3hxqurzepexejxxepnxscrvwfnv9nxzcn9xq6xyefhvgcxxcmyxymnserx"
let nsContent = content as NSString
let range = NSRange(location: 0, length: nsContent.length)
let matches = MessageFormattingEngine.Patterns.lnurl.matches(in: content, options: [], range: range)
#expect(matches.count == 1)
}
@Test func lightningSchemePattern_valid() {
let content = "Click: lightning:lnbc10u1example"
let nsContent = content as NSString
let range = NSRange(location: 0, length: nsContent.length)
let matches = MessageFormattingEngine.Patterns.lightningScheme.matches(in: content, options: [], range: range)
#expect(matches.count == 1)
}
@Test func cashuPattern_valid() {
let content = "Token: cashuAeyJwcm9vZnMiOlt7ImlkIjoiMDAwMDAwMDAwMDAwMDAwMCJ9XX0="
let nsContent = content as NSString
let range = NSRange(location: 0, length: nsContent.length)
let matches = MessageFormattingEngine.Patterns.cashu.matches(in: content, options: [], range: range)
#expect(matches.count == 1)
}
// MARK: - URL Detection Tests
@Test func linkDetector_httpURL() {
let content = "Check out http://example.com"
let nsContent = content as NSString
let range = NSRange(location: 0, length: nsContent.length)
let matches = MessageFormattingEngine.Patterns.linkDetector?.matches(in: content, options: [], range: range) ?? []
#expect(matches.count == 1)
}
@Test func linkDetector_httpsURL() {
let content = "Visit https://example.com/path?query=value"
let nsContent = content as NSString
let range = NSRange(location: 0, length: nsContent.length)
let matches = MessageFormattingEngine.Patterns.linkDetector?.matches(in: content, options: [], range: range) ?? []
#expect(matches.count == 1)
}
@Test func linkDetector_multipleURLs() {
let content = "See https://a.com and http://b.com"
let nsContent = content as NSString
let range = NSRange(location: 0, length: nsContent.length)
let matches = MessageFormattingEngine.Patterns.linkDetector?.matches(in: content, options: [], range: range) ?? []
#expect(matches.count == 2)
}
// MARK: - String Extension Tests
@Test func splitSuffix_withSuffix() {
let name = "alice#a1b2"
let (base, suffix) = name.splitSuffix()
#expect(base == "alice")
#expect(suffix == "#a1b2")
}
@Test func splitSuffix_withoutSuffix() {
let name = "alice"
let (base, suffix) = name.splitSuffix()
#expect(base == "alice")
#expect(suffix == "")
}
@Test func splitSuffix_withAtPrefix() {
let name = "@alice#a1b2"
let (base, suffix) = name.splitSuffix()
#expect(base == "alice")
#expect(suffix == "#a1b2")
}
@Test func hasVeryLongToken_noLongToken() {
let content = "Short words only here"
#expect(!content.hasVeryLongToken(threshold: 50))
}
@Test func hasVeryLongToken_withLongToken() {
let longToken = String(repeating: "a", count: 100)
let content = "Here is a \(longToken) token"
#expect(content.hasVeryLongToken(threshold: 50))
}
@Test func hasVeryLongToken_exactThreshold() {
let exactToken = String(repeating: "a", count: 50)
let content = "Token: \(exactToken)"
// Exactly at threshold DOES trigger (uses >= comparison)
#expect(content.hasVeryLongToken(threshold: 50))
}
}
+19 -5
View File
@@ -11,6 +11,8 @@ import Foundation
final class MockIdentityManager: SecureIdentityStateManagerProtocol { final class MockIdentityManager: SecureIdentityStateManagerProtocol {
private let keychain: KeychainManagerProtocol private let keychain: KeychainManagerProtocol
private var blockedFingerprints: Set<String> = []
private var blockedNostrPubkeys: Set<String> = []
init(_ keychain: KeychainManagerProtocol) { init(_ keychain: KeychainManagerProtocol) {
self.keychain = keychain self.keychain = keychain
@@ -45,19 +47,31 @@ final class MockIdentityManager: SecureIdentityStateManagerProtocol {
} }
func isBlocked(fingerprint: String) -> Bool { func isBlocked(fingerprint: String) -> Bool {
false blockedFingerprints.contains(fingerprint)
} }
func setBlocked(_ fingerprint: String, isBlocked: Bool) {} func setBlocked(_ fingerprint: String, isBlocked: Bool) {
if isBlocked {
blockedFingerprints.insert(fingerprint)
} else {
blockedFingerprints.remove(fingerprint)
}
}
func isNostrBlocked(pubkeyHexLowercased: String) -> Bool { func isNostrBlocked(pubkeyHexLowercased: String) -> Bool {
true blockedNostrPubkeys.contains(pubkeyHexLowercased)
} }
func setNostrBlocked(_ pubkeyHexLowercased: String, isBlocked: Bool) {} func setNostrBlocked(_ pubkeyHexLowercased: String, isBlocked: Bool) {
if isBlocked {
blockedNostrPubkeys.insert(pubkeyHexLowercased)
} else {
blockedNostrPubkeys.remove(pubkeyHexLowercased)
}
}
func getBlockedNostrPubkeys() -> Set<String> { func getBlockedNostrPubkeys() -> Set<String> {
Set() blockedNostrPubkeys
} }
func registerEphemeralSession(peerID: PeerID, handshakeState: HandshakeState) {} func registerEphemeralSession(peerID: PeerID, handshakeState: HandshakeState) {}
+99 -9
View File
@@ -11,6 +11,7 @@ import Foundation
final class MockKeychain: KeychainManagerProtocol { final class MockKeychain: KeychainManagerProtocol {
private var storage: [String: Data] = [:] private var storage: [String: Data] = [:]
private var serviceStorage: [String: [String: Data]] = [:]
func saveIdentityKey(_ keyData: Data, forKey key: String) -> Bool { func saveIdentityKey(_ keyData: Data, forKey key: String) -> Bool {
storage[key] = keyData storage[key] = keyData
@@ -28,11 +29,11 @@ final class MockKeychain: KeychainManagerProtocol {
func deleteAllKeychainData() -> Bool { func deleteAllKeychainData() -> Bool {
storage.removeAll() storage.removeAll()
serviceStorage.removeAll()
return true return true
} }
func secureClear(_ data: inout Data) { func secureClear(_ data: inout Data) {
//
data = Data() data = Data()
} }
@@ -43,22 +44,111 @@ final class MockKeychain: KeychainManagerProtocol {
func verifyIdentityKeyExists() -> Bool { func verifyIdentityKeyExists() -> Bool {
storage["identity_noiseStaticKey"] != nil storage["identity_noiseStaticKey"] != nil
} }
}
final class MockKeychainHelper: KeychainHelperProtocol { // MARK: - Generic Data Storage (consolidated from KeychainHelper)
private typealias Service = String
private typealias Key = String
private var storage: [Service: [Key: Data]] = [:]
func save(key: String, data: Data, service: String, accessible: CFString?) { func save(key: String, data: Data, service: String, accessible: CFString?) {
storage[service]?[key] = data if serviceStorage[service] == nil {
serviceStorage[service] = [:]
}
serviceStorage[service]?[key] = data
} }
func load(key: String, service: String) -> Data? { func load(key: String, service: String) -> Data? {
storage[service]?[key] serviceStorage[service]?[key]
} }
func delete(key: String, service: String) { func delete(key: String, service: String) {
storage[service]?.removeValue(forKey: key) serviceStorage[service]?.removeValue(forKey: key)
}
}
/// Typealias for backwards compatibility with tests using MockKeychainHelper
typealias MockKeychainHelper = MockKeychain
/// Mock keychain that tracks secureClear calls for testing DH secret clearing
final class TrackingMockKeychain: KeychainManagerProtocol {
private var storage: [String: Data] = [:]
private var serviceStorage: [String: [String: Data]] = [:]
/// Thread-safe counter for secureClear calls
private let lock = NSLock()
private var _secureClearDataCallCount = 0
private var _secureClearStringCallCount = 0
var secureClearDataCallCount: Int {
lock.lock()
defer { lock.unlock() }
return _secureClearDataCallCount
}
var secureClearStringCallCount: Int {
lock.lock()
defer { lock.unlock() }
return _secureClearStringCallCount
}
var totalSecureClearCallCount: Int {
return secureClearDataCallCount + secureClearStringCallCount
}
func resetCounts() {
lock.lock()
defer { lock.unlock() }
_secureClearDataCallCount = 0
_secureClearStringCallCount = 0
}
func saveIdentityKey(_ keyData: Data, forKey key: String) -> Bool {
storage[key] = keyData
return true
}
func getIdentityKey(forKey key: String) -> Data? {
storage[key]
}
func deleteIdentityKey(forKey key: String) -> Bool {
storage.removeValue(forKey: key)
return true
}
func deleteAllKeychainData() -> Bool {
storage.removeAll()
serviceStorage.removeAll()
return true
}
func secureClear(_ data: inout Data) {
lock.lock()
_secureClearDataCallCount += 1
lock.unlock()
data = Data()
}
func secureClear(_ string: inout String) {
lock.lock()
_secureClearStringCallCount += 1
lock.unlock()
string = ""
}
func verifyIdentityKeyExists() -> Bool {
storage["identity_noiseStaticKey"] != nil
}
func save(key: String, data: Data, service: String, accessible: CFString?) {
if serviceStorage[service] == nil {
serviceStorage[service] = [:]
}
serviceStorage[service]?[key] = data
}
func load(key: String, service: String) -> Data? {
serviceStorage[service]?[key]
}
func delete(key: String, service: String) {
serviceStorage[service]?.removeValue(forKey: key)
} }
} }
+228
View File
@@ -0,0 +1,228 @@
//
// MockTransport.swift
// bitchatTests
//
// Mock Transport implementation for unit testing ChatViewModel.
// This is free and unencumbered software released into the public domain.
//
import Foundation
import Combine
import CoreBluetooth
@testable import bitchat
/// Mock Transport implementation for testing ChatViewModel in isolation.
/// Records all method calls and allows test code to verify interactions.
final class MockTransport: Transport {
// MARK: - Protocol Properties
weak var delegate: BitchatDelegate?
weak var peerEventsDelegate: TransportPeerEventsDelegate?
var myPeerID: PeerID = PeerID(str: "TESTPEER")
var myNickname: String = "TestUser"
private let peerSnapshotSubject = CurrentValueSubject<[TransportPeerSnapshot], Never>([])
var peerSnapshotPublisher: AnyPublisher<[TransportPeerSnapshot], Never> {
peerSnapshotSubject.eraseToAnyPublisher()
}
// MARK: - Recording Properties (for test assertions)
private(set) var sentMessages: [(content: String, mentions: [String], messageID: String?, timestamp: Date?)] = []
private(set) var sentPrivateMessages: [(content: String, peerID: PeerID, recipientNickname: String, messageID: String)] = []
private(set) var sentReadReceipts: [(receipt: ReadReceipt, peerID: PeerID)] = []
private(set) var sentDeliveryAcks: [(messageID: String, peerID: PeerID)] = []
private(set) var sentFavoriteNotifications: [(peerID: PeerID, isFavorite: Bool)] = []
private(set) var sentVerifyChallenges: [(peerID: PeerID, noiseKeyHex: String, nonceA: Data)] = []
private(set) var sentVerifyResponses: [(peerID: PeerID, noiseKeyHex: String, nonceA: Data)] = []
private(set) var startServicesCallCount = 0
private(set) var stopServicesCallCount = 0
private(set) var emergencyDisconnectCallCount = 0
private(set) var broadcastAnnounceCallCount = 0
private(set) var triggeredHandshakes: [PeerID] = []
// MARK: - Configurable Mock State
var connectedPeers: Set<PeerID> = []
var reachablePeers: Set<PeerID> = []
var peerNicknames: [PeerID: String] = [:]
var peerFingerprints: [PeerID: String] = [:]
var peerNoiseStates: [PeerID: LazyHandshakeState] = [:]
private let mockKeychain = MockKeychain()
// MARK: - Transport Protocol Implementation
func currentPeerSnapshots() -> [TransportPeerSnapshot] {
peerSnapshotSubject.value
}
func setNickname(_ nickname: String) {
myNickname = nickname
}
func startServices() {
startServicesCallCount += 1
}
func stopServices() {
stopServicesCallCount += 1
}
func emergencyDisconnectAll() {
emergencyDisconnectCallCount += 1
connectedPeers.removeAll()
reachablePeers.removeAll()
}
func isPeerConnected(_ peerID: PeerID) -> Bool {
connectedPeers.contains(peerID)
}
func isPeerReachable(_ peerID: PeerID) -> Bool {
reachablePeers.contains(peerID) || connectedPeers.contains(peerID)
}
func peerNickname(peerID: PeerID) -> String? {
peerNicknames[peerID]
}
func getPeerNicknames() -> [PeerID: String] {
peerNicknames
}
func getFingerprint(for peerID: PeerID) -> String? {
peerFingerprints[peerID]
}
func getNoiseSessionState(for peerID: PeerID) -> LazyHandshakeState {
peerNoiseStates[peerID] ?? .none
}
func triggerHandshake(with peerID: PeerID) {
triggeredHandshakes.append(peerID)
}
func getNoiseService() -> NoiseEncryptionService {
NoiseEncryptionService(keychain: mockKeychain)
}
// MARK: - Messaging
func sendMessage(_ content: String, mentions: [String]) {
sentMessages.append((content, mentions, nil, nil))
}
func sendMessage(_ content: String, mentions: [String], messageID: String, timestamp: Date) {
sentMessages.append((content, mentions, messageID, timestamp))
}
func sendPrivateMessage(_ content: String, to peerID: PeerID, recipientNickname: String, messageID: String) {
sentPrivateMessages.append((content, peerID, recipientNickname, messageID))
}
func sendReadReceipt(_ receipt: ReadReceipt, to peerID: PeerID) {
sentReadReceipts.append((receipt, peerID))
}
func sendFavoriteNotification(to peerID: PeerID, isFavorite: Bool) {
sentFavoriteNotifications.append((peerID, isFavorite))
}
func sendBroadcastAnnounce() {
broadcastAnnounceCallCount += 1
}
func sendDeliveryAck(for messageID: String, to peerID: PeerID) {
sentDeliveryAcks.append((messageID, peerID))
}
func sendFileBroadcast(_ packet: BitchatFilePacket, transferId: String) {
// Not tracked for current tests
}
func sendFilePrivate(_ packet: BitchatFilePacket, to peerID: PeerID, transferId: String) {
// Not tracked for current tests
}
func cancelTransfer(_ transferId: String) {
// Not tracked for current tests
}
func sendVerifyChallenge(to peerID: PeerID, noiseKeyHex: String, nonceA: Data) {
sentVerifyChallenges.append((peerID, noiseKeyHex, nonceA))
}
func sendVerifyResponse(to peerID: PeerID, noiseKeyHex: String, nonceA: Data) {
sentVerifyResponses.append((peerID, noiseKeyHex, nonceA))
}
// MARK: - Test Helpers
/// Clears all recorded method calls for fresh assertions
func resetRecordings() {
sentMessages.removeAll()
sentPrivateMessages.removeAll()
sentReadReceipts.removeAll()
sentDeliveryAcks.removeAll()
sentFavoriteNotifications.removeAll()
sentVerifyChallenges.removeAll()
sentVerifyResponses.removeAll()
startServicesCallCount = 0
stopServicesCallCount = 0
emergencyDisconnectCallCount = 0
broadcastAnnounceCallCount = 0
triggeredHandshakes.removeAll()
}
/// Simulates a peer connecting
func simulateConnect(_ peerID: PeerID, nickname: String? = nil) {
connectedPeers.insert(peerID)
if let nickname = nickname {
peerNicknames[peerID] = nickname
}
delegate?.didConnectToPeer(peerID)
delegate?.didUpdatePeerList(Array(connectedPeers))
}
/// Simulates a peer disconnecting
func simulateDisconnect(_ peerID: PeerID) {
connectedPeers.remove(peerID)
peerNicknames.removeValue(forKey: peerID)
delegate?.didDisconnectFromPeer(peerID)
delegate?.didUpdatePeerList(Array(connectedPeers))
}
/// Simulates receiving a message
func simulateIncomingMessage(_ message: BitchatMessage) {
delegate?.didReceiveMessage(message)
}
/// Simulates receiving a public message
func simulateIncomingPublicMessage(
from peerID: PeerID,
nickname: String,
content: String,
timestamp: Date = Date(),
messageID: String? = nil
) {
delegate?.didReceivePublicMessage(
from: peerID,
nickname: nickname,
content: content,
timestamp: timestamp,
messageID: messageID
)
}
/// Simulates Bluetooth state change
func simulateBluetoothStateChange(_ state: CBManagerState) {
delegate?.didUpdateBluetoothState(state)
}
/// Updates the peer snapshot publisher
func updatePeerSnapshots(_ snapshots: [TransportPeerSnapshot]) {
peerSnapshotSubject.send(snapshots)
}
}
+436 -21
View File
@@ -6,11 +6,53 @@
// For more information, see <https://unlicense.org> // For more information, see <https://unlicense.org>
// //
import Testing
import CryptoKit import CryptoKit
import Foundation import Foundation
import Testing
@testable import bitchat @testable import bitchat
// MARK: - Test Vector Support
struct NoiseTestVector: Codable {
let protocol_name: String
let init_prologue: String
let init_static: String
let init_ephemeral: String
let init_psks: [String]?
let resp_prologue: String
let resp_static: String
let resp_ephemeral: String
let resp_psks: [String]?
let handshake_hash: String?
let messages: [TestMessage]
struct TestMessage: Codable {
let payload: String
let ciphertext: String
}
}
extension Data {
init?(hex: String) {
let cleaned = hex.replacingOccurrences(of: " ", with: "")
guard cleaned.count % 2 == 0 else { return nil }
var data = Data(capacity: cleaned.count / 2)
var index = cleaned.startIndex
while index < cleaned.endIndex {
let nextIndex = cleaned.index(index, offsetBy: 2)
guard let byte = UInt8(cleaned[index..<nextIndex], radix: 16) else { return nil }
data.append(byte)
index = nextIndex
}
self = data
}
func hexString() -> String {
map { String(format: "%02x", $0) }.joined()
}
}
struct NoiseProtocolTests { struct NoiseProtocolTests {
private let aliceKey = Curve25519.KeyAgreement.PrivateKey() private let aliceKey = Curve25519.KeyAgreement.PrivateKey()
@@ -69,8 +111,12 @@ struct NoiseProtocolTests {
#expect(bobSession.isEstablished()) #expect(bobSession.isEstablished())
// Verify they have each other's static keys // Verify they have each other's static keys
#expect(aliceSession.getRemoteStaticPublicKey()?.rawRepresentation == bobKey.publicKey.rawRepresentation) #expect(
#expect(bobSession.getRemoteStaticPublicKey()?.rawRepresentation == aliceKey.publicKey.rawRepresentation) aliceSession.getRemoteStaticPublicKey()?.rawRepresentation
== bobKey.publicKey.rawRepresentation)
#expect(
bobSession.getRemoteStaticPublicKey()?.rawRepresentation
== aliceKey.publicKey.rawRepresentation)
} }
@Test func handshakeStateValidation() throws { @Test func handshakeStateValidation() throws {
@@ -190,11 +236,13 @@ struct NoiseProtocolTests {
#expect(message2 != nil) #expect(message2 != nil)
// Continue handshake // Continue handshake
let message3 = try aliceManager.handleIncomingHandshake(from: alicePeerID, message: message2!) let message3 = try aliceManager.handleIncomingHandshake(
from: alicePeerID, message: message2!)
#expect(message3 != nil) #expect(message3 != nil)
// Complete handshake // Complete handshake
let finalMessage = try bobManager.handleIncomingHandshake(from: bobPeerID, message: message3!) let finalMessage = try bobManager.handleIncomingHandshake(
from: bobPeerID, message: message3!)
#expect(finalMessage == nil) #expect(finalMessage == nil)
// Both should have established sessions // Both should have established sessions
@@ -258,11 +306,19 @@ struct NoiseProtocolTests {
@Test func sessionIsolation() throws { @Test func sessionIsolation() throws {
// Create two separate session pairs // Create two separate session pairs
let aliceSession1 = NoiseSession(peerID: PeerID(str: "peer1"), role: .initiator, keychain: mockKeychain, localStaticKey: aliceKey) let aliceSession1 = NoiseSession(
let bobSession1 = NoiseSession(peerID: PeerID(str: "alice1"), role: .responder, keychain: mockKeychain, localStaticKey: bobKey) peerID: PeerID(str: "peer1"), role: .initiator, keychain: mockKeychain,
localStaticKey: aliceKey)
let bobSession1 = NoiseSession(
peerID: PeerID(str: "alice1"), role: .responder, keychain: mockKeychain,
localStaticKey: bobKey)
let aliceSession2 = NoiseSession(peerID: PeerID(str: "peer2"), role: .initiator, keychain: mockKeychain, localStaticKey: aliceKey) let aliceSession2 = NoiseSession(
let bobSession2 = NoiseSession(peerID: PeerID(str: "alice2"), role: .responder, keychain: mockKeychain, localStaticKey: bobKey) peerID: PeerID(str: "peer2"), role: .initiator, keychain: mockKeychain,
localStaticKey: aliceKey)
let bobSession2 = NoiseSession(
peerID: PeerID(str: "alice2"), role: .responder, keychain: mockKeychain,
localStaticKey: bobKey)
// Establish both pairs // Establish both pairs
try performHandshake(initiator: aliceSession1, responder: bobSession1) try performHandshake(initiator: aliceSession1, responder: bobSession1)
@@ -305,17 +361,20 @@ struct NoiseProtocolTests {
_ = try aliceManager.decrypt(message2, from: alicePeerID) _ = try aliceManager.decrypt(message2, from: alicePeerID)
// Simulate Bob restart by creating new manager with same key // Simulate Bob restart by creating new manager with same key
let bobManagerRestarted = NoiseSessionManager(localStaticKey: bobKey, keychain: mockKeychain) let bobManagerRestarted = NoiseSessionManager(
localStaticKey: bobKey, keychain: mockKeychain)
// Bob initiates new handshake after restart // Bob initiates new handshake after restart
let newHandshake1 = try bobManagerRestarted.initiateHandshake(with: bobPeerID) let newHandshake1 = try bobManagerRestarted.initiateHandshake(with: bobPeerID)
// Alice should accept the new handshake (clearing old session) // Alice should accept the new handshake (clearing old session)
let newHandshake2 = try aliceManager.handleIncomingHandshake(from: alicePeerID, message: newHandshake1) let newHandshake2 = try aliceManager.handleIncomingHandshake(
from: alicePeerID, message: newHandshake1)
#expect(newHandshake2 != nil) #expect(newHandshake2 != nil)
// Complete the new handshake // Complete the new handshake
let newHandshake3 = try bobManagerRestarted.handleIncomingHandshake(from: bobPeerID, message: newHandshake2!) let newHandshake3 = try bobManagerRestarted.handleIncomingHandshake(
from: bobPeerID, message: newHandshake2!)
#expect(newHandshake3 != nil) #expect(newHandshake3 != nil)
_ = try aliceManager.handleIncomingHandshake(from: alicePeerID, message: newHandshake3!) _ = try aliceManager.handleIncomingHandshake(from: alicePeerID, message: newHandshake3!)
@@ -328,8 +387,10 @@ struct NoiseProtocolTests {
@Test func nonceDesynchronizationRecovery() throws { @Test func nonceDesynchronizationRecovery() throws {
// Create two sessions // Create two sessions
let aliceSession = NoiseSession(peerID: alicePeerID, role: .initiator, keychain: mockKeychain, localStaticKey: aliceKey) let aliceSession = NoiseSession(
let bobSession = NoiseSession(peerID: bobPeerID, role: .responder, keychain: mockKeychain, localStaticKey: bobKey) peerID: alicePeerID, role: .initiator, keychain: mockKeychain, localStaticKey: aliceKey)
let bobSession = NoiseSession(
peerID: bobPeerID, role: .responder, keychain: mockKeychain, localStaticKey: bobKey)
// Establish sessions // Establish sessions
try performHandshake(initiator: aliceSession, responder: bobSession) try performHandshake(initiator: aliceSession, responder: bobSession)
@@ -361,7 +422,8 @@ struct NoiseProtocolTests {
let messageCount = 100 let messageCount = 100
try await confirmation("All messages encrypted and decrypted", expectedCount: messageCount) { completion in try await confirmation("All messages encrypted and decrypted", expectedCount: messageCount)
{ completion in
var encryptedMessages: [Int: Data] = [:] var encryptedMessages: [Int: Data] = [:]
// Encrypt messages sequentially to avoid nonce races in manager // Encrypt messages sequentially to avoid nonce races in manager
for i in 0..<messageCount { for i in 0..<messageCount {
@@ -454,11 +516,13 @@ struct NoiseProtocolTests {
let newHandshake1 = try aliceManager.initiateHandshake(with: alicePeerID) let newHandshake1 = try aliceManager.initiateHandshake(with: alicePeerID)
// Bob should accept the new handshake even though he has a valid session // Bob should accept the new handshake even though he has a valid session
let newHandshake2 = try bobManager.handleIncomingHandshake(from: bobPeerID, message: newHandshake1) let newHandshake2 = try bobManager.handleIncomingHandshake(
from: bobPeerID, message: newHandshake1)
#expect(newHandshake2 != nil, "Bob should accept handshake despite having valid session") #expect(newHandshake2 != nil, "Bob should accept handshake despite having valid session")
// Complete the handshake // Complete the handshake
let newHandshake3 = try aliceManager.handleIncomingHandshake(from: alicePeerID, message: newHandshake2!) let newHandshake3 = try aliceManager.handleIncomingHandshake(
from: alicePeerID, message: newHandshake2!)
#expect(newHandshake3 != nil) #expect(newHandshake3 != nil)
_ = try bobManager.handleIncomingHandshake(from: bobPeerID, message: newHandshake3!) _ = try bobManager.handleIncomingHandshake(from: bobPeerID, message: newHandshake3!)
@@ -489,7 +553,8 @@ struct NoiseProtocolTests {
} }
// With nonce carried in packet, decryption should not throw here // With nonce carried in packet, decryption should not throw here
let desyncMessage = try aliceManager.encrypt("This now succeeds".data(using: .utf8)!, for: alicePeerID) let desyncMessage = try aliceManager.encrypt(
"This now succeeds".data(using: .utf8)!, for: alicePeerID)
#expect(throws: Never.self) { #expect(throws: Never.self) {
try bobManager.decrypt(desyncMessage, from: bobPeerID) try bobManager.decrypt(desyncMessage, from: bobPeerID)
} }
@@ -499,11 +564,13 @@ struct NoiseProtocolTests {
let rehandshake1 = try bobManager.initiateHandshake(with: bobPeerID) let rehandshake1 = try bobManager.initiateHandshake(with: bobPeerID)
// Alice should accept despite having a "valid" (but desynced) session // Alice should accept despite having a "valid" (but desynced) session
let rehandshake2 = try aliceManager.handleIncomingHandshake(from: alicePeerID, message: rehandshake1) let rehandshake2 = try aliceManager.handleIncomingHandshake(
from: alicePeerID, message: rehandshake1)
#expect(rehandshake2 != nil, "Alice should accept handshake to fix desync") #expect(rehandshake2 != nil, "Alice should accept handshake to fix desync")
// Complete handshake // Complete handshake
let rehandshake3 = try bobManager.handleIncomingHandshake(from: bobPeerID, message: rehandshake2!) let rehandshake3 = try bobManager.handleIncomingHandshake(
from: bobPeerID, message: rehandshake2!)
#expect(rehandshake3 != nil) #expect(rehandshake3 != nil)
_ = try aliceManager.handleIncomingHandshake(from: alicePeerID, message: rehandshake3!) _ = try aliceManager.handleIncomingHandshake(from: alicePeerID, message: rehandshake3!)
@@ -514,6 +581,18 @@ struct NoiseProtocolTests {
#expect(decryptedResync == testResynced) #expect(decryptedResync == testResynced)
} }
// MARK: - Test Vector Tests
@Test func noiseTestVectors() throws {
// Load test vectors from bundle
let testVectors = try loadTestVectors()
for (index, testVector) in testVectors.enumerated() {
print("Running test vector \(index + 1): \(testVector.protocol_name)")
try runTestVector(testVector)
}
}
// MARK: - Helper Methods // MARK: - Helper Methods
private func performHandshake(initiator: NoiseSession, responder: NoiseSession) throws { private func performHandshake(initiator: NoiseSession, responder: NoiseSession) throws {
@@ -523,10 +602,346 @@ struct NoiseProtocolTests {
_ = try responder.processHandshakeMessage(msg3) _ = try responder.processHandshakeMessage(msg3)
} }
private func establishManagerSessions(aliceManager: NoiseSessionManager, bobManager: NoiseSessionManager) throws { private func establishManagerSessions(
aliceManager: NoiseSessionManager, bobManager: NoiseSessionManager
) throws {
let msg1 = try aliceManager.initiateHandshake(with: alicePeerID) let msg1 = try aliceManager.initiateHandshake(with: alicePeerID)
let msg2 = try bobManager.handleIncomingHandshake(from: bobPeerID, message: msg1)! let msg2 = try bobManager.handleIncomingHandshake(from: bobPeerID, message: msg1)!
let msg3 = try aliceManager.handleIncomingHandshake(from: alicePeerID, message: msg2)! let msg3 = try aliceManager.handleIncomingHandshake(from: alicePeerID, message: msg2)!
_ = try bobManager.handleIncomingHandshake(from: bobPeerID, message: msg3) _ = try bobManager.handleIncomingHandshake(from: bobPeerID, message: msg3)
} }
private func loadTestVectors() throws -> [NoiseTestVector] {
// Try to load from test bundle
let testBundle = Bundle(for: MockKeychain.self)
guard let url = testBundle.url(forResource: "NoiseTestVectors", withExtension: "json")
else {
throw NSError(
domain: "NoiseTests", code: 1,
userInfo: [
NSLocalizedDescriptionKey: "Could not find NoiseTestVectors.json in test bundle"
])
}
let data = try Data(contentsOf: url)
return try JSONDecoder().decode([NoiseTestVector].self, from: data)
}
private func runTestVector(_ testVector: NoiseTestVector) throws {
// Parse test inputs
guard let initStatic = Data(hex: testVector.init_static),
let initEphemeral = Data(hex: testVector.init_ephemeral),
let respStatic = Data(hex: testVector.resp_static),
let respEphemeral = Data(hex: testVector.resp_ephemeral),
let prologue = Data(hex: testVector.init_prologue)
else {
throw NSError(
domain: "NoiseTests", code: 2,
userInfo: [NSLocalizedDescriptionKey: "Failed to parse test vector hex strings"])
}
let expectedHash = testVector.handshake_hash.flatMap { Data(hex: $0) }
// Create keys
guard
let initStaticKey = try? Curve25519.KeyAgreement.PrivateKey(
rawRepresentation: initStatic),
let initEphemeralKey = try? Curve25519.KeyAgreement.PrivateKey(
rawRepresentation: initEphemeral),
let respStaticKey = try? Curve25519.KeyAgreement.PrivateKey(
rawRepresentation: respStatic),
let respEphemeralKey = try? Curve25519.KeyAgreement.PrivateKey(
rawRepresentation: respEphemeral)
else {
throw NSError(
domain: "NoiseTests", code: 3,
userInfo: [NSLocalizedDescriptionKey: "Failed to create keys from test vectors"])
}
let keychain = MockKeychain()
// Create handshake states
let initiatorHandshake = NoiseHandshakeState(
role: .initiator,
pattern: .XX,
keychain: keychain,
localStaticKey: initStaticKey,
prologue: prologue,
predeterminedEphemeralKey: initEphemeralKey
)
let responderHandshake = NoiseHandshakeState(
role: .responder,
pattern: .XX,
keychain: keychain,
localStaticKey: respStaticKey,
prologue: prologue,
predeterminedEphemeralKey: respEphemeralKey
)
// For XX pattern, we have 3 handshake messages, then transport messages
// The test vector messages are ordered as: [msg1, msg2, msg3, transport1, transport2, ...]
guard testVector.messages.count >= 3 else {
throw NSError(
domain: "NoiseTests", code: 5,
userInfo: [NSLocalizedDescriptionKey: "Test vector must have at least 3 messages for XX pattern"])
}
// Message 1: Initiator -> Responder (e)
guard let payload1 = Data(hex: testVector.messages[0].payload),
let expectedCiphertext1 = Data(hex: testVector.messages[0].ciphertext) else {
throw NSError(
domain: "NoiseTests", code: 4,
userInfo: [NSLocalizedDescriptionKey: "Message 1: Failed to parse hex"])
}
let msg1 = try initiatorHandshake.writeMessage(payload: payload1)
#expect(!msg1.isEmpty, "Message 1 should not be empty")
#expect(msg1 == expectedCiphertext1, "Message 1 ciphertext should match expected value. Got: \(msg1.hexString()), Expected: \(expectedCiphertext1.hexString())")
let decrypted1 = try responderHandshake.readMessage(msg1)
#expect(decrypted1 == payload1, "Message 1: Decrypted payload should match original")
// Message 2: Responder -> Initiator (e, ee, s, es)
guard let payload2 = Data(hex: testVector.messages[1].payload),
let expectedCiphertext2 = Data(hex: testVector.messages[1].ciphertext) else {
throw NSError(
domain: "NoiseTests", code: 4,
userInfo: [NSLocalizedDescriptionKey: "Message 2: Failed to parse hex"])
}
let msg2 = try responderHandshake.writeMessage(payload: payload2)
#expect(!msg2.isEmpty, "Message 2 should not be empty")
#expect(msg2 == expectedCiphertext2, "Message 2 ciphertext should match expected value. Got: \(msg2.hexString()), Expected: \(expectedCiphertext2.hexString())")
let decrypted2 = try initiatorHandshake.readMessage(msg2)
#expect(decrypted2 == payload2, "Message 2: Decrypted payload should match original")
// Message 3: Initiator -> Responder (s, se)
guard let payload3 = Data(hex: testVector.messages[2].payload),
let expectedCiphertext3 = Data(hex: testVector.messages[2].ciphertext) else {
throw NSError(
domain: "NoiseTests", code: 4,
userInfo: [NSLocalizedDescriptionKey: "Message 3: Failed to parse hex"])
}
let msg3 = try initiatorHandshake.writeMessage(payload: payload3)
#expect(!msg3.isEmpty, "Message 3 should not be empty")
#expect(msg3 == expectedCiphertext3, "Message 3 ciphertext should match expected value. Got: \(msg3.hexString()), Expected: \(expectedCiphertext3.hexString())")
let decrypted3 = try responderHandshake.readMessage(msg3)
#expect(decrypted3 == payload3, "Message 3: Decrypted payload should match original")
// Verify handshake hash
let initiatorHash = initiatorHandshake.getHandshakeHash()
let responderHash = responderHandshake.getHandshakeHash()
#expect(initiatorHash == responderHash, "Initiator and responder hashes should match")
if let expectedHash = expectedHash {
#expect(
initiatorHash == expectedHash,
"Handshake hash should match expected value from test vector. Got: \(initiatorHash.hexString()), Expected: \(expectedHash.hexString())")
}
// Get transport ciphers
let (initSend, initRecv) = try initiatorHandshake.getTransportCiphers(useExtractedNonce: false)
let (respSend, respRecv) = try responderHandshake.getTransportCiphers(useExtractedNonce: false)
// Test transport messages (messages after the 3 handshake messages)
for index in 3..<testVector.messages.count {
let testMsg = testVector.messages[index]
guard let payload = Data(hex: testMsg.payload),
let expectedCiphertext = Data(hex: testMsg.ciphertext) else {
throw NSError(
domain: "NoiseTests", code: 4,
userInfo: [
NSLocalizedDescriptionKey:
"Message \(index + 1): Failed to parse payload hex"
])
}
// Alternate between responder and initiator sending
// Responder sends first transport message (since initiator sent last handshake message)
let (sender, receiver): (NoiseCipherState, NoiseCipherState)
let transportIndex = index - 3
if transportIndex % 2 == 0 {
// Even transport messages: responder sends
sender = respSend
receiver = initRecv
} else {
// Odd transport messages: initiator sends
sender = initSend
receiver = respRecv
}
// Encrypt and validate ciphertext matches expected value
let ciphertext = try sender.encrypt(plaintext: payload)
#expect(
ciphertext == expectedCiphertext,
"Message \(index + 1) ciphertext should match expected value. Got: \(ciphertext.hexString()), Expected: \(expectedCiphertext.hexString())")
// Decrypt and validate payload
let decrypted = try receiver.decrypt(ciphertext: ciphertext)
#expect(
decrypted == payload,
"Message \(index + 1): Decrypted payload should match original")
}
}
// MARK: - DH Shared Secret Clearing Tests
@Test func secureClearCalledDuringHandshake() throws {
// Use TrackingMockKeychain to verify secureClear is called
let trackingKeychain = TrackingMockKeychain()
let aliceKey = Curve25519.KeyAgreement.PrivateKey()
let bobKey = Curve25519.KeyAgreement.PrivateKey()
let alice = NoiseSession(
peerID: PeerID(str: "alice-test"),
role: .initiator,
keychain: trackingKeychain,
localStaticKey: aliceKey
)
let bob = NoiseSession(
peerID: PeerID(str: "bob-test"),
role: .responder,
keychain: trackingKeychain,
localStaticKey: bobKey
)
// Perform handshake
let msg1 = try alice.startHandshake()
let msg2 = try bob.processHandshakeMessage(msg1)!
let msg3 = try alice.processHandshakeMessage(msg2)!
_ = try bob.processHandshakeMessage(msg3)
// In Noise XX pattern handshake:
// - Message 1 (initiator): e token only (no DH)
// - Message 2 (responder): e, ee, s, es tokens (2 DH operations: ee, es)
// - Message 3 (initiator): s, se tokens (1 DH operation: se)
// Total in writeMessage: 3 DH operations (ee, es, se)
//
// In readMessage (performDHOperation):
// - After msg1: no DH
// - After msg2: ee, es (2 DH operations)
// - After msg3: se (1 DH operation)
// Total in performDHOperation: 3 DH operations
//
// Grand total: 6 DH operations requiring secureClear
//
// Note: .ss pattern is only used in certain handshake patterns, not XX
let expectedMinimumCalls = 6
#expect(
trackingKeychain.secureClearDataCallCount >= expectedMinimumCalls,
"Expected at least \(expectedMinimumCalls) secureClear calls for DH secrets, got \(trackingKeychain.secureClearDataCallCount)"
)
}
@Test func encryptionWorksAfterSecureClear() throws {
// Verify that encryption/decryption still works correctly after adding secureClear
let trackingKeychain = TrackingMockKeychain()
let aliceKey = Curve25519.KeyAgreement.PrivateKey()
let bobKey = Curve25519.KeyAgreement.PrivateKey()
let alice = NoiseSession(
peerID: PeerID(str: "alice-test-enc"),
role: .initiator,
keychain: trackingKeychain,
localStaticKey: aliceKey
)
let bob = NoiseSession(
peerID: PeerID(str: "bob-test-enc"),
role: .responder,
keychain: trackingKeychain,
localStaticKey: bobKey
)
// Perform handshake
let msg1 = try alice.startHandshake()
let msg2 = try bob.processHandshakeMessage(msg1)!
let msg3 = try alice.processHandshakeMessage(msg2)!
_ = try bob.processHandshakeMessage(msg3)
// Verify both sessions are established
#expect(alice.isEstablished())
#expect(bob.isEstablished())
// Verify secureClear was called (basic sanity check)
#expect(trackingKeychain.secureClearDataCallCount > 0)
// Test encryption from Alice to Bob
let plaintext1 = "Hello from Alice after secureClear!".data(using: .utf8)!
let ciphertext1 = try alice.encrypt(plaintext1)
let decrypted1 = try bob.decrypt(ciphertext1)
#expect(decrypted1 == plaintext1)
// Test encryption from Bob to Alice
let plaintext2 = "Hello from Bob after secureClear!".data(using: .utf8)!
let ciphertext2 = try bob.encrypt(plaintext2)
let decrypted2 = try alice.decrypt(ciphertext2)
#expect(decrypted2 == plaintext2)
// Test multiple messages to verify cipher state is correct
for i in 1...10 {
let msg = "Message \(i) from Alice".data(using: .utf8)!
let cipher = try alice.encrypt(msg)
let dec = try bob.decrypt(cipher)
#expect(dec == msg)
}
}
@Test func secureClearCalledInBothWriteAndReadPaths() throws {
// Verify secureClear is called in both writeMessage and readMessage paths
// We do this by checking the count increases at each step
let aliceKeychain = TrackingMockKeychain()
let bobKeychain = TrackingMockKeychain()
let aliceKey = Curve25519.KeyAgreement.PrivateKey()
let bobKey = Curve25519.KeyAgreement.PrivateKey()
let alice = NoiseSession(
peerID: PeerID(str: "alice-paths"),
role: .initiator,
keychain: aliceKeychain,
localStaticKey: aliceKey
)
let bob = NoiseSession(
peerID: PeerID(str: "bob-paths"),
role: .responder,
keychain: bobKeychain,
localStaticKey: bobKey
)
// Message 1: Alice writes (e token only, no DH)
let msg1 = try alice.startHandshake()
let aliceCountAfterMsg1 = aliceKeychain.secureClearDataCallCount
// No DH in message 1 for initiator
#expect(aliceCountAfterMsg1 == 0, "No DH secrets in message 1 write")
// Bob reads message 1 (no DH) and writes message 2 (ee, es DH operations)
let msg2 = try bob.processHandshakeMessage(msg1)!
let bobCountAfterMsg2 = bobKeychain.secureClearDataCallCount
// Bob should have cleared secrets for: ee (read), es (read), ee (write), es (write)
#expect(bobCountAfterMsg2 >= 2, "Bob should clear DH secrets when processing/writing message 2")
// Alice reads message 2 (ee, es) and writes message 3 (se)
let msg3 = try alice.processHandshakeMessage(msg2)!
let aliceCountAfterMsg3 = aliceKeychain.secureClearDataCallCount
// Alice should have cleared: ee (read), es (read), se (write)
#expect(aliceCountAfterMsg3 >= 3, "Alice should clear DH secrets when processing/writing message 3")
// Bob reads message 3 (se)
_ = try bob.processHandshakeMessage(msg3)
let bobFinalCount = bobKeychain.secureClearDataCallCount
// Bob should have additionally cleared: se (read)
#expect(bobFinalCount > bobCountAfterMsg2, "Bob should clear DH secrets when processing message 3")
}
} }
+71
View File
@@ -0,0 +1,71 @@
[
{
"protocol_name": "Noise_XX_25519_ChaChaPoly_SHA256",
"init_prologue": "4a6f686e2047616c74",
"init_static": "e61ef9919cde45dd5f82166404bd08e38bceb5dfdfded0a34c8df7ed542214d1",
"init_ephemeral": "893e28b9dc6ca8d611ab664754b8ceb7bac5117349a4439a6b0569da977c464a",
"resp_prologue": "4a6f686e2047616c74",
"resp_static": "4a3acbfdb163dec651dfa3194dece676d437029c62a408b4c5ea9114246e4893",
"resp_ephemeral": "bbdb4cdbd309f1a1f2e1456967fe288cadd6f712d65dc7b7793d5e63da6b375b",
"handshake_hash": "c8e5f64e846193be2a834104c2a009868d6c9f3bd3c186299888b488b2f1f58e",
"messages": [
{
"payload": "4c756477696720766f6e204d69736573",
"ciphertext": "ca35def5ae56cec33dc2036731ab14896bc4c75dbb07a61f879f8e3afa4c79444c756477696720766f6e204d69736573"
},
{
"payload": "4d757272617920526f746862617264",
"ciphertext": "95ebc60d2b1fa672c1f46a8aa265ef51bfe38e7ccb39ec5be34069f14480884381cbad1f276e038c48378ffce2b65285e08d6b68aaa3629a5a8639392490e5b9bd5269c2f1e4f488ed8831161f19b7815528f8982ffe09be9b5c412f8a0db50f8814c7194e83f23dbd8d162c9326ad"
},
{
"payload": "462e20412e20486179656b",
"ciphertext": "c7195ffacac1307ff99046f219750fc47693e23c3cb08b89c2af808b444850a80ae475b9df0f169ae80a89be0865b57f58c9fea0d4ec82a286427402f113e4b6ae769a1d95941d49b25030"
},
{
"payload": "4361726c204d656e676572",
"ciphertext": "96763ed773f8e47bb3712f0e29b3060ffc956ffc146cee53d5e1df"
},
{
"payload": "4a65616e2d426170746973746520536179",
"ciphertext": "3e40f15f6f3a46ae446b253bf8b1d9ffb6ed9b174d272328ff91a7e2e5c79c07f5"
},
{
"payload": "457567656e2042f6686d20766f6e2042617765726b",
"ciphertext": "eb3f3515110702e047a6c9da4478b6ead94873c11c0f2d710ddb3f09fce024b3a58502ae3f"
}
]
},
{
"protocol_name": "Noise_XX_25519_ChaChaPoly_SHA256",
"init_prologue": "5468657265206973206e6f20726967687420616e642077726f6e672e2054686572652773206f6e6c792066756e20616e6420626f72696e672e",
"init_psks": [],
"init_static": "7dec208517a3b81a2861d7a71266d5d6dc944c5a8816634a86fe63198a0148ee",
"init_ephemeral": "a32daf21e93c0131495ce1d903181fde81cc46937daaeb990bae7c992709421e",
"resp_prologue": "5468657265206973206e6f20726967687420616e642077726f6e672e2054686572652773206f6e6c792066756e20616e6420626f72696e672e",
"resp_psks": [],
"resp_static": "4d0aed5098e3b4ef20357e9f686ce66204c792b358da2e475017d6c485304881",
"resp_ephemeral": "4eece0f195d026db035ff987597c429d3ad3bcc2944df37d649528951b2a27c5",
"messages": [
{
"payload": "d03c489139e645d0711a3c9e810d776b46a84912463fafa87b884eebf242dc34",
"ciphertext": "f9fa868ba97ab8a2686deccfaad5a484ee10a5bb85e3d1dce015a84797f92818d03c489139e645d0711a3c9e810d776b46a84912463fafa87b884eebf242dc34"
},
{
"payload": "d8190a92f7dc0c93dbea9118ba8055751fb7c6590c416ffbd419964132b99a85",
"ciphertext": "8c4e6fdb7d09d501a86f7eca5c234522751706ed409182c05cdf5f827d4dae47b81c6c5f43b025692c24391eefee725c17d8cb0fbe3e4abb8aedf42c4fd2592d4ea48ac08989d6ae8b4adae08b2c34087c808c7aa55a63c02b0fab9e930612336bd43eaea04d3c670a0a146691aa9cc9d357872320dc735dbc48580cffb553db"
},
{
"payload": "77891b19dcb92ef7c055b672c4a5aa7fdf1c84146b8b303459022729473ce254",
"ciphertext": "933ca6b5ed60df3df66121f0ab49a09e49efa45c613a86a3cecbf4c535cef2f83f72b42837b18e3572f2fdc2b74c331e2368a545cef54bdca081678ab0e9dd5348122459e0c034c851984d88ce610963d43cde6cfe73a67fbd5a63e8bfca96d0"
},
{
"payload": "d7efdf988072881941db045a42882433817555128fbf5663e56081712ec7d212",
"ciphertext": "54ef0ff0629e1aaa7685a2806ab111cba76b52331f2642276736f415868eacb69ab2577f3bda0cbf72f879685f6ed25f"
},
{
"payload": "dd7bf01a588bafb52c6cfba952e5d8fe35cc2b3f92b4730ae2474615157345ce",
"ciphertext": "356be70f110306d5c699bb834bb9d58d909e325924dfbec972e406e6f294dc63e1daebefe8a62a334facc8048ab4ad66"
}
]
}
]
@@ -0,0 +1,108 @@
//
// NostrTransportTests.swift
// bitchat
//
// This is free and unencumbered software released into the public domain.
// For more information, see <https://unlicense.org>
//
import Foundation
import Testing
@testable import bitchat
@Suite("NostrTransport Thread Safety Tests")
struct NostrTransportTests {
@Test("Concurrent read receipt enqueue does not crash")
@MainActor
func concurrentReadReceiptEnqueue() async throws {
let keychain = MockKeychain()
let idBridge = NostrIdentityBridge(keychain: keychain)
let transport = NostrTransport(keychain: keychain, idBridge: idBridge)
// Create 100 concurrent read receipt submissions
let iterations = 100
await withTaskGroup(of: Void.self) { group in
for i in 0..<iterations {
group.addTask {
let receipt = ReadReceipt(
originalMessageID: UUID().uuidString,
readerID: PeerID(str: String(format: "%016x", i)),
readerNickname: "Reader\(i)"
)
let peerID = PeerID(str: String(format: "%016x", i))
transport.sendReadReceipt(receipt, to: peerID)
}
}
}
// If we reach here without crashing, the test passes
// The concurrent enqueue operations completed without data races
}
@Test("Read queue processes under concurrent load")
@MainActor
func readQueueProcessingUnderLoad() async throws {
let keychain = MockKeychain()
let idBridge = NostrIdentityBridge(keychain: keychain)
let transport = NostrTransport(keychain: keychain, idBridge: idBridge)
// Rapidly enqueue many receipts from multiple concurrent sources
let iterations = 50
// First batch - rapid fire
for i in 0..<iterations {
let receipt = ReadReceipt(
originalMessageID: UUID().uuidString,
readerID: PeerID(str: String(format: "%016x", i)),
readerNickname: "Reader\(i)"
)
transport.sendReadReceipt(receipt, to: PeerID(str: String(format: "%016x", i)))
}
// Give some time for processing to start
try await Task.sleep(nanoseconds: 100_000_000) // 100ms
// Second batch - while first might be processing
await withTaskGroup(of: Void.self) { group in
for i in iterations..<(iterations * 2) {
group.addTask {
let receipt = ReadReceipt(
originalMessageID: UUID().uuidString,
readerID: PeerID(str: String(format: "%016x", i)),
readerNickname: "Reader\(i)"
)
transport.sendReadReceipt(receipt, to: PeerID(str: String(format: "%016x", i)))
}
}
}
// If we reach here without crashing or deadlocking, test passes
}
@Test("isPeerReachable is thread safe")
@MainActor
func isPeerReachableThreadSafety() async throws {
let keychain = MockKeychain()
let idBridge = NostrIdentityBridge(keychain: keychain)
let transport = NostrTransport(keychain: keychain, idBridge: idBridge)
let iterations = 100
// Concurrent reads on isPeerReachable
await withTaskGroup(of: Bool.self) { group in
for i in 0..<iterations {
group.addTask {
let peerID = PeerID(str: String(format: "%016x", i))
return transport.isPeerReachable(peerID)
}
}
// Collect results (all should be false since no favorites configured)
for await result in group {
#expect(result == false)
}
}
}
}
+108
View File
@@ -0,0 +1,108 @@
//
// HexStringTests.swift
// bitchatTests
//
// Tests for Data(hexString:) hex parsing
//
import Testing
import Foundation
@testable import bitchat
struct HexStringTests {
// MARK: - Valid Hex Strings
@Test func validHexString() {
let data = Data(hexString: "0102030405")
#expect(data == Data([0x01, 0x02, 0x03, 0x04, 0x05]))
}
@Test func validHexStringUppercase() {
let data = Data(hexString: "AABBCCDD")
#expect(data == Data([0xAA, 0xBB, 0xCC, 0xDD]))
}
@Test func validHexStringMixedCase() {
let data = Data(hexString: "aAbBcCdD")
#expect(data == Data([0xAA, 0xBB, 0xCC, 0xDD]))
}
@Test func validHexStringWith0xPrefix() {
let data = Data(hexString: "0x0102030405")
#expect(data == Data([0x01, 0x02, 0x03, 0x04, 0x05]))
}
@Test func validHexStringWith0XPrefix() {
let data = Data(hexString: "0XAABBCCDD")
#expect(data == Data([0xAA, 0xBB, 0xCC, 0xDD]))
}
@Test func validHexStringWithWhitespace() {
let data = Data(hexString: " 0102030405 ")
#expect(data == Data([0x01, 0x02, 0x03, 0x04, 0x05]))
}
@Test func validHexStringWith0xPrefixAndWhitespace() {
let data = Data(hexString: " 0x0102030405 ")
#expect(data == Data([0x01, 0x02, 0x03, 0x04, 0x05]))
}
@Test func emptyHexString() {
let data = Data(hexString: "")
#expect(data == Data())
}
@Test func emptyHexStringWithWhitespace() {
let data = Data(hexString: " ")
#expect(data == Data())
}
@Test func emptyHexStringWith0xPrefix() {
let data = Data(hexString: "0x")
#expect(data == Data())
}
// MARK: - Invalid Hex Strings
@Test func oddLengthHexStringReturnsNil() {
let data = Data(hexString: "012")
#expect(data == nil)
}
@Test func oddLengthHexStringWith0xPrefixReturnsNil() {
let data = Data(hexString: "0x012")
#expect(data == nil)
}
@Test func invalidCharactersReturnNil() {
let data = Data(hexString: "GHIJ")
#expect(data == nil)
}
@Test func mixedValidAndInvalidCharactersReturnNil() {
let data = Data(hexString: "01GH")
#expect(data == nil)
}
@Test func specialCharactersReturnNil() {
let data = Data(hexString: "01-02")
#expect(data == nil)
}
// MARK: - Round Trip Tests
@Test func roundTripConversion() {
let original = Data([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF])
let hexString = original.hexEncodedString()
let roundTripped = Data(hexString: hexString)
#expect(roundTripped == original)
}
@Test func roundTripConversionWith0xPrefix() {
let original = Data([0xDE, 0xAD, 0xBE, 0xEF])
let hexString = "0x" + original.hexEncodedString()
let roundTripped = Data(hexString: hexString)
#expect(roundTripped == original)
}
}
@@ -0,0 +1,222 @@
//
// XChaCha20Poly1305CompatTests.swift
// bitchatTests
//
// Tests for XChaCha20-Poly1305 encryption with proper error handling.
// This is free and unencumbered software released into the public domain.
//
import Testing
import struct Foundation.Data
@testable import bitchat
struct XChaCha20Poly1305CompatTests {
@Test func sealAndOpenRoundtrip() throws {
let plaintext = "Hello, XChaCha20-Poly1305!".data(using: .utf8)!
let key = Data(repeating: 0x42, count: 32)
let nonce = Data(repeating: 0x24, count: 24)
let sealed = try XChaCha20Poly1305Compat.seal(plaintext: plaintext, key: key, nonce24: nonce)
let decrypted = try XChaCha20Poly1305Compat.open(
ciphertext: sealed.ciphertext,
tag: sealed.tag,
key: key,
nonce24: nonce
)
#expect(decrypted == plaintext)
}
@Test func sealAndOpenWithAAD() throws {
let plaintext = "Secret message".data(using: .utf8)!
let key = Data(repeating: 0xAB, count: 32)
let nonce = Data(repeating: 0xCD, count: 24)
let aad = "additional authenticated data".data(using: .utf8)!
let sealed = try XChaCha20Poly1305Compat.seal(plaintext: plaintext, key: key, nonce24: nonce, aad: aad)
let decrypted = try XChaCha20Poly1305Compat.open(
ciphertext: sealed.ciphertext,
tag: sealed.tag,
key: key,
nonce24: nonce,
aad: aad
)
#expect(decrypted == plaintext)
}
@Test func sealProducesDifferentCiphertextWithDifferentNonces() throws {
let plaintext = "Same plaintext".data(using: .utf8)!
let key = Data(repeating: 0x42, count: 32)
let nonce1 = Data(repeating: 0x01, count: 24)
let nonce2 = Data(repeating: 0x02, count: 24)
let sealed1 = try XChaCha20Poly1305Compat.seal(plaintext: plaintext, key: key, nonce24: nonce1)
let sealed2 = try XChaCha20Poly1305Compat.seal(plaintext: plaintext, key: key, nonce24: nonce2)
#expect(sealed1.ciphertext != sealed2.ciphertext)
}
@Test func sealThrowsOnShortKey() {
let plaintext = "Test".data(using: .utf8)!
let shortKey = Data(repeating: 0x42, count: 16)
let nonce = Data(repeating: 0x24, count: 24)
var didThrow = false
do {
_ = try XChaCha20Poly1305Compat.seal(plaintext: plaintext, key: shortKey, nonce24: nonce)
} catch {
didThrow = true
}
#expect(didThrow)
}
@Test func sealThrowsOnLongKey() {
let plaintext = "Test".data(using: .utf8)!
let longKey = Data(repeating: 0x42, count: 64)
let nonce = Data(repeating: 0x24, count: 24)
var didThrow = false
do {
_ = try XChaCha20Poly1305Compat.seal(plaintext: plaintext, key: longKey, nonce24: nonce)
} catch {
didThrow = true
}
#expect(didThrow)
}
@Test func sealThrowsOnEmptyKey() {
let plaintext = "Test".data(using: .utf8)!
let emptyKey = Data()
let nonce = Data(repeating: 0x24, count: 24)
var didThrow = false
do {
_ = try XChaCha20Poly1305Compat.seal(plaintext: plaintext, key: emptyKey, nonce24: nonce)
} catch {
didThrow = true
}
#expect(didThrow)
}
@Test func openThrowsOnInvalidKeyLength() {
let ciphertext = Data(repeating: 0x00, count: 16)
let tag = Data(repeating: 0x00, count: 16)
let shortKey = Data(repeating: 0x42, count: 31)
let nonce = Data(repeating: 0x24, count: 24)
var didThrow = false
do {
_ = try XChaCha20Poly1305Compat.open(ciphertext: ciphertext, tag: tag, key: shortKey, nonce24: nonce)
} catch {
didThrow = true
}
#expect(didThrow)
}
@Test func sealThrowsOnShortNonce() {
let plaintext = "Test".data(using: .utf8)!
let key = Data(repeating: 0x42, count: 32)
let shortNonce = Data(repeating: 0x24, count: 12)
var didThrow = false
do {
_ = try XChaCha20Poly1305Compat.seal(plaintext: plaintext, key: key, nonce24: shortNonce)
} catch {
didThrow = true
}
#expect(didThrow)
}
@Test func sealThrowsOnLongNonce() {
let plaintext = "Test".data(using: .utf8)!
let key = Data(repeating: 0x42, count: 32)
let longNonce = Data(repeating: 0x24, count: 32)
var didThrow = false
do {
_ = try XChaCha20Poly1305Compat.seal(plaintext: plaintext, key: key, nonce24: longNonce)
} catch {
didThrow = true
}
#expect(didThrow)
}
@Test func sealThrowsOnEmptyNonce() {
let plaintext = "Test".data(using: .utf8)!
let key = Data(repeating: 0x42, count: 32)
let emptyNonce = Data()
var didThrow = false
do {
_ = try XChaCha20Poly1305Compat.seal(plaintext: plaintext, key: key, nonce24: emptyNonce)
} catch {
didThrow = true
}
#expect(didThrow)
}
@Test func openThrowsOnInvalidNonceLength() {
let ciphertext = Data(repeating: 0x00, count: 16)
let tag = Data(repeating: 0x00, count: 16)
let key = Data(repeating: 0x42, count: 32)
let shortNonce = Data(repeating: 0x24, count: 23)
var didThrow = false
do {
_ = try XChaCha20Poly1305Compat.open(ciphertext: ciphertext, tag: tag, key: key, nonce24: shortNonce)
} catch {
didThrow = true
}
#expect(didThrow)
}
@Test func openFailsWithWrongKey() throws {
let plaintext = "Secret".data(using: .utf8)!
let correctKey = Data(repeating: 0x42, count: 32)
let wrongKey = Data(repeating: 0x43, count: 32)
let nonce = Data(repeating: 0x24, count: 24)
let sealed = try XChaCha20Poly1305Compat.seal(plaintext: plaintext, key: correctKey, nonce24: nonce)
var didThrow = false
do {
_ = try XChaCha20Poly1305Compat.open(
ciphertext: sealed.ciphertext,
tag: sealed.tag,
key: wrongKey,
nonce24: nonce
)
} catch {
didThrow = true
}
#expect(didThrow)
}
@Test func openFailsWithTamperedCiphertext() throws {
let plaintext = "Secret".data(using: .utf8)!
let key = Data(repeating: 0x42, count: 32)
let nonce = Data(repeating: 0x24, count: 24)
let sealed = try XChaCha20Poly1305Compat.seal(plaintext: plaintext, key: key, nonce24: nonce)
// Create tampered ciphertext by changing first byte
var tamperedBytes = [UInt8](sealed.ciphertext)
tamperedBytes[0] = tamperedBytes[0] ^ 0xFF
let tampered = Data(tamperedBytes)
var didThrow = false
do {
_ = try XChaCha20Poly1305Compat.open(
ciphertext: tampered,
tag: sealed.tag,
key: key,
nonce24: nonce
)
} catch {
didThrow = true
}
#expect(didThrow)
}
}