diff --git a/packages/restapi/src/lib/chat/helpers/inbox.ts b/packages/restapi/src/lib/chat/helpers/inbox.ts index c2e0314cd..6a3412600 100644 --- a/packages/restapi/src/lib/chat/helpers/inbox.ts +++ b/packages/restapi/src/lib/chat/helpers/inbox.ts @@ -41,44 +41,18 @@ export const getInboxLists = async ( pgpHelper = PGP.PGPHelper ): Promise => { const { - lists, + lists: feeds, user, toDecrypt, pgpPrivateKey, env = Constants.ENV.PROD, } = options || {}; - const connectedUser = await getUser({ account: pCAIP10ToWallet(user), env }); - const feeds: IFeeds[] = []; - for (const list of lists) { - let message; - if (list.threadhash !== null) { - message = await getCID(list.threadhash, { env }); - } - // This is for groups that are created without any message - else { - message = { - encType: 'PlainText', - encryptedSecret: '', - fromCAIP10: '', - fromDID: '', - link: '', - messageContent: '', - messageType: '', - sigType: '', - signature: '', - toCAIP10: '', - toDID: '', - }; - } - feeds.push({ - ...list, - msg: message, - groupInformation: list.groupInformation, + if (toDecrypt) { + const connectedUser = await getUser({ + account: pCAIP10ToWallet(user), + env, }); - } - - if (toDecrypt) return decryptFeeds({ feeds, connectedUser, @@ -86,6 +60,7 @@ export const getInboxLists = async ( pgpHelper, env, }); + } return feeds; }; diff --git a/packages/restapi/src/lib/pushapi/PushAPI.ts b/packages/restapi/src/lib/pushapi/PushAPI.ts index 231274b5b..f22bd6694 100644 --- a/packages/restapi/src/lib/pushapi/PushAPI.ts +++ b/packages/restapi/src/lib/pushapi/PushAPI.ts @@ -218,8 +218,8 @@ export class PushAPI { } if (!readMode) { - if (user && user.encryptedPrivateKey) { - try { + try { + if (user && user.encryptedPrivateKey) { decryptedPGPPrivateKey = await PUSH_CHAT.decryptPGPKey({ encryptedPGPPrivateKey: user.encryptedPrivateKey, signer: signer, @@ -228,38 +228,38 @@ export class PushAPI { progressHook: settings.progressHook, env: settings.env, }); - } catch (error) { - const decryptionError = - 'Error decrypting PGP private key ...swiching to Guest mode'; - initializationErrors.push({ - type: 'ERROR', - message: decryptionError, + } else { + const newUser = await PUSH_USER.create({ + env: settings.env, + account: derivedAccount, + signer, + version: settings.version, + additionalMeta: settings.versionMeta, + origin: settings.origin, + progressHook: settings.progressHook, }); - console.error(decryptionError); - if (isValidCAIP10NFTAddress(derivedAccount)) { - const nftDecryptionError = - 'NFT Account Detected. If this NFT was recently transferred to you, please ensure you have received the correct password from the previous owner. Alternatively, you can reinitialize for a fresh start. Please be aware that reinitialization will result in the loss of all previous account data.'; - - initializationErrors.push({ - type: 'WARN', - message: nftDecryptionError, - }); - console.warn(nftDecryptionError); - } - readMode = true; + decryptedPGPPrivateKey = newUser.decryptedPrivateKey as string; + pgpPublicKey = newUser.publicKey; } - } else { - const newUser = await PUSH_USER.create({ - env: settings.env, - account: derivedAccount, - signer, - version: settings.version, - additionalMeta: settings.versionMeta, - origin: settings.origin, - progressHook: settings.progressHook, + } catch (error) { + const decryptionError = + 'Error decrypting PGP private key ...swiching to Guest mode'; + initializationErrors.push({ + type: 'ERROR', + message: decryptionError, }); - decryptedPGPPrivateKey = newUser.decryptedPrivateKey as string; - pgpPublicKey = newUser.publicKey; + console.error(decryptionError); + if (isValidCAIP10NFTAddress(derivedAccount)) { + const nftDecryptionError = + 'NFT Account Detected. If this NFT was recently transferred to you, please ensure you have received the correct password from the previous owner. Alternatively, you can reinitialize for a fresh start. Please be aware that reinitialization will result in the loss of all previous account data.'; + + initializationErrors.push({ + type: 'WARN', + message: nftDecryptionError, + }); + console.warn(nftDecryptionError); + } + readMode = true; } } diff --git a/packages/restapi/src/lib/pushapi/chat.ts b/packages/restapi/src/lib/pushapi/chat.ts index ded9a93c7..556c308e2 100644 --- a/packages/restapi/src/lib/pushapi/chat.ts +++ b/packages/restapi/src/lib/pushapi/chat.ts @@ -141,7 +141,7 @@ export class Chat { } async send(recipient: string, options: Message): Promise { - if (!this.signer) { + if (!this.decryptedPgpPvtKey) { throw new Error(PushAPI.ensureSignerMessage()); } @@ -160,7 +160,7 @@ export class Chat { } async decrypt(messagePayloads: IMessageIPFS[]) { - if (!this.signer) { + if (!this.decryptedPgpPvtKey) { throw new Error(PushAPI.ensureSignerMessage()); } return await PUSH_CHAT.decryptConversation({ @@ -173,7 +173,7 @@ export class Chat { } async accept(target: string): Promise { - if (!this.signer) { + if (!this.decryptedPgpPvtKey) { throw new Error(PushAPI.ensureSignerMessage()); } return await PUSH_CHAT.approve({ @@ -187,7 +187,7 @@ export class Chat { } async reject(target: string): Promise { - if (!this.signer) { + if (!this.decryptedPgpPvtKey) { throw new Error(PushAPI.ensureSignerMessage()); } await PUSH_CHAT.reject({ @@ -200,7 +200,7 @@ export class Chat { } async block(users: Array): Promise { - if (!this.signer || !this.decryptedPgpPvtKey) { + if (!this.decryptedPgpPvtKey) { throw new Error(PushAPI.ensureSignerMessage()); } const user = await PUSH_USER.get({ @@ -241,7 +241,7 @@ export class Chat { } async unblock(users: Array): Promise { - if (!this.signer || !this.decryptedPgpPvtKey) { + if (!this.decryptedPgpPvtKey) { throw new Error(PushAPI.ensureSignerMessage()); } const user = await PUSH_USER.get({ @@ -305,7 +305,7 @@ export class Chat { name: string, options?: GroupCreationOptions ): Promise => { - if (!this.signer) { + if (!this.decryptedPgpPvtKey) { throw new Error(PushAPI.ensureSignerMessage()); } @@ -421,7 +421,7 @@ export class Chat { chatId: string, options: GroupUpdateOptions ): Promise => { - if (!this.signer) { + if (!this.decryptedPgpPvtKey) { throw new Error(PushAPI.ensureSignerMessage()); } @@ -474,7 +474,7 @@ export class Chat { chatId: string, options: ManageGroupOptions ): Promise => { - if (!this.signer) { + if (!this.decryptedPgpPvtKey) { throw new Error(PushAPI.ensureSignerMessage()); } const { role, accounts } = options; @@ -533,7 +533,7 @@ export class Chat { ): Promise => { const { accounts } = options; - if (!this.signer) { + if (!this.decryptedPgpPvtKey) { throw new Error(PushAPI.ensureSignerMessage()); } @@ -591,7 +591,7 @@ export class Chat { modify: async (chatId: string, options: ManageGroupOptions) => { const { role, accounts } = options; - if (!this.signer) { + if (!this.decryptedPgpPvtKey) { throw new Error(PushAPI.ensureSignerMessage()); } const validRoles = ['ADMIN', 'MEMBER']; @@ -622,7 +622,7 @@ export class Chat { }, join: async (target: string): Promise => { - if (!this.signer) { + if (!this.decryptedPgpPvtKey) { throw new Error(PushAPI.ensureSignerMessage()); } const status = await PUSH_CHAT.getGroupMemberStatus({ @@ -655,7 +655,7 @@ export class Chat { }, leave: async (target: string): Promise => { - if (!this.signer) { + if (!this.decryptedPgpPvtKey) { throw new Error(PushAPI.ensureSignerMessage()); } @@ -700,7 +700,7 @@ export class Chat { }, reject: async (target: string): Promise => { - if (!this.signer) { + if (!this.decryptedPgpPvtKey) { throw new Error(PushAPI.ensureSignerMessage()); } await PUSH_CHAT.reject({ diff --git a/packages/restapi/src/lib/pushstream/PushStream.ts b/packages/restapi/src/lib/pushstream/PushStream.ts index 258a8c64b..5bb7420c2 100644 --- a/packages/restapi/src/lib/pushstream/PushStream.ts +++ b/packages/restapi/src/lib/pushstream/PushStream.ts @@ -285,7 +285,7 @@ export class PushStream extends EventEmitter { data.messageCategory == 'Request' ) { // Dont call this if read only mode ? - if (this.signer) { + if (this.decryptedPgpPvtKey) { data = await this.chatInstance.decrypt([data]); data = data[0]; }