Skip to content

Commit

Permalink
fix: Fix/re order internal accounts (#9993)
Browse files Browse the repository at this point in the history
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

This PR updates the `selectInternalAccounts` selector to return accounts
sorted by the order of the accounts in the KeyringController. This
update preserves the sorting behavior of accounts and matches behavior
of versions prior to 7.24.0.

## **Related issues**

Fixes: #9990

## **Manual testing steps**

To reproduce the issue

- Install 7.23.0
- Import accounts via SRP
- Create an account from the wallet
- Import an account via private key
- Create another account from the wallet
- Notice that the section of accounts on the account selector list is
ordered by 1. SRP, 2.. Wallet, 3. Private key

- Download 7.24.0
- Open the account selector list and notice that accounts are now sorted
by the order in which the accounts were created. Reference the steps
above for the order.

To see the fix
- Install this branch
- Open the account selector list and notice that the accounts are
ordered the same as it was on 7.23.0 - 1. SRP, 2.. Wallet, 3. Private
key.

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->
Creating accounts on 7.21.0, then upgrading to 7.24.0. The account name
change is unrelated to this fix and will be addressed in a separate
hotfox for 7.24.1.

https://github.com/MetaMask/metamask-mobile/assets/10508597/3c1a5587-1172-4db1-8bea-6d64239f7baf

### **After**

<!-- [screenshots/recordings] -->
Continuing from the state above, upgrading to this branch fixes the
ordering

https://github.com/MetaMask/metamask-mobile/assets/10508597/15277032-2adf-4370-bd3c-c92b3037c526


## **Pre-merge author checklist**

- [x] I’ve followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [x] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [x] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
  • Loading branch information
Cal-L authored Jun 14, 2024
1 parent 38745b8 commit d7cbdfe
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 41 deletions.
106 changes: 67 additions & 39 deletions app/selectors/accountsController.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AccountsControllerState } from '@metamask/accounts-controller';
import { captureException } from '@sentry/react-native';
import { Hex, isValidChecksumAddress } from '@metamask/utils';
import { InternalAccount } from '@metamask/keyring-api';
import DefaultPreference from 'react-native-default-preference';
import {
selectSelectedInternalAccount,
Expand All @@ -13,9 +14,48 @@ import {
expectedUuid2,
internalAccount1,
MOCK_ADDRESS_2,
createMockInternalAccount,
createMockUuidFromAddress,
} from '../util/test/accountsControllerTestUtils';
import { RootState } from '../reducers';
import { AGREED } from '../constants/storage';
import {
MOCK_KEYRINGS,
MOCK_KEYRING_CONTROLLER,
} from './keyringController/testUtils';

/**
* Generates a mocked AccountsController state
* The internal accounts are generated in reverse order relative to the mock keyrings that are used for generation
*
* @returns - A mocked state of AccountsController
*/
const MOCK_GENERATED_ACCOUNTS_CONTROLLER_REVERSED =
(): AccountsControllerState => {
const reversedKeyringAccounts = [...MOCK_KEYRINGS]
.reverse()
.flatMap((keyring) => [...keyring.accounts].reverse());
const accountsForInternalAccounts = reversedKeyringAccounts.reduce(
(record, keyringAccount, index) => {
const lowercasedKeyringAccount = keyringAccount.toLowerCase();
const accountName = `Account ${index}`;
const uuid = createMockUuidFromAddress(lowercasedKeyringAccount);
const internalAccount = createMockInternalAccount(
lowercasedKeyringAccount,
accountName,
);
record[uuid] = internalAccount;
return record;
},
{} as Record<string, InternalAccount>,
);
return {
internalAccounts: {
accounts: accountsForInternalAccounts,
selectedAccount: Object.values(accountsForInternalAccounts)[0].id,
},
};
};

jest.mock('@sentry/react-native', () => ({
captureException: jest.fn(),
Expand Down Expand Up @@ -84,47 +124,35 @@ describe('Accounts Controller Selectors', () => {
});
});
describe('selectInternalAccounts', () => {
it('returns all internal accounts in the accounts controller state', () => {
expect(
selectInternalAccounts({
engine: {
backgroundState: {
AccountsController: MOCK_ACCOUNTS_CONTROLLER_STATE,
},
it(`returns internal accounts of the accounts controller sorted by the keyring controller's accounts`, () => {
const mockAccountsControllerReversed =
MOCK_GENERATED_ACCOUNTS_CONTROLLER_REVERSED();
const internalAccountsResult = selectInternalAccounts({
engine: {
backgroundState: {
KeyringController: MOCK_KEYRING_CONTROLLER,
AccountsController: mockAccountsControllerReversed,
},
} as RootState),
).toEqual([
{
address: '0xc4955c0d639d99699bfd7ec54d9fafee40e4d272',
id: expectedUuid,
metadata: { name: 'Account 1', keyring: { type: 'HD Key Tree' } },
options: {},
methods: [
'personal_sign',
'eth_sign',
'eth_signTransaction',
'eth_signTypedData_v1',
'eth_signTypedData_v3',
'eth_signTypedData_v4',
],
type: 'eip155:eoa',
},
{
address: '0xc4966c0d659d99699bfd7eb54d8fafee40e4a756',
id: expectedUuid2,
metadata: { name: 'Account 2', keyring: { type: 'HD Key Tree' } },
options: {},
methods: [
'personal_sign',
'eth_sign',
'eth_signTransaction',
'eth_signTypedData_v1',
'eth_signTypedData_v3',
'eth_signTypedData_v4',
],
type: 'eip155:eoa',
},
]);
} as RootState);
const expectedInteralAccountsResult = Object.values(
mockAccountsControllerReversed.internalAccounts.accounts,
).reverse();

const internalAccountAddressesResult = internalAccountsResult.map(
(account) => account.address,
);
const expectedAccountAddressesResult = [...MOCK_KEYRINGS].flatMap(
(keyring) => keyring.accounts,
);

// Ensure accounts are correct
expect(internalAccountsResult).toEqual(expectedInteralAccountsResult);

// Ensure that order of internal accounts match order of keyring accounts
expect(internalAccountAddressesResult).toEqual(
expectedAccountAddressesResult,
);
});
});
describe('selectSelectedInternalAccountChecksummedAddress', () => {
Expand Down
34 changes: 32 additions & 2 deletions app/selectors/accountsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,43 @@ import { captureException } from '@sentry/react-native';
import { createSelector } from 'reselect';
import { RootState } from '../reducers';
import { createDeepEqualSelector } from './util';
import { selectFlattenedKeyringAccounts } from './keyringController';

/**
*
* @param state - Root redux state
* @returns - AccountsController state
*/
const selectAccountsControllerState = (state: RootState) =>
state.engine.backgroundState.AccountsController;

/**
* A memoized selector that returns internal accounts from the AccountsController, sorted by the order of KeyringController's keyring accounts
*/
export const selectInternalAccounts = createDeepEqualSelector(
selectAccountsControllerState,
(accountControllerState) =>
Object.values(accountControllerState.internalAccounts.accounts),
selectFlattenedKeyringAccounts,
(accountControllerState, orderedKeyringAccounts) => {
const keyringAccountsMap = new Map(
orderedKeyringAccounts.map((account, index) => [
account.toLowerCase(),
index,
]),
);
const sortedAccounts = Object.values(
accountControllerState.internalAccounts.accounts,
).sort(
(a, b) =>
(keyringAccountsMap.get(a.address.toLowerCase()) || 0) -
(keyringAccountsMap.get(b.address.toLowerCase()) || 0),
);
return sortedAccounts;
},
);

/**
* A memoized selector that returns the selected internal account from the AccountsController
*/
export const selectSelectedInternalAccount = createDeepEqualSelector(
selectAccountsControllerState,
(accountsControllerState: AccountsControllerState) => {
Expand All @@ -31,6 +58,9 @@ export const selectSelectedInternalAccount = createDeepEqualSelector(
},
);

/**
* A memoized selector that returns the selected internal account address in checksum format
*/
export const selectSelectedInternalAccountChecksummedAddress = createSelector(
selectSelectedInternalAccount,
(account) => {
Expand Down
43 changes: 43 additions & 0 deletions app/selectors/keyringController/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { selectKeyrings, selectFlattenedKeyringAccounts } from './index';
import { RootState } from '../../reducers';
import {
MOCK_SIMPLE_ACCOUNTS,
MOCK_QR_ACCOUNTS,
MOCK_HD_ACCOUNTS,
MOCK_KEYRINGS,
MOCK_KEYRING_CONTROLLER,
} from './testUtils';

describe('KeyringController Selectors', () => {
describe('selectKeyrings', () => {
it('returns keyrings', () => {
expect(
selectKeyrings({
engine: {
backgroundState: {
KeyringController: MOCK_KEYRING_CONTROLLER,
},
},
} as RootState),
).toEqual(MOCK_KEYRINGS);
});
});
describe('selectFlattenedKeyringAccounts', () => {
it('returns flattened keyring accounts', () => {
const expectedOrderedKeyringAccounts = [
...MOCK_SIMPLE_ACCOUNTS,
...MOCK_QR_ACCOUNTS,
...MOCK_HD_ACCOUNTS,
];
expect(
selectFlattenedKeyringAccounts({
engine: {
backgroundState: {
KeyringController: MOCK_KEYRING_CONTROLLER,
},
},
} as RootState),
).toEqual(expectedOrderedKeyringAccounts);
});
});
});
31 changes: 31 additions & 0 deletions app/selectors/keyringController/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { RootState } from '../../reducers';
import { createDeepEqualSelector } from '../util';

/**
*
* @param state - Root Redux state
* @returns - KeyringController state
*/
const selectKeyringControllerState = (state: RootState) =>
state.engine.backgroundState.KeyringController;

/**
* A memoized selector that retrieves keyrings from the KeyringController
*/
export const selectKeyrings = createDeepEqualSelector(
selectKeyringControllerState,
(keyringControllerState) => keyringControllerState.keyrings,
);

/**
* A memoized selector that returns the list of accounts from all keyrings in the form of a flattened array of strings.
*/
export const selectFlattenedKeyringAccounts = createDeepEqualSelector(
selectKeyrings,
(keyrings) => {
const flattenedKeyringAccounts = keyrings.flatMap(
(keyring) => keyring.accounts,
);
return flattenedKeyringAccounts;
},
);
18 changes: 18 additions & 0 deletions app/selectors/keyringController/testUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
KeyringControllerState,
KeyringObject,
KeyringTypes,
} from '@metamask/keyring-controller';

export const MOCK_SIMPLE_ACCOUNTS = ['0x1', '0x2'];
export const MOCK_QR_ACCOUNTS = ['0x3', '0x4'];
export const MOCK_HD_ACCOUNTS = ['0x5', '0x6'];
export const MOCK_KEYRINGS: KeyringObject[] = [
{ accounts: MOCK_SIMPLE_ACCOUNTS, type: KeyringTypes.simple },
{ accounts: MOCK_QR_ACCOUNTS, type: KeyringTypes.qr },
{ accounts: MOCK_HD_ACCOUNTS, type: KeyringTypes.hd },
];
export const MOCK_KEYRING_CONTROLLER: KeyringControllerState = {
isUnlocked: true,
keyrings: MOCK_KEYRINGS,
};

0 comments on commit d7cbdfe

Please sign in to comment.