-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(snaps): Add getAddressDisplayName
selector
#27868
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -12,6 +12,7 @@ import { NameType } from '@metamask/name-controller'; | |||
import { TransactionStatus } from '@metamask/transaction-controller'; | ||||
import { isEvmAccountType } from '@metamask/keyring-api'; | ||||
import { RpcEndpointType } from '@metamask/network-controller'; | ||||
import { parseCaipAccountId } from '@metamask/utils'; | ||||
// TODO: Remove restricted import | ||||
// eslint-disable-next-line import/no-restricted-paths | ||||
import { addHexPrefix, getEnvironmentType } from '../../app/scripts/lib/util'; | ||||
|
@@ -92,6 +93,7 @@ import { | |||
} from '../ducks/app/app'; | ||||
import { isEqualCaseInsensitive } from '../../shared/modules/string-utils'; | ||||
import { | ||||
decimalToHex, | ||||
getValueFromWeiHex, | ||||
hexToDecimal, | ||||
} from '../../shared/modules/conversion.utils'; | ||||
|
@@ -605,6 +607,20 @@ export function getAddressBook(state) { | |||
return Object.values(state.metamask.addressBook[chainId]); | ||||
} | ||||
|
||||
export function getAddressBookByNetwork(state, chainId) { | ||||
if (!state.metamask.addressBook[chainId]) { | ||||
return []; | ||||
} | ||||
return Object.values(state.metamask.addressBook[chainId]); | ||||
} | ||||
|
||||
export function getAddressBookEntryByNetwork(state, address, chainId) { | ||||
const addressBook = getAddressBookByNetwork(state, chainId); | ||||
return addressBook.find((contact) => | ||||
isEqualCaseInsensitive(contact.address, address), | ||||
); | ||||
} | ||||
|
||||
export function getEnsResolutionByAddress(state, address) { | ||||
if (state.metamask.ensResolutionsByAddress[address]) { | ||||
return state.metamask.ensResolutionsByAddress[address]; | ||||
|
@@ -1573,6 +1589,48 @@ export const getMemoizedUnapprovedTypedMessages = createDeepEqualSelector( | |||
(unapprovedTypedMessages) => unapprovedTypedMessages, | ||||
); | ||||
|
||||
/** | ||||
* Get the display name for an address. | ||||
* This selector will look into the internal accounts and address book to find a display name for the address. | ||||
* | ||||
* @param _state - The Redux state object. | ||||
* @param {string} address - The address to get the display name for in a CAIP-10 format. | ||||
* @returns {string} The display name for the address. | ||||
*/ | ||||
export const getAddressDisplayName = createDeepEqualSelector( | ||||
[rawStateSelector, (_state, address) => address], | ||||
(state, address) => { | ||||
const { | ||||
address: caipAddress, | ||||
chain: { namespace, reference }, | ||||
} = parseCaipAccountId(address); | ||||
|
||||
const isEip155 = namespace === 'eip155'; | ||||
|
||||
const parsedAddress = isEip155 | ||||
? toChecksumHexAddress(caipAddress) | ||||
: caipAddress; | ||||
|
||||
const accounts = getInternalAccounts(state); | ||||
const accountName = getAccountName(accounts, parsedAddress); | ||||
|
||||
// Address book will only work for EVM accounts. | ||||
const addressBookEntry = | ||||
isEip155 && | ||||
getAddressBookEntryByNetwork( | ||||
state, | ||||
parsedAddress, | ||||
`0x${decimalToHex(reference)}`, | ||||
); | ||||
|
||||
return ( | ||||
(accountName === '' ? undefined : accountName) ?? | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. metamask-extension/ui/selectors/selectors.js Line 641 in 04b17ff
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh well, maybe good reason to use |
||||
addressBookEntry?.name ?? | ||||
undefined | ||||
); | ||||
}, | ||||
); | ||||
|
||||
export function getSnaps(state) { | ||||
return state.metamask.snaps; | ||||
} | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don' think the memoization will be effective if we pass in the entire raw state. Wondering how we can structure this to only reference the state we need 🤔