@metamask-previews/chain-agnostic-permission 0.4.0-preview-72122b6f → 0.4.0-preview-83773db5

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 +5 -15
  2. package/dist/adapters/caip-permission-adapter-accounts.cjs +26 -150
  3. package/dist/adapters/caip-permission-adapter-accounts.cjs.map +1 -1
  4. package/dist/adapters/caip-permission-adapter-accounts.d.cts +1 -46
  5. package/dist/adapters/caip-permission-adapter-accounts.d.cts.map +1 -1
  6. package/dist/adapters/caip-permission-adapter-accounts.d.mts +1 -46
  7. package/dist/adapters/caip-permission-adapter-accounts.d.mts.map +1 -1
  8. package/dist/adapters/caip-permission-adapter-accounts.mjs +24 -144
  9. package/dist/adapters/caip-permission-adapter-accounts.mjs.map +1 -1
  10. package/dist/adapters/caip-permission-adapter-permittedChains.cjs +31 -125
  11. package/dist/adapters/caip-permission-adapter-permittedChains.cjs.map +1 -1
  12. package/dist/adapters/caip-permission-adapter-permittedChains.d.cts +4 -45
  13. package/dist/adapters/caip-permission-adapter-permittedChains.d.cts.map +1 -1
  14. package/dist/adapters/caip-permission-adapter-permittedChains.d.mts +4 -45
  15. package/dist/adapters/caip-permission-adapter-permittedChains.d.mts.map +1 -1
  16. package/dist/adapters/caip-permission-adapter-permittedChains.mjs +28 -118
  17. package/dist/adapters/caip-permission-adapter-permittedChains.mjs.map +1 -1
  18. package/dist/caip25Permission.cjs +3 -14
  19. package/dist/caip25Permission.cjs.map +1 -1
  20. package/dist/caip25Permission.d.cts +0 -19
  21. package/dist/caip25Permission.d.cts.map +1 -1
  22. package/dist/caip25Permission.d.mts +0 -19
  23. package/dist/caip25Permission.d.mts.map +1 -1
  24. package/dist/caip25Permission.mjs +4 -14
  25. package/dist/caip25Permission.mjs.map +1 -1
  26. package/dist/index.cjs +8 -15
  27. package/dist/index.cjs.map +1 -1
  28. package/dist/index.d.cts +7 -6
  29. package/dist/index.d.cts.map +1 -1
  30. package/dist/index.d.mts +7 -6
  31. package/dist/index.d.mts.map +1 -1
  32. package/dist/index.mjs +7 -6
  33. package/dist/index.mjs.map +1 -1
  34. package/dist/scope/authorization.cjs +1 -16
  35. package/dist/scope/authorization.cjs.map +1 -1
  36. package/dist/scope/authorization.d.cts +1 -9
  37. package/dist/scope/authorization.d.cts.map +1 -1
  38. package/dist/scope/authorization.d.mts +1 -9
  39. package/dist/scope/authorization.d.mts.map +1 -1
  40. package/dist/scope/authorization.mjs +0 -14
  41. package/dist/scope/authorization.mjs.map +1 -1
  42. package/dist/scope/constants.cjs +1 -11
  43. package/dist/scope/constants.cjs.map +1 -1
  44. package/dist/scope/constants.d.cts +0 -7
  45. package/dist/scope/constants.d.cts.map +1 -1
  46. package/dist/scope/constants.d.mts +0 -7
  47. package/dist/scope/constants.d.mts.map +1 -1
  48. package/dist/scope/constants.mjs +0 -9
  49. package/dist/scope/constants.mjs.map +1 -1
  50. package/dist/scope/filter.cjs +29 -1
  51. package/dist/scope/filter.cjs.map +1 -1
  52. package/dist/scope/filter.d.cts +18 -2
  53. package/dist/scope/filter.d.cts.map +1 -1
  54. package/dist/scope/filter.d.mts +18 -2
  55. package/dist/scope/filter.d.mts.map +1 -1
  56. package/dist/scope/filter.mjs +26 -0
  57. package/dist/scope/filter.mjs.map +1 -1
  58. package/dist/scope/validation.cjs +12 -1
  59. package/dist/scope/validation.cjs.map +1 -1
  60. package/dist/scope/validation.d.cts +8 -0
  61. package/dist/scope/validation.d.cts.map +1 -1
  62. package/dist/scope/validation.d.mts +8 -0
  63. package/dist/scope/validation.d.mts.map +1 -1
  64. package/dist/scope/validation.mjs +10 -0
  65. package/dist/scope/validation.mjs.map +1 -1
  66. package/package.json +1 -2
package/CHANGELOG.md CHANGED
@@ -7,21 +7,6 @@ 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`, `getAllNonWalletNamespacesFromCaip25CaveatValue`, `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 `overwriteCaipChainIdsInCaip25CaveatValue`. ([#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
-
25
10
  ## [0.4.0]
26
11
 
27
12
  ### Added
@@ -31,6 +16,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
31
16
  - Add and Export `getAllScopesFromScopesObjects` filtering utility function ([#5647](https://github.com/MetaMask/core/pull/5647))
32
17
  - Add and Export `getSupportedScopeObjects` filtering utility function ([#5647](https://github.com/MetaMask/core/pull/5647))
33
18
 
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
+
34
24
  ## [0.3.0]
35
25
 
36
26
  ### Added
@@ -1,20 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
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");
3
+ exports.setPermittedAccounts = exports.setEthAccounts = exports.getEthAccounts = void 0;
5
4
  const utils_1 = require("@metamask/utils");
6
5
  const constants_1 = require("../scope/constants.cjs");
7
6
  const transform_1 = require("../scope/transform.cjs");
8
7
  const types_1 = require("../scope/types.cjs");
9
- /*
10
- *
11
- *
12
- * EVM SPECIFIC GETTERS AND SETTERS
13
- *
14
- *
15
- */
16
8
  /**
17
- *
18
9
  * Checks if a scope string is either an EIP155 or wallet namespaced scope string.
19
10
  *
20
11
  * @param scopeString - The scope string to check.
@@ -109,167 +100,52 @@ const setEthAccounts = (caip25CaveatValue, accounts) => {
109
100
  };
110
101
  };
111
102
  exports.setEthAccounts = setEthAccounts;
112
- /*
113
- *
114
- *
115
- * GENERALIZED GETTERS AND SETTERS
116
- *
117
- *
118
- */
119
103
  /**
104
+ * Sets the permitted accounts to scopes with matching namespaces in the given scopes object.
120
105
  *
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
- *
163
- */
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.
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.
172
109
  */
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
- }
110
+ const setPermittedAccountsForScopesObject = (scopesObject, accounts) => {
182
111
  const updatedScopesObject = {};
183
- for (const [scopeString, scopeObject] of Object.entries(scopesObject)) {
112
+ Object.entries(scopesObject).forEach(([key, scopeObject]) => {
113
+ // Cast needed because index type is returned as `string` by `Object.entries`
114
+ const scopeString = key;
184
115
  const { namespace, reference } = (0, types_1.parseScopeString)(scopeString);
185
116
  let caipAccounts = [];
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
- }
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
+ }, []);
191
127
  }
128
+ const uniqueCaipAccounts = (0, transform_1.getUniqueArrayItems)(caipAccounts);
192
129
  updatedScopesObject[scopeString] = {
193
130
  ...scopeObject,
194
- accounts: (0, transform_1.getUniqueArrayItems)(caipAccounts),
131
+ accounts: uniqueCaipAccounts,
195
132
  };
196
- }
133
+ });
197
134
  return updatedScopesObject;
198
135
  };
199
136
  /**
200
137
  * 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.
203
138
  *
204
139
  * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
205
140
  * @param accounts - The permitted accounts to add to the appropriate scopes.
206
141
  * @returns The updated CAIP-25 caveat value with the permitted accounts set.
207
142
  */
208
- const setNonSCACaipAccountIdsInCaip25CaveatValue = (caip25CaveatValue, accounts) => {
143
+ const setPermittedAccounts = (caip25CaveatValue, accounts) => {
209
144
  return {
210
145
  ...caip25CaveatValue,
211
- requiredScopes: setNonSCACaipAccountIdsInScopesObject(caip25CaveatValue.requiredScopes, accounts),
212
- optionalScopes: setNonSCACaipAccountIdsInScopesObject(caip25CaveatValue.optionalScopes, accounts),
146
+ requiredScopes: setPermittedAccountsForScopesObject(caip25CaveatValue.requiredScopes, accounts),
147
+ optionalScopes: setPermittedAccountsForScopesObject(caip25CaveatValue.optionalScopes, accounts),
213
148
  };
214
149
  };
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;
150
+ exports.setPermittedAccounts = setPermittedAccounts;
275
151
  //# 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,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,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AS,KAAK,GAAG;oBAChB,SAAS,KAAK,sBAAsB,CAAC,KAAK,CAAC,SAAS,CAAC;oBACvD,IAAA,yCAAsB,EAAC,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,SAAgB,sCAAsC,CACpD,eAAgC,EAChC,iBAAkC;IAElC,MAAM,0BAA0B,GAAG,eAAe,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;QACtE,OAAO,IAAA,wBAAgB,EAAC,KAAK,CAAC,CAAC;IACjC,CAAC,CAAC,CAAC;IAEH,OAAO,8CAA8C,CACnD,eAAe,CAAC,OAAO,EACvB,0BAA0B,EAC1B,iBAAiB,CAClB,CAAC;AACJ,CAAC;AAbD,wFAaC;AAED;;;;;;;GAOG;AACH,SAAgB,oCAAoC,CAClD,SAAwB,EACxB,iBAAkC;IAElC,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,IAAA,0BAAkB,EAAC,SAAS,CAAC,CAAC;IAEzD,OAAO,8CAA8C,CACnD,OAAO,EACP,CAAC,KAAK,CAAC,EACP,iBAAiB,CAClB,CAAC;AACJ,CAAC;AAXD,oFAWC","sourcesContent":["import { isEqualCaseInsensitive } from '@metamask/controller-utils';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport {\n assertIsStrictHexString,\n type CaipAccountAddress,\n type CaipAccountId,\n type CaipNamespace,\n type CaipReference,\n type Hex,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\n\nimport type { Caip25CaveatValue } from '../caip25Permission';\nimport { KnownWalletScopeString } from '../scope/constants';\nimport { getUniqueArrayItems } from '../scope/transform';\nimport type { InternalScopeString, InternalScopesObject } from '../scope/types';\nimport { parseScopeString } from '../scope/types';\n\n/*\n *\n *\n * EVM SPECIFIC GETTERS AND SETTERS\n *\n *\n */\n\n/**\n *\n * Checks if a scope string is either an EIP155 or wallet namespaced scope string.\n *\n * @param scopeString - The scope string to check.\n * @returns True if the scope string is an EIP155 or wallet namespaced scope string, false otherwise.\n */\nconst isEip155ScopeString = (scopeString: InternalScopeString) => {\n const { namespace } = parseScopeString(scopeString);\n\n return (\n namespace === KnownCaipNamespace.Eip155 ||\n // We are trying to discern the type of `scopeString`.\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n scopeString === KnownWalletScopeString.Eip155\n );\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) accounts from internal scopes.\n *\n * @param scopes - The internal scopes from which to get the Ethereum accounts.\n * @returns An array of Ethereum accounts.\n */\nconst getEthAccountsFromScopes = (scopes: InternalScopesObject) => {\n const ethAccounts: Hex[] = [];\n\n Object.entries(scopes).forEach(([_, { accounts }]) => {\n accounts?.forEach((account) => {\n const { address, chainId } = parseCaipAccountId(account);\n\n if (isEip155ScopeString(chainId)) {\n // This address should always be a valid Hex string because\n // it's an EIP155/Ethereum account\n assertIsStrictHexString(address);\n ethAccounts.push(address);\n }\n });\n });\n\n return ethAccounts;\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to get the Ethereum accounts from.\n * @returns An array of Ethereum accounts.\n */\nexport const getEthAccounts = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n): Hex[] => {\n const { requiredScopes, optionalScopes } = caip25CaveatValue;\n\n const ethAccounts: Hex[] = [\n ...getEthAccountsFromScopes(requiredScopes),\n ...getEthAccountsFromScopes(optionalScopes),\n ];\n\n return getUniqueArrayItems(ethAccounts);\n};\n\n/**\n * Sets the Ethereum (EIP155 namespaced) accounts for the given scopes object.\n *\n * @param scopesObject - The scopes object to set the Ethereum accounts for.\n * @param accounts - The Ethereum accounts to set.\n * @returns The updated scopes object with the Ethereum accounts set.\n */\nconst setEthAccountsForScopesObject = (\n scopesObject: InternalScopesObject,\n accounts: Hex[],\n) => {\n const updatedScopesObject: InternalScopesObject = {};\n Object.entries(scopesObject).forEach(([key, scopeObject]) => {\n // Cast needed because index type is returned as `string` by `Object.entries`\n const scopeString = key as keyof typeof scopesObject;\n const isWalletNamespace = scopeString === KnownCaipNamespace.Wallet;\n const { namespace, reference } = parseScopeString(scopeString);\n if (!isEip155ScopeString(scopeString) && !isWalletNamespace) {\n updatedScopesObject[scopeString] = scopeObject;\n return;\n }\n\n let caipAccounts: CaipAccountId[] = [];\n if (namespace && reference) {\n caipAccounts = accounts.map<CaipAccountId>(\n (account) => `${namespace}:${reference}:${account}`,\n );\n }\n\n updatedScopesObject[scopeString] = {\n ...scopeObject,\n accounts: caipAccounts,\n };\n });\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the Ethereum (EIP155 namespaced) accounts for the given CAIP-25 caveat value.\n * We set the same accounts for all the scopes that are EIP155 or Wallet namespaced because\n * we do not provide UI/UX flows for selecting different accounts across different chains.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the Ethereum accounts for.\n * @param accounts - The Ethereum accounts to set.\n * @returns The updated CAIP-25 caveat value with the Ethereum accounts set.\n */\nexport const setEthAccounts = (\n caip25CaveatValue: Caip25CaveatValue,\n accounts: Hex[],\n): Caip25CaveatValue => {\n return {\n ...caip25CaveatValue,\n requiredScopes: setEthAccountsForScopesObject(\n caip25CaveatValue.requiredScopes,\n accounts,\n ),\n optionalScopes: setEthAccountsForScopesObject(\n caip25CaveatValue.optionalScopes,\n accounts,\n ),\n };\n};\n\n/*\n *\n *\n * GENERALIZED GETTERS AND SETTERS\n *\n *\n */\n\n/**\n *\n * Getters\n *\n */\n\n/**\n * Gets all accounts from an array of scopes objects\n * This extracts all account IDs from both required and optional scopes\n * and returns a unique set.\n *\n * @param scopesObjects - The scopes objects to extract accounts from\n * @returns Array of unique account IDs\n */\nexport function getCaipAccountIdsFromScopesObjects(\n scopesObjects: InternalScopesObject[],\n): CaipAccountId[] {\n const allAccounts = new Set<CaipAccountId>();\n\n for (const scopeObject of scopesObjects) {\n for (const { accounts } of Object.values(scopeObject)) {\n for (const account of accounts) {\n allAccounts.add(account);\n }\n }\n }\n\n return Array.from(allAccounts);\n}\n\n/**\n * Gets all permitted accounts from a CAIP-25 caveat\n * This extracts all account IDs from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to extract accounts from\n * @returns Array of unique account IDs\n */\nexport function getCaipAccountIdsFromCaip25CaveatValue(\n caip25CaveatValue: Caip25CaveatValue,\n): CaipAccountId[] {\n return getCaipAccountIdsFromScopesObjects([\n caip25CaveatValue.requiredScopes,\n caip25CaveatValue.optionalScopes,\n ]);\n}\n\n/**\n *\n * Setters\n *\n */\n\n/**\n * Sets the CAIP account IDs to scopes with matching namespaces in the given scopes object.\n * This function should not be used with Smart Contract Accounts (SCA) because\n * it adds the same account ID to all the scopes that have the same namespace.\n *\n * @param scopesObject - The scopes object to set the CAIP account IDs for.\n * @param accounts - The CAIP account IDs to add to the appropriate scopes.\n * @returns The updated scopes object with the CAIP account IDs set.\n */\nconst setNonSCACaipAccountIdsInScopesObject = (\n scopesObject: InternalScopesObject,\n accounts: CaipAccountId[],\n) => {\n const accountsByNamespace = new Map<string, Set<string>>();\n\n for (const account of accounts) {\n const {\n chain: { namespace },\n address,\n } = parseCaipAccountId(account);\n\n if (!accountsByNamespace.has(namespace)) {\n accountsByNamespace.set(namespace, new Set());\n }\n\n accountsByNamespace.get(namespace)?.add(address);\n }\n\n const updatedScopesObject: InternalScopesObject = {};\n\n for (const [scopeString, scopeObject] of Object.entries(scopesObject)) {\n const { namespace, reference } = parseScopeString(scopeString as string);\n\n let caipAccounts: CaipAccountId[] = [];\n\n if (namespace && reference && accountsByNamespace.has(namespace)) {\n const addressSet = accountsByNamespace.get(namespace);\n if (addressSet) {\n caipAccounts = Array.from(addressSet).map(\n (address) => `${namespace}:${reference}:${address}` as CaipAccountId,\n );\n }\n }\n\n updatedScopesObject[scopeString as keyof typeof scopesObject] = {\n ...scopeObject,\n accounts: getUniqueArrayItems(caipAccounts),\n };\n }\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the permitted accounts to scopes with matching namespaces in the given CAIP-25 caveat value.\n * This function should not be used with Smart Contract Accounts (SCA) because\n * it adds the same account ID to all scopes that have the same namespace as the account.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.\n * @param accounts - The permitted accounts to add to the appropriate scopes.\n * @returns The updated CAIP-25 caveat value with the permitted accounts set.\n */\nexport const setNonSCACaipAccountIdsInCaip25CaveatValue = (\n caip25CaveatValue: Caip25CaveatValue,\n accounts: CaipAccountId[],\n): Caip25CaveatValue => {\n return {\n ...caip25CaveatValue,\n requiredScopes: setNonSCACaipAccountIdsInScopesObject(\n caip25CaveatValue.requiredScopes,\n accounts,\n ),\n optionalScopes: setNonSCACaipAccountIdsInScopesObject(\n caip25CaveatValue.optionalScopes,\n accounts,\n ),\n };\n};\n\n/**\n * Checks if an address and list of parsed scopes are connected to any of\n * the permitted accounts based on scope matching\n *\n * @param address - The CAIP account address to check against permitted accounts\n * @param parsedAccountScopes - The list of parsed CAIP chain ID to check against permitted accounts\n * @param permittedAccounts - Array of CAIP account IDs that are permitted\n * @returns True if the address and any account scope is connected to any permitted account\n */\nfunction isAddressWithParsedScopesInPermittedAccountIds(\n address: CaipAccountAddress,\n parsedAccountScopes: {\n namespace?: CaipNamespace;\n reference?: CaipReference;\n }[],\n permittedAccounts: CaipAccountId[],\n) {\n if (!address || !parsedAccountScopes.length || !permittedAccounts.length) {\n return false;\n }\n\n return permittedAccounts.some((account) => {\n const parsedPermittedAccount = parseCaipAccountId(account);\n\n return parsedAccountScopes.some(({ namespace, reference }) => {\n if (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,2CAMyB;AAGzB,sDAA4D;AAC5D,sDAAyD;AAEzD,8CAAkD;AAElD;;;;;GAKG;AACH,MAAM,mBAAmB,GAAG,CAAC,WAAgC,EAAE,EAAE;IAC/D,MAAM,EAAE,SAAS,EAAE,GAAG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CAAC,cAAc,EAChC,QAAQ,CACT;QACD,cAAc,EAAE,mCAAmC,CACjD,iBAAiB,CAAC,cAAc,EAChC,QAAQ,CACT;KACF,CAAC;AACJ,CAAC,CAAC;AAfW,QAAA,oBAAoB,wBAe/B","sourcesContent":["import {\n assertIsStrictHexString,\n type CaipAccountId,\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 * 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,7 +1,5 @@
1
- import type { InternalAccount } from "@metamask/keyring-internal-api";
2
1
  import { type CaipAccountId, type Hex } from "@metamask/utils";
3
2
  import type { Caip25CaveatValue } from "../caip25Permission.cjs";
4
- import type { InternalScopesObject } from "../scope/types.cjs";
5
3
  /**
6
4
  * Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.
7
5
  *
@@ -19,55 +17,12 @@ export declare const getEthAccounts: (caip25CaveatValue: Pick<Caip25CaveatValue,
19
17
  * @returns The updated CAIP-25 caveat value with the Ethereum accounts set.
20
18
  */
21
19
  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[];
45
20
  /**
46
21
  * 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.
49
22
  *
50
23
  * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
51
24
  * @param accounts - The permitted accounts to add to the appropriate scopes.
52
25
  * @returns The updated CAIP-25 caveat value with the permitted accounts set.
53
26
  */
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;
27
+ export declare const setPermittedAccounts: (caip25CaveatValue: Caip25CaveatValue, accounts: CaipAccountId[]) => Caip25CaveatValue;
73
28
  //# 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":"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":"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,7 +1,5 @@
1
- import type { InternalAccount } from "@metamask/keyring-internal-api";
2
1
  import { type CaipAccountId, type Hex } from "@metamask/utils";
3
2
  import type { Caip25CaveatValue } from "../caip25Permission.mjs";
4
- import type { InternalScopesObject } from "../scope/types.mjs";
5
3
  /**
6
4
  * Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.
7
5
  *
@@ -19,55 +17,12 @@ export declare const getEthAccounts: (caip25CaveatValue: Pick<Caip25CaveatValue,
19
17
  * @returns The updated CAIP-25 caveat value with the Ethereum accounts set.
20
18
  */
21
19
  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[];
45
20
  /**
46
21
  * 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.
49
22
  *
50
23
  * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
51
24
  * @param accounts - The permitted accounts to add to the appropriate scopes.
52
25
  * @returns The updated CAIP-25 caveat value with the permitted accounts set.
53
26
  */
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;
27
+ export declare const setPermittedAccounts: (caip25CaveatValue: Caip25CaveatValue, accounts: CaipAccountId[]) => Caip25CaveatValue;
73
28
  //# 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":"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":"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"}