From 28cfb6e5f97516599aafbfbabd600a39c49d86c8 Mon Sep 17 00:00:00 2001 From: Krille Fear Date: Thu, 18 Nov 2021 07:59:35 +0100 Subject: [PATCH] fix: Start on Android and Linux --- android/app/proguard-rules.pro | 3 +- .../fluffybox_database.dart | 49 ++++++++++++------- pubspec.lock | 9 +++- pubspec.yaml | 2 + snap/snapcraft.yaml | 2 + 5 files changed, 44 insertions(+), 21 deletions(-) diff --git a/android/app/proguard-rules.pro b/android/app/proguard-rules.pro index d0f7f6ed..7163cf76 100644 --- a/android/app/proguard-rules.pro +++ b/android/app/proguard-rules.pro @@ -6,4 +6,5 @@ -keep class io.flutter.view.** { *; } -keep class io.flutter.** { *; } -keep class io.flutter.plugins.** { *; } --dontwarn io.flutter.embedding.** \ No newline at end of file +-dontwarn io.flutter.embedding.** +-keep class net.sqlcipher.** { *; } \ No newline at end of file diff --git a/lib/utils/matrix_sdk_extensions.dart/fluffybox_database.dart b/lib/utils/matrix_sdk_extensions.dart/fluffybox_database.dart index 06f11f00..ab38b00d 100644 --- a/lib/utils/matrix_sdk_extensions.dart/fluffybox_database.dart +++ b/lib/utils/matrix_sdk_extensions.dart/fluffybox_database.dart @@ -10,6 +10,7 @@ import 'package:encrypt/encrypt.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:matrix/matrix.dart'; import 'package:path_provider/path_provider.dart'; +import 'package:sqflite_common_ffi/sqflite_ffi.dart' as ffi; import 'package:sqflite_sqlcipher/sqflite.dart' as sqflite; class FlutterFluffyBoxDatabase extends FluffyBoxDatabase { @@ -27,26 +28,25 @@ class FlutterFluffyBoxDatabase extends FluffyBoxDatabase { static Future databaseBuilder(Client client) async { Logs().d('Open FluffyBox...'); String? password; - try { - // Workaround for secure storage is calling Platform.operatingSystem on web - if (kIsWeb) throw MissingPluginException(); + if (!kIsWeb && (Platform.isAndroid || Platform.isIOS)) { + try { + const secureStorage = FlutterSecureStorage(); + final containsEncryptionKey = + await secureStorage.containsKey(key: _cipherStorageKey); + if (!containsEncryptionKey) { + final key = SecureRandom(_cipherStorageKeyLength).base64; + await secureStorage.write( + key: _cipherStorageKey, + value: key, + ); + } - const secureStorage = FlutterSecureStorage(); - final containsEncryptionKey = - await secureStorage.containsKey(key: _cipherStorageKey); - if (!containsEncryptionKey) { - final key = SecureRandom(_cipherStorageKeyLength).base64; - await secureStorage.write( - key: _cipherStorageKey, - value: key, - ); + // workaround for if we just wrote to the key and it still doesn't exist + password = await secureStorage.read(key: _cipherStorageKey); + if (password == null) throw MissingPluginException(); + } on MissingPluginException catch (_) { + Logs().i('FluffyBox encryption is not supported on this platform'); } - - // workaround for if we just wrote to the key and it still doesn't exist - password = await secureStorage.read(key: _cipherStorageKey); - if (password == null) throw MissingPluginException(); - } on MissingPluginException catch (_) { - Logs().i('FluffyBox encryption is not supported on this platform'); } final db = FluffyBoxDatabase( @@ -63,7 +63,18 @@ class FlutterFluffyBoxDatabase extends FluffyBoxDatabase { String? password, ) async { final path = await _findDatabasePath(client); - return await sqflite.openDatabase(path, password: password); + try { + if (Platform.isAndroid || Platform.isIOS) { + //opensqflite.open.overrideFor(sqlite3.OperatingSystem.android, openCipherOnAndroid); + final db = await sqflite.openDatabase(path, password: password); + return db; + } + final db = await ffi.databaseFactoryFfi.openDatabase(path); + return db; + } catch (_) { + File(path).delete(); + rethrow; + } } static Future _findDatabasePath(Client client) async { diff --git a/pubspec.lock b/pubspec.lock index aef8f880..e054bc86 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1319,7 +1319,7 @@ packages: source: hosted version: "2.0.1+1" sqflite_common_ffi: - dependency: transitive + dependency: "direct main" description: name: sqflite_common_ffi url: "https://pub.dartlang.org" @@ -1332,6 +1332,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.0" + sqlcipher_flutter_libs: + dependency: "direct main" + description: + name: sqlcipher_flutter_libs + url: "https://pub.dartlang.org" + source: hosted + version: "0.5.1" sqlite3: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index e0d2234f..edc16394 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -67,7 +67,9 @@ dependencies: sentry: ^6.0.1 share: ^2.0.4 slugify: ^2.0.0 + sqflite_common_ffi: ^2.1.0 sqflite_sqlcipher: ^2.1.0 + sqlcipher_flutter_libs: ^0.5.1 swipe_to_action: ^0.2.0 uni_links: ^0.5.1 unifiedpush: ^1.0.6 diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index 3020e623..d251b53f 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -67,6 +67,8 @@ parts: stage-packages: - libsecret-1-dev - libjsoncpp-dev + - libsqlite3-0 + - libsqlite3-dev gnome-3-28-extension: build-packages: - gcc