From 38b698a531b01cbd795b5adf300cc813313f6351 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Tue, 14 May 2019 09:45:07 +1000 Subject: [PATCH 1/3] Fix OWSPrimaryStorage+PreKeyStore methods. --- .../AxolotlStore/OWSPrimaryStorage+PreKeyStore.h | 2 +- .../AxolotlStore/OWSPrimaryStorage+PreKeyStore.m | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/SignalServiceKit/src/Storage/AxolotlStore/OWSPrimaryStorage+PreKeyStore.h b/SignalServiceKit/src/Storage/AxolotlStore/OWSPrimaryStorage+PreKeyStore.h index 4de41a97f..7aba981e2 100644 --- a/SignalServiceKit/src/Storage/AxolotlStore/OWSPrimaryStorage+PreKeyStore.h +++ b/SignalServiceKit/src/Storage/AxolotlStore/OWSPrimaryStorage+PreKeyStore.h @@ -10,7 +10,7 @@ NS_ASSUME_NONNULL_BEGIN @interface OWSPrimaryStorage (PreKeyStore) - (NSArray *)generatePreKeyRecords; -- (NSArray *)generatePreKeyRecords:(int)amount; +- (NSArray *)generatePreKeyRecords:(NSUInteger)batchSize; - (void)storePreKeyRecords:(NSArray *)preKeyRecords NS_SWIFT_NAME(storePreKeyRecords(_:)); @end diff --git a/SignalServiceKit/src/Storage/AxolotlStore/OWSPrimaryStorage+PreKeyStore.m b/SignalServiceKit/src/Storage/AxolotlStore/OWSPrimaryStorage+PreKeyStore.m index b354f9b8f..13c77e5f8 100644 --- a/SignalServiceKit/src/Storage/AxolotlStore/OWSPrimaryStorage+PreKeyStore.m +++ b/SignalServiceKit/src/Storage/AxolotlStore/OWSPrimaryStorage+PreKeyStore.m @@ -18,7 +18,12 @@ NS_ASSUME_NONNULL_BEGIN @implementation OWSPrimaryStorage (PreKeyStore) -- (NSArray *)generatePreKeyRecords:(int)batchSize +- (NSArray *)generatePreKeyRecords +{ + return [self generatePreKeyRecords:BATCH_SIZE]; +} + +- (NSArray *)generatePreKeyRecords:(NSUInteger)batchSize { NSMutableArray *preKeyRecords = [NSMutableArray array]; @@ -42,11 +47,6 @@ NS_ASSUME_NONNULL_BEGIN return preKeyRecords; } -- (NSArray *)generatePreKeyRecords -{ - return [self generatePreKeyRecords:BATCH_SIZE]; -} - - (void)storePreKeyRecords:(NSArray *)preKeyRecords { for (PreKeyRecord *record in preKeyRecords) { From e714d9b3ab0e838b662b998979de4ee9b9c2fb83 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Tue, 14 May 2019 09:50:28 +1000 Subject: [PATCH 2/3] Review fixes. --- .../src/Account/CreatePreKeysOperation.swift | 4 ++-- .../src/Account/PreKeyRefreshOperation.swift | 2 +- .../src/Account/RotateSignedKeyOperation.swift | 2 +- .../src/Loki/Extensions/OWSPrimaryStorage+Loki.h | 4 ++-- .../src/Loki/Extensions/OWSPrimaryStorage+Loki.m | 12 ++++++------ 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/SignalServiceKit/src/Account/CreatePreKeysOperation.swift b/SignalServiceKit/src/Account/CreatePreKeysOperation.swift index cf4381568..b4fed8e50 100644 --- a/SignalServiceKit/src/Account/CreatePreKeysOperation.swift +++ b/SignalServiceKit/src/Account/CreatePreKeysOperation.swift @@ -30,13 +30,13 @@ public class CreatePreKeysOperation: OWSOperation { /// Loki: We don't generate PreKeyRecords here. /// This is because we need the records to be linked to a contact since we don't have a central server. /// It is done automatically when we generate a PreKeyBundle to send to a contact (`generatePreKeyBundleForContact:`). - /// You can use `getPreKeyForContact:` to generate one if needed. + /// You can use `getOrCreatePreKeyForContact:` to generate one if needed. let signedPreKeyRecord = self.primaryStorage.generateRandomSignedRecord() signedPreKeyRecord.markAsAcceptedByService() self.primaryStorage.storeSignedPreKey(signedPreKeyRecord.id, signedPreKeyRecord: signedPreKeyRecord) self.primaryStorage.setCurrentSignedPrekeyId(signedPreKeyRecord.id) - Logger.debug("done") + Logger.debug("[CreatePreKeysOperation] done") self.reportSuccess() /* Loki: Original Code diff --git a/SignalServiceKit/src/Account/PreKeyRefreshOperation.swift b/SignalServiceKit/src/Account/PreKeyRefreshOperation.swift index 672728516..0e177b6dc 100644 --- a/SignalServiceKit/src/Account/PreKeyRefreshOperation.swift +++ b/SignalServiceKit/src/Account/PreKeyRefreshOperation.swift @@ -52,7 +52,7 @@ public class RefreshPreKeysOperation: OWSOperation { TSPreKeyManager.clearPreKeyUpdateFailureCount() TSPreKeyManager.clearSignedPreKeyRecords() - Logger.debug("done") + Logger.debug("[PreKeyRefreshOperation] done") self.reportSuccess() } diff --git a/SignalServiceKit/src/Account/RotateSignedKeyOperation.swift b/SignalServiceKit/src/Account/RotateSignedKeyOperation.swift index a72ad8ce6..31b437568 100644 --- a/SignalServiceKit/src/Account/RotateSignedKeyOperation.swift +++ b/SignalServiceKit/src/Account/RotateSignedKeyOperation.swift @@ -37,7 +37,7 @@ public class RotateSignedPreKeyOperation: OWSOperation { TSPreKeyManager.clearPreKeyUpdateFailureCount() TSPreKeyManager.clearSignedPreKeyRecords() - Logger.debug("done") + Logger.debug("[RotateSignedKeyOperation] done") self.reportSuccess() } diff --git a/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.h b/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.h index 0ccc9a7ff..db91f0f9a 100644 --- a/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.h +++ b/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.h @@ -18,12 +18,12 @@ NS_ASSUME_NONNULL_BEGIN /** Get the PreKeyRecord associated with the given contact. - If the record doesn't exist then this will generate a new one. + If the record doesn't exist then this will create a new one. @param pubKey The hex encoded public key of the contact. @return The record associated with the contact. */ -- (PreKeyRecord *)getPreKeyForContact:(NSString *)pubKey; +- (PreKeyRecord *)getOrCreatePreKeyForContact:(NSString *)pubKey; # pragma mark - PreKeyBundle diff --git a/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.m b/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.m index a74f873c0..089b4ce46 100644 --- a/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.m +++ b/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.m @@ -20,12 +20,12 @@ return preKeyId > 0; } -- (PreKeyRecord *)getPreKeyForContact:(NSString *)pubKey { +- (PreKeyRecord *)getOrCreatePreKeyForContact:(NSString *)pubKey { OWSAssertDebug(pubKey.length > 0); int preKeyId = [self.dbReadWriteConnection intForKey:pubKey inCollection:LokiPreKeyContactCollection]; // If we don't have an id then generate and store a new one - if (preKeyId < 1) { + if (preKeyId <= 0) { return [self generateAndStorePreKeyForContact:pubKey]; } @@ -46,7 +46,7 @@ [self storePreKeyRecords:records]; OWSAssertDebug(records.count > 0); - PreKeyRecord *record = [records firstObject]; + PreKeyRecord *record = records.firstObject; [self.dbReadWriteConnection setInt:record.Id forKey:pubKey inCollection:LokiPreKeyContactCollection]; return record; @@ -58,7 +58,7 @@ // Check prekeys to make sure we have them for this function [TSPreKeyManager checkPreKeys]; - ECKeyPair *_Nullable myKeyPair = [[OWSIdentityManager sharedManager] identityKeyPair]; + ECKeyPair *_Nullable myKeyPair = OWSIdentityManager.sharedManager.identityKeyPair; OWSAssertDebug(myKeyPair); SignedPreKeyRecord *_Nullable signedPreKey = [self currentSignedPreKey]; @@ -66,8 +66,8 @@ OWSFailDebug(@"Signed prekey is null"); } - PreKeyRecord *preKey = [self getPreKeyForContact:pubKey]; - uint32_t registrationId = [[TSAccountManager sharedInstance] getOrGenerateRegistrationId]; + PreKeyRecord *preKey = [self getOrCreatePreKeyForContact:pubKey]; + uint32_t registrationId = [TSAccountManager.sharedInstance getOrGenerateRegistrationId]; PreKeyBundle *bundle = [[PreKeyBundle alloc] initWithRegistrationId:registrationId deviceId:OWSDevicePrimaryDeviceId From ad546eba5b8ab4556cb70a1aaf4648c03fa6d3d1 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Tue, 14 May 2019 10:06:58 +1000 Subject: [PATCH 3/3] Made code a bit more testable. --- .../src/Loki/Extensions/OWSPrimaryStorage+Loki.m | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.m b/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.m index 089b4ce46..cc66d4d7e 100644 --- a/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.m +++ b/SignalServiceKit/src/Loki/Extensions/OWSPrimaryStorage+Loki.m @@ -13,6 +13,16 @@ @implementation OWSPrimaryStorage (Loki) +# pragma mark - Dependencies + +- (OWSIdentityManager *)identityManager { + return OWSIdentityManager.sharedManager; +} + +- (TSAccountManager *)accountManager { + return TSAccountManager.sharedInstance; +} + # pragma mark - Prekey for contacts - (BOOL)hasPreKeyForContact:(NSString *)pubKey { @@ -58,7 +68,7 @@ // Check prekeys to make sure we have them for this function [TSPreKeyManager checkPreKeys]; - ECKeyPair *_Nullable myKeyPair = OWSIdentityManager.sharedManager.identityKeyPair; + ECKeyPair *_Nullable myKeyPair = self.identityManager.identityKeyPair; OWSAssertDebug(myKeyPair); SignedPreKeyRecord *_Nullable signedPreKey = [self currentSignedPreKey]; @@ -67,7 +77,7 @@ } PreKeyRecord *preKey = [self getOrCreatePreKeyForContact:pubKey]; - uint32_t registrationId = [TSAccountManager.sharedInstance getOrGenerateRegistrationId]; + uint32_t registrationId = [self.accountManager getOrGenerateRegistrationId]; PreKeyBundle *bundle = [[PreKeyBundle alloc] initWithRegistrationId:registrationId deviceId:OWSDevicePrimaryDeviceId