@metamask-previews/network-enablement-controller 0.1.0-preview-037d305

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +20 -0
  3. package/README.md +182 -0
  4. package/dist/NetworkEnablementController.cjs +178 -0
  5. package/dist/NetworkEnablementController.cjs.map +1 -0
  6. package/dist/NetworkEnablementController.d.cts +102 -0
  7. package/dist/NetworkEnablementController.d.cts.map +1 -0
  8. package/dist/NetworkEnablementController.d.mts +102 -0
  9. package/dist/NetworkEnablementController.d.mts.map +1 -0
  10. package/dist/NetworkEnablementController.mjs +174 -0
  11. package/dist/NetworkEnablementController.mjs.map +1 -0
  12. package/dist/constants.cjs +16 -0
  13. package/dist/constants.cjs.map +1 -0
  14. package/dist/constants.d.cts +2 -0
  15. package/dist/constants.d.cts.map +1 -0
  16. package/dist/constants.d.mts +2 -0
  17. package/dist/constants.d.mts.map +1 -0
  18. package/dist/constants.mjs +13 -0
  19. package/dist/constants.mjs.map +1 -0
  20. package/dist/index.cjs +14 -0
  21. package/dist/index.cjs.map +1 -0
  22. package/dist/index.d.cts +4 -0
  23. package/dist/index.d.cts.map +1 -0
  24. package/dist/index.d.mts +4 -0
  25. package/dist/index.d.mts.map +1 -0
  26. package/dist/index.mjs +3 -0
  27. package/dist/index.mjs.map +1 -0
  28. package/dist/selectors.cjs +87 -0
  29. package/dist/selectors.cjs.map +1 -0
  30. package/dist/selectors.d.cts +280 -0
  31. package/dist/selectors.d.cts.map +1 -0
  32. package/dist/selectors.d.mts +280 -0
  33. package/dist/selectors.d.mts.map +1 -0
  34. package/dist/selectors.mjs +81 -0
  35. package/dist/selectors.mjs.map +1 -0
  36. package/dist/types.cjs +13 -0
  37. package/dist/types.cjs.map +1 -0
  38. package/dist/types.d.cts +9 -0
  39. package/dist/types.d.cts.map +1 -0
  40. package/dist/types.d.mts +9 -0
  41. package/dist/types.d.mts.map +1 -0
  42. package/dist/types.mjs +10 -0
  43. package/dist/types.mjs.map +1 -0
  44. package/dist/utils.cjs +80 -0
  45. package/dist/utils.cjs.map +1 -0
  46. package/dist/utils.d.cts +43 -0
  47. package/dist/utils.d.cts.map +1 -0
  48. package/dist/utils.d.mts +43 -0
  49. package/dist/utils.d.mts.map +1 -0
  50. package/dist/utils.mjs +74 -0
  51. package/dist/utils.mjs.map +1 -0
  52. package/package.json +79 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.cjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,IAAY,QAIX;AAJD,WAAY,QAAQ;IAClB,8DAAkD,CAAA;IAClD,+DAAmD,CAAA;IACnD,+DAAmD,CAAA;AACrD,CAAC,EAJW,QAAQ,wBAAR,QAAQ,QAInB","sourcesContent":["/**\n * Scopes for Solana account type. See {@link KeyringAccount.scopes}.\n */\nexport enum SolScope {\n Devnet = 'solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1',\n Mainnet = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',\n Testnet = 'solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z',\n}\n"]}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Scopes for Solana account type. See {@link KeyringAccount.scopes}.
3
+ */
4
+ export declare enum SolScope {
5
+ Devnet = "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1",
6
+ Mainnet = "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
7
+ Testnet = "solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z"
8
+ }
9
+ //# sourceMappingURL=types.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.cts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,oBAAY,QAAQ;IAClB,MAAM,4CAA4C;IAClD,OAAO,4CAA4C;IACnD,OAAO,4CAA4C;CACpD"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Scopes for Solana account type. See {@link KeyringAccount.scopes}.
3
+ */
4
+ export declare enum SolScope {
5
+ Devnet = "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1",
6
+ Mainnet = "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
7
+ Testnet = "solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z"
8
+ }
9
+ //# sourceMappingURL=types.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.mts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,oBAAY,QAAQ;IAClB,MAAM,4CAA4C;IAClD,OAAO,4CAA4C;IACnD,OAAO,4CAA4C;CACpD"}
package/dist/types.mjs ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Scopes for Solana account type. See {@link KeyringAccount.scopes}.
3
+ */
4
+ export var SolScope;
5
+ (function (SolScope) {
6
+ SolScope["Devnet"] = "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1";
7
+ SolScope["Mainnet"] = "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp";
8
+ SolScope["Testnet"] = "solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z";
9
+ })(SolScope || (SolScope = {}));
10
+ //# sourceMappingURL=types.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.mjs","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,CAAN,IAAY,QAIX;AAJD,WAAY,QAAQ;IAClB,8DAAkD,CAAA;IAClD,+DAAmD,CAAA;IACnD,+DAAmD,CAAA;AACrD,CAAC,EAJW,QAAQ,KAAR,QAAQ,QAInB","sourcesContent":["/**\n * Scopes for Solana account type. See {@link KeyringAccount.scopes}.\n */\nexport enum SolScope {\n Devnet = 'solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1',\n Mainnet = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',\n Testnet = 'solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z',\n}\n"]}
package/dist/utils.cjs ADDED
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isPopularNetwork = exports.isOnlyNetworkEnabledInNamespace = exports.deriveKeys = void 0;
4
+ const controller_utils_1 = require("@metamask/controller-utils");
5
+ const multichain_network_controller_1 = require("@metamask/multichain-network-controller");
6
+ const utils_1 = require("@metamask/utils");
7
+ const constants_1 = require("./constants.cjs");
8
+ /**
9
+ * Derives the namespace, storage key, and CAIP chain ID from a given chain ID.
10
+ *
11
+ * This utility function handles the conversion between different chain ID formats.
12
+ * For EVM networks, it converts Hex chain IDs to CAIP-2 format and determines
13
+ * the appropriate storage key. For non-EVM networks, it parses the CAIP-2 chain ID
14
+ * and uses the full chain ID as the storage key.
15
+ *
16
+ * @param chainId - The chain ID to derive keys from (Hex or CAIP-2 format)
17
+ * @returns An object containing namespace, storageKey, and caipId
18
+ * @throws Error if the chain ID cannot be parsed
19
+ */
20
+ function deriveKeys(chainId) {
21
+ const caipChainId = (0, utils_1.isCaipChainId)(chainId)
22
+ ? chainId
23
+ : (0, multichain_network_controller_1.toEvmCaipChainId)(chainId);
24
+ const { namespace, reference } = (0, utils_1.parseCaipChainId)(caipChainId);
25
+ let storageKey;
26
+ if (namespace === utils_1.KnownCaipNamespace.Eip155) {
27
+ storageKey = (0, utils_1.isHexString)(chainId) ? chainId : (0, controller_utils_1.toHex)(reference);
28
+ }
29
+ else {
30
+ storageKey = caipChainId;
31
+ }
32
+ return { namespace, storageKey, caipChainId, reference };
33
+ }
34
+ exports.deriveKeys = deriveKeys;
35
+ /**
36
+ * Checks if the specified network is the only enabled network in its namespace.
37
+ *
38
+ * This function is used to prevent unnecessary state updates when trying to enable
39
+ * This method is used to prevent the last network in a namespace from being removed.
40
+ *
41
+ * @param state - The current controller state
42
+ * @param derivedKeys - The parsed keys object containing namespace and storageKey
43
+ * @returns True if the network is the only enabled network in the namespace, false otherwise
44
+ */
45
+ function isOnlyNetworkEnabledInNamespace(state, derivedKeys) {
46
+ const { namespace, storageKey } = derivedKeys;
47
+ // Early return if namespace doesn't exist
48
+ if (!state.enabledNetworkMap[namespace]) {
49
+ return false;
50
+ }
51
+ const networks = state.enabledNetworkMap[namespace];
52
+ // Get all enabled networks in this namespace
53
+ const enabledNetworks = Object.entries(networks).filter(([_, enabled]) => enabled);
54
+ // Check if there's exactly one enabled network and it matches our target
55
+ if (enabledNetworks.length === 1) {
56
+ const [onlyEnabledKey] = enabledNetworks[0];
57
+ return onlyEnabledKey === storageKey;
58
+ }
59
+ // Return false if there are zero or multiple enabled networks
60
+ return false;
61
+ }
62
+ exports.isOnlyNetworkEnabledInNamespace = isOnlyNetworkEnabledInNamespace;
63
+ /**
64
+ * Checks if a network is considered popular based on its reference.
65
+ *
66
+ * @param reference - The network reference (typically the chain ID reference part)
67
+ * @returns True if the network is popular, false otherwise
68
+ */
69
+ function isPopularNetwork(reference) {
70
+ try {
71
+ return constants_1.POPULAR_NETWORKS.includes((0, controller_utils_1.toHex)(reference));
72
+ }
73
+ catch {
74
+ // If toHex fails (e.g., for non-decimal references like Bitcoin hashes),
75
+ // the network is not popular
76
+ return false;
77
+ }
78
+ }
79
+ exports.isPopularNetwork = isPopularNetwork;
80
+ //# sourceMappingURL=utils.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAAA,iEAAmD;AACnD,2FAA2E;AAE3E,2CAKyB;AAEzB,+CAA+C;AAa/C;;;;;;;;;;;GAWG;AACH,SAAgB,UAAU,CAAC,OAA0B;IACnD,MAAM,WAAW,GAAG,IAAA,qBAAa,EAAC,OAAO,CAAC;QACxC,CAAC,CAAC,OAAO;QACT,CAAC,CAAC,IAAA,gDAAgB,EAAC,OAAO,CAAC,CAAC;IAE9B,MAAM,EAAE,SAAS,EA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K,EAAC,SAAS,CAAC,CAAC,CAAC;KACpD;IAAC,MAAM;QACN,yEAAyE;QACzE,6BAA6B;QAC7B,OAAO,KAAK,CAAC;KACd;AACH,CAAC;AARD,4CAQC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport { toEvmCaipChainId } from '@metamask/multichain-network-controller';\nimport type { CaipChainId, CaipNamespace, Hex } from '@metamask/utils';\nimport {\n isCaipChainId,\n isHexString,\n KnownCaipNamespace,\n parseCaipChainId,\n} from '@metamask/utils';\n\nimport { POPULAR_NETWORKS } from './constants';\nimport type { NetworkEnablementControllerState } from './NetworkEnablementController';\n\n/**\n * Represents the parsed keys derived from a chain ID.\n */\nexport type DerivedKeys = {\n namespace: CaipNamespace;\n storageKey: Hex | CaipChainId;\n caipChainId: CaipChainId;\n reference: string;\n};\n\n/**\n * Derives the namespace, storage key, and CAIP chain ID from a given chain ID.\n *\n * This utility function handles the conversion between different chain ID formats.\n * For EVM networks, it converts Hex chain IDs to CAIP-2 format and determines\n * the appropriate storage key. For non-EVM networks, it parses the CAIP-2 chain ID\n * and uses the full chain ID as the storage key.\n *\n * @param chainId - The chain ID to derive keys from (Hex or CAIP-2 format)\n * @returns An object containing namespace, storageKey, and caipId\n * @throws Error if the chain ID cannot be parsed\n */\nexport function deriveKeys(chainId: Hex | CaipChainId): DerivedKeys {\n const caipChainId = isCaipChainId(chainId)\n ? chainId\n : toEvmCaipChainId(chainId);\n\n const { namespace, reference } = parseCaipChainId(caipChainId);\n let storageKey;\n if (namespace === (KnownCaipNamespace.Eip155 as string)) {\n storageKey = isHexString(chainId) ? chainId : toHex(reference);\n } else {\n storageKey = caipChainId;\n }\n return { namespace, storageKey, caipChainId, reference };\n}\n\n/**\n * Checks if the specified network is the only enabled network in its namespace.\n *\n * This function is used to prevent unnecessary state updates when trying to enable\n * This method is used to prevent the last network in a namespace from being removed.\n *\n * @param state - The current controller state\n * @param derivedKeys - The parsed keys object containing namespace and storageKey\n * @returns True if the network is the only enabled network in the namespace, false otherwise\n */\nexport function isOnlyNetworkEnabledInNamespace(\n state: NetworkEnablementControllerState,\n derivedKeys: DerivedKeys,\n): boolean {\n const { namespace, storageKey } = derivedKeys;\n\n // Early return if namespace doesn't exist\n if (!state.enabledNetworkMap[namespace]) {\n return false;\n }\n\n const networks = state.enabledNetworkMap[namespace];\n\n // Get all enabled networks in this namespace\n const enabledNetworks = Object.entries(networks).filter(\n ([_, enabled]) => enabled,\n );\n\n // Check if there's exactly one enabled network and it matches our target\n if (enabledNetworks.length === 1) {\n const [onlyEnabledKey] = enabledNetworks[0];\n return onlyEnabledKey === storageKey;\n }\n\n // Return false if there are zero or multiple enabled networks\n return false;\n}\n\n/**\n * Checks if a network is considered popular based on its reference.\n *\n * @param reference - The network reference (typically the chain ID reference part)\n * @returns True if the network is popular, false otherwise\n */\nexport function isPopularNetwork(reference: string): boolean {\n try {\n return POPULAR_NETWORKS.includes(toHex(reference));\n } catch {\n // If toHex fails (e.g., for non-decimal references like Bitcoin hashes),\n // the network is not popular\n return false;\n }\n}\n"]}
@@ -0,0 +1,43 @@
1
+ import type { CaipChainId, CaipNamespace, Hex } from "@metamask/utils";
2
+ import type { NetworkEnablementControllerState } from "./NetworkEnablementController.cjs";
3
+ /**
4
+ * Represents the parsed keys derived from a chain ID.
5
+ */
6
+ export type DerivedKeys = {
7
+ namespace: CaipNamespace;
8
+ storageKey: Hex | CaipChainId;
9
+ caipChainId: CaipChainId;
10
+ reference: string;
11
+ };
12
+ /**
13
+ * Derives the namespace, storage key, and CAIP chain ID from a given chain ID.
14
+ *
15
+ * This utility function handles the conversion between different chain ID formats.
16
+ * For EVM networks, it converts Hex chain IDs to CAIP-2 format and determines
17
+ * the appropriate storage key. For non-EVM networks, it parses the CAIP-2 chain ID
18
+ * and uses the full chain ID as the storage key.
19
+ *
20
+ * @param chainId - The chain ID to derive keys from (Hex or CAIP-2 format)
21
+ * @returns An object containing namespace, storageKey, and caipId
22
+ * @throws Error if the chain ID cannot be parsed
23
+ */
24
+ export declare function deriveKeys(chainId: Hex | CaipChainId): DerivedKeys;
25
+ /**
26
+ * Checks if the specified network is the only enabled network in its namespace.
27
+ *
28
+ * This function is used to prevent unnecessary state updates when trying to enable
29
+ * This method is used to prevent the last network in a namespace from being removed.
30
+ *
31
+ * @param state - The current controller state
32
+ * @param derivedKeys - The parsed keys object containing namespace and storageKey
33
+ * @returns True if the network is the only enabled network in the namespace, false otherwise
34
+ */
35
+ export declare function isOnlyNetworkEnabledInNamespace(state: NetworkEnablementControllerState, derivedKeys: DerivedKeys): boolean;
36
+ /**
37
+ * Checks if a network is considered popular based on its reference.
38
+ *
39
+ * @param reference - The network reference (typically the chain ID reference part)
40
+ * @returns True if the network is popular, false otherwise
41
+ */
42
+ export declare function isPopularNetwork(reference: string): boolean;
43
+ //# sourceMappingURL=utils.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.cts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,GAAG,EAAE,wBAAwB;AASvE,OAAO,KAAK,EAAE,gCAAgC,EAAE,0CAAsC;AAEtF;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,SAAS,EAAE,aAAa,CAAC;IACzB,UAAU,EAAE,GAAG,GAAG,WAAW,CAAC;IAC9B,WAAW,EAAE,WAAW,CAAC;IACzB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,GAAG,GAAG,WAAW,GAAG,WAAW,CAalE;AAED;;;;;;;;;GASG;AACH,wBAAgB,+BAA+B,CAC7C,KAAK,EAAE,gCAAgC,EACvC,WAAW,EAAE,WAAW,GACvB,OAAO,CAuBT;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAQ3D"}
@@ -0,0 +1,43 @@
1
+ import type { CaipChainId, CaipNamespace, Hex } from "@metamask/utils";
2
+ import type { NetworkEnablementControllerState } from "./NetworkEnablementController.mjs";
3
+ /**
4
+ * Represents the parsed keys derived from a chain ID.
5
+ */
6
+ export type DerivedKeys = {
7
+ namespace: CaipNamespace;
8
+ storageKey: Hex | CaipChainId;
9
+ caipChainId: CaipChainId;
10
+ reference: string;
11
+ };
12
+ /**
13
+ * Derives the namespace, storage key, and CAIP chain ID from a given chain ID.
14
+ *
15
+ * This utility function handles the conversion between different chain ID formats.
16
+ * For EVM networks, it converts Hex chain IDs to CAIP-2 format and determines
17
+ * the appropriate storage key. For non-EVM networks, it parses the CAIP-2 chain ID
18
+ * and uses the full chain ID as the storage key.
19
+ *
20
+ * @param chainId - The chain ID to derive keys from (Hex or CAIP-2 format)
21
+ * @returns An object containing namespace, storageKey, and caipId
22
+ * @throws Error if the chain ID cannot be parsed
23
+ */
24
+ export declare function deriveKeys(chainId: Hex | CaipChainId): DerivedKeys;
25
+ /**
26
+ * Checks if the specified network is the only enabled network in its namespace.
27
+ *
28
+ * This function is used to prevent unnecessary state updates when trying to enable
29
+ * This method is used to prevent the last network in a namespace from being removed.
30
+ *
31
+ * @param state - The current controller state
32
+ * @param derivedKeys - The parsed keys object containing namespace and storageKey
33
+ * @returns True if the network is the only enabled network in the namespace, false otherwise
34
+ */
35
+ export declare function isOnlyNetworkEnabledInNamespace(state: NetworkEnablementControllerState, derivedKeys: DerivedKeys): boolean;
36
+ /**
37
+ * Checks if a network is considered popular based on its reference.
38
+ *
39
+ * @param reference - The network reference (typically the chain ID reference part)
40
+ * @returns True if the network is popular, false otherwise
41
+ */
42
+ export declare function isPopularNetwork(reference: string): boolean;
43
+ //# sourceMappingURL=utils.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.mts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,GAAG,EAAE,wBAAwB;AASvE,OAAO,KAAK,EAAE,gCAAgC,EAAE,0CAAsC;AAEtF;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,SAAS,EAAE,aAAa,CAAC;IACzB,UAAU,EAAE,GAAG,GAAG,WAAW,CAAC;IAC9B,WAAW,EAAE,WAAW,CAAC;IACzB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,wBAAgB,UAAU,CAAC,OAAO,EAAE,GAAG,GAAG,WAAW,GAAG,WAAW,CAalE;AAED;;;;;;;;;GASG;AACH,wBAAgB,+BAA+B,CAC7C,KAAK,EAAE,gCAAgC,EACvC,WAAW,EAAE,WAAW,GACvB,OAAO,CAuBT;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAQ3D"}
package/dist/utils.mjs ADDED
@@ -0,0 +1,74 @@
1
+ import { toHex } from "@metamask/controller-utils";
2
+ import { toEvmCaipChainId } from "@metamask/multichain-network-controller";
3
+ import { isCaipChainId, isHexString, KnownCaipNamespace, parseCaipChainId } from "@metamask/utils";
4
+ import { POPULAR_NETWORKS } from "./constants.mjs";
5
+ /**
6
+ * Derives the namespace, storage key, and CAIP chain ID from a given chain ID.
7
+ *
8
+ * This utility function handles the conversion between different chain ID formats.
9
+ * For EVM networks, it converts Hex chain IDs to CAIP-2 format and determines
10
+ * the appropriate storage key. For non-EVM networks, it parses the CAIP-2 chain ID
11
+ * and uses the full chain ID as the storage key.
12
+ *
13
+ * @param chainId - The chain ID to derive keys from (Hex or CAIP-2 format)
14
+ * @returns An object containing namespace, storageKey, and caipId
15
+ * @throws Error if the chain ID cannot be parsed
16
+ */
17
+ export function deriveKeys(chainId) {
18
+ const caipChainId = isCaipChainId(chainId)
19
+ ? chainId
20
+ : toEvmCaipChainId(chainId);
21
+ const { namespace, reference } = parseCaipChainId(caipChainId);
22
+ let storageKey;
23
+ if (namespace === KnownCaipNamespace.Eip155) {
24
+ storageKey = isHexString(chainId) ? chainId : toHex(reference);
25
+ }
26
+ else {
27
+ storageKey = caipChainId;
28
+ }
29
+ return { namespace, storageKey, caipChainId, reference };
30
+ }
31
+ /**
32
+ * Checks if the specified network is the only enabled network in its namespace.
33
+ *
34
+ * This function is used to prevent unnecessary state updates when trying to enable
35
+ * This method is used to prevent the last network in a namespace from being removed.
36
+ *
37
+ * @param state - The current controller state
38
+ * @param derivedKeys - The parsed keys object containing namespace and storageKey
39
+ * @returns True if the network is the only enabled network in the namespace, false otherwise
40
+ */
41
+ export function isOnlyNetworkEnabledInNamespace(state, derivedKeys) {
42
+ const { namespace, storageKey } = derivedKeys;
43
+ // Early return if namespace doesn't exist
44
+ if (!state.enabledNetworkMap[namespace]) {
45
+ return false;
46
+ }
47
+ const networks = state.enabledNetworkMap[namespace];
48
+ // Get all enabled networks in this namespace
49
+ const enabledNetworks = Object.entries(networks).filter(([_, enabled]) => enabled);
50
+ // Check if there's exactly one enabled network and it matches our target
51
+ if (enabledNetworks.length === 1) {
52
+ const [onlyEnabledKey] = enabledNetworks[0];
53
+ return onlyEnabledKey === storageKey;
54
+ }
55
+ // Return false if there are zero or multiple enabled networks
56
+ return false;
57
+ }
58
+ /**
59
+ * Checks if a network is considered popular based on its reference.
60
+ *
61
+ * @param reference - The network reference (typically the chain ID reference part)
62
+ * @returns True if the network is popular, false otherwise
63
+ */
64
+ export function isPopularNetwork(reference) {
65
+ try {
66
+ return POPULAR_NETWORKS.includes(toHex(reference));
67
+ }
68
+ catch {
69
+ // If toHex fails (e.g., for non-decimal references like Bitcoin hashes),
70
+ // the network is not popular
71
+ return false;
72
+ }
73
+ }
74
+ //# sourceMappingURL=utils.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,mCAAmC;AACnD,OAAO,EAAE,gBAAgB,EAAE,gDAAgD;AAE3E,OAAO,EACL,aAAa,EACb,WAAW,EAC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c,CAAC,GAAG,eAAe,CAAC,CAAC,CAAC,CAAC;QAC5C,OAAO,cAAc,KAAK,UAAU,CAAC;KACtC;IAED,8DAA8D;IAC9D,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,gBAAgB,CAAC,SAAiB;IAChD,IAAI;QACF,OAAO,gBAAgB,CAAC,QAAQ,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC;KACpD;IAAC,MAAM;QACN,yEAAyE;QACzE,6BAA6B;QAC7B,OAAO,KAAK,CAAC;KACd;AACH,CAAC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport { toEvmCaipChainId } from '@metamask/multichain-network-controller';\nimport type { CaipChainId, CaipNamespace, Hex } from '@metamask/utils';\nimport {\n isCaipChainId,\n isHexString,\n KnownCaipNamespace,\n parseCaipChainId,\n} from '@metamask/utils';\n\nimport { POPULAR_NETWORKS } from './constants';\nimport type { NetworkEnablementControllerState } from './NetworkEnablementController';\n\n/**\n * Represents the parsed keys derived from a chain ID.\n */\nexport type DerivedKeys = {\n namespace: CaipNamespace;\n storageKey: Hex | CaipChainId;\n caipChainId: CaipChainId;\n reference: string;\n};\n\n/**\n * Derives the namespace, storage key, and CAIP chain ID from a given chain ID.\n *\n * This utility function handles the conversion between different chain ID formats.\n * For EVM networks, it converts Hex chain IDs to CAIP-2 format and determines\n * the appropriate storage key. For non-EVM networks, it parses the CAIP-2 chain ID\n * and uses the full chain ID as the storage key.\n *\n * @param chainId - The chain ID to derive keys from (Hex or CAIP-2 format)\n * @returns An object containing namespace, storageKey, and caipId\n * @throws Error if the chain ID cannot be parsed\n */\nexport function deriveKeys(chainId: Hex | CaipChainId): DerivedKeys {\n const caipChainId = isCaipChainId(chainId)\n ? chainId\n : toEvmCaipChainId(chainId);\n\n const { namespace, reference } = parseCaipChainId(caipChainId);\n let storageKey;\n if (namespace === (KnownCaipNamespace.Eip155 as string)) {\n storageKey = isHexString(chainId) ? chainId : toHex(reference);\n } else {\n storageKey = caipChainId;\n }\n return { namespace, storageKey, caipChainId, reference };\n}\n\n/**\n * Checks if the specified network is the only enabled network in its namespace.\n *\n * This function is used to prevent unnecessary state updates when trying to enable\n * This method is used to prevent the last network in a namespace from being removed.\n *\n * @param state - The current controller state\n * @param derivedKeys - The parsed keys object containing namespace and storageKey\n * @returns True if the network is the only enabled network in the namespace, false otherwise\n */\nexport function isOnlyNetworkEnabledInNamespace(\n state: NetworkEnablementControllerState,\n derivedKeys: DerivedKeys,\n): boolean {\n const { namespace, storageKey } = derivedKeys;\n\n // Early return if namespace doesn't exist\n if (!state.enabledNetworkMap[namespace]) {\n return false;\n }\n\n const networks = state.enabledNetworkMap[namespace];\n\n // Get all enabled networks in this namespace\n const enabledNetworks = Object.entries(networks).filter(\n ([_, enabled]) => enabled,\n );\n\n // Check if there's exactly one enabled network and it matches our target\n if (enabledNetworks.length === 1) {\n const [onlyEnabledKey] = enabledNetworks[0];\n return onlyEnabledKey === storageKey;\n }\n\n // Return false if there are zero or multiple enabled networks\n return false;\n}\n\n/**\n * Checks if a network is considered popular based on its reference.\n *\n * @param reference - The network reference (typically the chain ID reference part)\n * @returns True if the network is popular, false otherwise\n */\nexport function isPopularNetwork(reference: string): boolean {\n try {\n return POPULAR_NETWORKS.includes(toHex(reference));\n } catch {\n // If toHex fails (e.g., for non-decimal references like Bitcoin hashes),\n // the network is not popular\n return false;\n }\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,79 @@
1
+ {
2
+ "name": "@metamask-previews/network-enablement-controller",
3
+ "version": "0.1.0-preview-037d305",
4
+ "description": "Provides an interface to the currently enabled network using a MetaMask-compatible provider object",
5
+ "keywords": [
6
+ "MetaMask",
7
+ "Ethereum"
8
+ ],
9
+ "homepage": "https://github.com/MetaMask/core/tree/main/packages/network-enablement-controller#readme",
10
+ "bugs": {
11
+ "url": "https://github.com/MetaMask/core/issues"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/MetaMask/core.git"
16
+ },
17
+ "license": "MIT",
18
+ "sideEffects": false,
19
+ "exports": {
20
+ ".": {
21
+ "import": {
22
+ "types": "./dist/index.d.mts",
23
+ "default": "./dist/index.mjs"
24
+ },
25
+ "require": {
26
+ "types": "./dist/index.d.cts",
27
+ "default": "./dist/index.cjs"
28
+ }
29
+ },
30
+ "./package.json": "./package.json"
31
+ },
32
+ "main": "./dist/index.cjs",
33
+ "types": "./dist/index.d.cts",
34
+ "files": [
35
+ "dist/"
36
+ ],
37
+ "scripts": {
38
+ "build": "ts-bridge --project tsconfig.build.json --verbose --clean --no-references",
39
+ "build:docs": "typedoc",
40
+ "changelog:update": "../../scripts/update-changelog.sh @metamask/network-enablement-controller",
41
+ "changelog:validate": "../../scripts/validate-changelog.sh @metamask/network-enablement-controller",
42
+ "publish:preview": "yarn npm publish --tag preview",
43
+ "since-latest-release": "../../scripts/since-latest-release.sh",
44
+ "test": "NODE_OPTIONS=--experimental-vm-modules jest --reporters=jest-silent-reporter",
45
+ "test:clean": "NODE_OPTIONS=--experimental-vm-modules jest --clearCache",
46
+ "test:verbose": "NODE_OPTIONS=--experimental-vm-modules jest --verbose",
47
+ "test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --watch"
48
+ },
49
+ "devDependencies": {
50
+ "@metamask/auto-changelog": "^3.4.4",
51
+ "@metamask/multichain-network-controller": "^0.11.0",
52
+ "@metamask/network-controller": "^24.0.1",
53
+ "@types/jest": "^27.4.1",
54
+ "deepmerge": "^4.2.2",
55
+ "jest": "^27.5.1",
56
+ "sinon": "^9.2.4",
57
+ "ts-jest": "^27.1.4",
58
+ "typedoc": "^0.24.8",
59
+ "typedoc-plugin-missing-exports": "^2.0.0",
60
+ "typescript": "~5.2.2"
61
+ },
62
+ "dependencies": {
63
+ "@metamask/base-controller": "^8.0.1",
64
+ "@metamask/controller-utils": "^11.11.0",
65
+ "@metamask/utils": "^11.4.2",
66
+ "reselect": "^5.1.1"
67
+ },
68
+ "peerDependencies": {
69
+ "@metamask/multichain-network-controller": "^0.11.0",
70
+ "@metamask/network-controller": "^24.0.0"
71
+ },
72
+ "engines": {
73
+ "node": "^18.18 || >=20"
74
+ },
75
+ "publishConfig": {
76
+ "access": "public",
77
+ "registry": "https://registry.npmjs.org/"
78
+ }
79
+ }