From 6d65fb089d263ef4e49d334ead19e7f7a457eeda Mon Sep 17 00:00:00 2001 From: Mark Nardi Date: Tue, 16 Jan 2024 18:58:56 +0100 Subject: [PATCH] fix: permissions are reset if dapp doesnt require any methods (#1705) * show different warning if dapp has supported networks on other profile * only show relevant permissions --------- Co-authored-by: Nicole O'Brien --- .../components/ConnectionSummary.svelte | 73 +++++++++++-------- .../components/PermissionSelection.svelte | 60 ++++++++++----- .../views/ConfirmConnectionDrawer.svelte | 11 ++- .../views/ConnectionRequestDrawer.svelte | 59 ++++++++++----- .../views/EditPermissionsDrawer.svelte | 3 +- .../utils/getPermissionForMethod.ts | 14 ++++ .../auxiliary/wallet-connect/utils/index.ts | 1 + packages/shared/src/locales/en.json | 3 +- 8 files changed, 152 insertions(+), 72 deletions(-) create mode 100644 packages/shared/src/lib/auxiliary/wallet-connect/utils/getPermissionForMethod.ts diff --git a/packages/desktop/views/dashboard/drawers/dapp-config/components/ConnectionSummary.svelte b/packages/desktop/views/dashboard/drawers/dapp-config/components/ConnectionSummary.svelte index c6891544be..2410b734ab 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/components/ConnectionSummary.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/components/ConnectionSummary.svelte @@ -29,23 +29,27 @@ const namespaces = Object.values(requiredNamespaces ?? {}) const _persistedNamespaces = Object.values(persistedNamespaces) - return Object.values(DappPermission).map((permission) => { - const supportedMethods = METHODS_FOR_PERMISSION[permission] ?? [] - - const isRequired = namespaces.some((namespace) => - supportedMethods.some((method) => namespace.methods.includes(method)) - ) - - const isEnabled = _persistedNamespaces.some((namespace) => - supportedMethods.some((method) => namespace.methods.includes(method)) - ) - - return { - label: localize(`views.dashboard.drawers.dapps.confirmConnection.permissions.${String(permission)}`), - enabled: isEnabled, - required: isRequired, - } - }) + return Object.values(DappPermission) + .map((permission) => { + const supportedMethods = METHODS_FOR_PERMISSION[permission] ?? [] + + const isRequired = namespaces.some((namespace) => + supportedMethods.some((method) => namespace.methods.includes(method)) + ) + + const isEnabled = _persistedNamespaces.some((namespace) => + supportedMethods.some((method) => namespace.methods.includes(method)) + ) + + return { + label: localize( + `views.dashboard.drawers.dapps.confirmConnection.permissions.${String(permission)}` + ), + enabled: isEnabled, + required: isRequired, + } + }) + .filter((permission) => permission.enabled) } function getNetworkPreferences(): string[] { @@ -89,21 +93,26 @@ -
- {localize(`${localeKey}.permissions.step`)} - {#if editable} - - {/if} -
- - {#each permissionPreferences as permission} - - - {localize(`general.${permission.required ? 'required' : permission.enabled ? 'yes' : 'no'}`)} - - - {/each} -
+ {#if permissionPreferences.length} +
+ {localize(`${localeKey}.permissions.step`)} + {#if editable} + + {/if} +
+ + {#each permissionPreferences as permission} + + + {localize(`general.${permission.required ? 'required' : permission.enabled ? 'yes' : 'no'}`)} + + + {/each} +
+ {/if}
{localize(`${localeKey}.networks.step`)} 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 b416e2bf63..f1060f250c 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/components/PermissionSelection.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/components/PermissionSelection.svelte @@ -1,41 +1,57 @@ - +{#if permissionSelections.length} + +{:else} + + {localize(`${localeKey}.title`)} + +
+ {localize(`${localeKey}.noPermissionsRequired`)} +
+
+{/if} 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 55214bd557..47f4792214 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/ConfirmConnectionDrawer.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConfirmConnectionDrawer.svelte @@ -35,6 +35,7 @@ localize(`${localeKey}.accounts.step`), ] + let permissionSelections: { label: string; value: string; checked: boolean; required: boolean }[] = [] let checkedAccounts: IAccountState[] = [] let checkedNetworks: string[] = [] let checkedMethods: string[] = [] @@ -44,7 +45,7 @@ $: isButtonDisabled = loading || - (!persistedNamespaces && currentStep === 0 && checkedMethods.length === 0) || + (!persistedNamespaces && currentStep === 0 && permissionSelections.length && checkedMethods.length === 0) || (currentStep === 1 && checkedNetworks.length === 0) || (currentStep === 2 && checkedAccounts.length === 0) @@ -122,20 +123,22 @@ body={localize(`${localeKey}.${tipLocale}.tip`)} dismissable={false} /> -
+
-
+
-
+
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 b07eca09d5..8f65e76d42 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequestDrawer.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/ConnectionRequestDrawer.svelte @@ -29,9 +29,10 @@ $: unsupportedMethods = getUnsupportedMethods($sessionProposal) $: supportedNetworks = getSupportedNetworks($sessionProposal) $: unsupportedRequiredNetworks = getUnsupportedRequiredNetworks($sessionProposal) - $: isSupportedOnOtherProfiles = areNetworksSupportedOnOtherProfiles(unsupportedRequiredNetworks) $: fulfillsRequirements = - unsupportedMethods.length === 0 && unsupportedRequiredNetworks.length === 0 && supportedNetworks.length > 0 + unsupportedMethods.length === 0 && + unsupportedRequiredNetworks.networks.length === 0 && + supportedNetworks.networks.length > 0 let timeout: ReturnType | undefined $: { @@ -48,17 +49,29 @@ } } - function getUnsupportedRequiredNetworks(_sessionProposal: Web3WalletTypes.SessionProposal | undefined): string[] { - if (!_sessionProposal) return [] + function getUnsupportedRequiredNetworks(_sessionProposal: Web3WalletTypes.SessionProposal | undefined): { + networks: string[] + isSupportedOnOtherProfiles: boolean + } { + if (!_sessionProposal) return { networks: [], isSupportedOnOtherProfiles: false } const requiredNamespaces = _sessionProposal?.params.requiredNamespaces const networksSupportedByProfile = getAllNetworkIds() const requiredNetworks = Object.values(requiredNamespaces).flatMap((namespace) => namespace.chains) - return requiredNetworks.filter((network) => !networksSupportedByProfile.includes(network)) + + const networks = requiredNetworks.filter((network) => !networksSupportedByProfile.includes(network)) + + const allSupportedNetworks: string[] = Object.values(SupportedNetworkId) + const isSupportedOnOtherProfiles = networks.every((network) => allSupportedNetworks.includes(network)) + + return { networks, isSupportedOnOtherProfiles } } - function getSupportedNetworks(_sessionProposal: Web3WalletTypes.SessionProposal | undefined): string[] { - if (!_sessionProposal) return [] + function getSupportedNetworks(_sessionProposal: Web3WalletTypes.SessionProposal | undefined): { + networks: string[] + hasSupportedOnOtherProfiles: boolean + } { + if (!_sessionProposal) return { networks: [], hasSupportedOnOtherProfiles: false } const requiredNamespaces = _sessionProposal?.params.requiredNamespaces const optionalNamespaces = _sessionProposal?.params.optionalNamespaces @@ -68,13 +81,14 @@ networksSupportedByDapp.push(...Object.values(requiredNamespaces).flatMap((namespace) => namespace.chains)) networksSupportedByDapp.push(...Object.values(optionalNamespaces).flatMap((namespace) => namespace.chains)) + const networks = networksSupportedByDapp.filter((network) => networksSupportedByProfile.includes(network)) - return networksSupportedByDapp.filter((network) => networksSupportedByProfile.includes(network)) - } - - function areNetworksSupportedOnOtherProfiles(_unsupportedRequiredNetworks: string[]): boolean { const allSupportedNetworks: string[] = Object.values(SupportedNetworkId) - return _unsupportedRequiredNetworks.every((network) => allSupportedNetworks.includes(network)) + const hasSupportedOnOtherProfiles = networksSupportedByDapp.some((network) => + allSupportedNetworks.includes(network) + ) + + return { networks, hasSupportedOnOtherProfiles } } function getUnsupportedMethods(_sessionProposal: Web3WalletTypes.SessionProposal | undefined): string[] { @@ -131,20 +145,31 @@
- {#if unsupportedRequiredNetworks.length} + {#if unsupportedRequiredNetworks.networks.length}
- {:else if supportedNetworks.length === 0} + {:else if supportedNetworks.networks.length === 0}
- +
{:else if unsupportedMethods.length}
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 e02d1a1411..88a51c47de 100644 --- a/packages/desktop/views/dashboard/drawers/dapp-config/views/EditPermissionsDrawer.svelte +++ b/packages/desktop/views/dashboard/drawers/dapp-config/views/EditPermissionsDrawer.svelte @@ -13,7 +13,8 @@ selections={{ methods: checkedMethods }} let:persistedNamespaces let:requiredNamespaces + let:optionalNamespaces disableContinue={checkedMethods.length === 0} > - + diff --git a/packages/shared/src/lib/auxiliary/wallet-connect/utils/getPermissionForMethod.ts b/packages/shared/src/lib/auxiliary/wallet-connect/utils/getPermissionForMethod.ts new file mode 100644 index 0000000000..feaefedc07 --- /dev/null +++ b/packages/shared/src/lib/auxiliary/wallet-connect/utils/getPermissionForMethod.ts @@ -0,0 +1,14 @@ +import { METHODS_FOR_PERMISSION } from '../constants' +import { DappPermission } from '../enums' + +export function getPermissionForMethod(method: string): DappPermission | undefined { + for (const permission of Object.values(DappPermission)) { + const supportedMethods = METHODS_FOR_PERMISSION[permission] ?? [] + + if (supportedMethods.includes(method)) { + return permission + } + } + + return undefined +} 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 47bc33fb0f..065fb6d11f 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 './approveSession' export * from './disconnectAllDapps' export * from './getAllEvmAddresses' +export * from './getPermissionForMethod' export * from './rejectSession' export * from './switchToRequiredAccount' export * from './updateSession' diff --git a/packages/shared/src/locales/en.json b/packages/shared/src/locales/en.json index 37ca7cba20..2d4bd1b0a7 100644 --- a/packages/shared/src/locales/en.json +++ b/packages/shared/src/locales/en.json @@ -510,7 +510,8 @@ "signData": "Request to sign data", "signTransaction": "Request to sign a transaction", "sendTransaction": "Request to send a transaction", - "empty": "You have to select at least one permission" + "empty": "You have to select at least one permission", + "noPermissionsRequired": "No permissions required to use this app" }, "networks": { "step": "Networks",