PeerID 7/n: Unify PeerIDUtils & PeerIDResolver (#744)

This commit is contained in:
Islam
2025-10-02 13:41:47 +02:00
committed by GitHub
parent 8cfee095d3
commit f2473f857b
6 changed files with 8 additions and 40 deletions
+1 -1
View File
@@ -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
-9
View File
@@ -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))
}
}
+3 -13
View File
@@ -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)
@@ -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
}
+2 -2
View File
@@ -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)
}
-12
View File
@@ -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
}
}