|
| 1 | +import { |
| 2 | + Infer, |
| 3 | + object, |
| 4 | + boolean, |
| 5 | + nullable, |
| 6 | + string, |
| 7 | + assert, |
| 8 | +} from '@metamask/superstruct'; |
| 9 | +import semver from 'semver'; |
| 10 | +import packageJson from '../../../package.json'; |
| 11 | +import { |
| 12 | + getRemoteFeatureFlags, |
| 13 | + type RemoteFeatureFlagsState, |
| 14 | +} from '../remote-feature-flags'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Feature flag structure for multichain accounts features |
| 18 | + */ |
| 19 | +const MultichainAccountsFeatureFlag = object({ |
| 20 | + enabled: boolean(), |
| 21 | + featureVersion: nullable(string()), |
| 22 | + minimumVersion: nullable(string()), |
| 23 | +}); |
| 24 | + |
| 25 | +/** |
| 26 | + * Feature flag type for multichain accounts features |
| 27 | + */ |
| 28 | +export type MultichainAccountsFeatureFlag = Infer< |
| 29 | + typeof MultichainAccountsFeatureFlag |
| 30 | +>; |
| 31 | + |
| 32 | +const APP_VERSION = packageJson.version; |
| 33 | +const FEATURE_VERSION_1 = '1'; |
| 34 | +const FEATURE_VERSION_2 = '2'; |
| 35 | + |
| 36 | +/** |
| 37 | + * Checks if the multichain accounts feature is enabled for a given state and feature version. |
| 38 | + * |
| 39 | + * @param enableMultichainAccounts - The MetaMask state object |
| 40 | + * @param featureVersion - The specific feature version to check |
| 41 | + * @returns boolean - True if the feature is enabled for the given state and version, false otherwise. |
| 42 | + */ |
| 43 | +export const isMultichainAccountsFeatureEnabled = ( |
| 44 | + enableMultichainAccounts: MultichainAccountsFeatureFlag, |
| 45 | + featureVersion: string, |
| 46 | +) => { |
| 47 | + const { |
| 48 | + enabled, |
| 49 | + featureVersion: currentFeatureVersion, |
| 50 | + minimumVersion, |
| 51 | + } = enableMultichainAccounts; |
| 52 | + |
| 53 | + return ( |
| 54 | + enabled && |
| 55 | + currentFeatureVersion && |
| 56 | + minimumVersion && |
| 57 | + currentFeatureVersion === featureVersion && |
| 58 | + semver.gte(minimumVersion, APP_VERSION) |
| 59 | + ); |
| 60 | +}; |
| 61 | + |
| 62 | +/** |
| 63 | + * Selector to get the multichain accounts remote feature flags. |
| 64 | + * |
| 65 | + * @param state - The MetaMask state object |
| 66 | + * @returns MultichainAccountsFeatureFlag - The feature flags for multichain accounts. |
| 67 | + */ |
| 68 | +export const getMultichainAccountsRemoteFeatureFlags = ( |
| 69 | + state: RemoteFeatureFlagsState, |
| 70 | +) => { |
| 71 | + const multichainAccountsFeatureFlags = |
| 72 | + getRemoteFeatureFlags(state).enableMultichainAccounts; |
| 73 | + |
| 74 | + try { |
| 75 | + assert(multichainAccountsFeatureFlags, MultichainAccountsFeatureFlag); |
| 76 | + } catch (error) { |
| 77 | + console.warn('Invalid multichain accounts feature flags:', error); |
| 78 | + return { |
| 79 | + enabled: false, |
| 80 | + featureVersion: null, |
| 81 | + minimumVersion: null, |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + return multichainAccountsFeatureFlags; |
| 86 | +}; |
| 87 | + |
| 88 | +/** |
| 89 | + * Selector to check if the multichain accounts feature is enabled for state 1. |
| 90 | + * |
| 91 | + * @param state - The MetaMask state object |
| 92 | + * @returns boolean - True if the feature is enabled for state 1, false otherwise. |
| 93 | + */ |
| 94 | +export const getIsMultichainAccountsState1Enabled = ( |
| 95 | + state: RemoteFeatureFlagsState, |
| 96 | +) => { |
| 97 | + const flags = getMultichainAccountsRemoteFeatureFlags(state); |
| 98 | + return ( |
| 99 | + isMultichainAccountsFeatureEnabled(flags, FEATURE_VERSION_2) || |
| 100 | + isMultichainAccountsFeatureEnabled(flags, FEATURE_VERSION_1) |
| 101 | + ); |
| 102 | +}; |
| 103 | + |
| 104 | +/** |
| 105 | + * Selector to check if the multichain accounts feature is enabled for state 2. |
| 106 | + * |
| 107 | + * @param state - The MetaMask state object |
| 108 | + * @returns boolean - True if the feature is enabled for state 2, false otherwise. |
| 109 | + */ |
| 110 | +export const getIsMultichainAccountsState2Enabled = ( |
| 111 | + state: RemoteFeatureFlagsState, |
| 112 | +) => { |
| 113 | + const flags = getMultichainAccountsRemoteFeatureFlags(state); |
| 114 | + return isMultichainAccountsFeatureEnabled(flags, FEATURE_VERSION_2); |
| 115 | +}; |
0 commit comments