Skip to content

Commit

Permalink
1119 read only mode bug fix (#1120)
Browse files Browse the repository at this point in the history
* fix: optimize chat.list (#1115)

* fix: read only guest mode

* fix: review comments

---------

Co-authored-by: Aman Gupta <[email protected]>
  • Loading branch information
mohammeds1992 and Aman035 authored Feb 21, 2024
1 parent ca38a40 commit b7305b3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 77 deletions.
37 changes: 6 additions & 31 deletions packages/restapi/src/lib/chat/helpers/inbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,51 +41,26 @@ export const getInboxLists = async (
pgpHelper = PGP.PGPHelper
): Promise<IFeeds[]> => {
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,
pgpPrivateKey,
pgpHelper,
env,
});
}
return feeds;
};

Expand Down
62 changes: 31 additions & 31 deletions packages/restapi/src/lib/pushapi/PushAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
}

Expand Down
28 changes: 14 additions & 14 deletions packages/restapi/src/lib/pushapi/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class Chat {
}

async send(recipient: string, options: Message): Promise<MessageWithCID> {
if (!this.signer) {
if (!this.decryptedPgpPvtKey) {
throw new Error(PushAPI.ensureSignerMessage());
}

Expand All @@ -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({
Expand All @@ -173,7 +173,7 @@ export class Chat {
}

async accept(target: string): Promise<string> {
if (!this.signer) {
if (!this.decryptedPgpPvtKey) {
throw new Error(PushAPI.ensureSignerMessage());
}
return await PUSH_CHAT.approve({
Expand All @@ -187,7 +187,7 @@ export class Chat {
}

async reject(target: string): Promise<void> {
if (!this.signer) {
if (!this.decryptedPgpPvtKey) {
throw new Error(PushAPI.ensureSignerMessage());
}
await PUSH_CHAT.reject({
Expand All @@ -200,7 +200,7 @@ export class Chat {
}

async block(users: Array<string>): Promise<IUser> {
if (!this.signer || !this.decryptedPgpPvtKey) {
if (!this.decryptedPgpPvtKey) {
throw new Error(PushAPI.ensureSignerMessage());
}
const user = await PUSH_USER.get({
Expand Down Expand Up @@ -241,7 +241,7 @@ export class Chat {
}

async unblock(users: Array<string>): Promise<IUser> {
if (!this.signer || !this.decryptedPgpPvtKey) {
if (!this.decryptedPgpPvtKey) {
throw new Error(PushAPI.ensureSignerMessage());
}
const user = await PUSH_USER.get({
Expand Down Expand Up @@ -305,7 +305,7 @@ export class Chat {
name: string,
options?: GroupCreationOptions
): Promise<GroupInfoDTO | GroupDTO> => {
if (!this.signer) {
if (!this.decryptedPgpPvtKey) {
throw new Error(PushAPI.ensureSignerMessage());
}

Expand Down Expand Up @@ -421,7 +421,7 @@ export class Chat {
chatId: string,
options: GroupUpdateOptions
): Promise<GroupInfoDTO | GroupDTO> => {
if (!this.signer) {
if (!this.decryptedPgpPvtKey) {
throw new Error(PushAPI.ensureSignerMessage());
}

Expand Down Expand Up @@ -474,7 +474,7 @@ export class Chat {
chatId: string,
options: ManageGroupOptions
): Promise<GroupInfoDTO | GroupDTO> => {
if (!this.signer) {
if (!this.decryptedPgpPvtKey) {
throw new Error(PushAPI.ensureSignerMessage());
}
const { role, accounts } = options;
Expand Down Expand Up @@ -533,7 +533,7 @@ export class Chat {
): Promise<GroupInfoDTO | GroupDTO> => {
const { accounts } = options;

if (!this.signer) {
if (!this.decryptedPgpPvtKey) {
throw new Error(PushAPI.ensureSignerMessage());
}

Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -622,7 +622,7 @@ export class Chat {
},

join: async (target: string): Promise<GroupInfoDTO | GroupDTO> => {
if (!this.signer) {
if (!this.decryptedPgpPvtKey) {
throw new Error(PushAPI.ensureSignerMessage());
}
const status = await PUSH_CHAT.getGroupMemberStatus({
Expand Down Expand Up @@ -655,7 +655,7 @@ export class Chat {
},

leave: async (target: string): Promise<GroupInfoDTO | GroupDTO> => {
if (!this.signer) {
if (!this.decryptedPgpPvtKey) {
throw new Error(PushAPI.ensureSignerMessage());
}

Expand Down Expand Up @@ -700,7 +700,7 @@ export class Chat {
},

reject: async (target: string): Promise<void> => {
if (!this.signer) {
if (!this.decryptedPgpPvtKey) {
throw new Error(PushAPI.ensureSignerMessage());
}
await PUSH_CHAT.reject({
Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/src/lib/pushstream/PushStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down

0 comments on commit b7305b3

Please sign in to comment.