@metamask-previews/network-enablement-controller 4.0.0-preview-009e026d → 4.0.0-preview-f8cdcfc2
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.
- package/CHANGELOG.md +7 -0
- package/dist/NetworkEnablementController.cjs +188 -59
- package/dist/NetworkEnablementController.cjs.map +1 -1
- package/dist/NetworkEnablementController.d.cts +52 -2
- package/dist/NetworkEnablementController.d.cts.map +1 -1
- package/dist/NetworkEnablementController.d.mts +52 -2
- package/dist/NetworkEnablementController.d.mts.map +1 -1
- package/dist/NetworkEnablementController.mjs +188 -59
- package/dist/NetworkEnablementController.mjs.map +1 -1
- package/dist/index.cjs +5 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/dist/services/Slip44Service.cjs +176 -0
- package/dist/services/Slip44Service.cjs.map +1 -0
- package/dist/services/Slip44Service.d.cts +74 -0
- package/dist/services/Slip44Service.d.cts.map +1 -0
- package/dist/services/Slip44Service.d.mts +74 -0
- package/dist/services/Slip44Service.d.mts.map +1 -0
- package/dist/services/Slip44Service.mjs +169 -0
- package/dist/services/Slip44Service.mjs.map +1 -0
- package/dist/services/index.cjs +9 -0
- package/dist/services/index.cjs.map +1 -0
- package/dist/services/index.d.cts +6 -0
- package/dist/services/index.d.cts.map +1 -0
- package/dist/services/index.d.mts +6 -0
- package/dist/services/index.d.mts.map +1 -0
- package/dist/services/index.mjs +6 -0
- package/dist/services/index.mjs.map +1 -0
- package/package.json +2 -1
|
@@ -21,8 +21,26 @@ export type NetworksInfo = {
|
|
|
21
21
|
* For other networks, the keys are CAIP chain IDs.
|
|
22
22
|
*/
|
|
23
23
|
type EnabledMap = Record<CaipNamespace, Record<CaipChainId | Hex, boolean>>;
|
|
24
|
+
/**
|
|
25
|
+
* A native asset identifier in CAIP-19-like format.
|
|
26
|
+
* Format: `{caip2ChainId}/slip44:{coinType}`
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* - `eip155:1/slip44:60` for Ethereum mainnet (ETH)
|
|
30
|
+
* - `solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501` for Solana mainnet (SOL)
|
|
31
|
+
* - `bip122:000000000019d6689c085ae165831e93/slip44:0` for Bitcoin mainnet (BTC)
|
|
32
|
+
*/
|
|
33
|
+
export type NativeAssetIdentifier = `${CaipChainId}/slip44:${number}`;
|
|
34
|
+
/**
|
|
35
|
+
* A map of CAIP-2 chain IDs to their native asset identifiers.
|
|
36
|
+
* Uses CAIP-19-like format to identify the native asset for each chain.
|
|
37
|
+
*
|
|
38
|
+
* @see https://github.com/satoshilabs/slips/blob/master/slip-0044.md
|
|
39
|
+
*/
|
|
40
|
+
export type NativeAssetIdentifiersMap = Record<CaipChainId, NativeAssetIdentifier>;
|
|
24
41
|
export type NetworkEnablementControllerState = {
|
|
25
42
|
enabledNetworkMap: EnabledMap;
|
|
43
|
+
nativeAssetIdentifiers: NativeAssetIdentifiersMap;
|
|
26
44
|
};
|
|
27
45
|
export type NetworkEnablementControllerGetStateAction = ControllerGetStateAction<typeof controllerName, NetworkEnablementControllerState>;
|
|
28
46
|
export type NetworkEnablementControllerSetEnabledNetworksAction = {
|
|
@@ -45,6 +63,14 @@ export type NetworkEnablementControllerEvents = NetworkEnablementControllerState
|
|
|
45
63
|
*/
|
|
46
64
|
export type AllowedEvents = NetworkControllerNetworkAddedEvent | NetworkControllerNetworkRemovedEvent | NetworkControllerStateChangeEvent | TransactionControllerTransactionSubmittedEvent;
|
|
47
65
|
export type NetworkEnablementControllerMessenger = Messenger<typeof controllerName, NetworkEnablementControllerActions | AllowedActions, NetworkEnablementControllerEvents | AllowedEvents>;
|
|
66
|
+
/**
|
|
67
|
+
* Network configuration with chain ID and native currency symbol.
|
|
68
|
+
* Used to initialize native asset identifiers.
|
|
69
|
+
*/
|
|
70
|
+
export type NetworkConfig = {
|
|
71
|
+
chainId: CaipChainId;
|
|
72
|
+
nativeCurrency: string;
|
|
73
|
+
};
|
|
48
74
|
/**
|
|
49
75
|
* Controller responsible for managing network enablement state across different blockchain networks.
|
|
50
76
|
*
|
|
@@ -116,14 +142,38 @@ export declare class NetworkEnablementController extends BaseController<typeof c
|
|
|
116
142
|
* Initializes the network enablement state from network controller configurations.
|
|
117
143
|
*
|
|
118
144
|
* This method reads the current network configurations from both NetworkController
|
|
119
|
-
* and MultichainNetworkController and syncs the enabled network map accordingly.
|
|
145
|
+
* and MultichainNetworkController and syncs the enabled network map and nativeAssetIdentifiers accordingly.
|
|
120
146
|
* It ensures proper namespace buckets exist for all configured networks and only
|
|
121
147
|
* adds missing networks with a default value of false, preserving existing user settings.
|
|
122
148
|
*
|
|
123
149
|
* This method should be called after the NetworkController and MultichainNetworkController
|
|
124
150
|
* have been initialized and their configurations are available.
|
|
125
151
|
*/
|
|
126
|
-
init(): void
|
|
152
|
+
init(): Promise<void>;
|
|
153
|
+
/**
|
|
154
|
+
* Initializes the native asset identifiers from network configurations.
|
|
155
|
+
* This method should be called from the client during controller initialization
|
|
156
|
+
* to populate the nativeAssetIdentifiers state based on actual network configurations.
|
|
157
|
+
*
|
|
158
|
+
* @param networks - Array of network configurations with chainId and nativeCurrency
|
|
159
|
+
* @example
|
|
160
|
+
* ```typescript
|
|
161
|
+
* const evmNetworks = Object.values(networkControllerState.networkConfigurationsByChainId)
|
|
162
|
+
* .map(config => ({
|
|
163
|
+
* chainId: toEvmCaipChainId(config.chainId),
|
|
164
|
+
* nativeCurrency: config.nativeCurrency,
|
|
165
|
+
* }));
|
|
166
|
+
*
|
|
167
|
+
* const multichainNetworks = Object.values(multichainState.multichainNetworkConfigurationsByChainId)
|
|
168
|
+
* .map(config => ({
|
|
169
|
+
* chainId: config.chainId,
|
|
170
|
+
* nativeCurrency: config.nativeCurrency,
|
|
171
|
+
* }));
|
|
172
|
+
*
|
|
173
|
+
* await controller.initNativeAssetIdentifiers([...evmNetworks, ...multichainNetworks]);
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
initNativeAssetIdentifiers(networks: NetworkConfig[]): Promise<void>;
|
|
127
177
|
/**
|
|
128
178
|
* Disables a network for the user.
|
|
129
179
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NetworkEnablementController.d.cts","sourceRoot":"","sources":["../src/NetworkEnablementController.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC3B,kCAAkC;AAGnC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,yCAAyC,EAAE,gDAAgD;AACzG,OAAO,KAAK,EACV,+BAA+B,EAC/B,kCAAkC,EAClC,oCAAoC,EACpC,iCAAiC,EAClC,qCAAqC;AACtC,OAAO,KAAK,EAAE,8CAA8C,EAAE,yCAAyC;AACvG,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,GAAG,EAAE,wBAAwB;
|
|
1
|
+
{"version":3,"file":"NetworkEnablementController.d.cts","sourceRoot":"","sources":["../src/NetworkEnablementController.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC3B,kCAAkC;AAGnC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,yCAAyC,EAAE,gDAAgD;AACzG,OAAO,KAAK,EACV,+BAA+B,EAC/B,kCAAkC,EAClC,oCAAoC,EACpC,iCAAiC,EAClC,qCAAqC;AACtC,OAAO,KAAK,EAAE,8CAA8C,EAAE,yCAAyC;AACvG,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,GAAG,EAAE,wBAAwB;AAWvE,QAAA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o = {
|
|
|
21
21
|
* For other networks, the keys are CAIP chain IDs.
|
|
22
22
|
*/
|
|
23
23
|
type EnabledMap = Record<CaipNamespace, Record<CaipChainId | Hex, boolean>>;
|
|
24
|
+
/**
|
|
25
|
+
* A native asset identifier in CAIP-19-like format.
|
|
26
|
+
* Format: `{caip2ChainId}/slip44:{coinType}`
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* - `eip155:1/slip44:60` for Ethereum mainnet (ETH)
|
|
30
|
+
* - `solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp/slip44:501` for Solana mainnet (SOL)
|
|
31
|
+
* - `bip122:000000000019d6689c085ae165831e93/slip44:0` for Bitcoin mainnet (BTC)
|
|
32
|
+
*/
|
|
33
|
+
export type NativeAssetIdentifier = `${CaipChainId}/slip44:${number}`;
|
|
34
|
+
/**
|
|
35
|
+
* A map of CAIP-2 chain IDs to their native asset identifiers.
|
|
36
|
+
* Uses CAIP-19-like format to identify the native asset for each chain.
|
|
37
|
+
*
|
|
38
|
+
* @see https://github.com/satoshilabs/slips/blob/master/slip-0044.md
|
|
39
|
+
*/
|
|
40
|
+
export type NativeAssetIdentifiersMap = Record<CaipChainId, NativeAssetIdentifier>;
|
|
24
41
|
export type NetworkEnablementControllerState = {
|
|
25
42
|
enabledNetworkMap: EnabledMap;
|
|
43
|
+
nativeAssetIdentifiers: NativeAssetIdentifiersMap;
|
|
26
44
|
};
|
|
27
45
|
export type NetworkEnablementControllerGetStateAction = ControllerGetStateAction<typeof controllerName, NetworkEnablementControllerState>;
|
|
28
46
|
export type NetworkEnablementControllerSetEnabledNetworksAction = {
|
|
@@ -45,6 +63,14 @@ export type NetworkEnablementControllerEvents = NetworkEnablementControllerState
|
|
|
45
63
|
*/
|
|
46
64
|
export type AllowedEvents = NetworkControllerNetworkAddedEvent | NetworkControllerNetworkRemovedEvent | NetworkControllerStateChangeEvent | TransactionControllerTransactionSubmittedEvent;
|
|
47
65
|
export type NetworkEnablementControllerMessenger = Messenger<typeof controllerName, NetworkEnablementControllerActions | AllowedActions, NetworkEnablementControllerEvents | AllowedEvents>;
|
|
66
|
+
/**
|
|
67
|
+
* Network configuration with chain ID and native currency symbol.
|
|
68
|
+
* Used to initialize native asset identifiers.
|
|
69
|
+
*/
|
|
70
|
+
export type NetworkConfig = {
|
|
71
|
+
chainId: CaipChainId;
|
|
72
|
+
nativeCurrency: string;
|
|
73
|
+
};
|
|
48
74
|
/**
|
|
49
75
|
* Controller responsible for managing network enablement state across different blockchain networks.
|
|
50
76
|
*
|
|
@@ -116,14 +142,38 @@ export declare class NetworkEnablementController extends BaseController<typeof c
|
|
|
116
142
|
* Initializes the network enablement state from network controller configurations.
|
|
117
143
|
*
|
|
118
144
|
* This method reads the current network configurations from both NetworkController
|
|
119
|
-
* and MultichainNetworkController and syncs the enabled network map accordingly.
|
|
145
|
+
* and MultichainNetworkController and syncs the enabled network map and nativeAssetIdentifiers accordingly.
|
|
120
146
|
* It ensures proper namespace buckets exist for all configured networks and only
|
|
121
147
|
* adds missing networks with a default value of false, preserving existing user settings.
|
|
122
148
|
*
|
|
123
149
|
* This method should be called after the NetworkController and MultichainNetworkController
|
|
124
150
|
* have been initialized and their configurations are available.
|
|
125
151
|
*/
|
|
126
|
-
init(): void
|
|
152
|
+
init(): Promise<void>;
|
|
153
|
+
/**
|
|
154
|
+
* Initializes the native asset identifiers from network configurations.
|
|
155
|
+
* This method should be called from the client during controller initialization
|
|
156
|
+
* to populate the nativeAssetIdentifiers state based on actual network configurations.
|
|
157
|
+
*
|
|
158
|
+
* @param networks - Array of network configurations with chainId and nativeCurrency
|
|
159
|
+
* @example
|
|
160
|
+
* ```typescript
|
|
161
|
+
* const evmNetworks = Object.values(networkControllerState.networkConfigurationsByChainId)
|
|
162
|
+
* .map(config => ({
|
|
163
|
+
* chainId: toEvmCaipChainId(config.chainId),
|
|
164
|
+
* nativeCurrency: config.nativeCurrency,
|
|
165
|
+
* }));
|
|
166
|
+
*
|
|
167
|
+
* const multichainNetworks = Object.values(multichainState.multichainNetworkConfigurationsByChainId)
|
|
168
|
+
* .map(config => ({
|
|
169
|
+
* chainId: config.chainId,
|
|
170
|
+
* nativeCurrency: config.nativeCurrency,
|
|
171
|
+
* }));
|
|
172
|
+
*
|
|
173
|
+
* await controller.initNativeAssetIdentifiers([...evmNetworks, ...multichainNetworks]);
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
initNativeAssetIdentifiers(networks: NetworkConfig[]): Promise<void>;
|
|
127
177
|
/**
|
|
128
178
|
* Disables a network for the user.
|
|
129
179
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NetworkEnablementController.d.mts","sourceRoot":"","sources":["../src/NetworkEnablementController.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC3B,kCAAkC;AAGnC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,yCAAyC,EAAE,gDAAgD;AACzG,OAAO,KAAK,EACV,+BAA+B,EAC/B,kCAAkC,EAClC,oCAAoC,EACpC,iCAAiC,EAClC,qCAAqC;AACtC,OAAO,KAAK,EAAE,8CAA8C,EAAE,yCAAyC;AACvG,OAAO,KAAK,EAAE,WAAW,EAAE,aAAa,EAAE,GAAG,EAAE,wBAAwB;
|
|
1
|
+
{"version":3,"file":"NetworkEnablementController.d.mts","sourceRoot":"","sources":["../src/NetworkEnablementController.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,kCAAkC;AAC3D,OAAO,KAAK,EACV,wBAAwB,EACxB,0BAA0B,EAC3B,kCAAkC;AAGnC,OAAO,KAAK,EAAE,SAAS,EAAE,4BAA4B;AACrD,OAAO,KAAK,EAAE,yCAAyC,EAAE,gDAAgD;AACzG,OAAO,KAAK,EACV,+BAA+B,EAC/B,kCAAkC,EAClC,oCAAoC,EACpC,iCAA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G,WAAW,GAAG,IAAI;IAShD;;;;;;;OAOG;IACH,gBAAgB,CAAC,OAAO,EAAE,GAAG,GAAG,WAAW,GAAG,OAAO;CA0JtD"}
|
|
@@ -9,8 +9,19 @@ import { BuiltInNetworkName, ChainId } from "@metamask/controller-utils";
|
|
|
9
9
|
import { BtcScope, SolScope, TrxScope } from "@metamask/keyring-api";
|
|
10
10
|
import { KnownCaipNamespace } from "@metamask/utils";
|
|
11
11
|
import { POPULAR_NETWORKS } from "./constants.mjs";
|
|
12
|
+
import { Slip44Service } from "./services/index.mjs";
|
|
12
13
|
import { deriveKeys, isOnlyNetworkEnabledInNamespace, isPopularNetwork } from "./utils.mjs";
|
|
13
14
|
const controllerName = 'NetworkEnablementController';
|
|
15
|
+
/**
|
|
16
|
+
* Builds a native asset identifier in CAIP-19-like format.
|
|
17
|
+
*
|
|
18
|
+
* @param caipChainId - The CAIP-2 chain ID (e.g., 'eip155:1', 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp')
|
|
19
|
+
* @param slip44CoinType - The SLIP-44 coin type number
|
|
20
|
+
* @returns The native asset identifier string (e.g., 'eip155:1/slip44:60')
|
|
21
|
+
*/
|
|
22
|
+
function buildNativeAssetIdentifier(caipChainId, slip44CoinType) {
|
|
23
|
+
return `${caipChainId}/slip44:${slip44CoinType}`;
|
|
24
|
+
}
|
|
14
25
|
/**
|
|
15
26
|
* Gets the default state for the NetworkEnablementController.
|
|
16
27
|
*
|
|
@@ -44,6 +55,9 @@ const getDefaultNetworkEnablementControllerState = () => ({
|
|
|
44
55
|
[TrxScope.Shasta]: false,
|
|
45
56
|
},
|
|
46
57
|
},
|
|
58
|
+
// nativeAssetIdentifiers is initialized as empty and should be populated
|
|
59
|
+
// by the client using initNativeAssetIdentifiers() during controller init
|
|
60
|
+
nativeAssetIdentifiers: {},
|
|
47
61
|
});
|
|
48
62
|
// Metadata for the controller state
|
|
49
63
|
const metadata = {
|
|
@@ -53,6 +67,12 @@ const metadata = {
|
|
|
53
67
|
includeInDebugSnapshot: true,
|
|
54
68
|
usedInUi: true,
|
|
55
69
|
},
|
|
70
|
+
nativeAssetIdentifiers: {
|
|
71
|
+
includeInStateLogs: true,
|
|
72
|
+
persist: true,
|
|
73
|
+
includeInDebugSnapshot: true,
|
|
74
|
+
usedInUi: true,
|
|
75
|
+
},
|
|
56
76
|
};
|
|
57
77
|
/**
|
|
58
78
|
* Controller responsible for managing network enablement state across different blockchain networks.
|
|
@@ -82,8 +102,9 @@ export class NetworkEnablementController extends BaseController {
|
|
|
82
102
|
},
|
|
83
103
|
});
|
|
84
104
|
_NetworkEnablementController_instances.add(this);
|
|
85
|
-
messenger.subscribe('NetworkController:networkAdded', ({ chainId }) => {
|
|
86
|
-
|
|
105
|
+
messenger.subscribe('NetworkController:networkAdded', ({ chainId, nativeCurrency }) => {
|
|
106
|
+
// eslint-disable-next-line no-void
|
|
107
|
+
void __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_onAddNetwork).call(this, chainId, nativeCurrency);
|
|
87
108
|
});
|
|
88
109
|
messenger.subscribe('NetworkController:networkRemoved', ({ chainId }) => {
|
|
89
110
|
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_removeNetworkEntry).call(this, chainId);
|
|
@@ -107,20 +128,20 @@ export class NetworkEnablementController extends BaseController {
|
|
|
107
128
|
*/
|
|
108
129
|
enableNetwork(chainId) {
|
|
109
130
|
const { namespace, storageKey } = deriveKeys(chainId);
|
|
110
|
-
this.update((
|
|
131
|
+
this.update((state) => {
|
|
111
132
|
// disable all networks in all namespaces first
|
|
112
|
-
Object.keys(
|
|
113
|
-
Object.keys(
|
|
114
|
-
|
|
133
|
+
Object.keys(state.enabledNetworkMap).forEach((ns) => {
|
|
134
|
+
Object.keys(state.enabledNetworkMap[ns]).forEach((key) => {
|
|
135
|
+
state.enabledNetworkMap[ns][key] = false;
|
|
115
136
|
});
|
|
116
137
|
});
|
|
117
138
|
// if the namespace bucket does not exist, return
|
|
118
139
|
// new nemespace are added only when a new network is added
|
|
119
|
-
if (!
|
|
140
|
+
if (!state.enabledNetworkMap[namespace]) {
|
|
120
141
|
return;
|
|
121
142
|
}
|
|
122
143
|
// enable the network
|
|
123
|
-
|
|
144
|
+
state.enabledNetworkMap[namespace][storageKey] = true;
|
|
124
145
|
});
|
|
125
146
|
}
|
|
126
147
|
/**
|
|
@@ -145,17 +166,17 @@ export class NetworkEnablementController extends BaseController {
|
|
|
145
166
|
if (derivedNamespace !== namespace) {
|
|
146
167
|
throw new Error(`Chain ID ${chainId} belongs to namespace ${derivedNamespace}, but namespace ${namespace} was specified`);
|
|
147
168
|
}
|
|
148
|
-
this.update((
|
|
169
|
+
this.update((state) => {
|
|
149
170
|
// Ensure the namespace bucket exists
|
|
150
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
171
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
|
|
151
172
|
// Disable all networks in the specified namespace first
|
|
152
|
-
if (
|
|
153
|
-
Object.keys(
|
|
154
|
-
|
|
173
|
+
if (state.enabledNetworkMap[namespace]) {
|
|
174
|
+
Object.keys(state.enabledNetworkMap[namespace]).forEach((key) => {
|
|
175
|
+
state.enabledNetworkMap[namespace][key] = false;
|
|
155
176
|
});
|
|
156
177
|
}
|
|
157
178
|
// Enable the target network in the specified namespace
|
|
158
|
-
|
|
179
|
+
state.enabledNetworkMap[namespace][storageKey] = true;
|
|
159
180
|
});
|
|
160
181
|
}
|
|
161
182
|
/**
|
|
@@ -169,11 +190,11 @@ export class NetworkEnablementController extends BaseController {
|
|
|
169
190
|
* Popular networks that don't exist in NetworkController or MultichainNetworkController configurations will be skipped silently.
|
|
170
191
|
*/
|
|
171
192
|
enableAllPopularNetworks() {
|
|
172
|
-
this.update((
|
|
193
|
+
this.update((state) => {
|
|
173
194
|
// First disable all networks across all namespaces
|
|
174
|
-
Object.keys(
|
|
175
|
-
Object.keys(
|
|
176
|
-
|
|
195
|
+
Object.keys(state.enabledNetworkMap).forEach((ns) => {
|
|
196
|
+
Object.keys(state.enabledNetworkMap[ns]).forEach((key) => {
|
|
197
|
+
state.enabledNetworkMap[ns][key] = false;
|
|
177
198
|
});
|
|
178
199
|
});
|
|
179
200
|
// Get current network configurations to check if networks exist
|
|
@@ -185,35 +206,36 @@ export class NetworkEnablementController extends BaseController {
|
|
|
185
206
|
// Check if network exists in NetworkController configurations
|
|
186
207
|
if (networkControllerState.networkConfigurationsByChainId[chainId]) {
|
|
187
208
|
// Ensure namespace bucket exists
|
|
188
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
209
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
|
|
189
210
|
// Enable the network
|
|
190
|
-
|
|
211
|
+
state.enabledNetworkMap[namespace][storageKey] = true;
|
|
191
212
|
}
|
|
192
213
|
});
|
|
193
214
|
// Enable Solana mainnet if it exists in MultichainNetworkController configurations
|
|
194
215
|
const solanaKeys = deriveKeys(SolScope.Mainnet);
|
|
195
216
|
if (multichainState.multichainNetworkConfigurationsByChainId[SolScope.Mainnet]) {
|
|
196
217
|
// Ensure namespace bucket exists
|
|
197
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
218
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, solanaKeys.namespace);
|
|
198
219
|
// Enable Solana mainnet
|
|
199
|
-
|
|
220
|
+
state.enabledNetworkMap[solanaKeys.namespace][solanaKeys.storageKey] =
|
|
221
|
+
true;
|
|
200
222
|
}
|
|
201
223
|
// Enable Bitcoin mainnet if it exists in MultichainNetworkController configurations
|
|
202
224
|
const bitcoinKeys = deriveKeys(BtcScope.Mainnet);
|
|
203
225
|
if (multichainState.multichainNetworkConfigurationsByChainId[BtcScope.Mainnet]) {
|
|
204
226
|
// Ensure namespace bucket exists
|
|
205
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
227
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, bitcoinKeys.namespace);
|
|
206
228
|
// Enable Bitcoin mainnet
|
|
207
|
-
|
|
229
|
+
state.enabledNetworkMap[bitcoinKeys.namespace][bitcoinKeys.storageKey] =
|
|
208
230
|
true;
|
|
209
231
|
}
|
|
210
232
|
// Enable Tron mainnet if it exists in MultichainNetworkController configurations
|
|
211
233
|
const tronKeys = deriveKeys(TrxScope.Mainnet);
|
|
212
234
|
if (multichainState.multichainNetworkConfigurationsByChainId[TrxScope.Mainnet]) {
|
|
213
235
|
// Ensure namespace bucket exists
|
|
214
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
236
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, tronKeys.namespace);
|
|
215
237
|
// Enable Tron mainnet
|
|
216
|
-
|
|
238
|
+
state.enabledNetworkMap[tronKeys.namespace][tronKeys.storageKey] = true;
|
|
217
239
|
}
|
|
218
240
|
});
|
|
219
241
|
}
|
|
@@ -221,39 +243,124 @@ export class NetworkEnablementController extends BaseController {
|
|
|
221
243
|
* Initializes the network enablement state from network controller configurations.
|
|
222
244
|
*
|
|
223
245
|
* This method reads the current network configurations from both NetworkController
|
|
224
|
-
* and MultichainNetworkController and syncs the enabled network map accordingly.
|
|
246
|
+
* and MultichainNetworkController and syncs the enabled network map and nativeAssetIdentifiers accordingly.
|
|
225
247
|
* It ensures proper namespace buckets exist for all configured networks and only
|
|
226
248
|
* adds missing networks with a default value of false, preserving existing user settings.
|
|
227
249
|
*
|
|
228
250
|
* This method should be called after the NetworkController and MultichainNetworkController
|
|
229
251
|
* have been initialized and their configurations are available.
|
|
230
252
|
*/
|
|
231
|
-
init() {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
253
|
+
async init() {
|
|
254
|
+
// Get network configurations from NetworkController (EVM networks)
|
|
255
|
+
const networkControllerState = this.messenger.call('NetworkController:getState');
|
|
256
|
+
// Get network configurations from MultichainNetworkController (all networks)
|
|
257
|
+
const multichainState = this.messenger.call('MultichainNetworkController:getState');
|
|
258
|
+
// Build nativeAssetIdentifiers for EVM networks using chainid.network
|
|
259
|
+
const evmNativeAssetUpdates = [];
|
|
260
|
+
for (const [chainId, config] of Object.entries(networkControllerState.networkConfigurationsByChainId)) {
|
|
261
|
+
const { caipChainId } = deriveKeys(chainId);
|
|
262
|
+
// Skip if already in state
|
|
263
|
+
if (this.state.nativeAssetIdentifiers[caipChainId] !== undefined) {
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
// Parse hex chainId to number for chainid.network lookup
|
|
267
|
+
const numericChainId = parseInt(chainId, 16);
|
|
268
|
+
// EVM networks: use getSlip44ByChainId (chainid.network data)
|
|
269
|
+
// Default to 60 (Ethereum) if no specific mapping is found
|
|
270
|
+
const slip44CoinType = (await Slip44Service.getSlip44ByChainId(numericChainId, config.nativeCurrency)) ?? 60;
|
|
271
|
+
evmNativeAssetUpdates.push({
|
|
272
|
+
caipChainId,
|
|
273
|
+
identifier: buildNativeAssetIdentifier(caipChainId, slip44CoinType),
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
// Update state synchronously
|
|
277
|
+
this.update((state) => {
|
|
237
278
|
// Initialize namespace buckets for EVM networks from NetworkController
|
|
238
|
-
Object.
|
|
279
|
+
Object.entries(networkControllerState.networkConfigurationsByChainId).forEach(([chainId]) => {
|
|
280
|
+
var _a;
|
|
239
281
|
const { namespace, storageKey } = deriveKeys(chainId);
|
|
240
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
282
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
|
|
241
283
|
// Only add network if it doesn't already exist in state (preserves user settings)
|
|
242
|
-
|
|
243
|
-
s.enabledNetworkMap[namespace][storageKey] = false;
|
|
244
|
-
}
|
|
284
|
+
(_a = state.enabledNetworkMap[namespace])[storageKey] ?? (_a[storageKey] = false);
|
|
245
285
|
});
|
|
286
|
+
// Apply nativeAssetIdentifier updates
|
|
287
|
+
for (const { caipChainId, identifier } of evmNativeAssetUpdates) {
|
|
288
|
+
state.nativeAssetIdentifiers[caipChainId] = identifier;
|
|
289
|
+
}
|
|
246
290
|
// Initialize namespace buckets for all networks from MultichainNetworkController
|
|
247
291
|
Object.keys(multichainState.multichainNetworkConfigurationsByChainId).forEach((chainId) => {
|
|
292
|
+
var _a;
|
|
248
293
|
const { namespace, storageKey } = deriveKeys(chainId);
|
|
249
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
294
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
|
|
250
295
|
// Only add network if it doesn't already exist in state (preserves user settings)
|
|
251
|
-
|
|
252
|
-
s.enabledNetworkMap[namespace][storageKey] = false;
|
|
253
|
-
}
|
|
296
|
+
(_a = state.enabledNetworkMap[namespace])[storageKey] ?? (_a[storageKey] = false);
|
|
254
297
|
});
|
|
255
298
|
});
|
|
256
299
|
}
|
|
300
|
+
/**
|
|
301
|
+
* Initializes the native asset identifiers from network configurations.
|
|
302
|
+
* This method should be called from the client during controller initialization
|
|
303
|
+
* to populate the nativeAssetIdentifiers state based on actual network configurations.
|
|
304
|
+
*
|
|
305
|
+
* @param networks - Array of network configurations with chainId and nativeCurrency
|
|
306
|
+
* @example
|
|
307
|
+
* ```typescript
|
|
308
|
+
* const evmNetworks = Object.values(networkControllerState.networkConfigurationsByChainId)
|
|
309
|
+
* .map(config => ({
|
|
310
|
+
* chainId: toEvmCaipChainId(config.chainId),
|
|
311
|
+
* nativeCurrency: config.nativeCurrency,
|
|
312
|
+
* }));
|
|
313
|
+
*
|
|
314
|
+
* const multichainNetworks = Object.values(multichainState.multichainNetworkConfigurationsByChainId)
|
|
315
|
+
* .map(config => ({
|
|
316
|
+
* chainId: config.chainId,
|
|
317
|
+
* nativeCurrency: config.nativeCurrency,
|
|
318
|
+
* }));
|
|
319
|
+
*
|
|
320
|
+
* await controller.initNativeAssetIdentifiers([...evmNetworks, ...multichainNetworks]);
|
|
321
|
+
* ```
|
|
322
|
+
*/
|
|
323
|
+
async initNativeAssetIdentifiers(networks) {
|
|
324
|
+
// Process networks and collect updates
|
|
325
|
+
const updates = [];
|
|
326
|
+
for (const { chainId, nativeCurrency } of networks) {
|
|
327
|
+
// Check if nativeCurrency is already in CAIP-19 format (e.g., "bip122:.../slip44:0")
|
|
328
|
+
// Non-EVM networks from MultichainNetworkController use this format
|
|
329
|
+
if (nativeCurrency.includes('/slip44:')) {
|
|
330
|
+
updates.push({
|
|
331
|
+
chainId,
|
|
332
|
+
identifier: nativeCurrency,
|
|
333
|
+
});
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
// Extract namespace from CAIP-2 chainId
|
|
337
|
+
const [namespace, reference] = chainId.split(':');
|
|
338
|
+
let slip44CoinType;
|
|
339
|
+
if (namespace === 'eip155') {
|
|
340
|
+
// EVM networks: use getSlip44ByChainId (chainid.network data)
|
|
341
|
+
// Default to 60 (Ethereum) if no specific mapping is found
|
|
342
|
+
const numericChainId = parseInt(reference, 10);
|
|
343
|
+
slip44CoinType =
|
|
344
|
+
(await Slip44Service.getSlip44ByChainId(numericChainId, nativeCurrency)) ?? 60;
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
// Non-EVM networks: use getSlip44BySymbol (@metamask/slip44 package)
|
|
348
|
+
slip44CoinType = Slip44Service.getSlip44BySymbol(nativeCurrency);
|
|
349
|
+
}
|
|
350
|
+
if (slip44CoinType !== undefined) {
|
|
351
|
+
updates.push({
|
|
352
|
+
chainId,
|
|
353
|
+
identifier: buildNativeAssetIdentifier(chainId, slip44CoinType),
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
// Apply all updates synchronously
|
|
358
|
+
this.update((state) => {
|
|
359
|
+
for (const { chainId, identifier } of updates) {
|
|
360
|
+
state.nativeAssetIdentifiers[chainId] = identifier;
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
}
|
|
257
364
|
/**
|
|
258
365
|
* Disables a network for the user.
|
|
259
366
|
*
|
|
@@ -271,8 +378,8 @@ export class NetworkEnablementController extends BaseController {
|
|
|
271
378
|
disableNetwork(chainId) {
|
|
272
379
|
const derivedKeys = deriveKeys(chainId);
|
|
273
380
|
const { namespace, storageKey } = derivedKeys;
|
|
274
|
-
this.update((
|
|
275
|
-
|
|
381
|
+
this.update((state) => {
|
|
382
|
+
state.enabledNetworkMap[namespace][storageKey] = false;
|
|
276
383
|
});
|
|
277
384
|
}
|
|
278
385
|
/**
|
|
@@ -310,22 +417,42 @@ _NetworkEnablementController_instances = new WeakSet(), _NetworkEnablementContro
|
|
|
310
417
|
return enabledPopularNetworksCount > 1;
|
|
311
418
|
}, _NetworkEnablementController_removeNetworkEntry = function _NetworkEnablementController_removeNetworkEntry(chainId) {
|
|
312
419
|
const derivedKeys = deriveKeys(chainId);
|
|
313
|
-
const { namespace, storageKey } = derivedKeys;
|
|
314
|
-
this.update((
|
|
420
|
+
const { namespace, storageKey, caipChainId } = derivedKeys;
|
|
421
|
+
this.update((state) => {
|
|
315
422
|
// fallback and enable ethereum mainnet
|
|
316
423
|
if (isOnlyNetworkEnabledInNamespace(this.state, derivedKeys)) {
|
|
317
|
-
|
|
318
|
-
true;
|
|
424
|
+
state.enabledNetworkMap[namespace][ChainId[BuiltInNetworkName.Mainnet]] = true;
|
|
319
425
|
}
|
|
320
|
-
if (namespace in
|
|
321
|
-
delete
|
|
426
|
+
if (namespace in state.enabledNetworkMap) {
|
|
427
|
+
delete state.enabledNetworkMap[namespace][storageKey];
|
|
322
428
|
}
|
|
429
|
+
// Remove from nativeAssetIdentifiers as well
|
|
430
|
+
delete state.nativeAssetIdentifiers[caipChainId];
|
|
323
431
|
});
|
|
324
|
-
}, _NetworkEnablementController_onAddNetwork =
|
|
325
|
-
|
|
326
|
-
|
|
432
|
+
}, _NetworkEnablementController_onAddNetwork =
|
|
433
|
+
/**
|
|
434
|
+
* Handles the addition of a new EVM network to the controller.
|
|
435
|
+
*
|
|
436
|
+
* @param chainId - The chain ID to add (Hex format)
|
|
437
|
+
* @param nativeCurrency - The native currency symbol of the network (e.g., 'ETH')
|
|
438
|
+
*
|
|
439
|
+
* @description
|
|
440
|
+
* - If in popular networks mode (>2 popular networks enabled) AND adding a popular network:
|
|
441
|
+
* - Keep current selection (add but don't enable the new network)
|
|
442
|
+
* - Otherwise:
|
|
443
|
+
* - Switch to the newly added network (disable all others, enable this one)
|
|
444
|
+
* - Also updates the nativeAssetIdentifiers with the CAIP-19-like identifier
|
|
445
|
+
*/
|
|
446
|
+
async function _NetworkEnablementController_onAddNetwork(chainId, nativeCurrency) {
|
|
447
|
+
const { namespace, storageKey, reference, caipChainId } = deriveKeys(chainId);
|
|
448
|
+
// Parse hex chainId to number for chainid.network lookup
|
|
449
|
+
const numericChainId = parseInt(reference, 16);
|
|
450
|
+
// EVM networks: use getSlip44ByChainId (chainid.network data)
|
|
451
|
+
// Default to 60 (Ethereum) if no specific mapping is found
|
|
452
|
+
const slip44CoinType = (await Slip44Service.getSlip44ByChainId(numericChainId, nativeCurrency)) ?? 60;
|
|
453
|
+
this.update((state) => {
|
|
327
454
|
// Ensure the namespace bucket exists
|
|
328
|
-
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this,
|
|
455
|
+
__classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_ensureNamespaceBucket).call(this, state, namespace);
|
|
329
456
|
// Check if popular networks mode is active (>2 popular networks enabled)
|
|
330
457
|
const inPopularNetworksMode = __classPrivateFieldGet(this, _NetworkEnablementController_instances, "m", _NetworkEnablementController_isInPopularNetworksMode).call(this);
|
|
331
458
|
// Check if the network being added is a popular network
|
|
@@ -334,18 +461,20 @@ _NetworkEnablementController_instances = new WeakSet(), _NetworkEnablementContro
|
|
|
334
461
|
const shouldKeepCurrentSelection = inPopularNetworksMode && isAddedNetworkPopular;
|
|
335
462
|
if (shouldKeepCurrentSelection) {
|
|
336
463
|
// Add the popular network but don't enable it (keep current selection)
|
|
337
|
-
|
|
464
|
+
state.enabledNetworkMap[namespace][storageKey] = true;
|
|
338
465
|
}
|
|
339
466
|
else {
|
|
340
467
|
// Switch to the newly added network (disable all others, enable this one)
|
|
341
|
-
Object.keys(
|
|
342
|
-
Object.keys(
|
|
343
|
-
|
|
468
|
+
Object.keys(state.enabledNetworkMap).forEach((ns) => {
|
|
469
|
+
Object.keys(state.enabledNetworkMap[ns]).forEach((key) => {
|
|
470
|
+
state.enabledNetworkMap[ns][key] = false;
|
|
344
471
|
});
|
|
345
472
|
});
|
|
346
473
|
// Enable the newly added network
|
|
347
|
-
|
|
474
|
+
state.enabledNetworkMap[namespace][storageKey] = true;
|
|
348
475
|
}
|
|
476
|
+
// Update nativeAssetIdentifiers with the CAIP-19-like identifier
|
|
477
|
+
state.nativeAssetIdentifiers[caipChainId] = buildNativeAssetIdentifier(caipChainId, slip44CoinType);
|
|
349
478
|
});
|
|
350
479
|
};
|
|
351
480
|
//# sourceMappingURL=NetworkEnablementController.mjs.map
|