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: signer compatibility with viem and ethers #567

Merged
merged 2 commits into from
Jul 21, 2023
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"tslib": "^2.3.0",
"unique-names-generator": "^4.7.1",
"uuid": "^9.0.0",
"video-stream-merger": "^4.0.1"
"video-stream-merger": "^4.0.1",
"viem": "^1.3.0"
},
"devDependencies": {
"@commitlint/cli": "^17.0.0",
Expand Down
121 changes: 76 additions & 45 deletions packages/examples/sdk-backend-node/src/chat/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,49 @@ import {
} from 'unique-names-generator';
import { ENV } from '../types';
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 *********************/
/**
* USING VIEM
*/
// Random Wallet Signers
const signer = ethers.Wallet.createRandom();
const signerSecondAccount = ethers.Wallet.createRandom();

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;
// Dummy Wallet Addresses
const randomWallet1 = ethers.Wallet.createRandom().address;
const randomWallet2 = ethers.Wallet.createRandom().address;
const randomWallet3 = ethers.Wallet.createRandom().address;
const randomWallet1 = privateKeyToAccount(generatePrivateKey()).address;
const randomWallet2 = privateKeyToAccount(generatePrivateKey()).address;
const randomWallet3 = privateKeyToAccount(generatePrivateKey()).address;

/**
* USING ETHERS
*/
// // Random Wallet Signers
// const signer = ethers.Wallet.createRandom();
// const signerAddress = signer.address;
// const secondSigner = ethers.Wallet.createRandom();
// const secondSignerAddress = secondSigner.address;
// // Dummy Wallet Addresses
// const randomWallet1 = ethers.Wallet.createRandom().address;
// const randomWallet2 = ethers.Wallet.createRandom().address;
// const randomWallet3 = ethers.Wallet.createRandom().address;

/************************************************************* */

// Group Chat Data
const groupName = uniqueNamesGenerator({
Expand Down Expand Up @@ -101,7 +132,7 @@ async function PushAPI_user_create(silent = !showAPIResponse) {
});

const user_2 = await PushAPI.user.create({
signer: signerSecondAccount,
signer: secondSigner,
env: env as ENV,
});

Expand All @@ -117,7 +148,7 @@ async function PushAPI_user_create(silent = !showAPIResponse) {
// Push Chat - PushAPI.user.get
async function PushAPI_user_get(silent = !showAPIResponse) {
const user = await PushAPI.user.get({
account: `eip155:${signer.address}`,
account: `eip155:${signerAddress}`,
env: env as ENV,
});

Expand All @@ -132,7 +163,7 @@ async function PushAPI_user_get(silent = !showAPIResponse) {
async function PushAPI_chat_decryptPGPKey(silent = !showAPIResponse) {
// get user and derive encrypted PGP key
const user = await PushAPI.user.get({
account: `eip155:${signer.address}`,
account: `eip155:${signerAddress}`,
env: env as ENV,
});

Expand All @@ -153,7 +184,7 @@ async function PushAPI_chat_decryptPGPKey(silent = !showAPIResponse) {
async function PushAPI_chat_chats(silent = !showAPIResponse) {
// Fetch user
const user = await PushAPI.user.get({
account: `eip155:${signer.address}`,
account: `eip155:${signerAddress}`,
env: env as ENV,
});

Expand All @@ -166,7 +197,7 @@ async function PushAPI_chat_chats(silent = !showAPIResponse) {

// Actual api
const response = await PushAPI.chat.chats({
account: `eip155:${signer.address}`,
account: `eip155:${signerAddress}`,
toDecrypt: true,
pgpPrivateKey: pgpDecrpyptedPvtKey,
env: env as ENV,
Expand All @@ -182,7 +213,7 @@ async function PushAPI_chat_chats(silent = !showAPIResponse) {
async function PushAPI_chat_requests(silent = !showAPIResponse) {
// Fetch user
const user = await PushAPI.user.get({
account: `eip155:${signer.address}`,
account: `eip155:${signerAddress}`,
env: env as ENV,
});

Expand All @@ -195,7 +226,7 @@ async function PushAPI_chat_requests(silent = !showAPIResponse) {

// Actual api
const response = await PushAPI.chat.requests({
account: `eip155:${signer.address}`,
account: `eip155:${signerAddress}`,
toDecrypt: true,
pgpPrivateKey: pgpDecrpyptedPvtKey,
env: env as ENV,
Expand All @@ -211,8 +242,8 @@ async function PushAPI_chat_requests(silent = !showAPIResponse) {
async function PushAPI_chat_conversationHash(silent = !showAPIResponse) {
// conversation hash are also called link inside chat messages
const conversationHash = await PushAPI.chat.conversationHash({
account: `eip155:${signer.address}`,
conversationId: `eip155:${signerSecondAccount.address}`, // 2nd address
account: `eip155:${signerAddress}`,
conversationId: `eip155:${secondSignerAddress}`, // 2nd address
env: env as ENV,
});

Expand All @@ -226,7 +257,7 @@ async function PushAPI_chat_conversationHash(silent = !showAPIResponse) {
async function PushAPI_chat_latest(silent = !showAPIResponse) {
// Fetch user
const user = await PushAPI.user.get({
account: `eip155:${signer.address}`,
account: `eip155:${signerAddress}`,
env: env as ENV,
});

Expand All @@ -240,15 +271,15 @@ async function PushAPI_chat_latest(silent = !showAPIResponse) {
// Fetch conversation hash
// conversation hash are also called link inside chat messages
const conversationHash = await PushAPI.chat.conversationHash({
account: `eip155:${signer.address}`,
conversationId: `eip155:${signerSecondAccount.address}`, // 2nd address
account: `eip155:${signerAddress}`,
conversationId: `eip155:${secondSignerAddress}`, // 2nd address
env: env as ENV,
});

// Actual API
const response = await PushAPI.chat.latest({
threadhash: conversationHash.threadHash, // get conversation hash from conversationHash function and send the response threadhash here
account: `eip155:${signer.address}`,
account: `eip155:${signerAddress}`,
toDecrypt: true,
pgpPrivateKey: pgpDecrpyptedPvtKey,
env: env as ENV,
Expand All @@ -264,7 +295,7 @@ async function PushAPI_chat_latest(silent = !showAPIResponse) {
async function PushAPI_chat_history(silent = !showAPIResponse) {
// Fetch user
const user = await PushAPI.user.get({
account: `eip155:${signer.address}`,
account: `eip155:${signerAddress}`,
env: env as ENV,
});

Expand All @@ -278,15 +309,15 @@ async function PushAPI_chat_history(silent = !showAPIResponse) {
// Fetch conversation hash
// conversation hash are also called link inside chat messages
const conversationHash = await PushAPI.chat.conversationHash({
account: `eip155:${signer.address}`,
conversationId: `eip155:${signerSecondAccount.address}`, // 2nd address
account: `eip155:${signerAddress}`,
conversationId: `eip155:${secondSignerAddress}`, // 2nd address
env: env as ENV,
});

// Actual API
const response = await PushAPI.chat.history({
threadhash: conversationHash.threadHash, // get conversation hash from conversationHash function and send the response threadhash here
account: `eip155:${signer.address}`,
account: `eip155:${signerAddress}`,
limit: 5,
toDecrypt: true,
pgpPrivateKey: pgpDecrpyptedPvtKey,
Expand All @@ -304,7 +335,7 @@ async function PushAPI_chat_history(silent = !showAPIResponse) {
async function PushAPI_chat_send(silent = !showAPIResponse) {
// Fetch user
const user = await PushAPI.user.get({
account: `eip155:${signer.address}`,
account: `eip155:${signerAddress}`,
env: env as ENV,
});

Expand All @@ -321,7 +352,7 @@ async function PushAPI_chat_send(silent = !showAPIResponse) {
content: "Gm gm! It's me... Mario",
},
messageType: 'Text', // can be "Text" | "Image" | "File" | "GIF"
receiverAddress: signerSecondAccount.address,
receiverAddress: secondSignerAddress,

signer: signer,
pgpPrivateKey: pgpDecrpyptedPvtKey,
Expand All @@ -339,23 +370,23 @@ async function PushAPI_chat_send(silent = !showAPIResponse) {
async function PushAPI_chat_approve(silent = !showAPIResponse) {
// Fetch user
const user = await PushAPI.user.get({
account: `eip155:${signerSecondAccount.address}`,
account: `eip155:${secondSignerAddress}`,
env: env as ENV,
});

// Decrypt PGP Key
const pgpDecrpyptedPvtKey = await PushAPI.chat.decryptPGPKey({
encryptedPGPPrivateKey: user.encryptedPrivateKey,

signer: signerSecondAccount,
signer: secondSigner,
});

// Actual api
const approve = await PushAPI.chat.approve({
status: 'Approved',
senderAddress: signer.address, // receiver's address or chatId of a group
senderAddress: signerAddress, // receiver's address or chatId of a group

signer: signerSecondAccount,
signer: secondSigner,
pgpPrivateKey: pgpDecrpyptedPvtKey,
env: env as ENV,
});
Expand All @@ -372,7 +403,7 @@ async function PushAPI_chat_createGroup(
): Promise<string> {
// Fetch user
const user = await PushAPI.user.get({
account: `eip155:${signer.address}`,
account: `eip155:${signerAddress}`,
env: env as ENV,
});

Expand Down Expand Up @@ -412,7 +443,7 @@ async function PushAPI_chat_updateGroup(
) {
// Fetch user
const user = await PushAPI.user.get({
account: `eip155:${signer.address}`,
account: `eip155:${signerAddress}`,
env: env as ENV,
});

Expand All @@ -435,10 +466,10 @@ async function PushAPI_chat_updateGroup(
`eip155:${randomWallet1}`,
`eip155:${randomWallet2}`,
`eip155:${randomWallet3}`,
`eip155:${signer.address}`,
`eip155:${signerAddress}`,
],
groupImage,
admins: [`eip155:${signer.address}`], // takes signer as admin automatically, add more if you want to
admins: [`eip155:${signerAddress}`], // takes signer as admin automatically, add more if you want to

signer: signer,
pgpPrivateKey: pgpDecrpyptedPvtKey,
Expand Down Expand Up @@ -484,7 +515,7 @@ async function PushAPI_chat_getGroup(
async function PushAPI_chat_decryptConversation(silent = !showAPIResponse) {
// Fetch user
const user = await PushAPI.user.get({
account: `eip155:${signer.address}`,
account: `eip155:${signerAddress}`,
env: env as ENV,
});

Expand All @@ -498,15 +529,15 @@ async function PushAPI_chat_decryptConversation(silent = !showAPIResponse) {
// Fetch conversation hash
// conversation hash are also called link inside chat messages
const conversationHash = await PushAPI.chat.conversationHash({
account: `eip155:${signer.address}`,
conversationId: `eip155:${signerSecondAccount.address}`, // 2nd address
account: `eip155:${signerAddress}`,
conversationId: `eip155:${secondSignerAddress}`, // 2nd address
env: env as ENV,
});

// Chat History
const encryptedChats = await PushAPI.chat.history({
threadhash: conversationHash.threadHash, // get conversation hash from conversationHash function and send the response threadhash here
account: `eip155:${signer.address}`,
account: `eip155:${signerAddress}`,
limit: 5,
toDecrypt: false,
pgpPrivateKey: pgpDecrpyptedPvtKey,
Expand All @@ -530,7 +561,7 @@ async function PushAPI_chat_decryptConversation(silent = !showAPIResponse) {
// Push Chat - Socket Connection
async function PushChatSDKSocket(silent = !showAPIResponse) {
const pushSDKSocket = createSocketConnection({
user: `eip155:${signer.address}`,
user: `eip155:${signerAddress}`,
socketType: 'chat',
socketOptions: { autoConnect: true, reconnectionAttempts: 3 },
env: env as ENV,
Expand All @@ -546,24 +577,24 @@ async function PushChatSDKSocket(silent = !showAPIResponse) {
// send a chat from other wallet to this one to see the result
// Fetch user
const user = await PushAPI.user.get({
account: `eip155:${signerSecondAccount.address}`,
account: `eip155:${secondSignerAddress}`,
env: env as ENV,
});

// Decrypt PGP Key
const pgpDecrpyptedPvtKey = await PushAPI.chat.decryptPGPKey({
encryptedPGPPrivateKey: user.encryptedPrivateKey,

signer: signerSecondAccount,
signer: secondSigner,
});

// Actual api
const response = await PushAPI.chat.send({
messageContent: "Gm gm! It's me... Mario",
messageType: 'Text',
receiverAddress: `eip155:${signer.address}`,
receiverAddress: `eip155:${signerAddress}`,

signer: signerSecondAccount,
signer: secondSigner,
pgpPrivateKey: pgpDecrpyptedPvtKey,
env: env as ENV,
});
Expand Down Expand Up @@ -596,7 +627,7 @@ async function PushAPI_chat_video_call_notification(
) {
// Fetch user
const user = await PushAPI.user.get({
account: signer.address,
account: signerAddress,
env: env as ENV,
});

Expand Down Expand Up @@ -628,8 +659,8 @@ async function PushAPI_chat_video_call_notification(
domain: 'push.org',
},
},
recipients: signerSecondAccount.address, // recipient address
channel: signer.address, // your channel address
recipients: secondSignerAddress, // recipient address
channel: signerAddress, // your channel address
env: env as ENV,
});

Expand Down
Loading