@metamask/multichain-network-controller 0.2.0 → 0.4.0

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 (40) hide show
  1. package/CHANGELOG.md +28 -2
  2. package/dist/MultichainNetworkController.cjs +46 -6
  3. package/dist/MultichainNetworkController.cjs.map +1 -1
  4. package/dist/MultichainNetworkController.d.cts +9 -0
  5. package/dist/MultichainNetworkController.d.cts.map +1 -1
  6. package/dist/MultichainNetworkController.d.mts +9 -0
  7. package/dist/MultichainNetworkController.d.mts.map +1 -1
  8. package/dist/MultichainNetworkController.mjs +48 -8
  9. package/dist/MultichainNetworkController.mjs.map +1 -1
  10. package/dist/constants.cjs +67 -4
  11. package/dist/constants.cjs.map +1 -1
  12. package/dist/constants.d.cts +22 -1
  13. package/dist/constants.d.cts.map +1 -1
  14. package/dist/constants.d.mts +22 -1
  15. package/dist/constants.d.mts.map +1 -1
  16. package/dist/constants.mjs +66 -3
  17. package/dist/constants.mjs.map +1 -1
  18. package/dist/index.cjs +5 -1
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.cts +1 -1
  21. package/dist/index.d.cts.map +1 -1
  22. package/dist/index.d.mts +1 -1
  23. package/dist/index.d.mts.map +1 -1
  24. package/dist/index.mjs +1 -1
  25. package/dist/index.mjs.map +1 -1
  26. package/dist/types.cjs.map +1 -1
  27. package/dist/types.d.cts +3 -3
  28. package/dist/types.d.cts.map +1 -1
  29. package/dist/types.d.mts +3 -3
  30. package/dist/types.d.mts.map +1 -1
  31. package/dist/types.mjs.map +1 -1
  32. package/dist/utils.cjs +32 -6
  33. package/dist/utils.cjs.map +1 -1
  34. package/dist/utils.d.cts +14 -0
  35. package/dist/utils.d.cts.map +1 -1
  36. package/dist/utils.d.mts +14 -0
  37. package/dist/utils.d.mts.map +1 -1
  38. package/dist/utils.mjs +30 -6
  39. package/dist/utils.mjs.map +1 -1
  40. package/package.json +7 -6
package/dist/utils.mjs CHANGED
@@ -1,7 +1,17 @@
1
1
  import { BtcScope, SolScope } from "@metamask/keyring-api";
2
- import { KnownCaipNamespace, toCaipChainId, hexToNumber } from "@metamask/utils";
2
+ import { KnownCaipNamespace, toCaipChainId, parseCaipChainId, hexToNumber, add0x } from "@metamask/utils";
3
3
  import { isAddress as isSolanaAddress } from "@solana/addresses";
4
4
  import { AVAILABLE_MULTICHAIN_NETWORK_CONFIGURATIONS } from "./constants.mjs";
5
+ /**
6
+ * Checks if the chain ID is EVM.
7
+ *
8
+ * @param chainId - The account type to check.
9
+ * @returns Whether the network is EVM.
10
+ */
11
+ export function isEvmCaipChainId(chainId) {
12
+ const { namespace } = parseCaipChainId(chainId);
13
+ return namespace === KnownCaipNamespace.Eip155;
14
+ }
5
15
  /**
6
16
  * Returns the chain id of the non-EVM network based on the account address.
7
17
  *
@@ -32,6 +42,19 @@ export function checkIfSupportedCaipChainId(id) {
32
42
  * @returns The Caip chain ID.
33
43
  */
34
44
  export const toEvmCaipChainId = (chainId) => toCaipChainId(KnownCaipNamespace.Eip155, hexToNumber(chainId).toString());
45
+ /**
46
+ * Convert an eip155 CAIP chain ID to a hex chain ID.
47
+ *
48
+ * @param chainId - The CAIP chain ID to convert.
49
+ * @returns The hex chain ID.
50
+ */
51
+ export function convertEvmCaipToHexChainId(chainId) {
52
+ const { namespace, reference } = parseCaipChainId(chainId);
53
+ if (namespace === KnownCaipNamespace.Eip155) {
54
+ return add0x(parseInt(reference, 10).toString(16));
55
+ }
56
+ throw new Error(`Unsupported CAIP chain ID namespace: ${namespace}. Only eip155 is supported.`);
57
+ }
35
58
  /**
36
59
  * Updates a network configuration to the format used by the MultichainNetworkController.
37
60
  * This method is exclusive for EVM networks with hex identifiers from the NetworkController.
@@ -40,13 +63,14 @@ export const toEvmCaipChainId = (chainId) => toCaipChainId(KnownCaipNamespace.Ei
40
63
  * @returns The updated network configuration.
41
64
  */
42
65
  export const toMultichainNetworkConfiguration = (network) => {
66
+ const { chainId, name, rpcEndpoints, defaultRpcEndpointIndex, nativeCurrency, blockExplorerUrls, defaultBlockExplorerUrlIndex, } = network;
43
67
  return {
44
- chainId: toEvmCaipChainId(network.chainId),
68
+ chainId: toEvmCaipChainId(chainId),
45
69
  isEvm: true,
46
- name: network.name,
47
- nativeCurrency: network.nativeCurrency,
48
- blockExplorerUrls: network.blockExplorerUrls,
49
- defaultBlockExplorerUrlIndex: network.defaultBlockExplorerUrlIndex || 0,
70
+ name: name || rpcEndpoints[defaultRpcEndpointIndex].url,
71
+ nativeCurrency,
72
+ blockExplorerUrls,
73
+ defaultBlockExplorerUrlIndex: defaultBlockExplorerUrlIndex || 0,
50
74
  };
51
75
  };
52
76
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,8BAA8B;AAE3D,OAAO,EAGL,kBAAkB,EAClB,aAAa,EACb,WAAW,EACZ,wBAAwB;AACzB,OAAO,EAAE,SAAS,IAAI,eAAe,EAAE,0BAA0B;AAEjE,OAAO,EAAE,2CAA2C,EAAE,wBAAoB;AAM1E;;;;;GAKG;AACH,MAAM,UAAU,0BAA0B,CACxC,OAAe;IAEf,2GAA2G;IAC3G,IAAI,eAAe,CAAC,OAAO,CAAC,EAAE;QAC5B,OAAO,QAAQ,CAAC,OAAO,CAAC;KACzB;IACD,OAAO,QAAQ,CAAC,OAAO,CAAC;AAC1B,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,2BAA2B,CACzC,EAAe;IAEf,qCAAqC;IACrC,OAAO,MAAM,CAAC,IAAI,CAAC,2CAA2C,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;AAC/E,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,OAAY,EAAe,EAAE,CAC5D,aAAa,CAAC,kBAAkB,CAAC,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC;AAE5E;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,gCAAgC,GAAG,CAC9C,OAA6B,EACG,EAAE;IAClC,OAAO;QACL,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,OAAO,CAAC;QAC1C,KAAK,EAAE,IAAI;QACX,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,cAAc,EAAE,OAAO,CAAC,cAAc;QACtC,iBAAiB,EAAE,OAAO,CAAC,iBAAiB;QAC5C,4BAA4B,EAAE,OAAO,CAAC,4BAA4B,IAAI,CAAC;KACxE,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,0CAA0C,GAAG,CACxD,8BAAoE,EACf,EAAE,CACvD,MAAM,CAAC,OAAO,CAAC,8BAA8B,CAAC,CAAC,MAAM,CACnD,CAAC,GAAG,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,EAAE,CAAC,CAAC;IACrB,GAAG,GAAG;IACN,CAAC,gBAAgB,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,EACjC,gCAAgC,CAAC,OAAO,CAAC;CAC5C,CAAC,EACF,EAAE,CACH,CAAC","sourcesContent":["import { BtcScope, SolScope } from '@metamask/keyring-api';\nimport type { NetworkConfiguration } from '@metamask/network-controller';\nimport {\n type Hex,\n type CaipChainId,\n KnownCaipNamespace,\n toCaipChainId,\n hexToNumber,\n} from '@metamask/utils';\nimport { isAddress as isSolanaAddress } from '@solana/addresses';\n\nimport { AVAILABLE_MULTICHAIN_NETWORK_CONFIGURATIONS } from './constants';\nimport type {\n SupportedCaipChainId,\n MultichainNetworkConfiguration,\n} from './types';\n\n/**\n * Returns the chain id of the non-EVM network based on the account address.\n *\n * @param address - The address to check.\n * @returns The caip chain id of the non-EVM network.\n */\nexport function getChainIdForNonEvmAddress(\n address: string,\n): SupportedCaipChainId {\n // This condition is not the most robust. Once we support more networks, we will need to update this logic.\n if (isSolanaAddress(address)) {\n return SolScope.Mainnet;\n }\n return BtcScope.Mainnet;\n}\n\n/**\n * Checks if the Caip chain ID is supported.\n *\n * @param id - The Caip chain IDto check.\n * @returns Whether the chain ID is supported.\n */\nexport function checkIfSupportedCaipChainId(\n id: CaipChainId,\n): id is SupportedCaipChainId {\n // Check if the chain id is supported\n return Object.keys(AVAILABLE_MULTICHAIN_NETWORK_CONFIGURATIONS).includes(id);\n}\n\n/**\n * Converts a hex chain ID to a Caip chain ID.\n *\n * @param chainId - The hex chain ID to convert.\n * @returns The Caip chain ID.\n */\nexport const toEvmCaipChainId = (chainId: Hex): CaipChainId =>\n toCaipChainId(KnownCaipNamespace.Eip155, hexToNumber(chainId).toString());\n\n/**\n * Updates a network configuration to the format used by the MultichainNetworkController.\n * This method is exclusive for EVM networks with hex identifiers from the NetworkController.\n *\n * @param network - The network configuration to update.\n * @returns The updated network configuration.\n */\nexport const toMultichainNetworkConfiguration = (\n network: NetworkConfiguration,\n): MultichainNetworkConfiguration => {\n return {\n chainId: toEvmCaipChainId(network.chainId),\n isEvm: true,\n name: network.name,\n nativeCurrency: network.nativeCurrency,\n blockExplorerUrls: network.blockExplorerUrls,\n defaultBlockExplorerUrlIndex: network.defaultBlockExplorerUrlIndex || 0,\n };\n};\n\n/**\n * Updates a record of network configurations to the format used by the MultichainNetworkController.\n * This method is exclusive for EVM networks with hex identifiers from the NetworkController.\n *\n * @param networkConfigurationsByChainId - The network configurations to update.\n * @returns The updated network configurations.\n */\nexport const toMultichainNetworkConfigurationsByChainId = (\n networkConfigurationsByChainId: Record<string, NetworkConfiguration>,\n): Record<CaipChainId, MultichainNetworkConfiguration> =>\n Object.entries(networkConfigurationsByChainId).reduce(\n (acc, [, network]) => ({\n ...acc,\n [toEvmCaipChainId(network.chainId)]:\n toMultichainNetworkConfiguration(network),\n }),\n {},\n );\n"]}
1
+ {"version":3,"file":"utils.mjs","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAA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AAC,CAAC;KACpD;IAED,MAAM,IAAI,KAAK,CACb,wCAAwC,SAAS,6BAA6B,CAC/E,CAAC;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,gCAAgC,GAAG,CAC9C,OAA6B,EACG,EAAE;IAClC,MAAM,EACJ,OAAO,EACP,IAAI,EACJ,YAAY,EACZ,uBAAuB,EACvB,cAAc,EACd,iBAAiB,EACjB,4BAA4B,GAC7B,GAAG,OAAO,CAAC;IACZ,OAAO;QACL,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC;QAClC,KAAK,EAAE,IAAI;QACX,IAAI,EAAE,IAAI,IAAI,YAAY,CAAC,uBAAuB,CAAC,CAAC,GAAG;QACvD,cAAc;QACd,iBAAiB;QACjB,4BAA4B,EAAE,4BAA4B,IAAI,CAAC;KAChE,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,0CAA0C,GAAG,CACxD,8BAAoE,EACf,EAAE,CACvD,MAAM,CAAC,OAAO,CAAC,8BAA8B,CAAC,CAAC,MAAM,CACnD,CAAC,GAAG,EAAE,CAAC,EAAE,OAAO,CAAC,EAAE,EAAE,CAAC,CAAC;IACrB,GAAG,GAAG;IACN,CAAC,gBAAgB,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,EACjC,gCAAgC,CAAC,OAAO,CAAC;CAC5C,CAAC,EACF,EAAE,CACH,CAAC","sourcesContent":["import { BtcScope, SolScope } from '@metamask/keyring-api';\nimport type { NetworkConfiguration } from '@metamask/network-controller';\nimport {\n type Hex,\n type CaipChainId,\n KnownCaipNamespace,\n toCaipChainId,\n parseCaipChainId,\n hexToNumber,\n add0x,\n} from '@metamask/utils';\nimport { isAddress as isSolanaAddress } from '@solana/addresses';\n\nimport { AVAILABLE_MULTICHAIN_NETWORK_CONFIGURATIONS } from './constants';\nimport type {\n SupportedCaipChainId,\n MultichainNetworkConfiguration,\n} from './types';\n\n/**\n * Checks if the chain ID is EVM.\n *\n * @param chainId - The account type to check.\n * @returns Whether the network is EVM.\n */\nexport function isEvmCaipChainId(chainId: CaipChainId): boolean {\n const { namespace } = parseCaipChainId(chainId);\n return namespace === (KnownCaipNamespace.Eip155 as string);\n}\n\n/**\n * Returns the chain id of the non-EVM network based on the account address.\n *\n * @param address - The address to check.\n * @returns The caip chain id of the non-EVM network.\n */\nexport function getChainIdForNonEvmAddress(\n address: string,\n): SupportedCaipChainId {\n // This condition is not the most robust. Once we support more networks, we will need to update this logic.\n if (isSolanaAddress(address)) {\n return SolScope.Mainnet;\n }\n return BtcScope.Mainnet;\n}\n\n/**\n * Checks if the Caip chain ID is supported.\n *\n * @param id - The Caip chain IDto check.\n * @returns Whether the chain ID is supported.\n */\nexport function checkIfSupportedCaipChainId(\n id: CaipChainId,\n): id is SupportedCaipChainId {\n // Check if the chain id is supported\n return Object.keys(AVAILABLE_MULTICHAIN_NETWORK_CONFIGURATIONS).includes(id);\n}\n\n/**\n * Converts a hex chain ID to a Caip chain ID.\n *\n * @param chainId - The hex chain ID to convert.\n * @returns The Caip chain ID.\n */\nexport const toEvmCaipChainId = (chainId: Hex): CaipChainId =>\n toCaipChainId(KnownCaipNamespace.Eip155, hexToNumber(chainId).toString());\n\n/**\n * Convert an eip155 CAIP chain ID to a hex chain ID.\n *\n * @param chainId - The CAIP chain ID to convert.\n * @returns The hex chain ID.\n */\nexport function convertEvmCaipToHexChainId(chainId: CaipChainId): Hex {\n const { namespace, reference } = parseCaipChainId(chainId);\n if (namespace === (KnownCaipNamespace.Eip155 as string)) {\n return add0x(parseInt(reference, 10).toString(16));\n }\n\n throw new Error(\n `Unsupported CAIP chain ID namespace: ${namespace}. Only eip155 is supported.`,\n );\n}\n\n/**\n * Updates a network configuration to the format used by the MultichainNetworkController.\n * This method is exclusive for EVM networks with hex identifiers from the NetworkController.\n *\n * @param network - The network configuration to update.\n * @returns The updated network configuration.\n */\nexport const toMultichainNetworkConfiguration = (\n network: NetworkConfiguration,\n): MultichainNetworkConfiguration => {\n const {\n chainId,\n name,\n rpcEndpoints,\n defaultRpcEndpointIndex,\n nativeCurrency,\n blockExplorerUrls,\n defaultBlockExplorerUrlIndex,\n } = network;\n return {\n chainId: toEvmCaipChainId(chainId),\n isEvm: true,\n name: name || rpcEndpoints[defaultRpcEndpointIndex].url,\n nativeCurrency,\n blockExplorerUrls,\n defaultBlockExplorerUrlIndex: defaultBlockExplorerUrlIndex || 0,\n };\n};\n\n/**\n * Updates a record of network configurations to the format used by the MultichainNetworkController.\n * This method is exclusive for EVM networks with hex identifiers from the NetworkController.\n *\n * @param networkConfigurationsByChainId - The network configurations to update.\n * @returns The updated network configurations.\n */\nexport const toMultichainNetworkConfigurationsByChainId = (\n networkConfigurationsByChainId: Record<string, NetworkConfiguration>,\n): Record<CaipChainId, MultichainNetworkConfiguration> =>\n Object.entries(networkConfigurationsByChainId).reduce(\n (acc, [, network]) => ({\n ...acc,\n [toEvmCaipChainId(network.chainId)]:\n toMultichainNetworkConfiguration(network),\n }),\n {},\n );\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask/multichain-network-controller",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Multichain network controller",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -48,14 +48,15 @@
48
48
  },
49
49
  "dependencies": {
50
50
  "@metamask/base-controller": "^8.0.0",
51
- "@metamask/keyring-api": "^17.2.0",
51
+ "@metamask/keyring-api": "^17.4.0",
52
52
  "@metamask/utils": "^11.2.0",
53
53
  "@solana/addresses": "^2.0.0"
54
54
  },
55
55
  "devDependencies": {
56
+ "@metamask/accounts-controller": "^27.0.0",
56
57
  "@metamask/auto-changelog": "^3.4.4",
57
- "@metamask/keyring-controller": "^21.0.0",
58
- "@metamask/network-controller": "^22.2.1",
58
+ "@metamask/keyring-controller": "^21.0.2",
59
+ "@metamask/network-controller": "^23.2.0",
59
60
  "@types/jest": "^27.4.1",
60
61
  "@types/uuid": "^8.3.0",
61
62
  "deepmerge": "^4.2.2",
@@ -68,8 +69,8 @@
68
69
  "typescript": "~5.2.2"
69
70
  },
70
71
  "peerDependencies": {
71
- "@metamask/accounts-controller": "^26.0.0",
72
- "@metamask/network-controller": "^22.0.0"
72
+ "@metamask/accounts-controller": "^27.0.0",
73
+ "@metamask/network-controller": "^23.0.0"
73
74
  },
74
75
  "engines": {
75
76
  "node": "^18.18 || >=20"