diff --git a/packages/restapi/src/lib/chat/addAdmins.ts b/packages/restapi/src/lib/chat/addAdmins.ts index 888f2680b..9ce57c2c6 100644 --- a/packages/restapi/src/lib/chat/addAdmins.ts +++ b/packages/restapi/src/lib/chat/addAdmins.ts @@ -1,4 +1,5 @@ -import Constants from '../constants'; +import { ALPHA_FEATURE_CONFIG } from '../config'; +import Constants, { PACKAGE_BUILD } from '../constants'; import { EnvOptionsType, SignerType, GroupInfoDTO } from '../types'; import { GroupMemberUpdateOptions, @@ -23,7 +24,9 @@ export const addAdmins = async ( signer = null, env = Constants.ENV.PROD, pgpPrivateKey = null, - overrideSecretKeyGeneration = true, + overrideSecretKeyGeneration = !ALPHA_FEATURE_CONFIG[ + PACKAGE_BUILD + ].feature.includes(Constants.ALPHA_FEATURES.SCALABILITY_V2), } = options || {}; try { if (account == null && signer == null) { diff --git a/packages/restapi/src/lib/chat/addMembers.ts b/packages/restapi/src/lib/chat/addMembers.ts index 8807d9ab2..96af53549 100644 --- a/packages/restapi/src/lib/chat/addMembers.ts +++ b/packages/restapi/src/lib/chat/addMembers.ts @@ -1,4 +1,5 @@ -import Constants from '../constants'; +import { ALPHA_FEATURE_CONFIG } from '../config'; +import Constants, { PACKAGE_BUILD } from '../constants'; import { EnvOptionsType, SignerType, GroupInfoDTO } from '../types'; import { updateGroupMembers } from './updateGroupMembers'; import { GroupMemberUpdateOptions } from './updateGroupMembers'; @@ -24,7 +25,9 @@ export const addMembers = async ( signer = null, env = Constants.ENV.PROD, pgpPrivateKey = null, - overrideSecretKeyGeneration = true, + overrideSecretKeyGeneration = !ALPHA_FEATURE_CONFIG[ + PACKAGE_BUILD + ].feature.includes(Constants.ALPHA_FEATURES.SCALABILITY_V2), } = options || {}; try { if (account == null && signer == null) { diff --git a/packages/restapi/src/lib/chat/approveRequest.ts b/packages/restapi/src/lib/chat/approveRequest.ts index 07b901f88..dd84b4d77 100644 --- a/packages/restapi/src/lib/chat/approveRequest.ts +++ b/packages/restapi/src/lib/chat/approveRequest.ts @@ -1,6 +1,6 @@ import axios from 'axios'; import { getAPIBaseUrls, isValidETHAddress } from '../helpers'; -import Constants from '../constants'; +import Constants, { PACKAGE_BUILD } from '../constants'; import { EnvOptionsType, SignerType } from '../types'; import { getAccountAddress, @@ -14,6 +14,7 @@ import * as CryptoJS from 'crypto-js'; import * as AES from '../chat/helpers/aes'; import { getGroupInfo } from './getGroupInfo'; import { getAllGroupMembersPublicKeys } from './getAllGroupMembersPublicKeys'; +import { ALPHA_FEATURE_CONFIG } from '../config'; export interface ApproveRequestOptionsType extends EnvOptionsType { /** @@ -52,7 +53,9 @@ export const approveCore = async ( senderAddress, env = Constants.ENV.PROD, pgpPrivateKey = null, - overrideSecretKeyGeneration = true, + overrideSecretKeyGeneration = !ALPHA_FEATURE_CONFIG[ + PACKAGE_BUILD + ].feature.includes(Constants.ALPHA_FEATURES.SCALABILITY_V2), } = options || {}; /** diff --git a/packages/restapi/src/lib/chat/modifyRole.ts b/packages/restapi/src/lib/chat/modifyRole.ts index 9b82a09bb..1b7884774 100644 --- a/packages/restapi/src/lib/chat/modifyRole.ts +++ b/packages/restapi/src/lib/chat/modifyRole.ts @@ -1,4 +1,5 @@ -import Constants from '../constants'; +import { ALPHA_FEATURE_CONFIG } from '../config'; +import Constants, { PACKAGE_BUILD } from '../constants'; import { EnvOptionsType, SignerType, GroupInfoDTO } from '../types'; import { GroupMemberUpdateOptions, @@ -26,7 +27,9 @@ export const modifyRoles = async ( signer = null, env = Constants.ENV.PROD, pgpPrivateKey = null, - overrideSecretKeyGeneration = true, + overrideSecretKeyGeneration = !ALPHA_FEATURE_CONFIG[ + PACKAGE_BUILD + ].feature.includes(Constants.ALPHA_FEATURES.SCALABILITY_V2), } = options || {}; try { diff --git a/packages/restapi/src/lib/chat/removeAdmins.ts b/packages/restapi/src/lib/chat/removeAdmins.ts index 77e6255ea..fd92f53d5 100644 --- a/packages/restapi/src/lib/chat/removeAdmins.ts +++ b/packages/restapi/src/lib/chat/removeAdmins.ts @@ -1,4 +1,5 @@ -import Constants from '../constants'; +import { ALPHA_FEATURE_CONFIG } from '../config'; +import Constants, { PACKAGE_BUILD } from '../constants'; import { EnvOptionsType, SignerType, GroupInfoDTO } from '../types'; import { GroupMemberUpdateOptions, @@ -27,7 +28,9 @@ export const removeAdmins = async ( signer = null, env = Constants.ENV.PROD, pgpPrivateKey = null, - overrideSecretKeyGeneration = true, + overrideSecretKeyGeneration = !ALPHA_FEATURE_CONFIG[ + PACKAGE_BUILD + ].feature.includes(Constants.ALPHA_FEATURES.SCALABILITY_V2), } = options || {}; try { if (account == null && signer == null) { diff --git a/packages/restapi/src/lib/chat/removeMembers.ts b/packages/restapi/src/lib/chat/removeMembers.ts index 67f2ed55a..94dc20b5f 100644 --- a/packages/restapi/src/lib/chat/removeMembers.ts +++ b/packages/restapi/src/lib/chat/removeMembers.ts @@ -1,4 +1,5 @@ -import Constants from '../constants'; +import { ALPHA_FEATURE_CONFIG } from '../config'; +import Constants, { PACKAGE_BUILD } from '../constants'; import { EnvOptionsType, SignerType, GroupInfoDTO } from '../types'; import { GroupMemberUpdateOptions, @@ -23,7 +24,9 @@ export const removeMembers = async ( signer = null, env = Constants.ENV.PROD, pgpPrivateKey = null, - overrideSecretKeyGeneration = true, + overrideSecretKeyGeneration = !ALPHA_FEATURE_CONFIG[ + PACKAGE_BUILD + ].feature.includes(Constants.ALPHA_FEATURES.SCALABILITY_V2), } = options || {}; try { if (account == null && signer == null) { diff --git a/packages/restapi/src/lib/chat/updateGroupMembers.ts b/packages/restapi/src/lib/chat/updateGroupMembers.ts index e1fbc027c..02a8013d0 100644 --- a/packages/restapi/src/lib/chat/updateGroupMembers.ts +++ b/packages/restapi/src/lib/chat/updateGroupMembers.ts @@ -1,6 +1,6 @@ import axios from 'axios'; import { getAPIBaseUrls } from '../helpers'; -import Constants from '../constants'; +import Constants, { PACKAGE_BUILD } from '../constants'; import { getWallet, PGPHelper, @@ -15,6 +15,7 @@ import { getGroupInfo } from './getGroupInfo'; import { getGroupMemberStatus } from './getGroupMemberStatus'; import * as AES from '../chat/helpers/aes'; import { getAllGroupMembersPublicKeys } from './getAllGroupMembersPublicKeys'; +import { ALPHA_FEATURE_CONFIG } from '../config'; export interface GroupMemberUpdateOptions extends EnvOptionsType { chatId: string; @@ -39,7 +40,9 @@ export const updateGroupMembers = async ( signer = null, env = Constants.ENV.PROD, pgpPrivateKey = null, - overrideSecretKeyGeneration = true, + overrideSecretKeyGeneration = !ALPHA_FEATURE_CONFIG[ + PACKAGE_BUILD + ].feature.includes(Constants.ALPHA_FEATURES.SCALABILITY_V2), } = options; try { validateGroupMemberUpdateOptions(options); diff --git a/packages/restapi/src/lib/config.ts b/packages/restapi/src/lib/config.ts index 4f8777afe..6c9cfb933 100644 --- a/packages/restapi/src/lib/config.ts +++ b/packages/restapi/src/lib/config.ts @@ -40,14 +40,19 @@ const BLOCKCHAIN_NETWORK = { POLYGON_ZK_EVM_TESTNET: 'eip155:1442', POLYGON_ZK_EVM_MAINNET: 'eip155:1101', ARBITRUM_TESTNET: 'eip155:421613', - ARBITRUMONE_MAINNET: "eip155:42161" + ARBITRUMONE_MAINNET: 'eip155:42161', }; -export type ALIAS_CHAIN = 'POLYGON' | 'BSC' | 'OPTIMISM' | 'POLYGONZKEVM' | "ARBITRUMONE"; +export type ALIAS_CHAIN = + | 'POLYGON' + | 'BSC' + | 'OPTIMISM' + | 'POLYGONZKEVM' + | 'ARBITRUMONE'; export const ETH_CHAIN_ID = { [ENV.PROD]: 1, - [ENV.STAGING]:11155111, + [ENV.STAGING]: 11155111, [ENV.DEV]: 11155111, [ENV.LOCAL]: 11155111, }; @@ -81,7 +86,7 @@ export const ALIAS_CHAIN_ID = { [ENV.STAGING]: 421613, [ENV.DEV]: 421613, [ENV.LOCAL]: 421613, - } + }, }; export const CHAIN_ID = { @@ -106,15 +111,14 @@ export const CHAIN_NAME: { [key: number]: string } = { 1101: 'POLYGONZKEVM', 1442: 'POLYGONZKEVM', // arbitrun - 421613:'ARBITRUN', - 42161:'ARBITRUM' + 421613: 'ARBITRUN', + 42161: 'ARBITRUM', }; export interface ConfigType { API_BASE_URL: string; EPNS_COMMUNICATOR_CONTRACT: string; } - export const VIEM_CORE_CONFIG = { [ENV.PROD]: { NETWORK: mainnet, @@ -277,7 +281,7 @@ export const TOKEN_VIEM_NETWORK_MAP = { [ENV.STAGING]: sepolia, [ENV.DEV]: sepolia, [ENV.LOCAL]: sepolia, -} +}; export const MIN_TOKEN_BALANCE = { [ENV.PROD]: 50, @@ -296,7 +300,6 @@ export const CHANNEL_TYPE = { GENERAL: 2, }; - export const VIEM_CONFIG = { [ENV.PROD]: { [BLOCKCHAIN_NETWORK.ETH_MAINNET]: { @@ -407,3 +410,12 @@ export const VIEM_CONFIG = { }, }, }; + +export const ALPHA_FEATURE_CONFIG = { + STABLE: { + feature: [] as string[], + }, + ALPHA: { + feature: [Constants.ALPHA_FEATURES.SCALABILITY_V2], + }, +}; diff --git a/packages/restapi/src/lib/constants.ts b/packages/restapi/src/lib/constants.ts index 6e631e28b..af0788ff1 100644 --- a/packages/restapi/src/lib/constants.ts +++ b/packages/restapi/src/lib/constants.ts @@ -1,3 +1,6 @@ +// eslint-disable-next-line @typescript-eslint/no-var-requires +const packageJson = require('../../package.json'); + /** * SUPPORTED ENVIRONEMENTS */ @@ -44,9 +47,13 @@ export enum MessageType { } export const ALPHA_FEATURES = { - GROUP_SCALABILITY: 'GROUP_SCALABILITY', + SCALABILITY_V2: 'SCALABILITY_V2', }; +export const PACKAGE_BUILD = packageJson.version.includes('alpha') + ? 'ALPHA' + : 'STABLE'; +console.log(PACKAGE_BUILD); const Constants = { ENV, ENCRYPTION_TYPE, diff --git a/packages/restapi/src/lib/pushapi/PushAPI.ts b/packages/restapi/src/lib/pushapi/PushAPI.ts index c70dfcd43..e205b9a7d 100644 --- a/packages/restapi/src/lib/pushapi/PushAPI.ts +++ b/packages/restapi/src/lib/pushapi/PushAPI.ts @@ -1,4 +1,4 @@ -import Constants, { ENV } from '../constants'; +import Constants, { ENV, PACKAGE_BUILD } from '../constants'; import { SignerType, ProgressHookType } from '../types'; import { PushAPIInitializeProps } from './pushAPITypes'; import * as PUSH_USER from '../user'; @@ -15,6 +15,7 @@ import { PushStreamInitializeProps, STREAM, } from '../pushstream/pushStreamTypes'; +import { ALPHA_FEATURE_CONFIG } from '../config'; export class PushAPI { private signer?: SignerType; @@ -137,7 +138,7 @@ export class PushAPI { alpha: options?.alpha && options.alpha.feature ? options.alpha - : { feature: [] }, + : ALPHA_FEATURE_CONFIG[PACKAGE_BUILD], }; const readMode = !signer; diff --git a/packages/restapi/src/lib/pushapi/chat.ts b/packages/restapi/src/lib/pushapi/chat.ts index 3d0b821de..9148cf331 100644 --- a/packages/restapi/src/lib/pushapi/chat.ts +++ b/packages/restapi/src/lib/pushapi/chat.ts @@ -35,7 +35,7 @@ import { updateGroupConfig } from '../chat/updateGroupConfig'; import { PushAPI } from './PushAPI'; export class Chat { private userInstance: User; - private groupScalabilityTag: 'ALPHA' | 'STABLE'; + private scalabilityV2Feature: boolean; constructor( private account: string, @@ -46,11 +46,9 @@ export class Chat { private progressHook?: (progress: ProgressHookType) => void ) { this.userInstance = new User(this.account, this.env); - this.groupScalabilityTag = this.alpha.feature.includes( - ALPHA_FEATURES.GROUP_SCALABILITY - ) - ? 'ALPHA' - : 'STABLE'; + this.scalabilityV2Feature = this.alpha.feature.includes( + ALPHA_FEATURES.SCALABILITY_V2 + ); } async list( @@ -81,6 +79,7 @@ export class Chat { throw new Error('Invalid Chat List Type'); } } + async latest(target: string) { const { threadHash } = await PUSH_CHAT.conversationHash({ conversationId: target, @@ -170,7 +169,7 @@ export class Chat { account: this.account, signer: this.signer, pgpPrivateKey: this.decryptedPgpPvtKey, - overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA', + overrideSecretKeyGeneration: !this.scalabilityV2Feature, }); } @@ -304,14 +303,14 @@ export class Chat { admins: options?.admins ? options.admins : [], }; const response = await PUSH_CHAT.createGroupV2(groupParams); - switch (this.groupScalabilityTag) { - case 'ALPHA': - return response; - case 'STABLE': - return await PUSH_CHAT.getGroup({ - chatId: response.chatId, - env: this.env, - }); + + if (this.scalabilityV2Feature) { + return response; + } else { + return await PUSH_CHAT.getGroup({ + chatId: response.chatId, + env: this.env, + }); } }, @@ -344,18 +343,15 @@ export class Chat { }, info: async (chatId: string): Promise => { - switch (this.groupScalabilityTag) { - case 'ALPHA': - return await PUSH_CHAT.getGroupInfo({ + return this.scalabilityV2Feature + ? await PUSH_CHAT.getGroupInfo({ chatId: chatId, env: this.env, - }); - case 'STABLE': - return await PUSH_CHAT.getGroup({ + }) + : await PUSH_CHAT.getGroup({ chatId: chatId, env: this.env, }); - } }, update: async ( chatId: string, @@ -400,14 +396,13 @@ export class Chat { await updateGroupProfile(updateGroupProfileOptions); const response = await updateGroupConfig(updateGroupConfigOptions); - switch (this.groupScalabilityTag) { - case 'ALPHA': - return response; - case 'STABLE': - return await PUSH_CHAT.getGroup({ - chatId: response.chatId, - env: this.env, - }); + if (this.scalabilityV2Feature) { + return response; + } else { + return await PUSH_CHAT.getGroup({ + chatId: response.chatId, + env: this.env, + }); } }, @@ -444,7 +439,7 @@ export class Chat { account: this.account, signer: this.signer, pgpPrivateKey: this.decryptedPgpPvtKey, - overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA', + overrideSecretKeyGeneration: !this.scalabilityV2Feature, }); } else { response = await PUSH_CHAT.addMembers({ @@ -454,18 +449,17 @@ export class Chat { account: this.account, signer: this.signer, pgpPrivateKey: this.decryptedPgpPvtKey, - overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA', + overrideSecretKeyGeneration: !this.scalabilityV2Feature, }); } - switch (this.groupScalabilityTag) { - case 'ALPHA': - return response; - case 'STABLE': - return await PUSH_CHAT.getGroup({ - chatId: response.chatId, - env: this.env, - }); + if (this.scalabilityV2Feature) { + return response; + } else { + return await PUSH_CHAT.getGroup({ + chatId: response.chatId, + env: this.env, + }); } }, @@ -513,7 +507,7 @@ export class Chat { account: this.account, signer: this.signer, pgpPrivateKey: this.decryptedPgpPvtKey, - overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA', + overrideSecretKeyGeneration: !this.scalabilityV2Feature, }); } @@ -525,21 +519,10 @@ export class Chat { account: this.account, signer: this.signer, pgpPrivateKey: this.decryptedPgpPvtKey, - overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA', + overrideSecretKeyGeneration: !this.scalabilityV2Feature, }); } - switch (this.groupScalabilityTag) { - case 'ALPHA': - return await PUSH_CHAT.getGroupInfo({ - chatId: chatId, - env: this.env, - }); - case 'STABLE': - return await PUSH_CHAT.getGroup({ - chatId: chatId, - env: this.env, - }); - } + return await this.group.info(chatId); }, modify: async (chatId: string, options: ManageGroupOptions) => { @@ -570,7 +553,7 @@ export class Chat { account: this.account, signer: this.signer, pgpPrivateKey: this.decryptedPgpPvtKey, - overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA', + overrideSecretKeyGeneration: !this.scalabilityV2Feature, }); }, @@ -591,7 +574,7 @@ export class Chat { account: this.account, signer: this.signer, pgpPrivateKey: this.decryptedPgpPvtKey, - overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA', + overrideSecretKeyGeneration: !this.scalabilityV2Feature, }); } else if (!status.isMember) { await PUSH_CHAT.addMembers({ @@ -601,21 +584,10 @@ export class Chat { account: this.account, signer: this.signer, pgpPrivateKey: this.decryptedPgpPvtKey, - overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA', + overrideSecretKeyGeneration: !this.scalabilityV2Feature, }); } - switch (this.groupScalabilityTag) { - case 'ALPHA': - return await PUSH_CHAT.getGroupInfo({ - chatId: target, - env: this.env, - }); - case 'STABLE': - return await PUSH_CHAT.getGroup({ - chatId: target, - env: this.env, - }); - } + return await this.group.info(target); }, leave: async (target: string): Promise => { @@ -639,7 +611,7 @@ export class Chat { account: this.account, signer: this.signer, pgpPrivateKey: this.decryptedPgpPvtKey, - overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA', + overrideSecretKeyGeneration: !this.scalabilityV2Feature, }); } else { response = await PUSH_CHAT.removeMembers({ @@ -649,18 +621,17 @@ export class Chat { account: this.account, signer: this.signer, pgpPrivateKey: this.decryptedPgpPvtKey, - overrideSecretKeyGeneration: this.groupScalabilityTag !== 'ALPHA', + overrideSecretKeyGeneration: !this.scalabilityV2Feature, }); } - switch (this.groupScalabilityTag) { - case 'ALPHA': - return response; - case 'STABLE': - return await PUSH_CHAT.getGroup({ - chatId: response.chatId, - env: this.env, - }); + if (this.scalabilityV2Feature) { + return response; + } else { + return await PUSH_CHAT.getGroup({ + chatId: response.chatId, + env: this.env, + }); } }, diff --git a/packages/restapi/src/lib/pushstream/PushStream.ts b/packages/restapi/src/lib/pushstream/PushStream.ts index b7702334b..192dd42a5 100644 --- a/packages/restapi/src/lib/pushstream/PushStream.ts +++ b/packages/restapi/src/lib/pushstream/PushStream.ts @@ -1,6 +1,6 @@ import { EventEmitter } from 'events'; import { createSocketConnection, EVENTS } from '@pushprotocol/socket'; -import { ENV } from '../constants'; +import { ENV, PACKAGE_BUILD } from '../constants'; import { GroupEventType, MessageEventType, @@ -12,6 +12,7 @@ import { DataModifier } from './DataModifier'; import { pCAIP10ToWallet, walletToPCAIP10 } from '../helpers'; import { Chat } from '../pushapi/chat'; import { ProgressHookType, SignerType } from '../types'; +import { ALPHA_FEATURE_CONFIG } from '../config'; export class PushStream extends EventEmitter { private pushChatSocket: any; @@ -42,7 +43,7 @@ export class PushStream extends EventEmitter { this.chatInstance = new Chat( this.account, this.options.env as ENV, - { feature: [] }, + ALPHA_FEATURE_CONFIG[PACKAGE_BUILD], this.decryptedPgpPvtKey, this.signer, this.progressHook diff --git a/packages/restapi/tests/lib/chat/privateGroup.test.ts b/packages/restapi/tests/lib/chat/privateGroup.test.ts index 84a136d13..536e2a873 100644 --- a/packages/restapi/tests/lib/chat/privateGroup.test.ts +++ b/packages/restapi/tests/lib/chat/privateGroup.test.ts @@ -40,7 +40,7 @@ describe('Private Groups', () => { account1 = `eip155:${signer1.address}`; userAlice = await PushAPI.initialize(signer1, { env: _env, - alpha: { feature: [Constants.ALPHA_FEATURES.GROUP_SCALABILITY] }, + alpha: { feature: [Constants.ALPHA_FEATURES.SCALABILITY_V2] }, }); const WALLET2 = ethers.Wallet.createRandom(); @@ -48,7 +48,7 @@ describe('Private Groups', () => { account2 = `eip155:${signer2.address}`; userBob = await PushAPI.initialize(signer2, { env: _env, - alpha: { feature: [Constants.ALPHA_FEATURES.GROUP_SCALABILITY] }, + alpha: { feature: [Constants.ALPHA_FEATURES.SCALABILITY_V2] }, }); const WALLET3 = ethers.Wallet.createRandom(); @@ -56,7 +56,7 @@ describe('Private Groups', () => { account3 = `eip155:${signer3.address}`; userJohn = await PushAPI.initialize(signer3, { env: _env, - alpha: { feature: [Constants.ALPHA_FEATURES.GROUP_SCALABILITY] }, + alpha: { feature: [Constants.ALPHA_FEATURES.SCALABILITY_V2] }, }); group = await userAlice.chat.group.create(groupName, {