wallet-connect-web-examples/wallets/react-wallet-auth/src/utils/WalletConnectUtil.ts
Cali 34cf292b06
feat(auth-wallet): add handling for push methods/events (#145)
Co-authored-by: Ben Kremer <ben@walletconnect.com>
2023-05-31 14:13:42 +02:00

60 lines
1.7 KiB
TypeScript

import AuthClient from '@walletconnect/auth-client'
import pkg from '@walletconnect/auth-client/package.json'
import { Core } from '@walletconnect/core'
import { WalletClient } from '@walletconnect/push-client'
const core = new Core({
projectId: process.env.NEXT_PUBLIC_PROJECT_ID!
})
console.log(`AuthClient@${pkg.version}`)
export let authClient: AuthClient
export let pushClient: WalletClient
export async function createAuthClient() {
authClient = await AuthClient.init({
core,
projectId: process.env.NEXT_PUBLIC_PROJECT_ID!,
relayUrl: process.env.NEXT_PUBLIC_RELAY_URL || 'wss://relay.walletconnect.com',
metadata: {
name: 'React Wallet',
description: 'React Wallet for WalletConnect',
url: 'https://walletconnect.com/',
icons: ['https://avatars.githubusercontent.com/u/37784886']
}
})
const authClientId = await authClient.core.crypto.getClientId()
console.log({ authClientId })
}
export async function createPushClient() {
pushClient = await WalletClient.init({
logger: 'debug',
core,
projectId: process.env.NEXT_PUBLIC_PROJECT_ID!,
relayUrl: process.env.NEXT_PUBLIC_RELAY_URL || 'wss://relay.walletconnect.com'
})
const pushClientId = await pushClient.core.crypto.getClientId()
console.log({ pushClientId })
}
export const getAndFormatNotifications = () => {
if (!pushClient) {
return []
}
const activeSubscriptions = pushClient.getActiveSubscriptions()
const allMessagesWithSubscription = Object.entries(activeSubscriptions)
.map(([topic, subscription]) => ({
subscription,
messages: Object.values(
pushClient.getMessageHistory({
topic
})
)
}))
.reverse()
return allMessagesWithSubscription
}