Skip to content

Commit

Permalink
fix: added feature Tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman035 committed Nov 27, 2023
1 parent 83576cf commit 2dd6284
Show file tree
Hide file tree
Showing 16 changed files with 235 additions and 99 deletions.
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
8 changes: 7 additions & 1 deletion packages/restapi/src/lib/pushapi/PushAPI.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Constants, { ENV } from '../constants';
import { SignerType, ProgressHookType } from '../types';
import { PushAPIInitializeProps } from './pushAPITypes';
import { FeatureTag, PushAPIInitializeProps } from './pushAPITypes';
import * as PUSH_USER from '../user';
import * as PUSH_CHAT from '../chat';
import { getAccountAddress, getWallet } from '../chat/helpers';
Expand All @@ -19,6 +19,7 @@ import {
export class PushAPI {
private signer?: SignerType;
private readMode: boolean;
private featureTag: FeatureTag;
private account: string;
private decryptedPgpPvtKey?: string;
private pgpPublicKey?: string;
Expand All @@ -38,13 +39,15 @@ export class PushAPI {
env: ENV,
account: string,
readMode: boolean,
featureTag: FeatureTag,
decryptedPgpPvtKey?: string,
pgpPublicKey?: string,
signer?: SignerType,
progressHook?: (progress: ProgressHookType) => void
) {
this.signer = signer;
this.readMode = readMode;
this.featureTag = featureTag;
this.env = env;
this.account = account;
this.decryptedPgpPvtKey = decryptedPgpPvtKey;
Expand All @@ -57,6 +60,7 @@ export class PushAPI {
this.chat = new Chat(
this.account,
this.env,
this.featureTag,
this.decryptedPgpPvtKey,
this.signer,
this.progressHook
Expand Down Expand Up @@ -130,6 +134,7 @@ export class PushAPI {
options?.autoUpgrade !== undefined
? options?.autoUpgrade
: defaultOptions.autoUpgrade,
featureTag: options?.featureTag || 'STABLE',
};

const readMode = !signer;
Expand Down Expand Up @@ -197,6 +202,7 @@ export class PushAPI {
settings.env as ENV,
derivedAccount,
readMode,
settings.featureTag,
decryptedPGPPrivateKey,
pgpPublicKey,
signer,
Expand Down
Loading

0 comments on commit 2dd6284

Please sign in to comment.