@metamask-previews/chain-agnostic-permission 0.2.0-preview-8c3b89c → 0.2.0-preview-35da3ac
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/dist/adapters/{caip-permission-adapter-accounts.cjs → caip-permission-adapter-eth-accounts.cjs} +2 -44
- package/dist/adapters/caip-permission-adapter-eth-accounts.cjs.map +1 -0
- package/dist/adapters/{caip-permission-adapter-accounts.d.cts → caip-permission-adapter-eth-accounts.d.cts} +2 -9
- package/dist/adapters/caip-permission-adapter-eth-accounts.d.cts.map +1 -0
- package/dist/adapters/{caip-permission-adapter-accounts.d.mts → caip-permission-adapter-eth-accounts.d.mts} +2 -9
- package/dist/adapters/caip-permission-adapter-eth-accounts.d.mts.map +1 -0
- package/dist/adapters/{caip-permission-adapter-accounts.mjs → caip-permission-adapter-eth-accounts.mjs} +1 -42
- package/dist/adapters/caip-permission-adapter-eth-accounts.mjs.map +1 -0
- package/dist/adapters/caip-permission-adapter-permittedChains.cjs +1 -65
- package/dist/adapters/caip-permission-adapter-permittedChains.cjs.map +1 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.d.cts +1 -16
- package/dist/adapters/caip-permission-adapter-permittedChains.d.cts.map +1 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.d.mts +1 -16
- package/dist/adapters/caip-permission-adapter-permittedChains.d.mts.map +1 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.mjs +1 -63
- package/dist/adapters/caip-permission-adapter-permittedChains.mjs.map +1 -1
- package/dist/caip25Permission.cjs +1 -25
- package/dist/caip25Permission.cjs.map +1 -1
- package/dist/caip25Permission.d.cts +1 -16
- package/dist/caip25Permission.d.cts.map +1 -1
- package/dist/caip25Permission.d.mts +1 -16
- package/dist/caip25Permission.d.mts.map +1 -1
- package/dist/caip25Permission.mjs +0 -23
- package/dist/caip25Permission.mjs.map +1 -1
- package/dist/index.cjs +4 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +3 -3
- package/dist/index.mjs.map +1 -1
- package/dist/scope/types.cjs +1 -11
- package/dist/scope/types.cjs.map +1 -1
- package/dist/scope/types.d.cts +1 -8
- package/dist/scope/types.d.cts.map +1 -1
- package/dist/scope/types.d.mts +1 -8
- package/dist/scope/types.d.mts.map +1 -1
- package/dist/scope/types.mjs +1 -10
- package/dist/scope/types.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/adapters/caip-permission-adapter-accounts.cjs.map +0 -1
- package/dist/adapters/caip-permission-adapter-accounts.d.cts.map +0 -1
- package/dist/adapters/caip-permission-adapter-accounts.d.mts.map +0 -1
- package/dist/adapters/caip-permission-adapter-accounts.mjs.map +0 -1
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
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,46 +100,4 @@ const setEthAccounts = (caip25CaveatValue, accounts) => {
|
|
100
100
|
};
|
101
101
|
};
|
102
102
|
exports.setEthAccounts = setEthAccounts;
|
103
|
-
|
104
|
-
* Sets the permitted accounts for the given scopes object.
|
105
|
-
* @param scopesObject - The scopes object to set the permitted accounts for.
|
106
|
-
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
107
|
-
* @returns The updated scopes object with the permitted accounts set.
|
108
|
-
*/
|
109
|
-
const setPermittedAccountsForScopesObject = (scopesObject, accounts) => {
|
110
|
-
const updatedScopesObject = {};
|
111
|
-
Object.entries(scopesObject).forEach(([key, scopeObject]) => {
|
112
|
-
// Cast needed because index type is returned as `string` by `Object.entries`
|
113
|
-
const scopeString = key;
|
114
|
-
const { namespace, reference } = (0, types_1.parseScopeString)(scopeString);
|
115
|
-
let caipAccounts = [];
|
116
|
-
if (namespace && reference) {
|
117
|
-
caipAccounts = accounts.reduce((acc, account) => {
|
118
|
-
if (account.startsWith(`${namespace}:${reference}`)) {
|
119
|
-
acc.push(account);
|
120
|
-
}
|
121
|
-
return acc;
|
122
|
-
}, []);
|
123
|
-
}
|
124
|
-
updatedScopesObject[scopeString] = {
|
125
|
-
...scopeObject,
|
126
|
-
accounts: caipAccounts,
|
127
|
-
};
|
128
|
-
});
|
129
|
-
return updatedScopesObject;
|
130
|
-
};
|
131
|
-
/**
|
132
|
-
* Sets the permitted accounts for the given CAIP-25 caveat value.
|
133
|
-
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
|
134
|
-
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
135
|
-
* @returns The updated CAIP-25 caveat value with the permitted accounts set.
|
136
|
-
*/
|
137
|
-
const setPermittedAccounts = (caip25CaveatValue, accounts) => {
|
138
|
-
return {
|
139
|
-
...caip25CaveatValue,
|
140
|
-
requiredScopes: setPermittedAccountsForScopesObject(caip25CaveatValue.requiredScopes, accounts),
|
141
|
-
optionalScopes: setPermittedAccountsForScopesObject(caip25CaveatValue.optionalScopes, accounts),
|
142
|
-
};
|
143
|
-
};
|
144
|
-
exports.setPermittedAccounts = setPermittedAccounts;
|
145
|
-
//# sourceMappingURL=caip-permission-adapter-accounts.cjs.map
|
103
|
+
//# sourceMappingURL=caip-permission-adapter-eth-accounts.cjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"caip-permission-adapter-eth-accounts.cjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-eth-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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ccountId,\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"]}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { type
|
1
|
+
import { 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,11 +17,4 @@ 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
|
-
|
21
|
-
* Sets the permitted accounts for the given CAIP-25 caveat value.
|
22
|
-
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
|
23
|
-
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
24
|
-
* @returns The updated CAIP-25 caveat value with the permitted accounts set.
|
25
|
-
*/
|
26
|
-
export declare const setPermittedAccounts: (caip25CaveatValue: Caip25CaveatValue, accounts: CaipAccountId[]) => Caip25CaveatValue;
|
27
|
-
//# sourceMappingURL=caip-permission-adapter-accounts.d.cts.map
|
20
|
+
//# sourceMappingURL=caip-permission-adapter-eth-accounts.d.cts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"caip-permission-adapter-eth-accounts.d.cts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-eth-accounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,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"}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { type
|
1
|
+
import { 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,11 +17,4 @@ 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
|
-
|
21
|
-
* Sets the permitted accounts for the given CAIP-25 caveat value.
|
22
|
-
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
|
23
|
-
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
24
|
-
* @returns The updated CAIP-25 caveat value with the permitted accounts set.
|
25
|
-
*/
|
26
|
-
export declare const setPermittedAccounts: (caip25CaveatValue: Caip25CaveatValue, accounts: CaipAccountId[]) => Caip25CaveatValue;
|
27
|
-
//# sourceMappingURL=caip-permission-adapter-accounts.d.mts.map
|
20
|
+
//# sourceMappingURL=caip-permission-adapter-eth-accounts.d.mts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"caip-permission-adapter-eth-accounts.d.mts","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-eth-accounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,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"}
|
@@ -95,45 +95,4 @@ export const setEthAccounts = (caip25CaveatValue, accounts) => {
|
|
95
95
|
optionalScopes: setEthAccountsForScopesObject(caip25CaveatValue.optionalScopes, accounts),
|
96
96
|
};
|
97
97
|
};
|
98
|
-
|
99
|
-
* Sets the permitted accounts for the given scopes object.
|
100
|
-
* @param scopesObject - The scopes object to set the permitted accounts for.
|
101
|
-
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
102
|
-
* @returns The updated scopes object with the permitted accounts set.
|
103
|
-
*/
|
104
|
-
const setPermittedAccountsForScopesObject = (scopesObject, accounts) => {
|
105
|
-
const updatedScopesObject = {};
|
106
|
-
Object.entries(scopesObject).forEach(([key, scopeObject]) => {
|
107
|
-
// Cast needed because index type is returned as `string` by `Object.entries`
|
108
|
-
const scopeString = key;
|
109
|
-
const { namespace, reference } = parseScopeString(scopeString);
|
110
|
-
let caipAccounts = [];
|
111
|
-
if (namespace && reference) {
|
112
|
-
caipAccounts = accounts.reduce((acc, account) => {
|
113
|
-
if (account.startsWith(`${namespace}:${reference}`)) {
|
114
|
-
acc.push(account);
|
115
|
-
}
|
116
|
-
return acc;
|
117
|
-
}, []);
|
118
|
-
}
|
119
|
-
updatedScopesObject[scopeString] = {
|
120
|
-
...scopeObject,
|
121
|
-
accounts: caipAccounts,
|
122
|
-
};
|
123
|
-
});
|
124
|
-
return updatedScopesObject;
|
125
|
-
};
|
126
|
-
/**
|
127
|
-
* Sets the permitted accounts for the given CAIP-25 caveat value.
|
128
|
-
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted accounts for.
|
129
|
-
* @param accounts - The permitted accounts to add to the appropriate scopes.
|
130
|
-
* @returns The updated CAIP-25 caveat value with the permitted accounts set.
|
131
|
-
*/
|
132
|
-
export const setPermittedAccounts = (caip25CaveatValue, accounts) => {
|
133
|
-
return {
|
134
|
-
...caip25CaveatValue,
|
135
|
-
requiredScopes: setPermittedAccountsForScopesObject(caip25CaveatValue.requiredScopes, accounts),
|
136
|
-
optionalScopes: setPermittedAccountsForScopesObject(caip25CaveatValue.optionalScopes, accounts),
|
137
|
-
};
|
138
|
-
};
|
139
|
-
//# sourceMappingURL=caip-permission-adapter-accounts.mjs.map
|
98
|
+
//# sourceMappingURL=caip-permission-adapter-eth-accounts.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"caip-permission-adapter-eth-accounts.mjs","sourceRoot":"","sources":["../../src/adapters/caip-permission-adapter-eth-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,GAAG,kBAAkB,CAAC,OAAO,CAAC,CAAC;YAEzD,IAAI,mBAAmB,CAAC,OAAO,CAAC,EAAE;gBAChC,2DAA2D;gBAC3D,kCAAkC;gBAClC,uBAAuB,CAAC,OAAO,CAAC,CAAC;gBACjC,WAAW,C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mB,CAAC,WAAW,CAAC,GAAG;YACjC,GAAG,WAAW;YACd,QAAQ,EAAE,YAAY;SACvB,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,OAAO,mBAAmB,CAAC;AAC7B,CAAC,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,iBAAoC,EACpC,QAAe,EACI,EAAE;IACrB,OAAO;QACL,GAAG,iBAAiB;QACpB,cAAc,EAAE,6BAA6B,CAC3C,iBAAiB,CAAC,cAAc,EAChC,QAAQ,CACT;QACD,cAAc,EAAE,6BAA6B,CAC3C,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"]}
|
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
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,68 +112,4 @@ const setPermittedEthChainIds = (caip25CaveatValue, chainIds) => {
|
|
112
112
|
return updatedCaveatValue;
|
113
113
|
};
|
114
114
|
exports.setPermittedEthChainIds = setPermittedEthChainIds;
|
115
|
-
/**
|
116
|
-
* Sets the permitted CAIP-2 chainIDs for the required and optional scopes.
|
117
|
-
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted CAIP-2 chainIDs for.
|
118
|
-
* @param chainIds - The CAIP-2 chainIDs to set as permitted.
|
119
|
-
* @returns The updated CAIP-25 caveat value with the permitted CAIP-2 chainIDs.
|
120
|
-
*/
|
121
|
-
const setPermittedChainIds = (caip25CaveatValue, chainIds) => {
|
122
|
-
let updatedCaveatValue = {
|
123
|
-
...caip25CaveatValue,
|
124
|
-
requiredScopes: filterChainScopesObjectByChainId(caip25CaveatValue.requiredScopes, chainIds),
|
125
|
-
optionalScopes: filterChainScopesObjectByChainId(caip25CaveatValue.optionalScopes, chainIds),
|
126
|
-
};
|
127
|
-
chainIds.forEach((chainId) => {
|
128
|
-
updatedCaveatValue = (0, exports.addPermittedChainId)(updatedCaveatValue, chainId);
|
129
|
-
});
|
130
|
-
return updatedCaveatValue;
|
131
|
-
};
|
132
|
-
exports.setPermittedChainIds = setPermittedChainIds;
|
133
|
-
/**
|
134
|
-
* Adds a chainID to the optional scopes if it is not already present
|
135
|
-
* in either the pre-existing required or optional scopes.
|
136
|
-
* @param caip25CaveatValue - The CAIP-25 caveat value to add the chainID to.
|
137
|
-
* @param chainId - The chainID to add.
|
138
|
-
* @returns The updated CAIP-25 caveat value with the added chainID.
|
139
|
-
*/
|
140
|
-
const addPermittedChainId = (caip25CaveatValue, chainId) => {
|
141
|
-
if (Object.keys(caip25CaveatValue.requiredScopes).includes(chainId) ||
|
142
|
-
Object.keys(caip25CaveatValue.optionalScopes).includes(chainId)) {
|
143
|
-
return caip25CaveatValue;
|
144
|
-
}
|
145
|
-
return {
|
146
|
-
...caip25CaveatValue,
|
147
|
-
optionalScopes: {
|
148
|
-
...caip25CaveatValue.optionalScopes,
|
149
|
-
[chainId]: {
|
150
|
-
accounts: [],
|
151
|
-
},
|
152
|
-
},
|
153
|
-
};
|
154
|
-
};
|
155
|
-
exports.addPermittedChainId = addPermittedChainId;
|
156
|
-
/**
|
157
|
-
* Filters the scopes object to only include:
|
158
|
-
* - Scopes without references (e.g. "wallet:")
|
159
|
-
* - CAIP-2 ChainId scopes for the given chainIDs
|
160
|
-
* @param scopesObject - The scopes object to filter.
|
161
|
-
* @param chainIds - The CAIP-2 chainIDs to filter for.
|
162
|
-
* @returns The filtered scopes object.
|
163
|
-
*/
|
164
|
-
const filterChainScopesObjectByChainId = (scopesObject, chainIds) => {
|
165
|
-
const updatedScopesObject = {};
|
166
|
-
Object.entries(scopesObject).forEach(([key, scopeObject]) => {
|
167
|
-
// Cast needed because index type is returned as `string` by `Object.entries`
|
168
|
-
const scopeString = key;
|
169
|
-
// If its a wallet scope or a wallet:* scope we don't filter it
|
170
|
-
if ((0, types_1.isWalletScope)(scopeString)) {
|
171
|
-
updatedScopesObject[scopeString] = scopeObject;
|
172
|
-
}
|
173
|
-
else if (chainIds.includes(scopeString)) {
|
174
|
-
updatedScopesObject[scopeString] = scopeObject;
|
175
|
-
}
|
176
|
-
});
|
177
|
-
return updatedScopesObject;
|
178
|
-
};
|
179
115
|
//# 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,GAAG,iBAAiB,CAAC;IAE7D,MAAM,WAAW,GAAU;QACzB,GAAG,iCAAiC,CAAC,cAAc,CAAC;QACpD,G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AC,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 * Sets the permitted CAIP-2 chainIDs for the required and optional scopes.\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\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 * @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 * Filters the scopes object to only include:\n * - Scopes without references (e.g. \"wallet:\")\n * - CAIP-2 ChainId scopes for the given chainIDs\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 its a wallet scope or a wallet:* scope we don't filter it\n if (isWalletScope(scopeString)) {\n updatedScopesObject[scopeString] = scopeObject;\n } else if (chainIds.includes(scopeString)) {\n updatedScopesObject[scopeString] = scopeObject;\n }\n });\n\n return updatedScopesObject;\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,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,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AC,CAAC;QAC/D,IAAI,CAAC,SAAS,EAAE;YACd,mBAAmB,CAAC,WAAW,CAAC,GAAG,WAAW,CAAC;YAC/C,OAAO;SACR;QACD,IAAI,SAAS,KAAK,0BAAkB,CAAC,MAAM,EAAE;YAC3C,MAAM,OAAO,GAAG,IAAA,wBAAK,EAAC,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,4 +1,4 @@
|
|
1
|
-
import type { Hex
|
1
|
+
import type { Hex } 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,19 +24,4 @@ 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
|
-
* Sets the permitted CAIP-2 chainIDs for the required and optional scopes.
|
29
|
-
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted CAIP-2 chainIDs for.
|
30
|
-
* @param chainIds - The CAIP-2 chainIDs to set as permitted.
|
31
|
-
* @returns The updated CAIP-25 caveat value with the permitted CAIP-2 chainIDs.
|
32
|
-
*/
|
33
|
-
export declare const setPermittedChainIds: (caip25CaveatValue: Caip25CaveatValue, chainIds: CaipChainId[]) => Caip25CaveatValue;
|
34
|
-
/**
|
35
|
-
* Adds a chainID to the optional scopes if it is not already present
|
36
|
-
* in either the pre-existing required or optional scopes.
|
37
|
-
* @param caip25CaveatValue - The CAIP-25 caveat value to add the chainID to.
|
38
|
-
* @param chainId - The chainID to add.
|
39
|
-
* @returns The updated CAIP-25 caveat value with the added chainID.
|
40
|
-
*/
|
41
|
-
export declare const addPermittedChainId: (caip25CaveatValue: Caip25CaveatValue, chainId: CaipChainId) => Caip25CaveatValue;
|
42
27
|
//# 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,
|
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;AAG3C,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"}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import type { Hex
|
1
|
+
import type { Hex } 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,19 +24,4 @@ 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
|
-
* Sets the permitted CAIP-2 chainIDs for the required and optional scopes.
|
29
|
-
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted CAIP-2 chainIDs for.
|
30
|
-
* @param chainIds - The CAIP-2 chainIDs to set as permitted.
|
31
|
-
* @returns The updated CAIP-25 caveat value with the permitted CAIP-2 chainIDs.
|
32
|
-
*/
|
33
|
-
export declare const setPermittedChainIds: (caip25CaveatValue: Caip25CaveatValue, chainIds: CaipChainId[]) => Caip25CaveatValue;
|
34
|
-
/**
|
35
|
-
* Adds a chainID to the optional scopes if it is not already present
|
36
|
-
* in either the pre-existing required or optional scopes.
|
37
|
-
* @param caip25CaveatValue - The CAIP-25 caveat value to add the chainID to.
|
38
|
-
* @param chainId - The chainID to add.
|
39
|
-
* @returns The updated CAIP-25 caveat value with the added chainID.
|
40
|
-
*/
|
41
|
-
export declare const addPermittedChainId: (caip25CaveatValue: Caip25CaveatValue, chainId: CaipChainId) => Caip25CaveatValue;
|
42
27
|
//# 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,
|
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;AAG3C,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"}
|
@@ -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 {
|
4
|
+
import { parseScopeString } from "../scope/types.mjs";
|
5
5
|
/**
|
6
6
|
* Gets the Ethereum (EIP155 namespaced) chainIDs from internal scopes.
|
7
7
|
*
|
@@ -106,66 +106,4 @@ export const setPermittedEthChainIds = (caip25CaveatValue, chainIds) => {
|
|
106
106
|
});
|
107
107
|
return updatedCaveatValue;
|
108
108
|
};
|
109
|
-
/**
|
110
|
-
* Sets the permitted CAIP-2 chainIDs for the required and optional scopes.
|
111
|
-
* @param caip25CaveatValue - The CAIP-25 caveat value to set the permitted CAIP-2 chainIDs for.
|
112
|
-
* @param chainIds - The CAIP-2 chainIDs to set as permitted.
|
113
|
-
* @returns The updated CAIP-25 caveat value with the permitted CAIP-2 chainIDs.
|
114
|
-
*/
|
115
|
-
export const setPermittedChainIds = (caip25CaveatValue, chainIds) => {
|
116
|
-
let updatedCaveatValue = {
|
117
|
-
...caip25CaveatValue,
|
118
|
-
requiredScopes: filterChainScopesObjectByChainId(caip25CaveatValue.requiredScopes, chainIds),
|
119
|
-
optionalScopes: filterChainScopesObjectByChainId(caip25CaveatValue.optionalScopes, chainIds),
|
120
|
-
};
|
121
|
-
chainIds.forEach((chainId) => {
|
122
|
-
updatedCaveatValue = addPermittedChainId(updatedCaveatValue, chainId);
|
123
|
-
});
|
124
|
-
return updatedCaveatValue;
|
125
|
-
};
|
126
|
-
/**
|
127
|
-
* Adds a chainID to the optional scopes if it is not already present
|
128
|
-
* in either the pre-existing required or optional scopes.
|
129
|
-
* @param caip25CaveatValue - The CAIP-25 caveat value to add the chainID to.
|
130
|
-
* @param chainId - The chainID to add.
|
131
|
-
* @returns The updated CAIP-25 caveat value with the added chainID.
|
132
|
-
*/
|
133
|
-
export const addPermittedChainId = (caip25CaveatValue, chainId) => {
|
134
|
-
if (Object.keys(caip25CaveatValue.requiredScopes).includes(chainId) ||
|
135
|
-
Object.keys(caip25CaveatValue.optionalScopes).includes(chainId)) {
|
136
|
-
return caip25CaveatValue;
|
137
|
-
}
|
138
|
-
return {
|
139
|
-
...caip25CaveatValue,
|
140
|
-
optionalScopes: {
|
141
|
-
...caip25CaveatValue.optionalScopes,
|
142
|
-
[chainId]: {
|
143
|
-
accounts: [],
|
144
|
-
},
|
145
|
-
},
|
146
|
-
};
|
147
|
-
};
|
148
|
-
/**
|
149
|
-
* Filters the scopes object to only include:
|
150
|
-
* - Scopes without references (e.g. "wallet:")
|
151
|
-
* - CAIP-2 ChainId scopes for the given chainIDs
|
152
|
-
* @param scopesObject - The scopes object to filter.
|
153
|
-
* @param chainIds - The CAIP-2 chainIDs to filter for.
|
154
|
-
* @returns The filtered scopes object.
|
155
|
-
*/
|
156
|
-
const filterChainScopesObjectByChainId = (scopesObject, chainIds) => {
|
157
|
-
const updatedScopesObject = {};
|
158
|
-
Object.entries(scopesObject).forEach(([key, scopeObject]) => {
|
159
|
-
// Cast needed because index type is returned as `string` by `Object.entries`
|
160
|
-
const scopeString = key;
|
161
|
-
// If its a wallet scope or a wallet:* scope we don't filter it
|
162
|
-
if (isWalletScope(scopeString)) {
|
163
|
-
updatedScopesObject[scopeString] = scopeObject;
|
164
|
-
}
|
165
|
-
else if (chainIds.includes(scopeString)) {
|
166
|
-
updatedScopesObject[scopeString] = scopeObject;
|
167
|
-
}
|
168
|
-
});
|
169
|
-
return updatedScopesObject;
|
170
|
-
};
|
171
109
|
//# 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,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,C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AAG,EAAE,WAAW,CAAC,EAAE,EAAE;QAC1D,6EAA6E;QAC7E,MAAM,WAAW,GAAG,GAAgC,CAAC;QACrD,+DAA+D;QAC/D,IAAI,aAAa,CAAC,WAAW,CAAC,EAAE;YAC9B,mBAAmB,CAAC,WAAW,CAAC,GAAG,WAAW,CAAC;SAChD;aAAM,IAAI,QAAQ,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE;YACzC,mBAAmB,CAAC,WAAW,CAAC,GAAG,WAAW,CAAC;SAChD;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,mBAAmB,CAAC;AAC7B,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 * Sets the permitted CAIP-2 chainIDs for the required and optional scopes.\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\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 * @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 * Filters the scopes object to only include:\n * - Scopes without references (e.g. \"wallet:\")\n * - CAIP-2 ChainId scopes for the given chainIDs\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 its a wallet scope or a wallet:* scope we don't filter it\n if (isWalletScope(scopeString)) {\n updatedScopesObject[scopeString] = scopeObject;\n } else if (chainIds.includes(scopeString)) {\n updatedScopesObject[scopeString] = scopeObject;\n }\n });\n\n return updatedScopesObject;\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,gBAAgB,EAAE,2BAAuB;AAElD;;;;;GAKG;AACH,MAAM,iCAAiC,GAAG,CAAC,MAA4B,EAAE,EAAE;IACzE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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,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.
|
3
|
+
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");
|
@@ -8,8 +8,6 @@ const assert_1 = require("./scope/assert.cjs");
|
|
8
8
|
const supported_1 = require("./scope/supported.cjs");
|
9
9
|
const transform_1 = require("./scope/transform.cjs");
|
10
10
|
const types_1 = require("./scope/types.cjs");
|
11
|
-
const caip_permission_adapter_permittedChains_1 = require("./adapters/caip-permission-adapter-permittedChains.cjs");
|
12
|
-
const caip_permission_adapter_accounts_1 = require("./adapters/caip-permission-adapter-accounts.cjs");
|
13
11
|
/**
|
14
12
|
* The name of the CAIP-25 permission caveat.
|
15
13
|
*/
|
@@ -295,26 +293,4 @@ function removeScope(caip25CaveatValue, targetScopeString) {
|
|
295
293
|
operation: permission_controller_1.CaveatMutatorOperation.RevokePermission,
|
296
294
|
};
|
297
295
|
}
|
298
|
-
/**
|
299
|
-
* Modifies the requested CAIP-25 permissions object after UI confirmation.
|
300
|
-
*
|
301
|
-
* @param caip25CaveatValue - The requested CAIP-25 caveat value to modify.
|
302
|
-
* @param accountAddresses - The list of permitted eth addresses.
|
303
|
-
* @param chainIds - The list of permitted eth chainIds.
|
304
|
-
*/
|
305
|
-
const generateCaip25Caveat = (caip25CaveatValue, accountAddresses, chainIds) => {
|
306
|
-
const caveatValueWithChains = (0, caip_permission_adapter_permittedChains_1.setPermittedChainIds)(caip25CaveatValue, chainIds);
|
307
|
-
const caveatValueWithAccounts = (0, caip_permission_adapter_accounts_1.setPermittedAccounts)(caveatValueWithChains, accountAddresses);
|
308
|
-
return {
|
309
|
-
[exports.Caip25EndowmentPermissionName]: {
|
310
|
-
caveats: [
|
311
|
-
{
|
312
|
-
type: exports.Caip25CaveatType,
|
313
|
-
value: caveatValueWithAccounts,
|
314
|
-
},
|
315
|
-
],
|
316
|
-
},
|
317
|
-
};
|
318
|
-
};
|
319
|
-
exports.generateCaip25Caveat = generateCaip25Caveat;
|
320
296
|
//# sourceMappingURL=caip25Permission.cjs.map
|