From 663c6b0e53d932a6df65cb4d00cce287290073c2 Mon Sep 17 00:00:00 2001 From: Matthew Wall Date: Tue, 11 Jun 2024 15:19:28 -0400 Subject: [PATCH] guard each entry point into swaps to prevent readonly wallets from accessing it --- .../profile-header/ProfileActionButtonsRow.tsx | 4 ++-- .../expanded-state/AvailableNetworksv2.tsx | 16 +++++++++------- .../sheet-action-buttons/SwapActionButton.tsx | 14 ++++++++++---- src/hooks/useExpandedStateNavigation.ts | 1 - src/screens/discover/components/DiscoverHome.js | 3 +-- .../discover/components/DiscoverSearch.js | 4 +--- 6 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/components/asset-list/RecyclerAssetList2/profile-header/ProfileActionButtonsRow.tsx b/src/components/asset-list/RecyclerAssetList2/profile-header/ProfileActionButtonsRow.tsx index b1ba6e60a02..e2eb7f8ded0 100644 --- a/src/components/asset-list/RecyclerAssetList2/profile-header/ProfileActionButtonsRow.tsx +++ b/src/components/asset-list/RecyclerAssetList2/profile-header/ProfileActionButtonsRow.tsx @@ -179,8 +179,6 @@ function SwapButton() { analytics.track('Tapped "Swap"', { category: 'home screen', }); - - android && delayNext(); if (swapsV2Enabled) { swapsStore.setState({ inputAsset: userAssetsStore.getState().getHighestValueAsset(), @@ -191,6 +189,8 @@ function SwapButton() { return; } + android && delayNext(); + const mainnetEth = await ethereumUtils.getNativeAssetForNetwork(Network.mainnet, accountAddress); navigate(Routes.EXCHANGE_MODAL, { fromDiscover: true, diff --git a/src/components/expanded-state/AvailableNetworksv2.tsx b/src/components/expanded-state/AvailableNetworksv2.tsx index bc1967a0d96..f4fcfa4952e 100644 --- a/src/components/expanded-state/AvailableNetworksv2.tsx +++ b/src/components/expanded-state/AvailableNetworksv2.tsx @@ -7,9 +7,9 @@ import { Box, Inline, Text } from '@/design-system'; import { useNavigation } from '@/navigation'; import Routes from '@/navigation/routesNames'; import { position } from '@/styles'; -import { ethereumUtils } from '@/utils'; +import { ethereumUtils, watchingAlert } from '@/utils'; import { CurrencySelectionTypes, ExchangeModalTypes, Network } from '@/helpers'; -import { useSwapCurrencyHandlers } from '@/hooks'; +import { useSwapCurrencyHandlers, useWallets } from '@/hooks'; import { RainbowToken } from '@/entities'; import { useTheme } from '@/theme'; import { ButtonPressAnimation } from '../animations'; @@ -17,7 +17,7 @@ import ContextMenuButton from '@/components/native-context-menu/contextMenu'; import { implementation } from '@/entities/dispersion'; import { RainbowNetworks, getNetworkObj } from '@/networks'; import { EthCoinIcon } from '../coin-icon/EthCoinIcon'; -import { SWAPS_V2, useExperimentalFlag } from '@/config'; +import { SWAPS_V2, enableActionsOnReadOnlyWallet, useExperimentalFlag } from '@/config'; import { useRemoteConfig } from '@/model/remoteConfig'; import { userAssetsStore } from '@/state/assets/userAssets'; import { parseSearchAsset } from '@/__swaps__/utils/assets'; @@ -43,6 +43,7 @@ const AvailableNetworksv2 = ({ const { goBack, navigate } = useNavigation(); const { swaps_v2 } = useRemoteConfig(); const swapsV2Enabled = useExperimentalFlag(SWAPS_V2); + const { isReadOnlyWallet } = useWallets(); const radialGradientProps = { center: [0, 1], @@ -67,6 +68,11 @@ const AvailableNetworksv2 = ({ }); const convertAssetAndNavigate = useCallback( (chosenNetwork: Network) => { + if (isReadOnlyWallet && !enableActionsOnReadOnlyWallet) { + watchingAlert(); + return; + } + const newAsset = asset; // we need to convert the mainnet asset to the selected network's @@ -179,7 +185,6 @@ const AvailableNetworksv2 = ({ <> diff --git a/src/components/sheet/sheet-action-buttons/SwapActionButton.tsx b/src/components/sheet/sheet-action-buttons/SwapActionButton.tsx index 8e5123b3394..b32726e14f9 100644 --- a/src/components/sheet/sheet-action-buttons/SwapActionButton.tsx +++ b/src/components/sheet/sheet-action-buttons/SwapActionButton.tsx @@ -1,14 +1,14 @@ import lang from 'i18n-js'; import React, { useCallback } from 'react'; import SheetActionButton from './SheetActionButton'; -import { useExpandedStateNavigation } from '@/hooks'; +import { useExpandedStateNavigation, useWallets } from '@/hooks'; import Routes from '@/navigation/routesNames'; import { useTheme } from '@/theme'; import { RainbowToken } from '@/entities'; import { useRemoteConfig } from '@/model/remoteConfig'; import { useNavigation } from '@/navigation'; -import { SWAPS_V2, useExperimentalFlag } from '@/config'; -import { ethereumUtils } from '@/utils'; +import { SWAPS_V2, useExperimentalFlag, enableActionsOnReadOnlyWallet } from '@/config'; +import { ethereumUtils, watchingAlert } from '@/utils'; import { userAssetsStore } from '@/state/assets/userAssets'; import { parseSearchAsset } from '@/__swaps__/utils/assets'; import { chainNameFromChainId } from '@/__swaps__/utils/chains'; @@ -31,12 +31,18 @@ function SwapActionButton({ asset, color: givenColor, inputType, label, fromDisc const { swaps_v2 } = useRemoteConfig(); const { navigate } = useNavigation(); const swapsV2Enabled = useExperimentalFlag(SWAPS_V2); + const { isReadOnlyWallet } = useWallets(); const color = givenColor || colors.swapPurple; const old_navigate = useExpandedStateNavigation(inputType, fromDiscover, asset); const goToSwap = useCallback(() => { if (swapsV2Enabled || swaps_v2) { + if (isReadOnlyWallet && !enableActionsOnReadOnlyWallet) { + watchingAlert(); + return; + } + const chainId = ethereumUtils.getChainIdFromNetwork(asset.network); const uniqueId = `${asset.address}_${chainId}`; const userAsset = userAssetsStore.getState().userAssets.get(uniqueId); @@ -110,7 +116,7 @@ function SwapActionButton({ asset, color: givenColor, inputType, label, fromDisc }; } }); - }, [asset, inputType, navigate, old_navigate, swapsV2Enabled, swaps_v2]); + }, [asset, inputType, isReadOnlyWallet, navigate, old_navigate, swapsV2Enabled, swaps_v2]); return (