Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: added sendNotificationV3 #1343

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions packages/restapi/src/lib/payloads/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,65 @@ export function getPayloadForAPIInput(
return null;
}

/**
* This function will map the Input options passed to the SDK to the "payload" structure
* needed by the API input
*
* We need notificationPayload only for identityType
* - DIRECT_PAYLOAD
* - MINIMAL
*/
export function getPayloadForAPIInputV2(
inputOptions: ISendNotificationInputOptions,
recipients: any
): INotificationPayload | null {
if (inputOptions?.notification && inputOptions?.payload) {
return {
notification: {
title: inputOptions?.notification?.title,
body: inputOptions?.notification?.body,
},
data: {
acta: inputOptions?.payload?.cta || '',
aimg: inputOptions?.payload?.img || '',
amsg: inputOptions?.payload?.body || '',
asub: inputOptions?.payload?.title || '',
type: inputOptions?.type?.toString() || '',
//deprecated
...(inputOptions?.expiry && { etime: inputOptions?.expiry }),
...(inputOptions?.payload?.etime && {
etime: inputOptions?.payload?.etime,
}),
//deprecated
...(inputOptions?.hidden && { hidden: inputOptions?.hidden }),
...(inputOptions?.payload?.hidden && {
hidden: inputOptions?.payload?.hidden,
}),
...(inputOptions?.payload?.silent && {
silent: inputOptions?.payload?.silent,
}),
...(inputOptions?.payload?.sectype && {
sectype: inputOptions?.payload?.sectype,
}),
//deprecated
...(inputOptions?.payload?.metadata && {
metadata: inputOptions?.payload?.metadata,
}),
...(inputOptions?.payload?.additionalMeta && {
additionalMeta: inputOptions?.payload?.additionalMeta,
}),
...(inputOptions?.payload?.index && {
index: inputOptions?.payload?.index,
}),
timestamp: Math.floor(Date.now() / 1000),
},
recipients: recipients,
};
}

return null;
}

/**
* This function returns the recipient format accepted by the API for different notification types
*/
Expand Down Expand Up @@ -285,6 +344,110 @@ export async function getVerificationProof({
return verificationProof;
}

export async function getVerificationProofV2({
senderType,
signer,
chainId,
notificationType,
identityType,
verifyingContract,
payload,
ipfsHash,
graph = {},
uuid,
chatId,
wallet,
pgpPrivateKey,
env,
rules,
}: {
senderType: 0 | 1;
signer: any;
chainId: number;
notificationType: NOTIFICATION_TYPE;
identityType: IDENTITY_TYPE;
verifyingContract: string;
payload: any;
ipfsHash?: string;
graph?: any;
uuid: string;
// for notifications which have additionalMeta in payload
chatId?: string;
wallet?: walletType;
pgpPrivateKey?: string;
env?: ENV;
rules?: VideoNotificationRules;
}) {
let message = null;
let verificationProof = null;

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');
}
}

switch (senderType) {
case 0: {
const type = {
Data: [{ name: 'data', type: 'string' }],
};
const domain = {
name: 'EPNS COMM V1',
chainId: chainId,
verifyingContract: verifyingContract,
};
const pushSigner = new Signer(signer);
const signature = await pushSigner.signTypedData(
domain,
type,
message,
'Data'
);
verificationProof = `eip712v3:${signature}::uid::${uuid}`;
break;
}
case 1: {
const hash = CryptoJS.SHA256(JSON.stringify(message)).toString();
const signature = await sign({
message: hash,
signingKey: pgpPrivateKey!,
});
verificationProof = `pgpv2:${signature}:meta:${chatId}::uid::${uuid}`;
break;
}
default: {
throw new Error('Invalid SenderType');
}
}
return verificationProof;
}

export function getPayloadIdentity({
identityType,
payload,
Expand Down
1 change: 1 addition & 0 deletions packages/restapi/src/lib/payloads/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './sendNotifications';
export {sendNotificationV2} from './sendNotificationsV2';
export {
NOTIFICATION_TYPE,
IDENTITY_TYPE,
Expand Down
Loading
Loading