From 5b88aa7dea6ff0b9369898c5a7994922ceaa012d Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Mon, 27 May 2024 12:32:59 +0200 Subject: [PATCH] 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'