Skip to content

Commit

Permalink
fix: fix videoCall Notif (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman035 authored Apr 19, 2023
1 parent 385b3dd commit 069783e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 70 deletions.
34 changes: 17 additions & 17 deletions packages/restapi/src/lib/payloads/constants.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
export interface ChainIdToSourceType {
[key: number]: string
[key: number]: string;
}

export const CHAIN_ID_TO_SOURCE: ChainIdToSourceType = {
1: "ETH_MAINNET",
5: "ETH_TEST_GOERLI",
137: "POLYGON_MAINNET",
80001: "POLYGON_TEST_MUMBAI",
56: "BSC_MAINNET",
97: "BSC_TESTNET",
10: "OPTIMISM_MAINNET",
420: "OPTIMISM_TESTNET"
1: 'ETH_MAINNET',
5: 'ETH_TEST_GOERLI',
137: 'POLYGON_MAINNET',
80001: 'POLYGON_TEST_MUMBAI',
56: 'BSC_MAINNET',
97: 'BSC_TESTNET',
10: 'OPTIMISM_MAINNET',
420: 'OPTIMISM_TESTNET',
};

export const SOURCE_TYPES = {
Expand All @@ -22,19 +22,19 @@ export const SOURCE_TYPES = {
BSC_TESTNET: 'BSC_TESTNET',
OPTIMISM_MAINNET: 'OPTIMISM_MAINNET',
OPTIMISM_TESTNET: 'OPTIMISM_TESTNET',
THE_GRAPH: 'THE_GRAPH'
THE_GRAPH: 'THE_GRAPH',
PUSH_VIDEO: 'PUSH_VIDEO',
};

export enum IDENTITY_TYPE {
export enum IDENTITY_TYPE {
MINIMAL = 0,
IPFS = 1,
DIRECT_PAYLOAD = 2,
SUBGRAPH = 3
};
SUBGRAPH = 3,
}

export enum NOTIFICATION_TYPE {
export enum NOTIFICATION_TYPE {
BROADCAST = 1,
TARGETTED = 3,
SUBSET = 4
};

SUBSET = 4,
}
115 changes: 64 additions & 51 deletions packages/restapi/src/lib/payloads/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { v4 as uuidv4 } from 'uuid';
import { ENV } from '../constants';
import { generateHash, getCAIPAddress } from '../helpers';
import { getCAIPAddress } from '../helpers';
import * as CryptoJS from 'crypto-js';

import {
ISendNotificationInputOptions,
Expand Down Expand Up @@ -195,68 +196,73 @@ export async function getVerificationProof({
pgpPrivateKey?: string;
env?: ENV;
}) {
// console.log('payload ---> \n\n', payload);

const type = {
Data: [{ name: 'data', type: 'string' }],
};
const domain = {
name: 'EPNS COMM V1',
chainId: chainId,
verifyingContract: verifyingContract,
};

let message = null;
let signature = null;
let verificationProof = null;

if (identityType === IDENTITY_TYPE.MINIMAL) {
message = {
data: `${identityType}+${notificationType}+${payload.notification.title}+${payload.notification.body}`,
};
signature = await signer._signTypedData(domain, type, message);
return `eip712v2:${signature}::uid::${uuid}`;
} else if (identityType === IDENTITY_TYPE.IPFS) {
message = {
data: `1+${ipfsHash}`,
};
signature = await signer._signTypedData(domain, type, message);
return `eip712v2:${signature}::uid::${uuid}`;
} else if (identityType === IDENTITY_TYPE.DIRECT_PAYLOAD) {
const payloadJSON = JSON.stringify(payload);
message = {
data: `2+${payloadJSON}`,
};
switch (identityType) {
case IDENTITY_TYPE.MINIMAL: {
message = {
data: `${identityType}+${notificationType}+${payload.notification.title}+${payload.notification.body}`,
};
break;
}
case IDENTITY_TYPE.IPFS: {
message = {
data: `1+${ipfsHash}`,
};
break;
}
case IDENTITY_TYPE.DIRECT_PAYLOAD: {
const payloadJSON = JSON.stringify(payload);
message = {
data: `2+${payloadJSON}`,
};
break;
}
case IDENTITY_TYPE.SUBGRAPH: {
message = {
data: `3+graph:${graph?.id}+${graph?.counter}`,
};
break;
}
default: {
throw new Error('Invalid IdentityType');
}
}

if (senderType === 1) {
// chat notification
// generate the pgpv2 verification proof
switch (senderType) {
case 0: {
const type = {
Data: [{ name: 'data', type: 'string' }],
};
const domain = {
name: 'EPNS COMM V1',
chainId: chainId,
verifyingContract: verifyingContract,
};
const signature = await signer._signTypedData(domain, type, message);
verificationProof = `eip712v2:${signature}::uid::${uuid}`;
break;
}
case 1: {
const connectedUser = await getConnectedUser(
wallet!,
pgpPrivateKey!,
env!
);

const hash = generateHash(JSON.stringify(message)).toString();
signature = await sign({
const hash = CryptoJS.SHA256(JSON.stringify(message)).toString();
const signature = await sign({
message: hash,
signingKey: connectedUser.privateKey!,
});
return `pgpv2:${signature}:meta:${chatId}::uid::${uuid}`;
verificationProof = `pgpv2:${signature}:meta:${chatId}::uid::${uuid}`;
break;
}
default: {
throw new Error('Invalid SenderType');
}

// channel notification
// generate eip712 verification proof
signature = await signer._signTypedData(domain, type, message);
return `eip712v2:${signature}::uid::${uuid}`;
} else if (identityType === IDENTITY_TYPE.SUBGRAPH) {
message = {
data: `3+graph:${graph?.id}+${graph?.counter}`,
};
signature = await signer._signTypedData(domain, type, message);
return `eip712v2:${signature}::uid::${uuid}`;
}

return signature;
return verificationProof;
}

export function getPayloadIdentity({
Expand Down Expand Up @@ -286,7 +292,14 @@ export function getPayloadIdentity({
return null;
}

export function getSource(chainId: number, identityType: IDENTITY_TYPE) {
export function getSource(
chainId: number,
identityType: IDENTITY_TYPE,
senderType: 0 | 1
) {
if (senderType === 1) {
return SOURCE_TYPES.PUSH_VIDEO;
}
if (identityType === IDENTITY_TYPE.SUBGRAPH) {
return SOURCE_TYPES.THE_GRAPH;
}
Expand Down
7 changes: 5 additions & 2 deletions packages/restapi/src/lib/payloads/sendNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,15 @@ export async function sendNotification(options: ISendNotificationInputOptions) {
ipfsHash,
});

const source = getSource(chainId, identityType);
const source = getSource(chainId, identityType, senderType);

const apiPayload = {
verificationProof,
identity,
sender: _channelAddress,
sender:
senderType === 1
? `${channelCAIPDetails?.blockchain}:${channelCAIPDetails?.address}`
: _channelAddress,
source,
/** note this recipient key has a different expectation from the BE API, see the funciton for more */
recipient: getRecipientFieldForAPIPayload({
Expand Down

0 comments on commit 069783e

Please sign in to comment.