Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.7 KiB

  1. import 'dart:convert';
  2. import 'dart:typed_data';
  3. import 'package:Envelope/database/repositories/conversation_users_repository.dart';
  4. import 'package:pointycastle/pointycastle.dart';
  5. import '/database/models/conversation_users.dart';
  6. import '/database/models/my_profile.dart';
  7. import '/database/models/conversations.dart';
  8. import '/utils/encryption/crypto_utils.dart';
  9. const messageTypeReceiver = 'receiver';
  10. const messageTypeSender = 'sender';
  11. class Message {
  12. String id;
  13. String symmetricKey;
  14. String userSymmetricKey;
  15. String senderId;
  16. String senderUsername;
  17. String associationKey;
  18. String createdAt;
  19. bool failedToSend;
  20. Message({
  21. required this.id,
  22. required this.symmetricKey,
  23. required this.userSymmetricKey,
  24. required this.senderId,
  25. required this.senderUsername,
  26. required this.associationKey,
  27. required this.createdAt,
  28. required this.failedToSend,
  29. });
  30. Future<List<Map<String, String>>> payloadJsonBase(
  31. Uint8List symmetricKey,
  32. Uint8List userSymmetricKey,
  33. Conversation conversation,
  34. String messageId,
  35. String messageDataId,
  36. ) async {
  37. MyProfile profile = await MyProfile.getProfile();
  38. if (profile.publicKey == null) {
  39. throw Exception('Could not get profile.publicKey');
  40. }
  41. RSAPublicKey publicKey = profile.publicKey!;
  42. List<Map<String, String>> messages = [];
  43. List<ConversationUser> conversationUsers = await ConversationUsersRepository.getConversationUsers(conversation);
  44. for (var i = 0; i < conversationUsers.length; i++) {
  45. ConversationUser user = conversationUsers[i];
  46. if (profile.id == user.userId) {
  47. messages.add({
  48. 'id': messageId,
  49. 'message_data_id': messageDataId,
  50. 'symmetric_key': base64.encode(CryptoUtils.rsaEncrypt(
  51. userSymmetricKey,
  52. publicKey,
  53. )),
  54. 'association_key': user.associationKey,
  55. });
  56. continue;
  57. }
  58. ConversationUser conversationUser = await ConversationUsersRepository.getConversationUser(conversation, user.userId);
  59. RSAPublicKey friendPublicKey = conversationUser.publicKey;
  60. messages.add({
  61. 'message_data_id': messageDataId,
  62. 'symmetric_key': base64.encode(CryptoUtils.rsaEncrypt(
  63. userSymmetricKey,
  64. friendPublicKey,
  65. )),
  66. 'association_key': user.associationKey,
  67. });
  68. }
  69. return messages;
  70. }
  71. String getContent() {
  72. return '';
  73. }
  74. Map<String, dynamic> toMap() {
  75. return {
  76. 'id': id,
  77. 'symmetric_key': symmetricKey,
  78. 'user_symmetric_key': userSymmetricKey,
  79. 'sender_id': senderId,
  80. 'sender_username': senderUsername,
  81. 'association_key': associationKey,
  82. 'created_at': createdAt,
  83. 'failed_to_send': failedToSend ? 1 : 0,
  84. };
  85. }
  86. }