Skip to content
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

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions ui/selectors/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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(
Copy link
Member

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 🤔

[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) ??
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export function getAccountName(accounts, accountAddress) {
😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh well, maybe good reason to use || then, Just leave a comment about why

addressBookEntry?.name ??
undefined
);
},
);

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

describe('getAddressBookByNetwork', () => {
it('returns the address book for the given network', () => {
expect(selectors.getAddressBookByNetwork(mockState, '0x5')).toStrictEqual(
[
{
address: '0xc42edfcc21ed14dda456aa0756c153f7985d8813',
chainId: '0x5',
isEns: false,
memo: '',
name: 'Address Book Account 1',
},
],
);
});

it('returns an empty object if no address book is found for the given network', () => {
expect(selectors.getAddressBookByNetwork(mockState, '0x1')).toStrictEqual(
[],
);
});
});

describe('getAddressBookEntryByNetwork', () => {
it('returns the address book entry for the given network and address', () => {
expect(
selectors.getAddressBookEntryByNetwork(
mockState,

'0xc42edfcc21ed14dda456aa0756c153f7985d8813',
'0x5',
),
).toStrictEqual({
address: '0xc42edfcc21ed14dda456aa0756c153f7985d8813',
chainId: '0x5',
isEns: false,
memo: '',
name: 'Address Book Account 1',
});
});

it('returns `undefined` if no entry is found for the given network and address', () => {
expect(
selectors.getAddressBookEntryByNetwork(
mockState,
'0xc42edfcc21ed14dda456aa0756c153f7985d8813',
'0x1',
),
).toBe(undefined);
});
});

describe('getAddressDisplayName', () => {
it('returns the account name', () => {
expect(
selectors.getAddressDisplayName(
mockState,
'eip155:1:0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc',
),
).toBe('Test Account');
});

it('returns the address book entry', () => {
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 null if no entry is found', () => {
const state = {
metamask: {
internalAccounts: {
accounts: [],
},
addressBook: {},
},
};

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