diff --git a/native/android/app/src/cpp/jsiInstaller.cpp b/native/android/app/src/cpp/jsiInstaller.cpp --- a/native/android/app/src/cpp/jsiInstaller.cpp +++ b/native/android/app/src/cpp/jsiInstaller.cpp @@ -1,5 +1,4 @@ #include "CommCoreModule.h" -#include "CommSecureStore.h" #include "GlobalNetworkSingletonJNIHelper.h" #include "SQLiteQueryExecutor.h" #include "jniHelpers.h" @@ -34,21 +33,9 @@ jni::local_ref sqliteFilePathObj = additionalParameters.get("sqliteFilePath"); - comm::SQLiteQueryExecutor::sqliteFilePath = sqliteFilePathObj->toString(); + std::string sqliteFilePath = sqliteFilePathObj->toString(); - comm::CommSecureStore commSecureStore; - folly::Optional maybeEncryptionKey = - commSecureStore.get("comm.encryptionKey"); - - if (maybeEncryptionKey) { - comm::SQLiteQueryExecutor::encryptionKey = maybeEncryptionKey.value(); - } else { - int sqlcipherEncryptionKeySize = 64; - std::string encryptionKey = comm::crypto::Tools::generateRandomHexString( - sqlcipherEncryptionKeySize); - commSecureStore.set("comm.encryptionKey", encryptionKey); - comm::SQLiteQueryExecutor::encryptionKey = encryptionKey; - } + comm::SQLiteQueryExecutor::initialize(sqliteFilePath); } static void registerNatives() { 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 @@ -4,6 +4,7 @@ #include "DatabaseQueryExecutor.h" #include "entities/Draft.h" +#include #include namespace comm { @@ -12,11 +13,16 @@ void migrate(); static auto &getStorage(); + static std::once_flag initialized; + static int sqlcipherEncryptionKeySize; + static std::string secureStoreEncryptionKeyID; + public: static std::string sqliteFilePath; static std::string encryptionKey; SQLiteQueryExecutor(); + static void initialize(std::string &databasePath); std::string getDraft(std::string key) const override; void updateDraft(std::string key, std::string text) const override; bool moveDraft(std::string oldKey, std::string newKey) const override; diff --git a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp --- a/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp +++ b/native/cpp/CommonCpp/DatabaseManagers/SQLiteQueryExecutor.cpp @@ -1,4 +1,5 @@ #include "SQLiteQueryExecutor.h" +#include "CommSecureStore.h" #include "Logger.h" #include "sqlite_orm.h" @@ -11,9 +12,11 @@ #include #include #include +#include #include #include #include +#include #define ACCOUNT_ID 1 @@ -23,6 +26,10 @@ std::string SQLiteQueryExecutor::sqliteFilePath; std::string SQLiteQueryExecutor::encryptionKey; +std::once_flag SQLiteQueryExecutor::initialized; +int SQLiteQueryExecutor::sqlcipherEncryptionKeySize = 64; +std::string SQLiteQueryExecutor::secureStoreEncryptionKeyID = + "comm.encryptionKey"; bool create_table(sqlite3 *db, std::string query, std::string tableName) { char *error; @@ -511,6 +518,25 @@ return storage; } +void SQLiteQueryExecutor::initialize(std::string &databasePath) { + std::call_once(SQLiteQueryExecutor::initialized, [&databasePath]() { + SQLiteQueryExecutor::sqliteFilePath = databasePath; + CommSecureStore commSecureStore; + folly::Optional maybeEncryptionKey = + commSecureStore.get(SQLiteQueryExecutor::secureStoreEncryptionKeyID); + + if (maybeEncryptionKey) { + SQLiteQueryExecutor::encryptionKey = maybeEncryptionKey.value(); + return; + } + std::string encryptionKey = comm::crypto::Tools::generateRandomHexString( + SQLiteQueryExecutor::sqlcipherEncryptionKeySize); + commSecureStore.set( + SQLiteQueryExecutor::secureStoreEncryptionKeyID, encryptionKey); + SQLiteQueryExecutor::encryptionKey = encryptionKey; + }); +} + SQLiteQueryExecutor::SQLiteQueryExecutor() { this->migrate(); } 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 @@ -110,28 +110,10 @@ getExportedModuleOfClass:EXSecureStore.class]; [[CommSecureStoreIOSWrapper sharedInstance] init:secureStore]; - // set sqlite file path - comm::SQLiteQueryExecutor::sqliteFilePath = + // initialize SQLiteQueryExecutor + std::string sqliteFilePath = std::string([[Tools getSQLiteFilePath] UTF8String]); - - // set sqlcipher encryption key - comm::CommSecureStore commSecureStore; - folly::Optional maybeEncryptionKey; - try { - maybeEncryptionKey = commSecureStore.get("comm.encryptionKey"); - } catch (NSException *exception) { - maybeEncryptionKey = folly::none; - } - - if (maybeEncryptionKey) { - comm::SQLiteQueryExecutor::encryptionKey = maybeEncryptionKey.value(); - } else { - int sqlcipherEncryptionKeySize = 64; - std::string encryptionKey = comm::crypto::Tools::generateRandomHexString( - sqlcipherEncryptionKeySize); - commSecureStore.set("comm.encryptionKey", encryptionKey); - comm::SQLiteQueryExecutor::encryptionKey = encryptionKey; - } + comm::SQLiteQueryExecutor::initialize(sqliteFilePath); return YES; }