From 8b0141596ceb8ea2625023c29acccb3085c44bdd Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 26 Sep 2019 11:32:47 +1000 Subject: [PATCH] Partially implement storage API --- Signal.xcodeproj/project.pbxproj | 12 +-- Signal/src/Loki/NewConversationVC.swift | 2 +- ...nQRCodeViewController.h => ScanQRCodeVC.h} | 2 +- ...nQRCodeViewController.m => ScanQRCodeVC.m} | 6 +- Signal/src/Signal-Bridging-Header.h | 2 +- .../translations/en.lproj/Localizable.strings | 2 + .../API/Group Chat/LokiGroupChatAPI.swift | 80 +------------------ SignalServiceKit/src/Loki/API/LokiAPI.swift | 6 +- .../src/Loki/API/LokiDotNetAPI.swift | 80 +++++++++++++++++++ .../src/Loki/API/LokiStorageAPI.swift | 55 ++++++++++--- .../Loki/API/Multi Device/DeviceLink.swift | 8 ++ .../Database/OWSPrimaryStorage+Loki.swift | 6 +- 12 files changed, 151 insertions(+), 110 deletions(-) rename Signal/src/Loki/{ScanQRCodeViewController.h => ScanQRCodeVC.h} (80%) rename Signal/src/Loki/{ScanQRCodeViewController.m => ScanQRCodeVC.m} (95%) create mode 100644 SignalServiceKit/src/Loki/API/LokiDotNetAPI.swift diff --git a/Signal.xcodeproj/project.pbxproj b/Signal.xcodeproj/project.pbxproj index a998b22ad..c86a729c3 100644 --- a/Signal.xcodeproj/project.pbxproj +++ b/Signal.xcodeproj/project.pbxproj @@ -563,7 +563,7 @@ B8162F0522892C5F00D46544 /* FriendRequestViewDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8162F0422892C5F00D46544 /* FriendRequestViewDelegate.swift */; }; B821F2F82272CED3002C88C0 /* DisplayNameVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B821F2F72272CED3002C88C0 /* DisplayNameVC.swift */; }; B821F2FA2272CEEE002C88C0 /* SeedVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B821F2F92272CEEE002C88C0 /* SeedVC.swift */; }; - B8258493230FA5E9001B41CB /* ScanQRCodeViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = B8258492230FA5E9001B41CB /* ScanQRCodeViewController.m */; }; + B8258493230FA5E9001B41CB /* ScanQRCodeVC.m in Sources */ = {isa = PBXBuildFile; fileRef = B8258492230FA5E9001B41CB /* ScanQRCodeVC.m */; }; B82584A02315024B001B41CB /* RSSFeedPoller.swift in Sources */ = {isa = PBXBuildFile; fileRef = B825849F2315024B001B41CB /* RSSFeedPoller.swift */; }; B845B4D4230CD09100D759F0 /* GroupChatPoller.swift in Sources */ = {isa = PBXBuildFile; fileRef = B845B4D3230CD09000D759F0 /* GroupChatPoller.swift */; }; B846365B22B7418B00AF1514 /* Identicon+ObjC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B846365A22B7418B00AF1514 /* Identicon+ObjC.swift */; }; @@ -1371,8 +1371,8 @@ B8162F0422892C5F00D46544 /* FriendRequestViewDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FriendRequestViewDelegate.swift; sourceTree = ""; }; B821F2F72272CED3002C88C0 /* DisplayNameVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisplayNameVC.swift; sourceTree = ""; }; B821F2F92272CEEE002C88C0 /* SeedVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeedVC.swift; sourceTree = ""; }; - B8258491230FA5DA001B41CB /* ScanQRCodeViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ScanQRCodeViewController.h; sourceTree = ""; }; - B8258492230FA5E9001B41CB /* ScanQRCodeViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ScanQRCodeViewController.m; sourceTree = ""; }; + B8258491230FA5DA001B41CB /* ScanQRCodeVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ScanQRCodeVC.h; sourceTree = ""; }; + B8258492230FA5E9001B41CB /* ScanQRCodeVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ScanQRCodeVC.m; sourceTree = ""; }; B825849F2315024B001B41CB /* RSSFeedPoller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RSSFeedPoller.swift; sourceTree = ""; }; B845B4D3230CD09000D759F0 /* GroupChatPoller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroupChatPoller.swift; sourceTree = ""; }; B846365A22B7418B00AF1514 /* Identicon+ObjC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Identicon+ObjC.swift"; sourceTree = ""; }; @@ -2649,8 +2649,8 @@ B8162F0422892C5F00D46544 /* FriendRequestViewDelegate.swift */, 24A830A12293CD0100F4CAC0 /* LokiP2PServer.swift */, B89841E222B7579F00B1BDC6 /* NewConversationVC.swift */, - B8258491230FA5DA001B41CB /* ScanQRCodeViewController.h */, - B8258492230FA5E9001B41CB /* ScanQRCodeViewController.m */, + B8258491230FA5DA001B41CB /* ScanQRCodeVC.h */, + B8258492230FA5E9001B41CB /* ScanQRCodeVC.m */, ); path = Loki; sourceTree = ""; @@ -3922,7 +3922,7 @@ B86BD08423399ACF000F5AE3 /* Modal.swift in Sources */, 34D1F0861F8678AA0066283D /* ConversationViewController.m in Sources */, 3427C64320F500E000EEC730 /* OWSMessageTimerView.m in Sources */, - B8258493230FA5E9001B41CB /* ScanQRCodeViewController.m in Sources */, + B8258493230FA5E9001B41CB /* ScanQRCodeVC.m in Sources */, B90418E6183E9DD40038554A /* DateUtil.m in Sources */, 3448E15E221333F5004B052E /* OnboardingController.swift in Sources */, 340FC8BD204DAC8D007AEB0F /* ShowGroupMembersViewController.m in Sources */, diff --git a/Signal/src/Loki/NewConversationVC.swift b/Signal/src/Loki/NewConversationVC.swift index 5430ab13b..7fd3fcfe4 100644 --- a/Signal/src/Loki/NewConversationVC.swift +++ b/Signal/src/Loki/NewConversationVC.swift @@ -81,7 +81,7 @@ final class NewConversationVC : OWSViewController, OWSQRScannerDelegate { @objc private func scanQRCode() { ows_ask(forCameraPermissions: { [weak self] hasCameraAccess in if hasCameraAccess { - let scanQRCodeVC = ScanQRCodeViewController() + let scanQRCodeVC = ScanQRCodeVC() scanQRCodeVC.delegate = self self?.navigationController!.pushViewController(scanQRCodeVC, animated: true) } else { diff --git a/Signal/src/Loki/ScanQRCodeViewController.h b/Signal/src/Loki/ScanQRCodeVC.h similarity index 80% rename from Signal/src/Loki/ScanQRCodeViewController.h rename to Signal/src/Loki/ScanQRCodeVC.h index e0af91258..c4a50a564 100644 --- a/Signal/src/Loki/ScanQRCodeViewController.h +++ b/Signal/src/Loki/ScanQRCodeVC.h @@ -3,7 +3,7 @@ NS_ASSUME_NONNULL_BEGIN -@interface ScanQRCodeViewController : OWSViewController +@interface ScanQRCodeVC : OWSViewController @property (nonatomic, weak) UIViewController *delegate; diff --git a/Signal/src/Loki/ScanQRCodeViewController.m b/Signal/src/Loki/ScanQRCodeVC.m similarity index 95% rename from Signal/src/Loki/ScanQRCodeViewController.m rename to Signal/src/Loki/ScanQRCodeVC.m index 744d1f6d5..c035dd0e3 100644 --- a/Signal/src/Loki/ScanQRCodeViewController.m +++ b/Signal/src/Loki/ScanQRCodeVC.m @@ -1,15 +1,15 @@ -#import "ScanQRCodeViewController.h" +#import "ScanQRCodeVC.h" #import "Session-Swift.h" NS_ASSUME_NONNULL_BEGIN -@interface ScanQRCodeViewController () +@interface ScanQRCodeVC () @property (nonatomic) OWSQRCodeScanningViewController *qrCodeScanningVC; @end -@implementation ScanQRCodeViewController +@implementation ScanQRCodeVC - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } diff --git a/Signal/src/Signal-Bridging-Header.h b/Signal/src/Signal-Bridging-Header.h index d314ddbdb..4e2f57b67 100644 --- a/Signal/src/Signal-Bridging-Header.h +++ b/Signal/src/Signal-Bridging-Header.h @@ -44,7 +44,7 @@ #import "PrivacySettingsTableViewController.h" #import "ProfileViewController.h" #import "RemoteVideoView.h" -#import "ScanQRCodeViewController.h" +#import "ScanQRCodeVC.h" #import "SignalApp.h" #import "UIViewController+Permissions.h" #import "ViewControllerUtils.h" diff --git a/Signal/translations/en.lproj/Localizable.strings b/Signal/translations/en.lproj/Localizable.strings index f4d6d41f2..46197c3f6 100644 --- a/Signal/translations/en.lproj/Localizable.strings +++ b/Signal/translations/en.lproj/Localizable.strings @@ -2632,3 +2632,5 @@ "Authorize" = "Authorize"; "Enter the Other Device's Public Key" = "Enter the Other Device's Public Key"; "This is your personal secret. It can be used to restore your account if you lose access, or to migrate to a new device." = "This is your personal secret. It can be used to restore your account if you lose access, or to migrate to a new device."; +"Device Link Authorized" = "Device Link Authorized"; +"Your device has been linked successfully" = "Your device has been linked successfully"; diff --git a/SignalServiceKit/src/Loki/API/Group Chat/LokiGroupChatAPI.swift b/SignalServiceKit/src/Loki/API/Group Chat/LokiGroupChatAPI.swift index f7102de9c..25cab98f0 100644 --- a/SignalServiceKit/src/Loki/API/Group Chat/LokiGroupChatAPI.swift +++ b/SignalServiceKit/src/Loki/API/Group Chat/LokiGroupChatAPI.swift @@ -1,9 +1,7 @@ import PromiseKit @objc(LKGroupChatAPI) -public final class LokiGroupChatAPI : NSObject { - private static let storage = OWSPrimaryStorage.shared() - +public final class LokiGroupChatAPI : LokiDotNetAPI { private static var moderators: [String:[UInt64:Set]] = [:] // Server URL to (channel ID to set of moderator IDs) // MARK: Settings @@ -24,38 +22,11 @@ public final class LokiGroupChatAPI : NSObject { return SSKEnvironment.shared.contactsManager.displayName(forPhoneIdentifier: userHexEncodedPublicKey) ?? "Anonymous" } - private static var userKeyPair: ECKeyPair { - return OWSIdentityManager.shared().identityKeyPair()! - } - - private static var userHexEncodedPublicKey: String { - return userKeyPair.hexEncodedPublicKey - } - - // MARK: Error - public enum Error : Swift.Error { - case parsingFailed, decryptionFailed - } - // MARK: Database - private static let authTokenCollection = "LokiGroupChatAuthTokenCollection" + override internal class var authTokenCollection: String { "LokiGroupChatAuthTokenCollection" } private static let lastMessageServerIDCollection = "LokiGroupChatLastMessageServerIDCollection" private static let lastDeletionServerIDCollection = "LokiGroupChatLastDeletionServerIDCollection" - private static func getAuthTokenFromDatabase(for server: String) -> String? { - var result: String? = nil - storage.dbReadConnection.read { transaction in - result = transaction.object(forKey: server, inCollection: authTokenCollection) as! String? - } - return result - } - - private static func setAuthToken(for server: String, to newValue: String) { - storage.dbReadWriteConnection.readWrite { transaction in - transaction.setObject(newValue, forKey: server, inCollection: authTokenCollection) - } - } - private static func getLastMessageServerID(for group: UInt64, on server: String) -> UInt? { var result: UInt? = nil storage.dbReadConnection.read { transaction in @@ -83,53 +54,6 @@ public final class LokiGroupChatAPI : NSObject { transaction.setObject(newValue, forKey: "\(server).\(group)", inCollection: lastDeletionServerIDCollection) } } - - // MARK: Lifecycle - override private init() { } - - // MARK: Private API - private static func requestNewAuthToken(for server: String) -> Promise { - print("[Loki] Requesting group chat auth token for server: \(server).") - let queryParameters = "pubKey=\(userHexEncodedPublicKey)" - let url = URL(string: "\(server)/loki/v1/get_challenge?\(queryParameters)")! - let request = TSRequest(url: url) - return TSNetworkManager.shared().makePromise(request: request).map { $0.responseObject }.map { rawResponse in - guard let json = rawResponse as? JSON, let base64EncodedChallenge = json["cipherText64"] as? String, let base64EncodedServerPublicKey = json["serverPubKey64"] as? String, - let challenge = Data(base64Encoded: base64EncodedChallenge), var serverPublicKey = Data(base64Encoded: base64EncodedServerPublicKey) else { - throw Error.parsingFailed - } - // Discard the "05" prefix if needed - if (serverPublicKey.count == 33) { - let hexEncodedServerPublicKey = serverPublicKey.hexadecimalString - serverPublicKey = Data.data(fromHex: hexEncodedServerPublicKey.substring(from: 2))! - } - // The challenge is prefixed by the 16 bit IV - guard let tokenAsData = try? DiffieHellman.decrypt(challenge, publicKey: serverPublicKey, privateKey: userKeyPair.privateKey), - let token = String(bytes: tokenAsData, encoding: .utf8) else { - throw Error.decryptionFailed - } - return token - } - } - - private static func submitAuthToken(_ token: String, for server: String) -> Promise { - print("[Loki] Submitting group chat auth token for server: \(server).") - let url = URL(string: "\(server)/loki/v1/submit_challenge")! - let parameters = [ "pubKey" : userHexEncodedPublicKey, "token" : token ] - let request = TSRequest(url: url, method: "POST", parameters: parameters) - return TSNetworkManager.shared().makePromise(request: request).map { _ in token } - } - - private static func getAuthToken(for server: String) -> Promise { - if let token = getAuthTokenFromDatabase(for: server) { - return Promise.value(token) - } else { - return requestNewAuthToken(for: server).then { submitAuthToken($0, for: server) }.map { token -> String in - setAuthToken(for: server, to: token) - return token - } - } - } // MARK: Public API public static func getMessages(for group: UInt64, on server: String) -> Promise<[LokiGroupMessage]> { diff --git a/SignalServiceKit/src/Loki/API/LokiAPI.swift b/SignalServiceKit/src/Loki/API/LokiAPI.swift index 6939d42e5..f471d54f4 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI.swift @@ -3,12 +3,10 @@ import PromiseKit @objc(LKAPI) public final class LokiAPI : NSObject { private static var lastDeviceLinkUpdate: [String:Date] = [:] // Hex encoded public key to date - internal static let storage = OWSPrimaryStorage.shared() // MARK: Convenience - internal static var userHexEncodedPublicKey: String { - return OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey - } + internal static let storage = OWSPrimaryStorage.shared() + internal static let userHexEncodedPublicKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey // MARK: Settings private static let version = "v1" diff --git a/SignalServiceKit/src/Loki/API/LokiDotNetAPI.swift b/SignalServiceKit/src/Loki/API/LokiDotNetAPI.swift new file mode 100644 index 000000000..3729dff44 --- /dev/null +++ b/SignalServiceKit/src/Loki/API/LokiDotNetAPI.swift @@ -0,0 +1,80 @@ +import PromiseKit + +public class LokiDotNetAPI : NSObject { + + // MARK: Convenience + private static let userKeyPair = OWSIdentityManager.shared().identityKeyPair()! + internal static let storage = OWSPrimaryStorage.shared() + internal static let userHexEncodedPublicKey = userKeyPair.hexEncodedPublicKey + + // MARK: Error + public enum Error : Swift.Error { + case parsingFailed, decryptionFailed + } + + // MARK: Database + /// To be overridden by subclasses. + internal class var authTokenCollection: String { preconditionFailure("authTokenCollection is abstract and must be overridden.") } + + private static func getAuthTokenFromDatabase(for server: String) -> String? { + var result: String? = nil + storage.dbReadConnection.read { transaction in + result = transaction.object(forKey: server, inCollection: authTokenCollection) as! String? + } + return result + } + + private static func setAuthToken(for server: String, to newValue: String) { + storage.dbReadWriteConnection.readWrite { transaction in + transaction.setObject(newValue, forKey: server, inCollection: authTokenCollection) + } + } + + // MARK: Lifecycle + override private init() { } + + // MARK: Internal API + internal static func getAuthToken(for server: String) -> Promise { + if let token = getAuthTokenFromDatabase(for: server) { + return Promise.value(token) + } else { + return requestNewAuthToken(for: server).then { submitAuthToken($0, for: server) }.map { token -> String in + setAuthToken(for: server, to: token) + return token + } + } + } + + // MARK: Private API + private static func requestNewAuthToken(for server: String) -> Promise { + print("[Loki] Requesting auth token for server: \(server).") + let queryParameters = "pubKey=\(userHexEncodedPublicKey)" + let url = URL(string: "\(server)/loki/v1/get_challenge?\(queryParameters)")! + let request = TSRequest(url: url) + return TSNetworkManager.shared().makePromise(request: request).map { $0.responseObject }.map { rawResponse in + guard let json = rawResponse as? JSON, let base64EncodedChallenge = json["cipherText64"] as? String, let base64EncodedServerPublicKey = json["serverPubKey64"] as? String, + let challenge = Data(base64Encoded: base64EncodedChallenge), var serverPublicKey = Data(base64Encoded: base64EncodedServerPublicKey) else { + throw Error.parsingFailed + } + // Discard the "05" prefix if needed + if (serverPublicKey.count == 33) { + let hexEncodedServerPublicKey = serverPublicKey.hexadecimalString + serverPublicKey = Data.data(fromHex: hexEncodedServerPublicKey.substring(from: 2))! + } + // The challenge is prefixed by the 16 bit IV + guard let tokenAsData = try? DiffieHellman.decrypt(challenge, publicKey: serverPublicKey, privateKey: userKeyPair.privateKey), + let token = String(bytes: tokenAsData, encoding: .utf8) else { + throw Error.decryptionFailed + } + return token + } + } + + private static func submitAuthToken(_ token: String, for server: String) -> Promise { + print("[Loki] Submitting auth token for server: \(server).") + let url = URL(string: "\(server)/loki/v1/submit_challenge")! + let parameters = [ "pubKey" : userHexEncodedPublicKey, "token" : token ] + let request = TSRequest(url: url, method: "POST", parameters: parameters) + return TSNetworkManager.shared().makePromise(request: request).map { _ in token } + } +} diff --git a/SignalServiceKit/src/Loki/API/LokiStorageAPI.swift b/SignalServiceKit/src/Loki/API/LokiStorageAPI.swift index c21bd16ca..d52ba0663 100644 --- a/SignalServiceKit/src/Loki/API/LokiStorageAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiStorageAPI.swift @@ -1,25 +1,54 @@ import PromiseKit @objc(LKStorageAPI) -public final class LokiStorageAPI : NSObject { - - // MARK: Lifecycle - override private init() { } +public final class LokiStorageAPI : LokiDotNetAPI { + + // MARK: Settings + private static let server = "" + + // MARK: Database + override internal class var authTokenCollection: String { return "LokiStorageAuthTokenCollection" } // MARK: Public API + /// Adds the given device link to the user's device mapping on the server. public static func addDeviceLink(_ deviceLink: DeviceLink) -> Promise { - // Adds the given device link to the user's device mapping on the server - notImplemented() + var deviceLinks: Set = [] + storage.dbReadConnection.read { transaction in + deviceLinks = storage.getDeviceLinks(for: userHexEncodedPublicKey, in: transaction) + } + deviceLinks.insert(deviceLink) + return setDeviceLinks(deviceLinks) } - + + /// Removes the given device link from the user's device mapping on the server. public static func removeDeviceLink(_ deviceLink: DeviceLink) -> Promise { - // Removes the given device link from the user's device mapping on the server - notImplemented() + var deviceLinks: Set = [] + storage.dbReadConnection.read { transaction in + deviceLinks = storage.getDeviceLinks(for: userHexEncodedPublicKey, in: transaction) + } + deviceLinks.remove(deviceLink) + return setDeviceLinks(deviceLinks) } - + + /// Gets the device links associated with the given hex encoded public key from the + /// server and stores and returns the valid ones. public static func getDeviceLinks(associatedWith hexEncodedPublicKey: String) -> Promise> { - // Gets the device links associated with the given hex encoded public key from the - // server and stores and returns the valid ones - return Promise.value(Set()) + return Promise.value(Set()) // TODO: Implement + } + + // MARK: Private API + public static func setDeviceLinks(_ deviceLinks: Set) -> Promise { + return getAuthToken(for: server).then { token -> Promise in + let isMaster = deviceLinks.contains { $0.master.hexEncodedPublicKey == userHexEncodedPublicKey } + let deviceLinksAsJSON = deviceLinks.map { $0.toJSON() } + let value = !deviceLinksAsJSON.isEmpty ? [ "isPrimary" : isMaster ? 1 : 0, "authorisations" : deviceLinksAsJSON ] : nil + let annotation: JSON = [ "type" : "network.loki.messenger.devicemapping", "value" : value ] + let parameters: JSON = [ "annotations" : [ annotation ] ] + let url = URL(string: "\(server)/users/me")! + let request = TSRequest(url: url, method: "PATCH", parameters: parameters) + request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ] + return TSNetworkManager.shared().makePromise(request: request).map { _ in } + } + } } diff --git a/SignalServiceKit/src/Loki/API/Multi Device/DeviceLink.swift b/SignalServiceKit/src/Loki/API/Multi Device/DeviceLink.swift index 8d45e1847..c194c5790 100644 --- a/SignalServiceKit/src/Loki/API/Multi Device/DeviceLink.swift +++ b/SignalServiceKit/src/Loki/API/Multi Device/DeviceLink.swift @@ -58,6 +58,14 @@ public final class DeviceLink : NSObject, NSCoding { coder.encode(master, forKey: "master") coder.encode(slave, forKey: "slave") } + + // MARK: JSON + public func toJSON() -> JSON { + var result = [ "primaryDevicePubKey" : master.hexEncodedPublicKey, "secondaryDevicePubKey" : slave.hexEncodedPublicKey ] + if let masterSignature = master.signature { result["grantSignature"] = masterSignature.base64EncodedString() } + if let slaveSignature = slave.signature { result["requestSignature"] = slaveSignature.base64EncodedString() } + return result + } // MARK: Equality @objc override public func isEqual(_ other: Any?) -> Bool { diff --git a/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.swift b/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.swift index 844598d31..6853a9c02 100644 --- a/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.swift +++ b/SignalServiceKit/src/Loki/Database/OWSPrimaryStorage+Loki.swift @@ -10,12 +10,12 @@ extension OWSPrimaryStorage { transaction.setObject(deviceLink, forKey: deviceLink.slave.hexEncodedPublicKey, inCollection: collection) } - public func getDeviceLinks(for masterHexEncodedPublicKey: String, in transaction: YapDatabaseReadTransaction) -> [DeviceLink] { + public func getDeviceLinks(for masterHexEncodedPublicKey: String, in transaction: YapDatabaseReadTransaction) -> Set { let collection = getCollection(for: masterHexEncodedPublicKey) - var result: [DeviceLink] = [] + var result: Set = [] transaction.enumerateRows(inCollection: collection) { _, object, _, _ in guard let deviceLink = object as? DeviceLink else { return } - result.append(deviceLink) + result.insert(deviceLink) } return result }