Skip to content

Commit

Permalink
refactor and test
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeRx committed Oct 16, 2024
1 parent fcb4bc8 commit 5f14a58
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 22 deletions.
49 changes: 27 additions & 22 deletions ui/selectors/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ 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';
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
import { isHexString, parseCaipAccountId } from '@metamask/utils';
import { parsed } from 'yargs';
import { addHexPrefix, getEnvironmentType } from '../../app/scripts/lib/util';
import {
TEST_CHAINS,
Expand Down Expand Up @@ -94,6 +93,7 @@ import {
} from '../ducks/app/app';
import { isEqualCaseInsensitive } from '../../shared/modules/string-utils';
import {
decimalToHex,
getValueFromWeiHex,
hexToDecimal,
} from '../../shared/modules/conversion.utils';
Expand Down Expand Up @@ -614,6 +614,13 @@ export function getAddressBookByNetwork(state, chainId) {
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];
Expand Down Expand Up @@ -1582,46 +1589,44 @@ export const getMemoizedUnapprovedTypedMessages = createDeepEqualSelector(
(unapprovedTypedMessages) => unapprovedTypedMessages,
);

export const selectAddress = (_, address) => address;

/**
* Get the display name for an address.
* This selector will look into the internal accounts, address book, metadata contracts, and ENS to find a display name for the 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.
* @returns {string} The display name for the address.
*/
export const getAddressDisplayName = createSelector((state, address) => {
export const getAddressDisplayName = (state, address) => {
const {
address: caipAddress,
chain: { namespace, reference },
} = parseCaipAccountId(
isHexString(address) ? `eip155:1:${address}` : address,
);

let parsedAddress;
const isEip155 = namespace === 'eip155';

if (namespace === 'eip155') {
parsedAddress = toChecksumHexAddress(caipAddress);
} else {
parsedAddress = caipAddress;
}
const parsedAddress = isEip155
? toChecksumHexAddress(caipAddress)
: caipAddress;

const accounts = getInternalAccounts(state);
const accountName = getAccountName(accounts, parsedAddress);
const metadataContractName = getMetadataContractName(state, parsedAddress);

// Address book and ENS entries will only work for EVM accounts.
const addressBookEntry = getAddressBookByNetwork(state, reference);
const ensName = getEnsResolutionByAddress(state, parsedAddress);
// Address book will only work for EVM accounts.
const addressBookEntry =
isEip155 &&
getAddressBookEntryByNetwork(
state,
parsedAddress,
`0x${decimalToHex(reference)}`,
);

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

export function getSnaps(state) {
return state.metamask.snaps;
Expand Down
112 changes: 112 additions & 0 deletions ui/selectors/selectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2194,4 +2194,116 @@ describe('#getConnectedSitesList', () => {
expect(selectors.getSelectedEvmInternalAccount(state)).toBe(undefined);
});
});

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', () => {
expect(
selectors.getAddressDisplayName(
mockState,
'eip155:1:0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
),
).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', () => {
const state = {
metamask: {
internalAccounts: {
accounts: [],
},
addressBook: {
'0x1': {
'0xc42edfcc21ed14dda456aa0756c153f7985d8813': {
address: '0xc42edfcc21ed14dda456aa0756c153f7985d8813',
chainId: '0x1',
isEns: false,
memo: '',
name: 'Address Book Account 1',
},
},
},
},
};

expect(
selectors.getAddressDisplayName(
state,
'eip155:1:0xc42edfcc21ed14dda456aa0756c153f7985d8813',
),
).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', () => {
const state = {
metamask: {
internalAccounts: {
accounts: [],
},
addressBook: {},
},
};

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

0 comments on commit 5f14a58

Please sign in to comment.