From 6eb649209c127901ca87a078dae46d0e0f5f6123 Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Tue, 2 Jul 2024 12:22:40 +0200 Subject: [PATCH 1/2] cache sessionrequest --- .../handlers/onSessionRequest.handler.ts | 21 ++++++++++++------- .../stores/cached-session-request.store.ts | 4 ++++ .../auxiliary/wallet-connect/stores/index.ts | 1 + .../profile/actions/active-profile/login.ts | 7 +++++++ packages/shared/src/locales/en.json | 3 +++ 5 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 packages/shared/src/lib/auxiliary/wallet-connect/stores/cached-session-request.store.ts 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..765f8c7e97 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) @@ -133,6 +135,11 @@ export async function login(loginOptions?: ILoginOptions): Promise { void registerProposalsFromNodes(loadedAccounts) } void cleanupOnboarding() + const _cachedSessionRequest = get(cachedSessionRequest) + if (_cachedSessionRequest) { + cachedSessionRequest.set(undefined) + onSessionRequest(_cachedSessionRequest) + } notificationsManager.setTrackedNetworkAccounts( loadedAccounts, getEvmNetworks().map(({ id }) => id) 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", From a0eea80cbaf3669eb49b5b8528f86edad1bcdc0c Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Tue, 2 Jul 2024 12:47:34 +0200 Subject: [PATCH 2/2] only handle request if logged in to same account --- .../lib/core/profile/actions/active-profile/login.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) 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 765f8c7e97..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 @@ -117,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) @@ -135,11 +142,6 @@ export async function login(loginOptions?: ILoginOptions): Promise { void registerProposalsFromNodes(loadedAccounts) } void cleanupOnboarding() - const _cachedSessionRequest = get(cachedSessionRequest) - if (_cachedSessionRequest) { - cachedSessionRequest.set(undefined) - onSessionRequest(_cachedSessionRequest) - } notificationsManager.setTrackedNetworkAccounts( loadedAccounts, getEvmNetworks().map(({ id }) => id)