-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix/re order internal accounts (#9993)
<!-- 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
Showing
5 changed files
with
191 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |