Skip to content

Commit

Permalink
address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeRx committed Oct 16, 2024
1 parent bdb4b9e commit 6a45a0e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 88 deletions.
63 changes: 34 additions & 29 deletions ui/selectors/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +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 { isHexString, parseCaipAccountId } from '@metamask/utils';
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';
Expand Down Expand Up @@ -1593,38 +1593,43 @@ export const getMemoizedUnapprovedTypedMessages = createDeepEqualSelector(
* 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 {object} state - The Redux state object.
* @param {string} address - The address to get the display name for.
* @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 = (state, address) => {
const {
address: caipAddress,
chain: { namespace, reference },
} = parseCaipAccountId(
isHexString(address) ? `eip155:1:${address}` : address,
);

const isEip155 = namespace === 'eip155';

const parsedAddress = isEip155
? toChecksumHexAddress(caipAddress)
: caipAddress;

const accounts = getInternalAccounts(state);
const accountName = getAccountName(accounts, parsedAddress);
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)}`,
);

// Address book will only work for EVM accounts.
const addressBookEntry =
isEip155 &&
getAddressBookEntryByNetwork(
state,
parsedAddress,
`0x${decimalToHex(reference)}`,
return (
(accountName === '' ? undefined : accountName) ??
addressBookEntry?.name ??
undefined
);

return accountName || addressBookEntry?.name || shortenAddress(parsedAddress);
};
},
);

export function getSnaps(state) {
return state.metamask.snaps;
Expand Down
63 changes: 4 additions & 59 deletions ui/selectors/selectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2247,16 +2247,7 @@ describe('#getConnectedSitesList', () => {
});

describe('getAddressDisplayName', () => {
it('returns the account name for a hex address', () => {
expect(
selectors.getAddressDisplayName(
mockState,
'0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
),
).toBe('Test Account');
});

it('returns the account name for a CAIP-10 ID', () => {
it('returns the account name', () => {
expect(
selectors.getAddressDisplayName(
mockState,
Expand All @@ -2265,35 +2256,7 @@ describe('#getConnectedSitesList', () => {
).toBe('Test Account');
});

it('returns the address book entry for a hex address', () => {
const state = {
metamask: {
internalAccounts: {
accounts: [],
},
addressBook: {
'0x1': {
'0xc42edfcc21ed14dda456aa0756c153f7985d8813': {
address: '0xc42edfcc21ed14dda456aa0756c153f7985d8813',
chainId: '0x1',
isEns: false,
memo: '',
name: 'Address Book Account 1',
},
},
},
},
};

expect(
selectors.getAddressDisplayName(
state,
'0xc42edfcc21ed14dda456aa0756c153f7985d8813',
),
).toBe('Address Book Account 1');
});

it('returns the address book entry for a CAIP-10 address', () => {
it('returns the address book entry', () => {
const state = {
metamask: {
internalAccounts: {
Expand Down Expand Up @@ -2321,25 +2284,7 @@ describe('#getConnectedSitesList', () => {
).toBe('Address Book Account 1');
});

it('returns the shorten address if no name is found for a hex address', () => {
const state = {
metamask: {
internalAccounts: {
accounts: [],
},
addressBook: {},
},
};

expect(
selectors.getAddressDisplayName(
state,
'0xc42edfcc21ed14dda456aa0756c153f7985d8813',
),
).toBe('0xc42ED...D8813');
});

it('returns the shorten address if no name is found for a CAIP-10 address', () => {
it('returns null if no entry is found', () => {
const state = {
metamask: {
internalAccounts: {
Expand All @@ -2354,7 +2299,7 @@ describe('#getConnectedSitesList', () => {
state,
'eip155:1:0xc42edfcc21ed14dda456aa0756c153f7985d8813',
),
).toBe('0xc42ED...D8813');
).toBe(undefined);
});
});
});

0 comments on commit 6a45a0e

Please sign in to comment.