From f2473f857b2e21a73885cfa906427f02a54d8b38 Mon Sep 17 00:00:00 2001 From: Islam <2553451+qalandarov@users.noreply.github.com> Date: Thu, 2 Oct 2025 12:41:47 +0100 Subject: [PATCH] PeerID 7/n: Unify `PeerIDUtils` & `PeerIDResolver` (#744) --- bitchat/Nostr/NostrEmbeddedBitChat.swift | 2 +- bitchat/Protocols/PeerIDUtils.swift | 9 --------- bitchat/Services/BLEService.swift | 16 +++------------- .../Services/FavoritesPersistenceService.swift | 5 ++--- bitchat/Services/MessageRouter.swift | 4 ++-- bitchat/Utils/PeerIDResolver.swift | 12 ------------ 6 files changed, 8 insertions(+), 40 deletions(-) delete mode 100644 bitchat/Protocols/PeerIDUtils.swift delete mode 100644 bitchat/Utils/PeerIDResolver.swift diff --git a/bitchat/Nostr/NostrEmbeddedBitChat.swift b/bitchat/Nostr/NostrEmbeddedBitChat.swift index 44807eff..30f791db 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 PeerID(publicKey: maybeData).id } else if maybeData.count == 8 { // Already an 8-byte peer ID return recipientPeerID diff --git a/bitchat/Protocols/PeerIDUtils.swift b/bitchat/Protocols/PeerIDUtils.swift deleted file mode 100644 index 7a3c345b..00000000 --- a/bitchat/Protocols/PeerIDUtils.swift +++ /dev/null @@ -1,9 +0,0 @@ -import Foundation - -struct PeerIDUtils { - /// Derive the stable 16-hex peer ID from a Noise static public key - static func derivePeerID(fromPublicKey publicKey: Data) -> String { - String(publicKey.sha256Fingerprint().prefix(16)) - } -} - diff --git a/bitchat/Services/BLEService.swift b/bitchat/Services/BLEService.swift index e18fcbd1..1fa8fa69 100644 --- a/bitchat/Services/BLEService.swift +++ b/bitchat/Services/BLEService.swift @@ -615,23 +615,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 = PeerID(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 = PeerID(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 } @@ -1579,7 +1569,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 = PeerID(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 ff4d9e08..3e38c297 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 PeerID(publicKey: pubkey).id == peerID { + return rel } return nil } diff --git a/bitchat/Services/MessageRouter.swift b/bitchat/Services/MessageRouter.swift index 7835c8dc..70c18c37 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 = PeerID(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 = PeerID(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 - } -} -