Skip to content

Commit

Permalink
Remove the user property from the ConnectedUser class (#75)
Browse files Browse the repository at this point in the history
* feat: add scalability changes fo updarte group

* chore: add fromUser constructor for ConnectedUser

---------

Co-authored-by: Gbogboade Ayomide <[email protected]>
Co-authored-by: Madhur Gupta <[email protected]>
  • Loading branch information
3 people authored Dec 14, 2023
1 parent 9b1ad8c commit e65148b
Show file tree
Hide file tree
Showing 16 changed files with 168 additions and 88 deletions.
4 changes: 2 additions & 2 deletions example/lib/test_functions/payload/send_notification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void testSendVideoCallNotification() async {

// 5. Decrypt keys in Grp
final pgpPrivateKey = await push.decryptPGPKey(
encryptedPGPPrivateKey: user?.encryptedPrivateKey as String,
encryptedPGPPrivateKey: user.encryptedPrivateKey as String,
wallet: push.getWallet(signer: signer),
);

Expand All @@ -47,7 +47,7 @@ void testSendVideoCallNotification() async {
senderType: 1,
signer: signer,
pgpPrivateKey: pgpPrivateKey,
pgpPublicKey: user?.publicKey,
pgpPublicKey: user.publicKey,
chatId: group?.chatId,
type: push.NOTIFICATION_TYPE.BROADCAST, // broadcast
identityType: push.IDENTITY_TYPE.DIRECT_PAYLOAD, // direct payload
Expand Down
2 changes: 1 addition & 1 deletion example/lib/test_functions/user/test_create_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void testCreateRandomUser() async {
},
);

print(result?.did);
print(result.did);
} catch (e) {
print(e);
}
Expand Down
6 changes: 2 additions & 4 deletions example/lib/views/group/chat_room/chat_room_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,8 @@ class ChatRoomProvider extends ChangeNotifier {

Future getLatesGroupInfo() async {
final result = await getGroup(chatId: _currentChatid);
if (result != null) {
_room.groupInformation = result;
notifyListeners();
}
_room.groupInformation = result;
notifyListeners();
}

GroupDTO? get groupInformation => _room.groupInformation;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/chat/src/approve_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Future<String?> approve({
);

final publickKeys = groupMembers.map((e) => e.publicKey).toList();
publickKeys.add(connectedUser!.publicKey!);
publickKeys.add(connectedUser.publicKey!);
encryptedSecret =
await pgpEncrypt(plainText: secretKey, keys: publickKeys);
}
Expand All @@ -81,7 +81,7 @@ Future<String?> approve({

final signature = await sign(
message: hash,
privateKey: connectedUser!.privateKey!,
privateKey: connectedUser.privateKey!,
);

final body = {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/chat/src/create_group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Future<GroupDTO?> createGroup({
final hash = generateHash(bodyToBeHashed);
final signature = await sign(
message: hash,
privateKey: connectedUser!.privateKey!,
privateKey: connectedUser.privateKey!,
);

const sigType = 'pgp';
Expand Down
2 changes: 1 addition & 1 deletion lib/src/chat/src/get_group.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:push_restapi_dart/push_restapi_dart.dart';

Future<GroupDTO?> getGroup({required String chatId}) async {
Future<GroupDTO> getGroup({required String chatId}) async {
if (chatId.isEmpty) {
throw Exception('chatId cannot be null or empty');
}
Expand Down
34 changes: 25 additions & 9 deletions lib/src/chat/src/helper/user.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
import 'package:push_restapi_dart/push_restapi_dart.dart';

Future<ConnectedUser?> getConnectedUserV2({
Future<ConnectedUser> getConnectedUserV2({
required Wallet wallet,
String? privateKey,
}) async {
return getConnectedUserV2Core(wallet: wallet, privateKey: privateKey);
}

Future<ConnectedUser> getConnectedUserV2Core({
required Wallet wallet,
String? privateKey,
}) async {
final user = await getUser(address: getAccountAddress(wallet));

if (user == null) {
return null;
}
if (user != null && user.encryptedPrivateKey != null) {
if (privateKey != null) {
return ConnectedUser.fromUser(user: user, privateKey: privateKey);
} else {
final decryptedPrivateKey = await getDecryptedPrivateKey(
address: wallet.address!,
wallet: wallet,
user: user,
);

if (privateKey != null) {
return ConnectedUser(user: user, privateKey: privateKey);
return ConnectedUser.fromUser(
user: user, privateKey: decryptedPrivateKey);
}
} else {
final newUser =
await createUser(signer: wallet.signer!, progressHook: (hook) {});
final decryptedPrivateKey = await getDecryptedPrivateKey(
address: wallet.address!,
wallet: wallet,
user: user,
user: newUser,
);

return ConnectedUser(user: user, privateKey: decryptedPrivateKey);
return ConnectedUser.fromUser(
user: newUser, privateKey: decryptedPrivateKey);
}
}
127 changes: 101 additions & 26 deletions lib/src/chat/src/update_group.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,58 @@
import '../../../push_restapi_dart.dart';

Future<GroupDTO?> updateGroup(
{required String chatId,
String? account,
Signer? signer,
required String groupName,
required String groupDescription,
String? groupImage,
required List<String> members,
required List<String> admins,
required bool isPublic,
String? contractAddressNFT,
int? numberOfNFTs,
String? contractAddressERC20,
int? numberOfERC20,
String? pgpPrivateKey,
String? meta,
DateTime? scheduleAt,
DateTime? scheduleEnd,
ChatStatus? status}) async {
Future<GroupDTO?> updateGroup({
required String chatId,
String? account,
Signer? signer,
required String groupName,
required String groupDescription,
String? groupImage,
required List<String> members,
required List<String> admins,
required bool isPublic,
String? pgpPrivateKey,
String? meta,
DateTime? scheduleAt,
DateTime? scheduleEnd,
ChatStatus? status,
Map<String, dynamic>? rules,
}) async {
return updateGroupCore(
chatId: chatId,
groupName: groupName,
groupDescription: groupDescription,
members: members,
admins: admins,
isPublic: isPublic,
account: account,
groupImage: groupImage,
meta: meta,
pgpPrivateKey: pgpPrivateKey,
rules: rules,
scheduleAt: scheduleAt,
scheduleEnd: scheduleEnd,
signer: signer,
status: status,
);
}

Future<GroupDTO?> updateGroupCore({
required String chatId,
String? account,
Signer? signer,
required String groupName,
required String groupDescription,
String? groupImage,
required List<String> members,
required List<String> admins,
required bool isPublic,
String? pgpPrivateKey,
String? meta,
DateTime? scheduleAt,
DateTime? scheduleEnd,
ChatStatus? status,
Map<String, dynamic>? rules,
}) async {
try {
account ??= getCachedWallet()?.address;
signer ??= getCachedWallet()?.signer;
Expand Down Expand Up @@ -46,25 +80,64 @@ Future<GroupDTO?> updateGroup(
privateKey: pgpPrivateKey,
);

final convertedMembersDIDList =
final convertedMembers =
await Future.wait(members.map((item) => getUserDID(address: item)));
final convertedAdminsDIDList =
final convertedAdmins =
await Future.wait(admins.map((item) => getUserDID(address: item)));

final groupChat = await getGroup(chatId: chatId);

// Compare members array with updateGroup.members array. If they have all the same elements then return true
final updatedParticipants =
Set.from(convertedAdmins.map((e) => e.toLowerCase()));

final participantStatus =
await getGroupMemberStatus(chatId: chatId, did: connectedUser.did!);

var sameMembers = true;

for (var member in groupChat.members) {
if (!updatedParticipants.contains(member.wallet.toLowerCase())) {
sameMembers = false;
}
}

String? encryptedSecret;
if ((!sameMembers || !participantStatus.isMember) && !groupChat.isPublic) {
final secretKey = generateRandomSecret(15);
var publicKeys = <String>[];

// This will now only take keys of non-removed members
for (var member in groupChat.members) {
if (updatedParticipants.contains(member.wallet.toLowerCase())) {
publicKeys.add(member.publicKey!);
}
}

// This is autoJoin Case
if (!participantStatus.isMember) {
publicKeys.add(connectedUser.publicKey!);
}

// Encrypt secret key with group members public keys
encryptedSecret =
await pgpEncrypt(plainText: secretKey, keys: publicKeys);
}

final bodyToBeHashed = {
'groupName': groupName,
'groupDescription': groupDescription,
'groupImage': groupImage,
'members': convertedMembersDIDList,
'admins': convertedAdminsDIDList,
'members': convertedMembers,
'admins': convertedAdmins,
'chatId': chatId,
};

final hash = generateHash(bodyToBeHashed);

final signature = await sign(
message: hash,
privateKey: connectedUser!.privateKey!,
privateKey: connectedUser.privateKey!,
);

final sigType = 'pgp';
Expand All @@ -74,14 +147,16 @@ Future<GroupDTO?> updateGroup(
'groupName': groupName,
'groupImage': groupImage,
'groupDescription': groupDescription,
'members': convertedMembersDIDList,
'admins': convertedAdminsDIDList,
'members': convertedMembers,
'admins': convertedAdmins,
'address': 'eip155:$userDID',
'verificationProof': verificationProof,
'encryptedSecret': encryptedSecret,
'scheduleAt': scheduleAt?.toIso8601String(),
'scheduleEnd': scheduleEnd?.toIso8601String(),
if (meta != null) 'meta': meta,
if (status != null) 'status': chatStringFromChatStatus(status),
if (rules != null) 'rules': rules,
};

final result = await http.put(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/chat/src/update_group_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Future<GroupDTO> updateGroupConfig({

final signature = await sign(
message: hash,
privateKey: connectedUser!.privateKey!,
privateKey: connectedUser.privateKey!,
);

final sigType = 'pgpv2';
Expand Down
10 changes: 5 additions & 5 deletions lib/src/chat/src/update_group_members.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ Future<GroupInfoDTO?> updateGroupMembers({

if (!group.isPublic) {
if (group.encryptedSecret != null) {
final isMember = (await getGroupMemberStatus(
chatId: chatId, did: connectedUser!.user.did!))
.isMember;
final isMember =
(await getGroupMemberStatus(chatId: chatId, did: connectedUser.did!))
.isMember;

var groupMembers = await getAllGroupMembersPublicKeys(chatId: chatId);

Expand Down Expand Up @@ -85,7 +85,7 @@ Future<GroupInfoDTO?> updateGroupMembers({

// This is autoJoin Case
if (!isMember) {
publicKeys.add(connectedUser.user.publicKey!);
publicKeys.add(connectedUser.publicKey!);
}

encryptedSecret = await pgpEncrypt(
Expand All @@ -106,7 +106,7 @@ Future<GroupInfoDTO?> updateGroupMembers({

final signature = await sign(
message: hash,
privateKey: connectedUser!.privateKey!,
privateKey: connectedUser.privateKey!,
);

final sigType = 'pgpv2';
Expand Down
3 changes: 2 additions & 1 deletion lib/src/chat/src/update_group_profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Future<GroupInfoDTO> updateGroupProfile({
);

final group = await getGroupInfo(chatId: chatId);

/**
* CREATE PROFILE VERIFICATION PROOF
*/
Expand All @@ -54,7 +55,7 @@ Future<GroupInfoDTO> updateGroupProfile({

final signature = await sign(
message: hash,
privateKey: connectedUser!.privateKey!,
privateKey: connectedUser.privateKey!,
);

final sigType = 'pgpv2';
Expand Down
28 changes: 15 additions & 13 deletions lib/src/models/src/user_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,25 @@ abstract class Signer {
}

class ConnectedUser extends User {
final User user;
final String? privateKey;

ConnectedUser({
required this.user,
required this.privateKey,
}) {
super.did = user.did;
super.profile = user.profile;
super.name = user.name;
super.about = user.about;
super.verificationProof = user.verificationProof;
super.publicKey = user.publicKey;
super.msgSent = user.msgSent;
super.maxMsgPersisted = user.maxMsgPersisted;
super.wallets = user.wallets;
super.encryptedPrivateKey = user.encryptedPrivateKey;
});

static ConnectedUser fromUser({required User user, String? privateKey}) {
var connectedUser = ConnectedUser(privateKey: privateKey);
connectedUser.did = user.did;
connectedUser.profile = user.profile;
connectedUser.name = user.name;
connectedUser.about = user.about;
connectedUser.verificationProof = user.verificationProof;
connectedUser.publicKey = user.publicKey;
connectedUser.msgSent = user.msgSent;
connectedUser.maxMsgPersisted = user.maxMsgPersisted;
connectedUser.wallets = user.wallets;
connectedUser.encryptedPrivateKey = user.encryptedPrivateKey;
return connectedUser;
}
}

Expand Down
Loading

0 comments on commit e65148b

Please sign in to comment.