diff --git a/src/Account/TSPreKeyManager.m b/src/Account/TSPreKeyManager.m index 9e05eacb4..d770464d1 100644 --- a/src/Account/TSPreKeyManager.m +++ b/src/Account/TSPreKeyManager.m @@ -153,12 +153,19 @@ static NSDate *lastPreKeyCheckTimestamp = nil; NSString *keyIdDictKey = @"keyId"; NSNumber *keyId = [responseObject objectForKey:keyIdDictKey]; OWSAssert(keyId); + TSStorageManager *storageManager = [TSStorageManager sharedManager]; - SignedPreKeyRecord *currentRecord = [storageManager loadSignedPrekey:keyId.intValue]; - OWSAssert(currentRecord); + BOOL shouldUpdateSignedPrekey = NO; + SignedPreKeyRecord *currentRecord = [storageManager loadSignedPrekeyOrNil:keyId.intValue]; + if (!currentRecord) { + DDLogError( + @"%@ %s Couldn't find signed prekey for id: %@", self.tag, __PRETTY_FUNCTION__, keyId); + shouldUpdateSignedPrekey = YES; + } else { + shouldUpdateSignedPrekey + = fabs([currentRecord.generatedAt timeIntervalSinceNow]) >= kSignedPreKeysRotationTime; + } - BOOL shouldUpdateSignedPrekey - = fabs([currentRecord.generatedAt timeIntervalSinceNow]) >= kSignedPreKeysRotationTime; if (shouldUpdateSignedPrekey) { DDLogInfo(@"%@ Updating signed prekey due to rotation period.", self.tag); updatePreKeys(RefreshPreKeysMode_SignedOnly); @@ -208,9 +215,14 @@ static NSDate *lastPreKeyCheckTimestamp = nil; // one thread is "registering" or "clearing" prekeys at a time. dispatch_async(TSPreKeyManager.prekeyQueue, ^{ TSStorageManager *storageManager = [TSStorageManager sharedManager]; - SignedPreKeyRecord *currentRecord = [storageManager loadSignedPrekey:keyId.intValue]; + SignedPreKeyRecord *currentRecord = [storageManager loadSignedPrekeyOrNil:keyId.intValue]; + if (!currentRecord) { + DDLogError(@"%@ %s Couldn't find signed prekey for id: %@", self.tag, __PRETTY_FUNCTION__, keyId); + } NSArray *allSignedPrekeys = [storageManager loadSignedPreKeys]; - NSArray *oldSignedPrekeys = [self removeCurrentRecord:currentRecord fromRecords:allSignedPrekeys]; + NSArray *oldSignedPrekeys + = (currentRecord != nil ? [self removeCurrentRecord:currentRecord fromRecords:allSignedPrekeys] + : allSignedPrekeys); NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateStyle = NSDateFormatterMediumStyle; diff --git a/src/Storage/AxolotlStore/TSStorageManager+SignedPreKeyStore.h b/src/Storage/AxolotlStore/TSStorageManager+SignedPreKeyStore.h index 5f03e6d2c..de67267b8 100644 --- a/src/Storage/AxolotlStore/TSStorageManager+SignedPreKeyStore.h +++ b/src/Storage/AxolotlStore/TSStorageManager+SignedPreKeyStore.h @@ -1,9 +1,5 @@ // -// TSStorageManager+SignedPreKeyStore.h -// TextSecureKit -// -// Created by Frederic Jacobs on 06/11/14. -// Copyright (c) 2014 Open Whisper Systems. All rights reserved. +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. // #import @@ -16,4 +12,6 @@ - (SignedPreKeyRecord *)generateRandomSignedRecord; +- (nullable SignedPreKeyRecord *)loadSignedPrekeyOrNil:(int)signedPreKeyId; + @end diff --git a/src/Storage/AxolotlStore/TSStorageManager+SignedPreKeyStore.m b/src/Storage/AxolotlStore/TSStorageManager+SignedPreKeyStore.m index c2c156ea3..6633191a0 100644 --- a/src/Storage/AxolotlStore/TSStorageManager+SignedPreKeyStore.m +++ b/src/Storage/AxolotlStore/TSStorageManager+SignedPreKeyStore.m @@ -37,6 +37,12 @@ } } +- (nullable SignedPreKeyRecord *)loadSignedPrekeyOrNil:(int)signedPreKeyId +{ + return [self signedPreKeyRecordForKey:[self keyFromInt:signedPreKeyId] + inCollection:TSStorageManagerSignedPreKeyStoreCollection]; +} + - (NSArray *)loadSignedPreKeys { NSMutableArray *signedPreKeyRecords = [NSMutableArray array];