@metamask-previews/chain-agnostic-permission 0.4.0-preview-514219da → 0.4.0-preview-dd19f50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +0 -8
- package/dist/adapters/caip-permission-adapter-accounts.cjs +26 -143
- package/dist/adapters/caip-permission-adapter-accounts.cjs.map +1 -1
- package/dist/adapters/caip-permission-adapter-accounts.d.cts +1 -46
- package/dist/adapters/caip-permission-adapter-accounts.d.cts.map +1 -1
- package/dist/adapters/caip-permission-adapter-accounts.d.mts +1 -46
- package/dist/adapters/caip-permission-adapter-accounts.d.mts.map +1 -1
- package/dist/adapters/caip-permission-adapter-accounts.mjs +24 -137
- package/dist/adapters/caip-permission-adapter-accounts.mjs.map +1 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.cjs +31 -125
- package/dist/adapters/caip-permission-adapter-permittedChains.cjs.map +1 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.d.cts +4 -45
- package/dist/adapters/caip-permission-adapter-permittedChains.d.cts.map +1 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.d.mts +4 -45
- package/dist/adapters/caip-permission-adapter-permittedChains.d.mts.map +1 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.mjs +28 -118
- package/dist/adapters/caip-permission-adapter-permittedChains.mjs.map +1 -1
- package/dist/caip25Permission.cjs +3 -14
- package/dist/caip25Permission.cjs.map +1 -1
- package/dist/caip25Permission.d.cts +0 -16
- package/dist/caip25Permission.d.cts.map +1 -1
- package/dist/caip25Permission.d.mts +0 -16
- package/dist/caip25Permission.d.mts.map +1 -1
- package/dist/caip25Permission.mjs +4 -14
- package/dist/caip25Permission.mjs.map +1 -1
- package/dist/index.cjs +8 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -6
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +7 -6
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +7 -6
- package/dist/index.mjs.map +1 -1
- package/dist/scope/authorization.cjs +1 -16
- package/dist/scope/authorization.cjs.map +1 -1
- package/dist/scope/authorization.d.cts +1 -9
- package/dist/scope/authorization.d.cts.map +1 -1
- package/dist/scope/authorization.d.mts +1 -9
- package/dist/scope/authorization.d.mts.map +1 -1
- package/dist/scope/authorization.mjs +0 -14
- package/dist/scope/authorization.mjs.map +1 -1
- package/dist/scope/constants.cjs +1 -11
- package/dist/scope/constants.cjs.map +1 -1
- package/dist/scope/constants.d.cts +0 -7
- package/dist/scope/constants.d.cts.map +1 -1
- package/dist/scope/constants.d.mts +0 -7
- package/dist/scope/constants.d.mts.map +1 -1
- package/dist/scope/constants.mjs +0 -9
- package/dist/scope/constants.mjs.map +1 -1
- package/dist/scope/filter.cjs +29 -1
- package/dist/scope/filter.cjs.map +1 -1
- package/dist/scope/filter.d.cts +18 -2
- package/dist/scope/filter.d.cts.map +1 -1
- package/dist/scope/filter.d.mts +18 -2
- package/dist/scope/filter.d.mts.map +1 -1
- package/dist/scope/filter.mjs +26 -0
- package/dist/scope/filter.mjs.map +1 -1
- package/dist/scope/validation.cjs +12 -1
- package/dist/scope/validation.cjs.map +1 -1
- package/dist/scope/validation.d.cts +8 -0
- package/dist/scope/validation.d.cts.map +1 -1
- package/dist/scope/validation.d.mts +8 -0
- package/dist/scope/validation.d.mts.map +1 -1
- package/dist/scope/validation.mjs +10 -0
- package/dist/scope/validation.mjs.map +1 -1
- package/package.json +1 -2
package/CHANGELOG.md
CHANGED
@@ -18,17 +18,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
18
18
|
|
19
19
|
### Changed
|
20
20
|
|
21
|
-
- **BREAKING:** Renamed `setPermittedAccounts` to `setNonSCACaipAccountIdsInCaip25CaveatValue`.
|
22
|
-
- **BREAKING:** Renamed `setPermittedChainIds` to `setCaipChainIdsInCaip25CaveatValue`.
|
23
21
|
- Bump `@metamask/controller-utils` to `^11.7.0` ([#5583](https://github.com/MetaMask/core/pull/5583))
|
24
22
|
- Bump `@metamask/network-controller` to `^23.2.0` ([#5583](https://github.com/MetaMask/core/pull/5583))
|
25
23
|
|
26
|
-
### Added
|
27
|
-
|
28
|
-
- Added `getCaipAccountIdsFromScopesObject`, `getCaipAccountIdsFromScopesObjects`, and `getCaipAccountIdsFromCaip25CaveatValue` functions.
|
29
|
-
- Added `getAllScopesFromScopesObjects`, `getAllScopesFromCaip25CaveatValue`, `getAllNonWalletNamespacesFromCaip25CaveatValue`, and `getAllScopesFromPermission`
|
30
|
-
functions.
|
31
|
-
|
32
24
|
## [0.3.0]
|
33
25
|
|
34
26
|
### Added
|
@@ -1,19 +1,11 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.setPermittedAccounts = exports.setEthAccounts = exports.getEthAccounts = void 0;
|
4
4
|
const utils_1 = require("@metamask/utils");
|
5
5
|
const constants_1 = require("../scope/constants.cjs");
|
6
6
|
const transform_1 = require("../scope/transform.cjs");
|
7
7
|
const types_1 = require("../scope/types.cjs");
|
8
|
-
/*
|
9
|
-
*
|
10
|
-
*
|
11
|
-
* EVM SPECIFIC GETTERS AND SETTERS
|
12
|
-
*
|
13
|
-
*
|
14
|
-
*/
|
15
8
|
/**
|
16
|
-
*
|
17
9
|
* Checks if a scope string is either an EIP155 or wallet namespaced scope string.
|
18
10
|
*
|
19
11
|
* @param scopeString - The scope string to check.
|
@@ -108,161 +100,52 @@ const setEthAccounts = (caip25CaveatValue, accounts) => {
|
|
108
100
|
};
|
109
101
|
};
|
110
102
|
exports.setEthAccounts = setEthAccounts;
|
111
|
-
/*
|
112
|
-
*
|
113
|
-
*
|
114
|
-
* GENERALIZED GETTERS AND SETTERS
|
115
|
-
*
|
116
|
-
*
|
117
|
-
*/
|
118
|
-
/**
|
119
|
-
*
|
120
|
-
* Getters
|
121
|
-
*
|
122
|
-
*/
|
123
|
-
/**
|
124
|
-
* Gets all accounts from an array of scopes objects
|
125
|
-
* This extracts all account IDs from both required and optional scopes
|
126
|
-
* and returns a unique set.
|
127
|
-
*
|
128
|
-
* @param scopesObjects - The scopes objects to extract accounts from
|
129
|
-
* @returns Array of unique account IDs
|
130
|
-
*/
|
131
|
-
function getCaipAccountIdsFromScopesObjects(scopesObjects) {
|
132
|
-
const allAccounts = new Set();
|
133
|
-
for (const scopeObject of scopesObjects) {
|
134
|
-
for (const { accounts } of Object.values(scopeObject)) {
|
135
|
-
for (const account of accounts) {
|
136
|
-
allAccounts.add(account);
|
137
|
-
}
|
138
|
-
}
|
139
|
-
}
|
140
|
-
return Array.from(allAccounts);
|
141
|
-
}
|
142
|
-
exports.getCaipAccountIdsFromScopesObjects = getCaipAccountIdsFromScopesObjects;
|
143
|
-
/**
|
144
|
-
* Gets all permitted accounts from a CAIP-25 caveat
|
145
|
-
* This extracts all account IDs from both required and optional scopes
|
146
|
-
* and returns a unique set.
|
147
|
-
*
|
148
|
-
* @param caip25CaveatValue - The CAIP-25 caveat value to extract accounts from
|
149
|
-
* @returns Array of unique account IDs
|
150
|
-
*/
|
151
|
-
function getCaipAccountIdsFromCaip25CaveatValue(caip25CaveatValue) {
|
152
|
-
return getCaipAccountIdsFromScopesObjects([
|
153
|
-
caip25CaveatValue.requiredScopes,
|
154
|
-
caip25CaveatValue.optionalScopes,
|
155
|
-
]);
|
156
|
-
}
|
157
|
-
exports.getCaipAccountIdsFromCaip25CaveatValue = getCaipAccountIdsFromCaip25CaveatValue;
|
158
|
-
/**
|
159
|
-
*
|
160
|
-
* Setters
|
161
|
-
*
|
162
|
-
*/
|
163
103
|
/**
|
164
|
-
* Sets the
|
165
|
-
* This function should not be used with Smart Contract Accounts (SCA) because
|
166
|
-
* it adds the same account ID to all the scopes that have the same namespace.
|
104
|
+
* Sets the permitted accounts to scopes with matching namespaces in the given scopes object.
|
167
105
|
*
|
168
|
-
* @param scopesObject - The scopes object to set the
|
169
|
-
* @param accounts - The
|
170
|
-
* @returns The updated scopes object with the
|
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.
|
171
109
|
*/
|
172
|
-
const
|
173
|
-
const accountsByNamespace = new Map();
|
174
|
-
for (const account of accounts) {
|
175
|
-
const { chain: { namespace }, address, } = (0, utils_1.parseCaipAccountId)(account);
|
176
|
-
if (!accountsByNamespace.has(namespace)) {
|
177
|
-
accountsByNamespace.set(namespace, new Set());
|
178
|
-
}
|
179
|
-
accountsByNamespace.get(namespace)?.add(address);
|
180
|
-
}
|
110
|
+
const setPermittedAccountsForScopesObject = (scopesObject, accounts) => {
|
181
111
|
const updatedScopesObject = {};
|
182
|
-
|
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
115
|
const { namespace, reference } = (0, types_1.parseScopeString)(scopeString);
|
184
116
|
let caipAccounts = [];
|
185
|
-
if (namespace && reference
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
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
|
+
}, []);
|
190
127
|
}
|
128
|
+
const uniqueCaipAccounts = (0, transform_1.getUniqueArrayItems)(caipAccounts);
|
191
129
|
updatedScopesObject[scopeString] = {
|
192
130
|
...scopeObject,
|
193
|
-
accounts:
|
131
|
+
accounts: uniqueCaipAccounts,
|
194
132
|
};
|
195
|
-
}
|
133
|
+
});
|
196
134
|
return updatedScopesObject;
|
197
135
|
};
|
198
136
|
/**
|
199
137
|
* Sets the permitted accounts to scopes with matching namespaces in the given CAIP-25 caveat value.
|
200
|
-
* This function should not be used with Smart Contract Accounts (SCA) because
|
201
|
-
* it adds the same account ID to all scopes that have the same namespace as the account.
|
202
138
|
*
|
203
139
|
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
|
204
140
|
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
205
141
|
* @returns The updated CAIP-25 caveat value with the permitted accounts set.
|
206
142
|
*/
|
207
|
-
const
|
143
|
+
const setPermittedAccounts = (caip25CaveatValue, accounts) => {
|
208
144
|
return {
|
209
145
|
...caip25CaveatValue,
|
210
|
-
requiredScopes:
|
211
|
-
optionalScopes:
|
146
|
+
requiredScopes: setPermittedAccountsForScopesObject(caip25CaveatValue.requiredScopes, accounts),
|
147
|
+
optionalScopes: setPermittedAccountsForScopesObject(caip25CaveatValue.optionalScopes, accounts),
|
212
148
|
};
|
213
149
|
};
|
214
|
-
exports.
|
215
|
-
/**
|
216
|
-
* Checks if an address and list of parsed scopes are connected to any of
|
217
|
-
* the permitted accounts based on scope matching
|
218
|
-
*
|
219
|
-
* @param address - The CAIP account address to check against permitted accounts
|
220
|
-
* @param parsedAccountScopes - The list of parsed CAIP chain ID to check against permitted accounts
|
221
|
-
* @param permittedAccounts - Array of CAIP account IDs that are permitted
|
222
|
-
* @returns True if the address and any account scope is connected to any permitted account
|
223
|
-
*/
|
224
|
-
function isAddressWithParsedScopesInPermittedAccountIds(address, parsedAccountScopes, permittedAccounts) {
|
225
|
-
if (!address || !parsedAccountScopes.length || !permittedAccounts.length) {
|
226
|
-
return false;
|
227
|
-
}
|
228
|
-
return permittedAccounts.some((account) => {
|
229
|
-
const parsedPermittedAccount = (0, utils_1.parseCaipAccountId)(account);
|
230
|
-
return parsedAccountScopes.some(({ namespace, reference }) => {
|
231
|
-
if (namespace !== parsedPermittedAccount.chain.namespace ||
|
232
|
-
address !== parsedPermittedAccount.address) {
|
233
|
-
return false;
|
234
|
-
}
|
235
|
-
return (reference === '0' ||
|
236
|
-
reference === parsedPermittedAccount.chain.reference);
|
237
|
-
});
|
238
|
-
});
|
239
|
-
}
|
240
|
-
/**
|
241
|
-
* Checks if an internal account is connected to any of the permitted accounts
|
242
|
-
* based on scope matching
|
243
|
-
*
|
244
|
-
* @param internalAccount - The internal account to check against permitted accounts
|
245
|
-
* @param permittedAccounts - Array of CAIP account IDs that are permitted
|
246
|
-
* @returns True if the account is connected to any permitted account
|
247
|
-
*/
|
248
|
-
function isInternalAccountInPermittedAccountIds(internalAccount, permittedAccounts) {
|
249
|
-
const parsedInteralAccountScopes = internalAccount.scopes.map((scope) => {
|
250
|
-
return (0, types_1.parseScopeString)(scope);
|
251
|
-
});
|
252
|
-
return isAddressWithParsedScopesInPermittedAccountIds(internalAccount.address, parsedInteralAccountScopes, permittedAccounts);
|
253
|
-
}
|
254
|
-
exports.isInternalAccountInPermittedAccountIds = isInternalAccountInPermittedAccountIds;
|
255
|
-
/**
|
256
|
-
* Checks if an CAIP account ID is connected to any of the permitted accounts
|
257
|
-
* based on scope matching
|
258
|
-
*
|
259
|
-
* @param accountId - The CAIP account ID to check against permitted accounts
|
260
|
-
* @param permittedAccounts - Array of CAIP account IDs that are permitted
|
261
|
-
* @returns True if the account is connected to any permitted account
|
262
|
-
*/
|
263
|
-
function isCaipAccountIdInPermittedAccountIds(accountId, permittedAccounts) {
|
264
|
-
const { address, chain } = (0, utils_1.parseCaipAccountId)(accountId);
|
265
|
-
return isAddressWithParsedScopesInPermittedAccountIds(address, [chain], permittedAccounts);
|
266
|
-
}
|
267
|
-
exports.isCaipAccountIdInPermittedAccountIds = isCaipAccountIdInPermittedAccountIds;
|
150
|
+
exports.setPermittedAccounts = setPermittedAccounts;
|
268
151
|
//# sourceMappingURL=caip-permission-adapter-accounts.cjs.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"caip-permission-adapter-accounts.c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AAK,CAAC,EACP,iBAAiB,CAClB,CAAC;AACJ,CAAC;AAXD,oFAWC","sourcesContent":["import type { InternalAccount } from '@metamask/keyring-internal-api';\nimport {\n assertIsStrictHexString,\n type CaipAccountAddress,\n type CaipAccountId,\n type CaipNamespace,\n type CaipReference,\n type Hex,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\n\nimport type { Caip25CaveatValue } from '../caip25Permission';\nimport { KnownWalletScopeString } from '../scope/constants';\nimport { getUniqueArrayItems } from '../scope/transform';\nimport type { InternalScopeString, InternalScopesObject } from '../scope/types';\nimport { parseScopeString } from '../scope/types';\n\n/*\n *\n *\n * EVM SPECIFIC GETTERS AND SETTERS\n *\n *\n */\n\n/**\n *\n * Checks if a scope string is either an EIP155 or wallet namespaced scope string.\n *\n * @param scopeString - The scope string to check.\n * @returns True if the scope string is an EIP155 or wallet namespaced scope string, false otherwise.\n */\nconst isEip155ScopeString = (scopeString: InternalScopeString) => {\n const { namespace } = parseScopeString(scopeString);\n\n return (\n namespace === KnownCaipNamespace.Eip155 ||\n // We are trying to discern the type of `scopeString`.\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n scopeString === KnownWalletScopeString.Eip155\n );\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) accounts from internal scopes.\n *\n * @param scopes - The internal scopes from which to get the Ethereum accounts.\n * @returns An array of Ethereum accounts.\n */\nconst getEthAccountsFromScopes = (scopes: InternalScopesObject) => {\n const ethAccounts: Hex[] = [];\n\n Object.entries(scopes).forEach(([_, { accounts }]) => {\n accounts?.forEach((account) => {\n const { address, chainId } = parseCaipAccountId(account);\n\n if (isEip155ScopeString(chainId)) {\n // This address should always be a valid Hex string because\n // it's an EIP155/Ethereum account\n assertIsStrictHexString(address);\n ethAccounts.push(address);\n }\n });\n });\n\n return ethAccounts;\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to get the Ethereum accounts from.\n * @returns An array of Ethereum accounts.\n */\nexport const getEthAccounts = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n): Hex[] => {\n const { requiredScopes, optionalScopes } = caip25CaveatValue;\n\n const ethAccounts: Hex[] = [\n ...getEthAccountsFromScopes(requiredScopes),\n ...getEthAccountsFromScopes(optionalScopes),\n ];\n\n return getUniqueArrayItems(ethAccounts);\n};\n\n/**\n * Sets the Ethereum (EIP155 namespaced) accounts for the given scopes object.\n *\n * @param scopesObject - The scopes object to set the Ethereum accounts for.\n * @param accounts - The Ethereum accounts to set.\n * @returns The updated scopes object with the Ethereum accounts set.\n */\nconst setEthAccountsForScopesObject = (\n scopesObject: InternalScopesObject,\n accounts: Hex[],\n) => {\n const updatedScopesObject: InternalScopesObject = {};\n Object.entries(scopesObject).forEach(([key, scopeObject]) => {\n // Cast needed because index type is returned as `string` by `Object.entries`\n const scopeString = key as keyof typeof scopesObject;\n const isWalletNamespace = scopeString === KnownCaipNamespace.Wallet;\n const { namespace, reference } = parseScopeString(scopeString);\n if (!isEip155ScopeString(scopeString) && !isWalletNamespace) {\n updatedScopesObject[scopeString] = scopeObject;\n return;\n }\n\n let caipAccounts: CaipAccountId[] = [];\n if (namespace && reference) {\n caipAccounts = accounts.map<CaipAccountId>(\n (account) => `${namespace}:${reference}:${account}`,\n );\n }\n\n updatedScopesObject[scopeString] = {\n ...scopeObject,\n accounts: caipAccounts,\n };\n });\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the Ethereum (EIP155 namespaced) accounts for the given CAIP-25 caveat value.\n * We set the same accounts for all the scopes that are EIP155 or Wallet namespaced because\n * we do not provide UI/UX flows for selecting different accounts across different chains.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the Ethereum accounts for.\n * @param accounts - The Ethereum accounts to set.\n * @returns The updated CAIP-25 caveat value with the Ethereum accounts set.\n */\nexport const setEthAccounts = (\n caip25CaveatValue: Caip25CaveatValue,\n accounts: Hex[],\n): Caip25CaveatValue => {\n return {\n ...caip25CaveatValue,\n requiredScopes: setEthAccountsForScopesObject(\n caip25CaveatValue.requiredScopes,\n accounts,\n ),\n optionalScopes: setEthAccountsForScopesObject(\n caip25CaveatValue.optionalScopes,\n accounts,\n ),\n };\n};\n\n/*\n *\n *\n * GENERALIZED GETTERS AND SETTERS\n *\n *\n */\n\n/**\n *\n * Getters\n *\n */\n\n/**\n * Gets all accounts from an array of scopes objects\n * This extracts all account IDs from both required and optional scopes\n * and returns a unique set.\n *\n * @param scopesObjects - The scopes objects to extract accounts from\n * @returns Array of unique account IDs\n */\nexport function getCaipAccountIdsFromScopesObjects(\n scopesObjects: InternalScopesObject[],\n): CaipAccountId[] {\n const allAccounts = new Set<CaipAccountId>();\n\n for (const scopeObject of scopesObjects) {\n for (const { accounts } of Object.values(scopeObject)) {\n for (const account of accounts) {\n allAccounts.add(account);\n }\n }\n }\n\n return Array.from(allAccounts);\n}\n\n/**\n * Gets all permitted accounts from a CAIP-25 caveat\n * This extracts all account IDs from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to extract accounts from\n * @returns Array of unique account IDs\n */\nexport function getCaipAccountIdsFromCaip25CaveatValue(\n caip25CaveatValue: Caip25CaveatValue,\n): CaipAccountId[] {\n return getCaipAccountIdsFromScopesObjects([\n caip25CaveatValue.requiredScopes,\n caip25CaveatValue.optionalScopes,\n ]);\n}\n\n/**\n *\n * Setters\n *\n */\n\n/**\n * Sets the CAIP account IDs to scopes with matching namespaces in the given scopes object.\n * This function should not be used with Smart Contract Accounts (SCA) because\n * it adds the same account ID to all the scopes that have the same namespace.\n *\n * @param scopesObject - The scopes object to set the CAIP account IDs for.\n * @param accounts - The CAIP account IDs to add to the appropriate scopes.\n * @returns The updated scopes object with the CAIP account IDs set.\n */\nconst setNonSCACaipAccountIdsInScopesObject = (\n scopesObject: InternalScopesObject,\n accounts: CaipAccountId[],\n) => {\n const accountsByNamespace = new Map<string, Set<string>>();\n\n for (const account of accounts) {\n const {\n chain: { namespace },\n address,\n } = parseCaipAccountId(account);\n\n if (!accountsByNamespace.has(namespace)) {\n accountsByNamespace.set(namespace, new Set());\n }\n\n accountsByNamespace.get(namespace)?.add(address);\n }\n\n const updatedScopesObject: InternalScopesObject = {};\n\n for (const [scopeString, scopeObject] of Object.entries(scopesObject)) {\n const { namespace, reference } = parseScopeString(scopeString as string);\n\n let caipAccounts: CaipAccountId[] = [];\n\n if (namespace && reference && accountsByNamespace.has(namespace)) {\n const addressSet = accountsByNamespace.get(namespace);\n if (addressSet) {\n caipAccounts = Array.from(addressSet).map(\n (address) => `${namespace}:${reference}:${address}` as CaipAccountId,\n );\n }\n }\n\n updatedScopesObject[scopeString as keyof typeof scopesObject] = {\n ...scopeObject,\n accounts: getUniqueArrayItems(caipAccounts),\n };\n }\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the permitted accounts to scopes with matching namespaces in the given CAIP-25 caveat value.\n * This function should not be used with Smart Contract Accounts (SCA) because\n * it adds the same account ID to all scopes that have the same namespace as the account.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.\n * @param accounts - The permitted accounts to add to the appropriate scopes.\n * @returns The updated CAIP-25 caveat value with the permitted accounts set.\n */\nexport const setNonSCACaipAccountIdsInCaip25CaveatValue = (\n caip25CaveatValue: Caip25CaveatValue,\n accounts: CaipAccountId[],\n): Caip25CaveatValue => {\n return {\n ...caip25CaveatValue,\n requiredScopes: setNonSCACaipAccountIdsInScopesObject(\n caip25CaveatValue.requiredScopes,\n accounts,\n ),\n optionalScopes: setNonSCACaipAccountIdsInScopesObject(\n caip25CaveatValue.optionalScopes,\n accounts,\n ),\n };\n};\n\n/**\n * Checks if an address and list of parsed scopes are connected to any of\n * the permitted accounts based on scope matching\n *\n * @param address - The CAIP account address to check against permitted accounts\n * @param parsedAccountScopes - The list of parsed CAIP chain ID to check against permitted accounts\n * @param permittedAccounts - Array of CAIP account IDs that are permitted\n * @returns True if the address and any account scope is connected to any permitted account\n */\nfunction isAddressWithParsedScopesInPermittedAccountIds(\n address: CaipAccountAddress,\n parsedAccountScopes: {\n namespace?: CaipNamespace;\n reference?: CaipReference;\n }[],\n permittedAccounts: CaipAccountId[],\n) {\n if (!address || !parsedAccountScopes.length || !permittedAccounts.length) {\n return false;\n }\n\n return permittedAccounts.some((account) => {\n const parsedPermittedAccount = parseCaipAccountId(account);\n\n return parsedAccountScopes.some(({ namespace, reference }) => {\n if (\n namespace !== parsedPermittedAccount.chain.namespace ||\n address !== parsedPermittedAccount.address\n ) {\n return false;\n }\n\n return (\n reference === '0' ||\n reference === parsedPermittedAccount.chain.reference\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":";;;A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AC,CAAC;AAEF;;;;;;GAMG;AACI,MAAM,oBAAoB,GAAG,CAClC,iBAAoC,EACpC,QAAyB,EACN,EAAE;IACrB,OAAO;QACL,GAAG,iBAAiB;QACpB,cAAc,EAAE,mCAAmC,CACjD,iBAAiB,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
|
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":"AAAA,OAAO,
|
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
|
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":"AAAA,OAAO,
|
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"}
|
@@ -2,15 +2,7 @@ import { assertIsStrictHexString, KnownCaipNamespace, parseCaipAccountId } from
|
|
2
2
|
import { KnownWalletScopeString } from "../scope/constants.mjs";
|
3
3
|
import { getUniqueArrayItems } from "../scope/transform.mjs";
|
4
4
|
import { parseScopeString } from "../scope/types.mjs";
|
5
|
-
/*
|
6
|
-
*
|
7
|
-
*
|
8
|
-
* EVM SPECIFIC GETTERS AND SETTERS
|
9
|
-
*
|
10
|
-
*
|
11
|
-
*/
|
12
5
|
/**
|
13
|
-
*
|
14
6
|
* Checks if a scope string is either an EIP155 or wallet namespaced scope string.
|
15
7
|
*
|
16
8
|
* @param scopeString - The scope string to check.
|
@@ -103,156 +95,51 @@ export const setEthAccounts = (caip25CaveatValue, accounts) => {
|
|
103
95
|
optionalScopes: setEthAccountsForScopesObject(caip25CaveatValue.optionalScopes, accounts),
|
104
96
|
};
|
105
97
|
};
|
106
|
-
/*
|
107
|
-
*
|
108
|
-
*
|
109
|
-
* GENERALIZED GETTERS AND SETTERS
|
110
|
-
*
|
111
|
-
*
|
112
|
-
*/
|
113
|
-
/**
|
114
|
-
*
|
115
|
-
* Getters
|
116
|
-
*
|
117
|
-
*/
|
118
|
-
/**
|
119
|
-
* Gets all accounts from an array of scopes objects
|
120
|
-
* This extracts all account IDs from both required and optional scopes
|
121
|
-
* and returns a unique set.
|
122
|
-
*
|
123
|
-
* @param scopesObjects - The scopes objects to extract accounts from
|
124
|
-
* @returns Array of unique account IDs
|
125
|
-
*/
|
126
|
-
export function getCaipAccountIdsFromScopesObjects(scopesObjects) {
|
127
|
-
const allAccounts = new Set();
|
128
|
-
for (const scopeObject of scopesObjects) {
|
129
|
-
for (const { accounts } of Object.values(scopeObject)) {
|
130
|
-
for (const account of accounts) {
|
131
|
-
allAccounts.add(account);
|
132
|
-
}
|
133
|
-
}
|
134
|
-
}
|
135
|
-
return Array.from(allAccounts);
|
136
|
-
}
|
137
|
-
/**
|
138
|
-
* Gets all permitted accounts from a CAIP-25 caveat
|
139
|
-
* This extracts all account IDs from both required and optional scopes
|
140
|
-
* and returns a unique set.
|
141
|
-
*
|
142
|
-
* @param caip25CaveatValue - The CAIP-25 caveat value to extract accounts from
|
143
|
-
* @returns Array of unique account IDs
|
144
|
-
*/
|
145
|
-
export function getCaipAccountIdsFromCaip25CaveatValue(caip25CaveatValue) {
|
146
|
-
return getCaipAccountIdsFromScopesObjects([
|
147
|
-
caip25CaveatValue.requiredScopes,
|
148
|
-
caip25CaveatValue.optionalScopes,
|
149
|
-
]);
|
150
|
-
}
|
151
|
-
/**
|
152
|
-
*
|
153
|
-
* Setters
|
154
|
-
*
|
155
|
-
*/
|
156
98
|
/**
|
157
|
-
* Sets the
|
158
|
-
* This function should not be used with Smart Contract Accounts (SCA) because
|
159
|
-
* it adds the same account ID to all the scopes that have the same namespace.
|
99
|
+
* Sets the permitted accounts to scopes with matching namespaces in the given scopes object.
|
160
100
|
*
|
161
|
-
* @param scopesObject - The scopes object to set the
|
162
|
-
* @param accounts - The
|
163
|
-
* @returns The updated scopes object with the
|
101
|
+
* @param scopesObject - The scopes object to set the permitted accounts for.
|
102
|
+
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
103
|
+
* @returns The updated scopes object with the permitted accounts set.
|
164
104
|
*/
|
165
|
-
const
|
166
|
-
const accountsByNamespace = new Map();
|
167
|
-
for (const account of accounts) {
|
168
|
-
const { chain: { namespace }, address, } = parseCaipAccountId(account);
|
169
|
-
if (!accountsByNamespace.has(namespace)) {
|
170
|
-
accountsByNamespace.set(namespace, new Set());
|
171
|
-
}
|
172
|
-
accountsByNamespace.get(namespace)?.add(address);
|
173
|
-
}
|
105
|
+
const setPermittedAccountsForScopesObject = (scopesObject, accounts) => {
|
174
106
|
const updatedScopesObject = {};
|
175
|
-
|
107
|
+
Object.entries(scopesObject).forEach(([key, scopeObject]) => {
|
108
|
+
// Cast needed because index type is returned as `string` by `Object.entries`
|
109
|
+
const scopeString = key;
|
176
110
|
const { namespace, reference } = parseScopeString(scopeString);
|
177
111
|
let caipAccounts = [];
|
178
|
-
if (namespace && reference
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
112
|
+
if (namespace && reference) {
|
113
|
+
caipAccounts = accounts.reduce((acc, account) => {
|
114
|
+
const { chain: { namespace: accountNamespace }, address: accountAddress, } = parseCaipAccountId(account);
|
115
|
+
// If the account namespace is the same as the scope namespace, add the account to the scope
|
116
|
+
// This will, for example, distribute all EIP155 accounts, regardless of reference, to all EIP155 scopes
|
117
|
+
if (namespace === accountNamespace) {
|
118
|
+
acc.push(`${namespace}:${reference}:${accountAddress}`);
|
119
|
+
}
|
120
|
+
return acc;
|
121
|
+
}, []);
|
183
122
|
}
|
123
|
+
const uniqueCaipAccounts = getUniqueArrayItems(caipAccounts);
|
184
124
|
updatedScopesObject[scopeString] = {
|
185
125
|
...scopeObject,
|
186
|
-
accounts:
|
126
|
+
accounts: uniqueCaipAccounts,
|
187
127
|
};
|
188
|
-
}
|
128
|
+
});
|
189
129
|
return updatedScopesObject;
|
190
130
|
};
|
191
131
|
/**
|
192
132
|
* Sets the permitted accounts to scopes with matching namespaces in the given CAIP-25 caveat value.
|
193
|
-
* This function should not be used with Smart Contract Accounts (SCA) because
|
194
|
-
* it adds the same account ID to all scopes that have the same namespace as the account.
|
195
133
|
*
|
196
134
|
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
|
197
135
|
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
198
136
|
* @returns The updated CAIP-25 caveat value with the permitted accounts set.
|
199
137
|
*/
|
200
|
-
export const
|
138
|
+
export const setPermittedAccounts = (caip25CaveatValue, accounts) => {
|
201
139
|
return {
|
202
140
|
...caip25CaveatValue,
|
203
|
-
requiredScopes:
|
204
|
-
optionalScopes:
|
141
|
+
requiredScopes: setPermittedAccountsForScopesObject(caip25CaveatValue.requiredScopes, accounts),
|
142
|
+
optionalScopes: setPermittedAccountsForScopesObject(caip25CaveatValue.optionalScopes, accounts),
|
205
143
|
};
|
206
144
|
};
|
207
|
-
/**
|
208
|
-
* Checks if an address and list of parsed scopes are connected to any of
|
209
|
-
* the permitted accounts based on scope matching
|
210
|
-
*
|
211
|
-
* @param address - The CAIP account address to check against permitted accounts
|
212
|
-
* @param parsedAccountScopes - The list of parsed CAIP chain ID to check against permitted accounts
|
213
|
-
* @param permittedAccounts - Array of CAIP account IDs that are permitted
|
214
|
-
* @returns True if the address and any account scope is connected to any permitted account
|
215
|
-
*/
|
216
|
-
function isAddressWithParsedScopesInPermittedAccountIds(address, parsedAccountScopes, permittedAccounts) {
|
217
|
-
if (!address || !parsedAccountScopes.length || !permittedAccounts.length) {
|
218
|
-
return false;
|
219
|
-
}
|
220
|
-
return permittedAccounts.some((account) => {
|
221
|
-
const parsedPermittedAccount = parseCaipAccountId(account);
|
222
|
-
return parsedAccountScopes.some(({ namespace, reference }) => {
|
223
|
-
if (namespace !== parsedPermittedAccount.chain.namespace ||
|
224
|
-
address !== parsedPermittedAccount.address) {
|
225
|
-
return false;
|
226
|
-
}
|
227
|
-
return (reference === '0' ||
|
228
|
-
reference === parsedPermittedAccount.chain.reference);
|
229
|
-
});
|
230
|
-
});
|
231
|
-
}
|
232
|
-
/**
|
233
|
-
* Checks if an internal account is connected to any of the permitted accounts
|
234
|
-
* based on scope matching
|
235
|
-
*
|
236
|
-
* @param internalAccount - The internal account to check against permitted accounts
|
237
|
-
* @param permittedAccounts - Array of CAIP account IDs that are permitted
|
238
|
-
* @returns True if the account is connected to any permitted account
|
239
|
-
*/
|
240
|
-
export function isInternalAccountInPermittedAccountIds(internalAccount, permittedAccounts) {
|
241
|
-
const parsedInteralAccountScopes = internalAccount.scopes.map((scope) => {
|
242
|
-
return parseScopeString(scope);
|
243
|
-
});
|
244
|
-
return isAddressWithParsedScopesInPermittedAccountIds(internalAccount.address, parsedInteralAccountScopes, permittedAccounts);
|
245
|
-
}
|
246
|
-
/**
|
247
|
-
* Checks if an CAIP account ID is connected to any of the permitted accounts
|
248
|
-
* based on scope matching
|
249
|
-
*
|
250
|
-
* @param accountId - The CAIP account ID to check against permitted accounts
|
251
|
-
* @param permittedAccounts - Array of CAIP account IDs that are permitted
|
252
|
-
* @returns True if the account is connected to any permitted account
|
253
|
-
*/
|
254
|
-
export function isCaipAccountIdInPermittedAccountIds(accountId, permittedAccounts) {
|
255
|
-
const { address, chain } = parseCaipAccountId(accountId);
|
256
|
-
return isAddressWithParsedScopesInPermittedAccountIds(address, [chain], permittedAccounts);
|
257
|
-
}
|
258
145
|
//# sourceMappingURL=caip-permission-adapter-accounts.mjs.map
|