@metamask-previews/network-enablement-controller 6.0.5-preview-8bfa290fb → 7.0.0-preview-0a30e47
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 +16 -2
- package/dist/{NetworkEnablementController-method-action-types.d.mts → NetworkEnablementController-method-action-types.d.ts} +2 -2
- package/dist/NetworkEnablementController-method-action-types.d.ts.map +1 -0
- package/dist/{NetworkEnablementController-method-action-types.mjs → NetworkEnablementController-method-action-types.js} +1 -1
- package/dist/NetworkEnablementController-method-action-types.js.map +1 -0
- package/dist/{NetworkEnablementController.d.cts → NetworkEnablementController.d.ts} +10 -10
- package/dist/NetworkEnablementController.d.ts.map +1 -0
- package/dist/{NetworkEnablementController.mjs → NetworkEnablementController.js} +167 -131
- package/dist/NetworkEnablementController.js.map +1 -0
- package/dist/constants.d.ts +3 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/{constants.mjs → constants.js} +1 -1
- package/dist/constants.js.map +1 -0
- package/dist/{index.d.cts → index.d.ts} +6 -6
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/{selectors.d.cts → selectors.d.ts} +18 -30
- package/dist/selectors.d.ts.map +1 -0
- package/dist/{selectors.mjs → selectors.js} +4 -4
- package/dist/selectors.js.map +1 -0
- package/dist/services/{Slip44Service.d.cts → Slip44Service.d.ts} +1 -1
- package/dist/services/Slip44Service.d.ts.map +1 -0
- package/dist/services/Slip44Service.js +167 -0
- package/dist/services/Slip44Service.js.map +1 -0
- package/dist/services/{index.d.cts → index.d.ts} +2 -2
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/{index.mjs → index.js} +2 -2
- package/dist/services/index.js.map +1 -0
- package/dist/{utils.d.mts → utils.d.ts} +3 -3
- package/dist/utils.d.ts.map +1 -0
- package/dist/{utils.mjs → utils.js} +4 -4
- package/dist/utils.js.map +1 -0
- package/package.json +19 -23
- package/dist/NetworkEnablementController-method-action-types.cjs +0 -7
- package/dist/NetworkEnablementController-method-action-types.cjs.map +0 -1
- package/dist/NetworkEnablementController-method-action-types.d.cts +0 -185
- package/dist/NetworkEnablementController-method-action-types.d.cts.map +0 -1
- package/dist/NetworkEnablementController-method-action-types.d.mts.map +0 -1
- package/dist/NetworkEnablementController-method-action-types.mjs.map +0 -1
- package/dist/NetworkEnablementController.cjs +0 -590
- package/dist/NetworkEnablementController.cjs.map +0 -1
- package/dist/NetworkEnablementController.d.cts.map +0 -1
- package/dist/NetworkEnablementController.d.mts +0 -234
- package/dist/NetworkEnablementController.d.mts.map +0 -1
- package/dist/NetworkEnablementController.mjs.map +0 -1
- package/dist/constants.cjs +0 -22
- package/dist/constants.cjs.map +0 -1
- package/dist/constants.d.cts +0 -3
- package/dist/constants.d.cts.map +0 -1
- package/dist/constants.d.mts +0 -3
- package/dist/constants.d.mts.map +0 -1
- package/dist/constants.mjs.map +0 -1
- package/dist/index.cjs +0 -18
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.mts +0 -6
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -4
- package/dist/index.mjs.map +0 -1
- package/dist/selectors.cjs +0 -87
- package/dist/selectors.cjs.map +0 -1
- package/dist/selectors.d.cts.map +0 -1
- package/dist/selectors.d.mts +0 -280
- package/dist/selectors.d.mts.map +0 -1
- package/dist/selectors.mjs.map +0 -1
- package/dist/services/Slip44Service.cjs +0 -182
- package/dist/services/Slip44Service.cjs.map +0 -1
- package/dist/services/Slip44Service.d.cts.map +0 -1
- package/dist/services/Slip44Service.d.mts +0 -63
- package/dist/services/Slip44Service.d.mts.map +0 -1
- package/dist/services/Slip44Service.mjs +0 -175
- package/dist/services/Slip44Service.mjs.map +0 -1
- package/dist/services/index.cjs +0 -11
- package/dist/services/index.cjs.map +0 -1
- package/dist/services/index.d.cts.map +0 -1
- package/dist/services/index.d.mts +0 -5
- package/dist/services/index.d.mts.map +0 -1
- package/dist/services/index.mjs.map +0 -1
- package/dist/utils.cjs +0 -62
- package/dist/utils.cjs.map +0 -1
- package/dist/utils.d.cts +0 -36
- package/dist/utils.d.cts.map +0 -1
- package/dist/utils.d.mts.map +0 -1
- package/dist/utils.mjs.map +0 -1
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Represents a single SLIP-44 entry with its metadata.
|
|
3
|
-
*/
|
|
4
|
-
export type Slip44Entry = {
|
|
5
|
-
index: string;
|
|
6
|
-
symbol: string;
|
|
7
|
-
name: string;
|
|
8
|
-
};
|
|
9
|
-
export declare class Slip44Service {
|
|
10
|
-
#private;
|
|
11
|
-
/**
|
|
12
|
-
* Gets the SLIP-44 coin type identifier for an EVM network by chain ID.
|
|
13
|
-
*
|
|
14
|
-
* **IMPORTANT: This method is for EVM networks only (eip155 namespace).**
|
|
15
|
-
* For non-EVM networks (Bitcoin, Solana, Tron, etc.), use `getSlip44BySymbol`.
|
|
16
|
-
*
|
|
17
|
-
* This method checks chainid.network data (which maps chainId directly
|
|
18
|
-
* to slip44). If not found, defaults to 60 (Ethereum).
|
|
19
|
-
*
|
|
20
|
-
* @param chainId - The EVM chain ID as a number (e.g., 1 for Ethereum, 56 for BNB Chain)
|
|
21
|
-
* @returns The SLIP-44 coin type number (defaults to 60 if not found)
|
|
22
|
-
* @example
|
|
23
|
-
* ```typescript
|
|
24
|
-
* // For EVM networks only
|
|
25
|
-
* const ethCoinType = await Slip44Service.getEvmSlip44(1);
|
|
26
|
-
* // Returns 60
|
|
27
|
-
*
|
|
28
|
-
* const bnbCoinType = await Slip44Service.getEvmSlip44(56);
|
|
29
|
-
* // Returns 714
|
|
30
|
-
*
|
|
31
|
-
* const unknownEvmChain = await Slip44Service.getEvmSlip44(99999);
|
|
32
|
-
* // Returns 60 (default for EVM)
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
static getEvmSlip44(chainId: number): Promise<number>;
|
|
36
|
-
/**
|
|
37
|
-
* Gets the SLIP-44 coin type identifier for a non-EVM network by symbol.
|
|
38
|
-
*
|
|
39
|
-
* **IMPORTANT: This method is for non-EVM networks only (Bitcoin, Solana, Tron, etc.).**
|
|
40
|
-
* For EVM networks (eip155 namespace), use `getEvmSlip44` instead.
|
|
41
|
-
*
|
|
42
|
-
* Note: Symbol lookup may return incorrect results for duplicate symbols
|
|
43
|
-
* (e.g., CPC is both CPChain and Capricoin).
|
|
44
|
-
*
|
|
45
|
-
* @param symbol - The network symbol (e.g., 'ETH', 'BTC', 'SOL')
|
|
46
|
-
* @returns The SLIP-44 coin type number, or undefined if not found
|
|
47
|
-
* @example
|
|
48
|
-
* ```typescript
|
|
49
|
-
* const ethCoinType = Slip44Service.getSlip44BySymbol('ETH');
|
|
50
|
-
* // Returns 60
|
|
51
|
-
*
|
|
52
|
-
* const btcCoinType = Slip44Service.getSlip44BySymbol('BTC');
|
|
53
|
-
* // Returns 0
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
static getSlip44BySymbol(symbol: string): number | undefined;
|
|
57
|
-
/**
|
|
58
|
-
* Clears all internal caches.
|
|
59
|
-
* Useful for testing or if the underlying data might change.
|
|
60
|
-
*/
|
|
61
|
-
static clearCache(): void;
|
|
62
|
-
}
|
|
63
|
-
//# sourceMappingURL=Slip44Service.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Slip44Service.d.mts","sourceRoot":"","sources":["../../src/services/Slip44Service.ts"],"names":[],"mappings":"AAMA;;GAEG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AA+CF,qBAAa,aAAa;;IA+DxB;;;;;;;;;;;;;;;;;;;;;;;OAuBG;WACU,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAmB3D;;;;;;;;;;;;;;;;;;;OAmBG;IACH,MAAM,CAAC,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS;IA2B5D;;;OAGG;IACH,MAAM,CAAC,UAAU,IAAI,IAAI;CAK1B"}
|
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
2
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
3
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
4
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5
|
-
};
|
|
6
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
7
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
10
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
11
|
-
};
|
|
12
|
-
var _a, _Slip44Service_chainIdCache, _Slip44Service_fetchPromise, _Slip44Service_symbolCache, _Slip44Service_fetchChainData;
|
|
13
|
-
import { fetchWithErrorHandling } from "@metamask/controller-utils";
|
|
14
|
-
// @ts-expect-error: No type definitions for '@metamask/slip44'
|
|
15
|
-
import slip44 from "@metamask/slip44" with { type: "json" };
|
|
16
|
-
const CHAINID_NETWORK_URL = 'https://chainid.network/chains.json';
|
|
17
|
-
/**
|
|
18
|
-
* Service for looking up SLIP-44 coin type identifiers.
|
|
19
|
-
*
|
|
20
|
-
* SLIP-44 defines registered coin types used in BIP-44 derivation paths.
|
|
21
|
-
*
|
|
22
|
-
* This service provides two lookup methods:
|
|
23
|
-
* 1. `getEvmSlip44` - For EVM networks, uses chainid.network data (recommended for eip155)
|
|
24
|
-
* 2. `getSlip44BySymbol` - Fallback method using @metamask/slip44 package
|
|
25
|
-
*
|
|
26
|
-
* @see https://github.com/satoshilabs/slips/blob/master/slip-0044.md
|
|
27
|
-
* @see https://chainid.network/chains.json
|
|
28
|
-
*/
|
|
29
|
-
/**
|
|
30
|
-
* Manual overrides for EVM chain IDs where chainid.network returns
|
|
31
|
-
* an incorrect SLIP-44 value due to chain ID collisions.
|
|
32
|
-
*/
|
|
33
|
-
const EVM_SLIP44_OVERRIDES = new Map([
|
|
34
|
-
[999, 2457], // HyperEVM — chainid.network returns 1 (Wanchain collision)
|
|
35
|
-
]);
|
|
36
|
-
export class Slip44Service {
|
|
37
|
-
/**
|
|
38
|
-
* Gets the SLIP-44 coin type identifier for an EVM network by chain ID.
|
|
39
|
-
*
|
|
40
|
-
* **IMPORTANT: This method is for EVM networks only (eip155 namespace).**
|
|
41
|
-
* For non-EVM networks (Bitcoin, Solana, Tron, etc.), use `getSlip44BySymbol`.
|
|
42
|
-
*
|
|
43
|
-
* This method checks chainid.network data (which maps chainId directly
|
|
44
|
-
* to slip44). If not found, defaults to 60 (Ethereum).
|
|
45
|
-
*
|
|
46
|
-
* @param chainId - The EVM chain ID as a number (e.g., 1 for Ethereum, 56 for BNB Chain)
|
|
47
|
-
* @returns The SLIP-44 coin type number (defaults to 60 if not found)
|
|
48
|
-
* @example
|
|
49
|
-
* ```typescript
|
|
50
|
-
* // For EVM networks only
|
|
51
|
-
* const ethCoinType = await Slip44Service.getEvmSlip44(1);
|
|
52
|
-
* // Returns 60
|
|
53
|
-
*
|
|
54
|
-
* const bnbCoinType = await Slip44Service.getEvmSlip44(56);
|
|
55
|
-
* // Returns 714
|
|
56
|
-
*
|
|
57
|
-
* const unknownEvmChain = await Slip44Service.getEvmSlip44(99999);
|
|
58
|
-
* // Returns 60 (default for EVM)
|
|
59
|
-
* ```
|
|
60
|
-
*/
|
|
61
|
-
static async getEvmSlip44(chainId) {
|
|
62
|
-
const override = EVM_SLIP44_OVERRIDES.get(chainId);
|
|
63
|
-
if (override !== undefined) {
|
|
64
|
-
return override;
|
|
65
|
-
}
|
|
66
|
-
// Ensure chain data is loaded
|
|
67
|
-
await __classPrivateFieldGet(this, _a, "m", _Slip44Service_fetchChainData).call(this);
|
|
68
|
-
// Check chainId cache first
|
|
69
|
-
const cached = __classPrivateFieldGet(this, _a, "f", _Slip44Service_chainIdCache)?.get(chainId);
|
|
70
|
-
if (cached !== undefined) {
|
|
71
|
-
return cached;
|
|
72
|
-
}
|
|
73
|
-
// Default to 60 (Ethereum) for EVM networks without specific mapping
|
|
74
|
-
return 60;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Gets the SLIP-44 coin type identifier for a non-EVM network by symbol.
|
|
78
|
-
*
|
|
79
|
-
* **IMPORTANT: This method is for non-EVM networks only (Bitcoin, Solana, Tron, etc.).**
|
|
80
|
-
* For EVM networks (eip155 namespace), use `getEvmSlip44` instead.
|
|
81
|
-
*
|
|
82
|
-
* Note: Symbol lookup may return incorrect results for duplicate symbols
|
|
83
|
-
* (e.g., CPC is both CPChain and Capricoin).
|
|
84
|
-
*
|
|
85
|
-
* @param symbol - The network symbol (e.g., 'ETH', 'BTC', 'SOL')
|
|
86
|
-
* @returns The SLIP-44 coin type number, or undefined if not found
|
|
87
|
-
* @example
|
|
88
|
-
* ```typescript
|
|
89
|
-
* const ethCoinType = Slip44Service.getSlip44BySymbol('ETH');
|
|
90
|
-
* // Returns 60
|
|
91
|
-
*
|
|
92
|
-
* const btcCoinType = Slip44Service.getSlip44BySymbol('BTC');
|
|
93
|
-
* // Returns 0
|
|
94
|
-
* ```
|
|
95
|
-
*/
|
|
96
|
-
static getSlip44BySymbol(symbol) {
|
|
97
|
-
// Check cache first
|
|
98
|
-
if (__classPrivateFieldGet(this, _a, "f", _Slip44Service_symbolCache).has(symbol)) {
|
|
99
|
-
return __classPrivateFieldGet(this, _a, "f", _Slip44Service_symbolCache).get(symbol);
|
|
100
|
-
}
|
|
101
|
-
const slip44Data = slip44;
|
|
102
|
-
const upperSymbol = symbol.toUpperCase();
|
|
103
|
-
// Iterate through all entries to find matching symbol
|
|
104
|
-
// Note: Object.keys returns numeric keys in ascending order,
|
|
105
|
-
// so for duplicate symbols we get the lowest coin type first
|
|
106
|
-
// (which is the convention for resolving duplicates)
|
|
107
|
-
for (const key of Object.keys(slip44Data)) {
|
|
108
|
-
const entry = slip44Data[key];
|
|
109
|
-
if (entry.symbol.toUpperCase() === upperSymbol) {
|
|
110
|
-
const coinType = parseInt(key, 10);
|
|
111
|
-
__classPrivateFieldGet(this, _a, "f", _Slip44Service_symbolCache).set(symbol, coinType);
|
|
112
|
-
return coinType;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
// Cache the miss as well to avoid repeated lookups
|
|
116
|
-
__classPrivateFieldGet(this, _a, "f", _Slip44Service_symbolCache).set(symbol, undefined);
|
|
117
|
-
return undefined;
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Clears all internal caches.
|
|
121
|
-
* Useful for testing or if the underlying data might change.
|
|
122
|
-
*/
|
|
123
|
-
static clearCache() {
|
|
124
|
-
__classPrivateFieldGet(this, _a, "f", _Slip44Service_symbolCache).clear();
|
|
125
|
-
__classPrivateFieldSet(this, _a, null, "f", _Slip44Service_chainIdCache);
|
|
126
|
-
__classPrivateFieldSet(this, _a, null, "f", _Slip44Service_fetchPromise);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
_a = Slip44Service, _Slip44Service_fetchChainData = async function _Slip44Service_fetchChainData() {
|
|
130
|
-
if (__classPrivateFieldGet(this, _a, "f", _Slip44Service_chainIdCache) !== null) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
// Avoid duplicate fetches
|
|
134
|
-
if (__classPrivateFieldGet(this, _a, "f", _Slip44Service_fetchPromise)) {
|
|
135
|
-
await __classPrivateFieldGet(this, _a, "f", _Slip44Service_fetchPromise);
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
__classPrivateFieldSet(this, _a, (async () => {
|
|
139
|
-
try {
|
|
140
|
-
const chains = await fetchWithErrorHandling({
|
|
141
|
-
url: CHAINID_NETWORK_URL,
|
|
142
|
-
timeout: 10000,
|
|
143
|
-
});
|
|
144
|
-
if (chains && Array.isArray(chains)) {
|
|
145
|
-
__classPrivateFieldSet(this, _a, new Map(chains
|
|
146
|
-
.filter((chain) => chain.slip44 !== undefined)
|
|
147
|
-
.map((chain) => [chain.chainId, chain.slip44])), "f", _Slip44Service_chainIdCache);
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
150
|
-
// Invalid response, initialize empty cache
|
|
151
|
-
__classPrivateFieldSet(this, _a, new Map(), "f", _Slip44Service_chainIdCache);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
catch {
|
|
155
|
-
// Network failed, initialize empty cache so we fall back to symbol lookup
|
|
156
|
-
__classPrivateFieldSet(this, _a, new Map(), "f", _Slip44Service_chainIdCache);
|
|
157
|
-
}
|
|
158
|
-
})(), "f", _Slip44Service_fetchPromise);
|
|
159
|
-
await __classPrivateFieldGet(this, _a, "f", _Slip44Service_fetchPromise);
|
|
160
|
-
__classPrivateFieldSet(this, _a, null, "f", _Slip44Service_fetchPromise);
|
|
161
|
-
};
|
|
162
|
-
/**
|
|
163
|
-
* Cache for chainId to slip44 lookups from chainid.network.
|
|
164
|
-
*/
|
|
165
|
-
_Slip44Service_chainIdCache = { value: null };
|
|
166
|
-
/**
|
|
167
|
-
* Whether a fetch is currently in progress.
|
|
168
|
-
*/
|
|
169
|
-
_Slip44Service_fetchPromise = { value: null };
|
|
170
|
-
/**
|
|
171
|
-
* Cache for symbol to slip44 index lookups.
|
|
172
|
-
* This avoids iterating through all entries on repeated lookups.
|
|
173
|
-
*/
|
|
174
|
-
_Slip44Service_symbolCache = { value: new Map() };
|
|
175
|
-
//# sourceMappingURL=Slip44Service.mjs.map
|
|
@@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AC;YAEL,IAAI,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;gBACpC,uBAAA,IAAI,MAAiB,IAAI,GAAG,CAC1B,MAAM;qBACH,MAAM,CACL,CAAC,KAAK,EAAqD,EAAE,CAC3D,KAAK,CAAC,MAAM,KAAK,SAAS,CAC7B;qBACA,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CACjD,mCAAA,CAAC;YACJ,CAAC;iBAAM,CAAC;gBACN,2CAA2C;gBAC3C,uBAAA,IAAI,MAAiB,IAAI,GAAG,EAAE,mCAAA,CAAC;YACjC,CAAC;QACH,CAAC;QAAC,MAAM,CAAC;YACP,0EAA0E;YAC1E,uBAAA,IAAI,MAAiB,IAAI,GAAG,EAAE,mCAAA,CAAC;QACjC,CAAC;IACH,CAAC,CAAC,EAAE,mCAAA,CAAC;IAEL,MAAM,uBAAA,IAAI,uCAAc,CAAC;IACzB,uBAAA,IAAI,MAAiB,IAAI,mCAAA,CAAC;AAC5B,CAAC;AA5DD;;GAEG;AACI,uCAA4C,IAAI,EAAnC,CAAoC;AAExD;;GAEG;AACI,uCAAsC,IAAI,EAA7B,CAA8B;AAElD;;;GAGG;AACa,sCAAgD,IAAI,GAAG,EAAE,EAA7C,CAA8C","sourcesContent":["import { fetchWithErrorHandling } from '@metamask/controller-utils';\n// @ts-expect-error: No type definitions for '@metamask/slip44'\nimport slip44 from '@metamask/slip44';\n\nconst CHAINID_NETWORK_URL = 'https://chainid.network/chains.json';\n\n/**\n * Represents a single SLIP-44 entry with its metadata.\n */\nexport type Slip44Entry = {\n index: string;\n symbol: string;\n name: string;\n};\n\n/**\n * Chain data from chainid.network\n */\ntype ChainIdNetworkEntry = {\n chainId: number;\n slip44?: number;\n nativeCurrency?: {\n symbol: string;\n };\n};\n\n/**\n * Internal type for SLIP-44 data from the @metamask/slip44 package.\n * Includes the hex field which we don't expose externally.\n */\ntype Slip44DataEntry = Slip44Entry & {\n // eslint-disable-next-line id-denylist\n hex: `0x${string}`;\n};\n\n/**\n * The SLIP-44 mapping type from the @metamask/slip44 package.\n */\ntype Slip44Data = Record<string, Slip44DataEntry>;\n\n/**\n * Service for looking up SLIP-44 coin type identifiers.\n *\n * SLIP-44 defines registered coin types used in BIP-44 derivation paths.\n *\n * This service provides two lookup methods:\n * 1. `getEvmSlip44` - For EVM networks, uses chainid.network data (recommended for eip155)\n * 2. `getSlip44BySymbol` - Fallback method using @metamask/slip44 package\n *\n * @see https://github.com/satoshilabs/slips/blob/master/slip-0044.md\n * @see https://chainid.network/chains.json\n */\n/**\n * Manual overrides for EVM chain IDs where chainid.network returns\n * an incorrect SLIP-44 value due to chain ID collisions.\n */\nconst EVM_SLIP44_OVERRIDES: ReadonlyMap<number, number> = new Map([\n [999, 2457], // HyperEVM — chainid.network returns 1 (Wanchain collision)\n]);\n\nexport class Slip44Service {\n /**\n * Cache for chainId to slip44 lookups from chainid.network.\n */\n static #chainIdCache: Map<number, number> | null = null;\n\n /**\n * Whether a fetch is currently in progress.\n */\n static #fetchPromise: Promise<void> | null = null;\n\n /**\n * Cache for symbol to slip44 index lookups.\n * This avoids iterating through all entries on repeated lookups.\n */\n static readonly #symbolCache: Map<string, number | undefined> = new Map();\n\n /**\n * Fetches and caches chain data from chainid.network.\n * This is called automatically by getEvmSlip44.\n */\n static async #fetchChainData(): Promise<void> {\n if (this.#chainIdCache !== null) {\n return;\n }\n\n // Avoid duplicate fetches\n if (this.#fetchPromise) {\n await this.#fetchPromise;\n return;\n }\n\n this.#fetchPromise = (async (): Promise<void> => {\n try {\n const chains: ChainIdNetworkEntry[] | undefined =\n await fetchWithErrorHandling({\n url: CHAINID_NETWORK_URL,\n timeout: 10000,\n });\n\n if (chains && Array.isArray(chains)) {\n this.#chainIdCache = new Map(\n chains\n .filter(\n (chain): chain is ChainIdNetworkEntry & { slip44: number } =>\n chain.slip44 !== undefined,\n )\n .map((chain) => [chain.chainId, chain.slip44]),\n );\n } else {\n // Invalid response, initialize empty cache\n this.#chainIdCache = new Map();\n }\n } catch {\n // Network failed, initialize empty cache so we fall back to symbol lookup\n this.#chainIdCache = new Map();\n }\n })();\n\n await this.#fetchPromise;\n this.#fetchPromise = null;\n }\n\n /**\n * Gets the SLIP-44 coin type identifier for an EVM network by chain ID.\n *\n * **IMPORTANT: This method is for EVM networks only (eip155 namespace).**\n * For non-EVM networks (Bitcoin, Solana, Tron, etc.), use `getSlip44BySymbol`.\n *\n * This method checks chainid.network data (which maps chainId directly\n * to slip44). If not found, defaults to 60 (Ethereum).\n *\n * @param chainId - The EVM chain ID as a number (e.g., 1 for Ethereum, 56 for BNB Chain)\n * @returns The SLIP-44 coin type number (defaults to 60 if not found)\n * @example\n * ```typescript\n * // For EVM networks only\n * const ethCoinType = await Slip44Service.getEvmSlip44(1);\n * // Returns 60\n *\n * const bnbCoinType = await Slip44Service.getEvmSlip44(56);\n * // Returns 714\n *\n * const unknownEvmChain = await Slip44Service.getEvmSlip44(99999);\n * // Returns 60 (default for EVM)\n * ```\n */\n static async getEvmSlip44(chainId: number): Promise<number> {\n const override = EVM_SLIP44_OVERRIDES.get(chainId);\n if (override !== undefined) {\n return override;\n }\n\n // Ensure chain data is loaded\n await this.#fetchChainData();\n\n // Check chainId cache first\n const cached = this.#chainIdCache?.get(chainId);\n if (cached !== undefined) {\n return cached;\n }\n\n // Default to 60 (Ethereum) for EVM networks without specific mapping\n return 60;\n }\n\n /**\n * Gets the SLIP-44 coin type identifier for a non-EVM network by symbol.\n *\n * **IMPORTANT: This method is for non-EVM networks only (Bitcoin, Solana, Tron, etc.).**\n * For EVM networks (eip155 namespace), use `getEvmSlip44` instead.\n *\n * Note: Symbol lookup may return incorrect results for duplicate symbols\n * (e.g., CPC is both CPChain and Capricoin).\n *\n * @param symbol - The network symbol (e.g., 'ETH', 'BTC', 'SOL')\n * @returns The SLIP-44 coin type number, or undefined if not found\n * @example\n * ```typescript\n * const ethCoinType = Slip44Service.getSlip44BySymbol('ETH');\n * // Returns 60\n *\n * const btcCoinType = Slip44Service.getSlip44BySymbol('BTC');\n * // Returns 0\n * ```\n */\n static getSlip44BySymbol(symbol: string): number | undefined {\n // Check cache first\n if (this.#symbolCache.has(symbol)) {\n return this.#symbolCache.get(symbol);\n }\n\n const slip44Data = slip44 as Slip44Data;\n const upperSymbol = symbol.toUpperCase();\n\n // Iterate through all entries to find matching symbol\n // Note: Object.keys returns numeric keys in ascending order,\n // so for duplicate symbols we get the lowest coin type first\n // (which is the convention for resolving duplicates)\n for (const key of Object.keys(slip44Data)) {\n const entry = slip44Data[key];\n if (entry.symbol.toUpperCase() === upperSymbol) {\n const coinType = parseInt(key, 10);\n this.#symbolCache.set(symbol, coinType);\n return coinType;\n }\n }\n\n // Cache the miss as well to avoid repeated lookups\n this.#symbolCache.set(symbol, undefined);\n return undefined;\n }\n\n /**\n * Clears all internal caches.\n * Useful for testing or if the underlying data might change.\n */\n static clearCache(): void {\n this.#symbolCache.clear();\n this.#chainIdCache = null;\n this.#fetchPromise = null;\n }\n}\n"]}
|
package/dist/services/index.cjs
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getSlip44BySymbol = exports.getEvmSlip44 = exports.Slip44Service = void 0;
|
|
4
|
-
const Slip44Service_js_1 = require("./Slip44Service.cjs");
|
|
5
|
-
Object.defineProperty(exports, "Slip44Service", { enumerable: true, get: function () { return Slip44Service_js_1.Slip44Service; } });
|
|
6
|
-
// Re-export static methods as standalone functions for convenience
|
|
7
|
-
// getEvmSlip44: For EVM networks (eip155) - uses chainId lookup, defaults to 60
|
|
8
|
-
exports.getEvmSlip44 = Slip44Service_js_1.Slip44Service.getEvmSlip44.bind(Slip44Service_js_1.Slip44Service);
|
|
9
|
-
// getSlip44BySymbol: For non-EVM networks (Bitcoin, Solana, Tron) - uses symbol lookup
|
|
10
|
-
exports.getSlip44BySymbol = Slip44Service_js_1.Slip44Service.getSlip44BySymbol.bind(Slip44Service_js_1.Slip44Service);
|
|
11
|
-
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":";;;AAAA,0DAAmD;AAE1C,8FAFA,gCAAa,OAEA;AAEtB,mEAAmE;AACnE,gFAAgF;AACnE,QAAA,YAAY,GAAG,gCAAa,CAAC,YAAY,CAAC,IAAI,CAAC,gCAAa,CAAC,CAAC;AAC3E,uFAAuF;AAC1E,QAAA,iBAAiB,GAC5B,gCAAa,CAAC,iBAAiB,CAAC,IAAI,CAAC,gCAAa,CAAC,CAAC","sourcesContent":["import { Slip44Service } from './Slip44Service.js';\n\nexport { Slip44Service };\n\n// Re-export static methods as standalone functions for convenience\n// getEvmSlip44: For EVM networks (eip155) - uses chainId lookup, defaults to 60\nexport const getEvmSlip44 = Slip44Service.getEvmSlip44.bind(Slip44Service);\n// getSlip44BySymbol: For non-EVM networks (Bitcoin, Solana, Tron) - uses symbol lookup\nexport const getSlip44BySymbol =\n Slip44Service.getSlip44BySymbol.bind(Slip44Service);\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,4BAA2B;AAEnD,OAAO,EAAE,aAAa,EAAE,CAAC;AAIzB,eAAO,MAAM,YAAY,mCAAiD,CAAC;AAE3E,eAAO,MAAM,iBAAiB,wCACuB,CAAC"}
|
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { Slip44Service } from "./Slip44Service.mjs";
|
|
2
|
-
export { Slip44Service };
|
|
3
|
-
export declare const getEvmSlip44: typeof Slip44Service.getEvmSlip44;
|
|
4
|
-
export declare const getSlip44BySymbol: typeof Slip44Service.getSlip44BySymbol;
|
|
5
|
-
//# sourceMappingURL=index.d.mts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,4BAA2B;AAEnD,OAAO,EAAE,aAAa,EAAE,CAAC;AAIzB,eAAO,MAAM,YAAY,mCAAiD,CAAC;AAE3E,eAAO,MAAM,iBAAiB,wCACuB,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,4BAA2B;AAEnD,OAAO,EAAE,aAAa,EAAE,CAAC;AAEzB,mEAAmE;AACnE,gFAAgF;AAChF,MAAM,CAAC,MAAM,YAAY,GAAG,aAAa,CAAC,YAAY,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;AAC3E,uFAAuF;AACvF,MAAM,CAAC,MAAM,iBAAiB,GAC5B,aAAa,CAAC,iBAAiB,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC","sourcesContent":["import { Slip44Service } from './Slip44Service.js';\n\nexport { Slip44Service };\n\n// Re-export static methods as standalone functions for convenience\n// getEvmSlip44: For EVM networks (eip155) - uses chainId lookup, defaults to 60\nexport const getEvmSlip44 = Slip44Service.getEvmSlip44.bind(Slip44Service);\n// getSlip44BySymbol: For non-EVM networks (Bitcoin, Solana, Tron) - uses symbol lookup\nexport const getSlip44BySymbol =\n Slip44Service.getSlip44BySymbol.bind(Slip44Service);\n"]}
|
package/dist/utils.cjs
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
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
|
-
/**
|
|
8
|
-
* Derives the namespace, storage key, and CAIP chain ID from a given chain ID.
|
|
9
|
-
*
|
|
10
|
-
* This utility function handles the conversion between different chain ID formats.
|
|
11
|
-
* For EVM networks, it converts Hex chain IDs to CAIP-2 format and determines
|
|
12
|
-
* the appropriate storage key. For non-EVM networks, it parses the CAIP-2 chain ID
|
|
13
|
-
* and uses the full chain ID as the storage key.
|
|
14
|
-
*
|
|
15
|
-
* @param chainId - The chain ID to derive keys from (Hex or CAIP-2 format)
|
|
16
|
-
* @returns An object containing namespace, storageKey, and caipId
|
|
17
|
-
* @throws Error if the chain ID cannot be parsed
|
|
18
|
-
*/
|
|
19
|
-
function deriveKeys(chainId) {
|
|
20
|
-
const caipChainId = (0, utils_1.isCaipChainId)(chainId)
|
|
21
|
-
? chainId
|
|
22
|
-
: (0, multichain_network_controller_1.toEvmCaipChainId)(chainId);
|
|
23
|
-
const { namespace, reference } = (0, utils_1.parseCaipChainId)(caipChainId);
|
|
24
|
-
let storageKey;
|
|
25
|
-
if (namespace === utils_1.KnownCaipNamespace.Eip155) {
|
|
26
|
-
storageKey = (0, utils_1.isHexString)(chainId) ? chainId : (0, controller_utils_1.toHex)(reference);
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
storageKey = caipChainId;
|
|
30
|
-
}
|
|
31
|
-
return { namespace, storageKey, caipChainId, reference };
|
|
32
|
-
}
|
|
33
|
-
exports.deriveKeys = deriveKeys;
|
|
34
|
-
/**
|
|
35
|
-
* Checks if the specified network is the only enabled network in its namespace.
|
|
36
|
-
*
|
|
37
|
-
* This function is used to prevent unnecessary state updates when trying to enable
|
|
38
|
-
* This method is used to prevent the last network in a namespace from being removed.
|
|
39
|
-
*
|
|
40
|
-
* @param state - The current controller state
|
|
41
|
-
* @param derivedKeys - The parsed keys object containing namespace and storageKey
|
|
42
|
-
* @returns True if the network is the only enabled network in the namespace, false otherwise
|
|
43
|
-
*/
|
|
44
|
-
function isOnlyNetworkEnabledInNamespace(state, derivedKeys) {
|
|
45
|
-
const { namespace, storageKey } = derivedKeys;
|
|
46
|
-
// Early return if namespace doesn't exist
|
|
47
|
-
if (!state.enabledNetworkMap[namespace]) {
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
const networks = state.enabledNetworkMap[namespace];
|
|
51
|
-
// Get all enabled networks in this namespace
|
|
52
|
-
const enabledNetworks = Object.entries(networks).filter(([_, enabled]) => enabled);
|
|
53
|
-
// Check if there's exactly one enabled network and it matches our target
|
|
54
|
-
if (enabledNetworks.length === 1) {
|
|
55
|
-
const [onlyEnabledKey] = enabledNetworks[0];
|
|
56
|
-
return onlyEnabledKey === storageKey;
|
|
57
|
-
}
|
|
58
|
-
// Return false if there are zero or multiple enabled networks
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
exports.isOnlyNetworkEnabledInNamespace = isOnlyNetworkEnabledInNamespace;
|
|
62
|
-
//# sourceMappingURL=utils.cjs.map
|
package/dist/utils.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAAA,iEAAmD;AACnD,2FAA2E;AAE3E,2CAKyB;AAczB;;;;;;;;;;;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,EAAE,SAAS,EAAE,GAAG,IAAA,wBAAgB,EAAC,WAAW,CAAC,CAAC;IAC/D,IAAI,UAAU,CAAC;IACf,IAAI,SAAS,KAAM,0BAAkB,CAAC,MAAiB,EAAE,CAAC;QACxD,UAAU,GAAG,IAAA,mBAAW,EAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAA,wBAAK,EAAC,SAAS,CAAC,CAAC;IACjE,CAAC;SAAM,CAAC;QACN,UAAU,GAAG,WAAW,CAAC;IAC3B,CAAC;IACD,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,WAAW,EAAE,SAAS,EAAE,CAAC;AAC3D,CAAC;AAbD,gCAaC;AAED;;;;;;;;;GASG;AACH,SAAgB,+BAA+B,CAC7C,KAAuC,EACvC,WAAwB;IAExB,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,WAAW,CAAC;IAE9C,0CAA0C;IAC1C,IAAI,CAAC,KAAK,CAAC,iBAAiB,CAAC,SAAS,CAAC,EAAE,CAAC;QACxC,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,QAAQ,GAAG,KAAK,CAAC,iBAAiB,CAAC,SAAS,CAAC,CAAC;IAEpD,6CAA6C;IAC7C,MAAM,eAAe,GAAG,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,MAAM,CACrD,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,EAAE,CAAC,OAAO,CAC1B,CAAC;IAEF,yEAAyE;IACzE,IAAI,eAAe,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACjC,MAAM,CAAC,cAAc,CAAC,GAAG,eAAe,CAAC,CAAC,CAAC,CAAC;QAC5C,OAAO,cAAc,KAAK,UAAU,CAAC;IACvC,CAAC;IAED,8DAA8D;IAC9D,OAAO,KAAK,CAAC;AACf,CAAC;AA1BD,0EA0BC","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 type { NetworkEnablementControllerState } from './NetworkEnablementController.js';\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"]}
|
package/dist/utils.d.cts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
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
|
-
//# sourceMappingURL=utils.d.cts.map
|
package/dist/utils.d.cts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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;AAQvE,OAAO,KAAK,EAAE,gCAAgC,EAAE,0CAAyC;AAEzF;;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"}
|
package/dist/utils.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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;AAQvE,OAAO,KAAK,EAAE,gCAAgC,EAAE,0CAAyC;AAEzF;;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,CAC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,KAAK,CAAC,iBAAiB,CAAC,SAAS,CAAC,CAAC;IAEpD,6CAA6C;IAC7C,MAAM,eAAe,GAAG,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,MAAM,CACrD,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,EAAE,CAAC,OAAO,CAC1B,CAAC;IAEF,yEAAyE;IACzE,IAAI,eAAe,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACjC,MAAM,CAAC,cAAc,CAAC,GAAG,eAAe,CAAC,CAAC,CAAC,CAAC;QAC5C,OAAO,cAAc,KAAK,UAAU,CAAC;IACvC,CAAC;IAED,8DAA8D;IAC9D,OAAO,KAAK,CAAC;AACf,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 type { NetworkEnablementControllerState } from './NetworkEnablementController.js';\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"]}
|