-
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.
- Loading branch information
1 parent
341e2c7
commit 05e03d0
Showing
12 changed files
with
262 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { UseNonceField } from '../../../ui/pages/confirmations/token-allowance/token-allowance.stories'; | ||
import PreferencesController from '../controllers/preferences'; | ||
import { migrate, version } from './122'; | ||
|
||
const oldVersion = 121; | ||
|
||
describe('migration #122', () => { | ||
it('updates the version metadata', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: {}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
|
||
expect(newStorage.meta).toStrictEqual({ version }); | ||
}); | ||
|
||
it('does nothing if no preferences controller state is set', async () => { | ||
const oldState = { | ||
OtherController: {}, | ||
}; | ||
|
||
const transformedState = await migrate({ | ||
meta: { version: oldVersion }, | ||
data: oldState, | ||
}); | ||
|
||
expect(transformedState.data).toEqual(oldState); | ||
}); | ||
|
||
it('does nothing if no preferences state is set', async () => { | ||
const oldState = { | ||
PreferencesController: {}, | ||
}; | ||
|
||
const transformedState = await migrate({ | ||
meta: { version: oldVersion }, | ||
data: oldState, | ||
}); | ||
|
||
expect(transformedState.data).toEqual(oldState); | ||
}); | ||
|
||
it('returns state with advanced details opened if `useNonceField` is enabled', async () => { | ||
const initialState = { | ||
PreferencesController: { | ||
preferences: { isConfirmationAdvancedDetailsOpen: false }, | ||
}, | ||
metamask: { | ||
useNonceField: true, | ||
}, | ||
}; | ||
const transformedState = await migrate({ | ||
meta: { version: oldVersion }, | ||
data: initialState, | ||
}); | ||
|
||
expect(transformedState).toEqual({ | ||
meta: { version: oldVersion + 1 }, | ||
data: { | ||
...initialState, | ||
PreferencesController: { | ||
...initialState.PreferencesController, | ||
preferences: { isConfirmationAdvancedDetailsOpen: true }, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('returns state with advanced details opened if `sendHexData` is enabled', async () => { | ||
const initialState = { | ||
PreferencesController: { | ||
preferences: { isConfirmationAdvancedDetailsOpen: false }, | ||
}, | ||
metamask: { | ||
featureFlags: { | ||
sendHexData: true, | ||
}, | ||
}, | ||
}; | ||
const transformedState = await migrate({ | ||
meta: { version: oldVersion }, | ||
data: initialState, | ||
}); | ||
|
||
expect(transformedState).toEqual({ | ||
meta: { version: oldVersion + 1 }, | ||
data: { | ||
...initialState, | ||
PreferencesController: { | ||
...initialState.PreferencesController, | ||
preferences: { isConfirmationAdvancedDetailsOpen: true }, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it('returns state with advanced details closed if `sendHexData` and `useNonceField` are disabled', async () => { | ||
const initialState = { | ||
PreferencesController: { | ||
preferences: { isConfirmationAdvancedDetailsOpen: false }, | ||
}, | ||
metamask: { | ||
useNonceField: false, | ||
featureFlags: { | ||
sendHexData: false, | ||
}, | ||
}, | ||
}; | ||
const transformedState = await migrate({ | ||
meta: { version: oldVersion }, | ||
data: initialState, | ||
}); | ||
|
||
expect(transformedState).toEqual({ | ||
meta: { version: oldVersion + 1 }, | ||
data: { | ||
...initialState, | ||
PreferencesController: { | ||
...initialState.PreferencesController, | ||
preferences: { isConfirmationAdvancedDetailsOpen: false }, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); |
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,53 @@ | ||
import { cloneDeep } from 'lodash'; | ||
import { hasProperty } from '@metamask/utils'; | ||
|
||
type VersionedData = { | ||
meta: { version: number }; | ||
data: Record<string, unknown>; | ||
}; | ||
|
||
export const version = 122; | ||
|
||
/** | ||
* This migration sets the preference `isConfirmationAdvancedDetailsOpen` to | ||
* `true` if the user has enabled `useNonceField` or `sendHexData`. | ||
* | ||
* @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; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function transformState(state: Record<string, any>) { | ||
if ( | ||
!hasProperty(state, 'PreferencesController') || | ||
!hasProperty(state.PreferencesController, 'preferences') | ||
) { | ||
return state; | ||
} | ||
|
||
const isCustomNonceFieldEnabled = state?.metamask?.useNonceField; | ||
const isHexDataVisibilityEnabled = state?.metamask?.featureFlags?.sendHexData; | ||
|
||
if (isCustomNonceFieldEnabled || isHexDataVisibilityEnabled) { | ||
state.PreferencesController.preferences.isConfirmationAdvancedDetailsOpen = | ||
true; | ||
} else { | ||
state.PreferencesController.preferences.isConfirmationAdvancedDetailsOpen = | ||
false; | ||
} | ||
|
||
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
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
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