diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.h @@ -15,12 +15,13 @@ static std::once_flag initialized; static int sqlcipherEncryptionKeySize; - static std::string secureStoreEncryptionKeyID; public: static std::string sqliteFilePath; static std::string encryptionKey; + static std::string secureStoreEncryptionKeyID; + SQLiteQueryExecutor(); static void initialize(std::string &databasePath); std::unique_ptr getThread(std::string threadID) const override; diff --git a/native/ios/Comm/AppDelegate.mm b/native/ios/Comm/AppDelegate.mm --- a/native/ios/Comm/AppDelegate.mm +++ b/native/ios/Comm/AppDelegate.mm @@ -15,6 +15,7 @@ #import #import "CommCoreModule.h" +#import "CommSecureStoreIOSWrapper.h" #import "GlobalNetworkSingleton.h" #import "Logger.h" #import "MessageOperationsUtilities.h" @@ -68,6 +69,12 @@ - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + NSString *secureStoreEncryptionKeyID = [NSString + stringWithUTF8String: + (comm::SQLiteQueryExecutor::secureStoreEncryptionKeyID.c_str())]; + [[CommSecureStoreIOSWrapper sharedInstance] + migrateOptionsForKey:secureStoreEncryptionKeyID + withVersion:@"0"]; [self attemptDatabaseInitialization]; return YES; } @@ -168,7 +175,9 @@ // this callback may be called from inactive state so we need // to initialize the database [self attemptDatabaseInitialization]; - comm::ThreadOperations::updateSQLiteUnreadStatus(threadID, false); + // Line that updates `unread` in SQLite will remain disabled until + // we confirm the line above is not the reason for the crash + // comm::ThreadOperations::updateSQLiteUnreadStatus(threadID, false); } [[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^( diff --git a/native/ios/Comm/CommSecureStoreIOSWrapper.h b/native/ios/Comm/CommSecureStoreIOSWrapper.h --- a/native/ios/Comm/CommSecureStoreIOSWrapper.h +++ b/native/ios/Comm/CommSecureStoreIOSWrapper.h @@ -9,4 +9,5 @@ - (void)set:(NSString *)key value:(NSString *)value; - (NSString *)get:(NSString *)key; +- (void)migrateOptionsForKey:(NSString *)key withVersion:(NSString *)version; @end diff --git a/native/ios/Comm/CommSecureStoreIOSWrapper.mm b/native/ios/Comm/CommSecureStoreIOSWrapper.mm --- a/native/ios/Comm/CommSecureStoreIOSWrapper.mm +++ b/native/ios/Comm/CommSecureStoreIOSWrapper.mm @@ -16,6 +16,7 @@ - (NSString *)_getValueWithKey:(NSString *)key withOptions:(NSDictionary *)options error:(NSError **)error; +- (void)_deleteValueWithKey:(NSString *)key withOptions:(NSDictionary *)options; @end @implementation CommSecureStoreIOSWrapper @@ -33,6 +34,8 @@ (EXSecureStore *)[[moduleRegistryProvider moduleRegistry] getExportedModuleOfClass:EXSecureStore.class]; shared.secureStore = secureStore; + shared.options = + @{@"keychainAccessible" : @(EXSecureStoreAccessibleAfterFirstUnlock)}; }); return shared; } @@ -64,4 +67,33 @@ error:&error]; } +- (void)migrateOptionsForKey:(NSString *)key withVersion:(NSString *)version { + NSString *secureStoreKeyVersionID = [key stringByAppendingString:@".version"]; + NSString *failureProtectionCopyKey = [key stringByAppendingString:@".copy"]; + + NSString *secureStoreKeyVersion = [self get:secureStoreKeyVersionID]; + if (secureStoreKeyVersion && + [secureStoreKeyVersion isEqualToString:version]) { + return; + } + + NSString *value = [self get:key]; + NSString *valueCopy = [self get:failureProtectionCopyKey]; + + if (value) { + [self set:failureProtectionCopyKey value:value]; + [[self secureStore] _deleteValueWithKey:key withOptions:[self options]]; + } else if (valueCopy) { + value = valueCopy; + } else { + [self set:secureStoreKeyVersionID value:version]; + return; + } + + [self set:key value:value]; + [self set:secureStoreKeyVersionID value:version]; + [[self secureStore] _deleteValueWithKey:failureProtectionCopyKey + withOptions:[self options]]; +} + @end