@metamask-previews/chain-agnostic-permission 0.4.0-preview-4bcedc0 → 0.4.0-preview-7f6d081

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 (66) hide show
  1. package/CHANGELOG.md +15 -5
  2. package/dist/adapters/caip-permission-adapter-accounts.cjs +150 -26
  3. package/dist/adapters/caip-permission-adapter-accounts.cjs.map +1 -1
  4. package/dist/adapters/caip-permission-adapter-accounts.d.cts +46 -1
  5. package/dist/adapters/caip-permission-adapter-accounts.d.cts.map +1 -1
  6. package/dist/adapters/caip-permission-adapter-accounts.d.mts +46 -1
  7. package/dist/adapters/caip-permission-adapter-accounts.d.mts.map +1 -1
  8. package/dist/adapters/caip-permission-adapter-accounts.mjs +144 -24
  9. package/dist/adapters/caip-permission-adapter-accounts.mjs.map +1 -1
  10. package/dist/adapters/caip-permission-adapter-permittedChains.cjs +127 -34
  11. package/dist/adapters/caip-permission-adapter-permittedChains.cjs.map +1 -1
  12. package/dist/adapters/caip-permission-adapter-permittedChains.d.cts +49 -7
  13. package/dist/adapters/caip-permission-adapter-permittedChains.d.cts.map +1 -1
  14. package/dist/adapters/caip-permission-adapter-permittedChains.d.mts +49 -7
  15. package/dist/adapters/caip-permission-adapter-permittedChains.d.mts.map +1 -1
  16. package/dist/adapters/caip-permission-adapter-permittedChains.mjs +120 -31
  17. package/dist/adapters/caip-permission-adapter-permittedChains.mjs.map +1 -1
  18. package/dist/caip25Permission.cjs +14 -3
  19. package/dist/caip25Permission.cjs.map +1 -1
  20. package/dist/caip25Permission.d.cts +19 -0
  21. package/dist/caip25Permission.d.cts.map +1 -1
  22. package/dist/caip25Permission.d.mts +19 -0
  23. package/dist/caip25Permission.d.mts.map +1 -1
  24. package/dist/caip25Permission.mjs +14 -4
  25. package/dist/caip25Permission.mjs.map +1 -1
  26. package/dist/index.cjs +15 -8
  27. package/dist/index.cjs.map +1 -1
  28. package/dist/index.d.cts +6 -7
  29. package/dist/index.d.cts.map +1 -1
  30. package/dist/index.d.mts +6 -7
  31. package/dist/index.d.mts.map +1 -1
  32. package/dist/index.mjs +6 -7
  33. package/dist/index.mjs.map +1 -1
  34. package/dist/scope/authorization.cjs +16 -1
  35. package/dist/scope/authorization.cjs.map +1 -1
  36. package/dist/scope/authorization.d.cts +9 -1
  37. package/dist/scope/authorization.d.cts.map +1 -1
  38. package/dist/scope/authorization.d.mts +9 -1
  39. package/dist/scope/authorization.d.mts.map +1 -1
  40. package/dist/scope/authorization.mjs +14 -0
  41. package/dist/scope/authorization.mjs.map +1 -1
  42. package/dist/scope/constants.cjs +11 -1
  43. package/dist/scope/constants.cjs.map +1 -1
  44. package/dist/scope/constants.d.cts +7 -0
  45. package/dist/scope/constants.d.cts.map +1 -1
  46. package/dist/scope/constants.d.mts +7 -0
  47. package/dist/scope/constants.d.mts.map +1 -1
  48. package/dist/scope/constants.mjs +9 -0
  49. package/dist/scope/constants.mjs.map +1 -1
  50. package/dist/scope/filter.cjs +1 -29
  51. package/dist/scope/filter.cjs.map +1 -1
  52. package/dist/scope/filter.d.cts +2 -18
  53. package/dist/scope/filter.d.cts.map +1 -1
  54. package/dist/scope/filter.d.mts +2 -18
  55. package/dist/scope/filter.d.mts.map +1 -1
  56. package/dist/scope/filter.mjs +0 -26
  57. package/dist/scope/filter.mjs.map +1 -1
  58. package/dist/scope/validation.cjs +1 -12
  59. package/dist/scope/validation.cjs.map +1 -1
  60. package/dist/scope/validation.d.cts +0 -8
  61. package/dist/scope/validation.d.cts.map +1 -1
  62. package/dist/scope/validation.d.mts +0 -8
  63. package/dist/scope/validation.d.mts.map +1 -1
  64. package/dist/scope/validation.mjs +0 -10
  65. package/dist/scope/validation.mjs.map +1 -1
  66. package/package.json +2 -1
package/CHANGELOG.md CHANGED
@@ -7,6 +7,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ### Added
11
+
12
+ - Added `getCaipAccountIdsFromCaip25CaveatValue`, `isInternalAccountInPermittedAccountIds`, and `isCaipAccountIdInPermittedAccountIds` account id functions. ([#5609](https://github.com/MetaMask/core/pull/5609))
13
+ - Added `getAllScopesFromCaip25CaveatValue`, `getAllWalletNamespacesFromCaip25CaveatValue`, `getAllScopesFromPermission`, `getAllScopesFromCaip25CaveatValue`, and `isNamespaceInScopesObject`
14
+ scope functions. ([#5609](https://github.com/MetaMask/core/pull/5609))
15
+ - Added `getCaip25CaveatFromPermission` misc functions. ([#5609](https://github.com/MetaMask/core/pull/5609))
16
+
17
+ ### Changed
18
+
19
+ - **BREAKING:** Renamed `setPermittedAccounts` to `setNonSCACaipAccountIdsInCaip25CaveatValue`. ([#5609](https://github.com/MetaMask/core/pull/5609))
20
+ - **BREAKING:** Renamed `setPermittedChainIds` to `setChainIdinCaip25CaveatValue`. ([#5609](https://github.com/MetaMask/core/pull/5609))
21
+ - **BREAKING:** Renamed `addPermittedChainId` to `addCaipChainIdInCaip25CaveatValue`. ([#5609](https://github.com/MetaMask/core/pull/5609))
22
+ - Bump `@metamask/controller-utils` to `^11.7.0` ([#5583](https://github.com/MetaMask/core/pull/5583))
23
+ - Bump `@metamask/network-controller` to `^23.2.0` ([#5583](https://github.com/MetaMask/core/pull/5583))
24
+
10
25
  ## [0.4.0]
11
26
 
12
27
  ### Added
@@ -16,11 +31,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
16
31
  - Add and Export `getAllScopesFromScopesObjects` filtering utility function ([#5647](https://github.com/MetaMask/core/pull/5647))
17
32
  - Add and Export `getSupportedScopeObjects` filtering utility function ([#5647](https://github.com/MetaMask/core/pull/5647))
18
33
 
19
- ### Changed
20
-
21
- - Bump `@metamask/controller-utils` to `^11.7.0` ([#5583](https://github.com/MetaMask/core/pull/5583))
22
- - Bump `@metamask/network-controller` to `^23.2.0` ([#5583](https://github.com/MetaMask/core/pull/5583))
23
-
24
34
  ## [0.3.0]
25
35
 
26
36
  ### Added
@@ -1,11 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.setPermittedAccounts = exports.setEthAccounts = exports.getEthAccounts = void 0;
3
+ exports.isCaipAccountIdInPermittedAccountIds = exports.isInternalAccountInPermittedAccountIds = exports.setNonSCACaipAccountIdsInCaip25CaveatValue = exports.getCaipAccountIdsFromCaip25CaveatValue = exports.getCaipAccountIdsFromScopesObjects = exports.setEthAccounts = exports.getEthAccounts = void 0;
4
+ const controller_utils_1 = require("@metamask/controller-utils");
4
5
  const utils_1 = require("@metamask/utils");
5
6
  const constants_1 = require("../scope/constants.cjs");
6
7
  const transform_1 = require("../scope/transform.cjs");
7
8
  const types_1 = require("../scope/types.cjs");
9
+ /*
10
+ *
11
+ *
12
+ * EVM SPECIFIC GETTERS AND SETTERS
13
+ *
14
+ *
15
+ */
8
16
  /**
17
+ *
9
18
  * Checks if a scope string is either an EIP155 or wallet namespaced scope string.
10
19
  *
11
20
  * @param scopeString - The scope string to check.
@@ -100,52 +109,167 @@ const setEthAccounts = (caip25CaveatValue, accounts) => {
100
109
  };
101
110
  };
102
111
  exports.setEthAccounts = setEthAccounts;
112
+ /*
113
+ *
114
+ *
115
+ * GENERALIZED GETTERS AND SETTERS
116
+ *
117
+ *
118
+ */
103
119
  /**
104
- * Sets the permitted accounts to scopes with matching namespaces in the given scopes object.
105
120
  *
106
- * @param scopesObject - The scopes object to set the permitted accounts for.
107
- * @param accounts - The permitted accounts to add to the appropriate scopes.
108
- * @returns The updated scopes object with the permitted accounts set.
121
+ * Getters
122
+ *
123
+ */
124
+ /**
125
+ * Gets all accounts from an array of scopes objects
126
+ * This extracts all account IDs from both required and optional scopes
127
+ * and returns a unique set.
128
+ *
129
+ * @param scopesObjects - The scopes objects to extract accounts from
130
+ * @returns Array of unique account IDs
131
+ */
132
+ function getCaipAccountIdsFromScopesObjects(scopesObjects) {
133
+ const allAccounts = new Set();
134
+ for (const scopeObject of scopesObjects) {
135
+ for (const { accounts } of Object.values(scopeObject)) {
136
+ for (const account of accounts) {
137
+ allAccounts.add(account);
138
+ }
139
+ }
140
+ }
141
+ return Array.from(allAccounts);
142
+ }
143
+ exports.getCaipAccountIdsFromScopesObjects = getCaipAccountIdsFromScopesObjects;
144
+ /**
145
+ * Gets all permitted accounts from a CAIP-25 caveat
146
+ * This extracts all account IDs from both required and optional scopes
147
+ * and returns a unique set.
148
+ *
149
+ * @param caip25CaveatValue - The CAIP-25 caveat value to extract accounts from
150
+ * @returns Array of unique account IDs
151
+ */
152
+ function getCaipAccountIdsFromCaip25CaveatValue(caip25CaveatValue) {
153
+ return getCaipAccountIdsFromScopesObjects([
154
+ caip25CaveatValue.requiredScopes,
155
+ caip25CaveatValue.optionalScopes,
156
+ ]);
157
+ }
158
+ exports.getCaipAccountIdsFromCaip25CaveatValue = getCaipAccountIdsFromCaip25CaveatValue;
159
+ /**
160
+ *
161
+ * Setters
162
+ *
109
163
  */
110
- const setPermittedAccountsForScopesObject = (scopesObject, accounts) => {
164
+ /**
165
+ * Sets the CAIP account IDs to scopes with matching namespaces in the given scopes object.
166
+ * This function should not be used with Smart Contract Accounts (SCA) because
167
+ * it adds the same account ID to all the scopes that have the same namespace.
168
+ *
169
+ * @param scopesObject - The scopes object to set the CAIP account IDs for.
170
+ * @param accounts - The CAIP account IDs to add to the appropriate scopes.
171
+ * @returns The updated scopes object with the CAIP account IDs set.
172
+ */
173
+ const setNonSCACaipAccountIdsInScopesObject = (scopesObject, accounts) => {
174
+ const accountsByNamespace = new Map();
175
+ for (const account of accounts) {
176
+ const { chain: { namespace }, address, } = (0, utils_1.parseCaipAccountId)(account);
177
+ if (!accountsByNamespace.has(namespace)) {
178
+ accountsByNamespace.set(namespace, new Set());
179
+ }
180
+ accountsByNamespace.get(namespace)?.add(address);
181
+ }
111
182
  const updatedScopesObject = {};
112
- Object.entries(scopesObject).forEach(([key, scopeObject]) => {
113
- // Cast needed because index type is returned as `string` by `Object.entries`
114
- const scopeString = key;
183
+ for (const [scopeString, scopeObject] of Object.entries(scopesObject)) {
115
184
  const { namespace, reference } = (0, types_1.parseScopeString)(scopeString);
116
185
  let caipAccounts = [];
117
- if (namespace && reference) {
118
- caipAccounts = accounts.reduce((acc, account) => {
119
- const { chain: { namespace: accountNamespace }, address: accountAddress, } = (0, utils_1.parseCaipAccountId)(account);
120
- // If the account namespace is the same as the scope namespace, add the account to the scope
121
- // This will, for example, distribute all EIP155 accounts, regardless of reference, to all EIP155 scopes
122
- if (namespace === accountNamespace) {
123
- acc.push(`${namespace}:${reference}:${accountAddress}`);
124
- }
125
- return acc;
126
- }, []);
186
+ if (namespace && reference && accountsByNamespace.has(namespace)) {
187
+ const addressSet = accountsByNamespace.get(namespace);
188
+ if (addressSet) {
189
+ caipAccounts = Array.from(addressSet).map((address) => `${namespace}:${reference}:${address}`);
190
+ }
127
191
  }
128
- const uniqueCaipAccounts = (0, transform_1.getUniqueArrayItems)(caipAccounts);
129
192
  updatedScopesObject[scopeString] = {
130
193
  ...scopeObject,
131
- accounts: uniqueCaipAccounts,
194
+ accounts: (0, transform_1.getUniqueArrayItems)(caipAccounts),
132
195
  };
133
- });
196
+ }
134
197
  return updatedScopesObject;
135
198
  };
136
199
  /**
137
200
  * Sets the permitted accounts to scopes with matching namespaces in the given CAIP-25 caveat value.
201
+ * This function should not be used with Smart Contract Accounts (SCA) because
202
+ * it adds the same account ID to all scopes that have the same namespace as the account.
138
203
  *
139
204
  * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
140
205
  * @param accounts - The permitted accounts to add to the appropriate scopes.
141
206
  * @returns The updated CAIP-25 caveat value with the permitted accounts set.
142
207
  */
143
- const setPermittedAccounts = (caip25CaveatValue, accounts) => {
208
+ const setNonSCACaipAccountIdsInCaip25CaveatValue = (caip25CaveatValue, accounts) => {
144
209
  return {
145
210
  ...caip25CaveatValue,
146
- requiredScopes: setPermittedAccountsForScopesObject(caip25CaveatValue.requiredScopes, accounts),
147
- optionalScopes: setPermittedAccountsForScopesObject(caip25CaveatValue.optionalScopes, accounts),
211
+ requiredScopes: setNonSCACaipAccountIdsInScopesObject(caip25CaveatValue.requiredScopes, accounts),
212
+ optionalScopes: setNonSCACaipAccountIdsInScopesObject(caip25CaveatValue.optionalScopes, accounts),
148
213
  };
149
214
  };
150
- exports.setPermittedAccounts = setPermittedAccounts;
215
+ exports.setNonSCACaipAccountIdsInCaip25CaveatValue = setNonSCACaipAccountIdsInCaip25CaveatValue;
216
+ /**
217
+ * Checks if an address and list of parsed scopes are connected to any of
218
+ * the permitted accounts based on scope matching
219
+ *
220
+ * @param address - The CAIP account address to check against permitted accounts
221
+ * @param parsedAccountScopes - The list of parsed CAIP chain ID to check against permitted accounts
222
+ * @param permittedAccounts - Array of CAIP account IDs that are permitted
223
+ * @returns True if the address and any account scope is connected to any permitted account
224
+ */
225
+ function isAddressWithParsedScopesInPermittedAccountIds(address, parsedAccountScopes, permittedAccounts) {
226
+ if (!address || !parsedAccountScopes.length || !permittedAccounts.length) {
227
+ return false;
228
+ }
229
+ return permittedAccounts.some((account) => {
230
+ const parsedPermittedAccount = (0, utils_1.parseCaipAccountId)(account);
231
+ return parsedAccountScopes.some(({ namespace, reference }) => {
232
+ if (namespace !== parsedPermittedAccount.chain.namespace) {
233
+ return false;
234
+ }
235
+ // handle eip155:0 case and insensitive evm address comparison
236
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
237
+ if (namespace === utils_1.KnownCaipNamespace.Eip155) {
238
+ return ((reference === '0' ||
239
+ reference === parsedPermittedAccount.chain.reference) &&
240
+ (0, controller_utils_1.isEqualCaseInsensitive)(address, parsedPermittedAccount.address));
241
+ }
242
+ return (reference === parsedPermittedAccount.chain.reference &&
243
+ address === parsedPermittedAccount.address);
244
+ });
245
+ });
246
+ }
247
+ /**
248
+ * Checks if an internal account is connected to any of the permitted accounts
249
+ * based on scope matching
250
+ *
251
+ * @param internalAccount - The internal account to check against permitted accounts
252
+ * @param permittedAccounts - Array of CAIP account IDs that are permitted
253
+ * @returns True if the account is connected to any permitted account
254
+ */
255
+ function isInternalAccountInPermittedAccountIds(internalAccount, permittedAccounts) {
256
+ const parsedInteralAccountScopes = internalAccount.scopes.map((scope) => {
257
+ return (0, types_1.parseScopeString)(scope);
258
+ });
259
+ return isAddressWithParsedScopesInPermittedAccountIds(internalAccount.address, parsedInteralAccountScopes, permittedAccounts);
260
+ }
261
+ exports.isInternalAccountInPermittedAccountIds = isInternalAccountInPermittedAccountIds;
262
+ /**
263
+ * Checks if an CAIP account ID is connected to any of the permitted accounts
264
+ * based on scope matching
265
+ *
266
+ * @param accountId - The CAIP account ID to check against permitted accounts
267
+ * @param permittedAccounts - Array of CAIP account IDs that are permitted
268
+ * @returns True if the account is connected to any permitted account
269
+ */
270
+ function isCaipAccountIdInPermittedAccountIds(accountId, permittedAccounts) {
271
+ const { address, chain } = (0, utils_1.parseCaipAccountId)(accountId);
272
+ return isAddressWithParsedScopesInPermittedAccountIds(address, [chain], permittedAccounts);
273
+ }
274
+ exports.isCaipAccountIdInPermittedAccountIds = isCaipAccountIdInPermittedAccountIds;
151
275
  //# sourceMappingURL=caip-permission-adapter-accounts.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"caip-permission-adapter-accounts.cjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-accounts.ts"],"names":[],"mappings":";;;AAAA,2CAMyB;AAGzB,sDAA4D;AAC5D,sDAAyD;AAEzD,8CAAkD;AAElD;;;;;GAKG;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,MAA4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'@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 * 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 * Sets the permitted accounts to scopes with matching namespaces in the given scopes object.\n *\n * @param scopesObject - The scopes object to set the permitted accounts for.\n * @param accounts - The permitted accounts to add to the appropriate scopes.\n * @returns The updated scopes object with the permitted accounts set.\n */\nconst setPermittedAccountsForScopesObject = (\n scopesObject: InternalScopesObject,\n accounts: CaipAccountId[],\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 { namespace, reference } = parseScopeString(scopeString);\n\n let caipAccounts: CaipAccountId[] = [];\n if (namespace && reference) {\n caipAccounts = accounts.reduce<CaipAccountId[]>((acc, account) => {\n const {\n chain: { namespace: accountNamespace },\n address: accountAddress,\n } = parseCaipAccountId(account);\n // If the account namespace is the same as the scope namespace, add the account to the scope\n // This will, for example, distribute all EIP155 accounts, regardless of reference, to all EIP155 scopes\n if (namespace === accountNamespace) {\n acc.push(`${namespace}:${reference}:${accountAddress}`);\n }\n return acc;\n }, []);\n }\n\n const uniqueCaipAccounts = getUniqueArrayItems(caipAccounts);\n\n updatedScopesObject[scopeString] = {\n ...scopeObject,\n accounts: uniqueCaipAccounts,\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 *\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 setPermittedAccounts = (\n caip25CaveatValue: Caip25CaveatValue,\n accounts: CaipAccountId[],\n): Caip25CaveatValue => {\n return {\n ...caip25CaveatValue,\n requiredScopes: setPermittedAccountsForScopesObject(\n caip25CaveatValue.requiredScopes,\n accounts,\n ),\n optionalScopes: setPermittedAccountsForScopesObject(\n caip25CaveatValue.optionalScopes,\n accounts,\n ),\n };\n};\n"]}
1
+ {"version":3,"file":"caip-permission-adapter-accounts.cjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-accounts.ts"],"names":[],"mappings":";;;AAA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;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,5 +1,7 @@
1
+ import type { InternalAccount } from "@metamask/keyring-internal-api";
1
2
  import { type CaipAccountId, type Hex } from "@metamask/utils";
2
3
  import type { Caip25CaveatValue } from "../caip25Permission.cjs";
4
+ import type { InternalScopesObject } from "../scope/types.cjs";
3
5
  /**
4
6
  * Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.
5
7
  *
@@ -17,12 +19,55 @@ export declare const getEthAccounts: (caip25CaveatValue: Pick<Caip25CaveatValue,
17
19
  * @returns The updated CAIP-25 caveat value with the Ethereum accounts set.
18
20
  */
19
21
  export declare const setEthAccounts: (caip25CaveatValue: Caip25CaveatValue, accounts: Hex[]) => Caip25CaveatValue;
22
+ /**
23
+ *
24
+ * Getters
25
+ *
26
+ */
27
+ /**
28
+ * Gets all accounts from an array of scopes objects
29
+ * This extracts all account IDs from both required and optional scopes
30
+ * and returns a unique set.
31
+ *
32
+ * @param scopesObjects - The scopes objects to extract accounts from
33
+ * @returns Array of unique account IDs
34
+ */
35
+ export declare function getCaipAccountIdsFromScopesObjects(scopesObjects: InternalScopesObject[]): CaipAccountId[];
36
+ /**
37
+ * Gets all permitted accounts from a CAIP-25 caveat
38
+ * This extracts all account IDs from both required and optional scopes
39
+ * and returns a unique set.
40
+ *
41
+ * @param caip25CaveatValue - The CAIP-25 caveat value to extract accounts from
42
+ * @returns Array of unique account IDs
43
+ */
44
+ export declare function getCaipAccountIdsFromCaip25CaveatValue(caip25CaveatValue: Caip25CaveatValue): CaipAccountId[];
20
45
  /**
21
46
  * Sets the permitted accounts to scopes with matching namespaces in the given CAIP-25 caveat value.
47
+ * This function should not be used with Smart Contract Accounts (SCA) because
48
+ * it adds the same account ID to all scopes that have the same namespace as the account.
22
49
  *
23
50
  * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
24
51
  * @param accounts - The permitted accounts to add to the appropriate scopes.
25
52
  * @returns The updated CAIP-25 caveat value with the permitted accounts set.
26
53
  */
27
- export declare const setPermittedAccounts: (caip25CaveatValue: Caip25CaveatValue, accounts: CaipAccountId[]) => Caip25CaveatValue;
54
+ export declare const setNonSCACaipAccountIdsInCaip25CaveatValue: (caip25CaveatValue: Caip25CaveatValue, accounts: CaipAccountId[]) => Caip25CaveatValue;
55
+ /**
56
+ * Checks if an internal account is connected to any of the permitted accounts
57
+ * based on scope matching
58
+ *
59
+ * @param internalAccount - The internal account to check against permitted accounts
60
+ * @param permittedAccounts - Array of CAIP account IDs that are permitted
61
+ * @returns True if the account is connected to any permitted account
62
+ */
63
+ export declare function isInternalAccountInPermittedAccountIds(internalAccount: InternalAccount, permittedAccounts: CaipAccountId[]): boolean;
64
+ /**
65
+ * Checks if an CAIP account ID is connected to any of the permitted accounts
66
+ * based on scope matching
67
+ *
68
+ * @param accountId - The CAIP account ID to check against permitted accounts
69
+ * @param permittedAccounts - Array of CAIP account IDs that are permitted
70
+ * @returns True if the account is connected to any permitted account
71
+ */
72
+ export declare function isCaipAccountIdInPermittedAccountIds(accountId: CaipAccountId, permittedAccounts: CaipAccountId[]): boolean;
28
73
  //# sourceMappingURL=caip-permission-adapter-accounts.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"caip-permission-adapter-accounts.d.cts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-accounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,aAAa,EAClB,KAAK,GAAG,EAGT,wBAAwB;AAEzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAgD7D;;;;;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;AA8CF;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,sBACZ,iBAAiB,YAC1B,aAAa,EAAE,KACxB,iBAYF,CAAC"}
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,5 +1,7 @@
1
+ import type { InternalAccount } from "@metamask/keyring-internal-api";
1
2
  import { type CaipAccountId, type Hex } from "@metamask/utils";
2
3
  import type { Caip25CaveatValue } from "../caip25Permission.mjs";
4
+ import type { InternalScopesObject } from "../scope/types.mjs";
3
5
  /**
4
6
  * Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.
5
7
  *
@@ -17,12 +19,55 @@ export declare const getEthAccounts: (caip25CaveatValue: Pick<Caip25CaveatValue,
17
19
  * @returns The updated CAIP-25 caveat value with the Ethereum accounts set.
18
20
  */
19
21
  export declare const setEthAccounts: (caip25CaveatValue: Caip25CaveatValue, accounts: Hex[]) => Caip25CaveatValue;
22
+ /**
23
+ *
24
+ * Getters
25
+ *
26
+ */
27
+ /**
28
+ * Gets all accounts from an array of scopes objects
29
+ * This extracts all account IDs from both required and optional scopes
30
+ * and returns a unique set.
31
+ *
32
+ * @param scopesObjects - The scopes objects to extract accounts from
33
+ * @returns Array of unique account IDs
34
+ */
35
+ export declare function getCaipAccountIdsFromScopesObjects(scopesObjects: InternalScopesObject[]): CaipAccountId[];
36
+ /**
37
+ * Gets all permitted accounts from a CAIP-25 caveat
38
+ * This extracts all account IDs from both required and optional scopes
39
+ * and returns a unique set.
40
+ *
41
+ * @param caip25CaveatValue - The CAIP-25 caveat value to extract accounts from
42
+ * @returns Array of unique account IDs
43
+ */
44
+ export declare function getCaipAccountIdsFromCaip25CaveatValue(caip25CaveatValue: Caip25CaveatValue): CaipAccountId[];
20
45
  /**
21
46
  * Sets the permitted accounts to scopes with matching namespaces in the given CAIP-25 caveat value.
47
+ * This function should not be used with Smart Contract Accounts (SCA) because
48
+ * it adds the same account ID to all scopes that have the same namespace as the account.
22
49
  *
23
50
  * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
24
51
  * @param accounts - The permitted accounts to add to the appropriate scopes.
25
52
  * @returns The updated CAIP-25 caveat value with the permitted accounts set.
26
53
  */
27
- export declare const setPermittedAccounts: (caip25CaveatValue: Caip25CaveatValue, accounts: CaipAccountId[]) => Caip25CaveatValue;
54
+ export declare const setNonSCACaipAccountIdsInCaip25CaveatValue: (caip25CaveatValue: Caip25CaveatValue, accounts: CaipAccountId[]) => Caip25CaveatValue;
55
+ /**
56
+ * Checks if an internal account is connected to any of the permitted accounts
57
+ * based on scope matching
58
+ *
59
+ * @param internalAccount - The internal account to check against permitted accounts
60
+ * @param permittedAccounts - Array of CAIP account IDs that are permitted
61
+ * @returns True if the account is connected to any permitted account
62
+ */
63
+ export declare function isInternalAccountInPermittedAccountIds(internalAccount: InternalAccount, permittedAccounts: CaipAccountId[]): boolean;
64
+ /**
65
+ * Checks if an CAIP account ID is connected to any of the permitted accounts
66
+ * based on scope matching
67
+ *
68
+ * @param accountId - The CAIP account ID to check against permitted accounts
69
+ * @param permittedAccounts - Array of CAIP account IDs that are permitted
70
+ * @returns True if the account is connected to any permitted account
71
+ */
72
+ export declare function isCaipAccountIdInPermittedAccountIds(accountId: CaipAccountId, permittedAccounts: CaipAccountId[]): boolean;
28
73
  //# sourceMappingURL=caip-permission-adapter-accounts.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"caip-permission-adapter-accounts.d.mts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-accounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,aAAa,EAClB,KAAK,GAAG,EAGT,wBAAwB;AAEzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAgD7D;;;;;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;AA8CF;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,sBACZ,iBAAiB,YAC1B,aAAa,EAAE,KACxB,iBAYF,CAAC"}
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"}