PeerID 16/n: BitchatPeer / PeerInfo / TransportPeer (#755)

* PeerID 15/n: Bitchat Message & Packet accept in init

* PeerID 16/n: BitchatPeer / PeerInfo / TransportPeer
This commit is contained in:
Islam
2025-10-06 17:17:57 +02:00
committed by GitHub
parent 5f44c56a90
commit 10e3f574ab
9 changed files with 81 additions and 83 deletions
+20 -20
View File
@@ -323,7 +323,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
func getShortIDForNoiseKey(_ fullNoiseKeyHex: String) -> String? {
// Check known peers for a noise key match
if let match = allPeers.first(where: { $0.noisePublicKey.hexEncodedString() == fullNoiseKeyHex }) {
return match.id
return match.peerID.id
}
// Also search cache mapping
if let pair = shortIDToNoiseKey.first(where: { $0.value == fullNoiseKeyHex }) {
@@ -586,10 +586,10 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
var uniquePeers: [String: BitchatPeer] = [:]
for peer in peers {
// Keep the first occurrence of each peer ID
if uniquePeers[peer.id] == nil {
uniquePeers[peer.id] = peer
if uniquePeers[peer.peerID.id] == nil {
uniquePeers[peer.peerID.id] = peer
} else {
SecureLogger.warning("⚠️ Duplicate peer ID detected: \(peer.id) (\(peer.displayName))", category: .session)
SecureLogger.warning("⚠️ Duplicate peer ID detected: \(peer.peerID) (\(peer.displayName))", category: .session)
}
}
self.peerIndex = uniquePeers
@@ -1220,11 +1220,11 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
// Find the ephemeral peer ID for this Noise key
let ephemeralPeerID = unifiedPeerService.peers.first { peer in
peer.noisePublicKey == noisePublicKey
}?.id
}?.peerID
if let ephemeralID = ephemeralPeerID {
// Found the ephemeral peer, use normal toggle
unifiedPeerService.toggleFavorite(ephemeralID)
unifiedPeerService.toggleFavorite(ephemeralID.id)
// Also trigger UI update
objectWillChange.send()
} else {
@@ -3936,8 +3936,8 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
// Build current peer->seed map (excluding self)
let myID = meshService.myPeerID
var currentSeeds: [String: String] = [:]
for p in allPeers where p.id != myID {
currentSeeds[p.id] = meshSeed(for: p.id)
for p in allPeers where p.peerID != myID {
currentSeeds[p.peerID.id] = meshSeed(for: p.peerID.id)
}
// If seeds unchanged and palette exists for both themes, skip
if currentSeeds == peerPaletteSeeds,
@@ -4322,7 +4322,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
// Also log any offline favorites and whether we consider them verified
let offlineFavorites = unifiedPeerService.favorites.filter { !$0.isConnected }
for fav in offlineFavorites {
let fp = unifiedPeerService.getFingerprint(for: fav.id)
let fp = unifiedPeerService.getFingerprint(for: fav.peerID.id)
let isVer = fp.flatMap { verifiedFingerprints.contains($0) } ?? false
let fpShort = fp?.prefix(8) ?? "nil"
SecureLogger.info("⭐️ Favorite offline: \(fav.nickname) fp=\(fpShort) verified=\(isVer)", category: .security)
@@ -4574,24 +4574,24 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
guard let peer = unifiedPeerService.peers.first(where: { $0.noisePublicKey.hexEncodedString().lowercased() == targetNoise }) else {
return false
}
let peerID = peer.id
let peerID = peer.peerID
// If we already have a pending verification with this peer, don't send another
if pendingQRVerifications[peerID] != nil {
if pendingQRVerifications[peerID.id] != nil {
return true
}
// Generate nonceA
var nonce = Data(count: 16)
_ = nonce.withUnsafeMutableBytes { SecRandomCopyBytes(kSecRandomDefault, 16, $0.baseAddress!) }
var pending = PendingVerification(noiseKeyHex: qr.noiseKeyHex, signKeyHex: qr.signKeyHex, nonceA: nonce, startedAt: Date(), sent: false)
pendingQRVerifications[peerID] = pending
pendingQRVerifications[peerID.id] = pending
// If Noise session is established, send immediately; otherwise trigger handshake and send on auth
let noise = meshService.getNoiseService()
if noise.hasEstablishedSession(with: PeerID(str: peerID)) {
meshService.sendVerifyChallenge(to: PeerID(str: peerID), noiseKeyHex: qr.noiseKeyHex, nonceA: nonce)
if noise.hasEstablishedSession(with: peerID) {
meshService.sendVerifyChallenge(to: peerID, noiseKeyHex: qr.noiseKeyHex, nonceA: nonce)
pending.sent = true
pendingQRVerifications[peerID] = pending
pendingQRVerifications[peerID.id] = pending
} else {
meshService.triggerHandshake(with: PeerID(str: peerID))
meshService.triggerHandshake(with: peerID)
}
return true
}
@@ -4766,7 +4766,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
/// Clean up stale unread peer IDs that no longer exist in the peer list
@MainActor
private func cleanupStaleUnreadPeerIDs() {
let currentPeerIDs = Set(unifiedPeerService.peers.map { $0.id })
let currentPeerIDs = Set(unifiedPeerService.peers.map { $0.peerID.id })
let staleIDs = unreadPrivateMessages.subtracting(currentPeerIDs)
if !staleIDs.isEmpty {
@@ -5253,7 +5253,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
@MainActor
private func mirrorToEphemeralIfNeeded(_ message: BitchatMessage, targetPeerID: String, key: Data?) {
guard let key,
let ephemeralPeerID = unifiedPeerService.peers.first(where: { $0.noisePublicKey == key })?.id,
let ephemeralPeerID = unifiedPeerService.peers.first(where: { $0.noisePublicKey == key })?.peerID.id,
ephemeralPeerID != targetPeerID
else {
return
@@ -5290,7 +5290,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
private func handleViewingThisChat(_ message: BitchatMessage, targetPeerID: String, key: Data?, senderPubkey: String) {
unreadPrivateMessages.remove(targetPeerID)
if let key,
let ephemeralPeerID = unifiedPeerService.peers.first(where: { $0.noisePublicKey == key })?.id {
let ephemeralPeerID = unifiedPeerService.peers.first(where: { $0.noisePublicKey == key })?.peerID.id {
unreadPrivateMessages.remove(ephemeralPeerID)
}
if !sentReadReceipts.contains(message.id) {
@@ -5322,7 +5322,7 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
unreadPrivateMessages.insert(targetPeerID)
if let key,
let ephemeralPeerID = unifiedPeerService.peers.first(where: { $0.noisePublicKey == key })?.id,
let ephemeralPeerID = unifiedPeerService.peers.first(where: { $0.noisePublicKey == key })?.peerID.id,
ephemeralPeerID != targetPeerID {
unreadPrivateMessages.insert(ephemeralPeerID)
}