Skip to content

Commit

Permalink
feat: prompt user to login if not logged in when receiving wallet con…
Browse files Browse the repository at this point in the history
…nect request (#2704)

* cache sessionrequest

* only handle request if logged in to same account
  • Loading branch information
MarkNerdi authored Jul 4, 2024
1 parent 1729a6f commit b8bbe50
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { JsonRpcResponse } from '@walletconnect/jsonrpc-types'
import { getSdkError } from '@walletconnect/utils'
import { Web3WalletTypes } from '@walletconnect/web3wallet'
import {
cachedSessionRequest,
getConnectedDappBySessionTopic,
getWalletClient,
setConnectedDapps,
Expand All @@ -19,8 +20,21 @@ import { EvmTransactionData, getEvmTransactionFromHexString } from '@core/layer-
import { activeProfileId } from '@core/profile/stores'
import { get } from 'svelte/store'
import { Platform } from '@core/app/classes'
import { showNotification } from '@auxiliary/notification'
import { localize } from '@core/i18n'

export function onSessionRequest(event: Web3WalletTypes.SessionRequest): void {
Platform.focusWindow()

if (!get(activeProfileId)) {
cachedSessionRequest.set(event)
showNotification({
variant: 'info',
text: localize('dashboard.drawers.dapps.general.loginPrompt'),
})
return
}

// We need to call this here, because if the dapp requests too fast after approval, we won't have the dapp in the store yet
setConnectedDapps()
const { topic, params, id, verifyContext } = event
Expand Down Expand Up @@ -54,13 +68,6 @@ export function onSessionRequest(event: Web3WalletTypes.SessionRequest): void {
}
}

Platform.focusWindow()

if (!get(activeProfileId)) {
returnResponse({ error: getSdkError('SESSION_SETTLEMENT_FAILED') })
return
}

const dapp = getConnectedDappBySessionTopic(topic)
const verifiedState = verifyContext.verified.isScam
? DappVerification.Scam
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Web3WalletTypes } from '@walletconnect/web3wallet'
import { Writable, writable } from 'svelte/store'

export const cachedSessionRequest: Writable<Web3WalletTypes.SessionRequest | undefined> = writable(undefined)
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './cached-session-request.store'
export * from './connected-dapps.store'
export * from './persisted-dapps.store'
export * from './persisted-dapp-namespaces.store'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import { fetchAndPersistTransactionsForAccounts } from '@core/transactions/actio
import { updateCirculatingSupplyForActiveProfile } from './updateCirculatingSupplyForActiveProfile'
import { notificationsManager } from '@auxiliary/wallet-connect/notifications'
import { getEvmNetworks } from '@core/network'
import { cachedSessionRequest } from '@auxiliary/wallet-connect/stores'
import { onSessionRequest } from '@auxiliary/wallet-connect/handlers'

export async function login(loginOptions?: ILoginOptions): Promise<void> {
const loginRouter = get(routerManager)?.getRouterForAppContext(AppContext.Login)
Expand Down Expand Up @@ -115,6 +117,13 @@ export async function login(loginOptions?: ILoginOptions): Promise<void> {

if (getLastLoggedInProfileId() !== _activeProfile.id) {
void disconnectAllDapps()
cachedSessionRequest.set(undefined)
} else {
const _cachedSessionRequest = get(cachedSessionRequest)
if (_cachedSessionRequest) {
cachedSessionRequest.set(undefined)
onSessionRequest(_cachedSessionRequest)
}
}

setSelectedAccount(lastUsedAccountIndex ?? loadedAccounts?.[0]?.index)
Expand Down
3 changes: 3 additions & 0 deletions packages/shared/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,9 @@
}
},
"dapps": {
"general": {
"loginPrompt": "Dapp request received while logged out. Please log in to continue."
},
"dappsList": {
"title": "Connected dApps",
"connectDapp": "Connect dApp",
Expand Down

0 comments on commit b8bbe50

Please sign in to comment.