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.

120 lines
3.0 KiB

  1. import 'dart:convert';
  2. import "package:pointycastle/export.dart";
  3. import '/utils/encryption/crypto_utils.dart';
  4. import '/utils/storage/database.dart';
  5. Friend findFriendByFriendId(List<Friend> friends, String id) {
  6. for (var friend in friends) {
  7. if (friend.friendId == id) {
  8. return friend;
  9. }
  10. }
  11. // Or return `null`.
  12. throw ArgumentError.value(id, "id", "No element with that id");
  13. }
  14. class Friend{
  15. String id;
  16. String userId;
  17. String? username;
  18. String friendId;
  19. String friendSymmetricKey;
  20. String acceptedAt;
  21. Friend({
  22. required this.id,
  23. required this.userId,
  24. required this.friendId,
  25. required this.friendSymmetricKey,
  26. required this.acceptedAt,
  27. this.username
  28. });
  29. factory Friend.fromJson(Map<String, dynamic> json, RSAPrivateKey privKey) {
  30. var friendIdDecrypted = CryptoUtils.rsaDecrypt(
  31. base64.decode(json['friend_id']),
  32. privKey,
  33. );
  34. var friendSymmetricKeyDecrypted = CryptoUtils.rsaDecrypt(
  35. base64.decode(json['symmetric_key']),
  36. privKey,
  37. );
  38. return Friend(
  39. id: json['id'],
  40. userId: json['user_id'],
  41. friendId: String.fromCharCodes(friendIdDecrypted),
  42. friendSymmetricKey: base64.encode(friendSymmetricKeyDecrypted),
  43. acceptedAt: json['accepted_at'],
  44. );
  45. }
  46. @override
  47. String toString() {
  48. return '''
  49. id: $id
  50. userId: $userId
  51. username: $username
  52. friendId: $friendId
  53. accepted_at: $acceptedAt''';
  54. }
  55. Map<String, dynamic> toMap() {
  56. return {
  57. 'id': id,
  58. 'user_id': userId,
  59. 'username': username,
  60. 'friend_id': friendId,
  61. 'symmetric_key': friendSymmetricKey,
  62. 'accepted_at': acceptedAt,
  63. };
  64. }
  65. }
  66. // A method that retrieves all the dogs from the dogs table.
  67. Future<List<Friend>> getFriends() async {
  68. final db = await getDatabaseConnection();
  69. final List<Map<String, dynamic>> maps = await db.query('friends');
  70. return List.generate(maps.length, (i) {
  71. return Friend(
  72. id: maps[i]['id'],
  73. userId: maps[i]['user_id'],
  74. friendId: maps[i]['friend_id'],
  75. friendSymmetricKey: maps[i]['symmetric_key'],
  76. acceptedAt: maps[i]['accepted_at'],
  77. username: maps[i]['username'],
  78. );
  79. });
  80. }
  81. // Future<Friend> getFriendByUserId(String userId) async {
  82. // final db = await getDatabaseConnection();
  83. //
  84. // List<dynamic> whereArguments = [userId];
  85. //
  86. // final List<Map<String, dynamic>> maps = await db.query(
  87. // 'friends',
  88. // where: 'friend_id = ?',
  89. // whereArgs: whereArguments,
  90. // );
  91. //
  92. // print(userId);
  93. //
  94. // if (maps.length != 1) {
  95. // throw ArgumentError('Invalid user id');
  96. // }
  97. //
  98. // return Friend(
  99. // id: maps[0]['id'],
  100. // userId: maps[0]['user_id'],
  101. // friendId: maps[0]['friend_id'],
  102. // friendSymmetricKey: maps[0]['symmetric_key'],
  103. // acceptedAt: maps[0]['accepted_at'],
  104. // username: maps[0]['username'],
  105. // );
  106. // }