Skip to content

Commit

Permalink
create db when initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
drriguz committed Mar 12, 2024
1 parent 6ebe094 commit 318b718
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 353 deletions.
11 changes: 11 additions & 0 deletions editing/lib/config/meta.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:kdbx/kdbx.dart';

class Meta {
final String appVersion;
final String sqlCipherVersion;
final String dbInstance;
final ProtectedValue dbEncryptionKey;

Meta(this.appVersion, this.sqlCipherVersion, this.dbInstance,
this.dbEncryptionKey);
}
48 changes: 20 additions & 28 deletions editing/lib/database/database.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'dart:io';
import 'dart:ffi';

import 'package:convert/convert.dart';
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:editing/common/uuid_util.dart';
import 'package:editing/config/version.dart';
import 'package:kdbx/kdbx.dart';
import 'package:logger/logger.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import 'package:sqlite3/open.dart';
Expand All @@ -13,42 +14,24 @@ import 'package:sqlcipher_flutter_libs/sqlcipher_flutter_libs.dart';
// run `dart run build_runner build` to generate
part 'database.g.dart';

const _encryptionPassword = 'somepassword';
final logger = Logger();

@DriftDatabase(include: {'tables.drift'})
class AppDb extends _$AppDb {
AppDb() : super(_openDatabase());
AppDb.from(QueryExecutor e) : super(e);
AppDb.open(String file, ProtectedValue password)
: super(_openDatabase(file, password));

@override
int get schemaVersion => 1;

Future<void> _insertConfiguration(
String key, String value, String createTime) async {
await into(configuration).insert(ConfigurationData(
name: key,
value: value,
createTime: createTime,
lastUpdateTime: createTime,
));
}

Future<void> _inintConfiguration() async {
final now = DateTime.now().toUtc().toIso8601String();
await _insertConfiguration('APP_VERSION', APP_VERSION, now);
await _insertConfiguration('SQLCIPHER_VERSION', SQLCIPHER_VERSION, now);
await _insertConfiguration('SEED', UuidUtil.generateUUID(), now);
}

@override
MigrationStrategy get migration {
return MigrationStrategy(
beforeOpen: (details) async {},
onCreate: (Migrator m) async {
await m.createAll();
await transaction(() async {
await _inintConfiguration();
});
await transaction(() async {});
},
);
}
Expand All @@ -63,11 +46,18 @@ void setupSqlCipher() {
OperatingSystem.windows, () => DynamicLibrary.open('sqlcipher.dll'));
}

QueryExecutor _openDatabase() {
QueryExecutor _openDatabase(String file, ProtectedValue key) {
if (key.binaryValue.length != 32) {
throw ArgumentError("SQL Cipher must use a key of 32 bytes");
}
return LazyDatabase(() async {
final path = await getApplicationDocumentsDirectory();
final realFile = File(p.join(path.path, file));
print("opening database ....$realFile");
logger.i("openning database:$realFile");

return NativeDatabase.createInBackground(
File(p.join(path.path, 'app.db.enc')),
realFile,
isolateSetup: setupSqlCipher,
setup: (rawDb) {
final result = rawDb.select('pragma cipher_version');
Expand All @@ -85,8 +75,10 @@ QueryExecutor _openDatabase() {
);
}
}
final escapedKey = _encryptionPassword.replaceAll("'", "''");
rawDb.execute("pragma key = '$escapedKey';");
// set key without key derivation:
// see: https://github.com/sqlcipher/sqlcipher/blob/master/README.md
// PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'";
rawDb.execute("pragma key = \"x'${hex.encode(key.binaryValue)}'\";");
rawDb.execute('select count(*) from sqlite_master');
},
);
Expand Down
Loading

0 comments on commit 318b718

Please sign in to comment.