diff --git a/packages/examples/sdk-backend-node/main.ts b/packages/examples/sdk-backend-node/main.ts index e9eeaba8b..e8a538268 100644 --- a/packages/examples/sdk-backend-node/main.ts +++ b/packages/examples/sdk-backend-node/main.ts @@ -17,7 +17,7 @@ const start = async (): Promise => { console.log(`${returnENVLog()}`); await runUserCases(); - //await runNotificationUseCases(); + await runNotificationUseCases(); await runChatUseCases(); await runVideoUseCases(); await runSpaceUseCases(); diff --git a/packages/examples/sdk-backend-node/pushAPI/channel.ts b/packages/examples/sdk-backend-node/pushAPI/channel.ts deleted file mode 100644 index c33154a92..000000000 --- a/packages/examples/sdk-backend-node/pushAPI/channel.ts +++ /dev/null @@ -1,192 +0,0 @@ -import { PushAPI } from '@pushprotocol/restapi'; -import { config } from '../config'; -import { ethers } from 'ethers'; - -// CONFIGS -const { env, showAPIResponse } = config; - -export const runPushAPIChannelCases = async (): Promise => { - if (!process.env.WALLET_PRIVATE_KEY) { - console.log( - 'skipping PushAPI.channel examples, no private key passed in .env' - ); - return; - } - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - // Signer Generation - const provider = new ethers.providers.JsonRpcProvider( - 'https://goerli.blockpi.network/v1/rpc/public' // Goerli Provider - ); - const signer = new ethers.Wallet( - `0x${process.env.WALLET_PRIVATE_KEY}`, - provider - ); - const randomWallet1 = ethers.Wallet.createRandom().address; - const randomWallet2 = ethers.Wallet.createRandom().address; - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - const userAlice = await PushAPI.initialize(signer, { env }); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.channel.info'); - const channelInfo = await userAlice.channel.info(); - if (showAPIResponse) { - console.log(channelInfo); - } - console.log('PushAPI.channel.info | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.channel.search'); - const searchedChannels = await userAlice.channel.search( - 'push' // search by name or address - ); - if (showAPIResponse) { - console.log(searchedChannels); - } - console.log('PushAPI.channel.search | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.channel.subscribers'); - const channelSubscribers = await userAlice.channel.subscribers(); - if (showAPIResponse) { - console.log(channelSubscribers); - } - console.log('PushAPI.channel.subscribers | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.channel.send'); - if (channelInfo) { - const broadcastNotif = await userAlice.channel.send(['*'], { - notification: { - title: 'test', - body: 'test', - }, - }); - const targetedNotif = await userAlice.channel.send([randomWallet1], { - notification: { - title: 'test', - body: 'test', - }, - }); - const subsetNotif = await userAlice.channel.send( - [randomWallet1, randomWallet2], - { - notification: { - title: 'test', - body: 'test', - }, - } - ); - if (showAPIResponse) { - console.log(broadcastNotif, targetedNotif, subsetNotif); - } - console.log('PushAPI.channel.send | Response - 200 OK\n\n'); - } else { - console.log( - 'skipping PushAPI.channel.send as no channel exists with the signer\n\n' - ); - } - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - // These Examples requires wallet to hold some ETH & PUSH - const balance = await provider.getBalance(signer.address); - if (parseFloat(ethers.utils.formatEther(balance)) < 0.001) { - console.log( - 'skipping PushAPI.channel examples, wallet does not have enough balance to pay fee' - ); - } - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.channel.create'); - if (channelInfo) { - console.log('skipping PushAPI.channel.create as it already exists\n\n'); - } else { - const createdChannel = await userAlice.channel.create({ - name: 'Test Channel', - description: 'Test Description', - icon: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAz0lEQVR4AcXBsU0EQQyG0e+saWJ7oACiKYDMEZVs6GgSpC2BIhzRwAS0sgk9HKn3gpFOAv3v3V4/3+4U4Z1q5KTy42Ql940qvFONnFSGmCFmiN2+fj7uCBlihpgh1ngwcvKfwjuVIWaIGWKNB+GdauSk8uNkJfeNKryzYogZYoZY40m5b/wlQ8wQM8TayMlKeKcaOVkJ71QjJyuGmCFmiDUe+HFy4VyEd57hx0mV+0ZliBlihlgL71w4FyMnVXhnZeSkiu93qheuDDFDzBD7BcCyMAOfy204AAAAAElFTkSuQmCC', - url: 'https://push.org', - }); - if (showAPIResponse) { - console.log(createdChannel); - } - console.log('PushAPI.channel.create | Response - 200 OK\n\n'); - } - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.channel.update'); - const updatedChannel = await userAlice.channel.update({ - name: 'Updated Name', - description: 'Testing new description', - url: 'https://google.com', - icon: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAz0lEQVR4AcXBsU0EQQyG0e+saWJ7oACiKYDMEZVs6GgSpC2BIhzRwAS0sgk9HKn3gpFOAv3v3V4/3+4U4Z1q5KTy42Ql940qvFONnFSGmCFmiN2+fj7uCBlihpgh1ngwcvKfwjuVIWaIGWKNB+GdauSk8uNkJfeNKryzYogZYoZY40m5b/wlQ8wQM8TayMlKeKcaOVkJ71QjJyuGmCFmiDUe+HFy4VyEd57hx0mV+0ZliBlihlgL71w4FyMnVXhnZeSkiu93qheuDDFDzBD7BcCyMAOfy204AAAAAElFTkSuQmCC', - }); - if (showAPIResponse) { - console.log(updatedChannel); - } - console.log('PushAPI.channel.update | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.channel.verify'); - // only verified channels can verify other channels (otherwise this action is skipped by sdk) - if (channelInfo.verified_status) { - const verifiedTrx = await userAlice.channel.verify( - '0x35B84d6848D16415177c64D64504663b998A6ab4' - ); - if (showAPIResponse) { - console.log(verifiedTrx); - } - } - console.log('PushAPI.channel.verify | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.channel.setting'); - const channelSettingTrx = await userAlice.channel.setting([ - { type: 1, default: 1, description: 'My Notif Settings' }, - ]); - if (showAPIResponse) { - console.log(channelSettingTrx); - } - console.log('PushAPI.channel.setting | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.channel.delegate.add'); - const addedDelegate = await userAlice.channel.delegate.add( - `eip155:5:${randomWallet1}` - ); - - if (showAPIResponse) { - console.log(addedDelegate); - } - console.log('PushAPI.channel.delegate.add | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.channel.delegate.get'); - const delegates = await userAlice.channel.delegate.get(); - if (showAPIResponse) { - console.log(delegates); - } - console.log('PushAPI.channel.delegate.get | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.channel.delegate.remove'); - const removedDelegate = await userAlice.channel.delegate.remove( - `eip155:5:${randomWallet1}` - ); - if (showAPIResponse) { - console.log(removedDelegate); - } - console.log('PushAPI.channel.delegate.remove | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.channel.alias.info'); - const aliasInfo = await userAlice.channel.alias.info({ - alias: '0x35B84d6848D16415177c64D64504663b998A6ab4', - aliasChain: 'POLYGON', - }); - if (showAPIResponse) { - console.log(aliasInfo); - } - console.log('PushAPI.channel.alias.info | Response - 200 OK\n\n'); -}; diff --git a/packages/examples/sdk-backend-node/pushAPI/chat.ts b/packages/examples/sdk-backend-node/pushAPI/chat.ts deleted file mode 100644 index 7877237a3..000000000 --- a/packages/examples/sdk-backend-node/pushAPI/chat.ts +++ /dev/null @@ -1,227 +0,0 @@ -import { CONSTANTS, PushAPI } from '@pushprotocol/restapi'; -import { - adjectives, - animals, - colors, - uniqueNamesGenerator, -} from 'unique-names-generator'; -import { config } from '../config'; -import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; -import { createWalletClient, http } from 'viem'; -import { goerli } from 'viem/chains'; - -// CONFIGS -const { env, showAPIResponse } = config; - -/***************** SAMPLE SIGNER GENERATION *********************/ -// Uing VIEM -// Random Wallet Signers -const signer = createWalletClient({ - account: privateKeyToAccount(generatePrivateKey()), - chain: goerli, - transport: http(), -}); -const signerAddress = signer.account.address; -const secondSigner = createWalletClient({ - account: privateKeyToAccount(generatePrivateKey()), - chain: goerli, - transport: http(), -}); -const secondSignerAddress = secondSigner.account.address; -const thirdSigner = createWalletClient({ - account: privateKeyToAccount(generatePrivateKey()), - chain: goerli, - transport: http(), -}); -const thirdSignerAddress = thirdSigner.account.address; - -// Dummy Wallet Addresses -const randomWallet1 = privateKeyToAccount(generatePrivateKey()).address; -const randomWallet2 = privateKeyToAccount(generatePrivateKey()).address; -const randomWallet3 = privateKeyToAccount(generatePrivateKey()).address; -/****************************************************************/ - -/***************** SAMPLE GROUP DATA ****************************/ -const groupName = uniqueNamesGenerator({ - dictionaries: [adjectives, colors, animals], -}); -const groupDescription = uniqueNamesGenerator({ - dictionaries: [adjectives, colors, animals], -}); -const groupImage = - 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAvklEQVR4AcXBsW2FMBiF0Y8r3GQb6jeBxRauYRpo4yGQkMd4A7kg7Z/GUfSKe8703fKDkTATZsJsrr0RlZSJ9r4RLayMvLmJjnQS1d6IhJkwE2bT13U/DBzp5BN73xgRZsJMmM1HOolqb/yWiWpvjJSUiRZWopIykTATZsJs5g+1N6KSMiO1N/5DmAkzYTa9Lh6MhJkwE2ZzSZlo7xvRwson3txERzqJhJkwE2bT6+JhoKTMJ2pvjAgzYSbMfgDlXixqjH6gRgAAAABJRU5ErkJggg=='; -/***************** SAMPLE GROUP DATA ****************************/ - -export const runPushAPIChatCases = async (): Promise => { - const userAlice = await PushAPI.initialize(signer, { env }); - const userBob = await PushAPI.initialize(secondSigner, { env }); - const tempUser = await PushAPI.initialize(thirdSigner, { env }); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.chat.list'); - const aliceChats = await userAlice.chat.list('CHATS'); - const aliceRequests = await userAlice.chat.list('REQUESTS'); - if (showAPIResponse) { - console.log(aliceChats); - console.log(aliceRequests); - } - console.log('PushAPI.chat.list | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.chat.latest'); - const aliceLatestChatWithBob = await userAlice.chat.latest( - secondSignerAddress - ); - if (showAPIResponse) { - console.log(aliceLatestChatWithBob); - } - console.log('PushAPI.chat.latest | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.chat.history'); - const aliceChatHistoryWithBob = await userAlice.chat.history( - secondSignerAddress - ); - if (showAPIResponse) { - console.log(aliceChatHistoryWithBob); - } - console.log('PushAPI.chat.history | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.chat.send'); - const aliceMessagesBob = await userAlice.chat.send(secondSignerAddress, { - content: 'Hello Bob!', - type: CONSTANTS.CHAT.MESSAGE_TYPE.TEXT, - }); - if (showAPIResponse) { - console.log(aliceMessagesBob); - } - console.log('PushAPI.chat.send | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.chat.accept'); - const bobAcceptsRequest = await userBob.chat.accept(signerAddress); - if (showAPIResponse) { - console.log(bobAcceptsRequest); - } - console.log('PushAPI.chat.accept | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.chat.reject'); - await tempUser.chat.send(secondSignerAddress, { - content: 'Sending malicious message', - type: CONSTANTS.CHAT.MESSAGE_TYPE.TEXT, - }); - const bobRejectsRequest = await userBob.chat.reject(thirdSignerAddress); - if (showAPIResponse) { - console.log(bobRejectsRequest); - } - console.log('PushAPI.chat.reject | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.chat.block'); - const AliceBlocksBob = await userAlice.chat.block([secondSignerAddress]); - if (showAPIResponse) { - console.log(AliceBlocksBob); - } - console.log('PushAPI.chat.block | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.chat.unblock'); - const AliceUnblocksBob = await userAlice.chat.unblock([secondSignerAddress]); - if (showAPIResponse) { - console.log(AliceUnblocksBob); - } - console.log('PushAPI.chat.unblock | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.group.create'); - const createdGroup = await userAlice.chat.group.create(groupName, { - description: groupDescription, - image: groupImage, - members: [randomWallet1, randomWallet2], - admins: [], - private: false, - }); - const groupChatId = createdGroup.chatId; // to be used in other examples - if (showAPIResponse) { - console.log(createdGroup); - } - console.log('PushAPI.group.create | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.group.permissions'); - const grouppermissions = await userAlice.chat.group.permissions(groupChatId); - if (showAPIResponse) { - console.log(grouppermissions); - } - console.log('PushAPI.group.permissions | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.group.info'); - const groupInfo = await userAlice.chat.group.info(groupChatId); - if (showAPIResponse) { - console.log(groupInfo); - } - console.log('PushAPI.group.info | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.group.update'); - const updatedGroup = await userAlice.chat.group.update(groupChatId, { - description: 'Updated Description', - }); - if (showAPIResponse) { - console.log(updatedGroup); - } - console.log('PushAPI.group.update | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.group.add'); - const addMember = await userAlice.chat.group.add(groupChatId, { - role: 'MEMBER', - accounts: [randomWallet3], - }); - if (showAPIResponse) { - console.log(addMember); - } - console.log('PushAPI.group.add | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.group.remove'); - const removeMember = await userAlice.chat.group.remove(groupChatId, { - role: 'MEMBER', - accounts: [randomWallet3], - }); - if (showAPIResponse) { - console.log(removeMember); - } - console.log('PushAPI.group.remove | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.group.join'); - const joinGrp = await userBob.chat.group.join(groupChatId); - if (showAPIResponse) { - console.log(joinGrp); - } - console.log('PushAPI.group.join | Response - 200 OK\n\n'); - //------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.group.leave'); - const leaveGrp = await userBob.chat.group.leave(groupChatId); - if (showAPIResponse) { - console.log(leaveGrp); - } - console.log('PushAPI.group.leave | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.group.reject'); - const sampleGrp = await userAlice.chat.group.create('Sample Grp', { - description: groupDescription, - image: groupImage, - members: [secondSignerAddress], // invite bob - admins: [], - private: true, - }); - await userBob.chat.group.reject(sampleGrp.chatId); - console.log('PushAPI.group.reject | Response - 200 OK\n\n'); -}; diff --git a/packages/examples/sdk-backend-node/pushAPI/encryption.ts b/packages/examples/sdk-backend-node/pushAPI/encryption.ts deleted file mode 100644 index 67300c65f..000000000 --- a/packages/examples/sdk-backend-node/pushAPI/encryption.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { PushAPI } from '@pushprotocol/restapi'; -import { config } from '../config'; -import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; -import { createWalletClient, http } from 'viem'; -import { goerli } from 'viem/chains'; - -// CONFIGS -const { env, showAPIResponse } = config; - -/***************** SAMPLE SIGNER GENERATION *********************/ -// Uing VIEM -// Random Wallet Signers -const signer = createWalletClient({ - account: privateKeyToAccount(generatePrivateKey()), - chain: goerli, - transport: http(), -}); - -export const runPushAPIEncryptionCases = async (): Promise => { - const userAlice = await PushAPI.initialize(signer, { env }); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.encryption.info'); - const encryptionInfo = await userAlice.encryption.info(); - if (showAPIResponse) { - console.log(encryptionInfo); - } - console.log('PushAPI.encryption.info | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.encryption.update'); - const PGP_V3 = 'eip191-aes256-gcm-hkdf-sha256'; - const encryptionUpdate = await userAlice.encryption.update(PGP_V3 as any); - if (showAPIResponse) { - console.log(encryptionUpdate); - } - console.log('PushAPI.encryption.update | Response - 200 OK\n\n'); -}; diff --git a/packages/examples/sdk-backend-node/pushAPI/notification.ts b/packages/examples/sdk-backend-node/pushAPI/notification.ts deleted file mode 100644 index 579ea1e2b..000000000 --- a/packages/examples/sdk-backend-node/pushAPI/notification.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { PushAPI } from '@pushprotocol/restapi'; -import { config } from '../config'; -import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; -import { createWalletClient, http } from 'viem'; -import { goerli } from 'viem/chains'; - -// CONFIGS -const { env, showAPIResponse } = config; - -/***************** SAMPLE SIGNER GENERATION *********************/ -// Uing VIEM -// Random Wallet Signers -const signer = createWalletClient({ - account: privateKeyToAccount(generatePrivateKey()), - chain: goerli, - transport: http(), -}); - -export const runPushAPINotificationCases = async (): Promise => { - const userAlice = await PushAPI.initialize(signer, { env }); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.notification.list'); - const inboxNotifications = await userAlice.notification.list('INBOX'); - const spamNotifications = await userAlice.notification.list('SPAM'); - if (showAPIResponse) { - console.log(inboxNotifications, spamNotifications); - } - console.log('PushAPI.notification.list | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.notification.subscribe'); - const subscribeResponse = await userAlice.notification.subscribe( - 'eip155:5:0xD8634C39BBFd4033c0d3289C4515275102423681' // channel to subscribe - ); - if (showAPIResponse) { - console.log(subscribeResponse); - } - console.log('PushAPI.notification.subscribe | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.notification.subscriptions'); - const aliceSubscriptions = await userAlice.notification.subscriptions(); - if (showAPIResponse) { - console.log(aliceSubscriptions); - } - console.log('PushAPI.notification.subscriptions | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.notification.unsubscribe'); - const unsubscribeResponse = await userAlice.notification.unsubscribe( - 'eip155:5:0xD8634C39BBFd4033c0d3289C4515275102423681' // channel to unsubscribe - ); - if (showAPIResponse) { - console.log(unsubscribeResponse); - } - console.log('PushAPI.notification.unsubscribe | Response - 200 OK\n\n'); -}; diff --git a/packages/examples/sdk-backend-node/pushAPI/profile.ts b/packages/examples/sdk-backend-node/pushAPI/profile.ts deleted file mode 100644 index f72d93f1b..000000000 --- a/packages/examples/sdk-backend-node/pushAPI/profile.ts +++ /dev/null @@ -1,38 +0,0 @@ -import { PushAPI } from '@pushprotocol/restapi'; -import { config } from '../config'; -import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; -import { createWalletClient, http } from 'viem'; -import { goerli } from 'viem/chains'; - -// CONFIGS -const { env, showAPIResponse } = config; - -/***************** SAMPLE SIGNER GENERATION *********************/ -// Uing VIEM -// Random Wallet Signers -const signer = createWalletClient({ - account: privateKeyToAccount(generatePrivateKey()), - chain: goerli, - transport: http(), -}); - -export const runPushAPIProfileCases = async (): Promise => { - const userAlice = await PushAPI.initialize(signer, { env }); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.profile.info'); - const userAliceProfileInfo = await userAlice.profile.info(); - if (showAPIResponse) { - console.log(userAliceProfileInfo); - } - console.log('PushAPI.profile.info | Response - 200 OK\n\n'); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('PushAPI.profile.update'); - const updatedName = 'Bob The Builder'; - const response = await userAlice.profile.update({ name: updatedName }); - if (showAPIResponse) { - console.log(response); - } - console.log('PushAPI.profile.update | Response - 200 OK\n\n'); -}; diff --git a/packages/examples/sdk-backend-node/pushAPI/stream.ts b/packages/examples/sdk-backend-node/pushAPI/stream.ts deleted file mode 100644 index b4e7c164a..000000000 --- a/packages/examples/sdk-backend-node/pushAPI/stream.ts +++ /dev/null @@ -1,225 +0,0 @@ -import { CONSTANTS, PushAPI } from '@pushprotocol/restapi'; -import { config } from '../config'; -import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; -import { createWalletClient, http } from 'viem'; -import { goerli } from 'viem/chains'; -import { STREAM } from '@pushprotocol/restapi/src/lib/pushstream/pushStreamTypes'; -import { PushStream } from '@pushprotocol/restapi/src/lib/pushstream/PushStream'; - -// CONFIGS -const { env, showAPIResponse } = config; - -/***************** SAMPLE SIGNER GENERATION *********************/ -// Uing VIEM -// Random Wallet Signers -const signer = createWalletClient({ - account: privateKeyToAccount(generatePrivateKey()), - chain: goerli, - transport: http(), -}); -const signerAddress = signer.account.address; -const secondSigner = createWalletClient({ - account: privateKeyToAccount(generatePrivateKey()), - chain: goerli, - transport: http(), -}); -const secondSignerAddress = secondSigner.account.address; -const thirdSigner = createWalletClient({ - account: privateKeyToAccount(generatePrivateKey()), - chain: goerli, - transport: http(), -}); -const thirdSignerAddress = thirdSigner.account.address; -// Dummy Wallet Addresses -const randomWallet1 = privateKeyToAccount(generatePrivateKey()).address; - -const eventlistener = async ( - stream: PushStream, - eventName: string -): Promise => { - stream.on(eventName, (data: any) => { - if (showAPIResponse) { - console.log(data); - } - }); -}; - -const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); - -export const runPushAPIStreamCases = async (): Promise => { - const userAlice = await PushAPI.initialize(signer, { env }); - const userBob = await PushAPI.initialize(secondSigner, { env }); - const userKate = await PushAPI.initialize(thirdSigner, { env }); - - - const stream = await userAlice.stream( - [CONSTANTS.STREAM.CHAT, CONSTANTS.STREAM.CHAT_OPS], - { - // stream supports other products as well, such as STREAM.CHAT, STREAM.CHAT_OPS - // more info can be found at push.org/docs/chat - - filter: { - channels: ['*'], - chats: ['*'], - }, - connection: { - auto: false, // should connection be automatic, else need to call stream.connect(); - retries: 3, // number of retries in case of error - }, - raw: true, // enable true to show all data - } - ); - - await stream.connect(); - - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log(`Listening ${STREAM.PROFILE} Events`); - eventlistener(stream, STREAM.PROFILE); - console.log(`Listening ${STREAM.ENCRYPTION} Events`); - eventlistener(stream, STREAM.ENCRYPTION); - console.log(`Listening ${STREAM.NOTIF} Events`); - eventlistener(stream, STREAM.NOTIF); - console.log(`Listening ${STREAM.NOTIF_OPS} Events`); - eventlistener(stream, STREAM.NOTIF_OPS); - console.log(`Listening ${STREAM.CHAT} Events`); - eventlistener(stream, STREAM.CHAT); - console.log(`Listening ${STREAM.CHAT_OPS} Events`); - eventlistener(stream, STREAM.CHAT_OPS); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('\n\nNew Chat Request, Expected Events:\n1. chat.request'); - await userAlice.chat.send(secondSignerAddress, { - content: 'Hello Bob! from Alice', - }); - await delay(3000); // Delay added to log the events in order - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('\n\nNew Chat Request, Expected Events:\n1. chat.request'); - await userAlice.chat.send(thirdSignerAddress, { - content: 'Hello Kate! from Alice', - }); - await delay(3000); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('\n\nChat Request Accept, Expected Events:\n1. chat.accept'); - await userBob.chat.accept(signerAddress); - await delay(3000); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('\n\nChat Request Reject, Expected Events:\n1. chat.reject'); - await userKate.chat.reject(signerAddress); - await delay(3000); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('\n\nCreate Chat Group, Expected Events:\n1. chat.group.create'); - const groupChatId = ( - await userAlice.chat.group.create('Test Grp', { - description: 'Test Desc', - image: - 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAvklEQVR4AcXBsW2FMBiF0Y8r3GQb6jeBxRauYRpo4yGQkMd4A7kg7Z/GUfSKe8703fKDkTATZsJsrr0RlZSJ9r4RLayMvLmJjnQS1d6IhJkwE2bT13U/DBzp5BN73xgRZsJMmM1HOolqb/yWiWpvjJSUiRZWopIykTATZsJs5g+1N6KSMiO1N/5DmAkzYTa9Lh6MhJkwE2ZzSZlo7xvRwson3txERzqJhJkwE2bT6+JhoKTMJ2pvjAgzYSbMfgDlXixqjH6gRgAAAABJRU5ErkJggg==', - members: [secondSignerAddress, thirdSignerAddress], - admins: [], - private: false, - }) - ).chatId; - await delay(3000); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('\n\nUpdate Chat Group, Expected Events:\n1. chat.group.update'); - await userAlice.chat.group.update(groupChatId, { - description: 'Updated Test Desc', - image: - 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAvklEQVR4AcXBsW2FMBiF0Y8r3GQb6jeBxRauYRpo4yGQkMd4A7kg7Z/GUfSKe8703fKDkTATZsJsrr0RlZSJ9r4RLayMvLmJjnQS1d6IhJkwE2bT13U/DBzp5BN73xgRZsJMmM1HOolqb/yWiWpvjJSUiRZWopIykTATZsJs5g+1N6KSMiO1N/5DmAkzYTa9Lh6MhJkwE2ZzSZlo7xvRwson3txERzqJhJkwE2bT6+JhoKTMJ2pvjAgzYSbMfgDlXixqjH6gRgAAAABJRU5ErkJggg==', - }); - await delay(3000); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('\n\nAdd member to Group, Expected Events:\n1. chat.request'); - await userAlice.chat.group.add(groupChatId, { - role: 'MEMBER', - accounts: [randomWallet1], - }); - await delay(3000); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log( - '\n\nRemove member from Group, Expected Events:\n1. chat.group.participant.remove' - ); - await userAlice.chat.group.remove(groupChatId, { - role: 'MEMBER', - accounts: [randomWallet1], - }); - await delay(3000); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('\n\nAdd Admin to Group, Expected Events:\n1. chat.request'); - await userAlice.chat.group.add(groupChatId, { - role: 'ADMIN', - accounts: [randomWallet1], - }); - await delay(3000); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log( - '\n\nRemove Admin from Group, Expected Events:\n1. chat.group.participant.remove' - ); - await userAlice.chat.group.remove(groupChatId, { - role: 'ADMIN', - accounts: [randomWallet1], - }); - await delay(3000); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log('\n\nJoin Group, Expected Events:\n1. chat.accept'); - await userBob.chat.group.join(groupChatId); - await delay(3000); - //------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log( - '\n\nLeave Group, Expected Events:\n1. chat.group.participant.leave' - ); - await userBob.chat.group.leave(groupChatId); - await delay(3000); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - console.log( - '\n\nReject Group Joining Request, Expected Events:\n1. chat.reject' - ); - await userKate.chat.group.reject(groupChatId); - await delay(3000); - // ------------------------------------------------------------------- - // ------------------------------------------------------------------- - if (process.env.WALLET_PRIVATE_KEY) { - // create signer - const channelSigner = createWalletClient({ - account: privateKeyToAccount(`0x${process.env.WALLET_PRIVATE_KEY}`), - chain: goerli, - transport: http(), - }); - - await userAlice.notification.subscribe( - `eip155:5:${channelSigner.account.address}` // channel to subscribe - ); - - const channelUser = await PushAPI.initialize(channelSigner, { env }); - console.log( - '\n\nSend channel notification, Expected Events:\n1. notif.send' - ); - await channelUser.channel.send(['*'], { - notification: { - title: 'test', - body: 'test', - }, - }); - await delay(3000); - - await userAlice.notification.unsubscribe( - `eip155:5:${channelSigner.account.address}` // channel to subscribe - ); - } else { - console.log( - 'Skipping channel notification streams, as WALLET_PRIVATE_KEY is not present in .env' - ); - } -}; diff --git a/packages/restapi/src/lib/pushapi/chat.ts b/packages/restapi/src/lib/pushapi/chat.ts index a15d7c1f0..bedff5a6c 100644 --- a/packages/restapi/src/lib/pushapi/chat.ts +++ b/packages/restapi/src/lib/pushapi/chat.ts @@ -381,7 +381,10 @@ export class Chat { } }, - remove: async (chatId: string, options: RemoveFromGroupOptions) => { + remove: async ( + chatId: string, + options: RemoveFromGroupOptions + ): Promise => { const { accounts } = options; if (!accounts || accounts.length === 0) { @@ -410,9 +413,10 @@ export class Chat { membersToRemove.push(account); } } + let response: any; if (adminsToRemove.length > 0) { - await PUSH_CHAT.removeAdmins({ + response = await PUSH_CHAT.removeAdmins({ chatId: chatId, admins: adminsToRemove, env: this.env, @@ -423,7 +427,7 @@ export class Chat { } if (membersToRemove.length > 0) { - await PUSH_CHAT.removeMembers({ + response = await PUSH_CHAT.removeMembers({ chatId: chatId, members: membersToRemove, env: this.env, @@ -432,6 +436,7 @@ export class Chat { pgpPrivateKey: this.decryptedPgpPvtKey, }); } + return response; }, modify: async (chatId: string, options: ManageGroupOptions) => { diff --git a/packages/restapi/src/lib/pushapi/pushAPITypes.ts b/packages/restapi/src/lib/pushapi/pushAPITypes.ts index 4b4a0b707..ca218da1f 100644 --- a/packages/restapi/src/lib/pushapi/pushAPITypes.ts +++ b/packages/restapi/src/lib/pushapi/pushAPITypes.ts @@ -30,6 +30,7 @@ export interface ManageGroupOptions { } export interface RemoveFromGroupOptions { + role?: 'ADMIN' | 'MEMBER'; accounts: string[]; } diff --git a/packages/restapi/tests/lib/chat/privateGroup.test.ts b/packages/restapi/tests/lib/chat/privateGroup.test.ts index 7b4bf91e7..e49f0f695 100644 --- a/packages/restapi/tests/lib/chat/privateGroup.test.ts +++ b/packages/restapi/tests/lib/chat/privateGroup.test.ts @@ -84,7 +84,6 @@ describe('Private Groups', () => { }); const updatedGroup = await userAlice.chat.group.remove(group.chatId, { - role: 'MEMBER', accounts: [account2], }); expect(updatedGroup.sessionKey).to.be.null; @@ -97,7 +96,6 @@ describe('Private Groups', () => { }); const updatedGroup = await userAlice.chat.group.remove(group.chatId, { - role: 'ADMIN', accounts: [account2], }); expect(updatedGroup.sessionKey).to.be.null; @@ -141,7 +139,6 @@ describe('Private Groups', () => { expect(updatedGroup1.sessionKey).to.not.be.null; const updatedGroup2 = await userAlice.chat.group.remove(group.chatId, { - role: 'MEMBER', accounts: [account2], }); expect(updatedGroup2.sessionKey).to.not.be.null; @@ -159,7 +156,6 @@ describe('Private Groups', () => { expect(updatedGroup1.sessionKey).to.not.be.null; const updatedGroup2 = await userAlice.chat.group.remove(group.chatId, { - role: 'ADMIN', accounts: [account2], }); expect(updatedGroup2.sessionKey).to.not.be.null; @@ -364,7 +360,6 @@ describe('Private Groups', () => { }); await userBob.chat.group.join(group.chatId); await userAlice.chat.group.remove(group.chatId, { - role: 'MEMBER', accounts: [account2, account3], }); await expect( @@ -387,7 +382,6 @@ describe('Private Groups', () => { }); await userBob.chat.group.join(group.chatId); await userBob.chat.group.remove(group.chatId, { - role: 'ADMIN', accounts: [account2, account3], }); await expect( @@ -522,7 +516,6 @@ describe('Private Groups', () => { }); await userAlice.chat.group.remove(group.chatId, { - role: 'ADMIN', accounts: [account2], }); @@ -545,7 +538,6 @@ describe('Private Groups', () => { }); await userAlice.chat.group.remove(group.chatId, { - role: 'MEMBER', accounts: [account2], }); @@ -568,7 +560,6 @@ describe('Private Groups', () => { }); await userAlice.chat.group.remove(group.chatId, { - role: 'ADMIN', accounts: [account2], }); @@ -688,7 +679,6 @@ describe('Private Groups', () => { await userBob.chat.group.join(group.chatId); await userAlice.chat.group.remove(group.chatId, { - role: 'MEMBER', accounts: [account2], }); @@ -713,7 +703,6 @@ describe('Private Groups', () => { // Accept Intent await userBob.chat.group.join(group.chatId); await userAlice.chat.group.remove(group.chatId, { - role: 'ADMIN', accounts: [account2], }); @@ -764,7 +753,6 @@ describe('Private Groups', () => { // Autojoin await userBob.chat.group.join(group.chatId); await userAlice.chat.group.remove(group.chatId, { - role: 'MEMBER', accounts: [account2], }); diff --git a/packages/restapi/tests/lib/stream/initialize.test.ts b/packages/restapi/tests/lib/stream/initialize.test.ts index 7bd872e75..0e797f415 100644 --- a/packages/restapi/tests/lib/stream/initialize.test.ts +++ b/packages/restapi/tests/lib/stream/initialize.test.ts @@ -11,7 +11,7 @@ import CONSTANTS from '../../../src/lib/constantsV2'; import * as util from 'util'; describe('PushStream.initialize functionality', () => { - it.only('Should initialize new stream and listen to events', async () => { + it('Should initialize new stream and listen to events', async () => { const MESSAGE = 'Hey There!!!'; const provider = ethers.getDefaultProvider(); @@ -172,9 +172,9 @@ describe('PushStream.initialize functionality', () => { content: "Gm gm! It's a me... Mario", }); }); - + await stream.connect(); - + stream.on(CONSTANTS.STREAM.DISCONNECT, () => { console.log('Stream Disconnected'); });