-
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 add migration to fix undefined sele…
…ctedAccount
- Loading branch information
1 parent
da1ebc4
commit 673c8e7
Showing
5 changed files
with
169 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { migrate, version } from './126'; | ||
import { createMockInternalAccount } from '../../../test/jest/mocks'; | ||
import { AccountsControllerState } from '@metamask/accounts-controller'; | ||
|
||
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 selectedAccount = ( | ||
newStorage.data.AccountsController as AccountsControllerState | ||
).internalAccounts.selectedAccount; | ||
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