Skip to content
Merged
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
80 changes: 53 additions & 27 deletions packages/restapi/src/lib/channels/subscribeV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
import Constants, { ENV } from '../constants';
import { SignerType } from '../types';
import { axiosPost } from '../utils/axiosUtil';
import { sign } from '../chat/helpers';
import * as CryptoJS from 'crypto-js';

export type SubscribeOptionsV2Type = {
signer: SignerType;
Expand All @@ -18,6 +20,7 @@ export type SubscribeOptionsV2Type = {
origin?: string;
onSuccess?: () => void;
onError?: (err: Error) => void;
pgpPrivateKey?: string;
};

export const subscribeV2 = async (options: SubscribeOptionsV2Type) => {
Expand All @@ -31,6 +34,7 @@ export const subscribeV2 = async (options: SubscribeOptionsV2Type) => {
origin,
onSuccess,
onError,
pgpPrivateKey,
} = options || {};
try {
const _channelAddress = await getCAIPAddress(
Expand All @@ -47,6 +51,7 @@ export const subscribeV2 = async (options: SubscribeOptionsV2Type) => {
const _userAddress = await getCAIPAddress(env, userAddress, 'User');

const userCAIPDetails = getCAIPDetails(_userAddress);

if (!userCAIPDetails) throw Error('Invalid User CAIP!');

const { API_BASE_URL, EPNS_COMMUNICATOR_CONTRACT } = getConfig(
Expand All @@ -55,39 +60,60 @@ export const subscribeV2 = async (options: SubscribeOptionsV2Type) => {
);

const requestUrl = `${API_BASE_URL}/v1/channels/${_channelAddress}/subscribe`;
// get domain information
const domainInformation = getDomainInformation(
chainId,
verifyingContractAddress || EPNS_COMMUNICATOR_CONTRACT
);

// get type information
const typeInformation = getTypeInformationV2();

// get message
const messageInformation = {
data: getSubscriptionMessageV2(
let verificationProof: string;
let messageInformation: { data: any } = { data: {} };
if (pgpPrivateKey) {
const data = getSubscriptionMessageV2(
channelCAIPDetails.address,
userCAIPDetails.address,
_userAddress,
'Subscribe',
settings
),
};
// sign a message using EIP712
const pushSigner = new Signer(signer);
const signature = await pushSigner.signTypedData(
domainInformation,
typeInformation,
messageInformation,
'Data'
);

const verificationProof = signature; // might change
);
messageInformation = {
data: data,
};
const hash = CryptoJS.SHA256(
JSON.stringify(messageInformation)
).toString();
console.log(' hash ', hash);
const signature = await sign({
message: hash,
signingKey: pgpPrivateKey!,
});
verificationProof = `pgpv4:${signature}`;
} else {
const domainInformation = getDomainInformation(
chainId,
verifyingContractAddress || EPNS_COMMUNICATOR_CONTRACT
);

// get type information
const typeInformation = getTypeInformationV2();

messageInformation = {
data: getSubscriptionMessageV2(
channelCAIPDetails.address,
userCAIPDetails.address,
'Subscribe',
settings
),
};
// Existing EIP712 flow
const pushSigner = new Signer(signer);
const signature = await pushSigner.signTypedData(
domainInformation,
typeInformation,
messageInformation,
'Data'
);
verificationProof = `eip712v2:${signature}`;
}

const body = {
verificationProof: `eip712v2:${verificationProof}`,
verificationProof: verificationProof,
message: messageInformation.data,
origin: origin
origin: origin,
};

const res = await axiosPost(requestUrl, body);
Expand All @@ -103,4 +129,4 @@ export const subscribeV2 = async (options: SubscribeOptionsV2Type) => {
message: err instanceof Error ? err.message : JSON.stringify(err),
};
}
};
};
60 changes: 43 additions & 17 deletions packages/restapi/src/lib/channels/unsubscribeV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
import Constants, { ENV } from '../constants';
import { SignerType } from '../types';
import { axiosPost } from '../utils/axiosUtil';
import { sign } from '../chat/helpers';
import * as CryptoJS from 'crypto-js';

export type UnSubscribeOptionsV2Type = {
signer: SignerType;
Expand All @@ -16,6 +18,7 @@ export type UnSubscribeOptionsV2Type = {
env?: ENV;
onSuccess?: () => void;
onError?: (err: Error) => void;
pgpPrivateKey?: string;
};

export const unsubscribeV2 = async (options: UnSubscribeOptionsV2Type) => {
Expand All @@ -27,6 +30,7 @@ export const unsubscribeV2 = async (options: UnSubscribeOptionsV2Type) => {
env = Constants.ENV.PROD,
onSuccess,
onError,
pgpPrivateKey,
} = options || {};

try {
Expand Down Expand Up @@ -63,27 +67,49 @@ export const unsubscribeV2 = async (options: UnSubscribeOptionsV2Type) => {
const typeInformation = getTypeInformationV2();

// get message
const messageInformation = {
data: getSubscriptionMessageV2(
let messageInformation: { data: any } = { data: {} };

let verificationProof: string;
if (pgpPrivateKey) {
// TODO: Entire payload: domainInformation typeInformation messageInformation
// TODO: Add chainid
const data = getSubscriptionMessageV2(
channelCAIPDetails.address,
userCAIPDetails.address,
_userAddress,
'Unsubscribe'
),
};

// sign a message using EIP712
const pushSigner = new Signer(signer);
const signature = await pushSigner.signTypedData(
domainInformation,
typeInformation,
messageInformation,
'Data'
);

const verificationProof = signature; // might change
);
messageInformation = {
data: data,
};
const hash = CryptoJS.SHA256(
JSON.stringify(messageInformation)
).toString();
const signature = await sign({
message: hash,
signingKey: pgpPrivateKey,
});
verificationProof = `pgpv4:${signature}`;
} else {
messageInformation = {
data: getSubscriptionMessageV2(
channelCAIPDetails.address,
userCAIPDetails.address,
'Unsubscribe'
),
};
// Existing EIP712 flow
const pushSigner = new Signer(signer);
const signature = await pushSigner.signTypedData(
domainInformation,
typeInformation,
messageInformation,
'Data'
);
verificationProof = `eip712v2:${signature}`;
}

const body = {
verificationProof: `eip712v2:${verificationProof}`,
verificationProof: verificationProof,
message: messageInformation.data,
};

Expand Down
41 changes: 25 additions & 16 deletions packages/restapi/src/lib/payloads/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,22 +251,31 @@ export async function getVerificationProof({

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 = `eip712v2:${signature}::uid::${uuid}`;
if (pgpPrivateKey) {
const hash = CryptoJS.SHA256(JSON.stringify(message)).toString();
const signature = await sign({
message: hash,
signingKey: pgpPrivateKey!,
});
verificationProof = `pgpv4:${signature}`;
} else {
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 = `eip712v2:${signature}::uid::${uuid}`;
}
break;
}
case 1: {
Expand Down
23 changes: 16 additions & 7 deletions packages/restapi/src/lib/pushNotification/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ export class Channel extends PushNotificationBaseClass {
public alias!: Alias;
public tags!: Tags;

constructor(signer?: SignerType, env?: ENV, account?: string) {
super(signer, env, account);
constructor(
signer?: SignerType,
env?: ENV,
account?: string,
pgpPrivateKey?: string
) {
super(signer, env, account, pgpPrivateKey);
this.delegate = new Delegate(signer, env, account);
this.alias = new Alias(signer, env, account);
this.tags = new Tags(this, signer, env, account);
Expand Down Expand Up @@ -79,7 +84,7 @@ export class Channel extends PushNotificationBaseClass {
limit = Constants.PAGINATION.LIMIT,
filter,
tag,
oldFormat = true
oldFormat = true,
} = options || {};
return await PUSH_CHANNEL.search({
query: query,
Expand All @@ -88,7 +93,7 @@ export class Channel extends PushNotificationBaseClass {
filter: filter,
tag: tag,
env: this.env,
oldFormat
oldFormat,
});
} catch (error) {
throw new Error(`Push SDK Error: API : channel::search : ${error}`);
Expand Down Expand Up @@ -145,6 +150,10 @@ export class Channel extends PushNotificationBaseClass {
options.channel! ?? this.account
);

options.advanced = options.advanced || {};
options.advanced.pgpPrivateKey =
options.advanced.pgpPrivateKey ?? this.pgpPrivateKey;

const lowLevelPayload = this.generateNotificationLowLevelPayload({
signer: this.signer!,
env: this.env!,
Expand Down Expand Up @@ -219,7 +228,7 @@ export class Channel extends PushNotificationBaseClass {
url: url,
icon: icon,
aliasDetails: aliasInfo ?? {},
tags
tags,
};
const cid = await this.uploadToIPFSViaPushNode(JSON.stringify(input));
const allowanceAmount = await this.fetchAllownace(
Expand Down Expand Up @@ -325,7 +334,7 @@ export class Channel extends PushNotificationBaseClass {
url: url,
icon: icon,
aliasDetails: aliasInfo ?? {},
tags
tags,
};
const cid = await this.uploadToIPFSViaPushNode(JSON.stringify(input));
// approve the tokens to core contract
Expand Down Expand Up @@ -489,7 +498,7 @@ export class Channel extends PushNotificationBaseClass {
sort,
order,
filter,
tag
tag,
});
} catch (error) {
throw new Error(`Push SDK Error: Contract : channel::list : ${error}`);
Expand Down
11 changes: 9 additions & 2 deletions packages/restapi/src/lib/pushNotification/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ export const FEED_MAP = {
SPAM: true,
};
export class Notification extends PushNotificationBaseClass {
constructor(signer?: SignerType, env?: ENV, account?: string) {
super(signer, env, account);
constructor(
signer?: SignerType,
env?: ENV,
account?: string,
pgpPrivateKey?: string
) {
super(signer, env, account, pgpPrivateKey);
}

/**
Expand Down Expand Up @@ -175,6 +180,7 @@ export class Notification extends PushNotificationBaseClass {
settings: minimalSetting ?? '',
onSuccess: onSuccess,
onError: onError,
pgpPrivateKey: this.pgpPrivateKey,
});
} catch (error) {
throw new Error(
Expand Down Expand Up @@ -225,6 +231,7 @@ export class Notification extends PushNotificationBaseClass {
env: this.env,
onSuccess: onSuccess,
onError: onError,
pgpPrivateKey: this.pgpPrivateKey,
});
} catch (error) {
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import { PushAPI } from '../pushapi/PushAPI';
import { channel } from 'diagnostics_channel';
import * as viem from 'viem';


// ERROR CONSTANTS
const ERROR_ACCOUNT_NEEDED = 'Account is required';
const ERROR_SIGNER_NEEDED = 'Signer object is required';
Expand All @@ -61,12 +60,19 @@ export class PushNotificationBaseClass {
protected env: ENV | undefined;
protected guestMode: boolean;
protected coreContract: any;
protected pgpPrivateKey?: string;

constructor(signer?: SignerType, env?: ENV, account?: string) {
constructor(
signer?: SignerType,
env?: ENV,
account?: string,
pgpPrivateKey?: string
) {
this.signer = signer;
this.env = env;
this.guestMode = !!(account && signer);
this.account = account;
this.pgpPrivateKey = pgpPrivateKey;
this.initializeCoreContract({ signer: this.signer, env: this.env });
}

Expand Down
Loading
Loading