@metamask-previews/chain-agnostic-permission 0.5.0-preview-fa22a478 → 0.6.0-preview-64413dce

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 CHANGED
@@ -7,8 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.6.0]
11
+
10
12
  ### Changed
11
13
 
14
+ - Fix `getAllNamespacesFromCaip25CaveatValue` to return the reference instead of full scope when passed in values are `wallet` namespaced ([#5759](https://github.com/MetaMask/core/pull/5759))
12
15
  - Bump `@metamask/network-controller` to `^23.3.0` ([#5789](https://github.com/MetaMask/core/pull/5789))
13
16
 
14
17
  ## [0.5.0]
@@ -73,7 +76,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
73
76
 
74
77
  - Initial release
75
78
 
76
- [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.5.0...HEAD
79
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.6.0...HEAD
80
+ [0.6.0]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.5.0...@metamask/chain-agnostic-permission@0.6.0
77
81
  [0.5.0]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.4.0...@metamask/chain-agnostic-permission@0.5.0
78
82
  [0.4.0]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.3.0...@metamask/chain-agnostic-permission@0.4.0
79
83
  [0.3.0]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.2.0...@metamask/chain-agnostic-permission@0.3.0
@@ -175,9 +175,9 @@ function getAllNamespacesFromCaip25CaveatValue(caip25CaveatValue) {
175
175
  const allScopes = getAllScopesFromCaip25CaveatValue(caip25CaveatValue);
176
176
  const namespaceSet = new Set();
177
177
  for (const scope of allScopes) {
178
- const { namespace } = (0, types_1.parseScopeString)(scope);
178
+ const { namespace, reference } = (0, types_1.parseScopeString)(scope);
179
179
  if (namespace === utils_1.KnownCaipNamespace.Wallet) {
180
- namespaceSet.add(scope);
180
+ namespaceSet.add(reference ?? namespace);
181
181
  }
182
182
  else if (namespace) {
183
183
  namespaceSet.add(namespace);
@@ -1 +1 @@
1
- {"version":3,"file":"caip-permission-adapter-permittedChains.cjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-permittedChains.ts"],"names":[],"mappings":";;;AAAA,iEAAmD;AAEnD,2CAAkE;AAElE,8DAA+E;AAC/E,sDAAyD;AAEzD,8CAAiE;AAEjE;;;;;;GAMG;AAEH;;;;;GAKG;AACH,MAAM,iCAAiC,GAAG,CAAC,MAA4B,EAAE,EAAE;IACzE,MAAM,WAAW,GAAU,EAAE,CAAC;IAE9B,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE;QAC1C,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,IAAA,wBAAgB,EAAC,WAAW,CAAC,CAAC;QAC/D,IAAI,SAAS,KAAK,0BAAkB,CAAC,MAAM,IAAI,SAAS,EAAE;YACxD,WAAW,CAAC,IAAI,CAAC,IAAA,wBAAK,EAAC,SAAS,CAAC,CAAC,CAAC;SACpC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAEF;;;;;GAKG;AACI,MAAM,uBAAuB,GAAG,CACrC,iBAGC,EACD,EAAE;IACF,MAAM,EAAE,cAAc,EAAE,cAAc,EAAE,GAAG,iBAAiB,CAAC;IAE7D,MAAM,WAAW,GAAU;QACzB,GAAG,iCAAiC,CAAC,cAAc,CAAC;QACpD,GAAG,iCAAiC,CAAC,cAAc,CAAC;KACrD,CAAC;IAEF,OAAO,IAAA,+BAAmB,EAAC,WAAW,CAAC,CAAC;AAC1C,CAAC,CAAC;AAdW,QAAA,uBAAuB,2B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AAC,OAAO,CAAC,EAAE;YACtE,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,GAAG,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC;SACnD;KACF;IAED,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAjCW,QAAA,8BAA8B,kCAiCzC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport type { Hex, CaipChainId, CaipNamespace } from '@metamask/utils';\nimport { hexToBigInt, KnownCaipNamespace } from '@metamask/utils';\n\nimport { Caip25CaveatType, type Caip25CaveatValue } from '../caip25Permission';\nimport { getUniqueArrayItems } from '../scope/transform';\nimport type { InternalScopesObject, InternalScopeString } from '../scope/types';\nimport { isWalletScope, parseScopeString } from '../scope/types';\n\n/*\n *\n *\n * EVM SPECIFIC GETTERS AND SETTERS\n *\n *\n */\n\n/**\n * Gets the Ethereum (EIP155 namespaced) chainIDs from internal scopes.\n *\n * @param scopes - The internal scopes from which to get the Ethereum chainIDs.\n * @returns An array of Ethereum chainIDs.\n */\nconst getPermittedEthChainIdsFromScopes = (scopes: InternalScopesObject) => {\n const ethChainIds: Hex[] = [];\n\n Object.keys(scopes).forEach((scopeString) => {\n const { namespace, reference } = parseScopeString(scopeString);\n if (namespace === KnownCaipNamespace.Eip155 && reference) {\n ethChainIds.push(toHex(reference));\n }\n });\n\n return ethChainIds;\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) chainIDs from the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value from which to get the Ethereum chainIDs.\n * @returns An array of Ethereum chainIDs.\n */\nexport const getPermittedEthChainIds = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n) => {\n const { requiredScopes, optionalScopes } = caip25CaveatValue;\n\n const ethChainIds: Hex[] = [\n ...getPermittedEthChainIdsFromScopes(requiredScopes),\n ...getPermittedEthChainIdsFromScopes(optionalScopes),\n ];\n\n return getUniqueArrayItems(ethChainIds);\n};\n\n/**\n * Adds an Ethereum (EIP155 namespaced) chainID to the optional scopes if it is not already present\n * in either the pre-existing required or optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to add the Ethereum chainID to.\n * @param chainId - The Ethereum chainID to add.\n * @returns The updated CAIP-25 caveat value with the added Ethereum chainID.\n */\nexport const addPermittedEthChainId = (\n caip25CaveatValue: Caip25CaveatValue,\n chainId: Hex,\n): Caip25CaveatValue => {\n const scopeString = `eip155:${hexToBigInt(chainId).toString(10)}`;\n if (\n Object.keys(caip25CaveatValue.requiredScopes).includes(scopeString) ||\n Object.keys(caip25CaveatValue.optionalScopes).includes(scopeString)\n ) {\n return caip25CaveatValue;\n }\n\n return {\n ...caip25CaveatValue,\n optionalScopes: {\n ...caip25CaveatValue.optionalScopes,\n [scopeString]: {\n accounts: [],\n },\n },\n };\n};\n\n/**\n * Filters the scopes object to only include:\n * - Scopes without references (e.g. \"wallet:\")\n * - EIP155 scopes for the given chainIDs\n * - Non EIP155 scopes (e.g. \"bip122:\" or any other non ethereum namespaces)\n *\n * @param scopesObject - The scopes object to filter.\n * @param chainIds - The chainIDs to filter EIP155 scopes by.\n * @returns The filtered scopes object.\n */\nconst filterEthScopesObjectByChainId = (\n scopesObject: InternalScopesObject,\n chainIds: Hex[],\n): InternalScopesObject => {\n const updatedScopesObject: InternalScopesObject = {};\n\n Object.entries(scopesObject).forEach(([key, scopeObject]) => {\n // Cast needed because index type is returned as `string` by `Object.entries`\n const scopeString = key as keyof typeof scopesObject;\n const { namespace, reference } = parseScopeString(scopeString);\n if (!reference) {\n updatedScopesObject[scopeString] = scopeObject;\n return;\n }\n if (namespace === KnownCaipNamespace.Eip155) {\n const chainId = toHex(reference);\n if (chainIds.includes(chainId)) {\n updatedScopesObject[scopeString] = scopeObject;\n }\n } else {\n updatedScopesObject[scopeString] = scopeObject;\n }\n });\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the permitted Ethereum (EIP155 namespaced) chainIDs for the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted Ethereum chainIDs for.\n * @param chainIds - The Ethereum chainIDs to set as permitted.\n * @returns The updated CAIP-25 caveat value with the permitted Ethereum chainIDs.\n */\nexport const setPermittedEthChainIds = (\n caip25CaveatValue: Caip25CaveatValue,\n chainIds: Hex[],\n): Caip25CaveatValue => {\n let updatedCaveatValue: Caip25CaveatValue = {\n ...caip25CaveatValue,\n requiredScopes: filterEthScopesObjectByChainId(\n caip25CaveatValue.requiredScopes,\n chainIds,\n ),\n optionalScopes: filterEthScopesObjectByChainId(\n caip25CaveatValue.optionalScopes,\n chainIds,\n ),\n };\n\n chainIds.forEach((chainId) => {\n updatedCaveatValue = addPermittedEthChainId(updatedCaveatValue, chainId);\n });\n\n return updatedCaveatValue;\n};\n\n/*\n *\n *\n * GENERALIZED GETTERS AND SETTERS\n *\n *\n */\n\n/*\n *\n * GETTERS\n *\n */\n\n/**\n * Gets all scopes from a CAIP-25 caveat value\n *\n * @param scopesObjects - The scopes objects to get the scopes from.\n * @returns An array of InternalScopeStrings.\n */\nexport function getAllScopesFromScopesObjects(\n scopesObjects: InternalScopesObject[],\n): InternalScopeString[] {\n const scopeSet = new Set<InternalScopeString>();\n\n for (const scopeObject of scopesObjects) {\n for (const key of Object.keys(scopeObject)) {\n scopeSet.add(key as InternalScopeString);\n }\n }\n\n return Array.from(scopeSet);\n}\n\n/**\n * Gets all scopes (chain IDs) from a CAIP-25 caveat\n * This extracts all scopes from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to extract scopes from\n * @returns Array of unique scope strings (chain IDs)\n */\nexport function getAllScopesFromCaip25CaveatValue(\n caip25CaveatValue: Caip25CaveatValue,\n): CaipChainId[] {\n return getAllScopesFromScopesObjects([\n caip25CaveatValue.requiredScopes,\n caip25CaveatValue.optionalScopes,\n ]) as CaipChainId[];\n}\n\n/**\n * Gets all non-wallet namespaces from a CAIP-25 caveat value\n * This extracts all namespaces from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to extract namespaces from\n * @returns Array of unique namespace strings\n */\nexport function getAllNamespacesFromCaip25CaveatValue(\n caip25CaveatValue: Caip25CaveatValue,\n): CaipNamespace[] {\n const allScopes = getAllScopesFromCaip25CaveatValue(caip25CaveatValue);\n const namespaceSet = new Set<CaipNamespace>();\n\n for (const scope of allScopes) {\n const { namespace } = parseScopeString(scope);\n if (namespace === KnownCaipNamespace.Wallet) {\n namespaceSet.add(scope);\n } else if (namespace) {\n namespaceSet.add(namespace);\n }\n }\n\n return Array.from(namespaceSet);\n}\n\n/**\n * Gets all scopes (chain IDs) from a CAIP-25 permission\n * This extracts all scopes from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25Permission - The CAIP-25 permission object\n * @param caip25Permission.caveats - The caveats of the CAIP-25 permission\n * @returns Array of unique scope strings (chain IDs)\n */\nexport function getAllScopesFromPermission(caip25Permission: {\n caveats: {\n type: string;\n value: Caip25CaveatValue;\n }[];\n}): CaipChainId[] {\n const caip25Caveat = caip25Permission.caveats.find(\n (caveat) => caveat.type === Caip25CaveatType,\n );\n if (!caip25Caveat) {\n return [];\n }\n\n return getAllScopesFromCaip25CaveatValue(caip25Caveat.value);\n}\n\n/*\n *\n * SETTERS\n *\n */\n\n/**\n * Adds a chainID to the optional scopes if it is not already present\n * in either the pre-existing required or optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to add the chainID to.\n * @param chainId - The chainID to add.\n * @returns The updated CAIP-25 caveat value with the added chainID.\n */\nexport const addCaipChainIdInCaip25CaveatValue = (\n caip25CaveatValue: Caip25CaveatValue,\n chainId: CaipChainId,\n): Caip25CaveatValue => {\n if (\n caip25CaveatValue.requiredScopes[chainId] ||\n caip25CaveatValue.optionalScopes[chainId]\n ) {\n return caip25CaveatValue;\n }\n\n return {\n ...caip25CaveatValue,\n optionalScopes: {\n ...caip25CaveatValue.optionalScopes,\n [chainId]: {\n accounts: [],\n },\n },\n };\n};\n\n/**\n * Sets the CAIP-2 chainIds for the required and optional scopes.\n * If the caip25CaveatValue contains chainIds not in the chainIds array arg they are filtered out\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted CAIP-2 chainIDs for.\n * @param chainIds - The CAIP-2 chainIDs to set.\n * @returns The updated CAIP-25 caveat value with the CAIP-2 chainIDs.\n */\nexport const setChainIdsInCaip25CaveatValue = (\n caip25CaveatValue: Caip25CaveatValue,\n chainIds: CaipChainId[],\n): Caip25CaveatValue => {\n const chainIdSet = new Set(chainIds);\n const result: Caip25CaveatValue = {\n requiredScopes: {},\n optionalScopes: {},\n sessionProperties: caip25CaveatValue.sessionProperties,\n isMultichainOrigin: caip25CaveatValue.isMultichainOrigin,\n };\n\n for (const [key, value] of Object.entries(caip25CaveatValue.requiredScopes)) {\n const scopeString = key as keyof typeof caip25CaveatValue.requiredScopes;\n if (isWalletScope(scopeString) || chainIdSet.has(scopeString)) {\n result.requiredScopes[scopeString] = value;\n }\n }\n\n for (const [key, value] of Object.entries(caip25CaveatValue.optionalScopes)) {\n const scopeString = key as keyof typeof caip25CaveatValue.optionalScopes;\n if (isWalletScope(scopeString) || chainIdSet.has(scopeString)) {\n result.optionalScopes[scopeString] = value;\n }\n }\n\n for (const chainId of chainIds) {\n if (!result.requiredScopes[chainId] && !result.optionalScopes[chainId]) {\n result.optionalScopes[chainId] = { accounts: [] };\n }\n }\n\n return result;\n};\n"]}
1
+ {"version":3,"file":"caip-permission-adapter-permittedChains.cjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-permittedChains.ts"],"names":[],"mappings":";;;AAAA,iEAAmD;AAEnD,2CAAkE;AAElE,8DAA+E;AAC/E,sDAAyD;AAEzD,8CAAiE;AAEjE;;;;;;GAMG;AAEH;;;;;GAKG;AACH,MAAM,iCAAiC,GAAG,CAAC,MAA4B,EAAE,EAAE;IACzE,MAAM,WAAW,GAAU,EAAE,CAAC;IAE9B,MAAM,CAAC,IAAI,CAAC,MAAM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I,MAAM,CAAC,OAAO,CAAC,iBAAiB,CAAC,cAAc,CAAC,EAAE;QAC3E,MAAM,WAAW,GAAG,GAAoD,CAAC;QACzE,IAAI,IAAA,qBAAa,EAAC,WAAW,CAAC,IAAI,UAAU,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE;YAC7D,MAAM,CAAC,cAAc,CAAC,WAAW,CAAC,GAAG,KAAK,CAAC;SAC5C;KACF;IAED,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,iBAAiB,CAAC,cAAc,CAAC,EAAE;QAC3E,MAAM,WAAW,GAAG,GAAoD,CAAC;QACzE,IAAI,IAAA,qBAAa,EAAC,WAAW,CAAC,IAAI,UAAU,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE;YAC7D,MAAM,CAAC,cAAc,CAAC,WAAW,CAAC,GAAG,KAAK,CAAC;SAC5C;KACF;IAED,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;QAC9B,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE;YACtE,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,GAAG,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC;SACnD;KACF;IAED,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAjCW,QAAA,8BAA8B,kCAiCzC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport type { Hex, CaipChainId, CaipNamespace } from '@metamask/utils';\nimport { hexToBigInt, KnownCaipNamespace } from '@metamask/utils';\n\nimport { Caip25CaveatType, type Caip25CaveatValue } from '../caip25Permission';\nimport { getUniqueArrayItems } from '../scope/transform';\nimport type { InternalScopesObject, InternalScopeString } from '../scope/types';\nimport { isWalletScope, parseScopeString } from '../scope/types';\n\n/*\n *\n *\n * EVM SPECIFIC GETTERS AND SETTERS\n *\n *\n */\n\n/**\n * Gets the Ethereum (EIP155 namespaced) chainIDs from internal scopes.\n *\n * @param scopes - The internal scopes from which to get the Ethereum chainIDs.\n * @returns An array of Ethereum chainIDs.\n */\nconst getPermittedEthChainIdsFromScopes = (scopes: InternalScopesObject) => {\n const ethChainIds: Hex[] = [];\n\n Object.keys(scopes).forEach((scopeString) => {\n const { namespace, reference } = parseScopeString(scopeString);\n if (namespace === KnownCaipNamespace.Eip155 && reference) {\n ethChainIds.push(toHex(reference));\n }\n });\n\n return ethChainIds;\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) chainIDs from the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value from which to get the Ethereum chainIDs.\n * @returns An array of Ethereum chainIDs.\n */\nexport const getPermittedEthChainIds = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n) => {\n const { requiredScopes, optionalScopes } = caip25CaveatValue;\n\n const ethChainIds: Hex[] = [\n ...getPermittedEthChainIdsFromScopes(requiredScopes),\n ...getPermittedEthChainIdsFromScopes(optionalScopes),\n ];\n\n return getUniqueArrayItems(ethChainIds);\n};\n\n/**\n * Adds an Ethereum (EIP155 namespaced) chainID to the optional scopes if it is not already present\n * in either the pre-existing required or optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to add the Ethereum chainID to.\n * @param chainId - The Ethereum chainID to add.\n * @returns The updated CAIP-25 caveat value with the added Ethereum chainID.\n */\nexport const addPermittedEthChainId = (\n caip25CaveatValue: Caip25CaveatValue,\n chainId: Hex,\n): Caip25CaveatValue => {\n const scopeString = `eip155:${hexToBigInt(chainId).toString(10)}`;\n if (\n Object.keys(caip25CaveatValue.requiredScopes).includes(scopeString) ||\n Object.keys(caip25CaveatValue.optionalScopes).includes(scopeString)\n ) {\n return caip25CaveatValue;\n }\n\n return {\n ...caip25CaveatValue,\n optionalScopes: {\n ...caip25CaveatValue.optionalScopes,\n [scopeString]: {\n accounts: [],\n },\n },\n };\n};\n\n/**\n * Filters the scopes object to only include:\n * - Scopes without references (e.g. \"wallet:\")\n * - EIP155 scopes for the given chainIDs\n * - Non EIP155 scopes (e.g. \"bip122:\" or any other non ethereum namespaces)\n *\n * @param scopesObject - The scopes object to filter.\n * @param chainIds - The chainIDs to filter EIP155 scopes by.\n * @returns The filtered scopes object.\n */\nconst filterEthScopesObjectByChainId = (\n scopesObject: InternalScopesObject,\n chainIds: Hex[],\n): InternalScopesObject => {\n const updatedScopesObject: InternalScopesObject = {};\n\n Object.entries(scopesObject).forEach(([key, scopeObject]) => {\n // Cast needed because index type is returned as `string` by `Object.entries`\n const scopeString = key as keyof typeof scopesObject;\n const { namespace, reference } = parseScopeString(scopeString);\n if (!reference) {\n updatedScopesObject[scopeString] = scopeObject;\n return;\n }\n if (namespace === KnownCaipNamespace.Eip155) {\n const chainId = toHex(reference);\n if (chainIds.includes(chainId)) {\n updatedScopesObject[scopeString] = scopeObject;\n }\n } else {\n updatedScopesObject[scopeString] = scopeObject;\n }\n });\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the permitted Ethereum (EIP155 namespaced) chainIDs for the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted Ethereum chainIDs for.\n * @param chainIds - The Ethereum chainIDs to set as permitted.\n * @returns The updated CAIP-25 caveat value with the permitted Ethereum chainIDs.\n */\nexport const setPermittedEthChainIds = (\n caip25CaveatValue: Caip25CaveatValue,\n chainIds: Hex[],\n): Caip25CaveatValue => {\n let updatedCaveatValue: Caip25CaveatValue = {\n ...caip25CaveatValue,\n requiredScopes: filterEthScopesObjectByChainId(\n caip25CaveatValue.requiredScopes,\n chainIds,\n ),\n optionalScopes: filterEthScopesObjectByChainId(\n caip25CaveatValue.optionalScopes,\n chainIds,\n ),\n };\n\n chainIds.forEach((chainId) => {\n updatedCaveatValue = addPermittedEthChainId(updatedCaveatValue, chainId);\n });\n\n return updatedCaveatValue;\n};\n\n/*\n *\n *\n * GENERALIZED GETTERS AND SETTERS\n *\n *\n */\n\n/*\n *\n * GETTERS\n *\n */\n\n/**\n * Gets all scopes from a CAIP-25 caveat value\n *\n * @param scopesObjects - The scopes objects to get the scopes from.\n * @returns An array of InternalScopeStrings.\n */\nexport function getAllScopesFromScopesObjects(\n scopesObjects: InternalScopesObject[],\n): InternalScopeString[] {\n const scopeSet = new Set<InternalScopeString>();\n\n for (const scopeObject of scopesObjects) {\n for (const key of Object.keys(scopeObject)) {\n scopeSet.add(key as InternalScopeString);\n }\n }\n\n return Array.from(scopeSet);\n}\n\n/**\n * Gets all scopes (chain IDs) from a CAIP-25 caveat\n * This extracts all scopes from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to extract scopes from\n * @returns Array of unique scope strings (chain IDs)\n */\nexport function getAllScopesFromCaip25CaveatValue(\n caip25CaveatValue: Caip25CaveatValue,\n): CaipChainId[] {\n return getAllScopesFromScopesObjects([\n caip25CaveatValue.requiredScopes,\n caip25CaveatValue.optionalScopes,\n ]) as CaipChainId[];\n}\n\n/**\n * Gets all non-wallet namespaces from a CAIP-25 caveat value\n * This extracts all namespaces from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to extract namespaces from\n * @returns Array of unique namespace strings\n */\nexport function getAllNamespacesFromCaip25CaveatValue(\n caip25CaveatValue: Caip25CaveatValue,\n): CaipNamespace[] {\n const allScopes = getAllScopesFromCaip25CaveatValue(caip25CaveatValue);\n const namespaceSet = new Set<CaipNamespace>();\n\n for (const scope of allScopes) {\n const { namespace, reference } = parseScopeString(scope);\n if (namespace === KnownCaipNamespace.Wallet) {\n namespaceSet.add(reference ?? namespace);\n } else if (namespace) {\n namespaceSet.add(namespace);\n }\n }\n\n return Array.from(namespaceSet);\n}\n\n/**\n * Gets all scopes (chain IDs) from a CAIP-25 permission\n * This extracts all scopes from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25Permission - The CAIP-25 permission object\n * @param caip25Permission.caveats - The caveats of the CAIP-25 permission\n * @returns Array of unique scope strings (chain IDs)\n */\nexport function getAllScopesFromPermission(caip25Permission: {\n caveats: {\n type: string;\n value: Caip25CaveatValue;\n }[];\n}): CaipChainId[] {\n const caip25Caveat = caip25Permission.caveats.find(\n (caveat) => caveat.type === Caip25CaveatType,\n );\n if (!caip25Caveat) {\n return [];\n }\n\n return getAllScopesFromCaip25CaveatValue(caip25Caveat.value);\n}\n\n/*\n *\n * SETTERS\n *\n */\n\n/**\n * Adds a chainID to the optional scopes if it is not already present\n * in either the pre-existing required or optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to add the chainID to.\n * @param chainId - The chainID to add.\n * @returns The updated CAIP-25 caveat value with the added chainID.\n */\nexport const addCaipChainIdInCaip25CaveatValue = (\n caip25CaveatValue: Caip25CaveatValue,\n chainId: CaipChainId,\n): Caip25CaveatValue => {\n if (\n caip25CaveatValue.requiredScopes[chainId] ||\n caip25CaveatValue.optionalScopes[chainId]\n ) {\n return caip25CaveatValue;\n }\n\n return {\n ...caip25CaveatValue,\n optionalScopes: {\n ...caip25CaveatValue.optionalScopes,\n [chainId]: {\n accounts: [],\n },\n },\n };\n};\n\n/**\n * Sets the CAIP-2 chainIds for the required and optional scopes.\n * If the caip25CaveatValue contains chainIds not in the chainIds array arg they are filtered out\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted CAIP-2 chainIDs for.\n * @param chainIds - The CAIP-2 chainIDs to set.\n * @returns The updated CAIP-25 caveat value with the CAIP-2 chainIDs.\n */\nexport const setChainIdsInCaip25CaveatValue = (\n caip25CaveatValue: Caip25CaveatValue,\n chainIds: CaipChainId[],\n): Caip25CaveatValue => {\n const chainIdSet = new Set(chainIds);\n const result: Caip25CaveatValue = {\n requiredScopes: {},\n optionalScopes: {},\n sessionProperties: caip25CaveatValue.sessionProperties,\n isMultichainOrigin: caip25CaveatValue.isMultichainOrigin,\n };\n\n for (const [key, value] of Object.entries(caip25CaveatValue.requiredScopes)) {\n const scopeString = key as keyof typeof caip25CaveatValue.requiredScopes;\n if (isWalletScope(scopeString) || chainIdSet.has(scopeString)) {\n result.requiredScopes[scopeString] = value;\n }\n }\n\n for (const [key, value] of Object.entries(caip25CaveatValue.optionalScopes)) {\n const scopeString = key as keyof typeof caip25CaveatValue.optionalScopes;\n if (isWalletScope(scopeString) || chainIdSet.has(scopeString)) {\n result.optionalScopes[scopeString] = value;\n }\n }\n\n for (const chainId of chainIds) {\n if (!result.requiredScopes[chainId] && !result.optionalScopes[chainId]) {\n result.optionalScopes[chainId] = { accounts: [] };\n }\n }\n\n return result;\n};\n"]}
@@ -167,9 +167,9 @@ export function getAllNamespacesFromCaip25CaveatValue(caip25CaveatValue) {
167
167
  const allScopes = getAllScopesFromCaip25CaveatValue(caip25CaveatValue);
168
168
  const namespaceSet = new Set();
169
169
  for (const scope of allScopes) {
170
- const { namespace } = parseScopeString(scope);
170
+ const { namespace, reference } = parseScopeString(scope);
171
171
  if (namespace === KnownCaipNamespace.Wallet) {
172
- namespaceSet.add(scope);
172
+ namespaceSet.add(reference ?? namespace);
173
173
  }
174
174
  else if (namespace) {
175
175
  namespaceSet.add(namespace);
@@ -1 +1 @@
1
- {"version":3,"file":"caip-permission-adapter-permittedChains.mjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-permittedChains.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,mCAAmC;AAEnD,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAE,wBAAwB;AAElE,OAAO,EAAE,gBAAgB,EAA0B,gCAA4B;AAC/E,OAAO,EAAE,mBAAmB,EAAE,+BAA2B;AAEzD,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,2BAAuB;AAEjE;;;;;;GAMG;AAEH;;;;;GAKG;AACH,MAAM,iCAAiC,GAAG,CAAC,MAA4B,EAAE,EAAE;IACzE,MAAM,WAAW,GAAU,EAAE,CAAC;IAE9B,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC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YAC7D,MAAM,CAAC,cAAc,CAAC,WAAW,CAAC,GAAG,KAAK,CAAC;SAC5C;KACF;IAED,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,iBAAiB,CAAC,cAAc,CAAC,EAAE;QAC3E,MAAM,WAAW,GAAG,GAAoD,CAAC;QACzE,IAAI,aAAa,CAAC,WAAW,CAAC,IAAI,UAAU,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE;YAC7D,MAAM,CAAC,cAAc,CAAC,WAAW,CAAC,GAAG,KAAK,CAAC;SAC5C;KACF;IAED,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;QAC9B,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE;YACtE,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,GAAG,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC;SACnD;KACF;IAED,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport type { Hex, CaipChainId, CaipNamespace } from '@metamask/utils';\nimport { hexToBigInt, KnownCaipNamespace } from '@metamask/utils';\n\nimport { Caip25CaveatType, type Caip25CaveatValue } from '../caip25Permission';\nimport { getUniqueArrayItems } from '../scope/transform';\nimport type { InternalScopesObject, InternalScopeString } from '../scope/types';\nimport { isWalletScope, parseScopeString } from '../scope/types';\n\n/*\n *\n *\n * EVM SPECIFIC GETTERS AND SETTERS\n *\n *\n */\n\n/**\n * Gets the Ethereum (EIP155 namespaced) chainIDs from internal scopes.\n *\n * @param scopes - The internal scopes from which to get the Ethereum chainIDs.\n * @returns An array of Ethereum chainIDs.\n */\nconst getPermittedEthChainIdsFromScopes = (scopes: InternalScopesObject) => {\n const ethChainIds: Hex[] = [];\n\n Object.keys(scopes).forEach((scopeString) => {\n const { namespace, reference } = parseScopeString(scopeString);\n if (namespace === KnownCaipNamespace.Eip155 && reference) {\n ethChainIds.push(toHex(reference));\n }\n });\n\n return ethChainIds;\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) chainIDs from the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value from which to get the Ethereum chainIDs.\n * @returns An array of Ethereum chainIDs.\n */\nexport const getPermittedEthChainIds = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n) => {\n const { requiredScopes, optionalScopes } = caip25CaveatValue;\n\n const ethChainIds: Hex[] = [\n ...getPermittedEthChainIdsFromScopes(requiredScopes),\n ...getPermittedEthChainIdsFromScopes(optionalScopes),\n ];\n\n return getUniqueArrayItems(ethChainIds);\n};\n\n/**\n * Adds an Ethereum (EIP155 namespaced) chainID to the optional scopes if it is not already present\n * in either the pre-existing required or optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to add the Ethereum chainID to.\n * @param chainId - The Ethereum chainID to add.\n * @returns The updated CAIP-25 caveat value with the added Ethereum chainID.\n */\nexport const addPermittedEthChainId = (\n caip25CaveatValue: Caip25CaveatValue,\n chainId: Hex,\n): Caip25CaveatValue => {\n const scopeString = `eip155:${hexToBigInt(chainId).toString(10)}`;\n if (\n Object.keys(caip25CaveatValue.requiredScopes).includes(scopeString) ||\n Object.keys(caip25CaveatValue.optionalScopes).includes(scopeString)\n ) {\n return caip25CaveatValue;\n }\n\n return {\n ...caip25CaveatValue,\n optionalScopes: {\n ...caip25CaveatValue.optionalScopes,\n [scopeString]: {\n accounts: [],\n },\n },\n };\n};\n\n/**\n * Filters the scopes object to only include:\n * - Scopes without references (e.g. \"wallet:\")\n * - EIP155 scopes for the given chainIDs\n * - Non EIP155 scopes (e.g. \"bip122:\" or any other non ethereum namespaces)\n *\n * @param scopesObject - The scopes object to filter.\n * @param chainIds - The chainIDs to filter EIP155 scopes by.\n * @returns The filtered scopes object.\n */\nconst filterEthScopesObjectByChainId = (\n scopesObject: InternalScopesObject,\n chainIds: Hex[],\n): InternalScopesObject => {\n const updatedScopesObject: InternalScopesObject = {};\n\n Object.entries(scopesObject).forEach(([key, scopeObject]) => {\n // Cast needed because index type is returned as `string` by `Object.entries`\n const scopeString = key as keyof typeof scopesObject;\n const { namespace, reference } = parseScopeString(scopeString);\n if (!reference) {\n updatedScopesObject[scopeString] = scopeObject;\n return;\n }\n if (namespace === KnownCaipNamespace.Eip155) {\n const chainId = toHex(reference);\n if (chainIds.includes(chainId)) {\n updatedScopesObject[scopeString] = scopeObject;\n }\n } else {\n updatedScopesObject[scopeString] = scopeObject;\n }\n });\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the permitted Ethereum (EIP155 namespaced) chainIDs for the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted Ethereum chainIDs for.\n * @param chainIds - The Ethereum chainIDs to set as permitted.\n * @returns The updated CAIP-25 caveat value with the permitted Ethereum chainIDs.\n */\nexport const setPermittedEthChainIds = (\n caip25CaveatValue: Caip25CaveatValue,\n chainIds: Hex[],\n): Caip25CaveatValue => {\n let updatedCaveatValue: Caip25CaveatValue = {\n ...caip25CaveatValue,\n requiredScopes: filterEthScopesObjectByChainId(\n caip25CaveatValue.requiredScopes,\n chainIds,\n ),\n optionalScopes: filterEthScopesObjectByChainId(\n caip25CaveatValue.optionalScopes,\n chainIds,\n ),\n };\n\n chainIds.forEach((chainId) => {\n updatedCaveatValue = addPermittedEthChainId(updatedCaveatValue, chainId);\n });\n\n return updatedCaveatValue;\n};\n\n/*\n *\n *\n * GENERALIZED GETTERS AND SETTERS\n *\n *\n */\n\n/*\n *\n * GETTERS\n *\n */\n\n/**\n * Gets all scopes from a CAIP-25 caveat value\n *\n * @param scopesObjects - The scopes objects to get the scopes from.\n * @returns An array of InternalScopeStrings.\n */\nexport function getAllScopesFromScopesObjects(\n scopesObjects: InternalScopesObject[],\n): InternalScopeString[] {\n const scopeSet = new Set<InternalScopeString>();\n\n for (const scopeObject of scopesObjects) {\n for (const key of Object.keys(scopeObject)) {\n scopeSet.add(key as InternalScopeString);\n }\n }\n\n return Array.from(scopeSet);\n}\n\n/**\n * Gets all scopes (chain IDs) from a CAIP-25 caveat\n * This extracts all scopes from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to extract scopes from\n * @returns Array of unique scope strings (chain IDs)\n */\nexport function getAllScopesFromCaip25CaveatValue(\n caip25CaveatValue: Caip25CaveatValue,\n): CaipChainId[] {\n return getAllScopesFromScopesObjects([\n caip25CaveatValue.requiredScopes,\n caip25CaveatValue.optionalScopes,\n ]) as CaipChainId[];\n}\n\n/**\n * Gets all non-wallet namespaces from a CAIP-25 caveat value\n * This extracts all namespaces from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to extract namespaces from\n * @returns Array of unique namespace strings\n */\nexport function getAllNamespacesFromCaip25CaveatValue(\n caip25CaveatValue: Caip25CaveatValue,\n): CaipNamespace[] {\n const allScopes = getAllScopesFromCaip25CaveatValue(caip25CaveatValue);\n const namespaceSet = new Set<CaipNamespace>();\n\n for (const scope of allScopes) {\n const { namespace } = parseScopeString(scope);\n if (namespace === KnownCaipNamespace.Wallet) {\n namespaceSet.add(scope);\n } else if (namespace) {\n namespaceSet.add(namespace);\n }\n }\n\n return Array.from(namespaceSet);\n}\n\n/**\n * Gets all scopes (chain IDs) from a CAIP-25 permission\n * This extracts all scopes from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25Permission - The CAIP-25 permission object\n * @param caip25Permission.caveats - The caveats of the CAIP-25 permission\n * @returns Array of unique scope strings (chain IDs)\n */\nexport function getAllScopesFromPermission(caip25Permission: {\n caveats: {\n type: string;\n value: Caip25CaveatValue;\n }[];\n}): CaipChainId[] {\n const caip25Caveat = caip25Permission.caveats.find(\n (caveat) => caveat.type === Caip25CaveatType,\n );\n if (!caip25Caveat) {\n return [];\n }\n\n return getAllScopesFromCaip25CaveatValue(caip25Caveat.value);\n}\n\n/*\n *\n * SETTERS\n *\n */\n\n/**\n * Adds a chainID to the optional scopes if it is not already present\n * in either the pre-existing required or optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to add the chainID to.\n * @param chainId - The chainID to add.\n * @returns The updated CAIP-25 caveat value with the added chainID.\n */\nexport const addCaipChainIdInCaip25CaveatValue = (\n caip25CaveatValue: Caip25CaveatValue,\n chainId: CaipChainId,\n): Caip25CaveatValue => {\n if (\n caip25CaveatValue.requiredScopes[chainId] ||\n caip25CaveatValue.optionalScopes[chainId]\n ) {\n return caip25CaveatValue;\n }\n\n return {\n ...caip25CaveatValue,\n optionalScopes: {\n ...caip25CaveatValue.optionalScopes,\n [chainId]: {\n accounts: [],\n },\n },\n };\n};\n\n/**\n * Sets the CAIP-2 chainIds for the required and optional scopes.\n * If the caip25CaveatValue contains chainIds not in the chainIds array arg they are filtered out\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted CAIP-2 chainIDs for.\n * @param chainIds - The CAIP-2 chainIDs to set.\n * @returns The updated CAIP-25 caveat value with the CAIP-2 chainIDs.\n */\nexport const setChainIdsInCaip25CaveatValue = (\n caip25CaveatValue: Caip25CaveatValue,\n chainIds: CaipChainId[],\n): Caip25CaveatValue => {\n const chainIdSet = new Set(chainIds);\n const result: Caip25CaveatValue = {\n requiredScopes: {},\n optionalScopes: {},\n sessionProperties: caip25CaveatValue.sessionProperties,\n isMultichainOrigin: caip25CaveatValue.isMultichainOrigin,\n };\n\n for (const [key, value] of Object.entries(caip25CaveatValue.requiredScopes)) {\n const scopeString = key as keyof typeof caip25CaveatValue.requiredScopes;\n if (isWalletScope(scopeString) || chainIdSet.has(scopeString)) {\n result.requiredScopes[scopeString] = value;\n }\n }\n\n for (const [key, value] of Object.entries(caip25CaveatValue.optionalScopes)) {\n const scopeString = key as keyof typeof caip25CaveatValue.optionalScopes;\n if (isWalletScope(scopeString) || chainIdSet.has(scopeString)) {\n result.optionalScopes[scopeString] = value;\n }\n }\n\n for (const chainId of chainIds) {\n if (!result.requiredScopes[chainId] && !result.optionalScopes[chainId]) {\n result.optionalScopes[chainId] = { accounts: [] };\n }\n }\n\n return result;\n};\n"]}
1
+ {"version":3,"file":"caip-permission-adapter-permittedChains.mjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-permittedChains.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,mCAAmC;AAEnD,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAE,wBAAwB;AAElE,OAAO,EAAE,gBAAgB,EAA0B,gCAA4B;AAC/E,OAAO,EAAE,mBAAmB,EAAE,+BAA2B;AAEzD,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,2BAAuB;AAEjE;;;;;;GAMG;AAEH;;;;;GAKG;AACH,MAAM,iCAAiC,GAAG,CAAC,MAA4B,EAAE,EAAE;IACzE,MAAM,WAAW,GAAU,EAAE,CAAC;IAE9B,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE;QAC1C,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,gBAAgB,CAAC,WAAW,CAAC,CAAC;QAC/D,IAAI,SAAS,KAAK,kBAAkB,CAAC,MAAM,IAAI,SAAS,EAAE;YACxD,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC;SACpC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CACrC,iBAGC,EACD,EAAE;IACF,MAAM,EAAE,cAAc,EAAE,cAAc,EAAE,GAAG,iBAAiB,CAAC;IAE7D,MAAM,WAAW,GAAU;QACzB,GAAG,iCAAiC,CAAC,cAAc,CAAC;QACpD,GAAG,iCAAiC,CAAC,cAAc,CAAC;KACrD,CAAC;IAEF,OAAO,mBAA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CAAC,GAAG,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC;SACnD;KACF;IAED,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport type { Hex, CaipChainId, CaipNamespace } from '@metamask/utils';\nimport { hexToBigInt, KnownCaipNamespace } from '@metamask/utils';\n\nimport { Caip25CaveatType, type Caip25CaveatValue } from '../caip25Permission';\nimport { getUniqueArrayItems } from '../scope/transform';\nimport type { InternalScopesObject, InternalScopeString } from '../scope/types';\nimport { isWalletScope, parseScopeString } from '../scope/types';\n\n/*\n *\n *\n * EVM SPECIFIC GETTERS AND SETTERS\n *\n *\n */\n\n/**\n * Gets the Ethereum (EIP155 namespaced) chainIDs from internal scopes.\n *\n * @param scopes - The internal scopes from which to get the Ethereum chainIDs.\n * @returns An array of Ethereum chainIDs.\n */\nconst getPermittedEthChainIdsFromScopes = (scopes: InternalScopesObject) => {\n const ethChainIds: Hex[] = [];\n\n Object.keys(scopes).forEach((scopeString) => {\n const { namespace, reference } = parseScopeString(scopeString);\n if (namespace === KnownCaipNamespace.Eip155 && reference) {\n ethChainIds.push(toHex(reference));\n }\n });\n\n return ethChainIds;\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) chainIDs from the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value from which to get the Ethereum chainIDs.\n * @returns An array of Ethereum chainIDs.\n */\nexport const getPermittedEthChainIds = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n) => {\n const { requiredScopes, optionalScopes } = caip25CaveatValue;\n\n const ethChainIds: Hex[] = [\n ...getPermittedEthChainIdsFromScopes(requiredScopes),\n ...getPermittedEthChainIdsFromScopes(optionalScopes),\n ];\n\n return getUniqueArrayItems(ethChainIds);\n};\n\n/**\n * Adds an Ethereum (EIP155 namespaced) chainID to the optional scopes if it is not already present\n * in either the pre-existing required or optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to add the Ethereum chainID to.\n * @param chainId - The Ethereum chainID to add.\n * @returns The updated CAIP-25 caveat value with the added Ethereum chainID.\n */\nexport const addPermittedEthChainId = (\n caip25CaveatValue: Caip25CaveatValue,\n chainId: Hex,\n): Caip25CaveatValue => {\n const scopeString = `eip155:${hexToBigInt(chainId).toString(10)}`;\n if (\n Object.keys(caip25CaveatValue.requiredScopes).includes(scopeString) ||\n Object.keys(caip25CaveatValue.optionalScopes).includes(scopeString)\n ) {\n return caip25CaveatValue;\n }\n\n return {\n ...caip25CaveatValue,\n optionalScopes: {\n ...caip25CaveatValue.optionalScopes,\n [scopeString]: {\n accounts: [],\n },\n },\n };\n};\n\n/**\n * Filters the scopes object to only include:\n * - Scopes without references (e.g. \"wallet:\")\n * - EIP155 scopes for the given chainIDs\n * - Non EIP155 scopes (e.g. \"bip122:\" or any other non ethereum namespaces)\n *\n * @param scopesObject - The scopes object to filter.\n * @param chainIds - The chainIDs to filter EIP155 scopes by.\n * @returns The filtered scopes object.\n */\nconst filterEthScopesObjectByChainId = (\n scopesObject: InternalScopesObject,\n chainIds: Hex[],\n): InternalScopesObject => {\n const updatedScopesObject: InternalScopesObject = {};\n\n Object.entries(scopesObject).forEach(([key, scopeObject]) => {\n // Cast needed because index type is returned as `string` by `Object.entries`\n const scopeString = key as keyof typeof scopesObject;\n const { namespace, reference } = parseScopeString(scopeString);\n if (!reference) {\n updatedScopesObject[scopeString] = scopeObject;\n return;\n }\n if (namespace === KnownCaipNamespace.Eip155) {\n const chainId = toHex(reference);\n if (chainIds.includes(chainId)) {\n updatedScopesObject[scopeString] = scopeObject;\n }\n } else {\n updatedScopesObject[scopeString] = scopeObject;\n }\n });\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the permitted Ethereum (EIP155 namespaced) chainIDs for the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted Ethereum chainIDs for.\n * @param chainIds - The Ethereum chainIDs to set as permitted.\n * @returns The updated CAIP-25 caveat value with the permitted Ethereum chainIDs.\n */\nexport const setPermittedEthChainIds = (\n caip25CaveatValue: Caip25CaveatValue,\n chainIds: Hex[],\n): Caip25CaveatValue => {\n let updatedCaveatValue: Caip25CaveatValue = {\n ...caip25CaveatValue,\n requiredScopes: filterEthScopesObjectByChainId(\n caip25CaveatValue.requiredScopes,\n chainIds,\n ),\n optionalScopes: filterEthScopesObjectByChainId(\n caip25CaveatValue.optionalScopes,\n chainIds,\n ),\n };\n\n chainIds.forEach((chainId) => {\n updatedCaveatValue = addPermittedEthChainId(updatedCaveatValue, chainId);\n });\n\n return updatedCaveatValue;\n};\n\n/*\n *\n *\n * GENERALIZED GETTERS AND SETTERS\n *\n *\n */\n\n/*\n *\n * GETTERS\n *\n */\n\n/**\n * Gets all scopes from a CAIP-25 caveat value\n *\n * @param scopesObjects - The scopes objects to get the scopes from.\n * @returns An array of InternalScopeStrings.\n */\nexport function getAllScopesFromScopesObjects(\n scopesObjects: InternalScopesObject[],\n): InternalScopeString[] {\n const scopeSet = new Set<InternalScopeString>();\n\n for (const scopeObject of scopesObjects) {\n for (const key of Object.keys(scopeObject)) {\n scopeSet.add(key as InternalScopeString);\n }\n }\n\n return Array.from(scopeSet);\n}\n\n/**\n * Gets all scopes (chain IDs) from a CAIP-25 caveat\n * This extracts all scopes from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to extract scopes from\n * @returns Array of unique scope strings (chain IDs)\n */\nexport function getAllScopesFromCaip25CaveatValue(\n caip25CaveatValue: Caip25CaveatValue,\n): CaipChainId[] {\n return getAllScopesFromScopesObjects([\n caip25CaveatValue.requiredScopes,\n caip25CaveatValue.optionalScopes,\n ]) as CaipChainId[];\n}\n\n/**\n * Gets all non-wallet namespaces from a CAIP-25 caveat value\n * This extracts all namespaces from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to extract namespaces from\n * @returns Array of unique namespace strings\n */\nexport function getAllNamespacesFromCaip25CaveatValue(\n caip25CaveatValue: Caip25CaveatValue,\n): CaipNamespace[] {\n const allScopes = getAllScopesFromCaip25CaveatValue(caip25CaveatValue);\n const namespaceSet = new Set<CaipNamespace>();\n\n for (const scope of allScopes) {\n const { namespace, reference } = parseScopeString(scope);\n if (namespace === KnownCaipNamespace.Wallet) {\n namespaceSet.add(reference ?? namespace);\n } else if (namespace) {\n namespaceSet.add(namespace);\n }\n }\n\n return Array.from(namespaceSet);\n}\n\n/**\n * Gets all scopes (chain IDs) from a CAIP-25 permission\n * This extracts all scopes from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25Permission - The CAIP-25 permission object\n * @param caip25Permission.caveats - The caveats of the CAIP-25 permission\n * @returns Array of unique scope strings (chain IDs)\n */\nexport function getAllScopesFromPermission(caip25Permission: {\n caveats: {\n type: string;\n value: Caip25CaveatValue;\n }[];\n}): CaipChainId[] {\n const caip25Caveat = caip25Permission.caveats.find(\n (caveat) => caveat.type === Caip25CaveatType,\n );\n if (!caip25Caveat) {\n return [];\n }\n\n return getAllScopesFromCaip25CaveatValue(caip25Caveat.value);\n}\n\n/*\n *\n * SETTERS\n *\n */\n\n/**\n * Adds a chainID to the optional scopes if it is not already present\n * in either the pre-existing required or optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to add the chainID to.\n * @param chainId - The chainID to add.\n * @returns The updated CAIP-25 caveat value with the added chainID.\n */\nexport const addCaipChainIdInCaip25CaveatValue = (\n caip25CaveatValue: Caip25CaveatValue,\n chainId: CaipChainId,\n): Caip25CaveatValue => {\n if (\n caip25CaveatValue.requiredScopes[chainId] ||\n caip25CaveatValue.optionalScopes[chainId]\n ) {\n return caip25CaveatValue;\n }\n\n return {\n ...caip25CaveatValue,\n optionalScopes: {\n ...caip25CaveatValue.optionalScopes,\n [chainId]: {\n accounts: [],\n },\n },\n };\n};\n\n/**\n * Sets the CAIP-2 chainIds for the required and optional scopes.\n * If the caip25CaveatValue contains chainIds not in the chainIds array arg they are filtered out\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted CAIP-2 chainIDs for.\n * @param chainIds - The CAIP-2 chainIDs to set.\n * @returns The updated CAIP-25 caveat value with the CAIP-2 chainIDs.\n */\nexport const setChainIdsInCaip25CaveatValue = (\n caip25CaveatValue: Caip25CaveatValue,\n chainIds: CaipChainId[],\n): Caip25CaveatValue => {\n const chainIdSet = new Set(chainIds);\n const result: Caip25CaveatValue = {\n requiredScopes: {},\n optionalScopes: {},\n sessionProperties: caip25CaveatValue.sessionProperties,\n isMultichainOrigin: caip25CaveatValue.isMultichainOrigin,\n };\n\n for (const [key, value] of Object.entries(caip25CaveatValue.requiredScopes)) {\n const scopeString = key as keyof typeof caip25CaveatValue.requiredScopes;\n if (isWalletScope(scopeString) || chainIdSet.has(scopeString)) {\n result.requiredScopes[scopeString] = value;\n }\n }\n\n for (const [key, value] of Object.entries(caip25CaveatValue.optionalScopes)) {\n const scopeString = key as keyof typeof caip25CaveatValue.optionalScopes;\n if (isWalletScope(scopeString) || chainIdSet.has(scopeString)) {\n result.optionalScopes[scopeString] = value;\n }\n }\n\n for (const chainId of chainIds) {\n if (!result.requiredScopes[chainId] && !result.optionalScopes[chainId]) {\n result.optionalScopes[chainId] = { accounts: [] };\n }\n }\n\n return result;\n};\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/chain-agnostic-permission",
3
- "version": "0.5.0-preview-fa22a478",
3
+ "version": "0.6.0-preview-64413dce",
4
4
  "description": "Defines a CAIP-25 based endowment permission and helpers for interfacing with it",
5
5
  "keywords": [
6
6
  "MetaMask",