Merge branch 'master' of github.com:loki-project/loki-messenger-ios

pull/14/head
Niels Andriesse 6 years ago
commit e04944d4f3

@ -30,13 +30,13 @@ public class CreatePreKeysOperation: OWSOperation {
/// Loki: We don't generate PreKeyRecords here. /// 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. /// 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:`). /// 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() let signedPreKeyRecord = self.primaryStorage.generateRandomSignedRecord()
signedPreKeyRecord.markAsAcceptedByService() signedPreKeyRecord.markAsAcceptedByService()
self.primaryStorage.storeSignedPreKey(signedPreKeyRecord.id, signedPreKeyRecord: signedPreKeyRecord) self.primaryStorage.storeSignedPreKey(signedPreKeyRecord.id, signedPreKeyRecord: signedPreKeyRecord)
self.primaryStorage.setCurrentSignedPrekeyId(signedPreKeyRecord.id) self.primaryStorage.setCurrentSignedPrekeyId(signedPreKeyRecord.id)
Logger.debug("done") Logger.debug("[CreatePreKeysOperation] done")
self.reportSuccess() self.reportSuccess()
/* Loki: Original Code /* Loki: Original Code

@ -52,7 +52,7 @@ public class RefreshPreKeysOperation: OWSOperation {
TSPreKeyManager.clearPreKeyUpdateFailureCount() TSPreKeyManager.clearPreKeyUpdateFailureCount()
TSPreKeyManager.clearSignedPreKeyRecords() TSPreKeyManager.clearSignedPreKeyRecords()
Logger.debug("done") Logger.debug("[PreKeyRefreshOperation] done")
self.reportSuccess() self.reportSuccess()
} }

@ -37,7 +37,7 @@ public class RotateSignedPreKeyOperation: OWSOperation {
TSPreKeyManager.clearPreKeyUpdateFailureCount() TSPreKeyManager.clearPreKeyUpdateFailureCount()
TSPreKeyManager.clearSignedPreKeyRecords() TSPreKeyManager.clearSignedPreKeyRecords()
Logger.debug("done") Logger.debug("[RotateSignedKeyOperation] done")
self.reportSuccess() self.reportSuccess()
} }

@ -18,12 +18,12 @@ NS_ASSUME_NONNULL_BEGIN
/** /**
Get the PreKeyRecord associated with the given contact. 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. @param pubKey The hex encoded public key of the contact.
@return The record associated with the contact. @return The record associated with the contact.
*/ */
- (PreKeyRecord *)getPreKeyForContact:(NSString *)pubKey; - (PreKeyRecord *)getOrCreatePreKeyForContact:(NSString *)pubKey;
# pragma mark - PreKeyBundle # pragma mark - PreKeyBundle

@ -13,6 +13,16 @@
@implementation OWSPrimaryStorage (Loki) @implementation OWSPrimaryStorage (Loki)
# pragma mark - Dependencies
- (OWSIdentityManager *)identityManager {
return OWSIdentityManager.sharedManager;
}
- (TSAccountManager *)accountManager {
return TSAccountManager.sharedInstance;
}
# pragma mark - Prekey for contacts # pragma mark - Prekey for contacts
- (BOOL)hasPreKeyForContact:(NSString *)pubKey { - (BOOL)hasPreKeyForContact:(NSString *)pubKey {
@ -20,12 +30,12 @@
return preKeyId > 0; return preKeyId > 0;
} }
- (PreKeyRecord *)getPreKeyForContact:(NSString *)pubKey { - (PreKeyRecord *)getOrCreatePreKeyForContact:(NSString *)pubKey {
OWSAssertDebug(pubKey.length > 0); OWSAssertDebug(pubKey.length > 0);
int preKeyId = [self.dbReadWriteConnection intForKey:pubKey inCollection:LokiPreKeyContactCollection]; int preKeyId = [self.dbReadWriteConnection intForKey:pubKey inCollection:LokiPreKeyContactCollection];
// If we don't have an id then generate and store a new one // If we don't have an id then generate and store a new one
if (preKeyId < 1) { if (preKeyId <= 0) {
return [self generateAndStorePreKeyForContact:pubKey]; return [self generateAndStorePreKeyForContact:pubKey];
} }
@ -46,7 +56,7 @@
[self storePreKeyRecords:records]; [self storePreKeyRecords:records];
OWSAssertDebug(records.count > 0); OWSAssertDebug(records.count > 0);
PreKeyRecord *record = [records firstObject]; PreKeyRecord *record = records.firstObject;
[self.dbReadWriteConnection setInt:record.Id forKey:pubKey inCollection:LokiPreKeyContactCollection]; [self.dbReadWriteConnection setInt:record.Id forKey:pubKey inCollection:LokiPreKeyContactCollection];
return record; return record;
@ -58,7 +68,7 @@
// Check prekeys to make sure we have them for this function // Check prekeys to make sure we have them for this function
[TSPreKeyManager checkPreKeys]; [TSPreKeyManager checkPreKeys];
ECKeyPair *_Nullable myKeyPair = [[OWSIdentityManager sharedManager] identityKeyPair]; ECKeyPair *_Nullable myKeyPair = self.identityManager.identityKeyPair;
OWSAssertDebug(myKeyPair); OWSAssertDebug(myKeyPair);
SignedPreKeyRecord *_Nullable signedPreKey = [self currentSignedPreKey]; SignedPreKeyRecord *_Nullable signedPreKey = [self currentSignedPreKey];
@ -66,8 +76,8 @@
OWSFailDebug(@"Signed prekey is null"); OWSFailDebug(@"Signed prekey is null");
} }
PreKeyRecord *preKey = [self getPreKeyForContact:pubKey]; PreKeyRecord *preKey = [self getOrCreatePreKeyForContact:pubKey];
uint32_t registrationId = [[TSAccountManager sharedInstance] getOrGenerateRegistrationId]; uint32_t registrationId = [self.accountManager getOrGenerateRegistrationId];
PreKeyBundle *bundle = [[PreKeyBundle alloc] initWithRegistrationId:registrationId PreKeyBundle *bundle = [[PreKeyBundle alloc] initWithRegistrationId:registrationId
deviceId:OWSDevicePrimaryDeviceId deviceId:OWSDevicePrimaryDeviceId

@ -10,7 +10,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface OWSPrimaryStorage (PreKeyStore) <PreKeyStore> @interface OWSPrimaryStorage (PreKeyStore) <PreKeyStore>
- (NSArray<PreKeyRecord *> *)generatePreKeyRecords; - (NSArray<PreKeyRecord *> *)generatePreKeyRecords;
- (NSArray<PreKeyRecord *> *)generatePreKeyRecords:(int)amount; - (NSArray<PreKeyRecord *> *)generatePreKeyRecords:(NSUInteger)batchSize;
- (void)storePreKeyRecords:(NSArray<PreKeyRecord *> *)preKeyRecords NS_SWIFT_NAME(storePreKeyRecords(_:)); - (void)storePreKeyRecords:(NSArray<PreKeyRecord *> *)preKeyRecords NS_SWIFT_NAME(storePreKeyRecords(_:));
@end @end

@ -18,7 +18,12 @@ NS_ASSUME_NONNULL_BEGIN
@implementation OWSPrimaryStorage (PreKeyStore) @implementation OWSPrimaryStorage (PreKeyStore)
- (NSArray<PreKeyRecord *> *)generatePreKeyRecords:(int)batchSize - (NSArray<PreKeyRecord *> *)generatePreKeyRecords
{
return [self generatePreKeyRecords:BATCH_SIZE];
}
- (NSArray<PreKeyRecord *> *)generatePreKeyRecords:(NSUInteger)batchSize
{ {
NSMutableArray *preKeyRecords = [NSMutableArray array]; NSMutableArray *preKeyRecords = [NSMutableArray array];
@ -42,11 +47,6 @@ NS_ASSUME_NONNULL_BEGIN
return preKeyRecords; return preKeyRecords;
} }
- (NSArray<PreKeyRecord *> *)generatePreKeyRecords
{
return [self generatePreKeyRecords:BATCH_SIZE];
}
- (void)storePreKeyRecords:(NSArray<PreKeyRecord *> *)preKeyRecords - (void)storePreKeyRecords:(NSArray<PreKeyRecord *> *)preKeyRecords
{ {
for (PreKeyRecord *record in preKeyRecords) { for (PreKeyRecord *record in preKeyRecords) {

Loading…
Cancel
Save