Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Krille/matrix-sdk-database #687

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ android {

defaultConfig {
applicationId "chat.fluffy.fluffychat"
minSdkVersion 19
minSdkVersion 21
targetSdkVersion 33
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
1 change: 1 addition & 0 deletions android/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-keep class net.sqlcipher.** { *; }
4 changes: 3 additions & 1 deletion lib/utils/client_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:fluffychat/utils/custom_http_client.dart';
import 'package:fluffychat/utils/custom_image_resizer.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/flutter_hive_collections_database.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'matrix_sdk_extensions/flutter_matrix_sdk_database_builder.dart';

abstract class ClientManager {
static const String clientNamespace = 'im.fluffychat.store.clients';
Expand Down Expand Up @@ -102,7 +103,8 @@ abstract class ClientManager {
EventTypes.RoomPowerLevels,
},
logLevel: kReleaseMode ? Level.warning : Level.verbose,
databaseBuilder: FlutterHiveCollectionsDatabase.databaseBuilder,
databaseBuilder: flutterMatrixSdkDatabaseBuilder,
legacyDatabaseBuilder: FlutterHiveCollectionsDatabase.databaseBuilder,
supportedLoginTypes: {
AuthenticationTypes.password,
AuthenticationTypes.sso,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class FlutterHiveCollectionsDatabase extends HiveCollectionsDatabase {

final db = FlutterHiveCollectionsDatabase(
'hive_collections_${client.clientName.replaceAll(' ', '_').toLowerCase()}',
await _findDatabasePath(client),
await findDatabasePath(client),
key: hiverCipher,
);
try {
Expand All @@ -80,7 +80,7 @@ class FlutterHiveCollectionsDatabase extends HiveCollectionsDatabase {
return db;
}

static Future<String> _findDatabasePath(Client client) async {
static Future<String> findDatabasePath(Client client) async {
String path = client.clientName;
if (!kIsWeb) {
Directory directory;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'dart:convert';
import 'dart:math';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.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';
import 'package:universal_html/html.dart' as html;

import 'package:fluffychat/utils/platform_infos.dart';

Future<MatrixSdkDatabase> flutterMatrixSdkDatabaseBuilder(Client client) async {
final database = await _constructDatabase(client);
await database.open();
return database;
}

Future<MatrixSdkDatabase> _constructDatabase(Client client) async {
if (kIsWeb) {
html.window.navigator.storage?.persist();
return MatrixSdkDatabase(client.clientName);
}
if (PlatformInfos.isDesktop) {
final path = await getApplicationSupportDirectory();
return MatrixSdkDatabase(
client.clientName,
database: await ffi.databaseFactoryFfi.openDatabase(
'$path/${client.clientName}',
),
maxFileSize: 1024 * 1024 * 10,
fileStoragePath: path,
deleteFilesAfterDuration: const Duration(days: 30),
);
}

final path = await getDatabasesPath();
const passwordStorageKey = 'database_password';
String? password;

try {
// Workaround for secure storage is calling Platform.operatingSystem on web
if (kIsWeb) throw MissingPluginException();

const secureStorage = FlutterSecureStorage();
final containsEncryptionKey =
await secureStorage.read(key: passwordStorageKey) != null;
if (!containsEncryptionKey) {
final rng = Random.secure();
final list = Uint8List(32);
list.setAll(0, Iterable.generate(list.length, (i) => rng.nextInt(256)));
final newPassword = base64UrlEncode(list);
await secureStorage.write(
key: passwordStorageKey,
value: newPassword,
);
}
// workaround for if we just wrote to the key and it still doesn't exist
password = await secureStorage.read(key: passwordStorageKey);
if (password == null) throw MissingPluginException();
} on MissingPluginException catch (_) {
const FlutterSecureStorage()
.delete(key: passwordStorageKey)
.catchError((_) {});
Logs().i('Database encryption is not supported on this platform');
} catch (e, s) {
const FlutterSecureStorage()
.delete(key: passwordStorageKey)
.catchError((_) {});
Logs().w('Unable to init database encryption', e, s);
}

return MatrixSdkDatabase(
client.clientName,
database: await openDatabase(
'$path/${client.clientName}',
password: password,
),
maxFileSize: 1024 * 1024 * 10,
fileStoragePath: await getTemporaryDirectory(),
deleteFilesAfterDuration: const Duration(days: 30),
);
}
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import record_macos
import share_plus
import shared_preferences_foundation
import sqflite
import sqflite_sqlcipher
import url_launcher_macos
import video_compress
import video_player_avfoundation
Expand Down Expand Up @@ -58,6 +59,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
SharePlusMacosPlugin.register(with: registry.registrar(forPlugin: "SharePlusMacosPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
SqfliteSqlCipherPlugin.register(with: registry.registrar(forPlugin: "SqfliteSqlCipherPlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
VideoCompressPlugin.register(with: registry.registrar(forPlugin: "VideoCompressPlugin"))
FVPVideoPlayerPlugin.register(with: registry.registrar(forPlugin: "FVPVideoPlayerPlugin"))
Expand Down
30 changes: 27 additions & 3 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1153,10 +1153,10 @@ packages:
dependency: "direct main"
description:
name: matrix
sha256: "155673171920dceb224bd209f51a3e41fc5c015f55ec780c9d95e54eb84ba582"
sha256: "21f8e3b3aaf32c80f62ccfbb0335161c32a2c3585661f967b1253c630367b9a7"
url: "https://pub.dev"
source: hosted
version: "0.24.0"
version: "0.24.1"
matrix_api_lite:
dependency: transitive
description:
Expand Down Expand Up @@ -1715,7 +1715,7 @@ packages:
source: hosted
version: "1.10.0"
sqflite:
dependency: transitive
dependency: "direct main"
description:
name: sqflite
sha256: "591f1602816e9c31377d5f008c2d9ef7b8aca8941c3f89cc5fd9d84da0c38a9a"
Expand All @@ -1730,6 +1730,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.5.0+2"
sqflite_common_ffi:
dependency: "direct main"
description:
name: sqflite_common_ffi
sha256: "35d2fce1e971707c227cc4775cc017d5eafe06c2654c3435ebd5c3ad6c170f5f"
url: "https://pub.dev"
source: hosted
version: "2.3.0+4"
sqflite_sqlcipher:
dependency: "direct main"
description:
name: sqflite_sqlcipher
sha256: e1dfb55bf21ee5a18c43f28faa4291272a801da4ab34a6ba9973b6c0e1ed77da
url: "https://pub.dev"
source: hosted
version: "2.2.1"
sqlite3:
dependency: transitive
description:
name: sqlite3
sha256: db65233e6b99e99b2548932f55a987961bc06d82a31a0665451fa0b4fff4c3fb
url: "https://pub.dev"
source: hosted
version: "2.1.0"
stack_trace:
dependency: transitive
description:
Expand Down
5 changes: 4 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ dependencies:
keyboard_shortcuts: ^0.1.4
latlong2: ^0.8.1
linkify: ^5.0.0
matrix: ^0.24.0
matrix: ^0.24.1
matrix_homeserver_recommendations: ^0.3.0
native_imaging: ^0.1.0
package_info_plus: ^4.0.0
Expand All @@ -78,6 +78,9 @@ dependencies:
share_plus: ^7.2.1
shared_preferences: ^2.2.0 # Pinned because https://github.com/flutter/flutter/issues/118401
slugify: ^2.0.0
sqflite: ^2.3.0
sqflite_common_ffi: ^2.3.0+4
sqflite_sqlcipher: ^2.2.1
swipe_to_action: ^0.2.0
tor_detector_web: ^1.1.0
uni_links: ^0.5.1
Expand Down
16 changes: 9 additions & 7 deletions scripts/enable-android-google-services.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/android/app/build.gradle b/android/app/build.gradle
index 001fbd72..339b35af 100644
index bf972f30..46cebdc6 100644
--- a/android/app/build.gradle
+++ b/android/app/build.gradle
@@ -70,6 +70,10 @@
Expand Down Expand Up @@ -28,13 +28,15 @@ index 001fbd72..339b35af 100644
-//apply plugin: 'com.google.gms.google-services'
+apply plugin: 'com.google.gms.google-services'
diff --git a/android/app/proguard-rules.pro b/android/app/proguard-rules.pro
new file mode 100644
index 00000000..40570865
--- /dev/null
index d0e0fbc9..0a546da0 100644
--- a/android/app/proguard-rules.pro
+++ b/android/app/proguard-rules.pro
@@ -0,0 +1,41 @@
@@ -1 +1,42 @@
--keep class net.sqlcipher.** { *; }
\ No newline at end of file
+-optimizationpasses 5
+## Flutter wrapper
+-keep class net.sqlcipher.** { *; }
+-keep class io.flutter.app.** { *; }
+-keep class io.flutter.plugin.** { *; }
+-keep class io.flutter.util.** { *; }
Expand Down Expand Up @@ -110,7 +112,7 @@ index 1afc4606..894d1571 100644
return provideEngine(this)
}
diff --git a/android/build.gradle b/android/build.gradle
index 85aa8647..3b7e09e7 100644
index bd394967..2e9d54de 100644
--- a/android/build.gradle
+++ b/android/build.gradle
@@ -8,7 +8,7 @@ buildscript {
Expand Down Expand Up @@ -145,7 +147,7 @@ index 8e67ae92..da4da5c3 100644
DateTime? lastReceivedPush;

diff --git a/pubspec.yaml b/pubspec.yaml
index 6999d0b8..b2c9144f 100644
index 193e6ed6..f70e48d4 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -26,7 +26,7 @@ dependencies:
Expand Down
Loading