Skip to content

Commit

Permalink
fix: merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammeds1992 committed May 23, 2023
2 parents 8c5e4b0 + 0549d3b commit 5d0bc64
Show file tree
Hide file tree
Showing 14 changed files with 214 additions and 67 deletions.
138 changes: 123 additions & 15 deletions packages/examples/sdk-backend-node/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ const updatedNftGroupName = uniqueNamesGenerator({
// Video Data
const videoChainId = +process.env.VIDEO_CHAIN_ID;
let videoData = PushAPI.video.initVideoCallData;
const videoSetData: (fn:(data:PushAPI.VideoCallData) => PushAPI.VideoCallData) => void = (fn) => {
const videoSetData: (
fn: (data: PushAPI.VideoCallData) => PushAPI.VideoCallData
) => void = (fn) => {
videoData = fn(videoData);
}
};
let videoObject = null;
const videoLocalStream = null; // get the local stream
const videoSenderAddress = process.env.VIDEO_SENDER_ADDRESS;
Expand Down Expand Up @@ -424,11 +426,14 @@ async function runChatUseCases() {
await PushAPI_chat_requests();

console.log('PushAPI.chat.send');
await PushAPI_chat_send();
const TargetChatId = await PushAPI_chat_send();

console.log('PushAPI.chat.approve');
await PushAPI_chat_approve();

console.log('PushAPI chat Video call Notification');
await PushAPI_chat_video_call_notification(TargetChatId);

console.log('PushAPI.chat.createGroup');
const chatId = await PushAPI_chat_createGroup();

Expand Down Expand Up @@ -694,6 +699,7 @@ async function PushAPI_chat_send(silent = !showAPIResponse) {
if (!silent) {
console.log(response);
}
return response.chatId;
}

// Push Chat - Approve
Expand Down Expand Up @@ -951,6 +957,55 @@ async function PushChatSDKSocket(silent = !showAPIResponse) {
await delay(4000);
}

async function PushAPI_chat_video_call_notification(
chatId: string,
silent = !showAPIResponse
) {
// Fetch user
const user = await PushAPI.user.get({
account: signer.address,
env: env as ENV,
});

// Decrypt PGP Key
const pgpDecrpyptedPvtKey = await PushAPI.chat.decryptPGPKey({
encryptedPGPPrivateKey: user.encryptedPrivateKey,
signer: signer,
});
// get PGP KEy
const apiResponse = await PushAPI.payloads.sendNotification({
senderType: 1,
signer: signer,
pgpPrivateKey: pgpDecrpyptedPvtKey,
chatId: chatId,
type: 3, // target
identityType: 2, // direct payload
notification: {
title: `VC TITLE:`,
body: `VC BODY`,
},
payload: {
title: `payload title`,
body: `sample msg body`,
cta: '',
img: '',
additionalMeta: {
type: '1+1',
data: 'Random DATA',
domain: 'push.org',
},
},
recipients: signerSecondAccount.address, // recipient address
channel: signer.address, // your channel address
env: env as ENV,
});

console.log('PushAPI.payloads.sendNotification | Response - 204 OK');
if (!silent) {
console.log(apiResponse);
}
}

// Push Chat - Run Chat Use cases
async function runNFTChatUseCases() {
console.log(`
Expand All @@ -976,11 +1031,14 @@ async function runNFTChatUseCases() {
await PushAPI_nft_chat_requests();

console.log('PushAPI.chat.send');
await PushAPI_nft_chat_send();
const TargetchatId = await PushAPI_nft_chat_send();

console.log('PushAPI.chat.approve');
await PushAPI_nft_chat_approve();

console.log('NFT Video Call Notification');
await PushAPI_nft_chat_video_call_notification(TargetchatId);

console.log('PushAPI.chat.createGroup');
const chatId = await PushAPI_nft_chat_createGroup();

Expand Down Expand Up @@ -1245,6 +1303,7 @@ async function PushAPI_nft_chat_send(silent = !showAPIResponse) {
if (!silent) {
console.log(response);
}
return response.chatId;
}

// Push Chat - Approve
Expand Down Expand Up @@ -1278,6 +1337,55 @@ async function PushAPI_nft_chat_approve(silent = !showAPIResponse) {
}
}

async function PushAPI_nft_chat_video_call_notification(
chatId: string,
silent = !showAPIResponse
) {
// Fetch user
const user = await PushAPI.user.get({
account: nftAccount1,
env: env as ENV,
});

// Decrypt PGP Key
const pgpDecrpyptedPvtKey = await PushAPI.chat.decryptPGPKey({
encryptedPGPPrivateKey: user.encryptedPrivateKey,
signer: nftSigner1,
});

const apiResponse = await PushAPI.payloads.sendNotification({
senderType: 1,
signer: nftSigner1,
pgpPrivateKey: pgpDecrpyptedPvtKey,
chatId: chatId,
type: 1, // target
identityType: 2, // direct payload
notification: {
title: `VC TITLE:`,
body: `VC BODY`,
},
payload: {
title: `payload title`,
body: `sample msg body`,
cta: '',
img: '',
additionalMeta: {
type: '1+1',
data: 'Random DATA',
domain: 'push.org',
},
},
recipients: nftAccount1, // recipient address
channel: nftAccount1, // your channel address
env: env as ENV,
});

console.log('PushAPI.payloads.sendNotification | Response - 204 OK');
if (!silent) {
console.log(apiResponse);
}
}

// Push Chat - PushAPI.chat.createGroup
async function PushAPI_nft_chat_createGroup(
silent = !showAPIResponse
Expand Down Expand Up @@ -1498,7 +1606,7 @@ async function PushNFTChatSDKSocket(silent = !showAPIResponse) {
}

// Push Video - Run Video Use cases
async function runVideoUseCases(){
async function runVideoUseCases() {
console.log(`
██╗ ██╗██╗██████╗ ███████╗ ██████╗
██║ ██║██║██╔══██╗██╔════╝██╔═══██╗
Expand Down Expand Up @@ -1530,7 +1638,7 @@ async function runVideoUseCases(){
await PushVideoSDKSocket();
}

async function PushAPI_video_object_init(){
async function PushAPI_video_object_init() {
// Fetch user
const user = await PushAPI.user.get({
account: `eip155:${signer.address}`,
Expand All @@ -1556,10 +1664,10 @@ async function PushAPI_video_object_init(){
return videoObject;
}

async function PushAPI_video_create(){
async function PushAPI_video_create() {
await videoObject.create({
stream: videoLocalStream
});
stream: videoLocalStream,
});
}

async function PushAPI_video_request() {
Expand All @@ -1570,7 +1678,7 @@ async function PushAPI_video_request() {
});
}

async function PushAPI_video_accept_request(){
async function PushAPI_video_accept_request() {
videoObject.acceptRequest({
signalData: videoSignalData_1,
senderAddress: videoRecipientAddress,
Expand All @@ -1579,13 +1687,13 @@ async function PushAPI_video_accept_request(){
});
}

async function PushAPI_video_connect(){
async function PushAPI_video_connect() {
videoObject.connect({
signalData: {} // signalData from sockets
})
signalData: {}, // signalData from sockets
});
}

async function PushAPI_video_disconnect(){
async function PushAPI_video_disconnect() {
videoObject.disconnect();
}

Expand Down Expand Up @@ -1660,7 +1768,7 @@ function start() {
runNotificaitonsUseCases().then(() => {
runChatUseCases().then(() => {
runNFTChatUseCases().then(() => {
if(videoLocalStream !== null){
if (videoLocalStream !== null) {
/*
- One instance of videoObject corresponds to one user/peer of the call
- For a wallet-to-wallet video call we need 2 such users/peers
Expand Down
22 changes: 19 additions & 3 deletions packages/restapi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,33 @@ This file was generated using [@jscutlery/semver](https://github.com/jscutlery/s
### Bug Fixes

* add deprecated info to chat.chat ([#392](https://github.com/ethereum-push-notification-service/push-sdk/issues/392)) ([d84ae64](https://github.com/ethereum-push-notification-service/push-sdk/commit/d84ae64c116adb7eea9e8faf7929b6eb3b74c657))
* Merge branch 'alpha' into alpha-deployment ([549fd83](https://github.com/ethereum-push-notification-service/push-sdk/commit/549fd839867d84a98a10206a7f9945aeb1720e49))
* Merge branch 'main' into alpha ([72464fd](https://github.com/ethereum-push-notification-service/push-sdk/commit/72464fd97260b612649035e3b241f946de8f9e92))
* show hardcoded msg rather than throwing error ([#388](https://github.com/ethereum-push-notification-service/push-sdk/issues/388)) ([8962e10](https://github.com/ethereum-push-notification-service/push-sdk/commit/8962e10036916d0bc161324f5684f74bb7b74dc3))
* **video:** set trickle to true while creating peer instance ([#397](https://github.com/ethereum-push-notification-service/push-sdk/issues/397)) ([0b609be](https://github.com/ethereum-push-notification-service/push-sdk/commit/0b609bec87a6187d1e7cbf8052a2b9a36b9fcd7a))



## [1.2.15](https://github.com/ethereum-push-notification-service/push-sdk/compare/restapi-1.2.14...restapi-1.2.15) (2023-05-12)
## [0.0.1-alpha.7](https://github.com/ethereum-push-notification-service/push-sdk/compare/restapi-0.0.1-alpha.6...restapi-0.0.1-alpha.7) (2023-05-15)


### Bug Fixes

* fix eip712v2 ([#385](https://github.com/ethereum-push-notification-service/push-sdk/issues/385)) ([42c10ba](https://github.com/ethereum-push-notification-service/push-sdk/commit/42c10ba6f5331c9caf5db7136ba94d1a6ed05e10))
* Merge branch 'main' into deployment ([3250a7c](https://github.com/ethereum-push-notification-service/push-sdk/commit/3250a7cfc3bb8fefd630179cd204deef023a5e6c))
* Merge branch 'alpha' into alpha-deployment ([37c1c8d](https://github.com/ethereum-push-notification-service/push-sdk/commit/37c1c8db579876f5835c2e5bb5e6baf073dc9208))
* **video:** remove status change upon connect in request ([#394](https://github.com/ethereum-push-notification-service/push-sdk/issues/394)) ([7441ef0](https://github.com/ethereum-push-notification-service/push-sdk/commit/7441ef0fb2c9ed39e12a4f1819ade0ee7e0d6b10))



## [0.0.1-alpha.6](https://github.com/ethereum-push-notification-service/push-sdk/compare/restapi-0.0.1-alpha.5...restapi-0.0.1-alpha.6) (2023-05-14)


### Bug Fixes

* add video call retry logic upon error & doc: add video call in restapi/readme ([#391](https://github.com/ethereum-push-notification-service/push-sdk/issues/391)) ([9c8c86b](https://github.com/ethereum-push-notification-service/push-sdk/commit/9c8c86b35d1cb0300d0170e87931dd31a15f9342))



## [0.0.1-alpha.5](https://github.com/ethereum-push-notification-service/push-sdk/compare/restapi-0.0.1-alpha.4...restapi-0.0.1-alpha.5) (2023-05-14)



Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/src/lib/channels/_getSubscribers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const _getSubscribers = async (
env = Constants.ENV.PROD,
} = options || {};

const _channelAddress = getCAIPAddress(env, channel, 'Channel');
const _channelAddress = await getCAIPAddress(env, channel, 'Channel');

const channelCAIPDetails = getCAIPDetails(_channelAddress);
if (!channelCAIPDetails) throw Error('Invalid Channel CAIP!');
Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/src/lib/channels/getChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const getChannel = async (
env = Constants.ENV.PROD,
} = options || {};

const _channel = getCAIPAddress(env, channel, 'Channel');
const _channel = await getCAIPAddress(env, channel, 'Channel');
const API_BASE_URL = getAPIBaseUrls(env);
const apiEndpoint = `${API_BASE_URL}/v1/channels`;
const requestUrl = `${apiEndpoint}/${_channel}`;
Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/src/lib/channels/getDelegates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const getDelegates = async (
env = Constants.ENV.PROD,
} = options || {};

const _channel = getCAIPAddress(env, channel, 'Channel');
const _channel = await getCAIPAddress(env, channel, 'Channel');
const API_BASE_URL = getAPIBaseUrls(env);
const apiEndpoint = `${API_BASE_URL}/v1/channels`;
const requestUrl = `${apiEndpoint}/${_channel}/delegates`;
Expand Down
2 changes: 1 addition & 1 deletion packages/restapi/src/lib/channels/getSubscribers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const getSubscribers = async (
if (limit > 30) {
throw new Error("limit must be lesser than or equal to 30");
}
const _channel = getCAIPAddress(env, channel, 'Channel');
const _channel = await getCAIPAddress(env, channel, 'Channel');
const API_BASE_URL = getAPIBaseUrls(env);
const apiEndpoint = `${API_BASE_URL}/v1/channels/${_channel}/subscribers?page=${page}&limit=${limit}`;
return await axios.get(apiEndpoint)
Expand Down
4 changes: 2 additions & 2 deletions packages/restapi/src/lib/channels/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ export const subscribe = async (
} = options || {};

try {
const _channelAddress = getCAIPAddress(env, channelAddress, 'Channel');
const _channelAddress = await getCAIPAddress(env, channelAddress, 'Channel');

const channelCAIPDetails = getCAIPDetails(_channelAddress);
if (!channelCAIPDetails) throw Error('Invalid Channel CAIP!');

const chainId = parseInt(channelCAIPDetails.networkId, 10);

const _userAddress = getCAIPAddress(env, userAddress, 'User');
const _userAddress = await getCAIPAddress(env, userAddress, 'User');

const userCAIPDetails = getCAIPDetails(_userAddress);
if (!userCAIPDetails) throw Error('Invalid User CAIP!');
Expand Down
4 changes: 2 additions & 2 deletions packages/restapi/src/lib/channels/unsubscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ export const unsubscribe = async (
} = options || {};

try {
const _channelAddress = getCAIPAddress(env, channelAddress, 'Channel');
const _channelAddress = await getCAIPAddress(env, channelAddress, 'Channel');

const channelCAIPDetails = getCAIPDetails(_channelAddress);
if (!channelCAIPDetails) throw Error('Invalid Channel CAIP!');

const chainId = parseInt(channelCAIPDetails.networkId, 10);

const _userAddress = getCAIPAddress(env, userAddress, 'User');
const _userAddress = await getCAIPAddress(env, userAddress, 'User');

const userCAIPDetails = getCAIPDetails(_userAddress);
if (!userCAIPDetails) throw Error('Invalid User CAIP!');
Expand Down
8 changes: 7 additions & 1 deletion packages/restapi/src/lib/helpers/address.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as ethers from 'ethers';
import Constants, {ENV} from '../constants';
import { getUserDID } from '../chat/helpers';

export interface AddressValidatorsType {
[key: string]: ({ address } : { address: string }) => boolean;
Expand Down Expand Up @@ -66,6 +67,8 @@ export function validateCAIP(addressInCAIP: string) {
if (!networkId) return false;
if (!address) return false;

if(isValidCAIP10NFTAddress(addressInCAIP))return true;

const validatorFn = AddressValidators[blockchain];

return validatorFn({ address });
Expand Down Expand Up @@ -115,7 +118,10 @@ export function getFallbackETHCAIPAddress(env: ENV, address: string) {
* else
* throw error!
*/
export function getCAIPAddress(env: ENV, address: string, msg?: string) {
export async function getCAIPAddress(env: ENV, address: string, msg?: string) {
if(isValidCAIP10NFTAddress(address)){
return await getUserDID(address, env);
}
if (validateCAIP(address)) {
return address;
} else {
Expand Down
Loading

0 comments on commit 5d0bc64

Please sign in to comment.