Skip to content

Commit

Permalink
🥅 Prevent multiple WC sessions (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
nwingt authored May 5, 2023
1 parent e3e0a23 commit a052440
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
4 changes: 2 additions & 2 deletions app/models/deep-link-handle-store/deep-link-handle-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const BaseModel = types
if (params.event === "wallet_connect_v1") {
const walletConnectURI = self.env.branchIO.parseWalletConnectEvent(params)
if (walletConnectURI) {
self.walletConnectStore.handleNewSessionRequest(walletConnectURI, { isMobile: true })
yield self.walletConnectStore.handleNewSessionRequest(walletConnectURI, { isMobile: true })
}
}
})
Expand Down Expand Up @@ -148,7 +148,7 @@ const BaseModel = types
} else if (url.includes('wcV1?')) {
const [, walletConnectURI = ''] = url.split('wcV1?')
if (walletConnectURI) {
self.walletConnectStore.handleNewSessionRequest(walletConnectURI, { isMobile: true })
yield self.walletConnectStore.handleNewSessionRequest(walletConnectURI, { isMobile: true })
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion app/models/wallet-connect-client/wallet-connect-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const WalletConnectClientModel = types
},
disconnect: flow(function * () {
try {
yield self.connector.killSession()
yield self.connector?.killSession()
} catch (error) {
logError(error)
}
Expand Down Expand Up @@ -136,6 +136,8 @@ export const WalletConnectClientModel = types
}))
.actions(self => ({
handleCallRequestApproval: flow(function * (payload: any) {
if (self.serializedSession === "" || !Object.values(self.env).length) return

let result = null
try {
switch (payload.method) {
Expand Down
23 changes: 17 additions & 6 deletions app/models/wallet-connect-store/wallet-connect-store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Instance, SnapshotOut, types } from "mobx-state-tree"
import { Instance, SnapshotOut, flow, types } from "mobx-state-tree"
import { withEnvironment, withExperimentalFeatures, withLanguageSettingsStore, withNavigationStore } from "../extensions"

import { WalletConnectClientModel } from "../wallet-connect-client"
Expand Down Expand Up @@ -40,15 +40,26 @@ export const WalletConnectStoreModel = types
},
}))
.actions(self => ({
handleNewSessionRequest(uri: string, opts?: { isMobile?: boolean }) {
const client = WalletConnectClientModel.create({})
client.createSession(uri, opts)
self.clients.push(client)
},
handleNewSessionRequest: flow(function * (uri: string, opts?: { isMobile?: boolean }) {
const newClient = WalletConnectClientModel.create({})
newClient.createSession(uri, opts)

// Deduplicate clients with same URL while adding new client
const toBeRemovedClients = self.clients.filter(client => client.connector.clientMeta.url === newClient.connector.clientMeta.url)

self.clients.push(newClient)

yield toBeRemovedClients.forEach(async client => {
await client.disconnect()
self.clients.remove(client)
})
}),
afterCreate() {
if (!self.experimentalFeatures || !self.experimentalFeatures.isWalletConnectActivated) return
self.clients.forEach(client => {
client.restoreSession()
// Disconnect all Wallet Connect sessions every time the app is restarted
client.disconnect()
})
},
reset() {
Expand Down

0 comments on commit a052440

Please sign in to comment.