diff --git a/eslint-warning-thresholds.json b/eslint-warning-thresholds.json index 758dbd97646..d9185860d0b 100644 --- a/eslint-warning-thresholds.json +++ b/eslint-warning-thresholds.json @@ -110,7 +110,7 @@ "import-x/namespace": 18 }, "packages/base-controller/src/next/BaseController.test.ts": { - "import-x/namespace": 18 + "import-x/namespace": 14 }, "packages/build-utils/src/transforms/remove-fenced-code.test.ts": { "import-x/order": 1 diff --git a/packages/base-controller/CHANGELOG.md b/packages/base-controller/CHANGELOG.md index c33c63fe222..be85a20f68a 100644 --- a/packages/base-controller/CHANGELOG.md +++ b/packages/base-controller/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Bump `@metamask/utils` from `^11.4.2` to `^11.8.0` ([#6588](https://github.com/MetaMask/core/pull/6588)) - In experimental `next` export, rename `anonymous` metadata property to `includeInDebugSnapshot` ([#6593](https://github.com/MetaMask/core/pull/6593)) - In experimental `next` export, make `includeInStateLogs` and `usedInUi` metadata properties required ([#6593](https://github.com/MetaMask/core/pull/6593)) +- In experimental `next` export, remove deprecated exports `getPersistentState` and `getAnonymizedState` ([#6611](https://github.com/MetaMask/core/pull/6611)) ## [8.3.0] diff --git a/packages/base-controller/src/next/BaseController.test.ts b/packages/base-controller/src/next/BaseController.test.ts index 7b05d6bd90e..fcedab13da7 100644 --- a/packages/base-controller/src/next/BaseController.test.ts +++ b/packages/base-controller/src/next/BaseController.test.ts @@ -11,12 +11,7 @@ import type { ControllerStateChangeEvent, StatePropertyMetadata, } from './BaseController'; -import { - BaseController, - getAnonymizedState, - getPersistentState, - deriveStateFromMetadata, -} from './BaseController'; +import { BaseController, deriveStateFromMetadata } from './BaseController'; export const countControllerName = 'CountController'; @@ -780,416 +775,6 @@ describe('BaseController', () => { }); }); -describe('getAnonymizedState', () => { - afterEach(() => { - sinon.restore(); - }); - - it('should return empty state', () => { - expect(getAnonymizedState({}, {})).toStrictEqual({}); - }); - - it('should return empty state when no properties are anonymized', () => { - const anonymizedState = getAnonymizedState( - { count: 1 }, - { - count: { - includeInDebugSnapshot: false, - includeInStateLogs: false, - persist: false, - usedInUi: false, - }, - }, - ); - expect(anonymizedState).toStrictEqual({}); - }); - - it('should return state that is already anonymized', () => { - const anonymizedState = getAnonymizedState( - { - password: 'secret password', - privateKey: '123', - network: 'mainnet', - tokens: ['DAI', 'USDC'], - }, - { - password: { - includeInDebugSnapshot: false, - includeInStateLogs: false, - persist: false, - usedInUi: false, - }, - privateKey: { - includeInDebugSnapshot: false, - includeInStateLogs: false, - persist: false, - usedInUi: false, - }, - network: { - includeInDebugSnapshot: true, - includeInStateLogs: false, - persist: false, - usedInUi: false, - }, - tokens: { - includeInDebugSnapshot: true, - includeInStateLogs: false, - persist: false, - usedInUi: false, - }, - }, - ); - expect(anonymizedState).toStrictEqual({ - network: 'mainnet', - tokens: ['DAI', 'USDC'], - }); - }); - - it('should use anonymizing function to anonymize state', () => { - const anonymizeTransactionHash = (hash: string) => { - return hash.split('').reverse().join(''); - }; - - const anonymizedState = getAnonymizedState( - { - transactionHash: '0x1234', - }, - { - transactionHash: { - includeInDebugSnapshot: anonymizeTransactionHash, - includeInStateLogs: false, - persist: false, - usedInUi: false, - }, - }, - ); - - expect(anonymizedState).toStrictEqual({ transactionHash: '4321x0' }); - }); - - it('should allow returning a partial object from an anonymizing function', () => { - const anonymizeTxMeta = (txMeta: { hash: string; value: number }) => { - return { value: txMeta.value }; - }; - - const anonymizedState = getAnonymizedState( - { - txMeta: { - hash: '0x123', - value: 10, - }, - }, - { - txMeta: { - includeInDebugSnapshot: anonymizeTxMeta, - includeInStateLogs: false, - persist: false, - usedInUi: false, - }, - }, - ); - - expect(anonymizedState).toStrictEqual({ txMeta: { value: 10 } }); - }); - - it('should allow returning a nested partial object from an anonymizing function', () => { - const anonymizeTxMeta = (txMeta: { - hash: string; - value: number; - history: { hash: string; value: number }[]; - }) => { - return { - history: txMeta.history.map((entry) => { - return { value: entry.value }; - }), - value: txMeta.value, - }; - }; - - const anonymizedState = getAnonymizedState( - { - txMeta: { - hash: '0x123', - history: [ - { - hash: '0x123', - value: 9, - }, - ], - value: 10, - }, - }, - { - txMeta: { - includeInDebugSnapshot: anonymizeTxMeta, - includeInStateLogs: false, - persist: false, - usedInUi: false, - }, - }, - ); - - expect(anonymizedState).toStrictEqual({ - txMeta: { history: [{ value: 9 }], value: 10 }, - }); - }); - - it('should allow transforming types in an anonymizing function', () => { - const anonymizedState = getAnonymizedState( - { - count: '1', - }, - { - count: { - includeInDebugSnapshot: (count) => Number(count), - includeInStateLogs: false, - persist: false, - usedInUi: false, - }, - }, - ); - - expect(anonymizedState).toStrictEqual({ count: 1 }); - }); - - it('should suppress errors thrown when deriving state', () => { - const setTimeoutStub = sinon.stub(globalThis, 'setTimeout'); - const persistentState = getAnonymizedState( - { - extraState: 'extraState', - privateKey: '123', - network: 'mainnet', - }, - // @ts-expect-error Intentionally testing invalid state - { - privateKey: { - includeInDebugSnapshot: true, - includeInStateLogs: true, - persist: true, - usedInUi: true, - }, - network: { - includeInDebugSnapshot: false, - includeInStateLogs: false, - persist: false, - usedInUi: false, - }, - }, - ); - expect(persistentState).toStrictEqual({ - privateKey: '123', - }); - expect(setTimeoutStub.callCount).toBe(1); - const onTimeout = setTimeoutStub.firstCall.args[0]; - expect(() => onTimeout()).toThrow(`No metadata found for 'extraState'`); - }); -}); - -describe('getPersistentState', () => { - afterEach(() => { - sinon.restore(); - }); - - it('should return empty state', () => { - expect(getPersistentState({}, {})).toStrictEqual({}); - }); - - it('should return empty state when no properties are persistent', () => { - const persistentState = getPersistentState( - { count: 1 }, - { - count: { - includeInDebugSnapshot: false, - includeInStateLogs: false, - persist: false, - usedInUi: false, - }, - }, - ); - expect(persistentState).toStrictEqual({}); - }); - - it('should return persistent state', () => { - const persistentState = getPersistentState( - { - password: 'secret password', - privateKey: '123', - network: 'mainnet', - tokens: ['DAI', 'USDC'], - }, - { - password: { - includeInDebugSnapshot: false, - includeInStateLogs: false, - persist: true, - usedInUi: false, - }, - privateKey: { - includeInDebugSnapshot: false, - includeInStateLogs: false, - persist: true, - usedInUi: false, - }, - network: { - includeInDebugSnapshot: false, - includeInStateLogs: false, - persist: false, - usedInUi: false, - }, - tokens: { - includeInDebugSnapshot: false, - includeInStateLogs: false, - persist: false, - usedInUi: false, - }, - }, - ); - expect(persistentState).toStrictEqual({ - password: 'secret password', - privateKey: '123', - }); - }); - - it('should use function to derive persistent state', () => { - const normalizeTransacitonHash = (hash: string) => { - return hash.toLowerCase(); - }; - - const persistentState = getPersistentState( - { - transactionHash: '0X1234', - }, - { - transactionHash: { - includeInDebugSnapshot: false, - includeInStateLogs: false, - persist: normalizeTransacitonHash, - usedInUi: false, - }, - }, - ); - - expect(persistentState).toStrictEqual({ transactionHash: '0x1234' }); - }); - - it('should allow returning a partial object from a persist function', () => { - const getPersistentTxMeta = (txMeta: { hash: string; value: number }) => { - return { value: txMeta.value }; - }; - - const persistentState = getPersistentState( - { - txMeta: { - hash: '0x123', - value: 10, - }, - }, - { - txMeta: { - includeInDebugSnapshot: false, - includeInStateLogs: false, - persist: getPersistentTxMeta, - usedInUi: false, - }, - }, - ); - - expect(persistentState).toStrictEqual({ txMeta: { value: 10 } }); - }); - - it('should allow returning a nested partial object from a persist function', () => { - const getPersistentTxMeta = (txMeta: { - hash: string; - value: number; - history: { hash: string; value: number }[]; - }) => { - return { - history: txMeta.history.map((entry) => { - return { value: entry.value }; - }), - value: txMeta.value, - }; - }; - - const persistentState = getPersistentState( - { - txMeta: { - hash: '0x123', - history: [ - { - hash: '0x123', - value: 9, - }, - ], - value: 10, - }, - }, - { - txMeta: { - includeInDebugSnapshot: false, - includeInStateLogs: false, - persist: getPersistentTxMeta, - usedInUi: false, - }, - }, - ); - - expect(persistentState).toStrictEqual({ - txMeta: { history: [{ value: 9 }], value: 10 }, - }); - }); - - it('should allow transforming types in a persist function', () => { - const persistentState = getPersistentState( - { - count: '1', - }, - { - count: { - includeInDebugSnapshot: false, - includeInStateLogs: false, - persist: (count) => Number(count), - usedInUi: false, - }, - }, - ); - - expect(persistentState).toStrictEqual({ count: 1 }); - }); - - it('should suppress errors thrown when deriving state', () => { - const setTimeoutStub = sinon.stub(globalThis, 'setTimeout'); - const persistentState = getPersistentState( - { - extraState: 'extraState', - privateKey: '123', - network: 'mainnet', - }, - // @ts-expect-error Intentionally testing invalid state - { - privateKey: { - includeInDebugSnapshot: false, - includeInStateLogs: false, - persist: true, - usedInUi: false, - }, - network: { - includeInDebugSnapshot: false, - includeInStateLogs: false, - persist: false, - usedInUi: true, - }, - }, - ); - expect(persistentState).toStrictEqual({ - privateKey: '123', - }); - expect(setTimeoutStub.callCount).toBe(1); - const onTimeout = setTimeoutStub.firstCall.args[0]; - expect(() => onTimeout()).toThrow(`No metadata found for 'extraState'`); - }); -}); - describe('deriveStateFromMetadata', () => { afterEach(() => { sinon.restore(); diff --git a/packages/base-controller/src/next/BaseController.ts b/packages/base-controller/src/next/BaseController.ts index dae070e066c..00676318d6a 100644 --- a/packages/base-controller/src/next/BaseController.ts +++ b/packages/base-controller/src/next/BaseController.ts @@ -361,40 +361,6 @@ export class BaseController< } } -/** - * Returns an anonymized representation of the controller state. - * - * By "anonymized" we mean that it should not contain any information that could be personally - * identifiable. - * - * @deprecated Use `deriveStateFromMetadata` instead. - * @param state - The controller state. - * @param metadata - The controller state metadata, which describes how to derive the - * anonymized state. - * @returns The anonymized controller state. - */ -export function getAnonymizedState( - state: ControllerState, - metadata: StateMetadata, -): Record { - return deriveStateFromMetadata(state, metadata, 'includeInDebugSnapshot'); -} - -/** - * Returns the subset of state that should be persisted. - * - * @deprecated Use `deriveStateFromMetadata` instead. - * @param state - The controller state. - * @param metadata - The controller state metadata, which describes which pieces of state should be persisted. - * @returns The subset of controller state that should be persisted. - */ -export function getPersistentState( - state: ControllerState, - metadata: StateMetadata, -): Record { - return deriveStateFromMetadata(state, metadata, 'persist'); -} - /** * Use the metadata to derive state according to the given metadata property. * diff --git a/packages/base-controller/src/next/index.ts b/packages/base-controller/src/next/index.ts index 74fdaced86e..a0b5b1ae940 100644 --- a/packages/base-controller/src/next/index.ts +++ b/packages/base-controller/src/next/index.ts @@ -11,9 +11,4 @@ export type { ControllerGetStateAction, ControllerStateChangeEvent, } from './BaseController'; -export { - BaseController, - deriveStateFromMetadata, - getAnonymizedState, - getPersistentState, -} from './BaseController'; +export { BaseController, deriveStateFromMetadata } from './BaseController';