Skip to content
Open
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 .fvmrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"flutter": "3.32.8"
"flutter": "3.35.0"
}
11 changes: 5 additions & 6 deletions lib/view_model/app_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ class TaskStream {
}

class AppViewModel with ChangeNotifier {
// Limit reduced from 8MB to 3MB to account for encryption overhead
// and stay under gRPC's 4MB message size limit.
// Currently it is not possible to configure dart's gRPC message size.
// TODO: Verify encryption overhead / JSON encoding overhead to find find optimal buffer.
// Please follow: https://github.com/grpc/grpc-dart/issues/551
static const maxDataSize = 3 * 1024 * 1024; // 3MB
// Be aware of the max gRPC message size limit:
// https://github.com/grpc/grpc-dart/issues/551

// For upload of large files (PDFs, images) use gRPC streaming!
static const maxDataSize = 8 * 1024 * 1024; // 8MB
Device? device;

final List<Task> allTasks = [];
Expand Down
53 changes: 46 additions & 7 deletions meesign_core/lib/src/data/decrypt_repository.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:drift/drift.dart';
import 'package:fixnum/fixnum.dart';
import 'package:meesign_native/meesign_native.dart';
import 'package:meesign_network/grpc.dart' as rpc;

Expand Down Expand Up @@ -34,13 +35,51 @@ class DecryptRepository extends TaskRepository<Decrypt> {
List<int> gid,
) async {
final enc = ElGamalWrapper.encrypt(data, gid);
await _dispatcher.unauth.decrypt(
rpc.DecryptRequest()
..groupId = gid
..name = description
..dataType = dataType.value
..data = enc,
);

// Use streaming for large encrypted data (> 1MB)
if (enc.length > 1024 * 1024) {
await _encryptStreaming(description, dataType, enc, gid);
} else {
// Use regular RPC for small data
await _dispatcher.unauth.decrypt(
rpc.DecryptRequest()
..groupId = gid
..name = description
..dataType = dataType.value
..data = enc,
);
}
}

/// Stream large encrypted data in chunks to avoid gRPC message size limits.
Future<void> _encryptStreaming(
String description,
MimeType dataType,
List<int> encryptedData,
List<int> gid,
) async {
const chunkSize = 256 * 1024; // 256KB chunks

Stream<rpc.DecryptRequestChunk> chunks() async* {
// First chunk: metadata
yield rpc.DecryptRequestChunk()
..metadata = (rpc.DecryptMetadata()
..name = description
..groupId = gid
..dataType = dataType.value
..totalSize = Int64(encryptedData.length));

// Subsequent chunks: data
for (var i = 0; i < encryptedData.length; i += chunkSize) {
final end = (i + chunkSize < encryptedData.length)
? i + chunkSize
: encryptedData.length;

yield rpc.DecryptRequestChunk()..chunk = encryptedData.sublist(i, end);
}
}

await _dispatcher.unauth.decryptStream(chunks());
}

@override
Expand Down
46 changes: 40 additions & 6 deletions meesign_core/lib/src/data/file_repository.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:drift/drift.dart';
import 'package:fixnum/fixnum.dart';
import 'package:meesign_core/src/model/group.dart';
import 'package:meesign_native/meesign_native.dart';
import 'package:meesign_network/grpc.dart' as rpc;
Expand Down Expand Up @@ -33,12 +34,45 @@ class FileRepository extends TaskRepository<File> {
) : super(rpc.TaskType.SIGN_PDF, taskSource, _taskDao);

Future<void> sign(String name, List<int> data, List<int> gid) async {
await _dispatcher.unauth.sign(
rpc.SignRequest()
..groupId = gid
..name = name
..data = data,
);
// Use streaming for large files (> 1MB)
if (data.length > 1024 * 1024) {
await _signStreaming(name, data, gid);
} else {
// Use regular RPC for small files
await _dispatcher.unauth.sign(
rpc.SignRequest()
..groupId = gid
..name = name
..data = data,
);
}
}

/// Stream large PDF files in chunks to avoid gRPC message size limits.
Future<void> _signStreaming(
String name,
List<int> data,
List<int> gid,
) async {
const chunkSize = 256 * 1024; // 256KB chunks

Stream<rpc.SignRequestChunk> chunks() async* {
// First chunk: metadata
yield rpc.SignRequestChunk()
..metadata = (rpc.SignMetadata()
..name = name
..groupId = gid
..totalSize = Int64(data.length));

// Subsequent chunks: data
for (var i = 0; i < data.length; i += chunkSize) {
final end = (i + chunkSize < data.length) ? i + chunkSize : data.length;

yield rpc.SignRequestChunk()..chunk = data.sublist(i, end);
}
}

await _dispatcher.unauth.signStream(chunks());
}

@override
Expand Down
1 change: 1 addition & 0 deletions meesign_core/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies:
collection: ^1.16.0
convert: ^3.1.1
drift: ^2.26.1
fixnum: ^1.0.1
freezed_annotation: ^3.1.0
meesign_native: {path: ../meesign_native}
meesign_network: {path: ../meesign_network}
Expand Down
Loading
Loading