-
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.
Merge branch 'Version-v12.3.0' into cherry-pick-v12.3.0-b960add20dda2…
…bfb2721f190b897be1103815622
- Loading branch information
Showing
6 changed files
with
171 additions
and
4 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,107 @@ | ||
import { migrate, version } from './125.1'; | ||
|
||
const oldVersion = 125; | ||
|
||
describe(`migration #${version}`, () => { | ||
afterEach(() => jest.resetAllMocks()); | ||
|
||
it('updates the version metadata', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: {}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
expect(newStorage.meta).toStrictEqual({ version }); | ||
}); | ||
|
||
it('Gracefully handles empty/undefined PreferencesController', async () => { | ||
for (const PreferencesController of [{}, undefined, null, 1, '', []]) { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: { PreferencesController }, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
expect(newStorage.data.TxController).toStrictEqual(undefined); | ||
} | ||
}); | ||
|
||
it('Enables token autodetection when basic functionality is on', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: { | ||
PreferencesController: { | ||
useExternalServices: true, | ||
}, | ||
}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
expect(newStorage.data).toEqual({ | ||
PreferencesController: { | ||
useExternalServices: true, | ||
useTokenDetection: true, | ||
}, | ||
}); | ||
}); | ||
|
||
it('Does not enable token autodetection when basic functionality is off', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: { | ||
PreferencesController: { | ||
useExternalServices: false, | ||
}, | ||
}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
expect(newStorage.data).toEqual({ | ||
PreferencesController: { | ||
useExternalServices: false, | ||
}, | ||
}); | ||
}); | ||
|
||
it('Removes showTokenAutodetectModalOnUpgrade from the app metadata controller', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: { | ||
AppMetadataController: { | ||
previousMigrationVersion: oldVersion, | ||
currentMigrationVersion: version, | ||
showTokenAutodetectModalOnUpgrade: null, | ||
}, | ||
}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
expect(newStorage.data).toEqual({ | ||
AppMetadataController: { | ||
previousMigrationVersion: oldVersion, | ||
currentMigrationVersion: version, | ||
}, | ||
}); | ||
}); | ||
|
||
it('Does nothing if showTokenAutodetectModalOnUpgrade is not in the app metadata controller', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: { | ||
AppMetadataController: { | ||
previousMigrationVersion: oldVersion, | ||
currentMigrationVersion: version, | ||
}, | ||
}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
expect(newStorage.data).toEqual({ | ||
AppMetadataController: { | ||
previousMigrationVersion: oldVersion, | ||
currentMigrationVersion: version, | ||
}, | ||
}); | ||
}); | ||
}); |
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,50 @@ | ||
import { hasProperty, isObject } from '@metamask/utils'; | ||
import { cloneDeep } from 'lodash'; | ||
|
||
type VersionedData = { | ||
meta: { version: number }; | ||
data: Record<string, unknown>; | ||
}; | ||
|
||
export const version = 125.1; | ||
|
||
/** | ||
* This migration enables token auto-detection if the basic functionality toggle is on. | ||
* | ||
* It also removes an unused property `showTokenAutodetectModalOnUpgrade` from the app metadata controller. | ||
* | ||
* @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>) { | ||
if ( | ||
hasProperty(state, 'PreferencesController') && | ||
isObject(state.PreferencesController) && | ||
state.PreferencesController.useExternalServices === true | ||
) { | ||
state.PreferencesController.useTokenDetection = true; | ||
} | ||
|
||
if ( | ||
hasProperty(state, 'AppMetadataController') && | ||
isObject(state.AppMetadataController) | ||
) { | ||
delete state.AppMetadataController.showTokenAutodetectModalOnUpgrade; | ||
} | ||
|
||
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