From 41ce0c34c300b6581624faddf2d8cdbad3d5adf3 Mon Sep 17 00:00:00 2001 From: Brian Plattenburg <5767019+bplattenburg@users.noreply.github.com> Date: Wed, 14 Aug 2024 10:15:06 -0400 Subject: [PATCH] Use peerKeyString (#107) * Update to require minimum Ditto SDK of 4.7.0 so that we can use peerKeyString instead of hashing the peerKey ourselves. * Remove extension we don't need anymore * Update tools app * update package resolved file (although we should consider not checking this in long term) --- .../DittoToolsApp.xcodeproj/project.pbxproj | 2 +- .../xcshareddata/swiftpm/Package.resolved | 8 ++++---- Package.resolved | 10 +++++----- Package.swift | 2 +- Sources/DittoHeartbeat/HeartbeatVM.swift | 12 ++---------- .../DittoPeersList/DittoPeer+Extensions.swift | 9 --------- Sources/DittoPeersList/PeersObserverVM.swift | 2 +- .../PresenceDegradationVM.swift | 18 ++++++------------ 8 files changed, 20 insertions(+), 43 deletions(-) diff --git a/DittoToolsApp/DittoToolsApp.xcodeproj/project.pbxproj b/DittoToolsApp/DittoToolsApp.xcodeproj/project.pbxproj index ec7c4f8..22be025 100644 --- a/DittoToolsApp/DittoToolsApp.xcodeproj/project.pbxproj +++ b/DittoToolsApp/DittoToolsApp.xcodeproj/project.pbxproj @@ -787,7 +787,7 @@ repositoryURL = "https://github.com/getditto/DittoSwiftPackage"; requirement = { kind = upToNextMajorVersion; - minimumVersion = 4.5.0; + minimumVersion = 4.7.0; }; }; /* End XCRemoteSwiftPackageReference section */ diff --git a/DittoToolsApp/DittoToolsApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/DittoToolsApp/DittoToolsApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index df66181..631426e 100644 --- a/DittoToolsApp/DittoToolsApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/DittoToolsApp/DittoToolsApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/getditto/DittoSwiftPackage", "state" : { - "revision" : "7b683558041b2437f5857e5338c590fe02528517", - "version" : "4.5.3" + "revision" : "87b0dd1c4a9bcbe2ae45ce3ad5975c152878b3ea", + "version" : "4.7.4" } }, { @@ -14,8 +14,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-collections", "state" : { - "revision" : "94cf62b3ba8d4bed62680a282d4c25f9c63c2efb", - "version" : "1.1.0" + "revision" : "3d2dc41a01f9e49d84f0a3925fb858bed64f702d", + "version" : "1.1.2" } } ], diff --git a/Package.resolved b/Package.resolved index d17cb7e..631426e 100644 --- a/Package.resolved +++ b/Package.resolved @@ -5,17 +5,17 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/getditto/DittoSwiftPackage", "state" : { - "revision" : "808b636b11ddd129b956cc8d9c71534880a8a1f1", - "version" : "4.7.0" + "revision" : "87b0dd1c4a9bcbe2ae45ce3ad5975c152878b3ea", + "version" : "4.7.4" } }, { "identity" : "swift-collections", "kind" : "remoteSourceControl", - "location" : "https://github.com/apple/swift-collections.git", + "location" : "https://github.com/apple/swift-collections", "state" : { - "revision" : "07e47b1e93e5a1e0ef0c50fcb2d6739fb6be4003", - "version" : "1.0.0" + "revision" : "3d2dc41a01f9e49d84f0a3925fb858bed64f702d", + "version" : "1.1.2" } } ], diff --git a/Package.swift b/Package.swift index 92fd426..5688145 100644 --- a/Package.swift +++ b/Package.swift @@ -53,7 +53,7 @@ let package = Package( targets: ["DittoAllToolsMenu"]), ], dependencies: [ - .package(url: "https://github.com/getditto/DittoSwiftPackage", from: "4.5.0"), + .package(url: "https://github.com/getditto/DittoSwiftPackage", from: "4.7.0"), .package(url: "https://github.com/apple/swift-collections", from: "1.0.0") ], targets: [ diff --git a/Sources/DittoHeartbeat/HeartbeatVM.swift b/Sources/DittoHeartbeat/HeartbeatVM.swift index 9451ba0..f592267 100644 --- a/Sources/DittoHeartbeat/HeartbeatVM.swift +++ b/Sources/DittoHeartbeat/HeartbeatVM.swift @@ -42,7 +42,7 @@ public class HeartbeatVM: ObservableObject { hbInfo = DittoHeartbeatInfo( id: config.id, schema: String._schemaValue, - peerKey: localPeerKeyString, + peerKey: ditto.presence.graph.localPeer.peerKeyString, secondsInterval: config.secondsInterval, sdk: ditto.presence.graph.localPeer.platformSDK, metadata: config.metadata ?? [:] @@ -141,7 +141,7 @@ public class HeartbeatVM: ObservableObject { bluetooth: types[String.bt] as Int? ?? 0, p2pWifi: types[String.p2pWifi] as Int? ?? 0, lan: types[String.lan] as Int? ?? 0, - peerKey: peerKeyHash(peer.peerKey) + peerKey: peer.peerKeyString ) connections.append(cx) } @@ -162,14 +162,6 @@ public class HeartbeatVM: ObservableObject { } return [String.bt: bt, String.p2pWifi: wifi, String.lan: lan] } - - private var localPeerKeyString: String { - peerKeyHash(ditto.presence.graph.localPeer.peerKey) - } - - private func peerKeyHash(_ data: Data) -> String { - "\(String.pk)\(data.base64EncodedString())" - } } diff --git a/Sources/DittoPeersList/DittoPeer+Extensions.swift b/Sources/DittoPeersList/DittoPeer+Extensions.swift index 724078c..608f1e3 100644 --- a/Sources/DittoPeersList/DittoPeer+Extensions.swift +++ b/Sources/DittoPeersList/DittoPeer+Extensions.swift @@ -17,15 +17,6 @@ extension DittoPeer { } return sdk + "N/A" } - - var peerKeyString: String { - Self.toPeerKeyString(self.peerKey) - } - - static func toPeerKeyString(_ data: Data) -> String { - let hash = Insecure.MD5.hash(data: data) - return hash.map { String(format: "%02hhx", $0) }.joined() - } } diff --git a/Sources/DittoPeersList/PeersObserverVM.swift b/Sources/DittoPeersList/PeersObserverVM.swift index 44758e2..7536cb4 100644 --- a/Sources/DittoPeersList/PeersObserverVM.swift +++ b/Sources/DittoPeersList/PeersObserverVM.swift @@ -38,7 +38,7 @@ import Foundation } func isLocalPeer(_ peer: DittoPeer) -> Bool { - peer.peerKey == localPeer.peerKey + peer.peerKeyString == localPeer.peerKeyString } func connectionsWithLocalPeer(_ peer: DittoPeer) -> [DittoConnection] { diff --git a/Sources/DittoPresenceDegradation/PresenceDegradationVM.swift b/Sources/DittoPresenceDegradation/PresenceDegradationVM.swift index 50a84f5..421808f 100644 --- a/Sources/DittoPresenceDegradation/PresenceDegradationVM.swift +++ b/Sources/DittoPresenceDegradation/PresenceDegradationVM.swift @@ -40,34 +40,33 @@ class PresenceDegradationVM: ObservableObject { transportInfo: localPeerTransportInfo, connected: true, lastSeen: Int(seenAt.timeIntervalSince1970), - key: self.hashPeerKeyUseCase(graph.localPeer.peerKey) + key: graph.localPeer.peerKeyString ) for peer in graph.remotePeers { - let hashedPeerKey = self.hashPeerKeyUseCase(peer.peerKey) if var remotePeers = self.remotePeers { - remotePeers[hashedPeerKey] = Peer( + remotePeers[peer.peerKeyString] = Peer( name: peer.deviceName, transportInfo: self.resolveTransportInfo(peer: peer), connected: true, lastSeen: Int(seenAt.timeIntervalSince1970), - key: hashedPeerKey + key: peer.peerKeyString ) self.remotePeers = remotePeers } else { - self.remotePeers = [hashedPeerKey: Peer( + self.remotePeers = [peer.peerKeyString: Peer( name: peer.deviceName, transportInfo: self.resolveTransportInfo(peer: peer), connected: true, lastSeen: Int(seenAt.timeIntervalSince1970), - key: hashedPeerKey + key: peer.peerKeyString )] } } if let peers = self.remotePeers?.values { for peer in peers { - if !graph.remotePeers.contains(where: { self.hashPeerKeyUseCase($0.peerKey) == peer.key}) { + if !graph.remotePeers.contains(where: { $0.peerKeyString == peer.key}) { self.remotePeers?[peer.key]?.connected = false } } @@ -95,11 +94,6 @@ class PresenceDegradationVM: ObservableObject { ) } - func hashPeerKeyUseCase(_ data: Data) -> String { - let base64String = data.base64EncodedString() - return "pk:" + base64String - } - func updateSettings() { var hasSeenExpectedPeers = false