mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-29 23:58:06 +01:00
49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
import { useSelector } from 'react-redux';
|
|
|
|
import { toChecksumHexAddress } from '../../shared/modules/hexstring-utils';
|
|
import {
|
|
getAddressBook,
|
|
getMetaMaskIdentities,
|
|
getTokenList,
|
|
getUseTokenDetection,
|
|
} from '../selectors';
|
|
import { shortenAddress } from '../helpers/utils/util';
|
|
|
|
const useAddressDetails = (toAddress) => {
|
|
const addressBook = useSelector(getAddressBook);
|
|
const identities = useSelector(getMetaMaskIdentities);
|
|
const tokenList = useSelector(getTokenList);
|
|
const useTokenDetection = useSelector(getUseTokenDetection);
|
|
const checksummedAddress = toChecksumHexAddress(toAddress);
|
|
|
|
if (!toAddress) {
|
|
return {};
|
|
}
|
|
const addressBookEntryObject = addressBook.find(
|
|
(entry) => entry.address === checksummedAddress,
|
|
);
|
|
if (addressBookEntryObject?.name) {
|
|
return { toName: addressBookEntryObject.name, isTrusted: true };
|
|
}
|
|
if (identities[toAddress]?.name) {
|
|
return { toName: identities[toAddress].name, isTrusted: true };
|
|
}
|
|
const casedTokenList = useTokenDetection
|
|
? tokenList
|
|
: Object.keys(tokenList).reduce((acc, base) => {
|
|
return {
|
|
...acc,
|
|
[base.toLowerCase()]: tokenList[base],
|
|
};
|
|
}, {});
|
|
if (casedTokenList[toAddress]?.name) {
|
|
return { toName: casedTokenList[toAddress].name, isTrusted: true };
|
|
}
|
|
return {
|
|
toName: shortenAddress(checksummedAddress),
|
|
isTrusted: false,
|
|
};
|
|
};
|
|
|
|
export default useAddressDetails;
|