@metamask-previews/chain-agnostic-permission 0.4.0-preview-4f38c5b → 0.4.0-preview-514219da
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 +8 -0
- package/dist/adapters/caip-permission-adapter-accounts.cjs +143 -26
- package/dist/adapters/caip-permission-adapter-accounts.cjs.map +1 -1
- package/dist/adapters/caip-permission-adapter-accounts.d.cts +46 -1
- package/dist/adapters/caip-permission-adapter-accounts.d.cts.map +1 -1
- package/dist/adapters/caip-permission-adapter-accounts.d.mts +46 -1
- package/dist/adapters/caip-permission-adapter-accounts.d.mts.map +1 -1
- package/dist/adapters/caip-permission-adapter-accounts.mjs +137 -24
- package/dist/adapters/caip-permission-adapter-accounts.mjs.map +1 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.cjs +125 -31
- package/dist/adapters/caip-permission-adapter-permittedChains.cjs.map +1 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.d.cts +45 -4
- package/dist/adapters/caip-permission-adapter-permittedChains.d.cts.map +1 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.d.mts +45 -4
- package/dist/adapters/caip-permission-adapter-permittedChains.d.mts.map +1 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.mjs +118 -28
- package/dist/adapters/caip-permission-adapter-permittedChains.mjs.map +1 -1
- package/dist/caip25Permission.cjs +14 -3
- package/dist/caip25Permission.cjs.map +1 -1
- package/dist/caip25Permission.d.cts +16 -0
- package/dist/caip25Permission.d.cts.map +1 -1
- package/dist/caip25Permission.d.mts +16 -0
- package/dist/caip25Permission.d.mts.map +1 -1
- package/dist/caip25Permission.mjs +14 -4
- package/dist/caip25Permission.mjs.map +1 -1
- package/dist/index.cjs +15 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -7
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +6 -7
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +6 -7
- package/dist/index.mjs.map +1 -1
- package/dist/scope/authorization.cjs +16 -1
- package/dist/scope/authorization.cjs.map +1 -1
- package/dist/scope/authorization.d.cts +9 -1
- package/dist/scope/authorization.d.cts.map +1 -1
- package/dist/scope/authorization.d.mts +9 -1
- package/dist/scope/authorization.d.mts.map +1 -1
- package/dist/scope/authorization.mjs +14 -0
- package/dist/scope/authorization.mjs.map +1 -1
- package/dist/scope/constants.cjs +11 -1
- package/dist/scope/constants.cjs.map +1 -1
- package/dist/scope/constants.d.cts +7 -0
- package/dist/scope/constants.d.cts.map +1 -1
- package/dist/scope/constants.d.mts +7 -0
- package/dist/scope/constants.d.mts.map +1 -1
- package/dist/scope/constants.mjs +9 -0
- package/dist/scope/constants.mjs.map +1 -1
- package/dist/scope/filter.cjs +1 -29
- package/dist/scope/filter.cjs.map +1 -1
- package/dist/scope/filter.d.cts +2 -18
- package/dist/scope/filter.d.cts.map +1 -1
- package/dist/scope/filter.d.mts +2 -18
- package/dist/scope/filter.d.mts.map +1 -1
- package/dist/scope/filter.mjs +0 -26
- package/dist/scope/filter.mjs.map +1 -1
- package/dist/scope/validation.cjs +1 -12
- package/dist/scope/validation.cjs.map +1 -1
- package/dist/scope/validation.d.cts +0 -8
- package/dist/scope/validation.d.cts.map +1 -1
- package/dist/scope/validation.d.mts +0 -8
- package/dist/scope/validation.d.mts.map +1 -1
- package/dist/scope/validation.mjs +0 -10
- package/dist/scope/validation.mjs.map +1 -1
- package/package.json +2 -1
package/CHANGELOG.md
CHANGED
@@ -18,9 +18,17 @@ 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`.
|
21
23
|
- Bump `@metamask/controller-utils` to `^11.7.0` ([#5583](https://github.com/MetaMask/core/pull/5583))
|
22
24
|
- Bump `@metamask/network-controller` to `^23.2.0` ([#5583](https://github.com/MetaMask/core/pull/5583))
|
23
25
|
|
26
|
+
### Added
|
27
|
+
|
28
|
+
- Added `getCaipAccountIdsFromScopesObject`, `getCaipAccountIdsFromScopesObjects`, and `getCaipAccountIdsFromCaip25CaveatValue` functions.
|
29
|
+
- Added `getAllScopesFromScopesObjects`, `getAllScopesFromCaip25CaveatValue`, `getAllNonWalletNamespacesFromCaip25CaveatValue`, and `getAllScopesFromPermission`
|
30
|
+
functions.
|
31
|
+
|
24
32
|
## [0.3.0]
|
25
33
|
|
26
34
|
### Added
|
@@ -1,11 +1,19 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.isCaipAccountIdInPermittedAccountIds = exports.isInternalAccountInPermittedAccountIds = exports.setNonSCACaipAccountIdsInCaip25CaveatValue = exports.getCaipAccountIdsFromCaip25CaveatValue = exports.getCaipAccountIdsFromScopesObjects = 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
|
+
*/
|
8
15
|
/**
|
16
|
+
*
|
9
17
|
* Checks if a scope string is either an EIP155 or wallet namespaced scope string.
|
10
18
|
*
|
11
19
|
* @param scopeString - The scope string to check.
|
@@ -100,52 +108,161 @@ const setEthAccounts = (caip25CaveatValue, accounts) => {
|
|
100
108
|
};
|
101
109
|
};
|
102
110
|
exports.setEthAccounts = setEthAccounts;
|
111
|
+
/*
|
112
|
+
*
|
113
|
+
*
|
114
|
+
* GENERALIZED GETTERS AND SETTERS
|
115
|
+
*
|
116
|
+
*
|
117
|
+
*/
|
103
118
|
/**
|
104
|
-
* Sets the permitted accounts to scopes with matching namespaces in the given scopes object.
|
105
119
|
*
|
106
|
-
*
|
107
|
-
*
|
108
|
-
|
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
|
+
/**
|
164
|
+
* Sets the CAIP account IDs to scopes with matching namespaces in the given scopes object.
|
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.
|
167
|
+
*
|
168
|
+
* @param scopesObject - The scopes object to set the CAIP account IDs for.
|
169
|
+
* @param accounts - The CAIP account IDs to add to the appropriate scopes.
|
170
|
+
* @returns The updated scopes object with the CAIP account IDs set.
|
109
171
|
*/
|
110
|
-
const
|
172
|
+
const setNonSCACaipAccountIdsInScopesObject = (scopesObject, accounts) => {
|
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
|
+
}
|
111
181
|
const updatedScopesObject = {};
|
112
|
-
|
113
|
-
// Cast needed because index type is returned as `string` by `Object.entries`
|
114
|
-
const scopeString = key;
|
182
|
+
for (const [scopeString, scopeObject] of Object.entries(scopesObject)) {
|
115
183
|
const { namespace, reference } = (0, types_1.parseScopeString)(scopeString);
|
116
184
|
let caipAccounts = [];
|
117
|
-
if (namespace && reference) {
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
if (namespace === accountNamespace) {
|
123
|
-
acc.push(`${namespace}:${reference}:${accountAddress}`);
|
124
|
-
}
|
125
|
-
return acc;
|
126
|
-
}, []);
|
185
|
+
if (namespace && reference && accountsByNamespace.has(namespace)) {
|
186
|
+
const addressSet = accountsByNamespace.get(namespace);
|
187
|
+
if (addressSet) {
|
188
|
+
caipAccounts = Array.from(addressSet).map((address) => `${namespace}:${reference}:${address}`);
|
189
|
+
}
|
127
190
|
}
|
128
|
-
const uniqueCaipAccounts = (0, transform_1.getUniqueArrayItems)(caipAccounts);
|
129
191
|
updatedScopesObject[scopeString] = {
|
130
192
|
...scopeObject,
|
131
|
-
accounts:
|
193
|
+
accounts: (0, transform_1.getUniqueArrayItems)(caipAccounts),
|
132
194
|
};
|
133
|
-
}
|
195
|
+
}
|
134
196
|
return updatedScopesObject;
|
135
197
|
};
|
136
198
|
/**
|
137
199
|
* 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.
|
138
202
|
*
|
139
203
|
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
|
140
204
|
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
141
205
|
* @returns The updated CAIP-25 caveat value with the permitted accounts set.
|
142
206
|
*/
|
143
|
-
const
|
207
|
+
const setNonSCACaipAccountIdsInCaip25CaveatValue = (caip25CaveatValue, accounts) => {
|
144
208
|
return {
|
145
209
|
...caip25CaveatValue,
|
146
|
-
requiredScopes:
|
147
|
-
optionalScopes:
|
210
|
+
requiredScopes: setNonSCACaipAccountIdsInScopesObject(caip25CaveatValue.requiredScopes, accounts),
|
211
|
+
optionalScopes: setNonSCACaipAccountIdsInScopesObject(caip25CaveatValue.optionalScopes, accounts),
|
148
212
|
};
|
149
213
|
};
|
150
|
-
exports.
|
214
|
+
exports.setNonSCACaipAccountIdsInCaip25CaveatValue = setNonSCACaipAccountIdsInCaip25CaveatValue;
|
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;
|
151
268
|
//# sourceMappingURL=caip-permission-adapter-accounts.cjs.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"caip-permission-adapter-accounts.cjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-accounts.ts"],"names":[],"mappings":";;;AAAA,2CAMyB;AAGzB,sDAA4D;AAC5D,sDAAyD;AAEzD,8CAAkD;AAElD;;;;;GAKG;AACH,MAAM,mBAAmB,GAAG,CAAC,WAAgC,EAAE,EAAE;IAC/D,MAAM,EAAE,SAAS,EAAE,GAAG,IAAA,wBAAgB,EAAC,WAAW,CAAC,CAAC;IAEpD,OAAO,CACL,SAAS,KAAK,0BAAkB,CAAC,MAAM;QACvC,sDAAsD;QACtD,wEAAwE;QACxE,WAAW,KAAK,kCAAsB,CAAC,MAAM,CAC9C,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,wBAAwB,GAAG,CAAC,MAA4B,EAAE,EAAE;IAChE,MAAM,WAAW,GAAU,EAAE,CAAC;IAE9B,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,EAAE;QACnD,QAAQ,EAAE,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;YAC5B,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,GAAG,IAAA,0BAAkB,EAAC,OAAO,CAAC,CAAC;YAEzD,IAAI,mBAAmB,CAAC,OAAO,CAAC,EAAE;gBAChC,2DAA2D;gBAC3D,kCAAkC;gBAClC,IAAA,+BAAuB,EAAC,OAAO,CAAC,CAAC;gBACjC,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;aAC3B;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAEF;;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C,CAAC;SACR;QAED,MAAM,kBAAkB,GAAG,IAAA,+BAAmB,EAAC,YAAY,CAAC,CAAC;QAE7D,mBAAmB,CAAC,WAAW,CAAC,GAAG;YACjC,GAAG,WAAW;YACd,QAAQ,EAAE,kBAAkB;SAC7B,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,OAAO,mBAAmB,CAAC;AAC7B,CAAC,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
|
+
{"version":3,"file":"caip-permission-adapter-accounts.cjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-accounts.ts"],"names":[],"mappings":";;;AACA,2CASyB;AAGzB,sDAA4D;AAC5D,sDAAyD;AAEzD,8CAAkD;AAElD;;;;;;GAMG;AAEH;;;;;;GAMG;AACH,MAAM,mBAAmB,GAAG,CAAC,WAAgC,EAAE,EAAE;IAC/D,MAAM,EAAE,SAAS,EAAE,GAAG,IAAA,wBAAgB,EAAC,WAAW,CAAC,CAAC;IAEpD,OAAO,CACL,SAAS,KAAK,0BAAkB,CAAC,MAAM;QACvC,sDAAsD;QACtD,wEAAwE;QACxE,WAAW,KAAK,kCAAsB,CAAC,MAAM,CAC9C,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,wBAAwB,GAAG,CAAC,MAA4B,EAAE,EAAE;IAChE,MAAM,WAAW,GAAU,EAAE,CAAC;IAE9B,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,QAAQ,EAAE,CAAC,EAAE,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AC;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 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,5 +1,7 @@
|
|
1
|
+
import type { InternalAccount } from "@metamask/keyring-internal-api";
|
1
2
|
import { type CaipAccountId, type Hex } from "@metamask/utils";
|
2
3
|
import type { Caip25CaveatValue } from "../caip25Permission.cjs";
|
4
|
+
import type { InternalScopesObject } from "../scope/types.cjs";
|
3
5
|
/**
|
4
6
|
* Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.
|
5
7
|
*
|
@@ -17,12 +19,55 @@ export declare const getEthAccounts: (caip25CaveatValue: Pick<Caip25CaveatValue,
|
|
17
19
|
* @returns The updated CAIP-25 caveat value with the Ethereum accounts set.
|
18
20
|
*/
|
19
21
|
export declare const setEthAccounts: (caip25CaveatValue: Caip25CaveatValue, accounts: Hex[]) => Caip25CaveatValue;
|
22
|
+
/**
|
23
|
+
*
|
24
|
+
* Getters
|
25
|
+
*
|
26
|
+
*/
|
27
|
+
/**
|
28
|
+
* Gets all accounts from an array of scopes objects
|
29
|
+
* This extracts all account IDs from both required and optional scopes
|
30
|
+
* and returns a unique set.
|
31
|
+
*
|
32
|
+
* @param scopesObjects - The scopes objects to extract accounts from
|
33
|
+
* @returns Array of unique account IDs
|
34
|
+
*/
|
35
|
+
export declare function getCaipAccountIdsFromScopesObjects(scopesObjects: InternalScopesObject[]): CaipAccountId[];
|
36
|
+
/**
|
37
|
+
* Gets all permitted accounts from a CAIP-25 caveat
|
38
|
+
* This extracts all account IDs from both required and optional scopes
|
39
|
+
* and returns a unique set.
|
40
|
+
*
|
41
|
+
* @param caip25CaveatValue - The CAIP-25 caveat value to extract accounts from
|
42
|
+
* @returns Array of unique account IDs
|
43
|
+
*/
|
44
|
+
export declare function getCaipAccountIdsFromCaip25CaveatValue(caip25CaveatValue: Caip25CaveatValue): CaipAccountId[];
|
20
45
|
/**
|
21
46
|
* Sets the permitted accounts to scopes with matching namespaces in the given CAIP-25 caveat value.
|
47
|
+
* This function should not be used with Smart Contract Accounts (SCA) because
|
48
|
+
* it adds the same account ID to all scopes that have the same namespace as the account.
|
22
49
|
*
|
23
50
|
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
|
24
51
|
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
25
52
|
* @returns The updated CAIP-25 caveat value with the permitted accounts set.
|
26
53
|
*/
|
27
|
-
export declare const
|
54
|
+
export declare const setNonSCACaipAccountIdsInCaip25CaveatValue: (caip25CaveatValue: Caip25CaveatValue, accounts: CaipAccountId[]) => Caip25CaveatValue;
|
55
|
+
/**
|
56
|
+
* Checks if an internal account is connected to any of the permitted accounts
|
57
|
+
* based on scope matching
|
58
|
+
*
|
59
|
+
* @param internalAccount - The internal account to check against permitted accounts
|
60
|
+
* @param permittedAccounts - Array of CAIP account IDs that are permitted
|
61
|
+
* @returns True if the account is connected to any permitted account
|
62
|
+
*/
|
63
|
+
export declare function isInternalAccountInPermittedAccountIds(internalAccount: InternalAccount, permittedAccounts: CaipAccountId[]): boolean;
|
64
|
+
/**
|
65
|
+
* Checks if an CAIP account ID is connected to any of the permitted accounts
|
66
|
+
* based on scope matching
|
67
|
+
*
|
68
|
+
* @param accountId - The CAIP account ID to check against permitted accounts
|
69
|
+
* @param permittedAccounts - Array of CAIP account IDs that are permitted
|
70
|
+
* @returns True if the account is connected to any permitted account
|
71
|
+
*/
|
72
|
+
export declare function isCaipAccountIdInPermittedAccountIds(accountId: CaipAccountId, permittedAccounts: CaipAccountId[]): boolean;
|
28
73
|
//# sourceMappingURL=caip-permission-adapter-accounts.d.cts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"caip-permission-adapter-accounts.d.cts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-accounts.ts"],"names":[],"mappings":"AAAA,OAAO,
|
1
|
+
{"version":3,"file":"caip-permission-adapter-accounts.d.cts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-accounts.ts"],"names":[],"mappings":"AAAA,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;AA0CF;;;;;;;GAOG;AACH,wBAAgB,sCAAsC,CACpD,eAAe,EAAE,eAAe,EAChC,iBAAiB,EAAE,aAAa,EAAE,GACjC,OAAO,CAUT;AAED;;;;;;;GAOG;AACH,wBAAgB,oCAAoC,CAClD,SAAS,EAAE,aAAa,EACxB,iBAAiB,EAAE,aAAa,EAAE,GACjC,OAAO,CAQT"}
|
@@ -1,5 +1,7 @@
|
|
1
|
+
import type { InternalAccount } from "@metamask/keyring-internal-api";
|
1
2
|
import { type CaipAccountId, type Hex } from "@metamask/utils";
|
2
3
|
import type { Caip25CaveatValue } from "../caip25Permission.mjs";
|
4
|
+
import type { InternalScopesObject } from "../scope/types.mjs";
|
3
5
|
/**
|
4
6
|
* Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.
|
5
7
|
*
|
@@ -17,12 +19,55 @@ export declare const getEthAccounts: (caip25CaveatValue: Pick<Caip25CaveatValue,
|
|
17
19
|
* @returns The updated CAIP-25 caveat value with the Ethereum accounts set.
|
18
20
|
*/
|
19
21
|
export declare const setEthAccounts: (caip25CaveatValue: Caip25CaveatValue, accounts: Hex[]) => Caip25CaveatValue;
|
22
|
+
/**
|
23
|
+
*
|
24
|
+
* Getters
|
25
|
+
*
|
26
|
+
*/
|
27
|
+
/**
|
28
|
+
* Gets all accounts from an array of scopes objects
|
29
|
+
* This extracts all account IDs from both required and optional scopes
|
30
|
+
* and returns a unique set.
|
31
|
+
*
|
32
|
+
* @param scopesObjects - The scopes objects to extract accounts from
|
33
|
+
* @returns Array of unique account IDs
|
34
|
+
*/
|
35
|
+
export declare function getCaipAccountIdsFromScopesObjects(scopesObjects: InternalScopesObject[]): CaipAccountId[];
|
36
|
+
/**
|
37
|
+
* Gets all permitted accounts from a CAIP-25 caveat
|
38
|
+
* This extracts all account IDs from both required and optional scopes
|
39
|
+
* and returns a unique set.
|
40
|
+
*
|
41
|
+
* @param caip25CaveatValue - The CAIP-25 caveat value to extract accounts from
|
42
|
+
* @returns Array of unique account IDs
|
43
|
+
*/
|
44
|
+
export declare function getCaipAccountIdsFromCaip25CaveatValue(caip25CaveatValue: Caip25CaveatValue): CaipAccountId[];
|
20
45
|
/**
|
21
46
|
* Sets the permitted accounts to scopes with matching namespaces in the given CAIP-25 caveat value.
|
47
|
+
* This function should not be used with Smart Contract Accounts (SCA) because
|
48
|
+
* it adds the same account ID to all scopes that have the same namespace as the account.
|
22
49
|
*
|
23
50
|
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
|
24
51
|
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
25
52
|
* @returns The updated CAIP-25 caveat value with the permitted accounts set.
|
26
53
|
*/
|
27
|
-
export declare const
|
54
|
+
export declare const setNonSCACaipAccountIdsInCaip25CaveatValue: (caip25CaveatValue: Caip25CaveatValue, accounts: CaipAccountId[]) => Caip25CaveatValue;
|
55
|
+
/**
|
56
|
+
* Checks if an internal account is connected to any of the permitted accounts
|
57
|
+
* based on scope matching
|
58
|
+
*
|
59
|
+
* @param internalAccount - The internal account to check against permitted accounts
|
60
|
+
* @param permittedAccounts - Array of CAIP account IDs that are permitted
|
61
|
+
* @returns True if the account is connected to any permitted account
|
62
|
+
*/
|
63
|
+
export declare function isInternalAccountInPermittedAccountIds(internalAccount: InternalAccount, permittedAccounts: CaipAccountId[]): boolean;
|
64
|
+
/**
|
65
|
+
* Checks if an CAIP account ID is connected to any of the permitted accounts
|
66
|
+
* based on scope matching
|
67
|
+
*
|
68
|
+
* @param accountId - The CAIP account ID to check against permitted accounts
|
69
|
+
* @param permittedAccounts - Array of CAIP account IDs that are permitted
|
70
|
+
* @returns True if the account is connected to any permitted account
|
71
|
+
*/
|
72
|
+
export declare function isCaipAccountIdInPermittedAccountIds(accountId: CaipAccountId, permittedAccounts: CaipAccountId[]): boolean;
|
28
73
|
//# sourceMappingURL=caip-permission-adapter-accounts.d.mts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"caip-permission-adapter-accounts.d.mts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-accounts.ts"],"names":[],"mappings":"AAAA,OAAO,
|
1
|
+
{"version":3,"file":"caip-permission-adapter-accounts.d.mts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-accounts.ts"],"names":[],"mappings":"AAAA,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;AA0CF;;;;;;;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"}
|
@@ -2,7 +2,15 @@ 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
|
+
*/
|
5
12
|
/**
|
13
|
+
*
|
6
14
|
* Checks if a scope string is either an EIP155 or wallet namespaced scope string.
|
7
15
|
*
|
8
16
|
* @param scopeString - The scope string to check.
|
@@ -95,51 +103,156 @@ export const setEthAccounts = (caip25CaveatValue, accounts) => {
|
|
95
103
|
optionalScopes: setEthAccountsForScopesObject(caip25CaveatValue.optionalScopes, accounts),
|
96
104
|
};
|
97
105
|
};
|
106
|
+
/*
|
107
|
+
*
|
108
|
+
*
|
109
|
+
* GENERALIZED GETTERS AND SETTERS
|
110
|
+
*
|
111
|
+
*
|
112
|
+
*/
|
98
113
|
/**
|
99
|
-
* Sets the permitted accounts to scopes with matching namespaces in the given scopes object.
|
100
114
|
*
|
101
|
-
*
|
102
|
-
*
|
103
|
-
|
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
|
+
/**
|
157
|
+
* Sets the CAIP account IDs to scopes with matching namespaces in the given scopes object.
|
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.
|
160
|
+
*
|
161
|
+
* @param scopesObject - The scopes object to set the CAIP account IDs for.
|
162
|
+
* @param accounts - The CAIP account IDs to add to the appropriate scopes.
|
163
|
+
* @returns The updated scopes object with the CAIP account IDs set.
|
104
164
|
*/
|
105
|
-
const
|
165
|
+
const setNonSCACaipAccountIdsInScopesObject = (scopesObject, accounts) => {
|
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
|
+
}
|
106
174
|
const updatedScopesObject = {};
|
107
|
-
|
108
|
-
// Cast needed because index type is returned as `string` by `Object.entries`
|
109
|
-
const scopeString = key;
|
175
|
+
for (const [scopeString, scopeObject] of Object.entries(scopesObject)) {
|
110
176
|
const { namespace, reference } = parseScopeString(scopeString);
|
111
177
|
let caipAccounts = [];
|
112
|
-
if (namespace && reference) {
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
if (namespace === accountNamespace) {
|
118
|
-
acc.push(`${namespace}:${reference}:${accountAddress}`);
|
119
|
-
}
|
120
|
-
return acc;
|
121
|
-
}, []);
|
178
|
+
if (namespace && reference && accountsByNamespace.has(namespace)) {
|
179
|
+
const addressSet = accountsByNamespace.get(namespace);
|
180
|
+
if (addressSet) {
|
181
|
+
caipAccounts = Array.from(addressSet).map((address) => `${namespace}:${reference}:${address}`);
|
182
|
+
}
|
122
183
|
}
|
123
|
-
const uniqueCaipAccounts = getUniqueArrayItems(caipAccounts);
|
124
184
|
updatedScopesObject[scopeString] = {
|
125
185
|
...scopeObject,
|
126
|
-
accounts:
|
186
|
+
accounts: getUniqueArrayItems(caipAccounts),
|
127
187
|
};
|
128
|
-
}
|
188
|
+
}
|
129
189
|
return updatedScopesObject;
|
130
190
|
};
|
131
191
|
/**
|
132
192
|
* 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.
|
133
195
|
*
|
134
196
|
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
|
135
197
|
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
136
198
|
* @returns The updated CAIP-25 caveat value with the permitted accounts set.
|
137
199
|
*/
|
138
|
-
export const
|
200
|
+
export const setNonSCACaipAccountIdsInCaip25CaveatValue = (caip25CaveatValue, accounts) => {
|
139
201
|
return {
|
140
202
|
...caip25CaveatValue,
|
141
|
-
requiredScopes:
|
142
|
-
optionalScopes:
|
203
|
+
requiredScopes: setNonSCACaipAccountIdsInScopesObject(caip25CaveatValue.requiredScopes, accounts),
|
204
|
+
optionalScopes: setNonSCACaipAccountIdsInScopesObject(caip25CaveatValue.optionalScopes, accounts),
|
143
205
|
};
|
144
206
|
};
|
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
|
+
}
|
145
258
|
//# sourceMappingURL=caip-permission-adapter-accounts.mjs.map
|