@metamask-previews/chain-agnostic-permission 0.7.1-preview-3636a128 → 0.8.0-preview-c6e5eb7

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 (49) hide show
  1. package/CHANGELOG.md +9 -1
  2. package/dist/caip25Permission.cjs +4 -4
  3. package/dist/caip25Permission.cjs.map +1 -1
  4. package/dist/caip25Permission.mjs +2 -2
  5. package/dist/caip25Permission.mjs.map +1 -1
  6. package/dist/index.cjs +22 -22
  7. package/dist/index.cjs.map +1 -1
  8. package/dist/index.d.cts +3 -3
  9. package/dist/index.d.cts.map +1 -1
  10. package/dist/index.d.mts +3 -3
  11. package/dist/index.d.mts.map +1 -1
  12. package/dist/index.mjs +3 -3
  13. package/dist/index.mjs.map +1 -1
  14. package/dist/{adapters/caip-permission-adapter-accounts.cjs → operators/caip-permission-operator-accounts.cjs} +8 -1
  15. package/dist/{adapters/caip-permission-adapter-accounts.cjs.map → operators/caip-permission-operator-accounts.cjs.map} +1 -1
  16. package/dist/{adapters/caip-permission-adapter-accounts.d.cts → operators/caip-permission-operator-accounts.d.cts} +1 -1
  17. package/dist/operators/caip-permission-operator-accounts.d.cts.map +1 -0
  18. package/dist/{adapters/caip-permission-adapter-accounts.d.mts → operators/caip-permission-operator-accounts.d.mts} +1 -1
  19. package/dist/operators/caip-permission-operator-accounts.d.mts.map +1 -0
  20. package/dist/{adapters/caip-permission-adapter-accounts.mjs → operators/caip-permission-operator-accounts.mjs} +8 -1
  21. package/dist/operators/caip-permission-operator-accounts.mjs.map +1 -0
  22. package/dist/{adapters/caip-permission-adapter-permittedChains.cjs → operators/caip-permission-operator-permittedChains.cjs} +1 -1
  23. package/dist/operators/caip-permission-operator-permittedChains.cjs.map +1 -0
  24. package/dist/{adapters/caip-permission-adapter-permittedChains.d.cts → operators/caip-permission-operator-permittedChains.d.cts} +1 -1
  25. package/dist/operators/caip-permission-operator-permittedChains.d.cts.map +1 -0
  26. package/dist/{adapters/caip-permission-adapter-permittedChains.d.mts → operators/caip-permission-operator-permittedChains.d.mts} +1 -1
  27. package/dist/operators/caip-permission-operator-permittedChains.d.mts.map +1 -0
  28. package/dist/{adapters/caip-permission-adapter-permittedChains.mjs → operators/caip-permission-operator-permittedChains.mjs} +1 -1
  29. package/dist/operators/caip-permission-operator-permittedChains.mjs.map +1 -0
  30. package/dist/{adapters/caip-permission-adapter-session-scopes.cjs → operators/caip-permission-operator-session-scopes.cjs} +1 -1
  31. package/dist/operators/caip-permission-operator-session-scopes.cjs.map +1 -0
  32. package/dist/{adapters/caip-permission-adapter-session-scopes.d.cts → operators/caip-permission-operator-session-scopes.d.cts} +1 -1
  33. package/dist/operators/caip-permission-operator-session-scopes.d.cts.map +1 -0
  34. package/dist/{adapters/caip-permission-adapter-session-scopes.d.mts → operators/caip-permission-operator-session-scopes.d.mts} +1 -1
  35. package/dist/operators/caip-permission-operator-session-scopes.d.mts.map +1 -0
  36. package/dist/{adapters/caip-permission-adapter-session-scopes.mjs → operators/caip-permission-operator-session-scopes.mjs} +1 -1
  37. package/dist/operators/caip-permission-operator-session-scopes.mjs.map +1 -0
  38. package/package.json +2 -2
  39. package/dist/adapters/caip-permission-adapter-accounts.d.cts.map +0 -1
  40. package/dist/adapters/caip-permission-adapter-accounts.d.mts.map +0 -1
  41. package/dist/adapters/caip-permission-adapter-accounts.mjs.map +0 -1
  42. package/dist/adapters/caip-permission-adapter-permittedChains.cjs.map +0 -1
  43. package/dist/adapters/caip-permission-adapter-permittedChains.d.cts.map +0 -1
  44. package/dist/adapters/caip-permission-adapter-permittedChains.d.mts.map +0 -1
  45. package/dist/adapters/caip-permission-adapter-permittedChains.mjs.map +0 -1
  46. package/dist/adapters/caip-permission-adapter-session-scopes.cjs.map +0 -1
  47. package/dist/adapters/caip-permission-adapter-session-scopes.d.cts.map +0 -1
  48. package/dist/adapters/caip-permission-adapter-session-scopes.d.mts.map +0 -1
  49. package/dist/adapters/caip-permission-adapter-session-scopes.mjs.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"caip-permission-adapter-accounts.cjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-accounts.ts"],"names":[],"mappings":";;;AAAA,iEAAoE;AAEpE,2CASyB;AAGzB,sDAA4D;AAC5D,sDAAyD;AAEzD,8CAAkD;AAElD;;;;;;GAMG;AAEH;;;;;;GAMG;AACH,MAAM,mBAAmB,GAAG,CAAC,WAAgC,EAAE,EAAE;IAC/D,MAAM,EAAE,SAAS,EAAE,GAAG,IAAA,wBAAgB,EAAC,WAAW,CAAC,CAAC;IAEpD,OAAO,CACL,SAAS,KAAK,0BAAkB,CAAC,MAAM;QACvC,sDAAsD;QACtD,wEAAwE;QACxE,WAAW,KAAK,kCAAsB,CAAC,MAAM,CAC9C,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,wBAAwB,GAAG,CAAC,MAA4B,EAAE,EAAE;IAChE,MAAM,WAAW,GAAU,EAAE,CAAC;IAE9B,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,EAAE;QACnD,QAAQ,EAAE,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC5B,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,IAAA,0BAAkB,EAAC,OAAO,CAAC,CAAC;YAEzD,IAAI,mBAAmB,CAAC,OAAO,CAAC,EAAE;gBAChC,2DAA2D;gBAC3D,kCAAkC;gBAClC,IAAA,+BAAuB,EAAC,OAAO,CAAC,CAAC;gBACjC,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;aAC3B;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAEF;;;;;GAKG;AACI,MAAM,cAAc,GAAG,CAC5B,iBAGC,EACM,EAAE;IACT,MAAM,EAAE,cA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BAAsB,CAAC,OAAO,CAAC;aACnD;YAED,OAAO,CACL,SAAS,KAAK,sBAAsB,CAAC,KAAK,CAAC,SAAS;gBACpD,OAAO,KAAK,sBAAsB,CAAC,OAAO,CAC3C,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,sCAAsC,CACpD,eAAgC,EAChC,iBAAkC;IAElC,MAAM,0BAA0B,GAAG,eAAe,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;QACtE,OAAO,IAAA,wBAAgB,EAAC,KAAK,CAAC,CAAC;IACjC,CAAC,CAAC,CAAC;IAEH,OAAO,8CAA8C,CACnD,eAAe,CAAC,OAAO,EACvB,0BAA0B,EAC1B,iBAAiB,CAClB,CAAC;AACJ,CAAC;AAbD,wFAaC;AAED;;;;;;;GAOG;AACH,SAAgB,oCAAoC,CAClD,SAAwB,EACxB,iBAAkC;IAElC,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,IAAA,0BAAkB,EAAC,SAAS,CAAC,CAAC;IAEzD,OAAO,8CAA8C,CACnD,OAAO,EACP,CAAC,KAAK,CAAC,EACP,iBAAiB,CAClB,CAAC;AACJ,CAAC;AAXD,oFAWC","sourcesContent":["import { isEqualCaseInsensitive } from '@metamask/controller-utils';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport {\n assertIsStrictHexString,\n type CaipAccountAddress,\n type CaipAccountId,\n type CaipNamespace,\n type CaipReference,\n type Hex,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\n\nimport type { Caip25CaveatValue } from '../caip25Permission';\nimport { KnownWalletScopeString } from '../scope/constants';\nimport { getUniqueArrayItems } from '../scope/transform';\nimport type { InternalScopeString, InternalScopesObject } from '../scope/types';\nimport { parseScopeString } from '../scope/types';\n\n/*\n *\n *\n * EVM SPECIFIC GETTERS AND SETTERS\n *\n *\n */\n\n/**\n *\n * Checks if a scope string is either an EIP155 or wallet namespaced scope string.\n *\n * @param scopeString - The scope string to check.\n * @returns True if the scope string is an EIP155 or wallet namespaced scope string, false otherwise.\n */\nconst isEip155ScopeString = (scopeString: InternalScopeString) => {\n const { namespace } = parseScopeString(scopeString);\n\n return (\n namespace === KnownCaipNamespace.Eip155 ||\n // We are trying to discern the type of `scopeString`.\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n scopeString === KnownWalletScopeString.Eip155\n );\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) accounts from internal scopes.\n *\n * @param scopes - The internal scopes from which to get the Ethereum accounts.\n * @returns An array of Ethereum accounts.\n */\nconst getEthAccountsFromScopes = (scopes: InternalScopesObject) => {\n const ethAccounts: Hex[] = [];\n\n Object.entries(scopes).forEach(([_, { accounts }]) => {\n accounts?.forEach((account) => {\n const { address, chainId } = parseCaipAccountId(account);\n\n if (isEip155ScopeString(chainId)) {\n // This address should always be a valid Hex string because\n // it's an EIP155/Ethereum account\n assertIsStrictHexString(address);\n ethAccounts.push(address);\n }\n });\n });\n\n return ethAccounts;\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to get the Ethereum accounts from.\n * @returns An array of Ethereum accounts.\n */\nexport const getEthAccounts = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n): Hex[] => {\n const { requiredScopes, optionalScopes } = caip25CaveatValue;\n\n const ethAccounts: Hex[] = [\n ...getEthAccountsFromScopes(requiredScopes),\n ...getEthAccountsFromScopes(optionalScopes),\n ];\n\n return getUniqueArrayItems(ethAccounts);\n};\n\n/**\n * Sets the Ethereum (EIP155 namespaced) accounts for the given scopes object.\n *\n * @param scopesObject - The scopes object to set the Ethereum accounts for.\n * @param accounts - The Ethereum accounts to set.\n * @returns The updated scopes object with the Ethereum accounts set.\n */\nconst setEthAccountsForScopesObject = (\n scopesObject: InternalScopesObject,\n accounts: Hex[],\n) => {\n const updatedScopesObject: InternalScopesObject = {};\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 isWalletNamespace = scopeString === KnownCaipNamespace.Wallet;\n const { namespace, reference } = parseScopeString(scopeString);\n if (!isEip155ScopeString(scopeString) && !isWalletNamespace) {\n updatedScopesObject[scopeString] = scopeObject;\n return;\n }\n\n let caipAccounts: CaipAccountId[] = [];\n if (namespace && reference) {\n caipAccounts = accounts.map<CaipAccountId>(\n (account) => `${namespace}:${reference}:${account}`,\n );\n }\n\n updatedScopesObject[scopeString] = {\n ...scopeObject,\n accounts: caipAccounts,\n };\n });\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the Ethereum (EIP155 namespaced) accounts for the given CAIP-25 caveat value.\n * We set the same accounts for all the scopes that are EIP155 or Wallet namespaced because\n * we do not provide UI/UX flows for selecting different accounts across different chains.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the Ethereum accounts for.\n * @param accounts - The Ethereum accounts to set.\n * @returns The updated CAIP-25 caveat value with the Ethereum accounts set.\n */\nexport const setEthAccounts = (\n caip25CaveatValue: Caip25CaveatValue,\n accounts: Hex[],\n): Caip25CaveatValue => {\n return {\n ...caip25CaveatValue,\n requiredScopes: setEthAccountsForScopesObject(\n caip25CaveatValue.requiredScopes,\n accounts,\n ),\n optionalScopes: setEthAccountsForScopesObject(\n caip25CaveatValue.optionalScopes,\n accounts,\n ),\n };\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 accounts from an array of scopes objects\n * This extracts all account IDs from both required and optional scopes\n * and returns a unique set.\n *\n * @param scopesObjects - The scopes objects to extract accounts from\n * @returns Array of unique account IDs\n */\nexport function getCaipAccountIdsFromScopesObjects(\n scopesObjects: InternalScopesObject[],\n): CaipAccountId[] {\n const allAccounts = new Set<CaipAccountId>();\n\n for (const scopeObject of scopesObjects) {\n for (const { accounts } of Object.values(scopeObject)) {\n for (const account of accounts) {\n allAccounts.add(account);\n }\n }\n }\n\n return Array.from(allAccounts);\n}\n\n/**\n * Gets all permitted accounts from a CAIP-25 caveat\n * This extracts all account IDs from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to extract accounts from\n * @returns Array of unique account IDs\n */\nexport function getCaipAccountIdsFromCaip25CaveatValue(\n caip25CaveatValue: Caip25CaveatValue,\n): CaipAccountId[] {\n return getCaipAccountIdsFromScopesObjects([\n caip25CaveatValue.requiredScopes,\n caip25CaveatValue.optionalScopes,\n ]);\n}\n\n/**\n *\n * Setters\n *\n */\n\n/**\n * Sets the CAIP account IDs to scopes with matching namespaces in the given scopes object.\n * This function should not be used with Smart Contract Accounts (SCA) because\n * it adds the same account ID to all the scopes that have the same namespace.\n *\n * @param scopesObject - The scopes object to set the CAIP account IDs for.\n * @param accounts - The CAIP account IDs to add to the appropriate scopes.\n * @returns The updated scopes object with the CAIP account IDs set.\n */\nconst setNonSCACaipAccountIdsInScopesObject = (\n scopesObject: InternalScopesObject,\n accounts: CaipAccountId[],\n) => {\n const accountsByNamespace = new Map<string, Set<string>>();\n\n for (const account of accounts) {\n const {\n chain: { namespace },\n address,\n } = parseCaipAccountId(account);\n\n if (!accountsByNamespace.has(namespace)) {\n accountsByNamespace.set(namespace, new Set());\n }\n\n accountsByNamespace.get(namespace)?.add(address);\n }\n\n const updatedScopesObject: InternalScopesObject = {};\n\n for (const [scopeString, scopeObject] of Object.entries(scopesObject)) {\n const { namespace, reference } = parseScopeString(scopeString as string);\n\n let caipAccounts: CaipAccountId[] = [];\n\n if (namespace && reference && accountsByNamespace.has(namespace)) {\n const addressSet = accountsByNamespace.get(namespace);\n if (addressSet) {\n caipAccounts = Array.from(addressSet).map(\n (address) => `${namespace}:${reference}:${address}` as CaipAccountId,\n );\n }\n }\n\n updatedScopesObject[scopeString as keyof typeof scopesObject] = {\n ...scopeObject,\n accounts: getUniqueArrayItems(caipAccounts),\n };\n }\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the permitted accounts to scopes with matching namespaces in the given CAIP-25 caveat value.\n * This function should not be used with Smart Contract Accounts (SCA) because\n * it adds the same account ID to all scopes that have the same namespace as the account.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.\n * @param accounts - The permitted accounts to add to the appropriate scopes.\n * @returns The updated CAIP-25 caveat value with the permitted accounts set.\n */\nexport const setNonSCACaipAccountIdsInCaip25CaveatValue = (\n caip25CaveatValue: Caip25CaveatValue,\n accounts: CaipAccountId[],\n): Caip25CaveatValue => {\n return {\n ...caip25CaveatValue,\n requiredScopes: setNonSCACaipAccountIdsInScopesObject(\n caip25CaveatValue.requiredScopes,\n accounts,\n ),\n optionalScopes: setNonSCACaipAccountIdsInScopesObject(\n caip25CaveatValue.optionalScopes,\n accounts,\n ),\n };\n};\n\n/**\n * Checks if an address and list of parsed scopes are connected to any of\n * the permitted accounts based on scope matching\n *\n * @param address - The CAIP account address to check against permitted accounts\n * @param parsedAccountScopes - The list of parsed CAIP chain ID to check against permitted accounts\n * @param permittedAccounts - Array of CAIP account IDs that are permitted\n * @returns True if the address and any account scope is connected to any permitted account\n */\nfunction isAddressWithParsedScopesInPermittedAccountIds(\n address: CaipAccountAddress,\n parsedAccountScopes: {\n namespace?: CaipNamespace;\n reference?: CaipReference;\n }[],\n permittedAccounts: CaipAccountId[],\n) {\n if (!address || !parsedAccountScopes.length || !permittedAccounts.length) {\n return false;\n }\n\n return permittedAccounts.some((account) => {\n const parsedPermittedAccount = parseCaipAccountId(account);\n\n return parsedAccountScopes.some(({ namespace, reference }) => {\n if (\n namespace !== parsedPermittedAccount.chain.namespace &&\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n parsedPermittedAccount.chain.namespace !== KnownCaipNamespace.Wallet\n ) {\n return false;\n }\n\n // handle wallet:<namespace>:<address> case where namespaces are mismatched but addresses match\n // i.e. wallet:notSolana:12389812309123 and solana:0:12389812309123\n if (\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n parsedPermittedAccount.chain.namespace === KnownCaipNamespace.Wallet &&\n namespace !== parsedPermittedAccount.chain.reference\n ) {\n return false;\n }\n\n // handle eip155:0 case and insensitive evm address comparison\n if (namespace === KnownCaipNamespace.Eip155) {\n return (\n (reference === '0' ||\n reference === parsedPermittedAccount.chain.reference) &&\n isEqualCaseInsensitive(address, parsedPermittedAccount.address)\n );\n }\n\n // handle wallet:<namespace>:<address> case\n if (\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n parsedPermittedAccount.chain.namespace === KnownCaipNamespace.Wallet\n ) {\n return address === parsedPermittedAccount.address;\n }\n\n return (\n reference === parsedPermittedAccount.chain.reference &&\n address === parsedPermittedAccount.address\n );\n });\n });\n}\n\n/**\n * Checks if an internal account is connected to any of the permitted accounts\n * based on scope matching\n *\n * @param internalAccount - The internal account to check against permitted accounts\n * @param permittedAccounts - Array of CAIP account IDs that are permitted\n * @returns True if the account is connected to any permitted account\n */\nexport function isInternalAccountInPermittedAccountIds(\n internalAccount: InternalAccount,\n permittedAccounts: CaipAccountId[],\n): boolean {\n const parsedInteralAccountScopes = internalAccount.scopes.map((scope) => {\n return parseScopeString(scope);\n });\n\n return isAddressWithParsedScopesInPermittedAccountIds(\n internalAccount.address,\n parsedInteralAccountScopes,\n permittedAccounts,\n );\n}\n\n/**\n * Checks if an CAIP account ID is connected to any of the permitted accounts\n * based on scope matching\n *\n * @param accountId - The CAIP account ID to check against permitted accounts\n * @param permittedAccounts - Array of CAIP account IDs that are permitted\n * @returns True if the account is connected to any permitted account\n */\nexport function isCaipAccountIdInPermittedAccountIds(\n accountId: CaipAccountId,\n permittedAccounts: CaipAccountId[],\n): boolean {\n const { address, chain } = parseCaipAccountId(accountId);\n\n return isAddressWithParsedScopesInPermittedAccountIds(\n address,\n [chain],\n permittedAccounts,\n );\n}\n"]}
1
+ {"version":3,"file":"caip-permission-operator-accounts.cjs","sourceRoot":"","sources":["../../src/operators/caip-permission-operator-accounts.ts"],"names":[],"mappings":";;;AAAA,iEAAoE;AAEpE,2CASyB;AAGzB,sDAA4D;AAC5D,sDAAyD;AAEzD,8CAAkD;AAElD;;;;;;GAMG;AAEH;;;;;;GAMG;AACH,MAAM,mBAAmB,GAAG,CAAC,WAAgC,EAAE,EAAE;IAC/D,MAAM,EAAE,SAAS,EAAE,GAAG,IAAA,wBAAgB,EAAC,WAAW,CAAC,CAAC;IAEpD,OAAO,CACL,SAAS,KAAK,0BAAkB,CAAC,MAAM;QACvC,sDAAsD;QACtD,wEAAwE;QACxE,WAAW,KAAK,kCAAsB,CAAC,MAAM,CAC9C,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,wBAAwB,GAAG,CAAC,MAA4B,EAAE,EAAE;IAChE,MAAM,WAAW,GAAU,EAAE,CAAC;IAE9B,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,EAAE;QACnD,QAAQ,EAAE,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC5B,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,IAAA,0BAAkB,EAAC,OAAO,CAAC,CAAC;YAEzD,IAAI,mBAAmB,CAAC,OAAO,CAAC,EAAE;gBAChC,2DAA2D;gBAC3D,kCAAkC;gBAClC,IAAA,+BAAuB,EAAC,OAAO,CAAC,CAAC;gBACjC,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;aAC3B;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAEF;;;;;GAKG;AACI,MAAM,cAAc,GAAG,CAC5B,iBAGC,EACM,EAAE;IACT,MAAM,EAAE,c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sBAAsB,CAAC,OAAO,CAAC;aACnD;YAED,OAAO,CACL,SAAS,KAAK,sBAAsB,CAAC,KAAK,CAAC,SAAS;gBACpD,OAAO,KAAK,sBAAsB,CAAC,OAAO,CAC3C,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,sCAAsC,CACpD,eAAgC,EAChC,iBAAkC;IAElC,kGAAkG;IAClG,mHAAmH;IACnH,0HAA0H;IAC1H,yCAAyC;IACzC,IAAI,CAAC,eAAe,EAAE,MAAM,EAAE,MAAM,EAAE;QACpC,OAAO,KAAK,CAAC;KACd;IAED,MAAM,0BAA0B,GAAG,eAAe,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;QACtE,OAAO,IAAA,wBAAgB,EAAC,KAAK,CAAC,CAAC;IACjC,CAAC,CAAC,CAAC;IAEH,OAAO,8CAA8C,CACnD,eAAe,CAAC,OAAO,EACvB,0BAA0B,EAC1B,iBAAiB,CAClB,CAAC;AACJ,CAAC;AArBD,wFAqBC;AAED;;;;;;;GAOG;AACH,SAAgB,oCAAoC,CAClD,SAAwB,EACxB,iBAAkC;IAElC,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,IAAA,0BAAkB,EAAC,SAAS,CAAC,CAAC;IAEzD,OAAO,8CAA8C,CACnD,OAAO,EACP,CAAC,KAAK,CAAC,EACP,iBAAiB,CAClB,CAAC;AACJ,CAAC;AAXD,oFAWC","sourcesContent":["import { isEqualCaseInsensitive } from '@metamask/controller-utils';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport {\n assertIsStrictHexString,\n type CaipAccountAddress,\n type CaipAccountId,\n type CaipNamespace,\n type CaipReference,\n type Hex,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\n\nimport type { Caip25CaveatValue } from '../caip25Permission';\nimport { KnownWalletScopeString } from '../scope/constants';\nimport { getUniqueArrayItems } from '../scope/transform';\nimport type { InternalScopeString, InternalScopesObject } from '../scope/types';\nimport { parseScopeString } from '../scope/types';\n\n/*\n *\n *\n * EVM SPECIFIC GETTERS AND SETTERS\n *\n *\n */\n\n/**\n *\n * Checks if a scope string is either an EIP155 or wallet namespaced scope string.\n *\n * @param scopeString - The scope string to check.\n * @returns True if the scope string is an EIP155 or wallet namespaced scope string, false otherwise.\n */\nconst isEip155ScopeString = (scopeString: InternalScopeString) => {\n const { namespace } = parseScopeString(scopeString);\n\n return (\n namespace === KnownCaipNamespace.Eip155 ||\n // We are trying to discern the type of `scopeString`.\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n scopeString === KnownWalletScopeString.Eip155\n );\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) accounts from internal scopes.\n *\n * @param scopes - The internal scopes from which to get the Ethereum accounts.\n * @returns An array of Ethereum accounts.\n */\nconst getEthAccountsFromScopes = (scopes: InternalScopesObject) => {\n const ethAccounts: Hex[] = [];\n\n Object.entries(scopes).forEach(([_, { accounts }]) => {\n accounts?.forEach((account) => {\n const { address, chainId } = parseCaipAccountId(account);\n\n if (isEip155ScopeString(chainId)) {\n // This address should always be a valid Hex string because\n // it's an EIP155/Ethereum account\n assertIsStrictHexString(address);\n ethAccounts.push(address);\n }\n });\n });\n\n return ethAccounts;\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to get the Ethereum accounts from.\n * @returns An array of Ethereum accounts.\n */\nexport const getEthAccounts = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n): Hex[] => {\n const { requiredScopes, optionalScopes } = caip25CaveatValue;\n\n const ethAccounts: Hex[] = [\n ...getEthAccountsFromScopes(requiredScopes),\n ...getEthAccountsFromScopes(optionalScopes),\n ];\n\n return getUniqueArrayItems(ethAccounts);\n};\n\n/**\n * Sets the Ethereum (EIP155 namespaced) accounts for the given scopes object.\n *\n * @param scopesObject - The scopes object to set the Ethereum accounts for.\n * @param accounts - The Ethereum accounts to set.\n * @returns The updated scopes object with the Ethereum accounts set.\n */\nconst setEthAccountsForScopesObject = (\n scopesObject: InternalScopesObject,\n accounts: Hex[],\n) => {\n const updatedScopesObject: InternalScopesObject = {};\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 isWalletNamespace = scopeString === KnownCaipNamespace.Wallet;\n const { namespace, reference } = parseScopeString(scopeString);\n if (!isEip155ScopeString(scopeString) && !isWalletNamespace) {\n updatedScopesObject[scopeString] = scopeObject;\n return;\n }\n\n let caipAccounts: CaipAccountId[] = [];\n if (namespace && reference) {\n caipAccounts = accounts.map<CaipAccountId>(\n (account) => `${namespace}:${reference}:${account}`,\n );\n }\n\n updatedScopesObject[scopeString] = {\n ...scopeObject,\n accounts: caipAccounts,\n };\n });\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the Ethereum (EIP155 namespaced) accounts for the given CAIP-25 caveat value.\n * We set the same accounts for all the scopes that are EIP155 or Wallet namespaced because\n * we do not provide UI/UX flows for selecting different accounts across different chains.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the Ethereum accounts for.\n * @param accounts - The Ethereum accounts to set.\n * @returns The updated CAIP-25 caveat value with the Ethereum accounts set.\n */\nexport const setEthAccounts = (\n caip25CaveatValue: Caip25CaveatValue,\n accounts: Hex[],\n): Caip25CaveatValue => {\n return {\n ...caip25CaveatValue,\n requiredScopes: setEthAccountsForScopesObject(\n caip25CaveatValue.requiredScopes,\n accounts,\n ),\n optionalScopes: setEthAccountsForScopesObject(\n caip25CaveatValue.optionalScopes,\n accounts,\n ),\n };\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 accounts from an array of scopes objects\n * This extracts all account IDs from both required and optional scopes\n * and returns a unique set.\n *\n * @param scopesObjects - The scopes objects to extract accounts from\n * @returns Array of unique account IDs\n */\nexport function getCaipAccountIdsFromScopesObjects(\n scopesObjects: InternalScopesObject[],\n): CaipAccountId[] {\n const allAccounts = new Set<CaipAccountId>();\n\n for (const scopeObject of scopesObjects) {\n for (const { accounts } of Object.values(scopeObject)) {\n for (const account of accounts) {\n allAccounts.add(account);\n }\n }\n }\n\n return Array.from(allAccounts);\n}\n\n/**\n * Gets all permitted accounts from a CAIP-25 caveat\n * This extracts all account IDs from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to extract accounts from\n * @returns Array of unique account IDs\n */\nexport function getCaipAccountIdsFromCaip25CaveatValue(\n caip25CaveatValue: Caip25CaveatValue,\n): CaipAccountId[] {\n return getCaipAccountIdsFromScopesObjects([\n caip25CaveatValue.requiredScopes,\n caip25CaveatValue.optionalScopes,\n ]);\n}\n\n/**\n *\n * Setters\n *\n */\n\n/**\n * Sets the CAIP account IDs to scopes with matching namespaces in the given scopes object.\n * This function should not be used with Smart Contract Accounts (SCA) because\n * it adds the same account ID to all the scopes that have the same namespace.\n *\n * @param scopesObject - The scopes object to set the CAIP account IDs for.\n * @param accounts - The CAIP account IDs to add to the appropriate scopes.\n * @returns The updated scopes object with the CAIP account IDs set.\n */\nconst setNonSCACaipAccountIdsInScopesObject = (\n scopesObject: InternalScopesObject,\n accounts: CaipAccountId[],\n) => {\n const accountsByNamespace = new Map<string, Set<string>>();\n\n for (const account of accounts) {\n const {\n chain: { namespace },\n address,\n } = parseCaipAccountId(account);\n\n if (!accountsByNamespace.has(namespace)) {\n accountsByNamespace.set(namespace, new Set());\n }\n\n accountsByNamespace.get(namespace)?.add(address);\n }\n\n const updatedScopesObject: InternalScopesObject = {};\n\n for (const [scopeString, scopeObject] of Object.entries(scopesObject)) {\n const { namespace, reference } = parseScopeString(scopeString as string);\n\n let caipAccounts: CaipAccountId[] = [];\n\n if (namespace && reference && accountsByNamespace.has(namespace)) {\n const addressSet = accountsByNamespace.get(namespace);\n if (addressSet) {\n caipAccounts = Array.from(addressSet).map(\n (address) => `${namespace}:${reference}:${address}` as CaipAccountId,\n );\n }\n }\n\n updatedScopesObject[scopeString as keyof typeof scopesObject] = {\n ...scopeObject,\n accounts: getUniqueArrayItems(caipAccounts),\n };\n }\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the permitted accounts to scopes with matching namespaces in the given CAIP-25 caveat value.\n * This function should not be used with Smart Contract Accounts (SCA) because\n * it adds the same account ID to all scopes that have the same namespace as the account.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.\n * @param accounts - The permitted accounts to add to the appropriate scopes.\n * @returns The updated CAIP-25 caveat value with the permitted accounts set.\n */\nexport const setNonSCACaipAccountIdsInCaip25CaveatValue = (\n caip25CaveatValue: Caip25CaveatValue,\n accounts: CaipAccountId[],\n): Caip25CaveatValue => {\n return {\n ...caip25CaveatValue,\n requiredScopes: setNonSCACaipAccountIdsInScopesObject(\n caip25CaveatValue.requiredScopes,\n accounts,\n ),\n optionalScopes: setNonSCACaipAccountIdsInScopesObject(\n caip25CaveatValue.optionalScopes,\n accounts,\n ),\n };\n};\n\n/**\n * Checks if an address and list of parsed scopes are connected to any of\n * the permitted accounts based on scope matching\n *\n * @param address - The CAIP account address to check against permitted accounts\n * @param parsedAccountScopes - The list of parsed CAIP chain ID to check against permitted accounts\n * @param permittedAccounts - Array of CAIP account IDs that are permitted\n * @returns True if the address and any account scope is connected to any permitted account\n */\nfunction isAddressWithParsedScopesInPermittedAccountIds(\n address: CaipAccountAddress,\n parsedAccountScopes: {\n namespace?: CaipNamespace;\n reference?: CaipReference;\n }[],\n permittedAccounts: CaipAccountId[],\n) {\n if (!address || !parsedAccountScopes.length || !permittedAccounts.length) {\n return false;\n }\n\n return permittedAccounts.some((account) => {\n const parsedPermittedAccount = parseCaipAccountId(account);\n\n return parsedAccountScopes.some(({ namespace, reference }) => {\n if (\n namespace !== parsedPermittedAccount.chain.namespace &&\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n parsedPermittedAccount.chain.namespace !== KnownCaipNamespace.Wallet\n ) {\n return false;\n }\n\n // handle wallet:<namespace>:<address> case where namespaces are mismatched but addresses match\n // i.e. wallet:notSolana:12389812309123 and solana:0:12389812309123\n if (\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n parsedPermittedAccount.chain.namespace === KnownCaipNamespace.Wallet &&\n namespace !== parsedPermittedAccount.chain.reference\n ) {\n return false;\n }\n\n // handle eip155:0 case and insensitive evm address comparison\n if (namespace === KnownCaipNamespace.Eip155) {\n return (\n (reference === '0' ||\n reference === parsedPermittedAccount.chain.reference) &&\n isEqualCaseInsensitive(address, parsedPermittedAccount.address)\n );\n }\n\n // handle wallet:<namespace>:<address> case\n if (\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n parsedPermittedAccount.chain.namespace === KnownCaipNamespace.Wallet\n ) {\n return address === parsedPermittedAccount.address;\n }\n\n return (\n reference === parsedPermittedAccount.chain.reference &&\n address === parsedPermittedAccount.address\n );\n });\n });\n}\n\n/**\n * Checks if an internal account is connected to any of the permitted accounts\n * based on scope matching\n *\n * @param internalAccount - The internal account to check against permitted accounts\n * @param permittedAccounts - Array of CAIP account IDs that are permitted\n * @returns True if the account is connected to any permitted account\n */\nexport function isInternalAccountInPermittedAccountIds(\n internalAccount: InternalAccount,\n permittedAccounts: CaipAccountId[],\n): boolean {\n // temporary fix for the issue where the internal account has no scopes and or scopes is undefined\n // TODO: remove this once the bug is fixed (tracked here: https://github.com/MetaMask/accounts-planning/issues/941)\n // there is currently a bug where an account associated with a snap can fail to add scopes to the internal account in time\n // before we attempt to access this state\n if (!internalAccount?.scopes?.length) {\n return false;\n }\n\n const parsedInteralAccountScopes = internalAccount.scopes.map((scope) => {\n return parseScopeString(scope);\n });\n\n return isAddressWithParsedScopesInPermittedAccountIds(\n internalAccount.address,\n parsedInteralAccountScopes,\n permittedAccounts,\n );\n}\n\n/**\n * Checks if an CAIP account ID is connected to any of the permitted accounts\n * based on scope matching\n *\n * @param accountId - The CAIP account ID to check against permitted accounts\n * @param permittedAccounts - Array of CAIP account IDs that are permitted\n * @returns True if the account is connected to any permitted account\n */\nexport function isCaipAccountIdInPermittedAccountIds(\n accountId: CaipAccountId,\n permittedAccounts: CaipAccountId[],\n): boolean {\n const { address, chain } = parseCaipAccountId(accountId);\n\n return isAddressWithParsedScopesInPermittedAccountIds(\n address,\n [chain],\n permittedAccounts,\n );\n}\n"]}
@@ -70,4 +70,4 @@ export declare function isInternalAccountInPermittedAccountIds(internalAccount:
70
70
  * @returns True if the account is connected to any permitted account
71
71
  */
72
72
  export declare function isCaipAccountIdInPermittedAccountIds(accountId: CaipAccountId, permittedAccounts: CaipAccountId[]): boolean;
73
- //# sourceMappingURL=caip-permission-adapter-accounts.d.cts.map
73
+ //# sourceMappingURL=caip-permission-operator-accounts.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-operator-accounts.d.cts","sourceRoot":"","sources":["../../src/operators/caip-permission-operator-accounts.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AACtE,OAAO,EAGL,KAAK,aAAa,EAGlB,KAAK,GAAG,EAGT,wBAAwB;AAEzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAG7D,OAAO,KAAK,EAAuB,oBAAoB,EAAE,2BAAuB;AAsDhF;;;;;GAKG;AACH,eAAO,MAAM,cAAc,sBACN,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC,KACA,GAAG,EASL,CAAC;AAwCF;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,sBACN,iBAAiB,YAC1B,GAAG,EAAE,KACd,iBAYF,CAAC;AAUF;;;;GAIG;AAEH;;;;;;;GAOG;AACH,wBAAgB,kCAAkC,CAChD,aAAa,EAAE,oBAAoB,EAAE,GACpC,aAAa,EAAE,CAYjB;AAED;;;;;;;GAOG;AACH,wBAAgB,sCAAsC,CACpD,iBAAiB,EAAE,iBAAiB,GACnC,aAAa,EAAE,CAKjB;AA6DD;;;;;;;;GAQG;AACH,eAAO,MAAM,0CAA0C,sBAClC,iBAAiB,YAC1B,aAAa,EAAE,KACxB,iBAYF,CAAC;AAsEF;;;;;;;GAOG;AACH,wBAAgB,sCAAsC,CACpD,eAAe,EAAE,eAAe,EAChC,iBAAiB,EAAE,aAAa,EAAE,GACjC,OAAO,CAkBT;AAED;;;;;;;GAOG;AACH,wBAAgB,oCAAoC,CAClD,SAAS,EAAE,aAAa,EACxB,iBAAiB,EAAE,aAAa,EAAE,GACjC,OAAO,CAQT"}
@@ -70,4 +70,4 @@ export declare function isInternalAccountInPermittedAccountIds(internalAccount:
70
70
  * @returns True if the account is connected to any permitted account
71
71
  */
72
72
  export declare function isCaipAccountIdInPermittedAccountIds(accountId: CaipAccountId, permittedAccounts: CaipAccountId[]): boolean;
73
- //# sourceMappingURL=caip-permission-adapter-accounts.d.mts.map
73
+ //# sourceMappingURL=caip-permission-operator-accounts.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-operator-accounts.d.mts","sourceRoot":"","sources":["../../src/operators/caip-permission-operator-accounts.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AACtE,OAAO,EAGL,KAAK,aAAa,EAGlB,KAAK,GAAG,EAGT,wBAAwB;AAEzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAG7D,OAAO,KAAK,EAAuB,oBAAoB,EAAE,2BAAuB;AAsDhF;;;;;GAKG;AACH,eAAO,MAAM,cAAc,sBACN,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC,KACA,GAAG,EASL,CAAC;AAwCF;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,sBACN,iBAAiB,YAC1B,GAAG,EAAE,KACd,iBAYF,CAAC;AAUF;;;;GAIG;AAEH;;;;;;;GAOG;AACH,wBAAgB,kCAAkC,CAChD,aAAa,EAAE,oBAAoB,EAAE,GACpC,aAAa,EAAE,CAYjB;AAED;;;;;;;GAOG;AACH,wBAAgB,sCAAsC,CACpD,iBAAiB,EAAE,iBAAiB,GACnC,aAAa,EAAE,CAKjB;AA6DD;;;;;;;;GAQG;AACH,eAAO,MAAM,0CAA0C,sBAClC,iBAAiB,YAC1B,aAAa,EAAE,KACxB,iBAYF,CAAC;AAsEF;;;;;;;GAOG;AACH,wBAAgB,sCAAsC,CACpD,eAAe,EAAE,eAAe,EAChC,iBAAiB,EAAE,aAAa,EAAE,GACjC,OAAO,CAkBT;AAED;;;;;;;GAOG;AACH,wBAAgB,oCAAoC,CAClD,SAAS,EAAE,aAAa,EACxB,iBAAiB,EAAE,aAAa,EAAE,GACjC,OAAO,CAQT"}
@@ -260,6 +260,13 @@ function isAddressWithParsedScopesInPermittedAccountIds(address, parsedAccountSc
260
260
  * @returns True if the account is connected to any permitted account
261
261
  */
262
262
  export function isInternalAccountInPermittedAccountIds(internalAccount, permittedAccounts) {
263
+ // temporary fix for the issue where the internal account has no scopes and or scopes is undefined
264
+ // TODO: remove this once the bug is fixed (tracked here: https://github.com/MetaMask/accounts-planning/issues/941)
265
+ // there is currently a bug where an account associated with a snap can fail to add scopes to the internal account in time
266
+ // before we attempt to access this state
267
+ if (!internalAccount?.scopes?.length) {
268
+ return false;
269
+ }
263
270
  const parsedInteralAccountScopes = internalAccount.scopes.map((scope) => {
264
271
  return parseScopeString(scope);
265
272
  });
@@ -277,4 +284,4 @@ export function isCaipAccountIdInPermittedAccountIds(accountId, permittedAccount
277
284
  const { address, chain } = parseCaipAccountId(accountId);
278
285
  return isAddressWithParsedScopesInPermittedAccountIds(address, [chain], permittedAccounts);
279
286
  }
280
- //# sourceMappingURL=caip-permission-adapter-accounts.mjs.map
287
+ //# sourceMappingURL=caip-permission-operator-accounts.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-operator-accounts.mjs","sourceRoot":"","sources":["../../src/operators/caip-permission-operator-accounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,mCAAmC;AAEpE,OAAO,EACL,uBAAuB,EAMvB,kBAAkB,EAClB,kBAAkB,EACnB,wBAAwB;AAGzB,OAAO,EAAE,sBAAsB,EAAE,+BAA2B;AAC5D,OAAO,EAAE,mBAAmB,EAAE,+BAA2B;AAEzD,OAAO,EAAE,gBAAgB,EAAE,2BAAuB;AAElD;;;;;;GAMG;AAEH;;;;;;GAMG;AACH,MAAM,mBAAmB,GAAG,CAAC,WAAgC,EAAE,EAAE;IAC/D,MAAM,EAAE,SAAS,EAAE,GAAG,gBAAgB,CAAC,WAAW,CAAC,CAAC;IAEpD,OAAO,CACL,SAAS,KAAK,kBAAkB,CAAC,MAAM;QACvC,sDAAsD;QACtD,wEAAwE;QACxE,WAAW,KAAK,sBAAsB,CAAC,MAAM,CAC9C,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,wBAAwB,GAAG,CAAC,MAA4B,EAAE,EAAE;IAChE,MAAM,WAAW,GAAU,EAAE,CAAC;IAE9B,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,EAAE;QACnD,QAAQ,EAAE,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC5B,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,kBAAkB,CAAC,OAAO,CAAC,CAAC;YAEzD,IAAI,mBAAmB,CAAC,OAAO,CAAC,EAAE;gBAChC,2DAA2D;gBAC3D,kCAAkC;gBAClC,uBAAuB,CAAC,OAAO,CAAC,CAAC;gBACjC,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;aAC3B;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,OAAO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,OAAO,KAAK,sBAAsB,CAAC,OAAO,CAAC;aACnD;YAED,OAAO,CACL,SAAS,KAAK,sBAAsB,CAAC,KAAK,CAAC,SAAS;gBACpD,OAAO,KAAK,sBAAsB,CAAC,OAAO,CAC3C,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,sCAAsC,CACpD,eAAgC,EAChC,iBAAkC;IAElC,kGAAkG;IAClG,mHAAmH;IACnH,0HAA0H;IAC1H,yCAAyC;IACzC,IAAI,CAAC,eAAe,EAAE,MAAM,EAAE,MAAM,EAAE;QACpC,OAAO,KAAK,CAAC;KACd;IAED,MAAM,0BAA0B,GAAG,eAAe,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;QACtE,OAAO,gBAAgB,CAAC,KAAK,CAAC,CAAC;IACjC,CAAC,CAAC,CAAC;IAEH,OAAO,8CAA8C,CACnD,eAAe,CAAC,OAAO,EACvB,0BAA0B,EAC1B,iBAAiB,CAClB,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,oCAAoC,CAClD,SAAwB,EACxB,iBAAkC;IAElC,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,kBAAkB,CAAC,SAAS,CAAC,CAAC;IAEzD,OAAO,8CAA8C,CACnD,OAAO,EACP,CAAC,KAAK,CAAC,EACP,iBAAiB,CAClB,CAAC;AACJ,CAAC","sourcesContent":["import { isEqualCaseInsensitive } from '@metamask/controller-utils';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport {\n assertIsStrictHexString,\n type CaipAccountAddress,\n type CaipAccountId,\n type CaipNamespace,\n type CaipReference,\n type Hex,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\n\nimport type { Caip25CaveatValue } from '../caip25Permission';\nimport { KnownWalletScopeString } from '../scope/constants';\nimport { getUniqueArrayItems } from '../scope/transform';\nimport type { InternalScopeString, InternalScopesObject } from '../scope/types';\nimport { parseScopeString } from '../scope/types';\n\n/*\n *\n *\n * EVM SPECIFIC GETTERS AND SETTERS\n *\n *\n */\n\n/**\n *\n * Checks if a scope string is either an EIP155 or wallet namespaced scope string.\n *\n * @param scopeString - The scope string to check.\n * @returns True if the scope string is an EIP155 or wallet namespaced scope string, false otherwise.\n */\nconst isEip155ScopeString = (scopeString: InternalScopeString) => {\n const { namespace } = parseScopeString(scopeString);\n\n return (\n namespace === KnownCaipNamespace.Eip155 ||\n // We are trying to discern the type of `scopeString`.\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n scopeString === KnownWalletScopeString.Eip155\n );\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) accounts from internal scopes.\n *\n * @param scopes - The internal scopes from which to get the Ethereum accounts.\n * @returns An array of Ethereum accounts.\n */\nconst getEthAccountsFromScopes = (scopes: InternalScopesObject) => {\n const ethAccounts: Hex[] = [];\n\n Object.entries(scopes).forEach(([_, { accounts }]) => {\n accounts?.forEach((account) => {\n const { address, chainId } = parseCaipAccountId(account);\n\n if (isEip155ScopeString(chainId)) {\n // This address should always be a valid Hex string because\n // it's an EIP155/Ethereum account\n assertIsStrictHexString(address);\n ethAccounts.push(address);\n }\n });\n });\n\n return ethAccounts;\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to get the Ethereum accounts from.\n * @returns An array of Ethereum accounts.\n */\nexport const getEthAccounts = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n): Hex[] => {\n const { requiredScopes, optionalScopes } = caip25CaveatValue;\n\n const ethAccounts: Hex[] = [\n ...getEthAccountsFromScopes(requiredScopes),\n ...getEthAccountsFromScopes(optionalScopes),\n ];\n\n return getUniqueArrayItems(ethAccounts);\n};\n\n/**\n * Sets the Ethereum (EIP155 namespaced) accounts for the given scopes object.\n *\n * @param scopesObject - The scopes object to set the Ethereum accounts for.\n * @param accounts - The Ethereum accounts to set.\n * @returns The updated scopes object with the Ethereum accounts set.\n */\nconst setEthAccountsForScopesObject = (\n scopesObject: InternalScopesObject,\n accounts: Hex[],\n) => {\n const updatedScopesObject: InternalScopesObject = {};\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 isWalletNamespace = scopeString === KnownCaipNamespace.Wallet;\n const { namespace, reference } = parseScopeString(scopeString);\n if (!isEip155ScopeString(scopeString) && !isWalletNamespace) {\n updatedScopesObject[scopeString] = scopeObject;\n return;\n }\n\n let caipAccounts: CaipAccountId[] = [];\n if (namespace && reference) {\n caipAccounts = accounts.map<CaipAccountId>(\n (account) => `${namespace}:${reference}:${account}`,\n );\n }\n\n updatedScopesObject[scopeString] = {\n ...scopeObject,\n accounts: caipAccounts,\n };\n });\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the Ethereum (EIP155 namespaced) accounts for the given CAIP-25 caveat value.\n * We set the same accounts for all the scopes that are EIP155 or Wallet namespaced because\n * we do not provide UI/UX flows for selecting different accounts across different chains.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the Ethereum accounts for.\n * @param accounts - The Ethereum accounts to set.\n * @returns The updated CAIP-25 caveat value with the Ethereum accounts set.\n */\nexport const setEthAccounts = (\n caip25CaveatValue: Caip25CaveatValue,\n accounts: Hex[],\n): Caip25CaveatValue => {\n return {\n ...caip25CaveatValue,\n requiredScopes: setEthAccountsForScopesObject(\n caip25CaveatValue.requiredScopes,\n accounts,\n ),\n optionalScopes: setEthAccountsForScopesObject(\n caip25CaveatValue.optionalScopes,\n accounts,\n ),\n };\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 accounts from an array of scopes objects\n * This extracts all account IDs from both required and optional scopes\n * and returns a unique set.\n *\n * @param scopesObjects - The scopes objects to extract accounts from\n * @returns Array of unique account IDs\n */\nexport function getCaipAccountIdsFromScopesObjects(\n scopesObjects: InternalScopesObject[],\n): CaipAccountId[] {\n const allAccounts = new Set<CaipAccountId>();\n\n for (const scopeObject of scopesObjects) {\n for (const { accounts } of Object.values(scopeObject)) {\n for (const account of accounts) {\n allAccounts.add(account);\n }\n }\n }\n\n return Array.from(allAccounts);\n}\n\n/**\n * Gets all permitted accounts from a CAIP-25 caveat\n * This extracts all account IDs from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to extract accounts from\n * @returns Array of unique account IDs\n */\nexport function getCaipAccountIdsFromCaip25CaveatValue(\n caip25CaveatValue: Caip25CaveatValue,\n): CaipAccountId[] {\n return getCaipAccountIdsFromScopesObjects([\n caip25CaveatValue.requiredScopes,\n caip25CaveatValue.optionalScopes,\n ]);\n}\n\n/**\n *\n * Setters\n *\n */\n\n/**\n * Sets the CAIP account IDs to scopes with matching namespaces in the given scopes object.\n * This function should not be used with Smart Contract Accounts (SCA) because\n * it adds the same account ID to all the scopes that have the same namespace.\n *\n * @param scopesObject - The scopes object to set the CAIP account IDs for.\n * @param accounts - The CAIP account IDs to add to the appropriate scopes.\n * @returns The updated scopes object with the CAIP account IDs set.\n */\nconst setNonSCACaipAccountIdsInScopesObject = (\n scopesObject: InternalScopesObject,\n accounts: CaipAccountId[],\n) => {\n const accountsByNamespace = new Map<string, Set<string>>();\n\n for (const account of accounts) {\n const {\n chain: { namespace },\n address,\n } = parseCaipAccountId(account);\n\n if (!accountsByNamespace.has(namespace)) {\n accountsByNamespace.set(namespace, new Set());\n }\n\n accountsByNamespace.get(namespace)?.add(address);\n }\n\n const updatedScopesObject: InternalScopesObject = {};\n\n for (const [scopeString, scopeObject] of Object.entries(scopesObject)) {\n const { namespace, reference } = parseScopeString(scopeString as string);\n\n let caipAccounts: CaipAccountId[] = [];\n\n if (namespace && reference && accountsByNamespace.has(namespace)) {\n const addressSet = accountsByNamespace.get(namespace);\n if (addressSet) {\n caipAccounts = Array.from(addressSet).map(\n (address) => `${namespace}:${reference}:${address}` as CaipAccountId,\n );\n }\n }\n\n updatedScopesObject[scopeString as keyof typeof scopesObject] = {\n ...scopeObject,\n accounts: getUniqueArrayItems(caipAccounts),\n };\n }\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the permitted accounts to scopes with matching namespaces in the given CAIP-25 caveat value.\n * This function should not be used with Smart Contract Accounts (SCA) because\n * it adds the same account ID to all scopes that have the same namespace as the account.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.\n * @param accounts - The permitted accounts to add to the appropriate scopes.\n * @returns The updated CAIP-25 caveat value with the permitted accounts set.\n */\nexport const setNonSCACaipAccountIdsInCaip25CaveatValue = (\n caip25CaveatValue: Caip25CaveatValue,\n accounts: CaipAccountId[],\n): Caip25CaveatValue => {\n return {\n ...caip25CaveatValue,\n requiredScopes: setNonSCACaipAccountIdsInScopesObject(\n caip25CaveatValue.requiredScopes,\n accounts,\n ),\n optionalScopes: setNonSCACaipAccountIdsInScopesObject(\n caip25CaveatValue.optionalScopes,\n accounts,\n ),\n };\n};\n\n/**\n * Checks if an address and list of parsed scopes are connected to any of\n * the permitted accounts based on scope matching\n *\n * @param address - The CAIP account address to check against permitted accounts\n * @param parsedAccountScopes - The list of parsed CAIP chain ID to check against permitted accounts\n * @param permittedAccounts - Array of CAIP account IDs that are permitted\n * @returns True if the address and any account scope is connected to any permitted account\n */\nfunction isAddressWithParsedScopesInPermittedAccountIds(\n address: CaipAccountAddress,\n parsedAccountScopes: {\n namespace?: CaipNamespace;\n reference?: CaipReference;\n }[],\n permittedAccounts: CaipAccountId[],\n) {\n if (!address || !parsedAccountScopes.length || !permittedAccounts.length) {\n return false;\n }\n\n return permittedAccounts.some((account) => {\n const parsedPermittedAccount = parseCaipAccountId(account);\n\n return parsedAccountScopes.some(({ namespace, reference }) => {\n if (\n namespace !== parsedPermittedAccount.chain.namespace &&\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n parsedPermittedAccount.chain.namespace !== KnownCaipNamespace.Wallet\n ) {\n return false;\n }\n\n // handle wallet:<namespace>:<address> case where namespaces are mismatched but addresses match\n // i.e. wallet:notSolana:12389812309123 and solana:0:12389812309123\n if (\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n parsedPermittedAccount.chain.namespace === KnownCaipNamespace.Wallet &&\n namespace !== parsedPermittedAccount.chain.reference\n ) {\n return false;\n }\n\n // handle eip155:0 case and insensitive evm address comparison\n if (namespace === KnownCaipNamespace.Eip155) {\n return (\n (reference === '0' ||\n reference === parsedPermittedAccount.chain.reference) &&\n isEqualCaseInsensitive(address, parsedPermittedAccount.address)\n );\n }\n\n // handle wallet:<namespace>:<address> case\n if (\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n parsedPermittedAccount.chain.namespace === KnownCaipNamespace.Wallet\n ) {\n return address === parsedPermittedAccount.address;\n }\n\n return (\n reference === parsedPermittedAccount.chain.reference &&\n address === parsedPermittedAccount.address\n );\n });\n });\n}\n\n/**\n * Checks if an internal account is connected to any of the permitted accounts\n * based on scope matching\n *\n * @param internalAccount - The internal account to check against permitted accounts\n * @param permittedAccounts - Array of CAIP account IDs that are permitted\n * @returns True if the account is connected to any permitted account\n */\nexport function isInternalAccountInPermittedAccountIds(\n internalAccount: InternalAccount,\n permittedAccounts: CaipAccountId[],\n): boolean {\n // temporary fix for the issue where the internal account has no scopes and or scopes is undefined\n // TODO: remove this once the bug is fixed (tracked here: https://github.com/MetaMask/accounts-planning/issues/941)\n // there is currently a bug where an account associated with a snap can fail to add scopes to the internal account in time\n // before we attempt to access this state\n if (!internalAccount?.scopes?.length) {\n return false;\n }\n\n const parsedInteralAccountScopes = internalAccount.scopes.map((scope) => {\n return parseScopeString(scope);\n });\n\n return isAddressWithParsedScopesInPermittedAccountIds(\n internalAccount.address,\n parsedInteralAccountScopes,\n permittedAccounts,\n );\n}\n\n/**\n * Checks if an CAIP account ID is connected to any of the permitted accounts\n * based on scope matching\n *\n * @param accountId - The CAIP account ID to check against permitted accounts\n * @param permittedAccounts - Array of CAIP account IDs that are permitted\n * @returns True if the account is connected to any permitted account\n */\nexport function isCaipAccountIdInPermittedAccountIds(\n accountId: CaipAccountId,\n permittedAccounts: CaipAccountId[],\n): boolean {\n const { address, chain } = parseCaipAccountId(accountId);\n\n return isAddressWithParsedScopesInPermittedAccountIds(\n address,\n [chain],\n permittedAccounts,\n );\n}\n"]}
@@ -268,4 +268,4 @@ const setChainIdsInCaip25CaveatValue = (caip25CaveatValue, chainIds) => {
268
268
  return result;
269
269
  };
270
270
  exports.setChainIdsInCaip25CaveatValue = setChainIdsInCaip25CaveatValue;
271
- //# sourceMappingURL=caip-permission-adapter-permittedChains.cjs.map
271
+ //# sourceMappingURL=caip-permission-operator-permittedChains.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-operator-permittedChains.cjs","sourceRoot":"","sources":["../../src/operators/caip-permission-operator-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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,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"]}
@@ -83,4 +83,4 @@ export declare const addCaipChainIdInCaip25CaveatValue: (caip25CaveatValue: Caip
83
83
  * @returns The updated CAIP-25 caveat value with the CAIP-2 chainIDs.
84
84
  */
85
85
  export declare const setChainIdsInCaip25CaveatValue: (caip25CaveatValue: Caip25CaveatValue, chainIds: CaipChainId[]) => Caip25CaveatValue;
86
- //# sourceMappingURL=caip-permission-adapter-permittedChains.d.cts.map
86
+ //# sourceMappingURL=caip-permission-operator-permittedChains.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-operator-permittedChains.d.cts","sourceRoot":"","sources":["../../src/operators/caip-permission-operator-permittedChains.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,WAAW,EAAE,aAAa,EAAE,wBAAwB;AAGvE,OAAO,EAAoB,KAAK,iBAAiB,EAAE,gCAA4B;AAE/E,OAAO,KAAK,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,2BAAuB;AA8BhF;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,sBACf,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC,oBAUF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,sBACd,iBAAiB,WAC3B,GAAG,KACX,iBAkBF,CAAC;AAuCF;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,sBACf,iBAAiB,YAC1B,GAAG,EAAE,KACd,iBAkBF,CAAC;AAgBF;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAC3C,aAAa,EAAE,oBAAoB,EAAE,GACpC,mBAAmB,EAAE,CAUvB;AAED;;;;;;;GAOG;AACH,wBAAgB,iCAAiC,CAC/C,iBAAiB,EAAE,iBAAiB,GACnC,WAAW,EAAE,CAKf;AAED;;;;;;;GAOG;AACH,wBAAgB,qCAAqC,CACnD,iBAAiB,EAAE,iBAAiB,GACnC,aAAa,EAAE,CAcjB;AAED;;;;;;;;GAQG;AACH,wBAAgB,0BAA0B,CAAC,gBAAgB,EAAE;IAC3D,OAAO,EAAE;QACP,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,iBAAiB,CAAC;KAC1B,EAAE,CAAC;CACL,GAAG,WAAW,EAAE,CAShB;AAQD;;;;;;;GAOG;AACH,eAAO,MAAM,iCAAiC,sBACzB,iBAAiB,WAC3B,WAAW,KACnB,iBAiBF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,8BAA8B,sBACtB,iBAAiB,YAC1B,WAAW,EAAE,KACtB,iBA8BF,CAAC"}
@@ -83,4 +83,4 @@ export declare const addCaipChainIdInCaip25CaveatValue: (caip25CaveatValue: Caip
83
83
  * @returns The updated CAIP-25 caveat value with the CAIP-2 chainIDs.
84
84
  */
85
85
  export declare const setChainIdsInCaip25CaveatValue: (caip25CaveatValue: Caip25CaveatValue, chainIds: CaipChainId[]) => Caip25CaveatValue;
86
- //# sourceMappingURL=caip-permission-adapter-permittedChains.d.mts.map
86
+ //# sourceMappingURL=caip-permission-operator-permittedChains.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-operator-permittedChains.d.mts","sourceRoot":"","sources":["../../src/operators/caip-permission-operator-permittedChains.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,WAAW,EAAE,aAAa,EAAE,wBAAwB;AAGvE,OAAO,EAAoB,KAAK,iBAAiB,EAAE,gCAA4B;AAE/E,OAAO,KAAK,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,2BAAuB;AA8BhF;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,sBACf,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC,oBAUF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,sBACd,iBAAiB,WAC3B,GAAG,KACX,iBAkBF,CAAC;AAuCF;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,sBACf,iBAAiB,YAC1B,GAAG,EAAE,KACd,iBAkBF,CAAC;AAgBF;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAC3C,aAAa,EAAE,oBAAoB,EAAE,GACpC,mBAAmB,EAAE,CAUvB;AAED;;;;;;;GAOG;AACH,wBAAgB,iCAAiC,CAC/C,iBAAiB,EAAE,iBAAiB,GACnC,WAAW,EAAE,CAKf;AAED;;;;;;;GAOG;AACH,wBAAgB,qCAAqC,CACnD,iBAAiB,EAAE,iBAAiB,GACnC,aAAa,EAAE,CAcjB;AAED;;;;;;;;GAQG;AACH,wBAAgB,0BAA0B,CAAC,gBAAgB,EAAE;IAC3D,OAAO,EAAE;QACP,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,iBAAiB,CAAC;KAC1B,EAAE,CAAC;CACL,GAAG,WAAW,EAAE,CAShB;AAQD;;;;;;;GAOG;AACH,eAAO,MAAM,iCAAiC,sBACzB,iBAAiB,WAC3B,WAAW,KACnB,iBAiBF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,8BAA8B,sBACtB,iBAAiB,YAC1B,WAAW,EAAE,KACtB,iBA8BF,CAAC"}
@@ -256,4 +256,4 @@ export const setChainIdsInCaip25CaveatValue = (caip25CaveatValue, chainIds) => {
256
256
  }
257
257
  return result;
258
258
  };
259
- //# sourceMappingURL=caip-permission-adapter-permittedChains.mjs.map
259
+ //# sourceMappingURL=caip-permission-operator-permittedChains.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-operator-permittedChains.mjs","sourceRoot":"","sources":["../../src/operators/caip-permission-operator-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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AC;IAEF,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,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, 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"]}
@@ -104,4 +104,4 @@ const getPermittedAccountsForScopes = (caip25CaveatValue, scopes) => {
104
104
  return [...new Set(scopeAccounts)];
105
105
  };
106
106
  exports.getPermittedAccountsForScopes = getPermittedAccountsForScopes;
107
- //# sourceMappingURL=caip-permission-adapter-session-scopes.cjs.map
107
+ //# sourceMappingURL=caip-permission-operator-session-scopes.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-operator-session-scopes.cjs","sourceRoot":"","sources":["../../src/operators/caip-permission-operator-session-scopes.ts"],"names":[],"mappings":";;;AAAA,2CAKyB;AAGzB,sDAK4B;AAC5B,sDAA2D;AAK3D,8CAAkD;AAElD;;;;;GAKG;AACI,MAAM,uBAAuB,GAAG,CACrC,sBAA8C,EAC9C,EAAE;IACF,MAAM,cAAc,GAAyB,EAAE,CAAC;IAEhD,MAAM,CAAC,OAAO,CAAC,sBAAsB,CAAC,CAAC,OAAO,CAC5C,CAAC,CAAC,YAAY,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,EAAE;QAC/B,MAAM,WAAW,GAAG,YAAmD,CAAC;QAExE,cAAc,CAAC,WAAW,CAAC,GAAG;YAC5B,QAAQ;SACT,CAAC;IACJ,CAAC,CACF,CAAC;IAEF,OAAO,cAAc,CAAC;AACxB,CAAC,CAAC;AAhBW,QAAA,uBAAuB,2BAgBlC;AAEF;;;;;;;G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AAM,gBAAgB,GAAG,CAC9B,iBAGC,EACD,EACE,yBAAyB,GAG1B,EACD,EAAE;IACF,OAAO,IAAA,iCAAqB,EAC1B,yBAAyB,CAAC,iBAAiB,CAAC,cAAc,EAAE;QAC1D,yBAAyB;KAC1B,CAAC,EACF,yBAAyB,CAAC,iBAAiB,CAAC,cAAc,EAAE;QAC1D,yBAAyB;KAC1B,CAAC,CACH,CAAC;AACJ,CAAC,CAAC;AAnBW,QAAA,gBAAgB,oBAmB3B;AAEF;;;;;;GAMG;AACI,MAAM,6BAA6B,GAAG,CAC3C,iBAGC,EACD,MAAqB,EACJ,EAAE;IACnB,MAAM,aAAa,GAAoB,EAAE,CAAC;IAE1C,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;QACvB,MAAM,aAAa,GAAG,iBAAiB,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;QAC9D,MAAM,aAAa,GAAG,iBAAiB,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;QAC9D,IAAI,aAAa,EAAE;YACjB,aAAa,CAAC,IAAI,CAAC,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;SAC/C;QAED,IAAI,aAAa,EAAE;YACjB,aAAa,CAAC,IAAI,CAAC,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;SAC/C;IACH,CAAC,CAAC,CAAC;IACH,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC;AACrC,CAAC,CAAC;AArBW,QAAA,6BAA6B,iCAqBxC","sourcesContent":["import {\n type CaipAccountId,\n type CaipChainId,\n isCaipChainId,\n KnownCaipNamespace,\n} from '@metamask/utils';\n\nimport type { Caip25CaveatValue } from '../caip25Permission';\nimport {\n KnownNotifications,\n KnownRpcMethods,\n KnownWalletNamespaceRpcMethods,\n KnownWalletRpcMethods,\n} from '../scope/constants';\nimport { mergeNormalizedScopes } from '../scope/transform';\nimport type {\n InternalScopesObject,\n NormalizedScopesObject,\n} from '../scope/types';\nimport { parseScopeString } from '../scope/types';\n\n/**\n * Converts an NormalizedScopesObject to a InternalScopesObject.\n *\n * @param normalizedScopesObject - The NormalizedScopesObject to convert.\n * @returns An InternalScopesObject.\n */\nexport const getInternalScopesObject = (\n normalizedScopesObject: NormalizedScopesObject,\n) => {\n const internalScopes: InternalScopesObject = {};\n\n Object.entries(normalizedScopesObject).forEach(\n ([_scopeString, { accounts }]) => {\n const scopeString = _scopeString as keyof typeof normalizedScopesObject;\n\n internalScopes[scopeString] = {\n accounts,\n };\n },\n );\n\n return internalScopes;\n};\n\n/**\n * Converts an InternalScopesObject to a NormalizedScopesObject.\n *\n * @param internalScopesObject - The InternalScopesObject to convert.\n * @param hooks - An object containing the following properties:\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @returns A NormalizedScopesObject.\n */\nconst getNormalizedScopesObject = (\n internalScopesObject: InternalScopesObject,\n {\n getNonEvmSupportedMethods,\n }: {\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n },\n) => {\n const normalizedScopes: NormalizedScopesObject = {};\n\n Object.entries(internalScopesObject).forEach(\n ([_scopeString, { accounts }]) => {\n const scopeString = _scopeString as keyof typeof internalScopesObject;\n const { namespace, reference } = parseScopeString(scopeString);\n let methods: string[] = [];\n let notifications: string[] = [];\n\n if (\n scopeString === KnownCaipNamespace.Wallet ||\n namespace === KnownCaipNamespace.Wallet\n ) {\n if (reference === KnownCaipNamespace.Eip155) {\n methods = KnownWalletNamespaceRpcMethods[reference];\n } else if (isCaipChainId(scopeString)) {\n methods = getNonEvmSupportedMethods(scopeString);\n } else {\n methods = KnownWalletRpcMethods;\n }\n } else if (namespace === KnownCaipNamespace.Eip155) {\n methods = KnownRpcMethods[namespace];\n notifications = KnownNotifications[namespace];\n } else {\n methods = getNonEvmSupportedMethods(scopeString);\n notifications = [];\n }\n\n normalizedScopes[scopeString] = {\n methods,\n notifications,\n accounts,\n };\n },\n );\n\n return normalizedScopes;\n};\n\n/**\n * Takes the scopes from an endowment:caip25 permission caveat value,\n * hydrates them with supported methods and notifications, and returns a NormalizedScopesObject.\n *\n * @param caip25CaveatValue - The CAIP-25 CaveatValue to convert.\n * @param hooks - An object containing the following properties:\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @returns A NormalizedScopesObject.\n */\nexport const getSessionScopes = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n {\n getNonEvmSupportedMethods,\n }: {\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n },\n) => {\n return mergeNormalizedScopes(\n getNormalizedScopesObject(caip25CaveatValue.requiredScopes, {\n getNonEvmSupportedMethods,\n }),\n getNormalizedScopesObject(caip25CaveatValue.optionalScopes, {\n getNonEvmSupportedMethods,\n }),\n );\n};\n\n/**\n * Get the permitted accounts for the given scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 CaveatValue to get the permitted accounts for\n * @param scopes - The scopes to get the permitted accounts for\n * @returns An array of permitted accounts\n */\nexport const getPermittedAccountsForScopes = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n scopes: CaipChainId[],\n): CaipAccountId[] => {\n const scopeAccounts: CaipAccountId[] = [];\n\n scopes.forEach((scope) => {\n const requiredScope = caip25CaveatValue.requiredScopes[scope];\n const optionalScope = caip25CaveatValue.optionalScopes[scope];\n if (requiredScope) {\n scopeAccounts.push(...requiredScope.accounts);\n }\n\n if (optionalScope) {\n scopeAccounts.push(...optionalScope.accounts);\n }\n });\n return [...new Set(scopeAccounts)];\n};\n"]}
@@ -28,4 +28,4 @@ export declare const getSessionScopes: (caip25CaveatValue: Pick<Caip25CaveatValu
28
28
  * @returns An array of permitted accounts
29
29
  */
30
30
  export declare const getPermittedAccountsForScopes: (caip25CaveatValue: Pick<Caip25CaveatValue, 'requiredScopes' | 'optionalScopes'>, scopes: CaipChainId[]) => CaipAccountId[];
31
- //# sourceMappingURL=caip-permission-adapter-session-scopes.d.cts.map
31
+ //# sourceMappingURL=caip-permission-operator-session-scopes.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-operator-session-scopes.d.cts","sourceRoot":"","sources":["../../src/operators/caip-permission-operator-session-scopes.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,WAAW,EAGjB,wBAAwB;AAEzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAQ7D,OAAO,KAAK,EACV,oBAAoB,EACpB,sBAAsB,EACvB,2BAAuB;AAGxB;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,2BACV,sBAAsB,yBAe/C,CAAC;AAyDF;;;;;;;;GAQG;AACH,eAAO,MAAM,gBAAgB,sBACR,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC;uCAIoC,WAAW,KAAK,MAAM,EAAE;4BAW9D,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,6BAA6B,sBACrB,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC,UACO,WAAW,EAAE,KACpB,aAAa,EAef,CAAC"}
@@ -28,4 +28,4 @@ export declare const getSessionScopes: (caip25CaveatValue: Pick<Caip25CaveatValu
28
28
  * @returns An array of permitted accounts
29
29
  */
30
30
  export declare const getPermittedAccountsForScopes: (caip25CaveatValue: Pick<Caip25CaveatValue, 'requiredScopes' | 'optionalScopes'>, scopes: CaipChainId[]) => CaipAccountId[];
31
- //# sourceMappingURL=caip-permission-adapter-session-scopes.d.mts.map
31
+ //# sourceMappingURL=caip-permission-operator-session-scopes.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-operator-session-scopes.d.mts","sourceRoot":"","sources":["../../src/operators/caip-permission-operator-session-scopes.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,WAAW,EAGjB,wBAAwB;AAEzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAQ7D,OAAO,KAAK,EACV,oBAAoB,EACpB,sBAAsB,EACvB,2BAAuB;AAGxB;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,2BACV,sBAAsB,yBAe/C,CAAC;AAyDF;;;;;;;;GAQG;AACH,eAAO,MAAM,gBAAgB,sBACR,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC;uCAIoC,WAAW,KAAK,MAAM,EAAE;4BAW9D,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,6BAA6B,sBACrB,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC,UACO,WAAW,EAAE,KACpB,aAAa,EAef,CAAC"}
@@ -98,4 +98,4 @@ export const getPermittedAccountsForScopes = (caip25CaveatValue, scopes) => {
98
98
  });
99
99
  return [...new Set(scopeAccounts)];
100
100
  };
101
- //# sourceMappingURL=caip-permission-adapter-session-scopes.mjs.map
101
+ //# sourceMappingURL=caip-permission-operator-session-scopes.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"caip-permission-operator-session-scopes.mjs","sourceRoot":"","sources":["../../src/operators/caip-permission-operator-session-scopes.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,aAAa,EACb,kBAAkB,EACnB,wBAAwB;AAGzB,OAAO,EACL,kBAAkB,EAClB,eAAe,EACf,8BAA8B,EAC9B,qBAAqB,EACtB,+BAA2B;AAC5B,OAAO,EAAE,qBAAqB,EAAE,+BAA2B;AAK3D,OAAO,EAAE,gBAAgB,EAAE,2BAAuB;AAElD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CACrC,sBAA8C,EAC9C,EAAE;IACF,MAAM,cAAc,GAAyB,EAAE,CAAC;IAEhD,MAAM,CAAC,OAAO,CAAC,sBAAsB,CAAC,CAAC,OAAO,CAC5C,CAAC,CAAC,YAAY,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,EAAE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CAAC,WAAW,CAAC,GAAG;YAC9B,OAAO;YACP,aAAa;YACb,QAAQ;SACT,CAAC;IACJ,CAAC,CACF,CAAC;IAEF,OAAO,gBAAgB,CAAC;AAC1B,CAAC,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAC9B,iBAGC,EACD,EACE,yBAAyB,GAG1B,EACD,EAAE;IACF,OAAO,qBAAqB,CAC1B,yBAAyB,CAAC,iBAAiB,CAAC,cAAc,EAAE;QAC1D,yBAAyB;KAC1B,CAAC,EACF,yBAAyB,CAAC,iBAAiB,CAAC,cAAc,EAAE;QAC1D,yBAAyB;KAC1B,CAAC,CACH,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,6BAA6B,GAAG,CAC3C,iBAGC,EACD,MAAqB,EACJ,EAAE;IACnB,MAAM,aAAa,GAAoB,EAAE,CAAC;IAE1C,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;QACvB,MAAM,aAAa,GAAG,iBAAiB,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;QAC9D,MAAM,aAAa,GAAG,iBAAiB,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;QAC9D,IAAI,aAAa,EAAE;YACjB,aAAa,CAAC,IAAI,CAAC,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;SAC/C;QAED,IAAI,aAAa,EAAE;YACjB,aAAa,CAAC,IAAI,CAAC,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;SAC/C;IACH,CAAC,CAAC,CAAC;IACH,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC;AACrC,CAAC,CAAC","sourcesContent":["import {\n type CaipAccountId,\n type CaipChainId,\n isCaipChainId,\n KnownCaipNamespace,\n} from '@metamask/utils';\n\nimport type { Caip25CaveatValue } from '../caip25Permission';\nimport {\n KnownNotifications,\n KnownRpcMethods,\n KnownWalletNamespaceRpcMethods,\n KnownWalletRpcMethods,\n} from '../scope/constants';\nimport { mergeNormalizedScopes } from '../scope/transform';\nimport type {\n InternalScopesObject,\n NormalizedScopesObject,\n} from '../scope/types';\nimport { parseScopeString } from '../scope/types';\n\n/**\n * Converts an NormalizedScopesObject to a InternalScopesObject.\n *\n * @param normalizedScopesObject - The NormalizedScopesObject to convert.\n * @returns An InternalScopesObject.\n */\nexport const getInternalScopesObject = (\n normalizedScopesObject: NormalizedScopesObject,\n) => {\n const internalScopes: InternalScopesObject = {};\n\n Object.entries(normalizedScopesObject).forEach(\n ([_scopeString, { accounts }]) => {\n const scopeString = _scopeString as keyof typeof normalizedScopesObject;\n\n internalScopes[scopeString] = {\n accounts,\n };\n },\n );\n\n return internalScopes;\n};\n\n/**\n * Converts an InternalScopesObject to a NormalizedScopesObject.\n *\n * @param internalScopesObject - The InternalScopesObject to convert.\n * @param hooks - An object containing the following properties:\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @returns A NormalizedScopesObject.\n */\nconst getNormalizedScopesObject = (\n internalScopesObject: InternalScopesObject,\n {\n getNonEvmSupportedMethods,\n }: {\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n },\n) => {\n const normalizedScopes: NormalizedScopesObject = {};\n\n Object.entries(internalScopesObject).forEach(\n ([_scopeString, { accounts }]) => {\n const scopeString = _scopeString as keyof typeof internalScopesObject;\n const { namespace, reference } = parseScopeString(scopeString);\n let methods: string[] = [];\n let notifications: string[] = [];\n\n if (\n scopeString === KnownCaipNamespace.Wallet ||\n namespace === KnownCaipNamespace.Wallet\n ) {\n if (reference === KnownCaipNamespace.Eip155) {\n methods = KnownWalletNamespaceRpcMethods[reference];\n } else if (isCaipChainId(scopeString)) {\n methods = getNonEvmSupportedMethods(scopeString);\n } else {\n methods = KnownWalletRpcMethods;\n }\n } else if (namespace === KnownCaipNamespace.Eip155) {\n methods = KnownRpcMethods[namespace];\n notifications = KnownNotifications[namespace];\n } else {\n methods = getNonEvmSupportedMethods(scopeString);\n notifications = [];\n }\n\n normalizedScopes[scopeString] = {\n methods,\n notifications,\n accounts,\n };\n },\n );\n\n return normalizedScopes;\n};\n\n/**\n * Takes the scopes from an endowment:caip25 permission caveat value,\n * hydrates them with supported methods and notifications, and returns a NormalizedScopesObject.\n *\n * @param caip25CaveatValue - The CAIP-25 CaveatValue to convert.\n * @param hooks - An object containing the following properties:\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @returns A NormalizedScopesObject.\n */\nexport const getSessionScopes = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n {\n getNonEvmSupportedMethods,\n }: {\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n },\n) => {\n return mergeNormalizedScopes(\n getNormalizedScopesObject(caip25CaveatValue.requiredScopes, {\n getNonEvmSupportedMethods,\n }),\n getNormalizedScopesObject(caip25CaveatValue.optionalScopes, {\n getNonEvmSupportedMethods,\n }),\n );\n};\n\n/**\n * Get the permitted accounts for the given scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 CaveatValue to get the permitted accounts for\n * @param scopes - The scopes to get the permitted accounts for\n * @returns An array of permitted accounts\n */\nexport const getPermittedAccountsForScopes = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n scopes: CaipChainId[],\n): CaipAccountId[] => {\n const scopeAccounts: CaipAccountId[] = [];\n\n scopes.forEach((scope) => {\n const requiredScope = caip25CaveatValue.requiredScopes[scope];\n const optionalScope = caip25CaveatValue.optionalScopes[scope];\n if (requiredScope) {\n scopeAccounts.push(...requiredScope.accounts);\n }\n\n if (optionalScope) {\n scopeAccounts.push(...optionalScope.accounts);\n }\n });\n return [...new Set(scopeAccounts)];\n};\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/chain-agnostic-permission",
3
- "version": "0.7.1-preview-3636a128",
3
+ "version": "0.8.0-preview-c6e5eb7",
4
4
  "description": "Defines a CAIP-25 based endowment permission and helpers for interfacing with it",
5
5
  "keywords": [
6
6
  "MetaMask",
@@ -49,7 +49,7 @@
49
49
  "dependencies": {
50
50
  "@metamask/api-specs": "^0.14.0",
51
51
  "@metamask/controller-utils": "^11.10.0",
52
- "@metamask/network-controller": "^23.6.0",
52
+ "@metamask/network-controller": "^24.0.0",
53
53
  "@metamask/permission-controller": "^11.0.6",
54
54
  "@metamask/rpc-errors": "^7.0.2",
55
55
  "@metamask/utils": "^11.2.0",
@@ -1 +0,0 @@
1
- {"version":3,"file":"caip-permission-adapter-accounts.d.cts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-accounts.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AACtE,OAAO,EAGL,KAAK,aAAa,EAGlB,KAAK,GAAG,EAGT,wBAAwB;AAEzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAG7D,OAAO,KAAK,EAAuB,oBAAoB,EAAE,2BAAuB;AAsDhF;;;;;GAKG;AACH,eAAO,MAAM,cAAc,sBACN,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC,KACA,GAAG,EASL,CAAC;AAwCF;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,sBACN,iBAAiB,YAC1B,GAAG,EAAE,KACd,iBAYF,CAAC;AAUF;;;;GAIG;AAEH;;;;;;;GAOG;AACH,wBAAgB,kCAAkC,CAChD,aAAa,EAAE,oBAAoB,EAAE,GACpC,aAAa,EAAE,CAYjB;AAED;;;;;;;GAOG;AACH,wBAAgB,sCAAsC,CACpD,iBAAiB,EAAE,iBAAiB,GACnC,aAAa,EAAE,CAKjB;AA6DD;;;;;;;;GAQG;AACH,eAAO,MAAM,0CAA0C,sBAClC,iBAAiB,YAC1B,aAAa,EAAE,KACxB,iBAYF,CAAC;AAsEF;;;;;;;GAOG;AACH,wBAAgB,sCAAsC,CACpD,eAAe,EAAE,eAAe,EAChC,iBAAiB,EAAE,aAAa,EAAE,GACjC,OAAO,CAUT;AAED;;;;;;;GAOG;AACH,wBAAgB,oCAAoC,CAClD,SAAS,EAAE,aAAa,EACxB,iBAAiB,EAAE,aAAa,EAAE,GACjC,OAAO,CAQT"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"caip-permission-adapter-accounts.d.mts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-accounts.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AACtE,OAAO,EAGL,KAAK,aAAa,EAGlB,KAAK,GAAG,EAGT,wBAAwB;AAEzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAG7D,OAAO,KAAK,EAAuB,oBAAoB,EAAE,2BAAuB;AAsDhF;;;;;GAKG;AACH,eAAO,MAAM,cAAc,sBACN,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC,KACA,GAAG,EASL,CAAC;AAwCF;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,sBACN,iBAAiB,YAC1B,GAAG,EAAE,KACd,iBAYF,CAAC;AAUF;;;;GAIG;AAEH;;;;;;;GAOG;AACH,wBAAgB,kCAAkC,CAChD,aAAa,EAAE,oBAAoB,EAAE,GACpC,aAAa,EAAE,CAYjB;AAED;;;;;;;GAOG;AACH,wBAAgB,sCAAsC,CACpD,iBAAiB,EAAE,iBAAiB,GACnC,aAAa,EAAE,CAKjB;AA6DD;;;;;;;;GAQG;AACH,eAAO,MAAM,0CAA0C,sBAClC,iBAAiB,YAC1B,aAAa,EAAE,KACxB,iBAYF,CAAC;AAsEF;;;;;;;GAOG;AACH,wBAAgB,sCAAsC,CACpD,eAAe,EAAE,eAAe,EAChC,iBAAiB,EAAE,aAAa,EAAE,GACjC,OAAO,CAUT;AAED;;;;;;;GAOG;AACH,wBAAgB,oCAAoC,CAClD,SAAS,EAAE,aAAa,EACxB,iBAAiB,EAAE,aAAa,EAAE,GACjC,OAAO,CAQT"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"caip-permission-adapter-accounts.mjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-accounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,mCAAmC;AAEpE,OAAO,EACL,uBAAuB,EAMvB,kBAAkB,EAClB,kBAAkB,EACnB,wBAAwB;AAGzB,OAAO,EAAE,sBAAsB,EAAE,+BAA2B;AAC5D,OAAO,EAAE,mBAAmB,EAAE,+BAA2B;AAEzD,OAAO,EAAE,gBAAgB,EAAE,2BAAuB;AAElD;;;;;;GAMG;AAEH;;;;;;GAMG;AACH,MAAM,mBAAmB,GAAG,CAAC,WAAgC,EAAE,EAAE;IAC/D,MAAM,EAAE,SAAS,EAAE,GAAG,gBAAgB,CAAC,WAAW,CAAC,CAAC;IAEpD,OAAO,CACL,SAAS,KAAK,kBAAkB,CAAC,MAAM;QACvC,sDAAsD;QACtD,wEAAwE;QACxE,WAAW,KAAK,sBAAsB,CAAC,MAAM,CAC9C,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,wBAAwB,GAAG,CAAC,MAA4B,EAAE,EAAE;IAChE,MAAM,WAAW,GAAU,EAAE,CAAC;IAE9B,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,EAAE;QACnD,QAAQ,EAAE,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC5B,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,kBAAkB,CAAC,OAAO,CAAC,CAAC;YAEzD,IAAI,mBAAmB,CAAC,OAAO,CAAC,EAAE;gBAChC,2DAA2D;gBAC3D,kCAAkC;gBAClC,uBAAuB,CAAC,OAAO,CAAC,CAAC;gBACjC,WAAW,CAAC,IAAI,CAAC,O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ACxE,sBAAsB,CAAC,KAAK,CAAC,SAAS,KAAK,kBAAkB,CAAC,MAAM,EACpE;gBACA,OAAO,OAAO,KAAK,sBAAsB,CAAC,OAAO,CAAC;aACnD;YAED,OAAO,CACL,SAAS,KAAK,sBAAsB,CAAC,KAAK,CAAC,SAAS;gBACpD,OAAO,KAAK,sBAAsB,CAAC,OAAO,CAC3C,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,sCAAsC,CACpD,eAAgC,EAChC,iBAAkC;IAElC,MAAM,0BAA0B,GAAG,eAAe,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;QACtE,OAAO,gBAAgB,CAAC,KAAK,CAAC,CAAC;IACjC,CAAC,CAAC,CAAC;IAEH,OAAO,8CAA8C,CACnD,eAAe,CAAC,OAAO,EACvB,0BAA0B,EAC1B,iBAAiB,CAClB,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,oCAAoC,CAClD,SAAwB,EACxB,iBAAkC;IAElC,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,kBAAkB,CAAC,SAAS,CAAC,CAAC;IAEzD,OAAO,8CAA8C,CACnD,OAAO,EACP,CAAC,KAAK,CAAC,EACP,iBAAiB,CAClB,CAAC;AACJ,CAAC","sourcesContent":["import { isEqualCaseInsensitive } from '@metamask/controller-utils';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport {\n assertIsStrictHexString,\n type CaipAccountAddress,\n type CaipAccountId,\n type CaipNamespace,\n type CaipReference,\n type Hex,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\n\nimport type { Caip25CaveatValue } from '../caip25Permission';\nimport { KnownWalletScopeString } from '../scope/constants';\nimport { getUniqueArrayItems } from '../scope/transform';\nimport type { InternalScopeString, InternalScopesObject } from '../scope/types';\nimport { parseScopeString } from '../scope/types';\n\n/*\n *\n *\n * EVM SPECIFIC GETTERS AND SETTERS\n *\n *\n */\n\n/**\n *\n * Checks if a scope string is either an EIP155 or wallet namespaced scope string.\n *\n * @param scopeString - The scope string to check.\n * @returns True if the scope string is an EIP155 or wallet namespaced scope string, false otherwise.\n */\nconst isEip155ScopeString = (scopeString: InternalScopeString) => {\n const { namespace } = parseScopeString(scopeString);\n\n return (\n namespace === KnownCaipNamespace.Eip155 ||\n // We are trying to discern the type of `scopeString`.\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n scopeString === KnownWalletScopeString.Eip155\n );\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) accounts from internal scopes.\n *\n * @param scopes - The internal scopes from which to get the Ethereum accounts.\n * @returns An array of Ethereum accounts.\n */\nconst getEthAccountsFromScopes = (scopes: InternalScopesObject) => {\n const ethAccounts: Hex[] = [];\n\n Object.entries(scopes).forEach(([_, { accounts }]) => {\n accounts?.forEach((account) => {\n const { address, chainId } = parseCaipAccountId(account);\n\n if (isEip155ScopeString(chainId)) {\n // This address should always be a valid Hex string because\n // it's an EIP155/Ethereum account\n assertIsStrictHexString(address);\n ethAccounts.push(address);\n }\n });\n });\n\n return ethAccounts;\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to get the Ethereum accounts from.\n * @returns An array of Ethereum accounts.\n */\nexport const getEthAccounts = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n): Hex[] => {\n const { requiredScopes, optionalScopes } = caip25CaveatValue;\n\n const ethAccounts: Hex[] = [\n ...getEthAccountsFromScopes(requiredScopes),\n ...getEthAccountsFromScopes(optionalScopes),\n ];\n\n return getUniqueArrayItems(ethAccounts);\n};\n\n/**\n * Sets the Ethereum (EIP155 namespaced) accounts for the given scopes object.\n *\n * @param scopesObject - The scopes object to set the Ethereum accounts for.\n * @param accounts - The Ethereum accounts to set.\n * @returns The updated scopes object with the Ethereum accounts set.\n */\nconst setEthAccountsForScopesObject = (\n scopesObject: InternalScopesObject,\n accounts: Hex[],\n) => {\n const updatedScopesObject: InternalScopesObject = {};\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 isWalletNamespace = scopeString === KnownCaipNamespace.Wallet;\n const { namespace, reference } = parseScopeString(scopeString);\n if (!isEip155ScopeString(scopeString) && !isWalletNamespace) {\n updatedScopesObject[scopeString] = scopeObject;\n return;\n }\n\n let caipAccounts: CaipAccountId[] = [];\n if (namespace && reference) {\n caipAccounts = accounts.map<CaipAccountId>(\n (account) => `${namespace}:${reference}:${account}`,\n );\n }\n\n updatedScopesObject[scopeString] = {\n ...scopeObject,\n accounts: caipAccounts,\n };\n });\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the Ethereum (EIP155 namespaced) accounts for the given CAIP-25 caveat value.\n * We set the same accounts for all the scopes that are EIP155 or Wallet namespaced because\n * we do not provide UI/UX flows for selecting different accounts across different chains.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the Ethereum accounts for.\n * @param accounts - The Ethereum accounts to set.\n * @returns The updated CAIP-25 caveat value with the Ethereum accounts set.\n */\nexport const setEthAccounts = (\n caip25CaveatValue: Caip25CaveatValue,\n accounts: Hex[],\n): Caip25CaveatValue => {\n return {\n ...caip25CaveatValue,\n requiredScopes: setEthAccountsForScopesObject(\n caip25CaveatValue.requiredScopes,\n accounts,\n ),\n optionalScopes: setEthAccountsForScopesObject(\n caip25CaveatValue.optionalScopes,\n accounts,\n ),\n };\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 accounts from an array of scopes objects\n * This extracts all account IDs from both required and optional scopes\n * and returns a unique set.\n *\n * @param scopesObjects - The scopes objects to extract accounts from\n * @returns Array of unique account IDs\n */\nexport function getCaipAccountIdsFromScopesObjects(\n scopesObjects: InternalScopesObject[],\n): CaipAccountId[] {\n const allAccounts = new Set<CaipAccountId>();\n\n for (const scopeObject of scopesObjects) {\n for (const { accounts } of Object.values(scopeObject)) {\n for (const account of accounts) {\n allAccounts.add(account);\n }\n }\n }\n\n return Array.from(allAccounts);\n}\n\n/**\n * Gets all permitted accounts from a CAIP-25 caveat\n * This extracts all account IDs from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to extract accounts from\n * @returns Array of unique account IDs\n */\nexport function getCaipAccountIdsFromCaip25CaveatValue(\n caip25CaveatValue: Caip25CaveatValue,\n): CaipAccountId[] {\n return getCaipAccountIdsFromScopesObjects([\n caip25CaveatValue.requiredScopes,\n caip25CaveatValue.optionalScopes,\n ]);\n}\n\n/**\n *\n * Setters\n *\n */\n\n/**\n * Sets the CAIP account IDs to scopes with matching namespaces in the given scopes object.\n * This function should not be used with Smart Contract Accounts (SCA) because\n * it adds the same account ID to all the scopes that have the same namespace.\n *\n * @param scopesObject - The scopes object to set the CAIP account IDs for.\n * @param accounts - The CAIP account IDs to add to the appropriate scopes.\n * @returns The updated scopes object with the CAIP account IDs set.\n */\nconst setNonSCACaipAccountIdsInScopesObject = (\n scopesObject: InternalScopesObject,\n accounts: CaipAccountId[],\n) => {\n const accountsByNamespace = new Map<string, Set<string>>();\n\n for (const account of accounts) {\n const {\n chain: { namespace },\n address,\n } = parseCaipAccountId(account);\n\n if (!accountsByNamespace.has(namespace)) {\n accountsByNamespace.set(namespace, new Set());\n }\n\n accountsByNamespace.get(namespace)?.add(address);\n }\n\n const updatedScopesObject: InternalScopesObject = {};\n\n for (const [scopeString, scopeObject] of Object.entries(scopesObject)) {\n const { namespace, reference } = parseScopeString(scopeString as string);\n\n let caipAccounts: CaipAccountId[] = [];\n\n if (namespace && reference && accountsByNamespace.has(namespace)) {\n const addressSet = accountsByNamespace.get(namespace);\n if (addressSet) {\n caipAccounts = Array.from(addressSet).map(\n (address) => `${namespace}:${reference}:${address}` as CaipAccountId,\n );\n }\n }\n\n updatedScopesObject[scopeString as keyof typeof scopesObject] = {\n ...scopeObject,\n accounts: getUniqueArrayItems(caipAccounts),\n };\n }\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the permitted accounts to scopes with matching namespaces in the given CAIP-25 caveat value.\n * This function should not be used with Smart Contract Accounts (SCA) because\n * it adds the same account ID to all scopes that have the same namespace as the account.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.\n * @param accounts - The permitted accounts to add to the appropriate scopes.\n * @returns The updated CAIP-25 caveat value with the permitted accounts set.\n */\nexport const setNonSCACaipAccountIdsInCaip25CaveatValue = (\n caip25CaveatValue: Caip25CaveatValue,\n accounts: CaipAccountId[],\n): Caip25CaveatValue => {\n return {\n ...caip25CaveatValue,\n requiredScopes: setNonSCACaipAccountIdsInScopesObject(\n caip25CaveatValue.requiredScopes,\n accounts,\n ),\n optionalScopes: setNonSCACaipAccountIdsInScopesObject(\n caip25CaveatValue.optionalScopes,\n accounts,\n ),\n };\n};\n\n/**\n * Checks if an address and list of parsed scopes are connected to any of\n * the permitted accounts based on scope matching\n *\n * @param address - The CAIP account address to check against permitted accounts\n * @param parsedAccountScopes - The list of parsed CAIP chain ID to check against permitted accounts\n * @param permittedAccounts - Array of CAIP account IDs that are permitted\n * @returns True if the address and any account scope is connected to any permitted account\n */\nfunction isAddressWithParsedScopesInPermittedAccountIds(\n address: CaipAccountAddress,\n parsedAccountScopes: {\n namespace?: CaipNamespace;\n reference?: CaipReference;\n }[],\n permittedAccounts: CaipAccountId[],\n) {\n if (!address || !parsedAccountScopes.length || !permittedAccounts.length) {\n return false;\n }\n\n return permittedAccounts.some((account) => {\n const parsedPermittedAccount = parseCaipAccountId(account);\n\n return parsedAccountScopes.some(({ namespace, reference }) => {\n if (\n namespace !== parsedPermittedAccount.chain.namespace &&\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n parsedPermittedAccount.chain.namespace !== KnownCaipNamespace.Wallet\n ) {\n return false;\n }\n\n // handle wallet:<namespace>:<address> case where namespaces are mismatched but addresses match\n // i.e. wallet:notSolana:12389812309123 and solana:0:12389812309123\n if (\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n parsedPermittedAccount.chain.namespace === KnownCaipNamespace.Wallet &&\n namespace !== parsedPermittedAccount.chain.reference\n ) {\n return false;\n }\n\n // handle eip155:0 case and insensitive evm address comparison\n if (namespace === KnownCaipNamespace.Eip155) {\n return (\n (reference === '0' ||\n reference === parsedPermittedAccount.chain.reference) &&\n isEqualCaseInsensitive(address, parsedPermittedAccount.address)\n );\n }\n\n // handle wallet:<namespace>:<address> case\n if (\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n parsedPermittedAccount.chain.namespace === KnownCaipNamespace.Wallet\n ) {\n return address === parsedPermittedAccount.address;\n }\n\n return (\n reference === parsedPermittedAccount.chain.reference &&\n address === parsedPermittedAccount.address\n );\n });\n });\n}\n\n/**\n * Checks if an internal account is connected to any of the permitted accounts\n * based on scope matching\n *\n * @param internalAccount - The internal account to check against permitted accounts\n * @param permittedAccounts - Array of CAIP account IDs that are permitted\n * @returns True if the account is connected to any permitted account\n */\nexport function isInternalAccountInPermittedAccountIds(\n internalAccount: InternalAccount,\n permittedAccounts: CaipAccountId[],\n): boolean {\n const parsedInteralAccountScopes = internalAccount.scopes.map((scope) => {\n return parseScopeString(scope);\n });\n\n return isAddressWithParsedScopesInPermittedAccountIds(\n internalAccount.address,\n parsedInteralAccountScopes,\n permittedAccounts,\n );\n}\n\n/**\n * Checks if an CAIP account ID is connected to any of the permitted accounts\n * based on scope matching\n *\n * @param accountId - The CAIP account ID to check against permitted accounts\n * @param permittedAccounts - Array of CAIP account IDs that are permitted\n * @returns True if the account is connected to any permitted account\n */\nexport function isCaipAccountIdInPermittedAccountIds(\n accountId: CaipAccountId,\n permittedAccounts: CaipAccountId[],\n): boolean {\n const { address, chain } = parseCaipAccountId(accountId);\n\n return isAddressWithParsedScopesInPermittedAccountIds(\n address,\n [chain],\n permittedAccounts,\n );\n}\n"]}
@@ -1 +0,0 @@
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,2BAclC;AAEF;;;;;;;GAOG;AACI,MAAM,sBAAsB,GAAG,CACpC,iBAAoC,EACpC,OAAY,EACO,EAAE;IACrB,MAAM,WAAW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AQ,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"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"caip-permission-adapter-permittedChains.d.cts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-permittedChains.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,WAAW,EAAE,aAAa,EAAE,wBAAwB;AAGvE,OAAO,EAAoB,KAAK,iBAAiB,EAAE,gCAA4B;AAE/E,OAAO,KAAK,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,2BAAuB;AA8BhF;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,sBACf,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC,oBAUF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,sBACd,iBAAiB,WAC3B,GAAG,KACX,iBAkBF,CAAC;AAuCF;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,sBACf,iBAAiB,YAC1B,GAAG,EAAE,KACd,iBAkBF,CAAC;AAgBF;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAC3C,aAAa,EAAE,oBAAoB,EAAE,GACpC,mBAAmB,EAAE,CAUvB;AAED;;;;;;;GAOG;AACH,wBAAgB,iCAAiC,CAC/C,iBAAiB,EAAE,iBAAiB,GACnC,WAAW,EAAE,CAKf;AAED;;;;;;;GAOG;AACH,wBAAgB,qCAAqC,CACnD,iBAAiB,EAAE,iBAAiB,GACnC,aAAa,EAAE,CAcjB;AAED;;;;;;;;GAQG;AACH,wBAAgB,0BAA0B,CAAC,gBAAgB,EAAE;IAC3D,OAAO,EAAE;QACP,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,iBAAiB,CAAC;KAC1B,EAAE,CAAC;CACL,GAAG,WAAW,EAAE,CAShB;AAQD;;;;;;;GAOG;AACH,eAAO,MAAM,iCAAiC,sBACzB,iBAAiB,WAC3B,WAAW,KACnB,iBAiBF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,8BAA8B,sBACtB,iBAAiB,YAC1B,WAAW,EAAE,KACtB,iBA8BF,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"caip-permission-adapter-permittedChains.d.mts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-permittedChains.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,WAAW,EAAE,aAAa,EAAE,wBAAwB;AAGvE,OAAO,EAAoB,KAAK,iBAAiB,EAAE,gCAA4B;AAE/E,OAAO,KAAK,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,2BAAuB;AA8BhF;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,sBACf,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC,oBAUF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,sBACd,iBAAiB,WAC3B,GAAG,KACX,iBAkBF,CAAC;AAuCF;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,sBACf,iBAAiB,YAC1B,GAAG,EAAE,KACd,iBAkBF,CAAC;AAgBF;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAC3C,aAAa,EAAE,oBAAoB,EAAE,GACpC,mBAAmB,EAAE,CAUvB;AAED;;;;;;;GAOG;AACH,wBAAgB,iCAAiC,CAC/C,iBAAiB,EAAE,iBAAiB,GACnC,WAAW,EAAE,CAKf;AAED;;;;;;;GAOG;AACH,wBAAgB,qCAAqC,CACnD,iBAAiB,EAAE,iBAAiB,GACnC,aAAa,EAAE,CAcjB;AAED;;;;;;;;GAQG;AACH,wBAAgB,0BAA0B,CAAC,gBAAgB,EAAE;IAC3D,OAAO,EAAE;QACP,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,iBAAiB,CAAC;KAC1B,EAAE,CAAC;CACL,GAAG,WAAW,EAAE,CAShB;AAQD;;;;;;;GAOG;AACH,eAAO,MAAM,iCAAiC,sBACzB,iBAAiB,WAC3B,WAAW,KACnB,iBAiBF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,8BAA8B,sBACtB,iBAAiB,YAC1B,WAAW,EAAE,KACtB,iBA8BF,CAAC"}
@@ -1 +0,0 @@
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,E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,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,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, 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"]}
@@ -1 +0,0 @@
1
- {"version":3,"file":"caip-permission-adapter-session-scopes.cjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-session-scopes.ts"],"names":[],"mappings":";;;AAAA,2CAKyB;AAGzB,sDAK4B;AAC5B,sDAA2D;AAK3D,8CAAkD;AAElD;;;;;GAKG;AACI,MAAM,uBAAuB,GAAG,CACrC,sBAA8C,EAC9C,EAAE;IACF,MAAM,cAAc,GAAyB,EAAE,CAAC;IAEhD,MAAM,CAAC,OAAO,CAAC,sBAAsB,CAAC,CAAC,OAAO,CAC5C,CAAC,CAAC,YAAY,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,EAAE;QAC/B,MAAM,WAAW,GAAG,YAAmD,CAAC;QAExE,cAAc,CAAC,WAAW,CAAC,GAAG;YAC5B,QAAQ;SACT,CAAC;IACJ,CAAC,CACF,CAAC;IAEF,OAAO,cAAc,CAAC;AACxB,CAAC,CAAC;AAhBW,QAAA,uBAAuB,2BAgBlC;AAEF;;;;;;;GAOG;AACH,MAAM,yBAAyB,GAAG,CAChC,oBAA0C,EAC1C,EACE,yBAAyB,GAG1B,EACD,EAAE;IACF,MAAM,gBAAgB,GAA2B,EAAE,CAAC;IAEpD,MAAM,CAAC,OAAO,CAAC,oBAAoB,CAAC,CAAC,OAAO,CAC1C,CAAC,CAAC,YAAY,EAAE,EAAE,QAAQ,EAAE,CAAC,E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1B,CAAC,CACH,CAAC;AACJ,CAAC,CAAC;AAnBW,QAAA,gBAAgB,oBAmB3B;AAEF;;;;;;GAMG;AACI,MAAM,6BAA6B,GAAG,CAC3C,iBAGC,EACD,MAAqB,EACJ,EAAE;IACnB,MAAM,aAAa,GAAoB,EAAE,CAAC;IAE1C,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;QACvB,MAAM,aAAa,GAAG,iBAAiB,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;QAC9D,MAAM,aAAa,GAAG,iBAAiB,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;QAC9D,IAAI,aAAa,EAAE;YACjB,aAAa,CAAC,IAAI,CAAC,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;SAC/C;QAED,IAAI,aAAa,EAAE;YACjB,aAAa,CAAC,IAAI,CAAC,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAC;SAC/C;IACH,CAAC,CAAC,CAAC;IACH,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC;AACrC,CAAC,CAAC;AArBW,QAAA,6BAA6B,iCAqBxC","sourcesContent":["import {\n type CaipAccountId,\n type CaipChainId,\n isCaipChainId,\n KnownCaipNamespace,\n} from '@metamask/utils';\n\nimport type { Caip25CaveatValue } from '../caip25Permission';\nimport {\n KnownNotifications,\n KnownRpcMethods,\n KnownWalletNamespaceRpcMethods,\n KnownWalletRpcMethods,\n} from '../scope/constants';\nimport { mergeNormalizedScopes } from '../scope/transform';\nimport type {\n InternalScopesObject,\n NormalizedScopesObject,\n} from '../scope/types';\nimport { parseScopeString } from '../scope/types';\n\n/**\n * Converts an NormalizedScopesObject to a InternalScopesObject.\n *\n * @param normalizedScopesObject - The NormalizedScopesObject to convert.\n * @returns An InternalScopesObject.\n */\nexport const getInternalScopesObject = (\n normalizedScopesObject: NormalizedScopesObject,\n) => {\n const internalScopes: InternalScopesObject = {};\n\n Object.entries(normalizedScopesObject).forEach(\n ([_scopeString, { accounts }]) => {\n const scopeString = _scopeString as keyof typeof normalizedScopesObject;\n\n internalScopes[scopeString] = {\n accounts,\n };\n },\n );\n\n return internalScopes;\n};\n\n/**\n * Converts an InternalScopesObject to a NormalizedScopesObject.\n *\n * @param internalScopesObject - The InternalScopesObject to convert.\n * @param hooks - An object containing the following properties:\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @returns A NormalizedScopesObject.\n */\nconst getNormalizedScopesObject = (\n internalScopesObject: InternalScopesObject,\n {\n getNonEvmSupportedMethods,\n }: {\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n },\n) => {\n const normalizedScopes: NormalizedScopesObject = {};\n\n Object.entries(internalScopesObject).forEach(\n ([_scopeString, { accounts }]) => {\n const scopeString = _scopeString as keyof typeof internalScopesObject;\n const { namespace, reference } = parseScopeString(scopeString);\n let methods: string[] = [];\n let notifications: string[] = [];\n\n if (\n scopeString === KnownCaipNamespace.Wallet ||\n namespace === KnownCaipNamespace.Wallet\n ) {\n if (reference === KnownCaipNamespace.Eip155) {\n methods = KnownWalletNamespaceRpcMethods[reference];\n } else if (isCaipChainId(scopeString)) {\n methods = getNonEvmSupportedMethods(scopeString);\n } else {\n methods = KnownWalletRpcMethods;\n }\n } else if (namespace === KnownCaipNamespace.Eip155) {\n methods = KnownRpcMethods[namespace];\n notifications = KnownNotifications[namespace];\n } else {\n methods = getNonEvmSupportedMethods(scopeString);\n notifications = [];\n }\n\n normalizedScopes[scopeString] = {\n methods,\n notifications,\n accounts,\n };\n },\n );\n\n return normalizedScopes;\n};\n\n/**\n * Takes the scopes from an endowment:caip25 permission caveat value,\n * hydrates them with supported methods and notifications, and returns a NormalizedScopesObject.\n *\n * @param caip25CaveatValue - The CAIP-25 CaveatValue to convert.\n * @param hooks - An object containing the following properties:\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @returns A NormalizedScopesObject.\n */\nexport const getSessionScopes = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n {\n getNonEvmSupportedMethods,\n }: {\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n },\n) => {\n return mergeNormalizedScopes(\n getNormalizedScopesObject(caip25CaveatValue.requiredScopes, {\n getNonEvmSupportedMethods,\n }),\n getNormalizedScopesObject(caip25CaveatValue.optionalScopes, {\n getNonEvmSupportedMethods,\n }),\n );\n};\n\n/**\n * Get the permitted accounts for the given scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 CaveatValue to get the permitted accounts for\n * @param scopes - The scopes to get the permitted accounts for\n * @returns An array of permitted accounts\n */\nexport const getPermittedAccountsForScopes = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n scopes: CaipChainId[],\n): CaipAccountId[] => {\n const scopeAccounts: CaipAccountId[] = [];\n\n scopes.forEach((scope) => {\n const requiredScope = caip25CaveatValue.requiredScopes[scope];\n const optionalScope = caip25CaveatValue.optionalScopes[scope];\n if (requiredScope) {\n scopeAccounts.push(...requiredScope.accounts);\n }\n\n if (optionalScope) {\n scopeAccounts.push(...optionalScope.accounts);\n }\n });\n return [...new Set(scopeAccounts)];\n};\n"]}