|
|
|
@ -2,78 +2,108 @@ import 'dart:convert';
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
import 'package:flutter/foundation.dart' hide Key;
|
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
|
|
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
|
|
|
import 'package:hive/hive.dart';
|
|
|
|
|
import 'package:hive_flutter/hive_flutter.dart';
|
|
|
|
|
import 'package:matrix/matrix.dart';
|
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
|
|
|
|
|
|
import '../platform_infos.dart';
|
|
|
|
|
|
|
|
|
|
class FlutterMatrixHiveStore extends FamedlySdkHiveDatabase {
|
|
|
|
|
FlutterMatrixHiveStore(String name, {HiveCipher? encryptionCipher})
|
|
|
|
|
: super(
|
|
|
|
|
class FlutterHiveCollectionsDatabase extends HiveCollectionsDatabase {
|
|
|
|
|
FlutterHiveCollectionsDatabase(
|
|
|
|
|
String name,
|
|
|
|
|
String path, {
|
|
|
|
|
HiveCipher? key,
|
|
|
|
|
}) : super(
|
|
|
|
|
name,
|
|
|
|
|
encryptionCipher: encryptionCipher,
|
|
|
|
|
path,
|
|
|
|
|
key: key,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
static bool _hiveInitialized = false;
|
|
|
|
|
static const String _hiveCipherStorageKey = 'hive_encryption_key';
|
|
|
|
|
static const String _cipherStorageKey = 'database_encryption_key';
|
|
|
|
|
|
|
|
|
|
static Future<FamedlySdkHiveDatabase> hiveDatabaseBuilder(
|
|
|
|
|
static Future<FlutterHiveCollectionsDatabase> databaseBuilder(
|
|
|
|
|
Client client) async {
|
|
|
|
|
if (!kIsWeb && !_hiveInitialized) {
|
|
|
|
|
_hiveInitialized = true;
|
|
|
|
|
}
|
|
|
|
|
HiveCipher? hiverCipher;
|
|
|
|
|
Logs().d('Open Hive...');
|
|
|
|
|
HiveAesCipher? hiverCipher;
|
|
|
|
|
try {
|
|
|
|
|
// Workaround for secure storage is calling Platform.operatingSystem on web
|
|
|
|
|
if (kIsWeb || Platform.isLinux) throw MissingPluginException();
|
|
|
|
|
if (kIsWeb) throw MissingPluginException();
|
|
|
|
|
|
|
|
|
|
const secureStorage = FlutterSecureStorage();
|
|
|
|
|
final containsEncryptionKey =
|
|
|
|
|
await secureStorage.containsKey(key: _hiveCipherStorageKey);
|
|
|
|
|
await secureStorage.containsKey(key: _cipherStorageKey);
|
|
|
|
|
if (!containsEncryptionKey) {
|
|
|
|
|
// do not try to create a buggy secure storage for new Linux users
|
|
|
|
|
if (Platform.isLinux) throw MissingPluginException();
|
|
|
|
|
final key = Hive.generateSecureKey();
|
|
|
|
|
await secureStorage.write(
|
|
|
|
|
key: _hiveCipherStorageKey,
|
|
|
|
|
key: _cipherStorageKey,
|
|
|
|
|
value: base64UrlEncode(key),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// workaround for if we just wrote to the key and it still doesn't exist
|
|
|
|
|
final rawEncryptionKey =
|
|
|
|
|
await secureStorage.read(key: _hiveCipherStorageKey);
|
|
|
|
|
final rawEncryptionKey = await secureStorage.read(key: _cipherStorageKey);
|
|
|
|
|
if (rawEncryptionKey == null) throw MissingPluginException();
|
|
|
|
|
|
|
|
|
|
final encryptionKey = base64Url.decode(rawEncryptionKey);
|
|
|
|
|
hiverCipher = HiveAesCipher(encryptionKey);
|
|
|
|
|
hiverCipher = HiveAesCipher(base64Url.decode(rawEncryptionKey));
|
|
|
|
|
} on MissingPluginException catch (_) {
|
|
|
|
|
Logs().i('Hive encryption is not supported on this platform');
|
|
|
|
|
} catch (_) {
|
|
|
|
|
const FlutterSecureStorage().delete(key: _cipherStorageKey);
|
|
|
|
|
rethrow;
|
|
|
|
|
}
|
|
|
|
|
final db = FlutterMatrixHiveStore(
|
|
|
|
|
client.clientName,
|
|
|
|
|
encryptionCipher: hiverCipher,
|
|
|
|
|
|
|
|
|
|
final db = FlutterHiveCollectionsDatabase(
|
|
|
|
|
'hive_collections_${client.clientName.replaceAll(' ', '_').toLowerCase()}',
|
|
|
|
|
await _findDatabasePath(client),
|
|
|
|
|
key: hiverCipher,
|
|
|
|
|
);
|
|
|
|
|
try {
|
|
|
|
|
await db.open();
|
|
|
|
|
} catch (e, s) {
|
|
|
|
|
Logs().e('Unable to open Hive. Delete and try again...', e, s);
|
|
|
|
|
} catch (_) {
|
|
|
|
|
Logs().w('Unable to open Hive. Delete database and storage key...');
|
|
|
|
|
const FlutterSecureStorage().delete(key: _cipherStorageKey);
|
|
|
|
|
await db.clear();
|
|
|
|
|
await db.open();
|
|
|
|
|
rethrow;
|
|
|
|
|
}
|
|
|
|
|
Logs().d('Hive is ready');
|
|
|
|
|
return db;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Future<String> _findDatabasePath(Client client) async {
|
|
|
|
|
String path = client.clientName;
|
|
|
|
|
if (!kIsWeb) {
|
|
|
|
|
Directory directory;
|
|
|
|
|
try {
|
|
|
|
|
if (Platform.isLinux) {
|
|
|
|
|
directory = await getApplicationSupportDirectory();
|
|
|
|
|
} else {
|
|
|
|
|
directory = await getApplicationDocumentsDirectory();
|
|
|
|
|
}
|
|
|
|
|
} catch (_) {
|
|
|
|
|
try {
|
|
|
|
|
directory = await getLibraryDirectory();
|
|
|
|
|
} catch (_) {
|
|
|
|
|
directory = Directory.current;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// do not destroy your stable FluffyChat in debug mode
|
|
|
|
|
if (kDebugMode) {
|
|
|
|
|
directory = Directory(directory.uri.resolve('debug').toFilePath());
|
|
|
|
|
directory.create(recursive: true);
|
|
|
|
|
}
|
|
|
|
|
path = directory.path;
|
|
|
|
|
}
|
|
|
|
|
return path;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
int get maxFileSize => supportsFileStoring ? 100 * 1024 * 1024 : 0;
|
|
|
|
|
@override
|
|
|
|
|
bool get supportsFileStoring => (PlatformInfos.isIOS ||
|
|
|
|
|
PlatformInfos.isAndroid ||
|
|
|
|
|
PlatformInfos.isDesktop);
|
|
|
|
|
bool get supportsFileStoring => !kIsWeb;
|
|
|
|
|
|
|
|
|
|
Future<String> _getFileStoreDirectory() async {
|
|
|
|
|
try {
|