@metamask-previews/chain-agnostic-permission 0.7.0-preview-7a575715 → 0.7.1-preview-d327019

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -7,12 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.7.1]
11
+
10
12
  ### Changed
11
13
 
14
+ - Bump `@metamask/keyring-internal-api` to `^6.2.0` ([#5871](https://github.com/MetaMask/core/pull/5871))
12
15
  - Bump `@metamask/controller-utils` to `^11.10.0` ([#5935](https://github.com/MetaMask/core/pull/5935))
13
16
  - Bump `@metamask/network-controller` to `^23.6.0` ([#5935](https://github.com/MetaMask/core/pull/5935),[#5882](https://github.com/MetaMask/core/pull/5882))
14
17
  - Change `caip25CaveatBuilder` to list unsupported scopes in the unsupported scopes error ([#5806](https://github.com/MetaMask/core/pull/5806))
15
18
 
19
+ ### Fixed
20
+
21
+ - Fix `isInternalAccountInPermittedAccountIds` and `isCaipAccountIdInPermittedAccountIds` to correctly handle comparison against `permittedAccounts` values of the `wallet:<namespace>:<address>` format ([#5980](https://github.com/MetaMask/core/pull/5980))
22
+
16
23
  ## [0.7.0]
17
24
 
18
25
  ### Changed
@@ -90,7 +97,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
90
97
 
91
98
  - Initial release
92
99
 
93
- [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.7.0...HEAD
100
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.7.1...HEAD
101
+ [0.7.1]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.7.0...@metamask/chain-agnostic-permission@0.7.1
94
102
  [0.7.0]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.6.0...@metamask/chain-agnostic-permission@0.7.0
95
103
  [0.6.0]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.5.0...@metamask/chain-agnostic-permission@0.6.0
96
104
  [0.5.0]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.4.0...@metamask/chain-agnostic-permission@0.5.0
@@ -229,16 +229,31 @@ function isAddressWithParsedScopesInPermittedAccountIds(address, parsedAccountSc
229
229
  return permittedAccounts.some((account) => {
230
230
  const parsedPermittedAccount = (0, utils_1.parseCaipAccountId)(account);
231
231
  return parsedAccountScopes.some(({ namespace, reference }) => {
232
- if (namespace !== parsedPermittedAccount.chain.namespace) {
232
+ if (namespace !== parsedPermittedAccount.chain.namespace &&
233
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
234
+ parsedPermittedAccount.chain.namespace !== utils_1.KnownCaipNamespace.Wallet) {
233
235
  return false;
234
236
  }
235
- // handle eip155:0 case and insensitive evm address comparison
237
+ // handle wallet:<namespace>:<address> case where namespaces are mismatched but addresses match
238
+ // i.e. wallet:notSolana:12389812309123 and solana:0:12389812309123
239
+ if (
236
240
  // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
241
+ parsedPermittedAccount.chain.namespace === utils_1.KnownCaipNamespace.Wallet &&
242
+ namespace !== parsedPermittedAccount.chain.reference) {
243
+ return false;
244
+ }
245
+ // handle eip155:0 case and insensitive evm address comparison
237
246
  if (namespace === utils_1.KnownCaipNamespace.Eip155) {
238
247
  return ((reference === '0' ||
239
248
  reference === parsedPermittedAccount.chain.reference) &&
240
249
  (0, controller_utils_1.isEqualCaseInsensitive)(address, parsedPermittedAccount.address));
241
250
  }
251
+ // handle wallet:<namespace>:<address> case
252
+ if (
253
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
254
+ parsedPermittedAccount.chain.namespace === utils_1.KnownCaipNamespace.Wallet) {
255
+ return address === parsedPermittedAccount.address;
256
+ }
242
257
  return (reference === parsedPermittedAccount.chain.reference &&
243
258
  address === parsedPermittedAccount.address);
244
259
  });
@@ -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,cAAc,EAAE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,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 (namespace !== parsedPermittedAccount.chain.namespace) {\n return false;\n }\n\n // handle eip155:0 case and insensitive evm address comparison\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n if (namespace === KnownCaipNamespace.Eip155) {\n return (\n (reference === '0' ||\n reference === parsedPermittedAccount.chain.reference) &&\n isEqualCaseInsensitive(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-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,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import 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 +1 @@
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;AAgDF;;;;;;;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
+ {"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 +1 @@
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;AAgDF;;;;;;;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
+ {"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"}
@@ -221,16 +221,31 @@ function isAddressWithParsedScopesInPermittedAccountIds(address, parsedAccountSc
221
221
  return permittedAccounts.some((account) => {
222
222
  const parsedPermittedAccount = parseCaipAccountId(account);
223
223
  return parsedAccountScopes.some(({ namespace, reference }) => {
224
- if (namespace !== parsedPermittedAccount.chain.namespace) {
224
+ if (namespace !== parsedPermittedAccount.chain.namespace &&
225
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
226
+ parsedPermittedAccount.chain.namespace !== KnownCaipNamespace.Wallet) {
225
227
  return false;
226
228
  }
227
- // handle eip155:0 case and insensitive evm address comparison
229
+ // handle wallet:<namespace>:<address> case where namespaces are mismatched but addresses match
230
+ // i.e. wallet:notSolana:12389812309123 and solana:0:12389812309123
231
+ if (
228
232
  // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
233
+ parsedPermittedAccount.chain.namespace === KnownCaipNamespace.Wallet &&
234
+ namespace !== parsedPermittedAccount.chain.reference) {
235
+ return false;
236
+ }
237
+ // handle eip155:0 case and insensitive evm address comparison
229
238
  if (namespace === KnownCaipNamespace.Eip155) {
230
239
  return ((reference === '0' ||
231
240
  reference === parsedPermittedAccount.chain.reference) &&
232
241
  isEqualCaseInsensitive(address, parsedPermittedAccount.address));
233
242
  }
243
+ // handle wallet:<namespace>:<address> case
244
+ if (
245
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
246
+ parsedPermittedAccount.chain.namespace === KnownCaipNamespace.Wallet) {
247
+ return address === parsedPermittedAccount.address;
248
+ }
234
249
  return (reference === parsedPermittedAccount.chain.reference &&
235
250
  address === parsedPermittedAccount.address);
236
251
  });
@@ -1 +1 @@
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,EAA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AAC,IAAI,CAAC,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,EAAE,EAAE;YAC3D,IAAI,SAAS,KAAK,sBAAsB,CAAC,KAAK,CAAC,SAAS,EAAE;gBACxD,OAAO,KAAK,CAAC;aACd;YAED,8DAA8D;YAC9D,wEAAwE;YACxE,IAAI,SAAS,KAAK,kBAAkB,CAAC,MAAM,EAAE;gBAC3C,OAAO,CACL,CAAC,SAAS,KAAK,GAAG;oBAChB,SAAS,KAAK,sBAAsB,CAAC,KAAK,CAAC,SAAS,CAAC;oBACvD,sBAAsB,CAAC,OAAO,EAAE,sBAAsB,CAAC,OAAO,CAAC,CAChE,CAAC;aACH;YACD,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 (namespace !== parsedPermittedAccount.chain.namespace) {\n return false;\n }\n\n // handle eip155:0 case and insensitive evm address comparison\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n if (namespace === KnownCaipNamespace.Eip155) {\n return (\n (reference === '0' ||\n reference === parsedPermittedAccount.chain.reference) &&\n isEqualCaseInsensitive(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-adapter-accounts.m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AO,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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/chain-agnostic-permission",
3
- "version": "0.7.0-preview-7a575715",
3
+ "version": "0.7.1-preview-d327019",
4
4
  "description": "Defines a CAIP-25 based endowment permission and helpers for interfacing with it",
5
5
  "keywords": [
6
6
  "MetaMask",