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

chore: added ethersV6 support for examples #969

Merged
merged 1 commit into from
Jan 3, 2024
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
10 changes: 5 additions & 5 deletions packages/examples/sdk-backend-node/chat/chat.lowlevel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -638,11 +638,11 @@ async function PushAPI_chat_video_call_notification(
senderType: 1,
signer,
pgpPrivateKey: pgpDecrpyptedPvtKey,
rules:{
access:{
rules: {
access: {
type: VIDEO_NOTIFICATION_ACCESS_TYPE.PUSH_CHAT,
data: chatId
}
data: chatId,
},
},
type: 3, // target
identityType: 2, // direct payload
Expand All @@ -657,7 +657,7 @@ async function PushAPI_chat_video_call_notification(
img: '',
additionalMeta: {
type: '1+1',
data: "DATA REQUIRED FOR VIDEO CALL",
data: 'DATA REQUIRED FOR VIDEO CALL',
domain: 'push.org',
},
},
Expand Down
61 changes: 40 additions & 21 deletions packages/examples/sdk-backend-node/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,55 @@ import { createWalletClient, http } from 'viem';
import { sepolia } from 'viem/chains';
import { STREAM } from '@pushprotocol/restapi/src/lib/pushstream/pushStreamTypes';
import { PushStream } from '@pushprotocol/restapi/src/lib/pushstream/PushStream';
import { ethers } from 'ethers';

// CONFIGS
const { env, showAPIResponse } = config;

/***************** SAMPLE SIGNER GENERATION *********************/
// Uing VIEM
// Random Wallet Signers
const signer = createWalletClient({
account: privateKeyToAccount(generatePrivateKey()),
chain: sepolia,
transport: http(),
});
const signerAddress = signer.account.address;
const secondSigner = createWalletClient({
account: privateKeyToAccount(generatePrivateKey()),
chain: sepolia,
transport: http(),
});
const secondSignerAddress = secondSigner.account.address;
const thirdSigner = createWalletClient({
account: privateKeyToAccount(generatePrivateKey()),
chain: sepolia,
transport: http(),
});
const thirdSignerAddress = thirdSigner.account.address;

// const signer = createWalletClient({
// account: privateKeyToAccount(generatePrivateKey()),
// chain: sepolia,
// transport: http(),
// });
// const signerAddress = signer.account.address;
// const secondSigner = createWalletClient({
// account: privateKeyToAccount(generatePrivateKey()),
// chain: sepolia,
// transport: http(),
// });
// const secondSignerAddress = secondSigner.account.address;
// const thirdSigner = createWalletClient({
// account: privateKeyToAccount(generatePrivateKey()),
// chain: sepolia,
// 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 SIGNER GENERATION *********************/
// Uing ETHERS
// Random Wallet Signers

const signer = ethers.Wallet.createRandom();
const signerAddress = signer.address;
const secondSigner = ethers.Wallet.createRandom();
const secondSignerAddress = secondSigner.address;
const thirdSigner = ethers.Wallet.createRandom();
const thirdSignerAddress = thirdSigner.address;

// Dummy Wallet Addresses
const randomWallet1 = privateKeyToAccount(generatePrivateKey()).address;
const randomWallet2 = privateKeyToAccount(generatePrivateKey()).address;
const randomWallet3 = privateKeyToAccount(generatePrivateKey()).address;
const randomWallet1 = ethers.Wallet.createRandom().address;
const randomWallet2 = ethers.Wallet.createRandom().address;
const randomWallet3 = ethers.Wallet.createRandom().address;
/****************************************************************/

/***************** SAMPLE GROUP DATA ****************************/
Expand Down
50 changes: 41 additions & 9 deletions packages/examples/sdk-backend-node/chat/nftChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {
import { config } from '../config';
import { PushStream } from '@pushprotocol/restapi/src/lib/pushstream/PushStream';
import { ethers } from 'ethers';
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
import { createWalletClient, http } from 'viem';
import { sepolia } from 'viem/chains';

// CONFIGS
const { env, showAPIResponse } = config;
Expand All @@ -18,7 +21,6 @@ const nftContractAddress1 = process.env.NFT_CONTRACT_ADDRESS_1 || '';
const nftTokenId1 = process.env.NFT_TOKEN_ID_1 || '';
const nftHolderWalletPrivatekey1 =
process.env.NFT_HOLDER_WALLET_PRIVATE_KEY_1 || '';
const nftSigner1 = new ethers.Wallet(`0x${nftHolderWalletPrivatekey1}`);
// NFT Account structure for Push Chat : nft:eip155:{chainId}:{contractAddress}:{tokenId}
const nftAccount1 = `nft:eip155:${nftChainId1}:${nftContractAddress1}:${nftTokenId1}`;
// NFT Profile Password ( Used for recovery in case of NFT transfers )
Expand All @@ -29,7 +31,6 @@ const nftContractAddress2 = process.env.NFT_CONTRACT_ADDRESS_2 || '';
const nftTokenId2 = process.env.NFT_TOKEN_ID_2 || '';
const nftHolderWalletPrivatekey2 =
process.env.NFT_HOLDER_WALLET_PRIVATE_KEY_2 || '';
const nftSigner2 = new ethers.Wallet(`0x${nftHolderWalletPrivatekey2}`);
const nftAccount2 = `nft:eip155:${nftChainId2}:${nftContractAddress2}:${nftTokenId2}`;
const nftProfilePassword2 = process.env.NFT_PROFILE_PASSWORD_2 || '';

Expand All @@ -38,13 +39,44 @@ const nftContractAddress3 = process.env.NFT_CONTRACT_ADDRESS_3 || '';
const nftTokenId3 = process.env.NFT_TOKEN_ID_3 || '';
const nftHolderWalletPrivatekey3 =
process.env.NFT_HOLDER_WALLET_PRIVATE_KEY_3 || '';
const nftSigner3 = new ethers.Wallet(`0x${nftHolderWalletPrivatekey3}`);
const nftAccount3 = `nft:eip155:${nftChainId3}:${nftContractAddress3}:${nftTokenId3}`;
const nftProfilePassword3 = process.env.NFT_PROFILE_PASSWORD_3 || '';
/****************************************************************/

/***************** SAMPLE SIGNER GENERATION *********************/
// Uing ETHERS

// const nftSigner1 = new ethers.Wallet(`0x${nftHolderWalletPrivatekey1}`);
// const nftSigner2 = new ethers.Wallet(`0x${nftHolderWalletPrivatekey2}`);
// const nftSigner3 = new ethers.Wallet(`0x${nftHolderWalletPrivatekey3}`);
// // Dummy Wallet Addresses
// const randomWallet1 = ethers.Wallet.createRandom().address;
// const randomWallet2 = ethers.Wallet.createRandom().address;
// const randomWallet3 = ethers.Wallet.createRandom().address;
/****************************************************************/

const randomWallet1 = ethers.Wallet.createRandom().address;
const randomWallet2 = ethers.Wallet.createRandom().address;
const randomWallet3 = ethers.Wallet.createRandom().address;
/***************** SAMPLE SIGNER GENERATION *********************/
// Uing VIEM

const nftSigner1 = createWalletClient({
account: privateKeyToAccount(`0x${nftHolderWalletPrivatekey1}`),
chain: sepolia,
transport: http(),
});
const nftSigner2 = createWalletClient({
account: privateKeyToAccount(`0x${nftHolderWalletPrivatekey2}`),
chain: sepolia,
transport: http(),
});
const nftSigner3 = createWalletClient({
account: privateKeyToAccount(`0x${nftHolderWalletPrivatekey3}`),
chain: sepolia,
transport: http(),
});
// Dummy Wallet Addresses
const randomWallet1 = privateKeyToAccount(generatePrivateKey()).address;
const randomWallet2 = privateKeyToAccount(generatePrivateKey()).address;
const randomWallet3 = privateKeyToAccount(generatePrivateKey()).address;
/****************************************************************/

/***************** SAMPLE GROUP DATA ****************************/
Expand Down Expand Up @@ -110,7 +142,7 @@ export const runNFTChatClassUseCases = async (): Promise<void> => {
const userAlice = await PushAPI.initialize(nftSigner1, {
env,
account: nftAccount1,
versionMeta: { NFTPGP_V1: { password: nftProfilePassword1 } },
versionMeta: { NFTPGP_V1: { password: 'wrongPass' } },
});

const stream = await userAlice.initStream(
Expand Down Expand Up @@ -144,13 +176,13 @@ export const runNFTChatClassUseCases = async (): Promise<void> => {
const userBob = await await PushAPI.initialize(nftSigner2, {
env,
account: nftAccount2,
versionMeta: { NFTPGP_V1: { password: nftProfilePassword2 } },
// versionMeta: { NFTPGP_V1: { password: nftProfilePassword2 } },
});

const userKate = await await PushAPI.initialize(nftSigner3, {
env,
account: nftAccount3,
versionMeta: { NFTPGP_V1: { password: nftProfilePassword3 } },
// versionMeta: { NFTPGP_V1: { password: nftProfilePassword3 } },
});

// Listen stream events to receive websocket events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,11 @@ export const runNotificaitonsLowLevelUseCases = async (): Promise<void> => {
console.log('PushAPI.channels._getSubscribers()');
await PushAPI_channels_getSubscribers();

console.log('Push Notification - PushSDKSocket()');
await PushSDKSocket();
/**
* @notice - can only be used with ethersV5
*/
// console.log('Push Notification - PushSDKSocket()');
// await PushSDKSocket();
}
};

Expand Down Expand Up @@ -280,7 +283,10 @@ async function PushAPI_payloads_sendNotification__direct_payload_group_of_recipi
cta: '',
img: '',
},
recipients: [`eip155:11155111:${signerAddress}`, `eip155:11155111:${randomWallet1}`], // recipient addresses
recipients: [
`eip155:11155111:${signerAddress}`,
`eip155:11155111:${randomWallet1}`,
], // recipient addresses
channel: `eip155:11155111:${channelAddress}`, // your channel address
env: env as ENV,
});
Expand Down
37 changes: 31 additions & 6 deletions packages/examples/sdk-backend-node/notification/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { config } from '../config';
import { ethers } from 'ethers';
import { STREAM } from '@pushprotocol/restapi/src/lib/pushstream/pushStreamTypes';
import { PushStream } from '@pushprotocol/restapi/src/lib/pushstream/PushStream';
import { privateKeyToAccount } from 'viem/accounts';
import { createWalletClient, http, formatEther } from 'viem';
import { sepolia } from 'viem/chains';

// CONFIGS
const { env, showAPIResponse } = config;
Expand Down Expand Up @@ -32,13 +35,35 @@ export const runNotificationClassUseCases = async (): Promise<void> => {
// -------------------------------------------------------------------
// -------------------------------------------------------------------
// Signer Generation
const provider = new ethers.providers.JsonRpcProvider(
'https://rpc.sepolia.org' // Goerli Provider

// ETHERS V5
// const provider = new ethers.providers.JsonRpcProvider(
// 'https://rpc.sepolia.org' // Sepolia Provider
// );
// const signer = new ethers.Wallet(
// `0x${process.env.WALLET_PRIVATE_KEY}`,
// provider
// );
// const address = signer.address

// ETHERS V6
const provider = new ethers.JsonRpcProvider(
'https://rpc.sepolia.org' // Sepolia Provider
);
const signer = new ethers.Wallet(
`0x${process.env.WALLET_PRIVATE_KEY}`,
provider
);
const address = signer.address;

// VIEM
// const signer = createWalletClient({
// account: privateKeyToAccount(`0x${process.env.WALLET_PRIVATE_KEY}`),
// chain: sepolia,
// transport: http(),
// });
// const address = signer.account.address;

const randomWallet1 = ethers.Wallet.createRandom().address;
const randomWallet2 = ethers.Wallet.createRandom().address;
// -------------------------------------------------------------------
Expand Down Expand Up @@ -125,15 +150,15 @@ export const runNotificationClassUseCases = async (): Promise<void> => {
},
});
await delay(3000); // Delay added to log the events in order
const targetedNotif = await userAlice.channel.send([signer.address], {
const targetedNotif = await userAlice.channel.send([address], {
notification: {
title: 'test',
body: 'test',
},
});
await delay(3000); // Delay added to log the events in order
const subsetNotif = await userAlice.channel.send(
[randomWallet1, randomWallet2, signer.address],
[randomWallet1, randomWallet2, address],
{
notification: {
title: 'test',
Expand All @@ -154,8 +179,8 @@ export const runNotificationClassUseCases = async (): Promise<void> => {
// -------------------------------------------------------------------
// -------------------------------------------------------------------
// These Examples requires wallet to hold some ETH & PUSH
const balance = await provider.getBalance(signer.address);
if (parseFloat(ethers.utils.formatEther(balance)) < 0.001) {
const balance = await provider.getBalance(address);
if (parseFloat(formatEther(balance)) < 0.001) {
console.log(
'skipping PushAPI.channel examples, wallet does not have enough balance to pay fee'
);
Expand Down
5 changes: 3 additions & 2 deletions packages/examples/sdk-backend-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"author": "",
"license": "ISC",
"dependencies": {
"@pushprotocol/restapi": "1.5.1",
"@pushprotocol/socket": "^0.5.2"
"@pushprotocol/restapi": "0.0.1-alpha.62",
"@pushprotocol/socket": "^0.5.2",
"ethers": "^6.9.2"
}
}
Loading
Loading