diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionRequest.handler.ts b/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionRequest.handler.ts index 0a66f11f3d..17972b049b 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionRequest.handler.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionRequest.handler.ts @@ -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, @@ -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 @@ -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 diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/stores/cached-session-request.store.ts b/packages/shared/src/lib/auxiliary/wallet-connect/stores/cached-session-request.store.ts new file mode 100644 index 0000000000..5ae11818cf --- /dev/null +++ b/packages/shared/src/lib/auxiliary/wallet-connect/stores/cached-session-request.store.ts @@ -0,0 +1,4 @@ +import { Web3WalletTypes } from '@walletconnect/web3wallet' +import { Writable, writable } from 'svelte/store' + +export const cachedSessionRequest: Writable = writable(undefined) diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/stores/index.ts b/packages/shared/src/lib/auxiliary/wallet-connect/stores/index.ts index f39a407893..99565eba62 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/stores/index.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/stores/index.ts @@ -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' diff --git a/packages/shared/src/lib/core/profile/actions/active-profile/login.ts b/packages/shared/src/lib/core/profile/actions/active-profile/login.ts index 28c847e529..974c7e7985 100644 --- a/packages/shared/src/lib/core/profile/actions/active-profile/login.ts +++ b/packages/shared/src/lib/core/profile/actions/active-profile/login.ts @@ -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 { const loginRouter = get(routerManager)?.getRouterForAppContext(AppContext.Login) @@ -115,6 +117,13 @@ export async function login(loginOptions?: ILoginOptions): Promise { 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) diff --git a/packages/shared/src/locales/en.json b/packages/shared/src/locales/en.json index 3ff2eed0bd..a45f391286 100644 --- a/packages/shared/src/locales/en.json +++ b/packages/shared/src/locales/en.json @@ -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",