@metamask-previews/chain-agnostic-permission 0.2.0-preview-57dc1075 → 0.3.0-preview-b0b2930
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 +17 -1
- package/dist/adapters/{caip-permission-adapter-eth-accounts.cjs → caip-permission-adapter-accounts.cjs} +50 -2
- package/dist/adapters/caip-permission-adapter-accounts.cjs.map +1 -0
- package/dist/adapters/{caip-permission-adapter-eth-accounts.d.cts → caip-permission-adapter-accounts.d.cts} +10 -2
- package/dist/adapters/caip-permission-adapter-accounts.d.cts.map +1 -0
- package/dist/adapters/{caip-permission-adapter-eth-accounts.d.mts → caip-permission-adapter-accounts.d.mts} +10 -2
- package/dist/adapters/caip-permission-adapter-accounts.d.mts.map +1 -0
- package/dist/adapters/{caip-permission-adapter-eth-accounts.mjs → caip-permission-adapter-accounts.mjs} +48 -1
- package/dist/adapters/caip-permission-adapter-accounts.mjs.map +1 -0
- package/dist/adapters/caip-permission-adapter-permittedChains.cjs +64 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.cjs.map +1 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.d.cts +18 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.d.cts.map +1 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.d.mts +18 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.d.mts.map +1 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.mjs +62 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.mjs.map +1 -1
- package/dist/caip25Permission.cjs +30 -1
- package/dist/caip25Permission.cjs.map +1 -1
- package/dist/caip25Permission.d.cts +17 -1
- package/dist/caip25Permission.d.cts.map +1 -1
- package/dist/caip25Permission.d.mts +17 -1
- package/dist/caip25Permission.d.mts.map +1 -1
- package/dist/caip25Permission.mjs +28 -0
- package/dist/caip25Permission.mjs.map +1 -1
- package/dist/index.cjs +10 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +4 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +4 -3
- package/dist/index.mjs.map +1 -1
- package/dist/scope/types.cjs +12 -1
- package/dist/scope/types.cjs.map +1 -1
- package/dist/scope/types.d.cts +9 -1
- package/dist/scope/types.d.cts.map +1 -1
- package/dist/scope/types.d.mts +9 -1
- package/dist/scope/types.d.mts.map +1 -1
- package/dist/scope/types.mjs +11 -1
- package/dist/scope/types.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/adapters/caip-permission-adapter-eth-accounts.cjs.map +0 -1
- package/dist/adapters/caip-permission-adapter-eth-accounts.d.cts.map +0 -1
- package/dist/adapters/caip-permission-adapter-eth-accounts.d.mts.map +0 -1
- package/dist/adapters/caip-permission-adapter-eth-accounts.mjs.map +0 -1
package/CHANGELOG.md
CHANGED
@@ -7,6 +7,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
7
7
|
|
8
8
|
## [Unreleased]
|
9
9
|
|
10
|
+
## [0.3.0]
|
11
|
+
|
12
|
+
### Added
|
13
|
+
|
14
|
+
- Export `KnownSessionProperties` enum ([#5522](https://github.com/MetaMask/core/pull/5522))
|
15
|
+
- Add more chain agnostic utility functions for interfacing w/ caip25 permission ([#5536](https://github.com/MetaMask/core/pull/5536))
|
16
|
+
- New `setPermittedAccounts` function that allows setting accounts for any CAIP namespace, not just EVM scopes.
|
17
|
+
- New `addPermittedChainId` and `setPermittedChainIds` functions for managing permitted chains across any CAIP namespace.
|
18
|
+
- New `generateCaip25Caveat` function to generate a valid `endowment:caip25` permission caveat from given accounts and chains of any CAIP namespace.
|
19
|
+
- New `isWalletScope` utility function to detect wallet-related scopes.
|
20
|
+
|
21
|
+
### Changed
|
22
|
+
|
23
|
+
- **BREAKING:** An error is now thrown in the caveat validator when a `caip25:endowment` permission caveat has no scopes in either `requiredScopes` or `optionalScopes` ([#5548](https://github.com/MetaMask/core/pull/5548))
|
24
|
+
|
10
25
|
## [0.2.0]
|
11
26
|
|
12
27
|
### Added
|
@@ -28,6 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
28
43
|
|
29
44
|
- Initial release
|
30
45
|
|
31
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.
|
46
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.3.0...HEAD
|
47
|
+
[0.3.0]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.2.0...@metamask/chain-agnostic-permission@0.3.0
|
32
48
|
[0.2.0]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.1.0...@metamask/chain-agnostic-permission@0.2.0
|
33
49
|
[0.1.0]: https://github.com/MetaMask/core/releases/tag/@metamask/chain-agnostic-permission@0.1.0
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.setEthAccounts = exports.getEthAccounts = void 0;
|
3
|
+
exports.setPermittedAccounts = exports.setEthAccounts = exports.getEthAccounts = void 0;
|
4
4
|
const utils_1 = require("@metamask/utils");
|
5
5
|
const constants_1 = require("../scope/constants.cjs");
|
6
6
|
const transform_1 = require("../scope/transform.cjs");
|
@@ -100,4 +100,52 @@ const setEthAccounts = (caip25CaveatValue, accounts) => {
|
|
100
100
|
};
|
101
101
|
};
|
102
102
|
exports.setEthAccounts = setEthAccounts;
|
103
|
-
|
103
|
+
/**
|
104
|
+
* Sets the permitted accounts to scopes with matching namespaces in the given scopes object.
|
105
|
+
*
|
106
|
+
* @param scopesObject - The scopes object to set the permitted accounts for.
|
107
|
+
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
108
|
+
* @returns The updated scopes object with the permitted accounts set.
|
109
|
+
*/
|
110
|
+
const setPermittedAccountsForScopesObject = (scopesObject, accounts) => {
|
111
|
+
const updatedScopesObject = {};
|
112
|
+
Object.entries(scopesObject).forEach(([key, scopeObject]) => {
|
113
|
+
// Cast needed because index type is returned as `string` by `Object.entries`
|
114
|
+
const scopeString = key;
|
115
|
+
const { namespace, reference } = (0, types_1.parseScopeString)(scopeString);
|
116
|
+
let caipAccounts = [];
|
117
|
+
if (namespace && reference) {
|
118
|
+
caipAccounts = accounts.reduce((acc, account) => {
|
119
|
+
const { chain: { namespace: accountNamespace }, address: accountAddress, } = (0, utils_1.parseCaipAccountId)(account);
|
120
|
+
// If the account namespace is the same as the scope namespace, add the account to the scope
|
121
|
+
// This will, for example, distribute all EIP155 accounts, regardless of reference, to all EIP155 scopes
|
122
|
+
if (namespace === accountNamespace) {
|
123
|
+
acc.push(`${namespace}:${reference}:${accountAddress}`);
|
124
|
+
}
|
125
|
+
return acc;
|
126
|
+
}, []);
|
127
|
+
}
|
128
|
+
const uniqueCaipAccounts = (0, transform_1.getUniqueArrayItems)(caipAccounts);
|
129
|
+
updatedScopesObject[scopeString] = {
|
130
|
+
...scopeObject,
|
131
|
+
accounts: uniqueCaipAccounts,
|
132
|
+
};
|
133
|
+
});
|
134
|
+
return updatedScopesObject;
|
135
|
+
};
|
136
|
+
/**
|
137
|
+
* Sets the permitted accounts to scopes with matching namespaces in the given CAIP-25 caveat value.
|
138
|
+
*
|
139
|
+
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
|
140
|
+
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
141
|
+
* @returns The updated CAIP-25 caveat value with the permitted accounts set.
|
142
|
+
*/
|
143
|
+
const setPermittedAccounts = (caip25CaveatValue, accounts) => {
|
144
|
+
return {
|
145
|
+
...caip25CaveatValue,
|
146
|
+
requiredScopes: setPermittedAccountsForScopesObject(caip25CaveatValue.requiredScopes, accounts),
|
147
|
+
optionalScopes: setPermittedAccountsForScopesObject(caip25CaveatValue.optionalScopes, accounts),
|
148
|
+
};
|
149
|
+
};
|
150
|
+
exports.setPermittedAccounts = setPermittedAccounts;
|
151
|
+
//# sourceMappingURL=caip-permission-adapter-accounts.cjs.map
|
@@ -0,0 +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;;;;;GAKG;AACI,MAAM,cAAc,GAAG,CAC5B,iBAGC,EACM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B,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,4 +1,4 @@
|
|
1
|
-
import { type Hex } from "@metamask/utils";
|
1
|
+
import { type CaipAccountId, type Hex } from "@metamask/utils";
|
2
2
|
import type { Caip25CaveatValue } from "../caip25Permission.cjs";
|
3
3
|
/**
|
4
4
|
* Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.
|
@@ -17,4 +17,12 @@ export declare const getEthAccounts: (caip25CaveatValue: Pick<Caip25CaveatValue,
|
|
17
17
|
* @returns The updated CAIP-25 caveat value with the Ethereum accounts set.
|
18
18
|
*/
|
19
19
|
export declare const setEthAccounts: (caip25CaveatValue: Caip25CaveatValue, accounts: Hex[]) => Caip25CaveatValue;
|
20
|
-
|
20
|
+
/**
|
21
|
+
* Sets the permitted accounts to scopes with matching namespaces in the given CAIP-25 caveat value.
|
22
|
+
*
|
23
|
+
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
|
24
|
+
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
25
|
+
* @returns The updated CAIP-25 caveat value with the permitted accounts set.
|
26
|
+
*/
|
27
|
+
export declare const setPermittedAccounts: (caip25CaveatValue: Caip25CaveatValue, accounts: CaipAccountId[]) => Caip25CaveatValue;
|
28
|
+
//# sourceMappingURL=caip-permission-adapter-accounts.d.cts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"caip-permission-adapter-accounts.d.cts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-accounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,aAAa,EAClB,KAAK,GAAG,EAGT,wBAAwB;AAEzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAgD7D;;;;;GAKG;AACH,eAAO,MAAM,cAAc,sBACN,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC,KACA,GAAG,EASL,CAAC;AAwCF;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,sBACN,iBAAiB,YAC1B,GAAG,EAAE,KACd,iBAYF,CAAC;AA8CF;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,sBACZ,iBAAiB,YAC1B,aAAa,EAAE,KACxB,iBAYF,CAAC"}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { type Hex } from "@metamask/utils";
|
1
|
+
import { type CaipAccountId, type Hex } from "@metamask/utils";
|
2
2
|
import type { Caip25CaveatValue } from "../caip25Permission.mjs";
|
3
3
|
/**
|
4
4
|
* Gets the Ethereum (EIP155 namespaced) accounts from the required and optional scopes.
|
@@ -17,4 +17,12 @@ export declare const getEthAccounts: (caip25CaveatValue: Pick<Caip25CaveatValue,
|
|
17
17
|
* @returns The updated CAIP-25 caveat value with the Ethereum accounts set.
|
18
18
|
*/
|
19
19
|
export declare const setEthAccounts: (caip25CaveatValue: Caip25CaveatValue, accounts: Hex[]) => Caip25CaveatValue;
|
20
|
-
|
20
|
+
/**
|
21
|
+
* Sets the permitted accounts to scopes with matching namespaces in the given CAIP-25 caveat value.
|
22
|
+
*
|
23
|
+
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
|
24
|
+
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
25
|
+
* @returns The updated CAIP-25 caveat value with the permitted accounts set.
|
26
|
+
*/
|
27
|
+
export declare const setPermittedAccounts: (caip25CaveatValue: Caip25CaveatValue, accounts: CaipAccountId[]) => Caip25CaveatValue;
|
28
|
+
//# sourceMappingURL=caip-permission-adapter-accounts.d.mts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"caip-permission-adapter-accounts.d.mts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-accounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,aAAa,EAClB,KAAK,GAAG,EAGT,wBAAwB;AAEzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAgD7D;;;;;GAKG;AACH,eAAO,MAAM,cAAc,sBACN,KACjB,iBAAiB,EACjB,gBAAgB,GAAG,gBAAgB,CACpC,KACA,GAAG,EASL,CAAC;AAwCF;;;;;;;;GAQG;AACH,eAAO,MAAM,cAAc,sBACN,iBAAiB,YAC1B,GAAG,EAAE,KACd,iBAYF,CAAC;AA8CF;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,sBACZ,iBAAiB,YAC1B,aAAa,EAAE,KACxB,iBAYF,CAAC"}
|
@@ -95,4 +95,51 @@ export const setEthAccounts = (caip25CaveatValue, accounts) => {
|
|
95
95
|
optionalScopes: setEthAccountsForScopesObject(caip25CaveatValue.optionalScopes, accounts),
|
96
96
|
};
|
97
97
|
};
|
98
|
-
|
98
|
+
/**
|
99
|
+
* Sets the permitted accounts to scopes with matching namespaces in the given scopes object.
|
100
|
+
*
|
101
|
+
* @param scopesObject - The scopes object to set the permitted accounts for.
|
102
|
+
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
103
|
+
* @returns The updated scopes object with the permitted accounts set.
|
104
|
+
*/
|
105
|
+
const setPermittedAccountsForScopesObject = (scopesObject, accounts) => {
|
106
|
+
const updatedScopesObject = {};
|
107
|
+
Object.entries(scopesObject).forEach(([key, scopeObject]) => {
|
108
|
+
// Cast needed because index type is returned as `string` by `Object.entries`
|
109
|
+
const scopeString = key;
|
110
|
+
const { namespace, reference } = parseScopeString(scopeString);
|
111
|
+
let caipAccounts = [];
|
112
|
+
if (namespace && reference) {
|
113
|
+
caipAccounts = accounts.reduce((acc, account) => {
|
114
|
+
const { chain: { namespace: accountNamespace }, address: accountAddress, } = parseCaipAccountId(account);
|
115
|
+
// If the account namespace is the same as the scope namespace, add the account to the scope
|
116
|
+
// This will, for example, distribute all EIP155 accounts, regardless of reference, to all EIP155 scopes
|
117
|
+
if (namespace === accountNamespace) {
|
118
|
+
acc.push(`${namespace}:${reference}:${accountAddress}`);
|
119
|
+
}
|
120
|
+
return acc;
|
121
|
+
}, []);
|
122
|
+
}
|
123
|
+
const uniqueCaipAccounts = getUniqueArrayItems(caipAccounts);
|
124
|
+
updatedScopesObject[scopeString] = {
|
125
|
+
...scopeObject,
|
126
|
+
accounts: uniqueCaipAccounts,
|
127
|
+
};
|
128
|
+
});
|
129
|
+
return updatedScopesObject;
|
130
|
+
};
|
131
|
+
/**
|
132
|
+
* Sets the permitted accounts to scopes with matching namespaces in the given CAIP-25 caveat value.
|
133
|
+
*
|
134
|
+
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
|
135
|
+
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
136
|
+
* @returns The updated CAIP-25 caveat value with the permitted accounts set.
|
137
|
+
*/
|
138
|
+
export const setPermittedAccounts = (caip25CaveatValue, accounts) => {
|
139
|
+
return {
|
140
|
+
...caip25CaveatValue,
|
141
|
+
requiredScopes: setPermittedAccountsForScopesObject(caip25CaveatValue.requiredScopes, accounts),
|
142
|
+
optionalScopes: setPermittedAccountsForScopesObject(caip25CaveatValue.optionalScopes, accounts),
|
143
|
+
};
|
144
|
+
};
|
145
|
+
//# sourceMappingURL=caip-permission-adapter-accounts.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"caip-permission-adapter-accounts.mjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-accounts.ts"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B;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,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.setPermittedEthChainIds = exports.addPermittedEthChainId = exports.getPermittedEthChainIds = void 0;
|
3
|
+
exports.setPermittedChainIds = exports.addPermittedChainId = 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
6
|
const transform_1 = require("../scope/transform.cjs");
|
@@ -112,4 +112,67 @@ const setPermittedEthChainIds = (caip25CaveatValue, chainIds) => {
|
|
112
112
|
return updatedCaveatValue;
|
113
113
|
};
|
114
114
|
exports.setPermittedEthChainIds = setPermittedEthChainIds;
|
115
|
+
/**
|
116
|
+
* Filters the scopes object to only include:
|
117
|
+
* - Scopes without references (e.g. "wallet:")
|
118
|
+
* - CAIP-2 ChainId scopes for the given chainIDs
|
119
|
+
*
|
120
|
+
* @param scopesObject - The scopes object to filter.
|
121
|
+
* @param chainIds - The CAIP-2 chainIDs to filter for.
|
122
|
+
* @returns The filtered scopes object.
|
123
|
+
*/
|
124
|
+
const filterChainScopesObjectByChainId = (scopesObject, chainIds) => {
|
125
|
+
const updatedScopesObject = {};
|
126
|
+
Object.entries(scopesObject).forEach(([key, scopeObject]) => {
|
127
|
+
// Cast needed because index type is returned as `string` by `Object.entries`
|
128
|
+
const scopeString = key;
|
129
|
+
if ((0, types_1.isWalletScope)(scopeString) || chainIds.includes(scopeString)) {
|
130
|
+
updatedScopesObject[scopeString] = scopeObject;
|
131
|
+
}
|
132
|
+
});
|
133
|
+
return updatedScopesObject;
|
134
|
+
};
|
135
|
+
/**
|
136
|
+
* Adds a chainID to the optional scopes if it is not already present
|
137
|
+
* in either the pre-existing required or optional scopes.
|
138
|
+
*
|
139
|
+
* @param caip25CaveatValue - The CAIP-25 caveat value to add the chainID to.
|
140
|
+
* @param chainId - The chainID to add.
|
141
|
+
* @returns The updated CAIP-25 caveat value with the added chainID.
|
142
|
+
*/
|
143
|
+
const addPermittedChainId = (caip25CaveatValue, chainId) => {
|
144
|
+
if (Object.keys(caip25CaveatValue.requiredScopes).includes(chainId) ||
|
145
|
+
Object.keys(caip25CaveatValue.optionalScopes).includes(chainId)) {
|
146
|
+
return caip25CaveatValue;
|
147
|
+
}
|
148
|
+
return {
|
149
|
+
...caip25CaveatValue,
|
150
|
+
optionalScopes: {
|
151
|
+
...caip25CaveatValue.optionalScopes,
|
152
|
+
[chainId]: {
|
153
|
+
accounts: [],
|
154
|
+
},
|
155
|
+
},
|
156
|
+
};
|
157
|
+
};
|
158
|
+
exports.addPermittedChainId = addPermittedChainId;
|
159
|
+
/**
|
160
|
+
* Sets the permitted CAIP-2 chainIDs for the required and optional scopes.
|
161
|
+
*
|
162
|
+
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted CAIP-2 chainIDs for.
|
163
|
+
* @param chainIds - The CAIP-2 chainIDs to set as permitted.
|
164
|
+
* @returns The updated CAIP-25 caveat value with the permitted CAIP-2 chainIDs.
|
165
|
+
*/
|
166
|
+
const setPermittedChainIds = (caip25CaveatValue, chainIds) => {
|
167
|
+
let updatedCaveatValue = {
|
168
|
+
...caip25CaveatValue,
|
169
|
+
requiredScopes: filterChainScopesObjectByChainId(caip25CaveatValue.requiredScopes, chainIds),
|
170
|
+
optionalScopes: filterChainScopesObjectByChainId(caip25CaveatValue.optionalScopes, chainIds),
|
171
|
+
};
|
172
|
+
chainIds.forEach((chainId) => {
|
173
|
+
updatedCaveatValue = (0, exports.addPermittedChainId)(updatedCaveatValue, chainId);
|
174
|
+
});
|
175
|
+
return updatedCaveatValue;
|
176
|
+
};
|
177
|
+
exports.setPermittedChainIds = setPermittedChainIds;
|
115
178
|
//# 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,8CAAkD;AAElD;;;;;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,GAAG,iBAAiB,CAAC;IAE7D,MAAM,WAAW,GAAU;QACzB,GAAG,iCAAiC,CAAC,cAAc,CAAC;QACpD,GAAG,iCAAiC,CAAC,cAAc,CAAC;KACrD,CAAC;IAEF,OAAO,IAAA,+BAAmB,EAAC,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AAC,SAAS,CAAC,CAAC;YACjC,IAAI,QAAQ,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;gBAC9B,mBAAmB,CAAC,WAAW,CAAC,GAAG,WAAW,CAAC;aAChD;SACF;aAAM;YACL,mBAAmB,CAAC,WAAW,CAAC,GAAG,WAAW,CAAC;SAChD;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,mBAAmB,CAAC;AAC7B,CAAC,CAAC;AAEF;;;;;;GAMG;AACI,MAAM,uBAAuB,GAAG,CACrC,iBAAoC,EACpC,QAAe,EACI,EAAE;IACrB,IAAI,kBAAkB,GAAsB;QAC1C,GAAG,iBAAiB;QACpB,cAAc,EAAE,8BAA8B,CAC5C,iBAAiB,CAAC,cAAc,EAChC,QAAQ,CACT;QACD,cAAc,EAAE,8BAA8B,CAC5C,iBAAiB,CAAC,cAAc,EAChC,QAAQ,CACT;KACF,CAAC;IAEF,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC3B,kBAAkB,GAAG,IAAA,8BAAsB,EAAC,kBAAkB,EAAE,OAAO,CAAC,CAAC;IAC3E,CAAC,CAAC,CAAC;IAEH,OAAO,kBAAkB,CAAC;AAC5B,CAAC,CAAC;AArBW,QAAA,uBAAuB,2BAqBlC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport type { Hex } 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 { 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"]}
|
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,MA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,mBAAmB,uBAoB9B;AAEF;;;;;;GAMG;AACI,MAAM,oBAAoB,GAAG,CAClC,iBAAoC,EACpC,QAAuB,EACJ,EAAE;IACrB,IAAI,kBAAkB,GAAsB;QAC1C,GAAG,iBAAiB;QACpB,cAAc,EAAE,gCAAgC,CAC9C,iBAAiB,CAAC,cAAc,EAChC,QAAQ,CACT;QACD,cAAc,EAAE,gCAAgC,CAC9C,iBAAiB,CAAC,cAAc,EAChC,QAAQ,CACT;KACF,CAAC;IAEF,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC3B,kBAAkB,GAAG,IAAA,2BAAmB,EAAC,kBAAkB,EAAE,OAAO,CAAC,CAAC;IACxE,CAAC,CAAC,CAAC;IAEH,OAAO,kBAAkB,CAAC;AAC5B,CAAC,CAAC;AArBW,QAAA,oBAAoB,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,4 +1,4 @@
|
|
1
|
-
import type { Hex } from "@metamask/utils";
|
1
|
+
import type { Hex, CaipChainId } from "@metamask/utils";
|
2
2
|
import type { Caip25CaveatValue } from "../caip25Permission.cjs";
|
3
3
|
/**
|
4
4
|
* Gets the Ethereum (EIP155 namespaced) chainIDs from the required and optional scopes.
|
@@ -24,4 +24,21 @@ export declare const addPermittedEthChainId: (caip25CaveatValue: Caip25CaveatVal
|
|
24
24
|
* @returns The updated CAIP-25 caveat value with the permitted Ethereum chainIDs.
|
25
25
|
*/
|
26
26
|
export declare const setPermittedEthChainIds: (caip25CaveatValue: Caip25CaveatValue, chainIds: Hex[]) => Caip25CaveatValue;
|
27
|
+
/**
|
28
|
+
* Adds a chainID to the optional scopes if it is not already present
|
29
|
+
* in either the pre-existing required or optional scopes.
|
30
|
+
*
|
31
|
+
* @param caip25CaveatValue - The CAIP-25 caveat value to add the chainID to.
|
32
|
+
* @param chainId - The chainID to add.
|
33
|
+
* @returns The updated CAIP-25 caveat value with the added chainID.
|
34
|
+
*/
|
35
|
+
export declare const addPermittedChainId: (caip25CaveatValue: Caip25CaveatValue, chainId: CaipChainId) => Caip25CaveatValue;
|
36
|
+
/**
|
37
|
+
* Sets the permitted CAIP-2 chainIDs for the required and optional scopes.
|
38
|
+
*
|
39
|
+
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted CAIP-2 chainIDs for.
|
40
|
+
* @param chainIds - The CAIP-2 chainIDs to set as permitted.
|
41
|
+
* @returns The updated CAIP-25 caveat value with the permitted CAIP-2 chainIDs.
|
42
|
+
*/
|
43
|
+
export declare const setPermittedChainIds: (caip25CaveatValue: Caip25CaveatValue, chainIds: CaipChainId[]) => Caip25CaveatValue;
|
27
44
|
//# 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,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,wBAAwB;AAGxD,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAwB7D;;;;;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;AA4BF;;;;;;;GAOG;AACH,eAAO,MAAM,mBAAmB,sBACX,iBAAiB,WAC3B,WAAW,KACnB,iBAiBF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,sBACZ,iBAAiB,YAC1B,WAAW,EAAE,KACtB,iBAkBF,CAAC"}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import type { Hex } from "@metamask/utils";
|
1
|
+
import type { Hex, CaipChainId } from "@metamask/utils";
|
2
2
|
import type { Caip25CaveatValue } from "../caip25Permission.mjs";
|
3
3
|
/**
|
4
4
|
* Gets the Ethereum (EIP155 namespaced) chainIDs from the required and optional scopes.
|
@@ -24,4 +24,21 @@ export declare const addPermittedEthChainId: (caip25CaveatValue: Caip25CaveatVal
|
|
24
24
|
* @returns The updated CAIP-25 caveat value with the permitted Ethereum chainIDs.
|
25
25
|
*/
|
26
26
|
export declare const setPermittedEthChainIds: (caip25CaveatValue: Caip25CaveatValue, chainIds: Hex[]) => Caip25CaveatValue;
|
27
|
+
/**
|
28
|
+
* Adds a chainID to the optional scopes if it is not already present
|
29
|
+
* in either the pre-existing required or optional scopes.
|
30
|
+
*
|
31
|
+
* @param caip25CaveatValue - The CAIP-25 caveat value to add the chainID to.
|
32
|
+
* @param chainId - The chainID to add.
|
33
|
+
* @returns The updated CAIP-25 caveat value with the added chainID.
|
34
|
+
*/
|
35
|
+
export declare const addPermittedChainId: (caip25CaveatValue: Caip25CaveatValue, chainId: CaipChainId) => Caip25CaveatValue;
|
36
|
+
/**
|
37
|
+
* Sets the permitted CAIP-2 chainIDs for the required and optional scopes.
|
38
|
+
*
|
39
|
+
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted CAIP-2 chainIDs for.
|
40
|
+
* @param chainIds - The CAIP-2 chainIDs to set as permitted.
|
41
|
+
* @returns The updated CAIP-25 caveat value with the permitted CAIP-2 chainIDs.
|
42
|
+
*/
|
43
|
+
export declare const setPermittedChainIds: (caip25CaveatValue: Caip25CaveatValue, chainIds: CaipChainId[]) => Caip25CaveatValue;
|
27
44
|
//# sourceMappingURL=caip-permission-adapter-permittedChains.d.mts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"caip-permission-adapter-permittedChains.d.mts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-permittedChains.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,wBAAwB;
|
1
|
+
{"version":3,"file":"caip-permission-adapter-permittedChains.d.mts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-permittedChains.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,WAAW,EAAE,wBAAwB;AAGxD,OAAO,KAAK,EAAE,iBAAiB,EAAE,gCAA4B;AAwB7D;;;;;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;AA4BF;;;;;;;GAOG;AACH,eAAO,MAAM,mBAAmB,sBACX,iBAAiB,WAC3B,WAAW,KACnB,iBAiBF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,sBACZ,iBAAiB,YAC1B,WAAW,EAAE,KACtB,iBAkBF,CAAC"}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { toHex } from "@metamask/controller-utils";
|
2
2
|
import { hexToBigInt, KnownCaipNamespace } from "@metamask/utils";
|
3
3
|
import { getUniqueArrayItems } from "../scope/transform.mjs";
|
4
|
-
import { parseScopeString } from "../scope/types.mjs";
|
4
|
+
import { isWalletScope, parseScopeString } from "../scope/types.mjs";
|
5
5
|
/**
|
6
6
|
* Gets the Ethereum (EIP155 namespaced) chainIDs from internal scopes.
|
7
7
|
*
|
@@ -106,4 +106,65 @@ export const setPermittedEthChainIds = (caip25CaveatValue, chainIds) => {
|
|
106
106
|
});
|
107
107
|
return updatedCaveatValue;
|
108
108
|
};
|
109
|
+
/**
|
110
|
+
* Filters the scopes object to only include:
|
111
|
+
* - Scopes without references (e.g. "wallet:")
|
112
|
+
* - CAIP-2 ChainId scopes for the given chainIDs
|
113
|
+
*
|
114
|
+
* @param scopesObject - The scopes object to filter.
|
115
|
+
* @param chainIds - The CAIP-2 chainIDs to filter for.
|
116
|
+
* @returns The filtered scopes object.
|
117
|
+
*/
|
118
|
+
const filterChainScopesObjectByChainId = (scopesObject, chainIds) => {
|
119
|
+
const updatedScopesObject = {};
|
120
|
+
Object.entries(scopesObject).forEach(([key, scopeObject]) => {
|
121
|
+
// Cast needed because index type is returned as `string` by `Object.entries`
|
122
|
+
const scopeString = key;
|
123
|
+
if (isWalletScope(scopeString) || chainIds.includes(scopeString)) {
|
124
|
+
updatedScopesObject[scopeString] = scopeObject;
|
125
|
+
}
|
126
|
+
});
|
127
|
+
return updatedScopesObject;
|
128
|
+
};
|
129
|
+
/**
|
130
|
+
* Adds a chainID to the optional scopes if it is not already present
|
131
|
+
* in either the pre-existing required or optional scopes.
|
132
|
+
*
|
133
|
+
* @param caip25CaveatValue - The CAIP-25 caveat value to add the chainID to.
|
134
|
+
* @param chainId - The chainID to add.
|
135
|
+
* @returns The updated CAIP-25 caveat value with the added chainID.
|
136
|
+
*/
|
137
|
+
export const addPermittedChainId = (caip25CaveatValue, chainId) => {
|
138
|
+
if (Object.keys(caip25CaveatValue.requiredScopes).includes(chainId) ||
|
139
|
+
Object.keys(caip25CaveatValue.optionalScopes).includes(chainId)) {
|
140
|
+
return caip25CaveatValue;
|
141
|
+
}
|
142
|
+
return {
|
143
|
+
...caip25CaveatValue,
|
144
|
+
optionalScopes: {
|
145
|
+
...caip25CaveatValue.optionalScopes,
|
146
|
+
[chainId]: {
|
147
|
+
accounts: [],
|
148
|
+
},
|
149
|
+
},
|
150
|
+
};
|
151
|
+
};
|
152
|
+
/**
|
153
|
+
* Sets the permitted CAIP-2 chainIDs for the required and optional scopes.
|
154
|
+
*
|
155
|
+
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted CAIP-2 chainIDs for.
|
156
|
+
* @param chainIds - The CAIP-2 chainIDs to set as permitted.
|
157
|
+
* @returns The updated CAIP-25 caveat value with the permitted CAIP-2 chainIDs.
|
158
|
+
*/
|
159
|
+
export const setPermittedChainIds = (caip25CaveatValue, chainIds) => {
|
160
|
+
let updatedCaveatValue = {
|
161
|
+
...caip25CaveatValue,
|
162
|
+
requiredScopes: filterChainScopesObjectByChainId(caip25CaveatValue.requiredScopes, chainIds),
|
163
|
+
optionalScopes: filterChainScopesObjectByChainId(caip25CaveatValue.optionalScopes, chainIds),
|
164
|
+
};
|
165
|
+
chainIds.forEach((chainId) => {
|
166
|
+
updatedCaveatValue = addPermittedChainId(updatedCaveatValue, chainId);
|
167
|
+
});
|
168
|
+
return updatedCaveatValue;
|
169
|
+
};
|
109
170
|
//# sourceMappingURL=caip-permission-adapter-permittedChains.mjs.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"caip-permission-adapter-permittedChains.mjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-permittedChains.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,mCAAmC;AAEnD,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAA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AAiB,CAAC,cAAc,EAChC,QAAQ,CACT;QACD,cAAc,EAAE,8BAA8B,CAC5C,iBAAiB,CAAC,cAAc,EAChC,QAAQ,CACT;KACF,CAAC;IAEF,QAAQ,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC3B,kBAAkB,GAAG,sBAAsB,CAAC,kBAAkB,EAAE,OAAO,CAAC,CAAC;IAC3E,CAAC,CAAC,CAAC;IAEH,OAAO,kBAAkB,CAAC;AAC5B,CAAC,CAAC","sourcesContent":["import { toHex } from '@metamask/controller-utils';\nimport type { Hex } 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 { 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"]}
|
1
|
+
{"version":3,"file":"caip-permission-adapter-permittedChains.mjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-permittedChains.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,mCAAmC;AAEnD,OAAO,EAAE,WAAW,EAAE,kBAAkB,EAAE,wBAAwB;AAGlE,OAAO,EAAE,mBAAmB,EAAE,+BAA2B;AAEzD,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,2BAAuB;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,gBAAgB,CAAC,WAAW,CAAC,CAAC;QAC/D,IAAI,SAAS,KAAK,kBAAkB,CAAC,MAAM,IAAI,SAAS,EAAE;YACxD,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC;SACpC;IACH,CAAC,CAAC,CA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AAO,kBAAkB,CAAC;AAC5B,CAAC,CAAC","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,9 +1,11 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.Caip25CaveatMutators = exports.caip25EndowmentBuilder = exports.caip25CaveatBuilder = exports.diffScopesForCaip25CaveatValue = exports.createCaip25Caveat = exports.Caip25EndowmentPermissionName = exports.Caip25CaveatType = void 0;
|
3
|
+
exports.generateCaip25Caveat = exports.Caip25CaveatMutators = exports.caip25EndowmentBuilder = exports.caip25CaveatBuilder = exports.diffScopesForCaip25CaveatValue = exports.createCaip25Caveat = exports.Caip25EndowmentPermissionName = exports.Caip25CaveatType = void 0;
|
4
4
|
const permission_controller_1 = require("@metamask/permission-controller");
|
5
5
|
const utils_1 = require("@metamask/utils");
|
6
6
|
const lodash_1 = require("lodash");
|
7
|
+
const caip_permission_adapter_accounts_1 = require("./adapters/caip-permission-adapter-accounts.cjs");
|
8
|
+
const caip_permission_adapter_permittedChains_1 = require("./adapters/caip-permission-adapter-permittedChains.cjs");
|
7
9
|
const assert_1 = require("./scope/assert.cjs");
|
8
10
|
const supported_1 = require("./scope/supported.cjs");
|
9
11
|
const transform_1 = require("./scope/transform.cjs");
|
@@ -106,6 +108,10 @@ const caip25CaveatBuilder = ({ findNetworkClientIdByChainId, listAccounts, isNon
|
|
106
108
|
}
|
107
109
|
(0, assert_1.assertIsInternalScopesObject)(requiredScopes);
|
108
110
|
(0, assert_1.assertIsInternalScopesObject)(optionalScopes);
|
111
|
+
if (Object.keys(requiredScopes).length === 0 &&
|
112
|
+
Object.keys(optionalScopes).length === 0) {
|
113
|
+
throw new Error(`${exports.Caip25EndowmentPermissionName} error: Received no scopes for caveat of type "${exports.Caip25CaveatType}".`);
|
114
|
+
}
|
109
115
|
const isEvmChainIdSupported = (chainId) => {
|
110
116
|
try {
|
111
117
|
findNetworkClientIdByChainId(chainId);
|
@@ -293,4 +299,27 @@ function removeScope(caip25CaveatValue, targetScopeString) {
|
|
293
299
|
operation: permission_controller_1.CaveatMutatorOperation.RevokePermission,
|
294
300
|
};
|
295
301
|
}
|
302
|
+
/**
|
303
|
+
* Modifies the requested CAIP-25 permissions object after UI confirmation.
|
304
|
+
*
|
305
|
+
* @param caip25CaveatValue - The requested CAIP-25 caveat value to modify.
|
306
|
+
* @param accountAddresses - The list of permitted eth addresses.
|
307
|
+
* @param chainIds - The list of permitted eth chainIds.
|
308
|
+
* @returns The updated CAIP-25 caveat value with the permitted accounts and chainIds set.
|
309
|
+
*/
|
310
|
+
const generateCaip25Caveat = (caip25CaveatValue, accountAddresses, chainIds) => {
|
311
|
+
const caveatValueWithChains = (0, caip_permission_adapter_permittedChains_1.setPermittedChainIds)(caip25CaveatValue, chainIds);
|
312
|
+
const caveatValueWithAccounts = (0, caip_permission_adapter_accounts_1.setPermittedAccounts)(caveatValueWithChains, accountAddresses);
|
313
|
+
return {
|
314
|
+
[exports.Caip25EndowmentPermissionName]: {
|
315
|
+
caveats: [
|
316
|
+
{
|
317
|
+
type: exports.Caip25CaveatType,
|
318
|
+
value: caveatValueWithAccounts,
|
319
|
+
},
|
320
|
+
],
|
321
|
+
},
|
322
|
+
};
|
323
|
+
};
|
324
|
+
exports.generateCaip25Caveat = generateCaip25Caveat;
|
296
325
|
//# sourceMappingURL=caip25Permission.cjs.map
|