Skip to content

Commit

Permalink
guard each entry point into swaps to prevent readonly wallets from ac…
Browse files Browse the repository at this point in the history
…cessing it
  • Loading branch information
walmat committed Jun 11, 2024
1 parent 0619c71 commit 663c6b0
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,6 @@ function SwapButton() {
analytics.track('Tapped "Swap"', {
category: 'home screen',
});

android && delayNext();
if (swapsV2Enabled) {
swapsStore.setState({
inputAsset: userAssetsStore.getState().getHighestValueAsset(),
Expand All @@ -191,6 +189,8 @@ function SwapButton() {
return;
}

android && delayNext();

const mainnetEth = await ethereumUtils.getNativeAssetForNetwork(Network.mainnet, accountAddress);
navigate(Routes.EXCHANGE_MODAL, {
fromDiscover: true,
Expand Down
16 changes: 9 additions & 7 deletions src/components/expanded-state/AvailableNetworksv2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ 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';
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';
Expand All @@ -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],
Expand All @@ -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
Expand Down Expand Up @@ -179,16 +185,13 @@ const AvailableNetworksv2 = ({
<>
<MenuWrapper
// @ts-ignore overloaded props

menuConfig={{ menuItems: networkMenuItems, menuTitle: '' }}
isMenuPrimaryAction
onPressMenuItem={handlePressContextMenu}
useActionSheetFallback={false}
>
<Box
as={ButtonPressAnimation}
// @ts-ignore overloaded props

scaleTo={0.96}
onPress={availableNetworks.length === 1 ? handlePressButton : NOOP}
marginHorizontal={{ custom: marginHorizontal }}
Expand All @@ -198,7 +201,6 @@ const AvailableNetworksv2 = ({
<RadialGradient
{...radialGradientProps}
// @ts-ignore overloaded props

borderRadius={99}
radius={600}
/>
Expand Down
14 changes: 10 additions & 4 deletions src/components/sheet/sheet-action-buttons/SwapActionButton.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -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 (
<SheetActionButton
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useExpandedStateNavigation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useRoute } from '@react-navigation/native';
import { useCallback, useMemo } from 'react';
import { InteractionManager } from 'react-native';

Expand Down
3 changes: 1 addition & 2 deletions src/screens/discover/components/DiscoverHome.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ import walletTypes from '@/helpers/walletTypes';
import { NFTOffersCard } from '@/components/cards/NFTOffersCard';
import { MintsCard } from '@/components/cards/MintsCard/MintsCard';
import { FeaturedMintCard } from '@/components/cards/FeaturedMintCard';
import { useMints } from '@/resources/mints';
import { IS_TEST } from '@/env';
import { RemoteCardCarousel, useRemoteCardContext } from '@/components/cards/remote-cards';
import { useRoute } from '@react-navigation/native';

export default function DiscoverHome() {
const { profiles_enabled, mints_enabled, op_rewards_enabled } = useRemoteConfig();
const { accountAddress, network } = useAccountSettings();
const { network } = useAccountSettings();
const { getCardsForPlacement } = useRemoteCardContext();
const { name } = useRoute();
const profilesEnabledLocalFlag = useExperimentalFlag(PROFILES);
Expand Down
4 changes: 1 addition & 3 deletions src/screens/discover/components/DiscoverSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { uniqBy } from 'lodash';
import React, { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
import { InteractionManager, View } from 'react-native';
import { IS_TESTING } from 'react-native-dotenv';
import { useDispatch } from 'react-redux';
import { useDebounce } from 'use-debounce';
import CurrencySelectionTypes from '@/helpers/currencySelectionTypes';

Expand Down Expand Up @@ -31,7 +30,6 @@ export const SearchContainer = styled(Row)({

export default function DiscoverSearch() {
const { navigate } = useNavigation();
const dispatch = useDispatch();
const { accountAddress } = useAccountSettings();
const {
isSearching,
Expand Down Expand Up @@ -176,7 +174,7 @@ export default function DiscoverSearch() {
});
}
},
[dispatch, navigate, profilesEnabled, searchInputRef, setIsSearchModeEnabled]
[navigate, profilesEnabled, searchInputRef, setIsSearchModeEnabled]
);

const itemProps = useMemo(
Expand Down

0 comments on commit 663c6b0

Please sign in to comment.