Skip to content

Commit d0f3047

Browse files
committed
chore: fix toChecksumAddress validation
1 parent 05bc122 commit d0f3047

File tree

18 files changed

+87
-50
lines changed

18 files changed

+87
-50
lines changed

app/component-library/components-temp/Price/AggregatedPercentage/AggregatedPercentageCrossChains.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ jest.mock('ethereumjs-util', () => ({
2626
zeroAddress: jest.fn(() => '0x0000000000000000000000000000000000000000'),
2727
}));
2828

29-
jest.mock('@metamask/utils', () => ({
30-
getChecksumAddress: jest.fn((address) => address),
29+
jest.mock('../../../../util/address', () => ({
30+
toChecksumAddress: jest.fn((address) => address),
3131
}));
3232

3333
describe('AggregatedPercentageCrossChains', () => {

app/component-library/components-temp/Price/AggregatedPercentage/AggregatedPercentageCrossChains.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ import {
1111
FORMATTED_PERCENTAGE_TEST_ID,
1212
} from './AggregatedPercentage.constants';
1313
import { zeroAddress } from 'ethereumjs-util';
14-
import { getChecksumAddress, Hex } from '@metamask/utils';
1514
import { selectTokenMarketData } from '../../../../selectors/tokenRatesController';
1615
import {
1716
MarketDataMapping,
1817
TokensWithBalances,
1918
} from '../../../../components/hooks/useGetFormattedTokensPerChain';
2019
import { getFormattedAmountChange, getPercentageTextColor } from './utils';
2120
import { AggregatedPercentageCrossChainsProps } from './AggregatedPercentageCrossChains.types';
21+
import { toChecksumAddress } from '../../../../util/address';
22+
2223
export const getCalculatedTokenAmount1dAgo = (
2324
tokenFiatBalance: number,
2425
tokenPricePercentChange1dAgo: number,
@@ -48,9 +49,7 @@ const AggregatedPercentageCrossChains = ({
4849
const totalPerChain1dAgoERC20 = tokensWithBalances.reduce(
4950
(total1dAgo: number, item: { address: string }, idx: number) => {
5051
const found =
51-
crossChainMarketData?.[chainId]?.[
52-
getChecksumAddress(item.address as Hex)
53-
];
52+
crossChainMarketData?.[chainId]?.[toChecksumAddress(item.address)];
5453

5554
const tokenFiat1dAgo = getCalculatedTokenAmount1dAgo(
5655
tokenFiatBalances[idx],

app/components/UI/AccountFromToInfoCard/AddressFrom.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { getChecksumAddress, Hex } from '@metamask/utils';
21
import React, { useEffect, useState } from 'react';
32
import { View } from 'react-native';
43
import { useSelector } from 'react-redux';
@@ -16,6 +15,7 @@ import {
1615
import {
1716
getLabelTextByAddress,
1817
renderAccountName,
18+
toChecksumAddress,
1919
} from '../../../util/address';
2020
import useAddressBalance from '../../hooks/useAddressBalance/useAddressBalance';
2121
import stylesheet from './AddressFrom.styles';
@@ -62,7 +62,7 @@ const AddressFrom = ({
6262
const accountsByChainId = useSelector(selectAccountsByChainId);
6363

6464
const internalAccounts = useSelector(selectInternalEvmAccounts);
65-
const activeAddress = getChecksumAddress(from as Hex);
65+
const activeAddress = toChecksumAddress(from);
6666

6767
const networkName = useSelector(selectEvmNetworkName);
6868
const networkImage = useSelector(selectEvmNetworkImageSource);

app/components/UI/Bridge/hooks/useTokensWithBalance/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useMemo } from 'react';
22
import { useSelector } from 'react-redux';
3-
import { CaipChainId, getChecksumAddress, Hex } from '@metamask/utils';
3+
import { CaipChainId, Hex } from '@metamask/utils';
44
import { TokenI } from '../../../Tokens/types';
55
import { selectTokensBalances } from '../../../../../selectors/tokenBalancesController';
66
import {
@@ -30,6 +30,7 @@ import { renderNumber, renderFiat } from '../../../../../util/number';
3030
import { formatUnits } from 'ethers/lib/utils';
3131
import { BigNumber } from 'ethers';
3232
import { selectAccountsByChainId } from '../../../../../selectors/accountTrackerController';
33+
import { toChecksumAddress } from '../../../../../util/address';
3334

3435
interface CalculateFiatBalancesParams {
3536
assets: TokenI[];
@@ -90,7 +91,7 @@ export const calculateEvmBalances = ({
9091
// Native EVM token
9192
if (token.isETH || token.isNative) {
9293
const nativeTokenBalanceAtomicHex =
93-
evmAccountsByChainId?.[chainId]?.[getChecksumAddress(selectedAddress)]
94+
evmAccountsByChainId?.[chainId]?.[toChecksumAddress(selectedAddress)]
9495
?.balance || '0x0';
9596
const nativeTokenBalance = formatUnits(
9697
BigNumber.from(nativeTokenBalanceAtomicHex),
@@ -116,7 +117,7 @@ export const calculateEvmBalances = ({
116117
);
117118
const erc20BalanceAtomicHex =
118119
multiChainTokenBalances?.[token.address as Hex] ||
119-
multiChainTokenBalances?.[getChecksumAddress(token.address as Hex)] ||
120+
multiChainTokenBalances?.[toChecksumAddress(token.address as Hex)] ||
120121
'0x0';
121122

122123
const erc20Balance = formatUnits(

app/components/UI/PaymentRequest/AssetList/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { fontStyles } from '../../../../styles/common';
66
import Identicon from '../../Identicon';
77
import NetworkMainAssetLogo from '../../NetworkMainAssetLogo';
88
import { useSelector } from 'react-redux';
9-
import { getChecksumAddress } from '@metamask/utils';
109
import { useTheme } from '../../../../util/theme';
1110
import { selectTokenList } from '../../../../selectors/tokenListController';
1211
import { ImportTokenViewSelectorsIDs } from '../../../../../e2e/selectors/wallet/ImportTokenView.selectors';
1312
import { FlashList } from '@shopify/flash-list';
13+
import { toChecksumAddress } from '../../../../util/address';
1414

1515
// TODO: Replace "any" with type
1616
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -104,7 +104,7 @@ const AssetList = ({
104104
return <NetworkMainAssetLogo big style={styles.ethLogo} />;
105105
}
106106
const token =
107-
tokenList?.[getChecksumAddress(address)] ||
107+
tokenList?.[toChecksumAddress(address)] ||
108108
tokenList?.[address.toLowerCase()];
109109
const iconUrl = token?.iconUrl;
110110
if (!iconUrl) {

app/components/UI/TransactionElement/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
renderFullAddress,
1717
areAddressesEqual,
1818
toFormattedAddress,
19+
toChecksumAddress,
1920
} from '../../../util/address';
2021
import {
2122
decodeTransferData,
@@ -24,7 +25,6 @@ import {
2425
getActionKey,
2526
TRANSACTION_TYPES,
2627
} from '../../../util/transactions';
27-
import { getChecksumAddress } from '@metamask/utils';
2828
import { swapsUtils } from '@metamask/swaps-controller';
2929
import { isSwapsNativeAsset } from '../Swaps/utils';
3030
import Engine from '../../../core/Engine';
@@ -242,7 +242,7 @@ export function decodeIncomingTransfer(args) {
242242
: undefined;
243243
const exchangeRate =
244244
token && contractExchangeRates
245-
? contractExchangeRates[getChecksumAddress(token.address)]?.price
245+
? contractExchangeRates[toChecksumAddress(token.address)]?.price
246246
: undefined;
247247

248248
let renderTokenFiatAmount, renderTokenFiatNumber;

app/components/Views/Settings/Contacts/ContactForm/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import PropTypes from 'prop-types';
1313
import { getEditableOptions } from '../../../../UI/Navbar';
1414
import StyledButton from '../../../../UI/StyledButton';
1515
import Engine from '../../../../../core/Engine';
16-
import { getChecksumAddress } from '@metamask/utils';
16+
import { toChecksumAddress } from '@metamask/utils';
1717
import { connect } from 'react-redux';
1818
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
1919
import { strings } from '../../../../../../locales/i18n';
@@ -335,12 +335,12 @@ class ContactForm extends PureComponent {
335335
if (wasChainIdChanged) {
336336
AddressBookController.delete(
337337
originalContactChainId,
338-
getChecksumAddress(address),
338+
toChecksumAddress(address),
339339
);
340340
}
341341

342342
AddressBookController.set(
343-
getChecksumAddress(toEnsAddress || address),
343+
toChecksumAddress(toEnsAddress || address),
344344
name,
345345
contactChainId || chainId,
346346
memo,

app/components/Views/confirmations/hooks/useAccountInfo.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { useMemo } from 'react';
22
import { useSelector } from 'react-redux';
3-
import { Hex, getChecksumAddress } from '@metamask/utils';
3+
import { Hex } from '@metamask/utils';
44

55
import Engine from '../../../../core/Engine';
66
import useAddressBalance from '../../../../components/hooks/useAddressBalance/useAddressBalance';
77
import { selectInternalAccounts } from '../../../../selectors/accountsController';
8-
import { renderAccountName } from '../../../../util/address';
8+
import { renderAccountName, toChecksumAddress } from '../../../../util/address';
99
import { selectCurrentCurrency } from '../../../../selectors/currencyRateController';
1010
import { formatWithThreshold } from '../../../../util/assets';
1111
import I18n from '../../../../../locales/i18n';
1212

1313
const useAccountInfo = (address: string, chainId: Hex) => {
1414
const internalAccounts = useSelector(selectInternalAccounts);
15-
const activeAddress = getChecksumAddress(address as Hex);
15+
const activeAddress = toChecksumAddress(address as Hex);
1616
const { addressBalance: accountBalance } = useAddressBalance(
1717
undefined,
1818
address,

app/components/Views/confirmations/legacy/Send/index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
fromWei,
1818
fromTokenMinimalUnit,
1919
} from '../../../../../util/number';
20-
import { getChecksumAddress } from '@metamask/utils';
2120
import { strings } from '../../../../../../locales/i18n';
2221
import { getTransactionOptionsTitle } from '../../../../UI/Navbar';
2322
import { connect } from 'react-redux';
@@ -36,7 +35,11 @@ import {
3635
generateTransferData,
3736
} from '../../../../../util/transactions';
3837
import Logger from '../../../../../util/Logger';
39-
import { getAddress, areAddressesEqual } from '../../../../../util/address';
38+
import {
39+
getAddress,
40+
areAddressesEqual,
41+
toChecksumAddress,
42+
} from '../../../../../util/address';
4043
import { MAINNET } from '../../../../../constants/network';
4144
import BigNumber from 'bignumber.js';
4245
import { WalletDevice } from '@metamask/transaction-controller';
@@ -443,7 +446,7 @@ class Send extends PureComponent {
443446
*/
444447
handleTokenDeeplink = async (address) => {
445448
const { tokens, tokenList } = this.props;
446-
address = getChecksumAddress(address);
449+
address = toChecksumAddress(address);
447450
// First check if we have token information in token list
448451
if (address in tokenList) {
449452
return tokenList[address];
@@ -582,15 +585,15 @@ class Send extends PureComponent {
582585
let checksummedAddress = null;
583586

584587
if (assetType === 'ETH') {
585-
checksummedAddress = getChecksumAddress(transactionMeta.transaction.to);
588+
checksummedAddress = toChecksumAddress(transactionMeta.transaction.to);
586589
} else if (assetType === 'ERC20') {
587590
try {
588591
const [addressTo] = decodeTransferData(
589592
'transfer',
590593
transactionMeta.transaction.data,
591594
);
592595
if (addressTo) {
593-
checksummedAddress = getChecksumAddress(addressTo);
596+
checksummedAddress = toChecksumAddress(addressTo);
594597
}
595598
} catch (e) {
596599
Logger.log('Error decoding transfer data', transactionMeta.data);
@@ -603,7 +606,7 @@ class Send extends PureComponent {
603606
);
604607
const addressTo = data[1];
605608
if (addressTo) {
606-
checksummedAddress = getChecksumAddress(addressTo);
609+
checksummedAddress = toChecksumAddress(addressTo);
607610
}
608611
} catch (e) {
609612
Logger.log('Error decoding transfer data', transactionMeta.data);

app/components/Views/confirmations/legacy/SendFlow/SendTo/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { Fragment, PureComponent } from 'react';
22
import { View, ScrollView, Alert, Platform, BackHandler } from 'react-native';
33
import PropTypes from 'prop-types';
44
import { connect } from 'react-redux';
5-
import { getChecksumAddress } from '@metamask/utils';
5+
import { toChecksumAddress } from '@metamask/utils';
66
import { SafeAreaView } from 'react-native-safe-area-context';
77
import Icon from 'react-native-vector-icons/FontAwesome';
88
import AddressList from '../AddressList';
@@ -490,7 +490,7 @@ class SendFlow extends PureComponent {
490490

491491
safeChecksumAddress = (address) => {
492492
try {
493-
return getChecksumAddress(address);
493+
return toChecksumAddress(address);
494494
} catch (error) {
495495
return address;
496496
}

0 commit comments

Comments
 (0)