From 2e10ccf99d6c81a2399f62e8fafd100ac3131c3b Mon Sep 17 00:00:00 2001 From: Morgan Pretty Date: Tue, 15 Mar 2022 16:06:53 +1100 Subject: [PATCH] Updated the code to cache the retrieval of the user public key --- Session.xcodeproj/project.pbxproj | 8 -------- Session/Settings/NukeDataModal.swift | 5 +++++ SessionMessagingKit/Meta/SessionMessagingKit.h | 1 - SessionMessagingKit/Utilities/General.swift | 18 ++++++++++++++++++ .../Utilities/GeneralUtilities.h | 11 ----------- .../Utilities/GeneralUtilities.m | 16 ---------------- .../NSENotificationPresenter.swift | 2 +- 7 files changed, 24 insertions(+), 37 deletions(-) delete mode 100644 SessionMessagingKit/Utilities/GeneralUtilities.h delete mode 100644 SessionMessagingKit/Utilities/GeneralUtilities.m diff --git a/Session.xcodeproj/project.pbxproj b/Session.xcodeproj/project.pbxproj index 19150c39c..eec7f7dfa 100644 --- a/Session.xcodeproj/project.pbxproj +++ b/Session.xcodeproj/project.pbxproj @@ -241,8 +241,6 @@ B894D0752339EDCF00B4D94D /* NukeDataModal.swift in Sources */ = {isa = PBXBuildFile; fileRef = B894D0742339EDCF00B4D94D /* NukeDataModal.swift */; }; B897621C25D201F7004F83B2 /* ScrollToBottomButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = B897621B25D201F7004F83B2 /* ScrollToBottomButton.swift */; }; B8AE75A425A6C6A6001A84D2 /* Data+Trimming.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8AE75A325A6C6A6001A84D2 /* Data+Trimming.swift */; }; - B8AE760B25ABFB5A001A84D2 /* GeneralUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = B8AE760A25ABFB5A001A84D2 /* GeneralUtilities.m */; }; - B8AE761425ABFBB9001A84D2 /* GeneralUtilities.h in Headers */ = {isa = PBXBuildFile; fileRef = B8AE760925ABFB00001A84D2 /* GeneralUtilities.h */; settings = {ATTRIBUTES = (Public, ); }; }; B8AF4BB426A5204600583500 /* SendSeedModal.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8AF4BB326A5204600583500 /* SendSeedModal.swift */; }; B8B32021258B1A650020074B /* Contact.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8B32020258B1A650020074B /* Contact.swift */; }; B8B32033258B235D0020074B /* Storage+Contacts.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8B32032258B235D0020074B /* Storage+Contacts.swift */; }; @@ -1249,8 +1247,6 @@ B894D0742339EDCF00B4D94D /* NukeDataModal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NukeDataModal.swift; sourceTree = ""; }; B897621B25D201F7004F83B2 /* ScrollToBottomButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScrollToBottomButton.swift; sourceTree = ""; }; B8AE75A325A6C6A6001A84D2 /* Data+Trimming.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Data+Trimming.swift"; sourceTree = ""; }; - B8AE760925ABFB00001A84D2 /* GeneralUtilities.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneralUtilities.h; sourceTree = ""; }; - B8AE760A25ABFB5A001A84D2 /* GeneralUtilities.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GeneralUtilities.m; sourceTree = ""; }; B8AF4BB326A5204600583500 /* SendSeedModal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SendSeedModal.swift; sourceTree = ""; }; B8B32020258B1A650020074B /* Contact.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Contact.swift; sourceTree = ""; }; B8B32032258B235D0020074B /* Storage+Contacts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Storage+Contacts.swift"; sourceTree = ""; }; @@ -3225,8 +3221,6 @@ C3BBE0C62554F1570050F1E3 /* FixedWidthInteger+BigEndian.swift */, C33FDB7F255A581100E217F9 /* FullTextSearchFinder.swift */, C33FDBC1255A581700E217F9 /* General.swift */, - B8AE760925ABFB00001A84D2 /* GeneralUtilities.h */, - B8AE760A25ABFB5A001A84D2 /* GeneralUtilities.m */, B82A0C3726B9098200C1BCE3 /* MessageInvalidator.swift */, C3A71D0A2558989C0043A11F /* MessageWrapper.swift */, C3A71D4E25589FF30043A11F /* NSData+messagePadding.h */, @@ -3789,7 +3783,6 @@ C32C5BF8256DC8F6003C73A2 /* OWSDisappearingMessagesJob.h in Headers */, C32C5AAA256DBE8F003C73A2 /* TSIncomingMessage.h in Headers */, B8856D72256F1421001CE70E /* OWSWindowManager.h in Headers */, - B8AE761425ABFBB9001A84D2 /* GeneralUtilities.h in Headers */, C32C5B6B256DC357003C73A2 /* OWSDisappearingConfigurationUpdateInfoMessage.h in Headers */, C32C5BBA256DC7E3003C73A2 /* ProfileManagerProtocol.h in Headers */, C3A3A193256E20D4004D228D /* SignalRecipient.h in Headers */, @@ -4782,7 +4775,6 @@ C32C5E5B256DDF45003C73A2 /* OWSStorage.m in Sources */, C32C5E15256DDC78003C73A2 /* SSKPreferences.swift in Sources */, C32C5D9C256DD6DC003C73A2 /* OWSOutgoingReceiptManager.m in Sources */, - B8AE760B25ABFB5A001A84D2 /* GeneralUtilities.m in Sources */, C32C5C4F256DCC36003C73A2 /* Storage+OpenGroups.swift in Sources */, C3DA9C0725AE7396008F7C7E /* ConfigurationMessage.swift in Sources */, B8856CEE256F1054001CE70E /* OWSAudioPlayer.m in Sources */, diff --git a/Session/Settings/NukeDataModal.swift b/Session/Settings/NukeDataModal.swift index 7c86d629c..e3110969d 100644 --- a/Session/Settings/NukeDataModal.swift +++ b/Session/Settings/NukeDataModal.swift @@ -1,4 +1,7 @@ +import UIKit +import SessionUIKit import SessionSnodeKit +import SessionMessagingKit @objc(LKNukeDataModal) final class NukeDataModal : Modal { @@ -125,6 +128,7 @@ final class NukeDataModal : Modal { appDelegate.forceSyncConfigurationNowIfNeeded().ensure(on: DispatchQueue.main) { self?.dismiss(animated: true, completion: nil) // Dismiss the loader UserDefaults.removeAll() // Not done in the nuke data implementation as unlinking requires this to happen later + General.Cache.cachedEncodedPublicKey = nil // Remove the cached key so it gets re-cached on next access NotificationCenter.default.post(name: .dataNukeRequested, object: nil) }.retainUntilComplete() } @@ -136,6 +140,7 @@ final class NukeDataModal : Modal { self?.dismiss(animated: true, completion: nil) // Dismiss the loader let potentiallyMaliciousSnodes = confirmations.compactMap { $0.value == false ? $0.key : nil } if potentiallyMaliciousSnodes.isEmpty { + General.Cache.cachedEncodedPublicKey = nil // Remove the cached key so it gets re-cached on next access UserDefaults.removeAll() // Not done in the nuke data implementation as unlinking requires this to happen later NotificationCenter.default.post(name: .dataNukeRequested, object: nil) } else { diff --git a/SessionMessagingKit/Meta/SessionMessagingKit.h b/SessionMessagingKit/Meta/SessionMessagingKit.h index f989c52e6..498e820cb 100644 --- a/SessionMessagingKit/Meta/SessionMessagingKit.h +++ b/SessionMessagingKit/Meta/SessionMessagingKit.h @@ -5,7 +5,6 @@ FOUNDATION_EXPORT const unsigned char SessionMessagingKitVersionString[]; #import #import -#import #import #import #import diff --git a/SessionMessagingKit/Utilities/General.swift b/SessionMessagingKit/Utilities/General.swift index 62fee4eb1..565338f65 100644 --- a/SessionMessagingKit/Utilities/General.swift +++ b/SessionMessagingKit/Utilities/General.swift @@ -1,7 +1,25 @@ +import Foundation + +public enum General { + public enum Cache { + public static var cachedEncodedPublicKey: String? = nil + } +} + +@objc(SNGeneralUtilities) +public class GeneralUtilities: NSObject { + @objc public static func getUserPublicKey() -> String { + return getUserHexEncodedPublicKey() + } +} public func getUserHexEncodedPublicKey() -> String { + if let cachedKey: String = General.Cache.cachedEncodedPublicKey { return cachedKey } + if let keyPair = OWSIdentityManager.shared().identityKeyPair() { // Can be nil under some circumstances + General.Cache.cachedEncodedPublicKey = keyPair.hexEncodedPublicKey return keyPair.hexEncodedPublicKey } + return "" } diff --git a/SessionMessagingKit/Utilities/GeneralUtilities.h b/SessionMessagingKit/Utilities/GeneralUtilities.h deleted file mode 100644 index cb120b993..000000000 --- a/SessionMessagingKit/Utilities/GeneralUtilities.h +++ /dev/null @@ -1,11 +0,0 @@ -#import - -NS_ASSUME_NONNULL_BEGIN - -@interface SNGeneralUtilities : NSObject - -+ (NSString *)getUserPublicKey; - -@end - -NS_ASSUME_NONNULL_END diff --git a/SessionMessagingKit/Utilities/GeneralUtilities.m b/SessionMessagingKit/Utilities/GeneralUtilities.m deleted file mode 100644 index 777a6c3fd..000000000 --- a/SessionMessagingKit/Utilities/GeneralUtilities.m +++ /dev/null @@ -1,16 +0,0 @@ -#import -#import "GeneralUtilities.h" -#import "OWSIdentityManager.h" - -NS_ASSUME_NONNULL_BEGIN - -@implementation SNGeneralUtilities - -+ (NSString *)getUserPublicKey -{ - return OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey; -} - -@end - -NS_ASSUME_NONNULL_END diff --git a/SessionNotificationServiceExtension/NSENotificationPresenter.swift b/SessionNotificationServiceExtension/NSENotificationPresenter.swift index 6e330b94a..a9aab1e44 100644 --- a/SessionNotificationServiceExtension/NSENotificationPresenter.swift +++ b/SessionNotificationServiceExtension/NSENotificationPresenter.swift @@ -28,7 +28,7 @@ public class NSENotificationPresenter: NSObject, NotificationsProtocol { } let senderPublicKey = incomingMessage.authorId - let userPublicKey = SNGeneralUtilities.getUserPublicKey() + let userPublicKey = GeneralUtilities.getUserPublicKey() guard senderPublicKey != userPublicKey else { // Ignore PNs for messages sent by the current user // after handling the message. Otherwise the closed