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

feat: prompt user to login if not logged in when receiving wallet connect request #2704

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
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
Loading