From f855141da955581aa8afe4e6efa32b92a1397ae1 Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Fri, 31 May 2024 13:59:50 +0200 Subject: [PATCH 01/24] generalize proposal store --- .../wallet-connect/actions/connectToDapp.ts | 3 +- .../actions/initializeWalletConnect.ts | 2 ++ .../wallet-connect/handlers/index.ts | 1 + .../handlers/onSessionAuthenticate.handler.ts | 12 ++++++++ .../handlers/onSessionDelete.handler.ts | 4 +-- .../handlers/onSessionProposal.handler.ts | 10 +++++-- .../stores/connection-request.store.ts | 18 ++++++++++++ .../auxiliary/wallet-connect/stores/index.ts | 2 +- .../stores/session-proposal.store.ts | 8 ------ .../auxiliary/wallet-connect/utils/index.ts | 2 +- .../utils/rejectConnectionRequest.ts | 28 +++++++++++++++++++ .../wallet-connect/utils/rejectSession.ts | 21 -------------- 12 files changed, 75 insertions(+), 36 deletions(-) create mode 100644 packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionAuthenticate.handler.ts create mode 100644 packages/shared/src/lib/auxiliary/wallet-connect/stores/connection-request.store.ts delete mode 100644 packages/shared/src/lib/auxiliary/wallet-connect/stores/session-proposal.store.ts create mode 100644 packages/shared/src/lib/auxiliary/wallet-connect/utils/rejectConnectionRequest.ts delete mode 100644 packages/shared/src/lib/auxiliary/wallet-connect/utils/rejectSession.ts diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/actions/connectToDapp.ts b/packages/shared/src/lib/auxiliary/wallet-connect/actions/connectToDapp.ts index 38a0a70b1e..6ec190a495 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/actions/connectToDapp.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/actions/connectToDapp.ts @@ -1,5 +1,5 @@ import { IAccountState } from '@core/account' -import { persistDapp } from '../stores' +import { clearConnectionRequest, persistDapp } from '../stores' import { approveSession } from './approveSession' import { buildSupportedNamespacesFromSelections } from './buildSupportedNamespaceFromSelections' import { clearOldPairings } from './clearOldPairings' @@ -33,4 +33,5 @@ export async function connectToDapp( verificationState.isScam ? DappVerification.Scam : (verificationState.validation as DappVerification), { supported: supportedNamespaces, required: requiredNamespaces, optional: optionalNamespaces } ) + clearConnectionRequest() } diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/actions/initializeWalletConnect.ts b/packages/shared/src/lib/auxiliary/wallet-connect/actions/initializeWalletConnect.ts index 81c2834812..3b806cdae6 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/actions/initializeWalletConnect.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/actions/initializeWalletConnect.ts @@ -7,6 +7,7 @@ import { WALLET_METADATA } from '../constants' import { onSessionDelete, onSessionProposal, onSessionRequest } from '../handlers' import { walletClient } from '../stores' import { setConnectedDapps } from '../stores/connected-dapps.store' +import { onSessionAuthenticate } from '../handlers/onSessionAuthenticate.handler' export async function initializeWalletConnect(): Promise { if (!features?.walletConnect?.enabled || get(walletClient)) { @@ -26,6 +27,7 @@ export async function initializeWalletConnect(): Promise { client.on('session_proposal', (sessionProposal) => void onSessionProposal(sessionProposal)) client.on('session_request', (event) => onSessionRequest(event)) client.on('session_delete', (event) => onSessionDelete(event)) + client.on('session_authenticate', (payload) => onSessionAuthenticate(payload)) } catch (err) { handleError(err) } diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/handlers/index.ts b/packages/shared/src/lib/auxiliary/wallet-connect/handlers/index.ts index f95f9bb901..e8b2f93292 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/handlers/index.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/handlers/index.ts @@ -1,5 +1,6 @@ export * from './eth_transaction.handler' export * from './eth_signTypedData.handler' +export * from './onSessionAuthenticate.handler' export * from './onSessionDelete.handler' export * from './onSessionProposal.handler' export * from './onSessionRequest.handler' diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionAuthenticate.handler.ts b/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionAuthenticate.handler.ts new file mode 100644 index 0000000000..8f42a570ec --- /dev/null +++ b/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionAuthenticate.handler.ts @@ -0,0 +1,12 @@ +import { Web3WalletTypes } from '@walletconnect/web3wallet' +import { connectionRequest } from '../stores' +import { get } from 'svelte/store' +import { rejectConnectionRequest } from '../utils' + +export function onSessionAuthenticate(event: Web3WalletTypes.SessionAuthenticate): void { + if (get(connectionRequest)) { + void rejectConnectionRequest() + } else { + connectionRequest.set({ type: 'session_authenticate', payload: event }) + } +} diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionDelete.handler.ts b/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionDelete.handler.ts index f6ef6be350..032463a78c 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionDelete.handler.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionDelete.handler.ts @@ -1,11 +1,11 @@ import { showNotification } from '@auxiliary/notification' import { Web3WalletTypes } from '@walletconnect/web3wallet' -import { getConnectedDapps, setConnectedDapps } from '../stores' +import { getConnectedDappBySessionTopic, setConnectedDapps } from '../stores' export function onSessionDelete(event: Web3WalletTypes.SessionDelete): void { const { topic } = event - const dapp = getConnectedDapps().find((_dapp) => _dapp.session?.topic === topic) + const dapp = getConnectedDappBySessionTopic(topic) showNotification({ variant: 'warning', diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionProposal.handler.ts b/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionProposal.handler.ts index f198ea4783..109488c5b6 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionProposal.handler.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionProposal.handler.ts @@ -1,6 +1,12 @@ import { Web3WalletTypes } from '@walletconnect/web3wallet' -import { sessionProposal } from '../stores' +import { connectionRequest } from '../stores' +import { get } from 'svelte/store' +import { rejectConnectionRequest } from '../utils' export function onSessionProposal(_sessionProposal: Web3WalletTypes.SessionProposal): void { - sessionProposal.set(_sessionProposal) + if (get(connectionRequest)) { + void rejectConnectionRequest() + } else { + connectionRequest.set({ type: 'session_proposal', payload: _sessionProposal }) + } } diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/stores/connection-request.store.ts b/packages/shared/src/lib/auxiliary/wallet-connect/stores/connection-request.store.ts new file mode 100644 index 0000000000..1277d5db86 --- /dev/null +++ b/packages/shared/src/lib/auxiliary/wallet-connect/stores/connection-request.store.ts @@ -0,0 +1,18 @@ +import { Writable, writable } from 'svelte/store' +import { Web3WalletTypes } from '@walletconnect/web3wallet' + +export type ConnectionRequest = + | { + type: 'session_proposal' + payload: Web3WalletTypes.SessionProposal + } + | { + type: 'session_authenticate' + payload: Web3WalletTypes.SessionAuthenticate + } + +export const connectionRequest: Writable = writable(undefined) + +export function clearConnectionRequest(): void { + connectionRequest.set(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 cac401ea14..3ad605dacc 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/stores/index.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/stores/index.ts @@ -3,4 +3,4 @@ export * from './wallet-client.store' export * from './persisted-dapps.store' export * from './persisted-dapp-namespaces.store' export * from './selected-dapp.store' -export * from './session-proposal.store' +export * from './connection-request.store' diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/stores/session-proposal.store.ts b/packages/shared/src/lib/auxiliary/wallet-connect/stores/session-proposal.store.ts deleted file mode 100644 index 61c93cdab1..0000000000 --- a/packages/shared/src/lib/auxiliary/wallet-connect/stores/session-proposal.store.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Writable, get, writable } from 'svelte/store' -import { Web3WalletTypes } from '@walletconnect/web3wallet' - -export const sessionProposal: Writable = writable(undefined) - -export function getSessionProposal(): Web3WalletTypes.SessionProposal | undefined { - return get(sessionProposal) -} diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/utils/index.ts b/packages/shared/src/lib/auxiliary/wallet-connect/utils/index.ts index 9ad8948cb7..2814e8ce4c 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/utils/index.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/utils/index.ts @@ -1,7 +1,7 @@ export * from './disconnectAllDapps' export * from './getBloomError' export * from './getPermissionForMethod' -export * from './rejectSession' +export * from './rejectConnectionRequest' export * from './switchToRequiredAccount' export * from './updateSession' export * from './validateConnectionCodeUri' diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/utils/rejectConnectionRequest.ts b/packages/shared/src/lib/auxiliary/wallet-connect/utils/rejectConnectionRequest.ts new file mode 100644 index 0000000000..9341eef637 --- /dev/null +++ b/packages/shared/src/lib/auxiliary/wallet-connect/utils/rejectConnectionRequest.ts @@ -0,0 +1,28 @@ +import { getSdkError } from '@walletconnect/utils' +import { getWalletClient, connectionRequest } from '../stores' +import { get } from 'svelte/store' +import { handleError } from '@core/error/handlers' + +export async function rejectConnectionRequest(): Promise { + const _connectionRequest = get(connectionRequest) + if (!_connectionRequest) { + return + } + + connectionRequest.set(undefined) + try { + if (_connectionRequest.type === 'session_proposal') { + await getWalletClient()?.rejectSession({ + id: _connectionRequest.payload.id, + reason: getSdkError('USER_REJECTED'), + }) + } else if (_connectionRequest.type === 'session_authenticate') { + await getWalletClient()?.rejectSessionAuthenticate({ + id: _connectionRequest.payload.id, + reason: getSdkError('USER_REJECTED'), + }) + } + } catch (err) { + handleError(err) + } +} diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/utils/rejectSession.ts b/packages/shared/src/lib/auxiliary/wallet-connect/utils/rejectSession.ts deleted file mode 100644 index 5a2b253289..0000000000 --- a/packages/shared/src/lib/auxiliary/wallet-connect/utils/rejectSession.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { getSdkError } from '@walletconnect/utils' -import { getWalletClient, sessionProposal } from '../stores' -import { get } from 'svelte/store' -import { handleError } from '@core/error/handlers' - -export async function rejectSession(): Promise { - const _sessionProposal = get(sessionProposal) - if (!_sessionProposal) { - return - } - - sessionProposal.set(undefined) - try { - await getWalletClient()?.rejectSession({ - id: _sessionProposal.id, - reason: getSdkError('USER_REJECTED'), - }) - } catch (err) { - handleError(err) - } -} From 68632a702012bb3c8a714747336c0abb3b39aea2 Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Fri, 31 May 2024 13:59:51 +0200 Subject: [PATCH 02/24] refactor selections --- .../components/NetworkSelection.svelte | 57 +++++++++---------- .../components/PermissionSelection.svelte | 37 ++++++------ .../handleDeepLinkAddWCConnectionOperation.ts | 4 +- 3 files changed, 46 insertions(+), 52 deletions(-) diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/components/NetworkSelection.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/components/NetworkSelection.svelte index 50880585c6..83389eb618 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/components/NetworkSelection.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/components/NetworkSelection.svelte @@ -1,5 +1,4 @@
- {#if requiredNetworks.length} + {#if requiredNetworksOptions.length} {/if} - {#if optionalNetworks.length} + {#if optionalNetworksOptions.length} diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/components/PermissionSelection.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/components/PermissionSelection.svelte index 76942695ff..57ab8eb731 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/components/PermissionSelection.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/components/PermissionSelection.svelte @@ -1,5 +1,4 @@ -{#if requiredPermissions.length || optionalPermissions.length} +{#if requiredPermissionsOptions.length || optionalPermissionsOptions.length}
- {#if requiredPermissions.length} + {#if requiredPermissionsOptions.length} {/if} - {#if optionalPermissions.length} + {#if optionalPermissionsOptions.length} @@ -96,7 +91,7 @@
{:else} - {localize(`${localeKey}.title`)} + {localize(`${localeKey}.step`)}
{localize(`${localeKey}.noPermissionsRequired`)} diff --git a/packages/shared/src/lib/auxiliary/deep-link/handlers/dapps/operations/handleDeepLinkAddWCConnectionOperation.ts b/packages/shared/src/lib/auxiliary/deep-link/handlers/dapps/operations/handleDeepLinkAddWCConnectionOperation.ts index 00893e9336..1c2e446bf5 100644 --- a/packages/shared/src/lib/auxiliary/deep-link/handlers/dapps/operations/handleDeepLinkAddWCConnectionOperation.ts +++ b/packages/shared/src/lib/auxiliary/deep-link/handlers/dapps/operations/handleDeepLinkAddWCConnectionOperation.ts @@ -1,5 +1,5 @@ import { pairWithNewDapp } from '@auxiliary/wallet-connect/actions' -import { rejectSession, validateConnectionCodeUri } from '@auxiliary/wallet-connect/utils' +import { rejectConnectionRequest, validateConnectionCodeUri } from '@auxiliary/wallet-connect/utils' import { get } from 'svelte/store' import { DrawerRoute, openDrawer } from '../../../../../../../../desktop/lib/auxiliary/drawer' import { dappConfigRouter } from '../../../../../../../../desktop/views/dashboard/drawers/dapp-config/dapp-config.router' @@ -24,7 +24,7 @@ export function handleDeepLinkAddWCConnectionOperation(searchParams: URLSearchPa route: DrawerRoute.Dashboard, id: DashboardDrawerRoute.DappConfig, initialSubroute: DappConfigRoute.ConnectionRequest, - props: { onClose: rejectSession }, + props: { onClose: rejectConnectionRequest }, }) } catch (err) { $dappConfigRouter?.reset() From ca51ba1240021793a3631a5a042d0a903dd341d7 Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Fri, 31 May 2024 13:59:52 +0200 Subject: [PATCH 03/24] update connectionrequest view --- .../components/UnsupportedDappHint.svelte | 30 +-- .../views/ConnectionRequest.svelte | 121 +++++++++++ .../views/ConnectionRequestDrawer.svelte | 202 +++++++----------- .../views/InputConnectionCodeDrawer.svelte | 4 +- 4 files changed, 211 insertions(+), 146 deletions(-) create mode 100644 packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequest.svelte diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/components/UnsupportedDappHint.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/components/UnsupportedDappHint.svelte index afae39ca96..04b1764856 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/components/UnsupportedDappHint.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/components/UnsupportedDappHint.svelte @@ -1,14 +1,14 @@ diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequest.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequest.svelte new file mode 100644 index 0000000000..0942ba487b --- /dev/null +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequest.svelte @@ -0,0 +1,121 @@ + + + +
+ +
+
+ + +
+ {localize(`general.${verifiedState === DappVerification.Valid ? 'yes' : 'no'}`)} +
+
+
+
+
+ {#if !fulfillsRequirements} +
+ +
+ {:else if verifiedState !== DappVerification.Valid} +
+ +
+ {/if} +
+
+
+
diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequestDrawer.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequestDrawer.svelte index e1f5ac2cb9..91dc5d74fe 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequestDrawer.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequestDrawer.svelte @@ -1,39 +1,24 @@ - -
- {#if $sessionProposal} - {@const metadata = $sessionProposal.params.proposer.metadata} - - -
-
- - -
- {localize( - `general.${verifiedState === DappVerification.Valid ? 'yes' : 'no'}` - )} -
-
-
-
-
- {#if !fulfillsRequirements} -
- -
- {:else if verifiedState !== DappVerification.Valid} -
- -
- {/if} - {:else} -
- -
- {/if} -
-
-
-
+{#if $connectionRequest?.type === 'session_proposal'} + {@const { dappMetadata, verifiedState, requiredNetworks, optionalNetworks, requiredMethods } = + getConnectionRequestDataFromSessionProposal($connectionRequest.payload)} + +{:else if $connectionRequest?.type === 'session_authenticate'} + {@const { dappMetadata, verifiedState, requiredNetworks, optionalNetworks, requiredMethods } = + getConnectionRequestDataFromSessionAuthenticate($connectionRequest.payload)} + +{:else} + +
+ +
+
+
+
+{/if} diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/InputConnectionCodeDrawer.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/InputConnectionCodeDrawer.svelte index 243634ab80..3f13d79937 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/InputConnectionCodeDrawer.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/InputConnectionCodeDrawer.svelte @@ -5,7 +5,7 @@ import { localize } from '@core/i18n' import { Router } from '@core/router' import { onMount } from 'svelte' - import { rejectSession, validateConnectionCodeUri } from '@auxiliary/wallet-connect/utils' + import { rejectConnectionRequest, validateConnectionCodeUri } from '@auxiliary/wallet-connect/utils' import { updateDrawerProps } from '@desktop/auxiliary/drawer' export let drawerRouter: Router @@ -21,7 +21,7 @@ if (isValid()) { try { pairWithNewDapp(walletConnectUri) - updateDrawerProps({ onClose: rejectSession }) + updateDrawerProps({ onClose: rejectConnectionRequest }) drawerRouter.next() } catch (err) { error = err From 8bede4d4675ee81cfb1da6261a09f99ab6ea9e9f Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Fri, 31 May 2024 13:59:52 +0200 Subject: [PATCH 04/24] implement one click auth flow --- .../views/ConfirmConnectionDrawer.svelte | 210 ++---------------- .../dapp-config/views/OneClickAuthFlow.svelte | 206 +++++++++++++++++ .../views/SessionProposalFlow.svelte | 204 +++++++++++++++++ 3 files changed, 428 insertions(+), 192 deletions(-) create mode 100644 packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte create mode 100644 packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/ConfirmConnectionDrawer.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConfirmConnectionDrawer.svelte index 552602801f..892f360e00 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/ConfirmConnectionDrawer.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConfirmConnectionDrawer.svelte @@ -1,203 +1,29 @@ - -
- {#if $sessionProposal} - {@const requiredNamespaces = $sessionProposal.params.requiredNamespaces} - {@const optionalNamespaces = $sessionProposal.params.optionalNamespaces} - - - -
- {#if persistedNamespaces && !isPreferenceSelectionRequired} -
- -
- {:else} - {@const tipLocale = currentStep === 0 ? 'permissions' : currentStep === 1 ? 'networks' : 'accounts'} -
- - -
-
- -
- -
-
- -
-
- -
-
-
-
- {/if} -
- {:else} -
- -
- {/if} -
-
-
-
+{#if $connectionRequest?.type === 'session_proposal'} + +{:else if $connectionRequest?.type === 'session_authenticate'} + +{:else} + +
+ +
+
+
+
+{/if} diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte new file mode 100644 index 0000000000..a6284d7cbf --- /dev/null +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte @@ -0,0 +1,206 @@ + + + +
+ + +
+
+ + +
+
+ +
+ +
+
+ +
+
+ +
+
+
+
+
+
+
+
+
diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte new file mode 100644 index 0000000000..2d6afc215b --- /dev/null +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte @@ -0,0 +1,204 @@ + + + +
+ + +
+ {#if persistedNamespaces && !isPreferenceSelectionRequired} +
+ +
+ {:else} + {@const tipLocale = currentStep === 0 ? 'permissions' : currentStep === 1 ? 'networks' : 'accounts'} +
+ + +
+
+ +
+ +
+
+ +
+
+ +
+
+
+
+ {/if} +
+
+
+
+
From 640d865d8bdda636a44fee24838c6ebc9c551c00 Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Fri, 31 May 2024 13:59:53 +0200 Subject: [PATCH 05/24] move approval function into own file --- .../views/ConnectionRequestDrawer.svelte | 4 +- .../dapp-config/views/OneClickAuthFlow.svelte | 49 ++----------- .../views/SessionProposalFlow.svelte | 4 +- .../actions/approveSessionAuthenticate.ts | 72 +++++++++++++++++++ .../auxiliary/wallet-connect/actions/index.ts | 1 + .../auxiliary/wallet-connect/utils/index.ts | 1 + .../utils/normalizeSupportedNamespace.ts | 18 +++++ 7 files changed, 103 insertions(+), 46 deletions(-) create mode 100644 packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSessionAuthenticate.ts create mode 100644 packages/shared/src/lib/auxiliary/wallet-connect/utils/normalizeSupportedNamespace.ts diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequestDrawer.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequestDrawer.svelte index 91dc5d74fe..6ca326efbd 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequestDrawer.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequestDrawer.svelte @@ -48,10 +48,10 @@ : (sessionProposal.verifyContext.verified.validation as DappVerification) const requiredNetworks = Object.values(sessionProposal.params.requiredNamespaces) - .flatMap(({ chains }) => chains) + .flatMap(({ chains }) => chains ?? []) .filter(Boolean) const optionalNetworks = Object.values(sessionProposal.params.optionalNamespaces) - .flatMap(({ chains }) => chains) + .flatMap(({ chains }) => chains ?? []) .filter(Boolean) const requiredMethods = Object.values(sessionProposal.params.requiredNamespaces).flatMap( ({ methods }) => methods diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte index a6284d7cbf..3ee2a9c0ea 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte @@ -7,19 +7,15 @@ import { rejectConnectionRequest } from '@auxiliary/wallet-connect/utils' import { AccountSelection, NetworkSelection, PermissionSelection } from '../components' import { handleError } from '@core/error/handlers' - import { IAccountState, getAddressFromAccountForNetwork } from '@core/account' + import { IAccountState } from '@core/account' import { DappConfigRoute } from '../dapp-config-route.enum' import { closeDrawer } from '@desktop/auxiliary/drawer' import { DappVerification } from '@auxiliary/wallet-connect/enums' import { Web3WalletTypes } from '@walletconnect/web3wallet' import { walletClient } from '@auxiliary/wallet-connect/stores' - import { populateAuthPayload, buildAuthObject } from '@walletconnect/utils' - import { NetworkId, getEvmNetwork } from '@core/network' - import { AuthTypes } from '@walletconnect/types' - import { signMessage } from '@core/wallet' import { checkActiveProfileAuth } from '@core/profile/actions' import { LedgerAppName } from '@core/ledger' - import { GENERAL_SUPPORTED_METHODS } from '@auxiliary/wallet-connect/constants' + import { approveSessionAuthenticate } from '@auxiliary/wallet-connect/actions/approveSessionAuthenticate' export let drawerRouter: Router export let sessionProposal: Web3WalletTypes.SessionAuthenticate @@ -107,49 +103,18 @@ } try { - const allowedMethods = [...checkedMethods, ...GENERAL_SUPPORTED_METHODS] - - const authPayload = populateAuthPayload({ - authPayload: sessionProposal.params.authPayload, + await approveSessionAuthenticate(sessionProposal, { + accounts: checkedAccounts, chains: checkedNetworks, - methods: allowedMethods, - }) - - const auths: AuthTypes.Cacao[] = [] - for (const chain of authPayload.chains) { - const network = getEvmNetwork(chain as NetworkId) - if (!network) { - continue - } - - for (const account of checkedAccounts) { - const address = `${chain}:${getAddressFromAccountForNetwork(account, chain as NetworkId)}` - - const message = $walletClient.formatAuthMessage({ - request: authPayload, - iss: address, - }) - const signature = await signMessage(message, network.coinType, account) - if (!signature) { - continue - } - - const auth = buildAuthObject(authPayload, { t: 'eip191', s: signature }, address) - auths.push(auth) - } - } - - await $walletClient.approveSessionAuthenticate({ - id: sessionProposal.id, - auths, + methods: checkedMethods, }) drawerRouter.reset() drawerRouter.goTo(DappConfigRoute.ConnectedDapps) - loading = false } catch (error) { - loading = false handleError(error) + } finally { + loading = false } } diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte index 2d6afc215b..6144a32c1d 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte @@ -46,10 +46,10 @@ const optionalNamespaces = sessionProposal.params.optionalNamespaces const requiredNetworks = Object.values(requiredNamespaces) - .flatMap(({ chains }) => chains) + .flatMap(({ chains }) => chains ?? []) .filter(Boolean) const optionalNetworks = Object.values(optionalNamespaces) - .flatMap(({ chains }) => chains) + .flatMap(({ chains }) => chains ?? []) .filter(Boolean) const requiredMethods = Object.values(requiredNamespaces).flatMap(({ methods }) => methods) as RpcMethod[] diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSessionAuthenticate.ts b/packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSessionAuthenticate.ts new file mode 100644 index 0000000000..3e789aa679 --- /dev/null +++ b/packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSessionAuthenticate.ts @@ -0,0 +1,72 @@ +import { getAddressFromAccountForNetwork } from '@core/account' +import { clearConnectionRequest, getWalletClient, persistDapp } from '../stores' +import { clearOldPairings } from './clearOldPairings' +import { Web3WalletTypes } from '@walletconnect/web3wallet' +import { DappVerification } from '../enums' +import { GENERAL_SUPPORTED_METHODS } from '../constants' +import { populateAuthPayload, buildAuthObject } from '@walletconnect/utils' +import { NetworkId, getEvmNetwork } from '@core/network' +import { AuthTypes } from '@walletconnect/types' +import { ISelections } from '../interface' +import { signMessage } from '@core/wallet' +import { normalizeSessionNamespace } from '../utils' + +export async function approveSessionAuthenticate( + sessionProposal: Web3WalletTypes.SessionAuthenticate, + selections: ISelections +): Promise { + const walletClient = getWalletClient() + if (!walletClient) { + return + } + + const allowedMethods = [...(selections.methods ?? []), ...GENERAL_SUPPORTED_METHODS] + + const authPayload = populateAuthPayload({ + authPayload: sessionProposal.params.authPayload, + chains: selections.chains ?? [], + methods: allowedMethods, + }) + + const auths: AuthTypes.Cacao[] = [] + for (const chain of authPayload.chains) { + const network = getEvmNetwork(chain as NetworkId) + if (!network) { + continue + } + + for (const account of selections.accounts ?? []) { + const address = `${chain}:${getAddressFromAccountForNetwork(account, network.id)}` + + const message = walletClient.formatAuthMessage({ + request: authPayload, + iss: address, + }) + const signature = await signMessage(message, network.coinType, account) + if (!signature) { + continue + } + + const auth = buildAuthObject(authPayload, { t: 'eip191', s: signature }, address) + auths.push(auth) + } + } + + const { session } = await walletClient.approveSessionAuthenticate({ + id: sessionProposal.id, + auths, + }) + + if (!session) { + throw new Error('Failed to approve session authenticate') + } + + const dappUrl = sessionProposal.params.requester.metadata.url + await clearOldPairings(dappUrl) + persistDapp(dappUrl, DappVerification.Unknown, { + supported: normalizeSessionNamespace(session.namespaces), + required: session.requiredNamespaces, + optional: session.optionalNamespaces, + }) + clearConnectionRequest() +} diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/actions/index.ts b/packages/shared/src/lib/auxiliary/wallet-connect/actions/index.ts index 87c5b73c23..2969f8bb30 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/actions/index.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/actions/index.ts @@ -1,4 +1,5 @@ export * from './approveSession' +export * from './approveSessionAuthenticate' export * from './buildSupportedNamespaceFromSelections' export * from './clearOldPairings' export * from './connectToDapp' diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/utils/index.ts b/packages/shared/src/lib/auxiliary/wallet-connect/utils/index.ts index 2814e8ce4c..9c010c3295 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/utils/index.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/utils/index.ts @@ -1,6 +1,7 @@ export * from './disconnectAllDapps' export * from './getBloomError' export * from './getPermissionForMethod' +export * from './normalizeSupportedNamespace' export * from './rejectConnectionRequest' export * from './switchToRequiredAccount' export * from './updateSession' diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/utils/normalizeSupportedNamespace.ts b/packages/shared/src/lib/auxiliary/wallet-connect/utils/normalizeSupportedNamespace.ts new file mode 100644 index 0000000000..12d064e0e2 --- /dev/null +++ b/packages/shared/src/lib/auxiliary/wallet-connect/utils/normalizeSupportedNamespace.ts @@ -0,0 +1,18 @@ +import type { SessionTypes } from '@walletconnect/types' +import type { ISupportedNamespace } from '../types' + +export function normalizeSessionNamespace( + sessionNamespace: SessionTypes.Namespaces +): Record { + const normalizedRecord: Record = {} + + for (const key in sessionNamespace) { + const session = sessionNamespace[key] + normalizedRecord[key] = { + ...session, + chains: session.chains || [], + } + } + + return normalizedRecord +} From f705d62931e399075763c364cfd4a68652c77c69 Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Fri, 31 May 2024 13:59:54 +0200 Subject: [PATCH 06/24] fix edit drawers --- .../components/EditSelectionDrawer.svelte | 127 ++++++++++++++++-- .../views/EditNetworksDrawer.svelte | 6 +- .../views/EditPermissionsDrawer.svelte | 6 +- .../dapp-config/views/OneClickAuthFlow.svelte | 25 +--- .../constants/all-evm-methods.constant.ts | 23 ++++ .../wallet-connect/constants/index.ts | 1 + 6 files changed, 148 insertions(+), 40 deletions(-) create mode 100644 packages/shared/src/lib/auxiliary/wallet-connect/constants/all-evm-methods.constant.ts diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/components/EditSelectionDrawer.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/components/EditSelectionDrawer.svelte index ec54918b88..4a82aca2f2 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/components/EditSelectionDrawer.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/components/EditSelectionDrawer.svelte @@ -6,35 +6,138 @@ import { updateSupportedDappNamespacesForDapp, selectedDapp, - sessionProposal, + connectionRequest, getPersistedDapp, + ConnectionRequest, } from '@auxiliary/wallet-connect/stores' import { onMount } from 'svelte' import { buildSupportedNamespacesFromSelections } from '@auxiliary/wallet-connect/actions' import { updateSession } from '@auxiliary/wallet-connect/utils' - import { IDappMetadata, ISelections } from '@auxiliary/wallet-connect/interface' + import { IConnectedDapp, IDappMetadata, ISelections } from '@auxiliary/wallet-connect/interface' import { DappInfo } from '@ui' + import { ALL_EVM_METHODS } from '@auxiliary/wallet-connect/constants' export let drawerRouter: Router export let selections: ISelections export let titleLocale: string export let disableContinue: boolean - $: dappMetadata = $selectedDapp?.metadata ?? ($sessionProposal?.params.proposer.metadata as IDappMetadata) + const dappMetadata = getDappMetadata($selectedDapp, $connectionRequest) + const { requiredMethods, optionalMethods } = getMethodsForNamespaces($selectedDapp, $connectionRequest) + const { requiredNetworks, optionalNetworks } = getNetworksForNamespaces($selectedDapp, $connectionRequest) + $: persistedDapp = dappMetadata ? getPersistedDapp(dappMetadata.url) : undefined - $: requiredNamespaces = - $selectedDapp?.session?.requiredNamespaces ?? $sessionProposal?.params.requiredNamespaces ?? {} - $: optionalNamespaces = - $selectedDapp?.session?.optionalNamespaces ?? $sessionProposal?.params.optionalNamespaces ?? {} + + function getDappMetadata( + selectedDapp: IConnectedDapp | undefined, + connectionRequest: ConnectionRequest | undefined + ): IDappMetadata | undefined { + if (selectedDapp) { + return selectedDapp?.metadata + } else if (connectionRequest?.type === 'session_proposal') { + return connectionRequest?.payload.params.proposer.metadata as IDappMetadata + } else if (connectionRequest?.type === 'session_authenticate') { + return connectionRequest?.payload.params.requester.metadata as IDappMetadata + } else { + return undefined + } + } + + function getMethodsForNamespaces( + selectedDapp: IConnectedDapp | undefined, + connectionRequest: ConnectionRequest | undefined + ): { requiredMethods: string[]; optionalMethods: string[] } { + if (selectedDapp) { + const { requiredNamespaces, optionalNamespaces } = selectedDapp.session ?? { + requiredNamespaces: [], + optionalNamespaces: [], + } + return { + requiredMethods: Object.values(requiredNamespaces) + .flatMap(({ methods }) => methods ?? []) + .filter(Boolean), + optionalMethods: Object.values(optionalNamespaces) + .flatMap(({ methods }) => methods ?? []) + .filter(Boolean), + } + } else if (connectionRequest?.type === 'session_proposal') { + const { requiredNamespaces, optionalNamespaces } = connectionRequest.payload?.params ?? { + requiredNamespaces: [], + optionalNamespaces: [], + } + return { + requiredMethods: Object.values(requiredNamespaces) + .flatMap(({ methods }) => methods ?? []) + .filter(Boolean), + optionalMethods: Object.values(optionalNamespaces) + .flatMap(({ methods }) => methods ?? []) + .filter(Boolean), + } + } else if (connectionRequest?.type === 'session_authenticate') { + return { requiredMethods: [], optionalMethods: ALL_EVM_METHODS } + } else { + return { requiredMethods: [], optionalMethods: [] } + } + } + + function getNetworksForNamespaces( + selectedDapp: IConnectedDapp | undefined, + connectionRequest: ConnectionRequest | undefined + ): { requiredNetworks: string[]; optionalNetworks: string[] } { + if (selectedDapp) { + const { requiredNamespaces, optionalNamespaces } = selectedDapp.session ?? { + requiredNamespaces: [], + optionalNamespaces: [], + } + return { + requiredNetworks: Object.values(requiredNamespaces) + .flatMap(({ chains }) => chains ?? []) + .filter(Boolean), + optionalNetworks: Object.values(optionalNamespaces) + .flatMap(({ chains }) => chains ?? []) + .filter(Boolean), + } + } else if (connectionRequest?.type === 'session_proposal') { + const { requiredNamespaces, optionalNamespaces } = connectionRequest.payload?.params ?? { + requiredNamespaces: [], + optionalNamespaces: [], + } + return { + requiredNetworks: Object.values(requiredNamespaces) + .flatMap(({ chains }) => chains ?? []) + .filter(Boolean), + optionalNetworks: Object.values(optionalNamespaces) + .flatMap(({ chains }) => chains ?? []) + .filter(Boolean), + } + } else if (connectionRequest?.type === 'session_authenticate') { + // TODO: Implement this + return { requiredNetworks: [], optionalNetworks: [] } + } else { + return { requiredNetworks: [], optionalNetworks: [] } + } + } function onConfirmClick(): void { + // TODO: Implement this + if ($connectionRequest?.type === 'session_authenticate') { + return + } + + const requiredNamespaces = + $selectedDapp?.session?.requiredNamespaces ?? $connectionRequest?.payload?.params.requiredNamespaces ?? {} + const optionalNamespaces = + $selectedDapp?.session?.optionalNamespaces ?? $connectionRequest?.payload?.params.optionalNamespaces ?? {} + const updatedNamespace = buildSupportedNamespacesFromSelections( selections, requiredNamespaces, optionalNamespaces, persistedDapp?.namespaces.supported ) - updateSupportedDappNamespacesForDapp(dappMetadata.url, updatedNamespace) + if (dappMetadata) { + updateSupportedDappNamespacesForDapp(dappMetadata.url, updatedNamespace) + } if ($selectedDapp?.session) { updateSession($selectedDapp.session.topic, updatedNamespace) } @@ -46,7 +149,7 @@ } onMount(() => { - if (!$selectedDapp && !$sessionProposal) { + if (!$selectedDapp && !$connectionRequest) { drawerRouter.previous() } }) @@ -60,8 +163,10 @@
diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/EditNetworksDrawer.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/EditNetworksDrawer.svelte index c531df7a96..65d6af6b70 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/EditNetworksDrawer.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/EditNetworksDrawer.svelte @@ -12,9 +12,9 @@ titleLocale="dapps.editNetworks" selections={{ chains: checkedNetworks }} let:persistedSupportedNamespaces - let:requiredNamespaces - let:optionalNamespaces + let:requiredNetworks + let:optionalNetworks disableContinue={checkedNetworks.length === 0} > - + diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/EditPermissionsDrawer.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/EditPermissionsDrawer.svelte index ab85a89f90..1b2fd7a17f 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/EditPermissionsDrawer.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/EditPermissionsDrawer.svelte @@ -12,9 +12,9 @@ titleLocale="dapps.editPermissions" selections={{ methods: checkedMethods }} let:persistedSupportedNamespaces - let:requiredNamespaces - let:optionalNamespaces + let:requiredMethods + let:optionalMethods disableContinue={checkedMethods.length === 0} > - + diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte index 3ee2a9c0ea..3c3932bd85 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte @@ -16,6 +16,7 @@ import { checkActiveProfileAuth } from '@core/profile/actions' import { LedgerAppName } from '@core/ledger' import { approveSessionAuthenticate } from '@auxiliary/wallet-connect/actions/approveSessionAuthenticate' + import { ALL_EVM_METHODS } from '@auxiliary/wallet-connect/constants' export let drawerRouter: Router export let sessionProposal: Web3WalletTypes.SessionAuthenticate @@ -42,29 +43,7 @@ const optionalNetworks = sessionProposal.params.authPayload.chains const requiredMethods = [] - const optionalMethods = [ - 'eth_accounts', - 'eth_requestAccounts', - 'eth_sendRawTransaction', - 'eth_sendTransaction', - 'eth_sign', - 'eth_signTransaction', - 'eth_signTypedData', - 'eth_signTypedData_v3', - 'eth_signTypedData_v4', - 'personal_sign', - 'wallet_addEthereumChain', - 'wallet_getCallsStatus', - 'wallet_getCapabilities', - 'wallet_getPermissions', - 'wallet_registerOnboarding', - 'wallet_requestPermissions', - 'wallet_scanQRCode', - 'wallet_sendCalls', - 'wallet_showCallsStatus', - 'wallet_switchEthereumChain', - 'wallet_watchAsset', - ] + const optionalMethods = ALL_EVM_METHODS $: isButtonDisabled = loading || diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/constants/all-evm-methods.constant.ts b/packages/shared/src/lib/auxiliary/wallet-connect/constants/all-evm-methods.constant.ts new file mode 100644 index 0000000000..b060708be7 --- /dev/null +++ b/packages/shared/src/lib/auxiliary/wallet-connect/constants/all-evm-methods.constant.ts @@ -0,0 +1,23 @@ +export const ALL_EVM_METHODS = [ + 'eth_accounts', + 'eth_requestAccounts', + 'eth_sendRawTransaction', + 'eth_sendTransaction', + 'eth_sign', + 'eth_signTransaction', + 'eth_signTypedData', + 'eth_signTypedData_v3', + 'eth_signTypedData_v4', + 'personal_sign', + 'wallet_addEthereumChain', + 'wallet_getCallsStatus', + 'wallet_getCapabilities', + 'wallet_getPermissions', + 'wallet_registerOnboarding', + 'wallet_requestPermissions', + 'wallet_scanQRCode', + 'wallet_sendCalls', + 'wallet_showCallsStatus', + 'wallet_switchEthereumChain', + 'wallet_watchAsset', +] diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/constants/index.ts b/packages/shared/src/lib/auxiliary/wallet-connect/constants/index.ts index ea6c8a12f5..b127cdfa57 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/constants/index.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/constants/index.ts @@ -1,3 +1,4 @@ +export * from './all-evm-methods.constant' export * from './all-supported-methods.constant' export * from './general-supported-methods.constant' export * from './methods-for-permissions.constant' From 109aac8ccf80de71600ccc9c0f1e2bc04349f905 Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Fri, 31 May 2024 13:59:54 +0200 Subject: [PATCH 07/24] cleanup --- .../components/EditSelectionDrawer.svelte | 75 +++++++------------ .../views/ConnectionRequestDrawer.svelte | 15 ++-- .../views/SessionProposalFlow.svelte | 15 +--- .../getNetworksAndMethodsFromNamespaces.ts | 42 +++++++++++ .../auxiliary/wallet-connect/utils/index.ts | 1 + 5 files changed, 78 insertions(+), 70 deletions(-) create mode 100644 packages/shared/src/lib/auxiliary/wallet-connect/utils/getNetworksAndMethodsFromNamespaces.ts diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/components/EditSelectionDrawer.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/components/EditSelectionDrawer.svelte index 4a82aca2f2..05ac7e6a6b 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/components/EditSelectionDrawer.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/components/EditSelectionDrawer.svelte @@ -12,7 +12,7 @@ } from '@auxiliary/wallet-connect/stores' import { onMount } from 'svelte' import { buildSupportedNamespacesFromSelections } from '@auxiliary/wallet-connect/actions' - import { updateSession } from '@auxiliary/wallet-connect/utils' + import { getNetworksAndMethodsFromNamespaces, updateSession } from '@auxiliary/wallet-connect/utils' import { IConnectedDapp, IDappMetadata, ISelections } from '@auxiliary/wallet-connect/interface' import { DappInfo } from '@ui' import { ALL_EVM_METHODS } from '@auxiliary/wallet-connect/constants' @@ -48,31 +48,20 @@ connectionRequest: ConnectionRequest | undefined ): { requiredMethods: string[]; optionalMethods: string[] } { if (selectedDapp) { - const { requiredNamespaces, optionalNamespaces } = selectedDapp.session ?? { - requiredNamespaces: [], - optionalNamespaces: [], - } - return { - requiredMethods: Object.values(requiredNamespaces) - .flatMap(({ methods }) => methods ?? []) - .filter(Boolean), - optionalMethods: Object.values(optionalNamespaces) - .flatMap(({ methods }) => methods ?? []) - .filter(Boolean), - } + const { requiredNamespaces, optionalNamespaces } = selectedDapp.session ?? {} + + const { requiredMethods, optionalMethods } = getNetworksAndMethodsFromNamespaces( + requiredNamespaces ?? {}, + optionalNamespaces ?? {} + ) + return { requiredMethods, optionalMethods } } else if (connectionRequest?.type === 'session_proposal') { - const { requiredNamespaces, optionalNamespaces } = connectionRequest.payload?.params ?? { - requiredNamespaces: [], - optionalNamespaces: [], - } - return { - requiredMethods: Object.values(requiredNamespaces) - .flatMap(({ methods }) => methods ?? []) - .filter(Boolean), - optionalMethods: Object.values(optionalNamespaces) - .flatMap(({ methods }) => methods ?? []) - .filter(Boolean), - } + const { requiredNamespaces, optionalNamespaces } = connectionRequest.payload?.params ?? {} + const { requiredMethods, optionalMethods } = getNetworksAndMethodsFromNamespaces( + requiredNamespaces ?? {}, + optionalNamespaces ?? {} + ) + return { requiredMethods, optionalMethods } } else if (connectionRequest?.type === 'session_authenticate') { return { requiredMethods: [], optionalMethods: ALL_EVM_METHODS } } else { @@ -85,31 +74,19 @@ connectionRequest: ConnectionRequest | undefined ): { requiredNetworks: string[]; optionalNetworks: string[] } { if (selectedDapp) { - const { requiredNamespaces, optionalNamespaces } = selectedDapp.session ?? { - requiredNamespaces: [], - optionalNamespaces: [], - } - return { - requiredNetworks: Object.values(requiredNamespaces) - .flatMap(({ chains }) => chains ?? []) - .filter(Boolean), - optionalNetworks: Object.values(optionalNamespaces) - .flatMap(({ chains }) => chains ?? []) - .filter(Boolean), - } + const { requiredNamespaces, optionalNamespaces } = selectedDapp.session ?? {} + const { requiredNetworks, optionalNetworks } = getNetworksAndMethodsFromNamespaces( + requiredNamespaces ?? {}, + optionalNamespaces ?? {} + ) + return { requiredNetworks, optionalNetworks } } else if (connectionRequest?.type === 'session_proposal') { - const { requiredNamespaces, optionalNamespaces } = connectionRequest.payload?.params ?? { - requiredNamespaces: [], - optionalNamespaces: [], - } - return { - requiredNetworks: Object.values(requiredNamespaces) - .flatMap(({ chains }) => chains ?? []) - .filter(Boolean), - optionalNetworks: Object.values(optionalNamespaces) - .flatMap(({ chains }) => chains ?? []) - .filter(Boolean), - } + const { requiredNamespaces, optionalNamespaces } = connectionRequest.payload?.params ?? {} + const { requiredNetworks, optionalNetworks } = getNetworksAndMethodsFromNamespaces( + requiredNamespaces ?? {}, + optionalNamespaces ?? {} + ) + return { requiredNetworks, optionalNetworks } } else if (connectionRequest?.type === 'session_authenticate') { // TODO: Implement this return { requiredNetworks: [], optionalNetworks: [] } diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequestDrawer.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequestDrawer.svelte index 6ca326efbd..cab03024d9 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequestDrawer.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequestDrawer.svelte @@ -6,7 +6,7 @@ import { connectionRequest } from '@auxiliary/wallet-connect/stores' import { closeDrawer } from '@desktop/auxiliary/drawer' import ConnectionRequest from './ConnectionRequest.svelte' - import { rejectConnectionRequest } from '@auxiliary/wallet-connect/utils' + import { getNetworksAndMethodsFromNamespaces, rejectConnectionRequest } from '@auxiliary/wallet-connect/utils' import { showNotification } from '@auxiliary/notification' import { onDestroy } from 'svelte' import { DappVerification, RpcMethod } from '@auxiliary/wallet-connect/enums' @@ -47,15 +47,10 @@ ? DappVerification.Scam : (sessionProposal.verifyContext.verified.validation as DappVerification) - const requiredNetworks = Object.values(sessionProposal.params.requiredNamespaces) - .flatMap(({ chains }) => chains ?? []) - .filter(Boolean) - const optionalNetworks = Object.values(sessionProposal.params.optionalNamespaces) - .flatMap(({ chains }) => chains ?? []) - .filter(Boolean) - const requiredMethods = Object.values(sessionProposal.params.requiredNamespaces).flatMap( - ({ methods }) => methods - ) as RpcMethod[] + const { requiredNetworks, optionalNetworks, requiredMethods } = getNetworksAndMethodsFromNamespaces( + sessionProposal.params.requiredNamespaces, + sessionProposal.params.optionalNamespaces + ) return { dappMetadata, diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte index 6144a32c1d..d6f92ce2df 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte @@ -6,14 +6,14 @@ import { DrawerTemplate } from '@components' import { connectToDapp } from '@auxiliary/wallet-connect/actions' import { getPersistedDappNamespacesForDapp } from '@auxiliary/wallet-connect/stores' - import { rejectConnectionRequest } from '@auxiliary/wallet-connect/utils' + import { getNetworksAndMethodsFromNamespaces, rejectConnectionRequest } from '@auxiliary/wallet-connect/utils' import { AccountSelection, ConnectionSummary, NetworkSelection, PermissionSelection } from '../components' import { handleError } from '@core/error/handlers' import { IAccountState } from '@core/account' import { DappConfigRoute } from '../dapp-config-route.enum' import { closeDrawer } from '@desktop/auxiliary/drawer' import { selectedAccount } from '@core/account/stores' - import { DappVerification, RpcMethod } from '@auxiliary/wallet-connect/enums' + import { DappVerification } from '@auxiliary/wallet-connect/enums' import { deepEquals } from '@core/utils/object' import { ProposalTypes } from '@walletconnect/types' import { IPersistedNamespaces } from '@auxiliary/wallet-connect/interface' @@ -45,15 +45,8 @@ const requiredNamespaces = sessionProposal.params.requiredNamespaces const optionalNamespaces = sessionProposal.params.optionalNamespaces - const requiredNetworks = Object.values(requiredNamespaces) - .flatMap(({ chains }) => chains ?? []) - .filter(Boolean) - const optionalNetworks = Object.values(optionalNamespaces) - .flatMap(({ chains }) => chains ?? []) - .filter(Boolean) - - const requiredMethods = Object.values(requiredNamespaces).flatMap(({ methods }) => methods) as RpcMethod[] - const optionalMethods = Object.values(optionalNamespaces).flatMap(({ methods }) => methods) as RpcMethod[] + const { requiredNetworks, optionalNetworks, requiredMethods, optionalMethods } = + getNetworksAndMethodsFromNamespaces(requiredNamespaces, optionalNamespaces) $: isButtonDisabled = loading || diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/utils/getNetworksAndMethodsFromNamespaces.ts b/packages/shared/src/lib/auxiliary/wallet-connect/utils/getNetworksAndMethodsFromNamespaces.ts new file mode 100644 index 0000000000..18e6245527 --- /dev/null +++ b/packages/shared/src/lib/auxiliary/wallet-connect/utils/getNetworksAndMethodsFromNamespaces.ts @@ -0,0 +1,42 @@ +import type { ProposalTypes } from '@walletconnect/types' +import { RpcMethod } from '../enums' + +export function getNetworksAndMethodsFromNamespaces( + requiredNamespaces: ProposalTypes.RequiredNamespaces, + optionalNamespaces: ProposalTypes.OptionalNamespaces +): { + requiredNetworks: string[] + optionalNetworks: string[] + requiredMethods: RpcMethod[] + optionalMethods: RpcMethod[] +} { + const requiredNetworks: string[] = [] + const optionalNetworks: string[] = [] + const requiredMethods: RpcMethod[] = [] + const optionalMethods: RpcMethod[] = [] + + for (const namespace of Object.values(requiredNamespaces ?? {})) { + if (namespace.chains) { + requiredNetworks.push(...namespace.chains) + } + if (namespace.methods) { + requiredMethods.push(...(namespace.methods as RpcMethod[])) + } + } + + for (const namespace of Object.values(optionalNamespaces ?? {})) { + if (namespace.chains) { + requiredNetworks.push(...namespace.chains) + } + if (namespace.methods) { + requiredMethods.push(...(namespace.methods as RpcMethod[])) + } + } + + return { + requiredNetworks, + optionalNetworks, + requiredMethods, + optionalMethods, + } +} diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/utils/index.ts b/packages/shared/src/lib/auxiliary/wallet-connect/utils/index.ts index 9c010c3295..f116495549 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/utils/index.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/utils/index.ts @@ -1,5 +1,6 @@ export * from './disconnectAllDapps' export * from './getBloomError' +export * from './getNetworksAndMethodsFromNamespaces' export * from './getPermissionForMethod' export * from './normalizeSupportedNamespace' export * from './rejectConnectionRequest' From 463f298c7b2d24cf57afd20913c2f747b6bc3973 Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Fri, 31 May 2024 14:00:12 +0200 Subject: [PATCH 08/24] update connected dapps type --- .../wallet-connect/actions/approveSession.ts | 13 +---- .../actions/clearOldPairings.ts | 4 +- .../wallet-connect/actions/disconnectDapp.ts | 4 +- .../actions/removeAllDisconnectedDapps.ts | 2 +- .../wallet-connect/actions/removeDapp.ts | 4 +- .../actions/updateAccountForConnectedDapps.ts | 4 +- .../actions/updateAccountForDappSession.ts | 16 ++++-- .../interface/connected-dapp.interface.ts | 18 ++++-- .../interface/session.interface.ts | 11 +--- .../stores/connected-dapps.store.ts | 56 +++++++++++++++---- 10 files changed, 83 insertions(+), 49 deletions(-) diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSession.ts b/packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSession.ts index 5604b20683..b86935dd30 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSession.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSession.ts @@ -5,18 +5,11 @@ import { Web3WalletTypes } from '@walletconnect/web3wallet' import { ISession } from '../interface/session.interface' import { getWalletClient, setConnectedDapps } from '../stores' import { updateAccountForDappSession } from './updateAccountForDappSession' +import { ISupportedNamespace } from '../types' export async function approveSession( sessionProposal: Web3WalletTypes.SessionProposal, - supportedNamespaces: Record< - string, - { - chains: string[] - methods: string[] - events: string[] - accounts: string[] - } - >, + supportedNamespaces: Record, account: IAccountState ): Promise { const { id, params } = sessionProposal @@ -32,7 +25,7 @@ export async function approveSession( namespaces: approvedNamespaces, })) as unknown as ISession setConnectedDapps() - await updateAccountForDappSession(session, account) + await updateAccountForDappSession(session.topic, session.namespaces, account) } catch (err) { handleError(err) } diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/actions/clearOldPairings.ts b/packages/shared/src/lib/auxiliary/wallet-connect/actions/clearOldPairings.ts index c14e5d270c..85984d7dd5 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/actions/clearOldPairings.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/actions/clearOldPairings.ts @@ -10,7 +10,9 @@ export async function clearOldPairings(dappOrigin: string): Promise { try { const duplicatedDapp = getConnectedDapps().filter((dapp) => dapp.metadata?.url === dappOrigin) for (const dapp of duplicatedDapp) { - await client.core.pairing.disconnect({ topic: dapp.topic }) + if (dapp.pairingTopic) { + await client.core.pairing.disconnect({ topic: dapp.pairingTopic }) + } } setConnectedDapps() } catch (err) { diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/actions/disconnectDapp.ts b/packages/shared/src/lib/auxiliary/wallet-connect/actions/disconnectDapp.ts index f8c1c9833e..da2b5e15d7 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/actions/disconnectDapp.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/actions/disconnectDapp.ts @@ -10,9 +10,9 @@ export async function disconnectDapp(dapp: IConnectedDapp): Promise { } try { - if (dapp.session) { + if (dapp.sessionTopic) { await client.disconnectSession({ - topic: dapp.session.topic, + topic: dapp.sessionTopic, reason: getSdkError('USER_DISCONNECTED'), }) } diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/actions/removeAllDisconnectedDapps.ts b/packages/shared/src/lib/auxiliary/wallet-connect/actions/removeAllDisconnectedDapps.ts index 037f7330d8..a340edb725 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/actions/removeAllDisconnectedDapps.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/actions/removeAllDisconnectedDapps.ts @@ -8,7 +8,7 @@ export function removeAllDisconnectedDapps(): void { } const connectedDappsForProfile = get(connectedDapps).filter( - (dapp) => !!getPersistedDapp(dapp.metadata?.url ?? '') && !dapp.session + (dapp) => !!getPersistedDapp(dapp.metadata?.url ?? '') && !dapp.sessionTopic ) for (const dapp of connectedDappsForProfile) { diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/actions/removeDapp.ts b/packages/shared/src/lib/auxiliary/wallet-connect/actions/removeDapp.ts index fa3fe239fc..d4b2802b47 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/actions/removeDapp.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/actions/removeDapp.ts @@ -10,9 +10,9 @@ export async function removeDapp(dapp: IConnectedDapp): Promise { } try { - if (dapp.session) { + if (dapp.sessionTopic) { await client.disconnectSession({ - topic: dapp.session.topic, + topic: dapp.sessionTopic, reason: getSdkError('USER_DISCONNECTED'), }) } diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/actions/updateAccountForConnectedDapps.ts b/packages/shared/src/lib/auxiliary/wallet-connect/actions/updateAccountForConnectedDapps.ts index b4bdc49b7c..b96aa5d5ef 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/actions/updateAccountForConnectedDapps.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/actions/updateAccountForConnectedDapps.ts @@ -6,8 +6,8 @@ import { updateAccountForDappSession } from './updateAccountForDappSession' export async function updateAccountForConnectedDapps(account: IAccountState): Promise { const dapps = get(connectedDapps) for (const dapp of dapps) { - if (dapp.session) { - await updateAccountForDappSession(dapp.session, account) + if (dapp.sessionTopic && dapp.namespaces) { + await updateAccountForDappSession(dapp.sessionTopic, dapp.namespaces, account) } } } diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/actions/updateAccountForDappSession.ts b/packages/shared/src/lib/auxiliary/wallet-connect/actions/updateAccountForDappSession.ts index c1fd4a3f58..4a91b82996 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/actions/updateAccountForDappSession.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/actions/updateAccountForDappSession.ts @@ -3,21 +3,25 @@ import { get } from 'svelte/store' import { WalletConnectEvents } from '../enums' import { walletClient } from '../stores' import { NetworkId } from '@core/network/types' -import { ISession } from '../interface/session.interface' +import { ISupportedNamespace } from '../types' -export async function updateAccountForDappSession(dappSession: ISession, account: IAccountState): Promise { +export async function updateAccountForDappSession( + sessionTopic: string, + namespaces: Record, + account: IAccountState +): Promise { const walletConnectClient = get(walletClient) - if (!walletConnectClient || !dappSession) { + if (!walletConnectClient) { return } - const protocols = Object.keys(dappSession.namespaces) + const protocols = Object.keys(namespaces ?? {}) for (const protocol of protocols) { - for (const chainId of dappSession.namespaces[protocol]?.chains ?? []) { + for (const chainId of namespaces[protocol]?.chains ?? []) { const address = getAddressFromAccountForNetwork(account, chainId as NetworkId) await walletConnectClient.emitSessionEvent({ - topic: dappSession.topic, + topic: sessionTopic, event: { name: WalletConnectEvents.AccountsChanged, data: [address], diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/interface/connected-dapp.interface.ts b/packages/shared/src/lib/auxiliary/wallet-connect/interface/connected-dapp.interface.ts index 166c855a28..b6ffd41cb5 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/interface/connected-dapp.interface.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/interface/connected-dapp.interface.ts @@ -1,8 +1,16 @@ +import { ISupportedNamespace } from '../types' import { IDappMetadata } from './dapp-metadata.interface' -import { IPairing } from './pairing.interface' -import { ISession } from './session.interface' +import type { ProposalTypes } from '@walletconnect/types' -export interface IConnectedDapp extends Omit { - metadata?: IDappMetadata - session?: ISession +export interface IConnectedDapp { + metadata: IDappMetadata + + // session + sessionTopic?: string + namespaces?: Record + requiredNamespaces?: ProposalTypes.RequiredNamespaces + optionalNamespaces?: ProposalTypes.OptionalNamespaces + + // pairing { + pairingTopic?: string } diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/interface/session.interface.ts b/packages/shared/src/lib/auxiliary/wallet-connect/interface/session.interface.ts index b274df967f..9b2356936b 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/interface/session.interface.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/interface/session.interface.ts @@ -1,4 +1,5 @@ import type { ProposalTypes, RelayerTypes, SignClientTypes } from '@walletconnect/types' +import { ISupportedNamespace } from '../types' export interface ISession { topic: string @@ -7,15 +8,7 @@ export interface ISession { expiry: number acknowledged: boolean controller: string - namespaces: Record< - string, - { - chains?: string[] - accounts: string[] - methods: string[] - events: string[] - } - > + namespaces: Record requiredNamespaces: ProposalTypes.RequiredNamespaces optionalNamespaces: ProposalTypes.OptionalNamespaces sessionProperties?: ProposalTypes.SessionProperties diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/stores/connected-dapps.store.ts b/packages/shared/src/lib/auxiliary/wallet-connect/stores/connected-dapps.store.ts index 43d54517f3..4a4dd78459 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/stores/connected-dapps.store.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/stores/connected-dapps.store.ts @@ -1,6 +1,8 @@ -import { Writable, get, writable } from 'svelte/store' +import { type Writable, get, writable } from 'svelte/store' import { getWalletClient } from './wallet-client.store' -import { IConnectedDapp } from '../interface/connected-dapp.interface' +import type { IConnectedDapp } from '../interface/connected-dapp.interface' +import type { ISupportedNamespace } from '../types' +import type { SessionTypes } from '@walletconnect/types' export const connectedDapps: Writable = writable([]) @@ -8,14 +10,32 @@ export function setConnectedDapps(): void { const pairings = getWalletClient()?.core.pairing.getPairings() ?? [] const sessions = getWalletClient()?.getActiveSessions() ?? {} - const dapps: IConnectedDapp[] = pairings.map((pairing) => ({ - topic: pairing.topic, - expiry: pairing.expiry, - relay: pairing.relay, - active: pairing.active, - metadata: pairing.peerMetadata, - session: Object.values(sessions).find((session) => session.pairingTopic === pairing.topic), - })) + const dapps: IConnectedDapp[] = [] + for (const session of Object.values(sessions)) { + dapps.push({ + sessionTopic: session.topic, + pairingTopic: session.pairingTopic, + metadata: session.peer.metadata, + namespaces: normalizeSessionNamespace(session.namespaces), + requiredNamespaces: session.requiredNamespaces, + optionalNamespaces: session.optionalNamespaces, + }) + } + for (const pairing of pairings) { + if (!pairing.peerMetadata) { + continue + } + + const existingDapp = dapps.find((dapp) => dapp.pairingTopic === pairing.topic) + if (existingDapp) { + existingDapp.pairingTopic = pairing.topic + } else { + dapps.push({ + pairingTopic: pairing.topic, + metadata: pairing.peerMetadata, + }) + } + } connectedDapps.set(dapps) } @@ -28,5 +48,19 @@ export function getConnectedDappByOrigin(origin: string): IConnectedDapp | undef } export function getConnectedDappBySessionTopic(sessionTopic: string): IConnectedDapp | undefined { - return get(connectedDapps).find((dapp) => dapp.session?.topic === sessionTopic) + return get(connectedDapps).find((dapp) => dapp.sessionTopic === sessionTopic) +} + +function normalizeSessionNamespace(sessionNamespace: SessionTypes.Namespaces): Record { + const normalizedRecord: Record = {} + + for (const key in sessionNamespace) { + const session = sessionNamespace[key] + normalizedRecord[key] = { + ...session, + chains: session.chains || [], + } + } + + return normalizedRecord } From 7cf778b55abb161d36cf2ee4247a10e919f64de2 Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Fri, 31 May 2024 14:00:12 +0200 Subject: [PATCH 09/24] update ui components --- .../dapp-config/views/ConnectedDappsDrawer.svelte | 13 ++++++++----- .../dapp-config/views/DappDetailsDrawer.svelte | 4 ++-- .../wallet-connect/stores/selected-dapp.store.ts | 4 ++++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectedDappsDrawer.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectedDappsDrawer.svelte index d2e4df094b..e6850d3293 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectedDappsDrawer.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectedDappsDrawer.svelte @@ -1,5 +1,10 @@ -{#if $connectionRequest?.type === 'session_proposal'} - -{:else if $connectionRequest?.type === 'session_authenticate'} - +{#if $sessionInitiationRequest?.type === 'session_proposal'} + +{:else if $sessionInitiationRequest?.type === 'session_authenticate'} + {:else}
diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequestDrawer.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequestDrawer.svelte index cab03024d9..8230eed6b5 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequestDrawer.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequestDrawer.svelte @@ -3,7 +3,7 @@ import { localize } from '@core/i18n' import { Router } from '@core/router' import { DrawerTemplate } from '@components' - import { connectionRequest } from '@auxiliary/wallet-connect/stores' + import { sessionInitiationRequest } from '@auxiliary/wallet-connect/stores' import { closeDrawer } from '@desktop/auxiliary/drawer' import ConnectionRequest from './ConnectionRequest.svelte' import { getNetworksAndMethodsFromNamespaces, rejectConnectionRequest } from '@auxiliary/wallet-connect/utils' @@ -18,7 +18,7 @@ let timeout: ReturnType | undefined $: { - if ($connectionRequest) { + if ($sessionInitiationRequest) { clearTimeout(timeout) } else { timeout = setTimeout(() => { @@ -83,9 +83,9 @@ }) -{#if $connectionRequest?.type === 'session_proposal'} +{#if $sessionInitiationRequest?.type === 'session_proposal'} {@const { dappMetadata, verifiedState, requiredNetworks, optionalNetworks, requiredMethods } = - getConnectionRequestDataFromSessionProposal($connectionRequest.payload)} + getConnectionRequestDataFromSessionProposal($sessionInitiationRequest.payload)} -{:else if $connectionRequest?.type === 'session_authenticate'} +{:else if $sessionInitiationRequest?.type === 'session_authenticate'} {@const { dappMetadata, verifiedState, requiredNetworks, optionalNetworks, requiredMethods } = - getConnectionRequestDataFromSessionAuthenticate($connectionRequest.payload)} + getConnectionRequestDataFromSessionAuthenticate($sessionInitiationRequest.payload)} = writable(undefined) +export const sessionInitiationRequest: Writable = writable(undefined) -export function clearConnectionRequest(): void { - connectionRequest.set(undefined) +export function clearSessionInitiationRequest(): void { + sessionInitiationRequest.set(undefined) } diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/utils/rejectConnectionRequest.ts b/packages/shared/src/lib/auxiliary/wallet-connect/utils/rejectConnectionRequest.ts index 9341eef637..849555a363 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/utils/rejectConnectionRequest.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/utils/rejectConnectionRequest.ts @@ -1,15 +1,15 @@ import { getSdkError } from '@walletconnect/utils' -import { getWalletClient, connectionRequest } from '../stores' +import { getWalletClient, sessionInitiationRequest } from '../stores' import { get } from 'svelte/store' import { handleError } from '@core/error/handlers' export async function rejectConnectionRequest(): Promise { - const _connectionRequest = get(connectionRequest) + const _connectionRequest = get(sessionInitiationRequest) if (!_connectionRequest) { return } - connectionRequest.set(undefined) + sessionInitiationRequest.set(undefined) try { if (_connectionRequest.type === 'session_proposal') { await getWalletClient()?.rejectSession({ From 7c91d6a49aeb1bef5851198108f7aa54a8c1566e Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Thu, 20 Jun 2024 13:51:36 +0200 Subject: [PATCH 11/24] Merge conflicts --- .../views/ConnectionRequest.svelte | 27 +- .../views/SessionProposalFlow.svelte | 311 ++++++++++-------- 2 files changed, 196 insertions(+), 142 deletions(-) diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequest.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequest.svelte index 0942ba487b..f98eaea870 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequest.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequest.svelte @@ -5,12 +5,15 @@ import { Router } from '@core/router' import { DrawerTemplate } from '@components' import { closeDrawer } from '@desktop/auxiliary/drawer' - import { SecurityWarning, UnsupportedDappHint } from '../components' + import { ConnectionRequestExpirationAlert, SecurityWarning, UnsupportedDappHint } from '../components' import { getAllNetworkIds } from '@core/network' import { ALL_SUPPORTED_METHODS } from '@auxiliary/wallet-connect/constants' import { rejectConnectionRequest } from '@auxiliary/wallet-connect/utils' import { DappVerification, RpcMethod } from '@auxiliary/wallet-connect/enums' import { Web3WalletTypes } from '@walletconnect/web3wallet' + import { onMount } from 'svelte' + import { MILLISECONDS_PER_SECOND } from '@core/utils' + import { time } from '@core/app/stores' export let drawerRouter: Router export let dappMetadata: Web3WalletTypes.Metadata @@ -18,11 +21,14 @@ export let requiredNetworks: string[] export let optionalNetworks: string[] export let requiredMethods: RpcMethod[] + export let expiryTimestamp: number | undefined const localeKey = 'views.dashboard.drawers.dapps.connectionRequest' let acceptedInsecureConnection = false let flashingCheckbox = false + $: hasRequestExpired = expiryTimestamp ? expiryTimestamp - $time.getTime() / MILLISECONDS_PER_SECOND <= 0 : false + const fulfillsRequirements = doesFulfillsRequirements() function doesFulfillsRequirements(): boolean { const supportedNetworksByDapp = [...requiredNetworks, ...optionalNetworks] @@ -71,11 +77,24 @@ drawerRouter.next() } + + onMount(() => { + const fulfillsRequirements = + doesFulfillNetworkRequirements(requiredNetworks, optionalNetworks) && + doesFulfillMethodRequirements(requiredMethods) + + if (fulfillsRequirements && verifiedState === DappVerification.Valid) { + drawerRouter.next() + } + }) - +
- +
+ + +
{#if fulfillsRequirements && verifiedState !== DappVerification.Scam}
- - !loading || !hasRequestExpired ? (activeSelection = Selection.Permissions) : undefined} - onEditNetworksClick={() => - !loading || !hasRequestExpired ? (activeSelection = Selection.Networks) : undefined} - onEditAccountsClick={() => - !loading || !hasRequestExpired ? (activeSelection = Selection.Accounts) : undefined} - /> - - {:else} -
- -
-
- -
-
- -
- {/if} - - -
- {#if activeSelection === Selection.Summary} -
- + diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlowShared.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlowShared.svelte new file mode 100644 index 0000000000..dea5286468 --- /dev/null +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlowShared.svelte @@ -0,0 +1,190 @@ + + + +
+
+ + +
+ +
+ {#if activeSelection === Selection.Summary} +
+
+ + !loading || !hasRequestExpired ? (activeSelection = Selection.Permissions) : undefined} + onEditNetworksClick={() => + !loading || !hasRequestExpired ? (activeSelection = Selection.Networks) : undefined} + onEditAccountsClick={() => + !loading || !hasRequestExpired ? (activeSelection = Selection.Accounts) : undefined} + /> + + {:else} +
+ +
+
+ +
+
+ +
+ {/if} + + +
+ {#if activeSelection === Selection.Summary} +
+ diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/index.ts b/packages/desktop/views/dashboard/drawers/dapp-config/views/index.ts index e0223130de..c949672304 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/index.ts +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/index.ts @@ -6,3 +6,6 @@ export { default as InputConnectionCodeDrawer } from './InputConnectionCodeDrawe export { default as EditAccountsDrawer } from './EditAccountsDrawer.svelte' export { default as EditNetworksDrawer } from './EditNetworksDrawer.svelte' export { default as EditPermissionsDrawer } from './EditPermissionsDrawer.svelte' +export { default as OneClickAuthFlow } from './OneClickAuthFlow.svelte' +export { default as SessionProposalFlow } from './SessionProposalFlow.svelte' +export { default as SessionProposalFlowShared } from './SessionProposalFlowShared.svelte' From 86ccc8e01c0c89395549180b672cf6498a195346 Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Tue, 25 Jun 2024 13:42:14 +0200 Subject: [PATCH 17/24] use shared connection comp for one click auth --- .../dapp-config/views/OneClickAuthFlow.svelte | 182 ++++++------------ .../views/SessionProposalFlow.svelte | 54 +++--- .../views/SessionProposalFlowShared.svelte | 16 +- .../utils/buildDefaultNamespaces.ts | 30 +++ .../auxiliary/wallet-connect/utils/index.ts | 1 + 5 files changed, 127 insertions(+), 156 deletions(-) create mode 100644 packages/shared/src/lib/auxiliary/wallet-connect/utils/buildDefaultNamespaces.ts diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte index 7f7c7f9930..cbdc055e42 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte @@ -1,150 +1,96 @@ - -
- - -
-
- - -
-
- -
- -
-
- -
-
- -
-
-
-
-
-
-
-
-
+ diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte index 7024a6bd59..2b54d4e4f6 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte @@ -1,14 +1,11 @@ @@ -80,5 +71,6 @@ {requiredNamespaces} {optionalNamespaces} {supportedNamespaces} + confirmButtonLocaleKey="general.confirm" {onConfirm} /> diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlowShared.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlowShared.svelte index dea5286468..6d53da36ae 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlowShared.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlowShared.svelte @@ -36,7 +36,8 @@ export let optionalNamespaces: ProposalTypes.OptionalNamespaces export let supportedNamespaces: SupportedNamespaces - export let onConfirm: (supportedNamespaces: SupportedNamespaces) => Promise + export let confirmButtonLocaleKey: string + export let onConfirm: (supportedNamespaces: SupportedNamespaces) => Promise let loading = false @@ -85,12 +86,13 @@ async function onConfirmClick(): Promise { try { loading = true - await onConfirm(supportedNamespaces) + const successful = await onConfirm(supportedNamespaces) + if (successful) { + clearSessionInitiationRequest() - clearSessionInitiationRequest() - - drawerRouter.reset() - drawerRouter.goTo(DappConfigRoute.ConnectedDapps) + drawerRouter.reset() + drawerRouter.goTo(DappConfigRoute.ConnectedDapps) + } } catch (error) { loading = false handleError(error) @@ -167,7 +169,7 @@ on:click={onConfirmClick} disabled={loading} busy={loading} - text={localize('actions.confirm')} + text={localize(confirmButtonLocaleKey)} /> {/if} {:else} diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/utils/buildDefaultNamespaces.ts b/packages/shared/src/lib/auxiliary/wallet-connect/utils/buildDefaultNamespaces.ts new file mode 100644 index 0000000000..2101cefc2c --- /dev/null +++ b/packages/shared/src/lib/auxiliary/wallet-connect/utils/buildDefaultNamespaces.ts @@ -0,0 +1,30 @@ +import { getAllNetworkIds, getEvmNetworks } from '@core/network' +import { SupportedNamespaces } from '../types' +import { getCaip10AddressForAccount } from './buildCaip10Address' +import { ALL_SUPPORTED_METHODS, SUPPORTED_EVENTS } from '../constants' +import { get } from 'svelte/store' +import { activeAccounts } from '@core/profile/stores' + +export function buildDefaultNamespaces(): SupportedNamespaces { + const allChainIds = getAllNetworkIds() + + const namespace: SupportedNamespaces = {} + for (const network of getEvmNetworks()) { + if (!allChainIds.includes(network.id)) { + continue + } + if (!namespace[network.namespace]) { + const accounts = get(activeAccounts) + .map((account) => getCaip10AddressForAccount(account, network.id) as string) + .filter(Boolean) + namespace[network.namespace] = { + chains: [], + methods: ALL_SUPPORTED_METHODS, + events: SUPPORTED_EVENTS, + accounts, + } + } + namespace[network.namespace].chains.push(network.id) + } + return namespace +} diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/utils/index.ts b/packages/shared/src/lib/auxiliary/wallet-connect/utils/index.ts index dcbd0a71f7..79dd22b9b9 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/utils/index.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/utils/index.ts @@ -1,4 +1,5 @@ export * from './buildCaip10Address' +export * from './buildDefaultNamespaces' export * from './disconnectAllDapps' export * from './doesNamespaceSupportEvent' export * from './getBloomError' From 3bb90c37ee253569324ecddc3a5fd334b819ddcc Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Tue, 25 Jun 2024 13:48:19 +0200 Subject: [PATCH 18/24] fix locale --- .../dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte | 2 +- .../drawers/dapp-config/views/SessionProposalFlow.svelte | 2 +- packages/shared/src/locales/en.json | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte index cbdc055e42..e0b217c4bb 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte @@ -91,6 +91,6 @@ {requiredNamespaces} {optionalNamespaces} {supportedNamespaces} - confirmButtonLocaleKey="general.signUp" + confirmButtonLocaleKey="actions.signIn" {onConfirm} /> diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte index 2b54d4e4f6..064af5c8a8 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/SessionProposalFlow.svelte @@ -71,6 +71,6 @@ {requiredNamespaces} {optionalNamespaces} {supportedNamespaces} - confirmButtonLocaleKey="general.confirm" + confirmButtonLocaleKey="actions.confirm" {onConfirm} /> diff --git a/packages/shared/src/locales/en.json b/packages/shared/src/locales/en.json index 703af9153c..700b71f7bc 100644 --- a/packages/shared/src/locales/en.json +++ b/packages/shared/src/locales/en.json @@ -1491,6 +1491,7 @@ "testDeepLink": "Test deep link", "delete": "Delete", "remove": "Remove", + "signIn": "Sign in", "getStarted": "Get started", "vote": "Vote", "addProposal": "Add proposal", From 49f18828cb5a39c9314d5fb1b1787851750f46ef Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Tue, 25 Jun 2024 14:19:11 +0200 Subject: [PATCH 19/24] fix types --- .../auxiliary/wallet-connect/actions/approveSession.ts | 3 ++- .../handlers/onSessionAuthenticate.handler.ts | 10 +++------- .../handlers/onSessionProposal.handler.ts | 2 +- .../utils/rejectAndClearSessionInitiationRequest.ts | 4 ++-- 4 files changed, 8 insertions(+), 11 deletions(-) diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSession.ts b/packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSession.ts index b86935dd30..9dfc4e71aa 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSession.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSession.ts @@ -6,6 +6,7 @@ import { ISession } from '../interface/session.interface' import { getWalletClient, setConnectedDapps } from '../stores' import { updateAccountForDappSession } from './updateAccountForDappSession' import { ISupportedNamespace } from '../types' +import { normalizeSessionNamespace } from '../utils' export async function approveSession( sessionProposal: Web3WalletTypes.SessionProposal, @@ -25,7 +26,7 @@ export async function approveSession( namespaces: approvedNamespaces, })) as unknown as ISession setConnectedDapps() - await updateAccountForDappSession(session.topic, session.namespaces, account) + await updateAccountForDappSession(session.topic, normalizeSessionNamespace(session.namespaces), account) } catch (err) { handleError(err) } diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionAuthenticate.handler.ts b/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionAuthenticate.handler.ts index fe2ff72bc0..43b188cbe4 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionAuthenticate.handler.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionAuthenticate.handler.ts @@ -1,15 +1,11 @@ -import { getSdkError } from '@walletconnect/utils' import { Web3WalletTypes } from '@walletconnect/web3wallet' import { get } from 'svelte/store' -import { getWalletClient, sessionInitiationRequest } from '../stores' +import { sessionInitiationRequest } from '../stores' +import { rejectSessionInitiationRequest } from '../utils' export function onSessionAuthenticate(event: Web3WalletTypes.SessionAuthenticate): void { if (get(sessionInitiationRequest)) { - // TODO: should we reject the existing one and set the new one, while resetting the flow for the user? - void getWalletClient()?.rejectSessionAuthenticate({ - id: event.id, - reason: getSdkError('USER_REJECTED'), - }) + void rejectSessionInitiationRequest(event.id, 'session_authenticate') } else { sessionInitiationRequest.set({ type: 'session_authenticate', payload: event }) } diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionProposal.handler.ts b/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionProposal.handler.ts index ba59291c3e..ebc659cbe7 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionProposal.handler.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/handlers/onSessionProposal.handler.ts @@ -5,7 +5,7 @@ import { rejectSessionInitiationRequest } from '../utils' export function onSessionProposal(_sessionProposal: Web3WalletTypes.SessionProposal): void { if (get(sessionInitiationRequest)) { - void rejectSessionInitiationRequest(_sessionProposal.id) + void rejectSessionInitiationRequest(_sessionProposal.id, 'session_proposal') } else { sessionInitiationRequest.set({ type: 'session_proposal', payload: _sessionProposal }) } diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/utils/rejectAndClearSessionInitiationRequest.ts b/packages/shared/src/lib/auxiliary/wallet-connect/utils/rejectAndClearSessionInitiationRequest.ts index fee1d92df8..965cab1096 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/utils/rejectAndClearSessionInitiationRequest.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/utils/rejectAndClearSessionInitiationRequest.ts @@ -9,11 +9,11 @@ export async function rejectAndClearSessionInitiationRequest(): Promise { return } - const { id, params } = _sessionInitiationRequest + const { id, params } = _sessionInitiationRequest.payload const isExpired = (params.expiryTimestamp - Date.now()) / MILLISECONDS_PER_SECOND <= 0 if (!isExpired) { - await rejectSessionInitiationRequest(id) + await rejectSessionInitiationRequest(id, _sessionInitiationRequest.type) } clearSessionInitiationRequest() } From f3ccc00b7eb52d9f7b61ea98d56cdbac899507c1 Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Tue, 25 Jun 2024 16:37:10 +0200 Subject: [PATCH 20/24] fix authenticate approval --- .../drawers/dapp-config/views/OneClickAuthFlow.svelte | 6 +++++- .../wallet-connect/actions/approveSessionAuthenticate.ts | 8 +++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte index e0b217c4bb..5df0b6a7ed 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/OneClickAuthFlow.svelte @@ -57,6 +57,7 @@ return defaultNamespaces } + async function onConfirm(supportedNamespaces: SupportedNamespaces): Promise { try { await checkActiveProfileAuth(LedgerAppName.Ethereum) @@ -67,7 +68,10 @@ const checkedAccounts = $activeAccounts.filter((account) => supportedNamespaces.eip155.accounts.some((caipAddress) => { const [namespace, chainId, address] = caipAddress.split(':') - return getAddressFromAccountForNetwork(account, `${namespace}:${chainId}` as EvmNetworkId) === address + return ( + getAddressFromAccountForNetwork(account, `${namespace}:${chainId}` as EvmNetworkId) === + address.toLowerCase() + ) }) ) const checkedNetworks = supportedNamespaces.eip155.chains diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSessionAuthenticate.ts b/packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSessionAuthenticate.ts index d8339e05ee..f5d382b67d 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSessionAuthenticate.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSessionAuthenticate.ts @@ -1,4 +1,3 @@ -import { getAddressFromAccountForNetwork } from '@core/account' import { clearSessionInitiationRequest, getWalletClient, persistDapp } from '../stores' import { clearOldPairings } from './clearOldPairings' import { Web3WalletTypes } from '@walletconnect/web3wallet' @@ -9,7 +8,7 @@ import { NetworkId, getEvmNetwork } from '@core/network' import { AuthTypes } from '@walletconnect/types' import { ISelections } from '../interface' import { signMessage } from '@core/wallet' -import { normalizeSessionNamespace } from '../utils' +import { getCaip10AddressForAccount, normalizeSessionNamespace } from '../utils' export async function approveSessionAuthenticate( sessionProposal: Web3WalletTypes.SessionAuthenticate, @@ -36,7 +35,10 @@ export async function approveSessionAuthenticate( } for (const account of selections.accounts ?? []) { - const address = `${chain}:${getAddressFromAccountForNetwork(account, network.id)}` + const address = getCaip10AddressForAccount(account, network.id) + if (!address) { + continue + } const message = walletClient.formatAuthMessage({ request: authPayload, From 14842f107d85612247714404fd6bcd2841c7e06b Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Tue, 25 Jun 2024 17:29:47 +0200 Subject: [PATCH 21/24] cleanup session --- .../actions/approveSessionAuthenticate.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSessionAuthenticate.ts b/packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSessionAuthenticate.ts index f5d382b67d..bbd364d293 100644 --- a/packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSessionAuthenticate.ts +++ b/packages/shared/src/lib/auxiliary/wallet-connect/actions/approveSessionAuthenticate.ts @@ -5,10 +5,11 @@ import { DappVerification } from '../enums' import { GENERAL_SUPPORTED_METHODS } from '../constants' import { populateAuthPayload, buildAuthObject } from '@walletconnect/utils' import { NetworkId, getEvmNetwork } from '@core/network' -import { AuthTypes } from '@walletconnect/types' +import { AuthTypes, SessionTypes } from '@walletconnect/types' import { ISelections } from '../interface' import { signMessage } from '@core/wallet' import { getCaip10AddressForAccount, normalizeSessionNamespace } from '../utils' +import { SupportedNamespaces } from '../types' export async function approveSessionAuthenticate( sessionProposal: Web3WalletTypes.SessionAuthenticate, @@ -66,9 +67,19 @@ export async function approveSessionAuthenticate( const dappUrl = sessionProposal.params.requester.metadata.url await clearOldPairings(dappUrl) persistDapp(dappUrl, DappVerification.Unknown, { - supported: normalizeSessionNamespace(session.namespaces), + supported: cleanupNamespaces(session.namespaces), required: session.requiredNamespaces, optional: session.optionalNamespaces, }) clearSessionInitiationRequest() } + +// This is needed because the returned namespaces from the wallet connect client can contain duplicated chains +function cleanupNamespaces(namespaces: SessionTypes.Namespaces): SupportedNamespaces { + const normalizedNamespaces = normalizeSessionNamespace(namespaces) + for (const key in normalizedNamespaces) { + const namespace = normalizedNamespaces[key] + namespace.chains = [...new Set(namespace.chains)] + } + return normalizedNamespaces +} From da99aae8bc668979cadc4a33b2705a16064a9470 Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Wed, 26 Jun 2024 09:23:16 +0200 Subject: [PATCH 22/24] show different description --- .../views/ConnectionRequest.svelte | 12 ++------- .../views/ConnectionRequestDrawer.svelte | 26 ++++++++++++++++--- .../dapp-config/views/OneClickAuthFlow.svelte | 16 ++++++++++-- .../views/SessionProposalFlow.svelte | 17 ++++++++++-- .../views/SessionProposalFlowShared.svelte | 12 ++------- 5 files changed, 56 insertions(+), 27 deletions(-) diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequest.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequest.svelte index c8f4de054f..7b947845f3 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequest.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequest.svelte @@ -1,5 +1,5 @@