@metamask-previews/chain-agnostic-permission 0.4.0-preview-4bcedc0 → 0.4.0-preview-7f6d081
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -5
- package/dist/adapters/caip-permission-adapter-accounts.cjs +150 -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 +144 -24
- package/dist/adapters/caip-permission-adapter-accounts.mjs.map +1 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.cjs +127 -34
- package/dist/adapters/caip-permission-adapter-permittedChains.cjs.map +1 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.d.cts +49 -7
- package/dist/adapters/caip-permission-adapter-permittedChains.d.cts.map +1 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.d.mts +49 -7
- package/dist/adapters/caip-permission-adapter-permittedChains.d.mts.map +1 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.mjs +120 -31
- 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 +19 -0
- package/dist/caip25Permission.d.cts.map +1 -1
- package/dist/caip25Permission.d.mts +19 -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
@@ -1,8 +1,17 @@
|
|
1
|
+
import { isEqualCaseInsensitive } from "@metamask/controller-utils";
|
1
2
|
import { assertIsStrictHexString, KnownCaipNamespace, parseCaipAccountId } from "@metamask/utils";
|
2
3
|
import { KnownWalletScopeString } from "../scope/constants.mjs";
|
3
4
|
import { getUniqueArrayItems } from "../scope/transform.mjs";
|
4
5
|
import { parseScopeString } from "../scope/types.mjs";
|
6
|
+
/*
|
7
|
+
*
|
8
|
+
*
|
9
|
+
* EVM SPECIFIC GETTERS AND SETTERS
|
10
|
+
*
|
11
|
+
*
|
12
|
+
*/
|
5
13
|
/**
|
14
|
+
*
|
6
15
|
* Checks if a scope string is either an EIP155 or wallet namespaced scope string.
|
7
16
|
*
|
8
17
|
* @param scopeString - The scope string to check.
|
@@ -95,51 +104,162 @@ export const setEthAccounts = (caip25CaveatValue, accounts) => {
|
|
95
104
|
optionalScopes: setEthAccountsForScopesObject(caip25CaveatValue.optionalScopes, accounts),
|
96
105
|
};
|
97
106
|
};
|
107
|
+
/*
|
108
|
+
*
|
109
|
+
*
|
110
|
+
* GENERALIZED GETTERS AND SETTERS
|
111
|
+
*
|
112
|
+
*
|
113
|
+
*/
|
98
114
|
/**
|
99
|
-
* Sets the permitted accounts to scopes with matching namespaces in the given scopes object.
|
100
115
|
*
|
101
|
-
*
|
102
|
-
*
|
103
|
-
|
116
|
+
* Getters
|
117
|
+
*
|
118
|
+
*/
|
119
|
+
/**
|
120
|
+
* Gets all accounts from an array of scopes objects
|
121
|
+
* This extracts all account IDs from both required and optional scopes
|
122
|
+
* and returns a unique set.
|
123
|
+
*
|
124
|
+
* @param scopesObjects - The scopes objects to extract accounts from
|
125
|
+
* @returns Array of unique account IDs
|
126
|
+
*/
|
127
|
+
export function getCaipAccountIdsFromScopesObjects(scopesObjects) {
|
128
|
+
const allAccounts = new Set();
|
129
|
+
for (const scopeObject of scopesObjects) {
|
130
|
+
for (const { accounts } of Object.values(scopeObject)) {
|
131
|
+
for (const account of accounts) {
|
132
|
+
allAccounts.add(account);
|
133
|
+
}
|
134
|
+
}
|
135
|
+
}
|
136
|
+
return Array.from(allAccounts);
|
137
|
+
}
|
138
|
+
/**
|
139
|
+
* Gets all permitted accounts from a CAIP-25 caveat
|
140
|
+
* This extracts all account IDs from both required and optional scopes
|
141
|
+
* and returns a unique set.
|
142
|
+
*
|
143
|
+
* @param caip25CaveatValue - The CAIP-25 caveat value to extract accounts from
|
144
|
+
* @returns Array of unique account IDs
|
145
|
+
*/
|
146
|
+
export function getCaipAccountIdsFromCaip25CaveatValue(caip25CaveatValue) {
|
147
|
+
return getCaipAccountIdsFromScopesObjects([
|
148
|
+
caip25CaveatValue.requiredScopes,
|
149
|
+
caip25CaveatValue.optionalScopes,
|
150
|
+
]);
|
151
|
+
}
|
152
|
+
/**
|
153
|
+
*
|
154
|
+
* Setters
|
155
|
+
*
|
104
156
|
*/
|
105
|
-
|
157
|
+
/**
|
158
|
+
* Sets the CAIP account IDs to scopes with matching namespaces in the given scopes object.
|
159
|
+
* This function should not be used with Smart Contract Accounts (SCA) because
|
160
|
+
* it adds the same account ID to all the scopes that have the same namespace.
|
161
|
+
*
|
162
|
+
* @param scopesObject - The scopes object to set the CAIP account IDs for.
|
163
|
+
* @param accounts - The CAIP account IDs to add to the appropriate scopes.
|
164
|
+
* @returns The updated scopes object with the CAIP account IDs set.
|
165
|
+
*/
|
166
|
+
const setNonSCACaipAccountIdsInScopesObject = (scopesObject, accounts) => {
|
167
|
+
const accountsByNamespace = new Map();
|
168
|
+
for (const account of accounts) {
|
169
|
+
const { chain: { namespace }, address, } = parseCaipAccountId(account);
|
170
|
+
if (!accountsByNamespace.has(namespace)) {
|
171
|
+
accountsByNamespace.set(namespace, new Set());
|
172
|
+
}
|
173
|
+
accountsByNamespace.get(namespace)?.add(address);
|
174
|
+
}
|
106
175
|
const updatedScopesObject = {};
|
107
|
-
|
108
|
-
// Cast needed because index type is returned as `string` by `Object.entries`
|
109
|
-
const scopeString = key;
|
176
|
+
for (const [scopeString, scopeObject] of Object.entries(scopesObject)) {
|
110
177
|
const { namespace, reference } = parseScopeString(scopeString);
|
111
178
|
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
|
-
}, []);
|
179
|
+
if (namespace && reference && accountsByNamespace.has(namespace)) {
|
180
|
+
const addressSet = accountsByNamespace.get(namespace);
|
181
|
+
if (addressSet) {
|
182
|
+
caipAccounts = Array.from(addressSet).map((address) => `${namespace}:${reference}:${address}`);
|
183
|
+
}
|
122
184
|
}
|
123
|
-
const uniqueCaipAccounts = getUniqueArrayItems(caipAccounts);
|
124
185
|
updatedScopesObject[scopeString] = {
|
125
186
|
...scopeObject,
|
126
|
-
accounts:
|
187
|
+
accounts: getUniqueArrayItems(caipAccounts),
|
127
188
|
};
|
128
|
-
}
|
189
|
+
}
|
129
190
|
return updatedScopesObject;
|
130
191
|
};
|
131
192
|
/**
|
132
193
|
* Sets the permitted accounts to scopes with matching namespaces in the given CAIP-25 caveat value.
|
194
|
+
* This function should not be used with Smart Contract Accounts (SCA) because
|
195
|
+
* it adds the same account ID to all scopes that have the same namespace as the account.
|
133
196
|
*
|
134
197
|
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
|
135
198
|
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
136
199
|
* @returns The updated CAIP-25 caveat value with the permitted accounts set.
|
137
200
|
*/
|
138
|
-
export const
|
201
|
+
export const setNonSCACaipAccountIdsInCaip25CaveatValue = (caip25CaveatValue, accounts) => {
|
139
202
|
return {
|
140
203
|
...caip25CaveatValue,
|
141
|
-
requiredScopes:
|
142
|
-
optionalScopes:
|
204
|
+
requiredScopes: setNonSCACaipAccountIdsInScopesObject(caip25CaveatValue.requiredScopes, accounts),
|
205
|
+
optionalScopes: setNonSCACaipAccountIdsInScopesObject(caip25CaveatValue.optionalScopes, accounts),
|
143
206
|
};
|
144
207
|
};
|
208
|
+
/**
|
209
|
+
* Checks if an address and list of parsed scopes are connected to any of
|
210
|
+
* the permitted accounts based on scope matching
|
211
|
+
*
|
212
|
+
* @param address - The CAIP account address to check against permitted accounts
|
213
|
+
* @param parsedAccountScopes - The list of parsed CAIP chain ID to check against permitted accounts
|
214
|
+
* @param permittedAccounts - Array of CAIP account IDs that are permitted
|
215
|
+
* @returns True if the address and any account scope is connected to any permitted account
|
216
|
+
*/
|
217
|
+
function isAddressWithParsedScopesInPermittedAccountIds(address, parsedAccountScopes, permittedAccounts) {
|
218
|
+
if (!address || !parsedAccountScopes.length || !permittedAccounts.length) {
|
219
|
+
return false;
|
220
|
+
}
|
221
|
+
return permittedAccounts.some((account) => {
|
222
|
+
const parsedPermittedAccount = parseCaipAccountId(account);
|
223
|
+
return parsedAccountScopes.some(({ namespace, reference }) => {
|
224
|
+
if (namespace !== parsedPermittedAccount.chain.namespace) {
|
225
|
+
return false;
|
226
|
+
}
|
227
|
+
// handle eip155:0 case and insensitive evm address comparison
|
228
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
|
229
|
+
if (namespace === KnownCaipNamespace.Eip155) {
|
230
|
+
return ((reference === '0' ||
|
231
|
+
reference === parsedPermittedAccount.chain.reference) &&
|
232
|
+
isEqualCaseInsensitive(address, parsedPermittedAccount.address));
|
233
|
+
}
|
234
|
+
return (reference === parsedPermittedAccount.chain.reference &&
|
235
|
+
address === parsedPermittedAccount.address);
|
236
|
+
});
|
237
|
+
});
|
238
|
+
}
|
239
|
+
/**
|
240
|
+
* Checks if an internal account is connected to any of the permitted accounts
|
241
|
+
* based on scope matching
|
242
|
+
*
|
243
|
+
* @param internalAccount - The internal account to check against permitted accounts
|
244
|
+
* @param permittedAccounts - Array of CAIP account IDs that are permitted
|
245
|
+
* @returns True if the account is connected to any permitted account
|
246
|
+
*/
|
247
|
+
export function isInternalAccountInPermittedAccountIds(internalAccount, permittedAccounts) {
|
248
|
+
const parsedInteralAccountScopes = internalAccount.scopes.map((scope) => {
|
249
|
+
return parseScopeString(scope);
|
250
|
+
});
|
251
|
+
return isAddressWithParsedScopesInPermittedAccountIds(internalAccount.address, parsedInteralAccountScopes, permittedAccounts);
|
252
|
+
}
|
253
|
+
/**
|
254
|
+
* Checks if an CAIP account ID is connected to any of the permitted accounts
|
255
|
+
* based on scope matching
|
256
|
+
*
|
257
|
+
* @param accountId - The CAIP account ID to check against permitted accounts
|
258
|
+
* @param permittedAccounts - Array of CAIP account IDs that are permitted
|
259
|
+
* @returns True if the account is connected to any permitted account
|
260
|
+
*/
|
261
|
+
export function isCaipAccountIdInPermittedAccountIds(accountId, permittedAccounts) {
|
262
|
+
const { address, chain } = parseCaipAccountId(accountId);
|
263
|
+
return isAddressWithParsedScopesInPermittedAccountIds(address, [chain], permittedAccounts);
|
264
|
+
}
|
145
265
|
//# sourceMappingURL=caip-permission-adapter-accounts.mjs.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"caip-permission-adapter-accounts.mjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-accounts.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EAGvB,kBAAkB,EAClB,kBAAkB,EACnB,wBAAwB;AAGzB,OAAO,EAAE,sBAAsB,EAAE,+BAA2B;AAC5D,OAAO,EAAE,mBAAmB,EAAE,+BAA2B;AAEzD,OAAO,EAAE,gBAAgB,EAAE,2BAAuB;AAElD;;;;;GAKG;AACH,MAAM,mBAAmB,GAAG,CAAC,WAAgC,EAAE,EAAE;IAC/D,MAAM,EAAE,SAAS,EAAE,GAAG,gBAAgB,CAAC,WAAW,CAAC,CAAC;IAEpD,OAAO,CACL,SAAS,KAAK,kBAAkB,CAAC,MAAM;QACvC,sDAAsD;QACtD,wEAAwE;QACxE,WAAW,KAAK,sBAAsB,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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ACxB,GAAG,kBAAkB,CAAC,OAAO,CAAC,CAAC;gBAChC,4FAA4F;gBAC5F,wGAAwG;gBACxG,IAAI,SAAS,KAAK,gBAAgB,EAAE;oBAClC,GAAG,CAAC,IAAI,CAAC,GAAG,SAAS,IAAI,SAAS,IAAI,cAAc,EAAE,CAAC,CAAC;iBACzD;gBACD,OAAO,GAAG,CAAC;YACb,CAAC,EAAE,EAAE,CAAC,CAAC;SACR;QAED,MAAM,kBAAkB,GAAG,mBAAmB,CAAC,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;AACH,MAAM,CAAC,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","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.mjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-accounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,mCAAmC;AAEpE,OAAO,EACL,uBAAuB,EAMvB,kBAAkB,EAClB,kBAAkB,EACnB,wBAAwB;AAGzB,OAAO,EAAE,sBAAsB,EAAE,+BAA2B;AAC5D,OAAO,EAAE,mBAAmB,EAAE,+BAA2B;AAEzD,OAAO,EAAE,gBAAgB,EAAE,2BAAuB;AAElD;;;;;;GAMG;AAEH;;;;;;GAMG;AACH,MAAM,mBAAmB,GAAG,CAAC,WAAgC,EAAE,EAAE;IAC/D,MAAM,EAAE,SAAS,EAAE,G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,CAAC,EAAE,SAAS,EAAE,SAAS,EAAE,EAAE,EAAE;YAC3D,IAAI,SAAS,KAAK,sBAAsB,CAAC,KAAK,CAAC,SAAS,EAAE;gBACxD,OAAO,KAAK,CAAC;aACd;YAED,8DAA8D;YAC9D,wEAAwE;YACxE,IAAI,SAAS,KAAK,kBAAkB,CAAC,MAAM,EAAE;gBAC3C,OAAO,CACL,CAAC,SAAS,KAAK,GAAG;oBAChB,SAAS,KAAK,sBAAsB,CAAC,KAAK,CAAC,SAAS,CAAC;oBACvD,sBAAsB,CAAC,OAAO,EAAE,sBAAsB,CAAC,OAAO,CAAC,CAChE,CAAC;aACH;YACD,OAAO,CACL,SAAS,KAAK,sBAAsB,CAAC,KAAK,CAAC,SAAS;gBACpD,OAAO,KAAK,sBAAsB,CAAC,OAAO,CAC3C,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,sCAAsC,CACpD,eAAgC,EAChC,iBAAkC;IAElC,MAAM,0BAA0B,GAAG,eAAe,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;QACtE,OAAO,gBAAgB,CAAC,KAAK,CAAC,CAAC;IACjC,CAAC,CAAC,CAAC;IAEH,OAAO,8CAA8C,CACnD,eAAe,CAAC,OAAO,EACvB,0BAA0B,EAC1B,iBAAiB,CAClB,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,oCAAoC,CAClD,SAAwB,EACxB,iBAAkC;IAElC,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,GAAG,kBAAkB,CAAC,SAAS,CAAC,CAAC;IAEzD,OAAO,8CAA8C,CACnD,OAAO,EACP,CAAC,KAAK,CAAC,EACP,iBAAiB,CAClB,CAAC;AACJ,CAAC","sourcesContent":["import { isEqualCaseInsensitive } from '@metamask/controller-utils';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\nimport {\n assertIsStrictHexString,\n type CaipAccountAddress,\n type CaipAccountId,\n type CaipNamespace,\n type CaipReference,\n type Hex,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\n\nimport type { Caip25CaveatValue } from '../caip25Permission';\nimport { KnownWalletScopeString } from '../scope/constants';\nimport { getUniqueArrayItems } from '../scope/transform';\nimport type { InternalScopeString, InternalScopesObject } from '../scope/types';\nimport { parseScopeString } from '../scope/types';\n\n/*\n *\n *\n * EVM SPECIFIC GETTERS AND SETTERS\n *\n *\n */\n\n/**\n *\n * Checks if a scope string is either an EIP155 or wallet namespaced scope string.\n *\n * @param scopeString - The scope string to check.\n * @returns True if the scope string is an EIP155 or wallet namespaced scope string, false otherwise.\n */\nconst isEip155ScopeString = (scopeString: InternalScopeString) => {\n const { namespace } = parseScopeString(scopeString);\n\n return (\n namespace === KnownCaipNamespace.Eip155 ||\n // We are trying to discern the type of `scopeString`.\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n scopeString === KnownWalletScopeString.Eip155\n );\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) accounts from internal scopes.\n *\n * @param scopes - The internal scopes from which to get the Ethereum accounts.\n * @returns An array of Ethereum accounts.\n */\nconst getEthAccountsFromScopes = (scopes: InternalScopesObject) => {\n const ethAccounts: Hex[] = [];\n\n Object.entries(scopes).forEach(([_, { accounts }]) => {\n accounts?.forEach((account) => {\n const { address, chainId } = parseCaipAccountId(account);\n\n if (isEip155ScopeString(chainId)) {\n // This address should always be a valid Hex string because\n // it's an EIP155/Ethereum account\n assertIsStrictHexString(address);\n ethAccounts.push(address);\n }\n });\n });\n\n return ethAccounts;\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to get the Ethereum accounts from.\n * @returns An array of Ethereum accounts.\n */\nexport const getEthAccounts = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n): Hex[] => {\n const { requiredScopes, optionalScopes } = caip25CaveatValue;\n\n const ethAccounts: Hex[] = [\n ...getEthAccountsFromScopes(requiredScopes),\n ...getEthAccountsFromScopes(optionalScopes),\n ];\n\n return getUniqueArrayItems(ethAccounts);\n};\n\n/**\n * Sets the Ethereum (EIP155 namespaced) accounts for the given scopes object.\n *\n * @param scopesObject - The scopes object to set the Ethereum accounts for.\n * @param accounts - The Ethereum accounts to set.\n * @returns The updated scopes object with the Ethereum accounts set.\n */\nconst setEthAccountsForScopesObject = (\n scopesObject: InternalScopesObject,\n accounts: Hex[],\n) => {\n const updatedScopesObject: InternalScopesObject = {};\n Object.entries(scopesObject).forEach(([key, scopeObject]) => {\n // Cast needed because index type is returned as `string` by `Object.entries`\n const scopeString = key as keyof typeof scopesObject;\n const isWalletNamespace = scopeString === KnownCaipNamespace.Wallet;\n const { namespace, reference } = parseScopeString(scopeString);\n if (!isEip155ScopeString(scopeString) && !isWalletNamespace) {\n updatedScopesObject[scopeString] = scopeObject;\n return;\n }\n\n let caipAccounts: CaipAccountId[] = [];\n if (namespace && reference) {\n caipAccounts = accounts.map<CaipAccountId>(\n (account) => `${namespace}:${reference}:${account}`,\n );\n }\n\n updatedScopesObject[scopeString] = {\n ...scopeObject,\n accounts: caipAccounts,\n };\n });\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the Ethereum (EIP155 namespaced) accounts for the given CAIP-25 caveat value.\n * We set the same accounts for all the scopes that are EIP155 or Wallet namespaced because\n * we do not provide UI/UX flows for selecting different accounts across different chains.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the Ethereum accounts for.\n * @param accounts - The Ethereum accounts to set.\n * @returns The updated CAIP-25 caveat value with the Ethereum accounts set.\n */\nexport const setEthAccounts = (\n caip25CaveatValue: Caip25CaveatValue,\n accounts: Hex[],\n): Caip25CaveatValue => {\n return {\n ...caip25CaveatValue,\n requiredScopes: setEthAccountsForScopesObject(\n caip25CaveatValue.requiredScopes,\n accounts,\n ),\n optionalScopes: setEthAccountsForScopesObject(\n caip25CaveatValue.optionalScopes,\n accounts,\n ),\n };\n};\n\n/*\n *\n *\n * GENERALIZED GETTERS AND SETTERS\n *\n *\n */\n\n/**\n *\n * Getters\n *\n */\n\n/**\n * Gets all accounts from an array of scopes objects\n * This extracts all account IDs from both required and optional scopes\n * and returns a unique set.\n *\n * @param scopesObjects - The scopes objects to extract accounts from\n * @returns Array of unique account IDs\n */\nexport function getCaipAccountIdsFromScopesObjects(\n scopesObjects: InternalScopesObject[],\n): CaipAccountId[] {\n const allAccounts = new Set<CaipAccountId>();\n\n for (const scopeObject of scopesObjects) {\n for (const { accounts } of Object.values(scopeObject)) {\n for (const account of accounts) {\n allAccounts.add(account);\n }\n }\n }\n\n return Array.from(allAccounts);\n}\n\n/**\n * Gets all permitted accounts from a CAIP-25 caveat\n * This extracts all account IDs from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to extract accounts from\n * @returns Array of unique account IDs\n */\nexport function getCaipAccountIdsFromCaip25CaveatValue(\n caip25CaveatValue: Caip25CaveatValue,\n): CaipAccountId[] {\n return getCaipAccountIdsFromScopesObjects([\n caip25CaveatValue.requiredScopes,\n caip25CaveatValue.optionalScopes,\n ]);\n}\n\n/**\n *\n * Setters\n *\n */\n\n/**\n * Sets the CAIP account IDs to scopes with matching namespaces in the given scopes object.\n * This function should not be used with Smart Contract Accounts (SCA) because\n * it adds the same account ID to all the scopes that have the same namespace.\n *\n * @param scopesObject - The scopes object to set the CAIP account IDs for.\n * @param accounts - The CAIP account IDs to add to the appropriate scopes.\n * @returns The updated scopes object with the CAIP account IDs set.\n */\nconst setNonSCACaipAccountIdsInScopesObject = (\n scopesObject: InternalScopesObject,\n accounts: CaipAccountId[],\n) => {\n const accountsByNamespace = new Map<string, Set<string>>();\n\n for (const account of accounts) {\n const {\n chain: { namespace },\n address,\n } = parseCaipAccountId(account);\n\n if (!accountsByNamespace.has(namespace)) {\n accountsByNamespace.set(namespace, new Set());\n }\n\n accountsByNamespace.get(namespace)?.add(address);\n }\n\n const updatedScopesObject: InternalScopesObject = {};\n\n for (const [scopeString, scopeObject] of Object.entries(scopesObject)) {\n const { namespace, reference } = parseScopeString(scopeString as string);\n\n let caipAccounts: CaipAccountId[] = [];\n\n if (namespace && reference && accountsByNamespace.has(namespace)) {\n const addressSet = accountsByNamespace.get(namespace);\n if (addressSet) {\n caipAccounts = Array.from(addressSet).map(\n (address) => `${namespace}:${reference}:${address}` as CaipAccountId,\n );\n }\n }\n\n updatedScopesObject[scopeString as keyof typeof scopesObject] = {\n ...scopeObject,\n accounts: getUniqueArrayItems(caipAccounts),\n };\n }\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the permitted accounts to scopes with matching namespaces in the given CAIP-25 caveat value.\n * This function should not be used with Smart Contract Accounts (SCA) because\n * it adds the same account ID to all scopes that have the same namespace as the account.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.\n * @param accounts - The permitted accounts to add to the appropriate scopes.\n * @returns The updated CAIP-25 caveat value with the permitted accounts set.\n */\nexport const setNonSCACaipAccountIdsInCaip25CaveatValue = (\n caip25CaveatValue: Caip25CaveatValue,\n accounts: CaipAccountId[],\n): Caip25CaveatValue => {\n return {\n ...caip25CaveatValue,\n requiredScopes: setNonSCACaipAccountIdsInScopesObject(\n caip25CaveatValue.requiredScopes,\n accounts,\n ),\n optionalScopes: setNonSCACaipAccountIdsInScopesObject(\n caip25CaveatValue.optionalScopes,\n accounts,\n ),\n };\n};\n\n/**\n * Checks if an address and list of parsed scopes are connected to any of\n * the permitted accounts based on scope matching\n *\n * @param address - The CAIP account address to check against permitted accounts\n * @param parsedAccountScopes - The list of parsed CAIP chain ID to check against permitted accounts\n * @param permittedAccounts - Array of CAIP account IDs that are permitted\n * @returns True if the address and any account scope is connected to any permitted account\n */\nfunction isAddressWithParsedScopesInPermittedAccountIds(\n address: CaipAccountAddress,\n parsedAccountScopes: {\n namespace?: CaipNamespace;\n reference?: CaipReference;\n }[],\n permittedAccounts: CaipAccountId[],\n) {\n if (!address || !parsedAccountScopes.length || !permittedAccounts.length) {\n return false;\n }\n\n return permittedAccounts.some((account) => {\n const parsedPermittedAccount = parseCaipAccountId(account);\n\n return parsedAccountScopes.some(({ namespace, reference }) => {\n if (namespace !== parsedPermittedAccount.chain.namespace) {\n return false;\n }\n\n // handle eip155:0 case and insensitive evm address comparison\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n if (namespace === KnownCaipNamespace.Eip155) {\n return (\n (reference === '0' ||\n reference === parsedPermittedAccount.chain.reference) &&\n isEqualCaseInsensitive(address, parsedPermittedAccount.address)\n );\n }\n return (\n reference === parsedPermittedAccount.chain.reference &&\n address === parsedPermittedAccount.address\n );\n });\n });\n}\n\n/**\n * Checks if an internal account is connected to any of the permitted accounts\n * based on scope matching\n *\n * @param internalAccount - The internal account to check against permitted accounts\n * @param permittedAccounts - Array of CAIP account IDs that are permitted\n * @returns True if the account is connected to any permitted account\n */\nexport function isInternalAccountInPermittedAccountIds(\n internalAccount: InternalAccount,\n permittedAccounts: CaipAccountId[],\n): boolean {\n const parsedInteralAccountScopes = internalAccount.scopes.map((scope) => {\n return parseScopeString(scope);\n });\n\n return isAddressWithParsedScopesInPermittedAccountIds(\n internalAccount.address,\n parsedInteralAccountScopes,\n permittedAccounts,\n );\n}\n\n/**\n * Checks if an CAIP account ID is connected to any of the permitted accounts\n * based on scope matching\n *\n * @param accountId - The CAIP account ID to check against permitted accounts\n * @param permittedAccounts - Array of CAIP account IDs that are permitted\n * @returns True if the account is connected to any permitted account\n */\nexport function isCaipAccountIdInPermittedAccountIds(\n accountId: CaipAccountId,\n permittedAccounts: CaipAccountId[],\n): boolean {\n const { address, chain } = parseCaipAccountId(accountId);\n\n return isAddressWithParsedScopesInPermittedAccountIds(\n address,\n [chain],\n permittedAccounts,\n );\n}\n"]}
|
@@ -1,10 +1,18 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
exports.setChainIdsInCaip25CaveatValue = exports.addCaipChainIdInCaip25CaveatValue = exports.getAllScopesFromPermission = exports.getAllNamespacesFromCaip25CaveatValue = exports.getAllScopesFromCaip25CaveatValue = exports.getAllScopesFromScopesObjects = exports.setPermittedEthChainIds = exports.addPermittedEthChainId = exports.getPermittedEthChainIds = void 0;
|
4
4
|
const controller_utils_1 = require("@metamask/controller-utils");
|
5
5
|
const utils_1 = require("@metamask/utils");
|
6
|
+
const caip25Permission_1 = require("../caip25Permission.cjs");
|
6
7
|
const transform_1 = require("../scope/transform.cjs");
|
7
8
|
const types_1 = require("../scope/types.cjs");
|
9
|
+
/*
|
10
|
+
*
|
11
|
+
*
|
12
|
+
* EVM SPECIFIC GETTERS AND SETTERS
|
13
|
+
*
|
14
|
+
*
|
15
|
+
*/
|
8
16
|
/**
|
9
17
|
* Gets the Ethereum (EIP155 namespaced) chainIDs from internal scopes.
|
10
18
|
*
|
@@ -112,26 +120,94 @@ const setPermittedEthChainIds = (caip25CaveatValue, chainIds) => {
|
|
112
120
|
return updatedCaveatValue;
|
113
121
|
};
|
114
122
|
exports.setPermittedEthChainIds = setPermittedEthChainIds;
|
123
|
+
/*
|
124
|
+
*
|
125
|
+
*
|
126
|
+
* GENERALIZED GETTERS AND SETTERS
|
127
|
+
*
|
128
|
+
*
|
129
|
+
*/
|
130
|
+
/*
|
131
|
+
*
|
132
|
+
* GETTERS
|
133
|
+
*
|
134
|
+
*/
|
115
135
|
/**
|
116
|
-
*
|
117
|
-
* - Scopes without references (e.g. "wallet:")
|
118
|
-
* - CAIP-2 ChainId scopes for the given chainIDs
|
136
|
+
* Gets all scopes from a CAIP-25 caveat value
|
119
137
|
*
|
120
|
-
* @param
|
121
|
-
* @
|
122
|
-
* @returns The filtered scopes object.
|
138
|
+
* @param scopesObjects - The scopes objects to get the scopes from.
|
139
|
+
* @returns An array of InternalScopeStrings.
|
123
140
|
*/
|
124
|
-
|
125
|
-
const
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
if ((0, types_1.isWalletScope)(scopeString) || chainIds.includes(scopeString)) {
|
130
|
-
updatedScopesObject[scopeString] = scopeObject;
|
141
|
+
function getAllScopesFromScopesObjects(scopesObjects) {
|
142
|
+
const scopeSet = new Set();
|
143
|
+
for (const scopeObject of scopesObjects) {
|
144
|
+
for (const key of Object.keys(scopeObject)) {
|
145
|
+
scopeSet.add(key);
|
131
146
|
}
|
132
|
-
}
|
133
|
-
return
|
134
|
-
}
|
147
|
+
}
|
148
|
+
return Array.from(scopeSet);
|
149
|
+
}
|
150
|
+
exports.getAllScopesFromScopesObjects = getAllScopesFromScopesObjects;
|
151
|
+
/**
|
152
|
+
* Gets all scopes (chain IDs) from a CAIP-25 caveat
|
153
|
+
* This extracts all scopes from both required and optional scopes
|
154
|
+
* and returns a unique set.
|
155
|
+
*
|
156
|
+
* @param caip25CaveatValue - The CAIP-25 caveat value to extract scopes from
|
157
|
+
* @returns Array of unique scope strings (chain IDs)
|
158
|
+
*/
|
159
|
+
function getAllScopesFromCaip25CaveatValue(caip25CaveatValue) {
|
160
|
+
return getAllScopesFromScopesObjects([
|
161
|
+
caip25CaveatValue.requiredScopes,
|
162
|
+
caip25CaveatValue.optionalScopes,
|
163
|
+
]);
|
164
|
+
}
|
165
|
+
exports.getAllScopesFromCaip25CaveatValue = getAllScopesFromCaip25CaveatValue;
|
166
|
+
/**
|
167
|
+
* Gets all non-wallet namespaces from a CAIP-25 caveat value
|
168
|
+
* This extracts all namespaces from both required and optional scopes
|
169
|
+
* and returns a unique set.
|
170
|
+
*
|
171
|
+
* @param caip25CaveatValue - The CAIP-25 caveat value to extract namespaces from
|
172
|
+
* @returns Array of unique namespace strings
|
173
|
+
*/
|
174
|
+
function getAllNamespacesFromCaip25CaveatValue(caip25CaveatValue) {
|
175
|
+
const allScopes = getAllScopesFromCaip25CaveatValue(caip25CaveatValue);
|
176
|
+
const namespaceSet = new Set();
|
177
|
+
for (const scope of allScopes) {
|
178
|
+
const { namespace } = (0, types_1.parseScopeString)(scope);
|
179
|
+
if (namespace === utils_1.KnownCaipNamespace.Wallet) {
|
180
|
+
namespaceSet.add(scope);
|
181
|
+
}
|
182
|
+
else if (namespace) {
|
183
|
+
namespaceSet.add(namespace);
|
184
|
+
}
|
185
|
+
}
|
186
|
+
return Array.from(namespaceSet);
|
187
|
+
}
|
188
|
+
exports.getAllNamespacesFromCaip25CaveatValue = getAllNamespacesFromCaip25CaveatValue;
|
189
|
+
/**
|
190
|
+
* Gets all scopes (chain IDs) from a CAIP-25 permission
|
191
|
+
* This extracts all scopes from both required and optional scopes
|
192
|
+
* and returns a unique set.
|
193
|
+
*
|
194
|
+
* @param caip25Permission - The CAIP-25 permission object
|
195
|
+
* @param caip25Permission.caveats - The caveats of the CAIP-25 permission
|
196
|
+
* @returns Array of unique scope strings (chain IDs)
|
197
|
+
*/
|
198
|
+
function getAllScopesFromPermission(caip25Permission) {
|
199
|
+
const caip25Caveat = caip25Permission.caveats.find((caveat) => caveat.type === caip25Permission_1.Caip25CaveatType);
|
200
|
+
if (!caip25Caveat) {
|
201
|
+
return [];
|
202
|
+
}
|
203
|
+
return getAllScopesFromCaip25CaveatValue(caip25Caveat.value);
|
204
|
+
}
|
205
|
+
exports.getAllScopesFromPermission = getAllScopesFromPermission;
|
206
|
+
/*
|
207
|
+
*
|
208
|
+
* SETTERS
|
209
|
+
*
|
210
|
+
*/
|
135
211
|
/**
|
136
212
|
* Adds a chainID to the optional scopes if it is not already present
|
137
213
|
* in either the pre-existing required or optional scopes.
|
@@ -140,9 +216,9 @@ const filterChainScopesObjectByChainId = (scopesObject, chainIds) => {
|
|
140
216
|
* @param chainId - The chainID to add.
|
141
217
|
* @returns The updated CAIP-25 caveat value with the added chainID.
|
142
218
|
*/
|
143
|
-
const
|
144
|
-
if (
|
145
|
-
|
219
|
+
const addCaipChainIdInCaip25CaveatValue = (caip25CaveatValue, chainId) => {
|
220
|
+
if (caip25CaveatValue.requiredScopes[chainId] ||
|
221
|
+
caip25CaveatValue.optionalScopes[chainId]) {
|
146
222
|
return caip25CaveatValue;
|
147
223
|
}
|
148
224
|
return {
|
@@ -155,24 +231,41 @@ const addPermittedChainId = (caip25CaveatValue, chainId) => {
|
|
155
231
|
},
|
156
232
|
};
|
157
233
|
};
|
158
|
-
exports.
|
234
|
+
exports.addCaipChainIdInCaip25CaveatValue = addCaipChainIdInCaip25CaveatValue;
|
159
235
|
/**
|
160
|
-
* Sets the
|
236
|
+
* Sets the CAIP-2 chainIds for the required and optional scopes.
|
237
|
+
* If the caip25CaveatValue contains chainIds not in the chainIds array arg they are filtered out
|
161
238
|
*
|
162
239
|
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted CAIP-2 chainIDs for.
|
163
|
-
* @param chainIds - The CAIP-2 chainIDs to set
|
164
|
-
* @returns The updated CAIP-25 caveat value with the
|
240
|
+
* @param chainIds - The CAIP-2 chainIDs to set.
|
241
|
+
* @returns The updated CAIP-25 caveat value with the CAIP-2 chainIDs.
|
165
242
|
*/
|
166
|
-
const
|
167
|
-
|
168
|
-
|
169
|
-
requiredScopes:
|
170
|
-
optionalScopes:
|
243
|
+
const setChainIdsInCaip25CaveatValue = (caip25CaveatValue, chainIds) => {
|
244
|
+
const chainIdSet = new Set(chainIds);
|
245
|
+
const result = {
|
246
|
+
requiredScopes: {},
|
247
|
+
optionalScopes: {},
|
248
|
+
sessionProperties: caip25CaveatValue.sessionProperties,
|
249
|
+
isMultichainOrigin: caip25CaveatValue.isMultichainOrigin,
|
171
250
|
};
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
251
|
+
for (const [key, value] of Object.entries(caip25CaveatValue.requiredScopes)) {
|
252
|
+
const scopeString = key;
|
253
|
+
if ((0, types_1.isWalletScope)(scopeString) || chainIdSet.has(scopeString)) {
|
254
|
+
result.requiredScopes[scopeString] = value;
|
255
|
+
}
|
256
|
+
}
|
257
|
+
for (const [key, value] of Object.entries(caip25CaveatValue.optionalScopes)) {
|
258
|
+
const scopeString = key;
|
259
|
+
if ((0, types_1.isWalletScope)(scopeString) || chainIdSet.has(scopeString)) {
|
260
|
+
result.optionalScopes[scopeString] = value;
|
261
|
+
}
|
262
|
+
}
|
263
|
+
for (const chainId of chainIds) {
|
264
|
+
if (!result.requiredScopes[chainId] && !result.optionalScopes[chainId]) {
|
265
|
+
result.optionalScopes[chainId] = { accounts: [] };
|
266
|
+
}
|
267
|
+
}
|
268
|
+
return result;
|
176
269
|
};
|
177
|
-
exports.
|
270
|
+
exports.setChainIdsInCaip25CaveatValue = setChainIdsInCaip25CaveatValue;
|
178
271
|
//# sourceMappingURL=caip-permission-adapter-permittedChains.cjs.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"caip-permission-adapter-permittedChains.cjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-permittedChains.ts"],"names":[],"mappings":";;;AAAA,iEAAmD;AAEnD,2CAAkE;AAGlE,sDAAyD;AAEzD,8CAAiE;AAEjE;;;;;GAKG;AACH,MAAM,iCAAiC,GAAG,CAAC,MAA4B,EAAE,EAAE;IACzE,MAAM,WAAW,GAAU,EAAE,CAAC;IAE9B,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE;QAC1C,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,IAAA,wBAAgB,EAAC,WAAW,CAAC,CAAC;QAC/D,IAAI,SAAS,KAAK,0BAAkB,CAAC,MAAM,IAAI,SAAS,EAAE;YACxD,WAAW,CAAC,IAAI,CAAC,IAAA,wBAAK,EAAC,SAAS,CAAC,CAAC,CAAC;SACpC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAEF;;;;;GAKG;AACI,MAAM,uBAAuB,GAAG,CACrC,iBAGC,EACD,EAAE;IACF,MAAM,EAAE,cAAc,EAAE,cAAc,EAAE,GA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BAAoB,wBAqB/B","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport type { Hex, CaipChainId } from '@metamask/utils';\nimport { hexToBigInt, KnownCaipNamespace } from '@metamask/utils';\n\nimport type { Caip25CaveatValue } from '../caip25Permission';\nimport { getUniqueArrayItems } from '../scope/transform';\nimport type { InternalScopesObject } from '../scope/types';\nimport { isWalletScope, parseScopeString } from '../scope/types';\n\n/**\n * Gets the Ethereum (EIP155 namespaced) chainIDs from internal scopes.\n *\n * @param scopes - The internal scopes from which to get the Ethereum chainIDs.\n * @returns An array of Ethereum chainIDs.\n */\nconst getPermittedEthChainIdsFromScopes = (scopes: InternalScopesObject) => {\n const ethChainIds: Hex[] = [];\n\n Object.keys(scopes).forEach((scopeString) => {\n const { namespace, reference } = parseScopeString(scopeString);\n if (namespace === KnownCaipNamespace.Eip155 && reference) {\n ethChainIds.push(toHex(reference));\n }\n });\n\n return ethChainIds;\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) chainIDs from the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value from which to get the Ethereum chainIDs.\n * @returns An array of Ethereum chainIDs.\n */\nexport const getPermittedEthChainIds = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n) => {\n const { requiredScopes, optionalScopes } = caip25CaveatValue;\n\n const ethChainIds: Hex[] = [\n ...getPermittedEthChainIdsFromScopes(requiredScopes),\n ...getPermittedEthChainIdsFromScopes(optionalScopes),\n ];\n\n return getUniqueArrayItems(ethChainIds);\n};\n\n/**\n * Adds an Ethereum (EIP155 namespaced) chainID to the optional scopes if it is not already present\n * in either the pre-existing required or optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to add the Ethereum chainID to.\n * @param chainId - The Ethereum chainID to add.\n * @returns The updated CAIP-25 caveat value with the added Ethereum chainID.\n */\nexport const addPermittedEthChainId = (\n caip25CaveatValue: Caip25CaveatValue,\n chainId: Hex,\n): Caip25CaveatValue => {\n const scopeString = `eip155:${hexToBigInt(chainId).toString(10)}`;\n if (\n Object.keys(caip25CaveatValue.requiredScopes).includes(scopeString) ||\n Object.keys(caip25CaveatValue.optionalScopes).includes(scopeString)\n ) {\n return caip25CaveatValue;\n }\n\n return {\n ...caip25CaveatValue,\n optionalScopes: {\n ...caip25CaveatValue.optionalScopes,\n [scopeString]: {\n accounts: [],\n },\n },\n };\n};\n\n/**\n * Filters the scopes object to only include:\n * - Scopes without references (e.g. \"wallet:\")\n * - EIP155 scopes for the given chainIDs\n * - Non EIP155 scopes (e.g. \"bip122:\" or any other non ethereum namespaces)\n *\n * @param scopesObject - The scopes object to filter.\n * @param chainIds - The chainIDs to filter EIP155 scopes by.\n * @returns The filtered scopes object.\n */\nconst filterEthScopesObjectByChainId = (\n scopesObject: InternalScopesObject,\n chainIds: Hex[],\n): InternalScopesObject => {\n const updatedScopesObject: InternalScopesObject = {};\n\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 if (!reference) {\n updatedScopesObject[scopeString] = scopeObject;\n return;\n }\n if (namespace === KnownCaipNamespace.Eip155) {\n const chainId = toHex(reference);\n if (chainIds.includes(chainId)) {\n updatedScopesObject[scopeString] = scopeObject;\n }\n } else {\n updatedScopesObject[scopeString] = scopeObject;\n }\n });\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the permitted Ethereum (EIP155 namespaced) chainIDs for the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted Ethereum chainIDs for.\n * @param chainIds - The Ethereum chainIDs to set as permitted.\n * @returns The updated CAIP-25 caveat value with the permitted Ethereum chainIDs.\n */\nexport const setPermittedEthChainIds = (\n caip25CaveatValue: Caip25CaveatValue,\n chainIds: Hex[],\n): Caip25CaveatValue => {\n let updatedCaveatValue: Caip25CaveatValue = {\n ...caip25CaveatValue,\n requiredScopes: filterEthScopesObjectByChainId(\n caip25CaveatValue.requiredScopes,\n chainIds,\n ),\n optionalScopes: filterEthScopesObjectByChainId(\n caip25CaveatValue.optionalScopes,\n chainIds,\n ),\n };\n\n chainIds.forEach((chainId) => {\n updatedCaveatValue = addPermittedEthChainId(updatedCaveatValue, chainId);\n });\n\n return updatedCaveatValue;\n};\n\n/**\n * Filters the scopes object to only include:\n * - Scopes without references (e.g. \"wallet:\")\n * - CAIP-2 ChainId scopes for the given chainIDs\n *\n * @param scopesObject - The scopes object to filter.\n * @param chainIds - The CAIP-2 chainIDs to filter for.\n * @returns The filtered scopes object.\n */\nconst filterChainScopesObjectByChainId = (\n scopesObject: InternalScopesObject,\n chainIds: CaipChainId[],\n): InternalScopesObject => {\n const updatedScopesObject: InternalScopesObject = {};\n\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 if (isWalletScope(scopeString) || chainIds.includes(scopeString)) {\n updatedScopesObject[scopeString] = scopeObject;\n }\n });\n\n return updatedScopesObject;\n};\n\n/**\n * Adds a chainID to the optional scopes if it is not already present\n * in either the pre-existing required or optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to add the chainID to.\n * @param chainId - The chainID to add.\n * @returns The updated CAIP-25 caveat value with the added chainID.\n */\nexport const addPermittedChainId = (\n caip25CaveatValue: Caip25CaveatValue,\n chainId: CaipChainId,\n): Caip25CaveatValue => {\n if (\n Object.keys(caip25CaveatValue.requiredScopes).includes(chainId) ||\n Object.keys(caip25CaveatValue.optionalScopes).includes(chainId)\n ) {\n return caip25CaveatValue;\n }\n\n return {\n ...caip25CaveatValue,\n optionalScopes: {\n ...caip25CaveatValue.optionalScopes,\n [chainId]: {\n accounts: [],\n },\n },\n };\n};\n\n/**\n * Sets the permitted CAIP-2 chainIDs for the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted CAIP-2 chainIDs for.\n * @param chainIds - The CAIP-2 chainIDs to set as permitted.\n * @returns The updated CAIP-25 caveat value with the permitted CAIP-2 chainIDs.\n */\nexport const setPermittedChainIds = (\n caip25CaveatValue: Caip25CaveatValue,\n chainIds: CaipChainId[],\n): Caip25CaveatValue => {\n let updatedCaveatValue: Caip25CaveatValue = {\n ...caip25CaveatValue,\n requiredScopes: filterChainScopesObjectByChainId(\n caip25CaveatValue.requiredScopes,\n chainIds,\n ),\n optionalScopes: filterChainScopesObjectByChainId(\n caip25CaveatValue.optionalScopes,\n chainIds,\n ),\n };\n\n chainIds.forEach((chainId) => {\n updatedCaveatValue = addPermittedChainId(updatedCaveatValue, chainId);\n });\n\n return updatedCaveatValue;\n};\n"]}
|
1
|
+
{"version":3,"file":"caip-permission-adapter-permittedChains.cjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-permittedChains.ts"],"names":[],"mappings":";;;AAAA,iEAAmD;AAEnD,2CAAkE;AAElE,8DAA+E;AAC/E,sDAAyD;AAEzD,8CAAiE;AAEjE;;;;;;GAMG;AAEH;;;;;GAKG;AACH,MAAM,iCAAiC,GAAG,CAAC,MAA4B,EAAE,EAAE;IACzE,MAAM,WAAW,GAAU,EAAE,CAAC;IAE9B,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE;QAC1C,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,IAAA,wBAAgB,EAAC,WAAW,CAAC,CAAC;QAC/D,IAAI,SAAS,KAAK,0BAAkB,CAAC,MAAM,IAAI,SAAS,EAAE;YACxD,WAAW,CAAC,IAAI,CAAC,IAAA,wBAAK,EAAC,SAAS,CAAC,CAAC,CAAC;SACpC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAEF;;;;;GAKG;AACI,MAAM,uBAAuB,GAAG,CACrC,iBAGC,EACD,EAAE;IACF,MAAM,EAAE,cAAc,EAA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AU,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE;YAC7D,MAAM,CAAC,cAAc,CAAC,WAAW,CAAC,GAAG,KAAK,CAAC;SAC5C;KACF;IAED,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;QAC9B,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE;YACtE,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,GAAG,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC;SACnD;KACF;IAED,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAjCW,QAAA,8BAA8B,kCAiCzC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport type { Hex, CaipChainId, CaipNamespace } from '@metamask/utils';\nimport { hexToBigInt, KnownCaipNamespace } from '@metamask/utils';\n\nimport { Caip25CaveatType, type Caip25CaveatValue } from '../caip25Permission';\nimport { getUniqueArrayItems } from '../scope/transform';\nimport type { InternalScopesObject, InternalScopeString } from '../scope/types';\nimport { isWalletScope, parseScopeString } from '../scope/types';\n\n/*\n *\n *\n * EVM SPECIFIC GETTERS AND SETTERS\n *\n *\n */\n\n/**\n * Gets the Ethereum (EIP155 namespaced) chainIDs from internal scopes.\n *\n * @param scopes - The internal scopes from which to get the Ethereum chainIDs.\n * @returns An array of Ethereum chainIDs.\n */\nconst getPermittedEthChainIdsFromScopes = (scopes: InternalScopesObject) => {\n const ethChainIds: Hex[] = [];\n\n Object.keys(scopes).forEach((scopeString) => {\n const { namespace, reference } = parseScopeString(scopeString);\n if (namespace === KnownCaipNamespace.Eip155 && reference) {\n ethChainIds.push(toHex(reference));\n }\n });\n\n return ethChainIds;\n};\n\n/**\n * Gets the Ethereum (EIP155 namespaced) chainIDs from the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value from which to get the Ethereum chainIDs.\n * @returns An array of Ethereum chainIDs.\n */\nexport const getPermittedEthChainIds = (\n caip25CaveatValue: Pick<\n Caip25CaveatValue,\n 'requiredScopes' | 'optionalScopes'\n >,\n) => {\n const { requiredScopes, optionalScopes } = caip25CaveatValue;\n\n const ethChainIds: Hex[] = [\n ...getPermittedEthChainIdsFromScopes(requiredScopes),\n ...getPermittedEthChainIdsFromScopes(optionalScopes),\n ];\n\n return getUniqueArrayItems(ethChainIds);\n};\n\n/**\n * Adds an Ethereum (EIP155 namespaced) chainID to the optional scopes if it is not already present\n * in either the pre-existing required or optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to add the Ethereum chainID to.\n * @param chainId - The Ethereum chainID to add.\n * @returns The updated CAIP-25 caveat value with the added Ethereum chainID.\n */\nexport const addPermittedEthChainId = (\n caip25CaveatValue: Caip25CaveatValue,\n chainId: Hex,\n): Caip25CaveatValue => {\n const scopeString = `eip155:${hexToBigInt(chainId).toString(10)}`;\n if (\n Object.keys(caip25CaveatValue.requiredScopes).includes(scopeString) ||\n Object.keys(caip25CaveatValue.optionalScopes).includes(scopeString)\n ) {\n return caip25CaveatValue;\n }\n\n return {\n ...caip25CaveatValue,\n optionalScopes: {\n ...caip25CaveatValue.optionalScopes,\n [scopeString]: {\n accounts: [],\n },\n },\n };\n};\n\n/**\n * Filters the scopes object to only include:\n * - Scopes without references (e.g. \"wallet:\")\n * - EIP155 scopes for the given chainIDs\n * - Non EIP155 scopes (e.g. \"bip122:\" or any other non ethereum namespaces)\n *\n * @param scopesObject - The scopes object to filter.\n * @param chainIds - The chainIDs to filter EIP155 scopes by.\n * @returns The filtered scopes object.\n */\nconst filterEthScopesObjectByChainId = (\n scopesObject: InternalScopesObject,\n chainIds: Hex[],\n): InternalScopesObject => {\n const updatedScopesObject: InternalScopesObject = {};\n\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 if (!reference) {\n updatedScopesObject[scopeString] = scopeObject;\n return;\n }\n if (namespace === KnownCaipNamespace.Eip155) {\n const chainId = toHex(reference);\n if (chainIds.includes(chainId)) {\n updatedScopesObject[scopeString] = scopeObject;\n }\n } else {\n updatedScopesObject[scopeString] = scopeObject;\n }\n });\n\n return updatedScopesObject;\n};\n\n/**\n * Sets the permitted Ethereum (EIP155 namespaced) chainIDs for the required and optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted Ethereum chainIDs for.\n * @param chainIds - The Ethereum chainIDs to set as permitted.\n * @returns The updated CAIP-25 caveat value with the permitted Ethereum chainIDs.\n */\nexport const setPermittedEthChainIds = (\n caip25CaveatValue: Caip25CaveatValue,\n chainIds: Hex[],\n): Caip25CaveatValue => {\n let updatedCaveatValue: Caip25CaveatValue = {\n ...caip25CaveatValue,\n requiredScopes: filterEthScopesObjectByChainId(\n caip25CaveatValue.requiredScopes,\n chainIds,\n ),\n optionalScopes: filterEthScopesObjectByChainId(\n caip25CaveatValue.optionalScopes,\n chainIds,\n ),\n };\n\n chainIds.forEach((chainId) => {\n updatedCaveatValue = addPermittedEthChainId(updatedCaveatValue, chainId);\n });\n\n return updatedCaveatValue;\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 scopes from a CAIP-25 caveat value\n *\n * @param scopesObjects - The scopes objects to get the scopes from.\n * @returns An array of InternalScopeStrings.\n */\nexport function getAllScopesFromScopesObjects(\n scopesObjects: InternalScopesObject[],\n): InternalScopeString[] {\n const scopeSet = new Set<InternalScopeString>();\n\n for (const scopeObject of scopesObjects) {\n for (const key of Object.keys(scopeObject)) {\n scopeSet.add(key as InternalScopeString);\n }\n }\n\n return Array.from(scopeSet);\n}\n\n/**\n * Gets all scopes (chain IDs) from a CAIP-25 caveat\n * This extracts all scopes from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to extract scopes from\n * @returns Array of unique scope strings (chain IDs)\n */\nexport function getAllScopesFromCaip25CaveatValue(\n caip25CaveatValue: Caip25CaveatValue,\n): CaipChainId[] {\n return getAllScopesFromScopesObjects([\n caip25CaveatValue.requiredScopes,\n caip25CaveatValue.optionalScopes,\n ]) as CaipChainId[];\n}\n\n/**\n * Gets all non-wallet namespaces from a CAIP-25 caveat value\n * This extracts all namespaces from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to extract namespaces from\n * @returns Array of unique namespace strings\n */\nexport function getAllNamespacesFromCaip25CaveatValue(\n caip25CaveatValue: Caip25CaveatValue,\n): CaipNamespace[] {\n const allScopes = getAllScopesFromCaip25CaveatValue(caip25CaveatValue);\n const namespaceSet = new Set<CaipNamespace>();\n\n for (const scope of allScopes) {\n const { namespace } = parseScopeString(scope);\n if (namespace === KnownCaipNamespace.Wallet) {\n namespaceSet.add(scope);\n } else if (namespace) {\n namespaceSet.add(namespace);\n }\n }\n\n return Array.from(namespaceSet);\n}\n\n/**\n * Gets all scopes (chain IDs) from a CAIP-25 permission\n * This extracts all scopes from both required and optional scopes\n * and returns a unique set.\n *\n * @param caip25Permission - The CAIP-25 permission object\n * @param caip25Permission.caveats - The caveats of the CAIP-25 permission\n * @returns Array of unique scope strings (chain IDs)\n */\nexport function getAllScopesFromPermission(caip25Permission: {\n caveats: {\n type: string;\n value: Caip25CaveatValue;\n }[];\n}): CaipChainId[] {\n const caip25Caveat = caip25Permission.caveats.find(\n (caveat) => caveat.type === Caip25CaveatType,\n );\n if (!caip25Caveat) {\n return [];\n }\n\n return getAllScopesFromCaip25CaveatValue(caip25Caveat.value);\n}\n\n/*\n *\n * SETTERS\n *\n */\n\n/**\n * Adds a chainID to the optional scopes if it is not already present\n * in either the pre-existing required or optional scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to add the chainID to.\n * @param chainId - The chainID to add.\n * @returns The updated CAIP-25 caveat value with the added chainID.\n */\nexport const addCaipChainIdInCaip25CaveatValue = (\n caip25CaveatValue: Caip25CaveatValue,\n chainId: CaipChainId,\n): Caip25CaveatValue => {\n if (\n caip25CaveatValue.requiredScopes[chainId] ||\n caip25CaveatValue.optionalScopes[chainId]\n ) {\n return caip25CaveatValue;\n }\n\n return {\n ...caip25CaveatValue,\n optionalScopes: {\n ...caip25CaveatValue.optionalScopes,\n [chainId]: {\n accounts: [],\n },\n },\n };\n};\n\n/**\n * Sets the CAIP-2 chainIds for the required and optional scopes.\n * If the caip25CaveatValue contains chainIds not in the chainIds array arg they are filtered out\n *\n * @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted CAIP-2 chainIDs for.\n * @param chainIds - The CAIP-2 chainIDs to set.\n * @returns The updated CAIP-25 caveat value with the CAIP-2 chainIDs.\n */\nexport const setChainIdsInCaip25CaveatValue = (\n caip25CaveatValue: Caip25CaveatValue,\n chainIds: CaipChainId[],\n): Caip25CaveatValue => {\n const chainIdSet = new Set(chainIds);\n const result: Caip25CaveatValue = {\n requiredScopes: {},\n optionalScopes: {},\n sessionProperties: caip25CaveatValue.sessionProperties,\n isMultichainOrigin: caip25CaveatValue.isMultichainOrigin,\n };\n\n for (const [key, value] of Object.entries(caip25CaveatValue.requiredScopes)) {\n const scopeString = key as keyof typeof caip25CaveatValue.requiredScopes;\n if (isWalletScope(scopeString) || chainIdSet.has(scopeString)) {\n result.requiredScopes[scopeString] = value;\n }\n }\n\n for (const [key, value] of Object.entries(caip25CaveatValue.optionalScopes)) {\n const scopeString = key as keyof typeof caip25CaveatValue.optionalScopes;\n if (isWalletScope(scopeString) || chainIdSet.has(scopeString)) {\n result.optionalScopes[scopeString] = value;\n }\n }\n\n for (const chainId of chainIds) {\n if (!result.requiredScopes[chainId] && !result.optionalScopes[chainId]) {\n result.optionalScopes[chainId] = { accounts: [] };\n }\n }\n\n return result;\n};\n"]}
|
@@ -1,5 +1,6 @@
|
|
1
|
-
import type { Hex, CaipChainId } from "@metamask/utils";
|
2
|
-
import type
|
1
|
+
import type { Hex, CaipChainId, CaipNamespace } from "@metamask/utils";
|
2
|
+
import { type Caip25CaveatValue } from "../caip25Permission.cjs";
|
3
|
+
import type { InternalScopesObject, InternalScopeString } from "../scope/types.cjs";
|
3
4
|
/**
|
4
5
|
* Gets the Ethereum (EIP155 namespaced) chainIDs from the required and optional scopes.
|
5
6
|
*
|
@@ -24,6 +25,46 @@ export declare const addPermittedEthChainId: (caip25CaveatValue: Caip25CaveatVal
|
|
24
25
|
* @returns The updated CAIP-25 caveat value with the permitted Ethereum chainIDs.
|
25
26
|
*/
|
26
27
|
export declare const setPermittedEthChainIds: (caip25CaveatValue: Caip25CaveatValue, chainIds: Hex[]) => Caip25CaveatValue;
|
28
|
+
/**
|
29
|
+
* Gets all scopes from a CAIP-25 caveat value
|
30
|
+
*
|
31
|
+
* @param scopesObjects - The scopes objects to get the scopes from.
|
32
|
+
* @returns An array of InternalScopeStrings.
|
33
|
+
*/
|
34
|
+
export declare function getAllScopesFromScopesObjects(scopesObjects: InternalScopesObject[]): InternalScopeString[];
|
35
|
+
/**
|
36
|
+
* Gets all scopes (chain IDs) from a CAIP-25 caveat
|
37
|
+
* This extracts all scopes from both required and optional scopes
|
38
|
+
* and returns a unique set.
|
39
|
+
*
|
40
|
+
* @param caip25CaveatValue - The CAIP-25 caveat value to extract scopes from
|
41
|
+
* @returns Array of unique scope strings (chain IDs)
|
42
|
+
*/
|
43
|
+
export declare function getAllScopesFromCaip25CaveatValue(caip25CaveatValue: Caip25CaveatValue): CaipChainId[];
|
44
|
+
/**
|
45
|
+
* Gets all non-wallet namespaces from a CAIP-25 caveat value
|
46
|
+
* This extracts all namespaces from both required and optional scopes
|
47
|
+
* and returns a unique set.
|
48
|
+
*
|
49
|
+
* @param caip25CaveatValue - The CAIP-25 caveat value to extract namespaces from
|
50
|
+
* @returns Array of unique namespace strings
|
51
|
+
*/
|
52
|
+
export declare function getAllNamespacesFromCaip25CaveatValue(caip25CaveatValue: Caip25CaveatValue): CaipNamespace[];
|
53
|
+
/**
|
54
|
+
* Gets all scopes (chain IDs) from a CAIP-25 permission
|
55
|
+
* This extracts all scopes from both required and optional scopes
|
56
|
+
* and returns a unique set.
|
57
|
+
*
|
58
|
+
* @param caip25Permission - The CAIP-25 permission object
|
59
|
+
* @param caip25Permission.caveats - The caveats of the CAIP-25 permission
|
60
|
+
* @returns Array of unique scope strings (chain IDs)
|
61
|
+
*/
|
62
|
+
export declare function getAllScopesFromPermission(caip25Permission: {
|
63
|
+
caveats: {
|
64
|
+
type: string;
|
65
|
+
value: Caip25CaveatValue;
|
66
|
+
}[];
|
67
|
+
}): CaipChainId[];
|
27
68
|
/**
|
28
69
|
* Adds a chainID to the optional scopes if it is not already present
|
29
70
|
* in either the pre-existing required or optional scopes.
|
@@ -32,13 +73,14 @@ export declare const setPermittedEthChainIds: (caip25CaveatValue: Caip25CaveatVa
|
|
32
73
|
* @param chainId - The chainID to add.
|
33
74
|
* @returns The updated CAIP-25 caveat value with the added chainID.
|
34
75
|
*/
|
35
|
-
export declare const
|
76
|
+
export declare const addCaipChainIdInCaip25CaveatValue: (caip25CaveatValue: Caip25CaveatValue, chainId: CaipChainId) => Caip25CaveatValue;
|
36
77
|
/**
|
37
|
-
* Sets the
|
78
|
+
* Sets the CAIP-2 chainIds for the required and optional scopes.
|
79
|
+
* If the caip25CaveatValue contains chainIds not in the chainIds array arg they are filtered out
|
38
80
|
*
|
39
81
|
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted CAIP-2 chainIDs for.
|
40
|
-
* @param chainIds - The CAIP-2 chainIDs to set
|
41
|
-
* @returns The updated CAIP-25 caveat value with the
|
82
|
+
* @param chainIds - The CAIP-2 chainIDs to set.
|
83
|
+
* @returns The updated CAIP-25 caveat value with the CAIP-2 chainIDs.
|
42
84
|
*/
|
43
|
-
export declare const
|
85
|
+
export declare const setChainIdsInCaip25CaveatValue: (caip25CaveatValue: Caip25CaveatValue, chainIds: CaipChainId[]) => Caip25CaveatValue;
|
44
86
|
//# sourceMappingURL=caip-permission-adapter-permittedChains.d.cts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"caip-permission-adapter-permittedChains.d.cts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-permittedChains.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,WAAW,EAAE,wBAAwB;
|
1
|
+
{"version":3,"file":"caip-permission-adapter-permittedChains.d.cts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-permittedChains.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,WAAW,EAAE,aAAa,EAAE,wBAAwB;AAGvE,OAAO,EAAoB,KAAK,iBAAiB,EAAE,gCAA4B;AAE/E,OAAO,KAAK,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,2BAAuB;AA8BhF;;;;;GAKG;AACH,eAAO,MAAM,uBAAuB,sBACf,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC,oBAUF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,sBAAsB,sBACd,iBAAiB,WAC3B,GAAG,KACX,iBAkBF,CAAC;AAuCF;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,sBACf,iBAAiB,YAC1B,GAAG,EAAE,KACd,iBAkBF,CAAC;AAgBF;;;;;GAKG;AACH,wBAAgB,6BAA6B,CAC3C,aAAa,EAAE,oBAAoB,EAAE,GACpC,mBAAmB,EAAE,CAUvB;AAED;;;;;;;GAOG;AACH,wBAAgB,iCAAiC,CAC/C,iBAAiB,EAAE,iBAAiB,GACnC,WAAW,EAAE,CAKf;AAED;;;;;;;GAOG;AACH,wBAAgB,qCAAqC,CACnD,iBAAiB,EAAE,iBAAiB,GACnC,aAAa,EAAE,CAcjB;AAED;;;;;;;;GAQG;AACH,wBAAgB,0BAA0B,CAAC,gBAAgB,EAAE;IAC3D,OAAO,EAAE;QACP,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,iBAAiB,CAAC;KAC1B,EAAE,CAAC;CACL,GAAG,WAAW,EAAE,CAShB;AAQD;;;;;;;GAOG;AACH,eAAO,MAAM,iCAAiC,sBACzB,iBAAiB,WAC3B,WAAW,KACnB,iBAiBF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,8BAA8B,sBACtB,iBAAiB,YAC1B,WAAW,EAAE,KACtB,iBA8BF,CAAC"}
|