From 7b20bdf821ba5c32139ae953f9937b50d3522ad3 Mon Sep 17 00:00:00 2001 From: islam <2553451+qalandarov@users.noreply.github.com> Date: Thu, 18 Sep 2025 01:49:37 +0100 Subject: [PATCH] Unify `PeerIDUtils` & `PeerIDResolver` in `Peer` --- bitchat/Models/Peer.swift | 20 +++++++++++++++++++ bitchat/Nostr/NostrEmbeddedBitChat.swift | 2 +- bitchat/Protocols/PeerID.swift | 14 ------------- bitchat/Services/BLEService.swift | 16 +++------------ .../FavoritesPersistenceService.swift | 5 ++--- bitchat/Services/MessageRouter.swift | 4 ++-- bitchat/Utils/PeerIDResolver.swift | 12 ----------- 7 files changed, 28 insertions(+), 45 deletions(-) delete mode 100644 bitchat/Protocols/PeerID.swift delete mode 100644 bitchat/Utils/PeerIDResolver.swift diff --git a/bitchat/Models/Peer.swift b/bitchat/Models/Peer.swift index eee9ae12..07c0b52c 100644 --- a/bitchat/Models/Peer.swift +++ b/bitchat/Models/Peer.swift @@ -7,6 +7,7 @@ // import Foundation +import struct CryptoKit.SHA256 struct Peer: Equatable, Hashable { let id: String @@ -111,3 +112,22 @@ extension Peer { self.init(str: str) } } + +// MARK: - Noise Public Key Helpers + +extension Peer { + /// Derive the stable 16-hex peer ID from a Noise static public key + init(publicKey: Data) { + let digest = SHA256.hash(data: publicKey) + let hex = digest.map { String(format: "%02x", $0) }.joined() + self.init(str: hex.prefix(16)) + } + + /// Returns a 16-hex short peer ID derived from a 64-hex Noise public key if needed + func toShort() -> Peer { + if id.count == Constants.maxIDLength, let data = Data(hexString: id) { + return Peer(publicKey: data) + } + return self + } +} diff --git a/bitchat/Nostr/NostrEmbeddedBitChat.swift b/bitchat/Nostr/NostrEmbeddedBitChat.swift index 44807eff..725fbeea 100644 --- a/bitchat/Nostr/NostrEmbeddedBitChat.swift +++ b/bitchat/Nostr/NostrEmbeddedBitChat.swift @@ -100,7 +100,7 @@ struct NostrEmbeddedBitChat { if let maybeData = Data(hexString: recipientPeerID) { if maybeData.count == 32 { // Treat as Noise static public key; derive peerID from fingerprint - return PeerIDUtils.derivePeerID(fromPublicKey: maybeData) + return Peer(publicKey: maybeData).id } else if maybeData.count == 8 { // Already an 8-byte peer ID return recipientPeerID diff --git a/bitchat/Protocols/PeerID.swift b/bitchat/Protocols/PeerID.swift deleted file mode 100644 index f77bcff4..00000000 --- a/bitchat/Protocols/PeerID.swift +++ /dev/null @@ -1,14 +0,0 @@ -import Foundation -import CryptoKit - -// MARK: - Peer ID Utilities - -struct PeerIDUtils { - /// Derive the stable 16-hex peer ID from a Noise static public key - static func derivePeerID(fromPublicKey publicKey: Data) -> String { - let digest = SHA256.hash(data: publicKey) - let hex = digest.map { String(format: "%02x", $0) }.joined() - return String(hex.prefix(16)) - } -} - diff --git a/bitchat/Services/BLEService.swift b/bitchat/Services/BLEService.swift index 33ac41fd..60102bc3 100644 --- a/bitchat/Services/BLEService.swift +++ b/bitchat/Services/BLEService.swift @@ -556,23 +556,13 @@ final class BLEService: NSObject { func isPeerConnected(_ peerID: String) -> Bool { // Accept both 16-hex short IDs and 64-hex Noise keys - let shortID: String = { - if peerID.count == 64, let key = Data(hexString: peerID) { - return PeerIDUtils.derivePeerID(fromPublicKey: key) - } - return peerID - }() + let shortID = Peer(str: peerID).toShort().id return collectionsQueue.sync { peers[shortID]?.isConnected ?? false } } func isPeerReachable(_ peerID: String) -> Bool { // Accept both 16-hex short IDs and 64-hex Noise keys - let shortID: String = { - if peerID.count == 64, let key = Data(hexString: peerID) { - return PeerIDUtils.derivePeerID(fromPublicKey: key) - } - return peerID - }() + let shortID = Peer(str: peerID).toShort().id return collectionsQueue.sync { // Must be mesh-attached: at least one live direct link to the mesh let meshAttached = peers.values.contains { $0.isConnected } @@ -1518,7 +1508,7 @@ final class BLEService: NSObject { // Verify that the sender's derived ID from the announced noise public key matches the packet senderID // This helps detect relayed or spoofed announces. Only warn in release; assert in debug. - let derivedFromKey = PeerIDUtils.derivePeerID(fromPublicKey: announcement.noisePublicKey) + let derivedFromKey = Peer(publicKey: announcement.noisePublicKey).id if derivedFromKey != peerID { SecureLogger.warning("⚠️ Announce sender mismatch: derived \(derivedFromKey.prefix(8))… vs packet \(peerID.prefix(8))…", category: .security) diff --git a/bitchat/Services/FavoritesPersistenceService.swift b/bitchat/Services/FavoritesPersistenceService.swift index 21e7be5d..effbf41e 100644 --- a/bitchat/Services/FavoritesPersistenceService.swift +++ b/bitchat/Services/FavoritesPersistenceService.swift @@ -182,9 +182,8 @@ final class FavoritesPersistenceService: ObservableObject { func getFavoriteStatus(forPeerID peerID: String) -> FavoriteRelationship? { // Quick sanity: peerID should be 16 hex chars (8 bytes) guard peerID.count == 16 else { return nil } - for (pubkey, rel) in favorites { - let derived = PeerIDUtils.derivePeerID(fromPublicKey: pubkey) - if derived == peerID { return rel } + for (pubkey, rel) in favorites where Peer(publicKey: pubkey).id == peerID { + return rel } return nil } diff --git a/bitchat/Services/MessageRouter.swift b/bitchat/Services/MessageRouter.swift index 7835c8dc..63d8a92d 100644 --- a/bitchat/Services/MessageRouter.swift +++ b/bitchat/Services/MessageRouter.swift @@ -21,7 +21,7 @@ final class MessageRouter { ) { [weak self] note in guard let self = self else { return } if let data = note.userInfo?["peerPublicKey"] as? Data { - let peerID = PeerIDUtils.derivePeerID(fromPublicKey: data) + let peerID = Peer(publicKey: data).id Task { @MainActor in self.flushOutbox(for: peerID) } @@ -29,7 +29,7 @@ final class MessageRouter { // Handle key updates if let newKey = note.userInfo?["peerPublicKey"] as? Data, let _ = note.userInfo?["isKeyUpdate"] as? Bool { - let peerID = PeerIDUtils.derivePeerID(fromPublicKey: newKey) + let peerID = Peer(publicKey: newKey).id Task { @MainActor in self.flushOutbox(for: peerID) } diff --git a/bitchat/Utils/PeerIDResolver.swift b/bitchat/Utils/PeerIDResolver.swift deleted file mode 100644 index 151b3aba..00000000 --- a/bitchat/Utils/PeerIDResolver.swift +++ /dev/null @@ -1,12 +0,0 @@ -import Foundation - -struct PeerIDResolver { - /// Returns a 16-hex short peer ID derived from a 64-hex Noise public key if needed - static func toShortID(_ id: String) -> String { - if id.count == 64, let data = Data(hexString: id) { - return PeerIDUtils.derivePeerID(fromPublicKey: data) - } - return id - } -} -