Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added feature Tag #888

Merged
merged 3 commits into from
Nov 27, 2023
Merged
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
3 changes: 3 additions & 0 deletions packages/restapi/src/lib/chat/addAdmins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface AddAdminsToGroupType extends EnvOptionsType {
account?: string | null;
signer?: SignerType | null;
pgpPrivateKey?: string | null;
overrideSecretKeyGeneration?: boolean;
}

export const addAdmins = async (
Expand All @@ -22,6 +23,7 @@ export const addAdmins = async (
signer = null,
env = Constants.ENV.PROD,
pgpPrivateKey = null,
overrideSecretKeyGeneration = true,
} = options || {};
try {
if (account == null && signer == null) {
Expand All @@ -45,6 +47,7 @@ export const addAdmins = async (
signer: signer,
pgpPrivateKey: pgpPrivateKey,
env: env,
overrideSecretKeyGeneration,
};
return await updateGroupMembers(groupMemberUpdateOptions);
} catch (err) {
Expand Down
3 changes: 3 additions & 0 deletions packages/restapi/src/lib/chat/addMembers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface AddMembersToGroupType extends EnvOptionsType {
account?: string | null;
signer?: SignerType | null;
pgpPrivateKey?: string | null;
overrideSecretKeyGeneration?: boolean;
}

/**
Expand All @@ -23,6 +24,7 @@ export const addMembers = async (
signer = null,
env = Constants.ENV.PROD,
pgpPrivateKey = null,
overrideSecretKeyGeneration = true,
} = options || {};
try {
if (account == null && signer == null) {
Expand All @@ -46,6 +48,7 @@ export const addMembers = async (
signer: signer,
pgpPrivateKey: pgpPrivateKey,
env: env,
overrideSecretKeyGeneration,
};
return await updateGroupMembers(groupMemberUpdateOptions);
} catch (err) {
Expand Down
41 changes: 23 additions & 18 deletions packages/restapi/src/lib/chat/approveRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
IPGPHelper,
} from './helpers';
import * as CryptoJS from 'crypto-js';
import { getGroup } from './getGroup';
import * as AES from '../chat/helpers/aes';
import { getGroupInfo } from './getGroupInfo';
import { getAllGroupMembersPublicKeys } from './getAllGroupMembersPublicKeys';
Expand All @@ -30,6 +29,7 @@ export interface ApproveRequestOptionsType extends EnvOptionsType {
// sigType?: string;
account?: string | null;
signer?: SignerType | null;
overrideSecretKeyGeneration?: boolean;
}

/**
Expand All @@ -52,6 +52,7 @@ export const approveCore = async (
senderAddress,
env = Constants.ENV.PROD,
pgpPrivateKey = null,
overrideSecretKeyGeneration = true,
} = options || {};

/**
Expand Down Expand Up @@ -93,30 +94,34 @@ export const approveCore = async (
const group = await getGroupInfo({ chatId: senderAddress, env });

if (group && !group.isPublic) {
sigType = 'pgpv2';
const secretKey = AES.generateRandomSecret(15);

const groupMembers = await getAllGroupMembersPublicKeys({
chatId: group.chatId,
env,
});
// Encrypt secret key with group members public keys
const publicKeys: string[] = groupMembers.map(
(member) => member.publicKey
);
publicKeys.push(connectedUser.publicKey);
encryptedSecret = await pgpHelper.pgpEncrypt({
plainText: secretKey,
keys: publicKeys,
});
/**
* Secret Key Gen Override has no effect if an encrypted secret key is already present
*/
if (group.encryptedSecret || !overrideSecretKeyGeneration) {
sigType = 'pgpv2';
const secretKey = AES.generateRandomSecret(15);

const groupMembers = await getAllGroupMembersPublicKeys({
chatId: group.chatId,
env,
});
// Encrypt secret key with group members public keys
const publicKeys: string[] = groupMembers.map(
(member) => member.publicKey
);
publicKeys.push(connectedUser.publicKey);
encryptedSecret = await pgpHelper.pgpEncrypt({
plainText: secretKey,
keys: publicKeys,
});
}
}
}

let bodyToBeHashed: {
fromDID: string;
toDID: string;
status: string;
sessionKey?: string | null;
encryptedSecret?: string | null;
};

Expand Down
4 changes: 2 additions & 2 deletions packages/restapi/src/lib/chat/createGroupV2.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from 'axios';
import { getAPIBaseUrls, isValidETHAddress } from '../helpers';
import Constants from '../constants';
import { EnvOptionsType, GroupDTO, SignerType, Rules } from '../types';
import { EnvOptionsType, GroupInfoDTO, SignerType, Rules } from '../types';
import {
getWallet,
getUserDID,
Expand Down Expand Up @@ -40,7 +40,7 @@ export const createGroupV2 = async (options: ChatCreateGroupTypeV2) => {
export const createGroupCoreV2 = async (
options: ChatCreateGroupTypeV2,
pgpHelper: IPGPHelper
): Promise<GroupDTO> => {
): Promise<GroupInfoDTO> => {
const {
account = null,
signer = null,
Expand Down
3 changes: 3 additions & 0 deletions packages/restapi/src/lib/chat/modifyRole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface ModifyRolesType extends EnvOptionsType {
account?: string | null;
signer?: SignerType | null;
pgpPrivateKey?: string | null;
overrideSecretKeyGeneration?: boolean;
}

export const modifyRoles = async (
Expand All @@ -25,6 +26,7 @@ export const modifyRoles = async (
signer = null,
env = Constants.ENV.PROD,
pgpPrivateKey = null,
overrideSecretKeyGeneration = true,
} = options || {};

try {
Expand All @@ -49,6 +51,7 @@ export const modifyRoles = async (
signer: signer,
pgpPrivateKey: pgpPrivateKey,
env: env,
overrideSecretKeyGeneration,
};

return await updateGroupMembers(groupMemberUpdateOptions);
Expand Down
3 changes: 3 additions & 0 deletions packages/restapi/src/lib/chat/removeAdmins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface RemoveAdminsFromGroupType extends EnvOptionsType {
account?: string | null;
signer?: SignerType | null;
pgpPrivateKey?: string | null;
overrideSecretKeyGeneration?: boolean;
}

/**
Expand All @@ -26,6 +27,7 @@ export const removeAdmins = async (
signer = null,
env = Constants.ENV.PROD,
pgpPrivateKey = null,
overrideSecretKeyGeneration = true,
} = options || {};
try {
if (account == null && signer == null) {
Expand All @@ -47,6 +49,7 @@ export const removeAdmins = async (
signer: signer,
pgpPrivateKey: pgpPrivateKey,
env: env,
overrideSecretKeyGeneration,
};
return await updateGroupMembers(groupMemberUpdateOptions);
} catch (err) {
Expand Down
3 changes: 3 additions & 0 deletions packages/restapi/src/lib/chat/removeMembers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface RemoveMembersFromGroupType extends EnvOptionsType {
account?: string | null;
signer?: SignerType | null;
pgpPrivateKey?: string | null;
overrideSecretKeyGeneration?: boolean;
}

export const removeMembers = async (
Expand All @@ -22,6 +23,7 @@ export const removeMembers = async (
signer = null,
env = Constants.ENV.PROD,
pgpPrivateKey = null,
overrideSecretKeyGeneration = true,
} = options || {};
try {
if (account == null && signer == null) {
Expand All @@ -43,6 +45,7 @@ export const removeMembers = async (
signer: signer,
pgpPrivateKey: pgpPrivateKey,
env: env,
overrideSecretKeyGeneration,
};
return await updateGroupMembers(groupMemberUpdateOptions);
} catch (err) {
Expand Down
71 changes: 39 additions & 32 deletions packages/restapi/src/lib/chat/updateGroupMembers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface GroupMemberUpdateOptions extends EnvOptionsType {
account?: string | null;
signer?: SignerType | null;
pgpPrivateKey?: string | null;
overrideSecretKeyGeneration?: boolean;
}

export const updateGroupMembers = async (
Expand All @@ -38,6 +39,7 @@ export const updateGroupMembers = async (
signer = null,
env = Constants.ENV.PROD,
pgpPrivateKey = null,
overrideSecretKeyGeneration = true,
} = options;
try {
validateGroupMemberUpdateOptions(options);
Expand Down Expand Up @@ -73,46 +75,51 @@ export const updateGroupMembers = async (
}

if (!group.isPublic) {
const { isMember } = await getGroupMemberStatus({
chatId,
did: connectedUser.did,
env,
});

const groupMembers = await getAllGroupMembersPublicKeys({ chatId, env });

const removeParticipantSet = new Set(
convertedRemove.map((participant) => participant.toLowerCase())
);
let sameMembers = true;
if (group.encryptedSecret || !overrideSecretKeyGeneration) {
const { isMember } = await getGroupMemberStatus({
chatId,
did: connectedUser.did,
env,
});

groupMembers.map((element) => {
if (removeParticipantSet.has(element.did.toLowerCase())) {
sameMembers = false;
}
});
const groupMembers = await getAllGroupMembersPublicKeys({
chatId,
env,
});

if (!sameMembers || !isMember) {
const secretKey = AES.generateRandomSecret(15);
const removeParticipantSet = new Set(
convertedRemove.map((participant) => participant.toLowerCase())
);
let sameMembers = true;

const publicKeys: string[] = [];
// This will now only take keys of non-removed members
groupMembers.map((element) => {
if (!removeParticipantSet.has(element.did.toLowerCase())) {
publicKeys.push(element.publicKey as string);
if (removeParticipantSet.has(element.did.toLowerCase())) {
sameMembers = false;
}
});

// This is autoJoin Case
if (!isMember) {
publicKeys.push(connectedUser.publicKey);
}
if (!sameMembers || !isMember) {
const secretKey = AES.generateRandomSecret(15);

// Encrypt secret key with group members public keys
encryptedSecret = await pgpEncrypt({
plainText: secretKey,
keys: publicKeys,
});
const publicKeys: string[] = [];
// This will now only take keys of non-removed members
groupMembers.map((element) => {
if (!removeParticipantSet.has(element.did.toLowerCase())) {
publicKeys.push(element.publicKey as string);
}
});

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

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

Expand Down
5 changes: 5 additions & 0 deletions packages/restapi/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export enum MessageType {
PAYMENT = 'Payment',
}

export const ALPHA_FEATURES = {
GROUP_SCALABILITY: 'GROUP_SCALABILITY',
};

const Constants = {
ENV,
ENCRYPTION_TYPE,
Expand All @@ -60,6 +64,7 @@ const Constants = {
ENC_TYPE_V2: 'aes256GcmHkdfSha256',
ENC_TYPE_V3: 'eip191-aes256-gcm-hkdf-sha256',
ENC_TYPE_V4: 'pgpv1:nft',
ALPHA_FEATURES,
};

export default Constants;
17 changes: 11 additions & 6 deletions packages/restapi/src/lib/constantsV2.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { ENV, MessageType } from "./constants";
import { ChatListType } from "./pushapi/pushAPITypes";
import { STREAM } from "./pushstream/pushStreamTypes";
import { ConditionType, GROUP_INVITER_ROLE, GROUP_RULES_CATEGORY, GROUP_RULES_PERMISSION, GROUP_RULES_SUB_CATEGORY } from "./types";


import { ENV, MessageType, ALPHA_FEATURES } from './constants';
import { ChatListType } from './pushapi/pushAPITypes';
import { STREAM } from './pushstream/pushStreamTypes';
import {
ConditionType,
GROUP_INVITER_ROLE,
GROUP_RULES_CATEGORY,
GROUP_RULES_PERMISSION,
GROUP_RULES_SUB_CATEGORY,
} from './types';

// TODO: Change this do . type
// TODO: Add Notif type.
Expand All @@ -25,6 +29,7 @@ const CONSTANTS = {
},
},
},
ALPHA_FEATURES: ALPHA_FEATURES,
};

export default CONSTANTS;
Loading