Skip to content

Commit

Permalink
fix: prevent creating duplicate sessions (#2678)
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Nardi <[email protected]>
  • Loading branch information
nicole-obrien and MarkNerdi authored Jun 28, 2024
1 parent a22001e commit 9358ca2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import { handleError } from '@core/error/handlers'
import { buildApprovedNamespaces } from '@walletconnect/utils'
import { Web3WalletTypes } from '@walletconnect/web3wallet'
import { ISession } from '../interface/session.interface'
import { getWalletClient, setConnectedDapps } from '../stores'
import { connectedDapps, getWalletClient, setConnectedDapps } from '../stores'
import { updateAccountForDappSession } from './updateAccountForDappSession'
import { ISupportedNamespace } from '../types'
import { normalizeSessionNamespace } from '../utils'
import { IDappMetadata } from '../interface'
import { get } from 'svelte/store'
import { disconnectDapp } from './disconnectDapp'

export async function approveSession(
sessionProposal: Web3WalletTypes.SessionProposal,
supportedNamespaces: Record<string, ISupportedNamespace>,
dappMetadata: IDappMetadata,
account: IAccountState
): Promise<void> {
const { id, params } = sessionProposal
Expand All @@ -21,6 +25,11 @@ export async function approveSession(
})

try {
const dapp = get(connectedDapps).find((_dapp) => _dapp.metadata.url === dappMetadata.url)
if (dapp?.sessionTopic) {
await disconnectDapp(dapp)
}

const session = (await getWalletClient()?.approveSession({
id,
namespaces: approvedNamespaces,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function connectToDapp(
const { requiredNamespaces, optionalNamespaces } = sessionProposal.params

await clearOldPairings(dappUrl)
await approveSession(sessionProposal, supportedNamespaces, account)
await approveSession(sessionProposal, supportedNamespaces, sessionProposal.params.proposer.metadata, account)

const verificationState = sessionProposal.verifyContext.verified
persistDapp(
Expand Down

0 comments on commit 9358ca2

Please sign in to comment.