Skip to content

Commit

Permalink
Pub key cache to speed up chat messages verification (#1081)
Browse files Browse the repository at this point in the history
* fix: minor fixes

* fix: cache implementation for public keys
  • Loading branch information
Aman035 authored Feb 2, 2024
1 parent 32e6cea commit 27165e8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 24 deletions.
47 changes: 33 additions & 14 deletions packages/restapi/src/lib/chat/helpers/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { IPGPHelper } from './pgp';
import { aesDecrypt } from './aes';
import { getEncryptedSecret } from './getEncryptedSecret';
import { getGroup } from '../getGroup';
import { cache } from '../../helpers/cache';

const SIG_TYPE_V2 = 'eip712v2';

Expand Down Expand Up @@ -161,32 +162,50 @@ export const decryptFeeds = async ({
pgpHelper: PGP.IPGPHelper;
env: ENV;
}): Promise<IFeeds[]> => {
let otherPeer: IUser;
let signatureValidationPubliKey: string; // To do signature verification it depends on who has sent the message
for (const feed of feeds) {
let gotOtherPeer = false;
const validateAndDecryptFeed = async (feed: IFeeds) => {
if (!pgpPrivateKey) {
throw new Error('Decrypted private key is necessary');
}

if (feed.msg.encType !== 'PlainText') {
if (!pgpPrivateKey) {
throw Error('Decrypted private key is necessary');
}
if (feed.msg.fromCAIP10 !== connectedUser.wallets.split(',')[0]) {
if (!gotOtherPeer) {
otherPeer = await getUser({ account: feed.msg.fromCAIP10, env });
gotOtherPeer = true;
const senderCAIP10 = feed.msg.fromCAIP10;
const isSenderConnectedUser =
senderCAIP10 === connectedUser.wallets.split(',')[0];
let publicKey: string;

if (!isSenderConnectedUser) {
/**
* CACHE
*/
const cacheKey = `pgpPubKey-${senderCAIP10}`;
// Check if the pubkey is already in the cache
if (cache.has(cacheKey)) {
publicKey = cache.get(cacheKey);
} else {
// If not in cache, fetch from API
const otherPeer = await getUser({ account: senderCAIP10, env });
// Cache the pubkey data
cache.set(cacheKey, otherPeer.publicKey);
publicKey = otherPeer.publicKey;
}
signatureValidationPubliKey = otherPeer!.publicKey!;
} else {
signatureValidationPubliKey = connectedUser.publicKey!;
publicKey = connectedUser.publicKey;
}

feed.msg = await decryptAndVerifyMessage(
feed.msg,
signatureValidationPubliKey,
publicKey,
pgpPrivateKey,
env,
pgpHelper
);
}
};

for (const feed of feeds) {
await validateAndDecryptFeed(feed);
}

return feeds;
};

Expand Down
41 changes: 32 additions & 9 deletions packages/restapi/src/lib/chat/helpers/inbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IFeeds, IMessageIPFS, IUser, SpaceIFeeds } from '../../types';
import { get as getUser } from '../../user';
import { getCID } from '../ipfs';
import { decryptFeeds, decryptAndVerifyMessage } from './crypto';
import { cache } from '../../helpers/cache';

type InboxListsType = {
lists: IFeeds[];
Expand Down Expand Up @@ -78,7 +79,13 @@ export const getInboxLists = async (
}

if (toDecrypt)
return decryptFeeds({ feeds, connectedUser, pgpPrivateKey, pgpHelper, env });
return decryptFeeds({
feeds,
connectedUser,
pgpPrivateKey,
pgpHelper,
env,
});
return feeds;
};

Expand Down Expand Up @@ -123,7 +130,13 @@ export const getSpaceInboxLists = async (
}

if (toDecrypt)
return decryptFeeds({ feeds, connectedUser, pgpPrivateKey, pgpHelper: PGP.PGPHelper, env });
return decryptFeeds({
feeds,
connectedUser,
pgpPrivateKey,
pgpHelper: PGP.PGPHelper,
env,
});
return feeds;
};

Expand Down Expand Up @@ -170,11 +183,9 @@ export const decryptConversation = async (options: DecryptConverationType) => {
pgpHelper = PGP.PGPHelper,
env = Constants.ENV.PROD,
} = options || {};
let otherPeer: IUser;
let signatureValidationPubliKey: string; // To do signature verification it depends on who has sent the message
for (let i = 0; i < messages.length; i++) {
const message = messages[i];
let gotOtherPeer = false;
if (message.encType !== 'PlainText') {
// check if message is already decrypted
if (
Expand All @@ -187,11 +198,23 @@ export const decryptConversation = async (options: DecryptConverationType) => {
throw Error('Decrypted private key is necessary');
}
if (message.fromCAIP10 !== connectedUser.wallets.split(',')[0]) {
if (!gotOtherPeer) {
otherPeer = await getUser({ account: message.fromCAIP10, env });
gotOtherPeer = true;
/**
* CACHE
*/
const cacheKey = `pgpPubKey-${message.fromCAIP10}`;
// Check if the pubkey is already in the cache
if (cache.has(cacheKey)) {
signatureValidationPubliKey = cache.get(cacheKey);
} else {
// If not in cache, fetch from API
const otherPeer = await getUser({
account: message.fromCAIP10,
env,
});
// Cache the pubkey data
cache.set(cacheKey, otherPeer.publicKey);
signatureValidationPubliKey = otherPeer.publicKey;
}
signatureValidationPubliKey = otherPeer!.publicKey;
} else {
signatureValidationPubliKey = connectedUser.publicKey;
}
Expand All @@ -200,7 +223,7 @@ export const decryptConversation = async (options: DecryptConverationType) => {
signatureValidationPubliKey,
pgpPrivateKey,
env,
pgpHelper,
pgpHelper
);
}
}
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion packages/restapi/src/lib/pushapi/PushAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { ALPHA_FEATURE_CONFIG } from '../config';
import { Video } from './video';
import { isValidCAIP10NFTAddress } from '../helpers';
import { LRUCache } from 'lru-cache';
import { cache } from './cache';
import { cache } from '../helpers/cache';

export class PushAPI {
private signer?: SignerType;
Expand Down Expand Up @@ -307,6 +307,7 @@ export class PushAPI {
this.profile = new Profile(
this.account,
this.env,
this.cache,
this.decryptedPgpPvtKey,
this.progressHook
);
Expand Down

0 comments on commit 27165e8

Please sign in to comment.