Skip to content

Commit

Permalink
Use peerKeyString (#107)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
bplattenburg authored Aug 14, 2024
1 parent e49e687 commit 41ce0c3
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 43 deletions.
2 changes: 1 addition & 1 deletion DittoToolsApp/DittoToolsApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@
repositoryURL = "https://github.com/getditto/DittoSwiftPackage";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 4.5.0;
minimumVersion = 4.7.0;
};
};
/* End XCRemoteSwiftPackageReference section */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/getditto/DittoSwiftPackage",
"state" : {
"revision" : "7b683558041b2437f5857e5338c590fe02528517",
"version" : "4.5.3"
"revision" : "87b0dd1c4a9bcbe2ae45ce3ad5975c152878b3ea",
"version" : "4.7.4"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections",
"state" : {
"revision" : "94cf62b3ba8d4bed62680a282d4c25f9c63c2efb",
"version" : "1.1.0"
"revision" : "3d2dc41a01f9e49d84f0a3925fb858bed64f702d",
"version" : "1.1.2"
}
}
],
Expand Down
10 changes: 5 additions & 5 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
],
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
12 changes: 2 additions & 10 deletions Sources/DittoHeartbeat/HeartbeatVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? [:]
Expand Down Expand Up @@ -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)
}
Expand All @@ -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())"
}
}


Expand Down
9 changes: 0 additions & 9 deletions Sources/DittoPeersList/DittoPeer+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}


2 changes: 1 addition & 1 deletion Sources/DittoPeersList/PeersObserverVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import Foundation
}

func isLocalPeer(_ peer: DittoPeer) -> Bool {
peer.peerKey == localPeer.peerKey
peer.peerKeyString == localPeer.peerKeyString
}

func connectionsWithLocalPeer(_ peer: DittoPeer) -> [DittoConnection] {
Expand Down
18 changes: 6 additions & 12 deletions Sources/DittoPresenceDegradation/PresenceDegradationVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -95,11 +94,6 @@ class PresenceDegradationVM: ObservableObject {
)
}

func hashPeerKeyUseCase(_ data: Data) -> String {
let base64String = data.base64EncodedString()
return "pk:" + base64String
}

func updateSettings() {
var hasSeenExpectedPeers = false

Expand Down

0 comments on commit 41ce0c3

Please sign in to comment.