-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: bump accounts controller and migration to fix undefined selected…
…Account (#26573) <!-- 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** This PR bumps the `AccountsController` and introduces a new migration. The `updateAccounts` methods from the `AccountsController` now checks if the selectedAccount is undefined and recovers from this. The migration updates the selectedAccount values that are not defined. <!-- 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? --> [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/26573?quickstart=1) ## **Related issues** Fixes: #26377 ## **Manual testing steps** 1. Go to this page... 2. 3. ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.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-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] 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. --------- Co-authored-by: MetaMask Bot <metamaskbot@users.noreply.github.com>
- Loading branch information
1 parent
d81d69b
commit 759b92e
Showing
18 changed files
with
333 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
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,69 @@ | ||
import { AccountsControllerState } from '@metamask/accounts-controller'; | ||
import { createMockInternalAccount } from '../../../test/jest/mocks'; | ||
import { migrate, version } from './126'; | ||
|
||
const oldVersion = 125; | ||
|
||
const mockInternalAccount = createMockInternalAccount(); | ||
const mockAccountsControllerState: AccountsControllerState = { | ||
internalAccounts: { | ||
accounts: { | ||
[mockInternalAccount.id]: mockInternalAccount, | ||
}, | ||
selectedAccount: mockInternalAccount.id, | ||
}, | ||
}; | ||
|
||
describe('migration #126', () => { | ||
afterEach(() => jest.resetAllMocks()); | ||
|
||
it('updates the version metadata', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: { | ||
AccountsController: mockAccountsControllerState, | ||
}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
expect(newStorage.meta).toStrictEqual({ version }); | ||
}); | ||
|
||
it('updates selected account if it is not found in the list of accounts', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: { | ||
AccountsController: { | ||
...mockAccountsControllerState, | ||
internalAccounts: { | ||
...mockAccountsControllerState.internalAccounts, | ||
selectedAccount: 'unknown id', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
const { | ||
internalAccounts: { selectedAccount }, | ||
} = newStorage.data.AccountsController as AccountsControllerState; | ||
expect(selectedAccount).toStrictEqual(mockInternalAccount.id); | ||
expect(newStorage.data.AccountsController).toStrictEqual( | ||
mockAccountsControllerState, | ||
); | ||
}); | ||
|
||
it('does nothing if the selectedAccount is found in the list of accounts', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: { | ||
AccountsController: mockAccountsControllerState, | ||
}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
expect(newStorage.data.AccountsController).toStrictEqual( | ||
mockAccountsControllerState, | ||
); | ||
}); | ||
}); |
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,49 @@ | ||
import { AccountsControllerState } from '@metamask/accounts-controller'; | ||
import { cloneDeep } from 'lodash'; | ||
|
||
type VersionedData = { | ||
meta: { version: number }; | ||
data: Record<string, unknown>; | ||
}; | ||
|
||
export const version = 126; | ||
|
||
/** | ||
* This migration removes depreciated `Txcontroller` key if it is present in state. | ||
* | ||
* @param originalVersionedData - Versioned MetaMask extension state, exactly | ||
* what we persist to dist. | ||
* @param originalVersionedData.meta - State metadata. | ||
* @param originalVersionedData.meta.version - The current state version. | ||
* @param originalVersionedData.data - The persisted MetaMask state, keyed by | ||
* controller. | ||
* @returns Updated versioned MetaMask extension state. | ||
*/ | ||
export async function migrate( | ||
originalVersionedData: VersionedData, | ||
): Promise<VersionedData> { | ||
const versionedData = cloneDeep(originalVersionedData); | ||
versionedData.meta.version = version; | ||
transformState(versionedData.data); | ||
return versionedData; | ||
} | ||
|
||
function transformState(state: Record<string, unknown>) { | ||
const accountsControllerState = state?.AccountsController as | ||
| AccountsControllerState | ||
| undefined; | ||
|
||
if ( | ||
accountsControllerState && | ||
Object.values(accountsControllerState?.internalAccounts.accounts).length > | ||
0 && | ||
!accountsControllerState?.internalAccounts.accounts[ | ||
accountsControllerState?.internalAccounts.selectedAccount | ||
] | ||
) { | ||
accountsControllerState.internalAccounts.selectedAccount = Object.values( | ||
accountsControllerState?.internalAccounts.accounts, | ||
)[0].id; | ||
} | ||
return state; | ||
} |
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
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
Oops, something went wrong.