Remove dead code and simplify codebase (#436)

* refactor: remove dead code and consolidate system messages

- Delete 3 unused functions in ShareViewController (36 lines)
- Extract addSystemMessage() helper to eliminate duplication (120+ lines)
- Remove 22 'let _ =' wasteful computations across multiple files
- Net reduction of 215 lines of dead/duplicate code
- Improves maintainability and reduces technical debt

* fix: remove remaining unused variables to eliminate compiler warnings

- Remove unused senderNoiseKey in ChatViewModel
- Remove unused lastSuccess variable in BluetoothMeshService
- Eliminates all compiler warnings related to unused values

* refactor: remove all dead legacy and migration code

- Remove unused migrateSession() functions (never called in production)
  - NoiseSession.migrateSession() - 13 lines
  - NoiseEncryptionService.migratePeerSession() - 18 lines
  - Test for migration functionality - 17 lines

- Remove unnecessary keychain cleanup code (no legacy data existed)
  - cleanupLegacyKeychainItems() - 62 lines
  - aggressiveCleanupLegacyItems() - 72 lines
  - resetCleanupFlag() - 4 lines
  - Simplified panic mode to just use deleteAllKeychainData()

Total removed: 194 lines of dead/unnecessary code

Analysis revealed:
- KeychainManager introduced July 5, 2025
- Cleanup code added July 15, 2025 (10 days later)
- Legacy service names were never used in production
- Migration functions were incomplete implementation never called
- Peer ID rotation remains active (not legacy)

* Remove dead code and simplify codebase

- Remove unused BinaryEncodable protocol and BinaryMessageType enum
- Delete MockNoiseSession.swift (never used in tests)
- Remove all relay detection code (hardcoded to false)
  - Removed isRelayConnected property from BitchatPeer
  - Removed relayConnected case from ConnectionState enum
  - Cleaned up relay-related UI indicators in ContentView
  - Removed relay status checks from ChatViewModel
- Simplified peer connection logic by removing relay layer

Total: 169 lines removed across 5 files

---------

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
This commit is contained in:
jack
2025-08-12 11:33:08 +02:00
committed by GitHub
co-authored by jack
parent 7a7c89e689
commit 275f0ebaaf
13 changed files with 57 additions and 626 deletions
-101
View File
@@ -1,101 +0,0 @@
//
// MockNoiseSession.swift
// bitchatTests
//
// This is free and unencumbered software released into the public domain.
// For more information, see <https://unlicense.org>
//
import Foundation
import CryptoKit
@testable import bitchat
class MockNoiseSession: NoiseSession {
var mockState: NoiseSessionState = .uninitialized
var shouldFailHandshake = false
var shouldFailEncryption = false
var handshakeMessages: [Data] = []
var encryptedData: [Data] = []
var decryptedData: [Data] = []
override func getState() -> NoiseSessionState {
return mockState
}
override func isEstablished() -> Bool {
return mockState == .established
}
override func startHandshake() throws -> Data {
if shouldFailHandshake {
mockState = .failed(NoiseSessionError.handshakeFailed(TestError.testFailure("Mock handshake failure")))
throw NoiseSessionError.handshakeFailed(TestError.testFailure("Mock handshake failure"))
}
mockState = .handshaking
let handshakeData = TestHelpers.generateRandomData(length: 32)
handshakeMessages.append(handshakeData)
return handshakeData
}
override func processHandshakeMessage(_ message: Data) throws -> Data? {
if shouldFailHandshake {
mockState = .failed(NoiseSessionError.handshakeFailed(TestError.testFailure("Mock handshake failure")))
throw NoiseSessionError.handshakeFailed(TestError.testFailure("Mock handshake failure"))
}
handshakeMessages.append(message)
// Simulate handshake completion after 2 messages
if handshakeMessages.count >= 2 {
mockState = .established
return nil
} else {
let response = TestHelpers.generateRandomData(length: 48)
handshakeMessages.append(response)
return response
}
}
override func encrypt(_ plaintext: Data) throws -> Data {
if shouldFailEncryption {
throw NoiseSessionError.notEstablished
}
guard mockState == .established else {
throw NoiseSessionError.notEstablished
}
// Simple mock encryption: prepend magic bytes and append the data
var encrypted = Data([0xDE, 0xAD, 0xBE, 0xEF])
encrypted.append(plaintext)
encryptedData.append(encrypted)
return encrypted
}
override func decrypt(_ ciphertext: Data) throws -> Data {
if shouldFailEncryption {
throw NoiseSessionError.notEstablished
}
guard mockState == .established else {
throw NoiseSessionError.notEstablished
}
// Simple mock decryption: remove magic bytes
guard ciphertext.count > 4 else {
throw TestError.testFailure("Invalid ciphertext")
}
let plaintext = ciphertext.dropFirst(4)
decryptedData.append(plaintext)
return plaintext
}
override func reset() {
mockState = .uninitialized
handshakeMessages.removeAll()
encryptedData.removeAll()
decryptedData.removeAll()
}
}
@@ -226,23 +226,6 @@ final class NoiseProtocolTests: XCTestCase {
XCTAssertEqual(decrypted, plaintext)
}
func testSessionMigration() throws {
let manager = NoiseSessionManager(localStaticKey: aliceKey)
// Create and establish a session
_ = try manager.initiateHandshake(with: TestConstants.testPeerID2)
// Migrate to new peer ID
let newPeerID = TestConstants.testPeerID3
manager.migrateSession(from: TestConstants.testPeerID2, to: newPeerID)
// Old peer ID should not have session
XCTAssertNil(manager.getSession(for: TestConstants.testPeerID2))
// New peer ID should have the session
XCTAssertNotNil(manager.getSession(for: newPeerID))
}
// MARK: - Security Tests
func testTamperedCiphertextDetection() throws {