mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
4.8 KiB
Matlab
122 lines
4.8 KiB
Matlab
6 years ago
|
#import "OWSPrimaryStorage+Loki.h"
|
||
|
#import "OWSPrimaryStorage+PreKeyStore.h"
|
||
6 years ago
|
#import "OWSPrimaryStorage+SignedPreKeyStore.h"
|
||
6 years ago
|
#import "OWSPrimaryStorage+keyFromIntLong.h"
|
||
6 years ago
|
#import "OWSDevice.h"
|
||
|
#import "OWSIdentityManager.h"
|
||
|
#import "TSAccountManager.h"
|
||
6 years ago
|
#import "TSPreKeyManager.h"
|
||
6 years ago
|
#import "YapDatabaseConnection+OWS.h"
|
||
6 years ago
|
#import "YapDatabaseTransaction+OWS.h"
|
||
6 years ago
|
#import <AxolotlKit/NSData+keyVersionByte.h>
|
||
6 years ago
|
|
||
6 years ago
|
#define OWSPrimaryStoragePreKeyStoreCollection @"TSStorageManagerPreKeyStoreCollection"
|
||
6 years ago
|
#define LokiPreKeyContactCollection @"LokiPreKeyContactCollection"
|
||
6 years ago
|
#define LokiPreKeyBundleCollection @"LokiPreKeyBundleCollection"
|
||
6 years ago
|
|
||
|
@implementation OWSPrimaryStorage (Loki)
|
||
|
|
||
6 years ago
|
# pragma mark - Dependencies
|
||
|
|
||
|
- (OWSIdentityManager *)identityManager {
|
||
|
return OWSIdentityManager.sharedManager;
|
||
|
}
|
||
|
|
||
|
- (TSAccountManager *)accountManager {
|
||
|
return TSAccountManager.sharedInstance;
|
||
|
}
|
||
|
|
||
6 years ago
|
# pragma mark - Prekey for Contact
|
||
6 years ago
|
|
||
|
- (BOOL)hasPreKeyForContact:(NSString *)pubKey {
|
||
|
int preKeyId = [self.dbReadWriteConnection intForKey:pubKey inCollection:LokiPreKeyContactCollection];
|
||
|
return preKeyId > 0;
|
||
|
}
|
||
|
|
||
6 years ago
|
- (PreKeyRecord *_Nullable)getPreKeyForContact:(NSString *)pubKey transaction:(YapDatabaseReadTransaction *)transaction {
|
||
|
OWSAssertDebug(pubKey.length > 0);
|
||
|
int preKeyId = [transaction intForKey:pubKey inCollection:LokiPreKeyContactCollection];
|
||
|
|
||
|
// If we don't have an id then return nil
|
||
6 years ago
|
if (preKeyId <= 0) { return nil; }
|
||
6 years ago
|
|
||
6 years ago
|
/// throws_loadPreKey doesn't allow us to pass transaction ;(
|
||
|
return [transaction preKeyRecordForKey:[self keyFromInt:preKeyId] inCollection:OWSPrimaryStoragePreKeyStoreCollection];
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
- (PreKeyRecord *)getOrCreatePreKeyForContact:(NSString *)pubKey {
|
||
6 years ago
|
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
|
||
6 years ago
|
if (preKeyId <= 0) {
|
||
6 years ago
|
return [self generateAndStorePreKeyForContact:pubKey];
|
||
|
}
|
||
|
|
||
6 years ago
|
// Load the prekey otherwise just generate a new one
|
||
6 years ago
|
@try {
|
||
|
return [self throws_loadPreKey:preKeyId];
|
||
|
} @catch (NSException *exception) {
|
||
6 years ago
|
OWSLogWarn(@"[Loki] New prekey generated for %@.", pubKey);
|
||
6 years ago
|
return [self generateAndStorePreKeyForContact:pubKey];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// Generate prekey for a contact and store it
|
||
|
- (PreKeyRecord *)generateAndStorePreKeyForContact:(NSString *)pubKey {
|
||
|
OWSAssertDebug(pubKey.length > 0);
|
||
|
|
||
|
NSArray<PreKeyRecord *> *records = [self generatePreKeyRecords:1];
|
||
|
[self storePreKeyRecords:records];
|
||
|
|
||
|
OWSAssertDebug(records.count > 0);
|
||
6 years ago
|
PreKeyRecord *record = records.firstObject;
|
||
6 years ago
|
[self.dbReadWriteConnection setInt:record.Id forKey:pubKey inCollection:LokiPreKeyContactCollection];
|
||
|
|
||
|
return record;
|
||
|
}
|
||
|
|
||
6 years ago
|
# pragma mark - PreKeyBundle
|
||
|
|
||
|
- (PreKeyBundle *)generatePreKeyBundleForContact:(NSString *)pubKey {
|
||
6 years ago
|
// Check prekeys to make sure we have them for this function
|
||
|
[TSPreKeyManager checkPreKeys];
|
||
|
|
||
6 years ago
|
ECKeyPair *_Nullable keyPair = self.identityManager.identityKeyPair;
|
||
|
OWSAssertDebug(keyPair);
|
||
6 years ago
|
|
||
6 years ago
|
SignedPreKeyRecord *_Nullable signedPreKey = self.currentSignedPreKey;
|
||
6 years ago
|
if (!signedPreKey) {
|
||
|
OWSFailDebug(@"Signed prekey is null");
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
PreKeyRecord *preKey = [self getOrCreatePreKeyForContact:pubKey];
|
||
6 years ago
|
uint32_t registrationId = [self.accountManager getOrGenerateRegistrationId];
|
||
6 years ago
|
|
||
|
PreKeyBundle *bundle = [[PreKeyBundle alloc] initWithRegistrationId:registrationId
|
||
|
deviceId:OWSDevicePrimaryDeviceId
|
||
|
preKeyId:preKey.Id
|
||
6 years ago
|
preKeyPublic:preKey.keyPair.publicKey.prependKeyType
|
||
|
signedPreKeyPublic:signedPreKey.keyPair.publicKey.prependKeyType
|
||
6 years ago
|
signedPreKeyId:signedPreKey.Id
|
||
|
signedPreKeySignature:signedPreKey.signature
|
||
6 years ago
|
identityKey:keyPair.publicKey.prependKeyType];
|
||
6 years ago
|
return bundle;
|
||
|
}
|
||
|
|
||
|
- (PreKeyBundle *_Nullable)getPreKeyBundleForContact:(NSString *)pubKey {
|
||
6 years ago
|
return [self.dbReadConnection preKeyBundleForKey:pubKey inCollection:LokiPreKeyBundleCollection];
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
- (void)setPreKeyBundle:(PreKeyBundle *)bundle forContact:(NSString *)pubKey transaction:(YapDatabaseReadWriteTransaction *)transaction {
|
||
|
[transaction setObject:bundle
|
||
|
forKey:pubKey
|
||
|
inCollection:LokiPreKeyBundleCollection];
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
- (void)removePreKeyBundleForContact:(NSString *)pubKey transaction:(YapDatabaseReadWriteTransaction *)transaction {
|
||
|
[transaction removeObjectForKey:pubKey inCollection:LokiPreKeyBundleCollection];
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
@end
|