Skip to content

Commit 997387c

Browse files
committed
chore: faster address checksum
1 parent 450aa69 commit 997387c

File tree

19 files changed

+95
-82
lines changed

19 files changed

+95
-82
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ jest.mock('../../../../selectors/currencyRateController', () => ({
2323
}));
2424

2525
jest.mock('ethereumjs-util', () => ({
26-
toChecksumAddress: jest.fn((address) => address),
2726
zeroAddress: jest.fn(() => '0x0000000000000000000000000000000000000000'),
2827
}));
2928

29+
jest.mock('@metamask/utils', () => ({
30+
getChecksumAddress: jest.fn((address) => address),
31+
}));
32+
3033
describe('AggregatedPercentageCrossChains', () => {
3134
const mockStore = configureStore([]);
3235
let store: Store;

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import {
1010
FORMATTED_VALUE_PRICE_TEST_ID,
1111
FORMATTED_PERCENTAGE_TEST_ID,
1212
} from './AggregatedPercentage.constants';
13-
import { toChecksumAddress, zeroAddress } from 'ethereumjs-util';
13+
import { zeroAddress } from 'ethereumjs-util';
14+
import { getChecksumAddress, Hex } from '@metamask/utils';
1415
import { selectTokenMarketData } from '../../../../selectors/tokenRatesController';
1516
import {
1617
MarketDataMapping,
1718
TokensWithBalances,
1819
} from '../../../../components/hooks/useGetFormattedTokensPerChain';
1920
import { getFormattedAmountChange, getPercentageTextColor } from './utils';
2021
import { AggregatedPercentageCrossChainsProps } from './AggregatedPercentageCrossChains.types';
21-
2222
export const getCalculatedTokenAmount1dAgo = (
2323
tokenFiatBalance: number,
2424
tokenPricePercentChange1dAgo: number,
@@ -48,7 +48,9 @@ const AggregatedPercentageCrossChains = ({
4848
const totalPerChain1dAgoERC20 = tokensWithBalances.reduce(
4949
(total1dAgo: number, item: { address: string }, idx: number) => {
5050
const found =
51-
crossChainMarketData?.[chainId]?.[toChecksumAddress(item.address)];
51+
crossChainMarketData?.[chainId]?.[
52+
getChecksumAddress(item.address as Hex)
53+
];
5254

5355
const tokenFiat1dAgo = getCalculatedTokenAmount1dAgo(
5456
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,4 @@
1-
import { toChecksumAddress } from 'ethereumjs-util';
1+
import { getChecksumAddress, Hex } from '@metamask/utils';
22
import React, { useEffect, useState } from 'react';
33
import { View } from 'react-native';
44
import { useSelector } from 'react-redux';
@@ -62,7 +62,7 @@ const AddressFrom = ({
6262
const accountsByChainId = useSelector(selectAccountsByChainId);
6363

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

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { fontStyles } from '../../../../styles/common';
66
import Identicon from '../../Identicon';
77
import NetworkMainAssetLogo from '../../NetworkMainAssetLogo';
88
import { useSelector } from 'react-redux';
9-
import { toChecksumAddress } from 'ethereumjs-util';
9+
import { getChecksumAddress } from '@metamask/utils';
1010
import { useTheme } from '../../../../util/theme';
1111
import { selectTokenList } from '../../../../selectors/tokenListController';
1212
import { ImportTokenViewSelectorsIDs } from '../../../../../e2e/selectors/wallet/ImportTokenView.selectors';
@@ -104,7 +104,7 @@ const AssetList = ({
104104
return <NetworkMainAssetLogo big style={styles.ethLogo} />;
105105
}
106106
const token =
107-
tokenList?.[toChecksumAddress(address)] ||
107+
tokenList?.[getChecksumAddress(address)] ||
108108
tokenList?.[address.toLowerCase()];
109109
const iconUrl = token?.iconUrl;
110110
if (!iconUrl) {

app/components/UI/TransactionElement/utils.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
getActionKey,
2525
TRANSACTION_TYPES,
2626
} from '../../../util/transactions';
27-
import { toChecksumAddress } from 'ethereumjs-util';
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[toChecksumAddress(token.address)]?.price
245+
? contractExchangeRates[getChecksumAddress(token.address)]?.price
246246
: undefined;
247247

248248
let renderTokenFiatAmount, renderTokenFiatNumber;
@@ -402,7 +402,8 @@ function decodeTransferFromTx(args) {
402402

403403
const renderFrom = renderFullAddress(addressFrom);
404404
const renderTo = renderFullAddress(addressTo);
405-
const ticker = networkConfigurationsByChainId?.[txChainId]?.nativeCurrency || args.ticker;
405+
const ticker =
406+
networkConfigurationsByChainId?.[txChainId]?.nativeCurrency || args.ticker;
406407

407408
const { SENT_COLLECTIBLE, RECEIVED_COLLECTIBLE } = TRANSACTION_TYPES;
408409
const transactionType =
@@ -473,7 +474,8 @@ function decodeDeploymentTx(args) {
473474
actionKey,
474475
primaryCurrency,
475476
} = args;
476-
const ticker = networkConfigurationsByChainId?.[txChainId]?.nativeCurrency || args.ticker;
477+
const ticker =
478+
networkConfigurationsByChainId?.[txChainId]?.nativeCurrency || args.ticker;
477479

478480
const totalGas = calculateTotalGas(txParams);
479481
const renderTotalEth = `${renderFromWei(totalGas)} ${ticker}`;

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

Lines changed: 2 additions & 2 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 { toChecksumAddress } from 'ethereumjs-util';
16+
import { getChecksumAddress } 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';
@@ -272,7 +272,7 @@ class ContactForm extends PureComponent {
272272
const { AddressBookController } = Engine.context;
273273
if (!name || !address) return;
274274
AddressBookController.set(
275-
toChecksumAddress(toEnsAddress || address),
275+
getChecksumAddress(toEnsAddress || address),
276276
name,
277277
chainId,
278278
memo,

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { toChecksumAddress } from 'ethereumjs-util';
21
import { useMemo } from 'react';
32
import { useSelector } from 'react-redux';
4-
import { Hex } from '@metamask/utils';
3+
import { Hex, getChecksumAddress } from '@metamask/utils';
54

65
import Engine from '../../../../core/Engine';
76
import useAddressBalance from '../../../../components/hooks/useAddressBalance/useAddressBalance';
@@ -13,7 +12,7 @@ import I18n from '../../../../../locales/i18n';
1312

1413
const useAccountInfo = (address: string, chainId: Hex) => {
1514
const internalAccounts = useSelector(selectInternalAccounts);
16-
const activeAddress = toChecksumAddress(address);
15+
const activeAddress = getChecksumAddress(address as Hex);
1716
const { addressBalance: accountBalance } = useAddressBalance(
1817
undefined,
1918
address,

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
fromWei,
1818
fromTokenMinimalUnit,
1919
} from '../../../../../util/number';
20-
import { toChecksumAddress } from 'ethereumjs-util';
20+
import { getChecksumAddress } from '@metamask/utils';
2121
import { strings } from '../../../../../../locales/i18n';
2222
import { getTransactionOptionsTitle } from '../../../../UI/Navbar';
2323
import { connect } from 'react-redux';
@@ -443,7 +443,7 @@ class Send extends PureComponent {
443443
*/
444444
handleTokenDeeplink = async (address) => {
445445
const { tokens, tokenList } = this.props;
446-
address = toChecksumAddress(address);
446+
address = getChecksumAddress(address);
447447
// First check if we have token information in token list
448448
if (address in tokenList) {
449449
return tokenList[address];
@@ -582,15 +582,15 @@ class Send extends PureComponent {
582582
let checksummedAddress = null;
583583

584584
if (assetType === 'ETH') {
585-
checksummedAddress = toChecksumAddress(transactionMeta.transaction.to);
585+
checksummedAddress = getChecksumAddress(transactionMeta.transaction.to);
586586
} else if (assetType === 'ERC20') {
587587
try {
588588
const [addressTo] = decodeTransferData(
589589
'transfer',
590590
transactionMeta.transaction.data,
591591
);
592592
if (addressTo) {
593-
checksummedAddress = toChecksumAddress(addressTo);
593+
checksummedAddress = getChecksumAddress(addressTo);
594594
}
595595
} catch (e) {
596596
Logger.log('Error decoding transfer data', transactionMeta.data);
@@ -603,7 +603,7 @@ class Send extends PureComponent {
603603
);
604604
const addressTo = data[1];
605605
if (addressTo) {
606-
checksummedAddress = toChecksumAddress(addressTo);
606+
checksummedAddress = getChecksumAddress(addressTo);
607607
}
608608
} catch (e) {
609609
Logger.log('Error decoding transfer data', transactionMeta.data);

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

Lines changed: 5 additions & 9 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 { toChecksumAddress } from 'ethereumjs-util';
5+
import { getChecksumAddress } 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';
@@ -278,7 +278,8 @@ class SendFlow extends PureComponent {
278278
};
279279

280280
onTransactionDirectionSet = async () => {
281-
const { setRecipient, navigation, providerType, selectedAddress } = this.props;
281+
const { setRecipient, navigation, providerType, selectedAddress } =
282+
this.props;
282283
const {
283284
toAccount,
284285
toEnsName,
@@ -291,12 +292,7 @@ class SendFlow extends PureComponent {
291292
}
292293

293294
const toAddress = toEnsAddressResolved || toAccount;
294-
setRecipient(
295-
selectedAddress,
296-
toAddress,
297-
toEnsName,
298-
toSelectedAddressName,
299-
);
295+
setRecipient(selectedAddress, toAddress, toEnsName, toSelectedAddressName);
300296
this.props.metrics.trackEvent(
301297
this.props.metrics
302298
.createEventBuilder(MetaMetricsEvents.SEND_FLOW_ADDS_RECIPIENT)
@@ -482,7 +478,7 @@ class SendFlow extends PureComponent {
482478

483479
safeChecksumAddress = (address) => {
484480
try {
485-
return toChecksumAddress(address);
481+
return getChecksumAddress(address);
486482
} catch (error) {
487483
return address;
488484
}

app/components/Views/confirmations/legacy/components/ApproveTransactionHeader/ApproveTransactionHeader.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { toChecksumAddress } from 'ethereumjs-util';
21
import React, { useEffect, useState } from 'react';
32
import { View } from 'react-native';
43
import { useSelector } from 'react-redux';
5-
import { Hex } from '@metamask/utils';
4+
import { Hex, getChecksumAddress } from '@metamask/utils';
65

76
import { strings } from '../../../../../../../locales/i18n';
87
import AccountBalance from '../../../../../../component-library/components-temp/Accounts/AccountBalance';
@@ -58,7 +57,7 @@ const ApproveTransactionHeader = ({
5857
const accountsByChainId = useSelector(selectAccountsByChainId);
5958

6059
const internalAccounts = useSelector(selectInternalAccounts);
61-
const activeAddress = toChecksumAddress(from);
60+
const activeAddress = getChecksumAddress(from as Hex);
6261

6362
const networkName = networkConfiguration?.name;
6463

0 commit comments

Comments
 (0)