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'