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.

115 lines
3.2 KiB

  1. import 'dart:convert';
  2. import 'package:pointycastle/export.dart';
  3. import '/utils/encryption/crypto_utils.dart';
  4. import '/utils/encryption/aes_helper.dart';
  5. import '/utils/storage/database.dart';
  6. Conversation findConversationByDetailId(List<Conversation> conversations, String id) {
  7. for (var conversation in conversations) {
  8. if (conversation.conversationDetailId == id) {
  9. return conversation;
  10. }
  11. }
  12. // Or return `null`.
  13. throw ArgumentError.value(id, "id", "No element with that id");
  14. }
  15. class Conversation {
  16. String id;
  17. String userId;
  18. String conversationDetailId;
  19. String messageThreadKey;
  20. String symmetricKey;
  21. bool admin;
  22. String name;
  23. String? users;
  24. Conversation({
  25. required this.id,
  26. required this.userId,
  27. required this.conversationDetailId,
  28. required this.messageThreadKey,
  29. required this.symmetricKey,
  30. required this.admin,
  31. required this.name,
  32. this.users,
  33. });
  34. factory Conversation.fromJson(Map<String, dynamic> json, RSAPrivateKey privKey) {
  35. var symmetricKeyDecrypted = CryptoUtils.rsaDecrypt(
  36. base64.decode(json['symmetric_key']),
  37. privKey,
  38. );
  39. var detailId = AesHelper.aesDecrypt(
  40. symmetricKeyDecrypted,
  41. base64.decode(json['conversation_detail_id']),
  42. );
  43. var threadKey = AesHelper.aesDecrypt(
  44. symmetricKeyDecrypted,
  45. base64.decode(json['message_thread_key']),
  46. );
  47. var admin = AesHelper.aesDecrypt(
  48. symmetricKeyDecrypted,
  49. base64.decode(json['admin']),
  50. );
  51. return Conversation(
  52. id: json['id'],
  53. userId: json['user_id'],
  54. conversationDetailId: detailId,
  55. messageThreadKey: threadKey,
  56. symmetricKey: base64.encode(symmetricKeyDecrypted),
  57. admin: admin == 'true',
  58. name: 'Unknown',
  59. );
  60. }
  61. @override
  62. String toString() {
  63. return '''
  64. id: $id
  65. userId: $userId
  66. name: $name
  67. admin: $admin''';
  68. }
  69. Map<String, dynamic> toMap() {
  70. return {
  71. 'id': id,
  72. 'user_id': userId,
  73. 'conversation_detail_id': conversationDetailId,
  74. 'message_thread_key': messageThreadKey,
  75. 'symmetric_key': symmetricKey,
  76. 'admin': admin ? 1 : 0,
  77. 'name': name,
  78. 'users': users,
  79. };
  80. }
  81. }
  82. // A method that retrieves all the dogs from the dogs table.
  83. Future<List<Conversation>> getConversations() async {
  84. final db = await getDatabaseConnection();
  85. final List<Map<String, dynamic>> maps = await db.query('conversations');
  86. return List.generate(maps.length, (i) {
  87. return Conversation(
  88. id: maps[i]['id'],
  89. userId: maps[i]['user_id'],
  90. conversationDetailId: maps[i]['conversation_detail_id'],
  91. messageThreadKey: maps[i]['message_thread_key'],
  92. symmetricKey: maps[i]['symmetric_key'],
  93. admin: maps[i]['admin'] == 1,
  94. name: maps[i]['name'],
  95. users: maps[i]['users'],
  96. );
  97. });
  98. }