@metamask-previews/chain-agnostic-permission 0.1.0-preview-b69c669
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 -0
- package/LICENSE +20 -0
- package/README.md +15 -0
- package/dist/adapters/caip-permission-adapter-eth-accounts.cjs +103 -0
- package/dist/adapters/caip-permission-adapter-eth-accounts.cjs.map +1 -0
- package/dist/adapters/caip-permission-adapter-eth-accounts.d.cts +20 -0
- package/dist/adapters/caip-permission-adapter-eth-accounts.d.cts.map +1 -0
- package/dist/adapters/caip-permission-adapter-eth-accounts.d.mts +20 -0
- package/dist/adapters/caip-permission-adapter-eth-accounts.d.mts.map +1 -0
- package/dist/adapters/caip-permission-adapter-eth-accounts.mjs +98 -0
- package/dist/adapters/caip-permission-adapter-eth-accounts.mjs.map +1 -0
- package/dist/adapters/caip-permission-adapter-permittedChains.cjs +115 -0
- package/dist/adapters/caip-permission-adapter-permittedChains.cjs.map +1 -0
- package/dist/adapters/caip-permission-adapter-permittedChains.d.cts +27 -0
- package/dist/adapters/caip-permission-adapter-permittedChains.d.cts.map +1 -0
- package/dist/adapters/caip-permission-adapter-permittedChains.d.mts +27 -0
- package/dist/adapters/caip-permission-adapter-permittedChains.d.mts.map +1 -0
- package/dist/adapters/caip-permission-adapter-permittedChains.mjs +109 -0
- package/dist/adapters/caip-permission-adapter-permittedChains.mjs.map +1 -0
- package/dist/adapters/caip-permission-adapter-session-scopes.cjs +85 -0
- package/dist/adapters/caip-permission-adapter-session-scopes.cjs.map +1 -0
- package/dist/adapters/caip-permission-adapter-session-scopes.d.cts +23 -0
- package/dist/adapters/caip-permission-adapter-session-scopes.d.cts.map +1 -0
- package/dist/adapters/caip-permission-adapter-session-scopes.d.mts +23 -0
- package/dist/adapters/caip-permission-adapter-session-scopes.d.mts.map +1 -0
- package/dist/adapters/caip-permission-adapter-session-scopes.mjs +80 -0
- package/dist/adapters/caip-permission-adapter-session-scopes.mjs.map +1 -0
- package/dist/caip25Permission.cjs +285 -0
- package/dist/caip25Permission.cjs.map +1 -0
- package/dist/caip25Permission.d.cts +131 -0
- package/dist/caip25Permission.d.cts.map +1 -0
- package/dist/caip25Permission.d.mts +131 -0
- package/dist/caip25Permission.d.mts.map +1 -0
- package/dist/caip25Permission.mjs +280 -0
- package/dist/caip25Permission.mjs.map +1 -0
- package/dist/index.cjs +43 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +14 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +14 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +11 -0
- package/dist/index.mjs.map +1 -0
- package/dist/scope/assert.cjs +170 -0
- package/dist/scope/assert.cjs.map +1 -0
- package/dist/scope/assert.d.cts +50 -0
- package/dist/scope/assert.d.cts.map +1 -0
- package/dist/scope/assert.d.mts +50 -0
- package/dist/scope/assert.d.mts.map +1 -0
- package/dist/scope/assert.mjs +162 -0
- package/dist/scope/assert.mjs.map +1 -0
- package/dist/scope/authorization.cjs +51 -0
- package/dist/scope/authorization.cjs.map +1 -0
- package/dist/scope/authorization.d.cts +50 -0
- package/dist/scope/authorization.d.cts.map +1 -0
- package/dist/scope/authorization.d.mts +50 -0
- package/dist/scope/authorization.d.mts.map +1 -0
- package/dist/scope/authorization.mjs +46 -0
- package/dist/scope/authorization.mjs.map +1 -0
- package/dist/scope/constants.cjs +83 -0
- package/dist/scope/constants.cjs.map +1 -0
- package/dist/scope/constants.d.cts +32 -0
- package/dist/scope/constants.d.cts.map +1 -0
- package/dist/scope/constants.d.mts +32 -0
- package/dist/scope/constants.d.mts.map +1 -0
- package/dist/scope/constants.mjs +84 -0
- package/dist/scope/constants.mjs.map +1 -0
- package/dist/scope/errors.cjs +47 -0
- package/dist/scope/errors.cjs.map +1 -0
- package/dist/scope/errors.d.cts +44 -0
- package/dist/scope/errors.d.cts.map +1 -0
- package/dist/scope/errors.d.mts +44 -0
- package/dist/scope/errors.d.mts.map +1 -0
- package/dist/scope/errors.mjs +44 -0
- package/dist/scope/errors.mjs.map +1 -0
- package/dist/scope/filter.cjs +76 -0
- package/dist/scope/filter.cjs.map +1 -0
- package/dist/scope/filter.d.cts +35 -0
- package/dist/scope/filter.d.cts.map +1 -0
- package/dist/scope/filter.d.mts +35 -0
- package/dist/scope/filter.d.mts.map +1 -0
- package/dist/scope/filter.mjs +71 -0
- package/dist/scope/filter.mjs.map +1 -0
- package/dist/scope/supported.cjs +116 -0
- package/dist/scope/supported.cjs.map +1 -0
- package/dist/scope/supported.d.cts +52 -0
- package/dist/scope/supported.d.cts.map +1 -0
- package/dist/scope/supported.d.mts +52 -0
- package/dist/scope/supported.d.mts.map +1 -0
- package/dist/scope/supported.mjs +109 -0
- package/dist/scope/supported.mjs.map +1 -0
- package/dist/scope/transform.cjs +152 -0
- package/dist/scope/transform.cjs.map +1 -0
- package/dist/scope/transform.d.cts +50 -0
- package/dist/scope/transform.d.cts.map +1 -0
- package/dist/scope/transform.d.mts +50 -0
- package/dist/scope/transform.d.mts.map +1 -0
- package/dist/scope/transform.mjs +144 -0
- package/dist/scope/transform.mjs.map +1 -0
- package/dist/scope/types.cjs +23 -0
- package/dist/scope/types.cjs.map +1 -0
- package/dist/scope/types.d.cts +81 -0
- package/dist/scope/types.d.cts.map +1 -0
- package/dist/scope/types.d.mts +81 -0
- package/dist/scope/types.d.mts.map +1 -0
- package/dist/scope/types.mjs +19 -0
- package/dist/scope/types.mjs.map +1 -0
- package/dist/scope/validation.cjs +87 -0
- package/dist/scope/validation.cjs.map +1 -0
- package/dist/scope/validation.d.cts +21 -0
- package/dist/scope/validation.d.cts.map +1 -0
- package/dist/scope/validation.d.mts +21 -0
- package/dist/scope/validation.d.mts.map +1 -0
- package/dist/scope/validation.mjs +82 -0
- package/dist/scope/validation.mjs.map +1 -0
- package/package.json +75 -0
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"filter.d.mts","sourceRoot":"","sources":["../../src/scope/filter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,GAAG,EAAE,wBAAwB;AAIxD,OAAO,KAAK,EAGV,sBAAsB,EACvB,oBAAgB;AAEjB;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,qBAAqB,WACxB,sBAAsB;qCAMK,GAAG,KAAK,OAAO;oCAChB,WAAW,KAAK,OAAO;uCACpB,WAAW,KAAK,MAAM,EAAE;;;;CAqB9D,CAAC;AAsCF;;;;;;;;GAQG;AACH,eAAO,MAAM,wBAAwB,WAC3B,sBAAsB;uCAIO,WAAW,KAAK,MAAM,EAAE;4BAe9D,CAAC"}
|
@@ -0,0 +1,71 @@
|
|
1
|
+
import { assertIsInternalScopeString, assertScopeSupported } from "./assert.mjs";
|
2
|
+
import { isSupportedMethod, isSupportedNotification } from "./supported.mjs";
|
3
|
+
/**
|
4
|
+
* Groups a NormalizedScopesObject into two separate
|
5
|
+
* NormalizedScopesObject with supported scopes in one
|
6
|
+
* and unsupported scopes in the other.
|
7
|
+
*
|
8
|
+
* @param scopes - The NormalizedScopesObject to group.
|
9
|
+
* @param hooks - An object containing the following properties:
|
10
|
+
* @param hooks.isEvmChainIdSupported - A predicate that determines if an EVM chainID is supported.
|
11
|
+
* @param hooks.isNonEvmScopeSupported - A predicate that determines if an non EVM scopeString is supported.
|
12
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
13
|
+
* @returns The supported and unsupported scopes.
|
14
|
+
*/
|
15
|
+
export const bucketScopesBySupport = (scopes, { isEvmChainIdSupported, isNonEvmScopeSupported, getNonEvmSupportedMethods, }) => {
|
16
|
+
const supportedScopes = {};
|
17
|
+
const unsupportedScopes = {};
|
18
|
+
for (const [scopeString, scopeObject] of Object.entries(scopes)) {
|
19
|
+
assertIsInternalScopeString(scopeString);
|
20
|
+
try {
|
21
|
+
assertScopeSupported(scopeString, scopeObject, {
|
22
|
+
isEvmChainIdSupported,
|
23
|
+
isNonEvmScopeSupported,
|
24
|
+
getNonEvmSupportedMethods,
|
25
|
+
});
|
26
|
+
supportedScopes[scopeString] = scopeObject;
|
27
|
+
}
|
28
|
+
catch {
|
29
|
+
unsupportedScopes[scopeString] = scopeObject;
|
30
|
+
}
|
31
|
+
}
|
32
|
+
return { supportedScopes, unsupportedScopes };
|
33
|
+
};
|
34
|
+
/**
|
35
|
+
* Returns a NormalizedScopeObject with
|
36
|
+
* unsupported methods and notifications removed.
|
37
|
+
*
|
38
|
+
* @param scopeString - The InternalScopeString for the scopeObject.
|
39
|
+
* @param scopeObject - The NormalizedScopeObject to filter.
|
40
|
+
* @param hooks - An object containing the following properties:
|
41
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
42
|
+
* @returns a NormalizedScopeObject with only methods and notifications that are currently supported.
|
43
|
+
*/
|
44
|
+
const getSupportedScopeObject = (scopeString, scopeObject, { getNonEvmSupportedMethods, }) => {
|
45
|
+
const { methods, notifications } = scopeObject;
|
46
|
+
const supportedMethods = methods.filter((method) => isSupportedMethod(scopeString, method, { getNonEvmSupportedMethods }));
|
47
|
+
const supportedNotifications = notifications.filter((notification) => isSupportedNotification(scopeString, notification));
|
48
|
+
return {
|
49
|
+
...scopeObject,
|
50
|
+
methods: supportedMethods,
|
51
|
+
notifications: supportedNotifications,
|
52
|
+
};
|
53
|
+
};
|
54
|
+
/**
|
55
|
+
* Returns a NormalizedScopesObject with
|
56
|
+
* unsupported methods and notifications removed from scopeObjects.
|
57
|
+
*
|
58
|
+
* @param scopes - The NormalizedScopesObject to filter.
|
59
|
+
* @param hooks - An object containing the following properties:
|
60
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
61
|
+
* @returns a NormalizedScopesObject with only methods, and notifications that are currently supported.
|
62
|
+
*/
|
63
|
+
export const getSupportedScopeObjects = (scopes, { getNonEvmSupportedMethods, }) => {
|
64
|
+
const filteredScopesObject = {};
|
65
|
+
for (const [scopeString, scopeObject] of Object.entries(scopes)) {
|
66
|
+
assertIsInternalScopeString(scopeString);
|
67
|
+
filteredScopesObject[scopeString] = getSupportedScopeObject(scopeString, scopeObject, { getNonEvmSupportedMethods });
|
68
|
+
}
|
69
|
+
return filteredScopesObject;
|
70
|
+
};
|
71
|
+
//# sourceMappingURL=filter.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"filter.mjs","sourceRoot":"","sources":["../../src/scope/filter.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,2BAA2B,EAAE,oBAAoB,EAAE,qBAAiB;AAC7E,OAAO,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,wBAAoB;AAOzE;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CACnC,MAA8B,EAC9B,EACE,qBAAqB,EACrB,sBAAsB,EACtB,yBAAyB,GAK1B,EACD,EAAE;IACF,MAAM,eAAe,GAA2B,EAAE,CAAC;IACnD,MAAM,iBAAiB,GAA2B,EAAE,CAAC;IAErD,KAAK,MAAM,CAAC,WAAW,EAAE,WAAW,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;QAC/D,2BAA2B,CAAC,WAAW,CAAC,CAAC;QACzC,IAAI;YACF,oBAAoB,CAAC,WAAW,EAAE,WAAW,EAAE;gBAC7C,qBAAqB;gBACrB,sBAAsB;gBACtB,yBAAyB;aAC1B,CAAC,CAAC;YACH,eAAe,CAAC,WAAW,CAAC,GAAG,WAAW,CAAC;SAC5C;QAAC,MAAM;YACN,iBAAiB,CAAC,WAAW,CAAC,GAAG,WAAW,CAAC;SAC9C;KACF;IAED,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,CAAC;AAChD,CAAC,CAAC;AAEF;;;;;;;;;GASG;AACH,MAAM,uBAAuB,GAAG,CAC9B,WAAgC,EAChC,WAAkC,EAClC,EACE,yBAAyB,GAG1B,EACD,EAAE;IACF,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,GAAG,WAAW,CAAC;IAE/C,MAAM,gBAAgB,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CACjD,iBAAiB,CAAC,WAAW,EAAE,MAAM,EAAE,EAAE,yBAAyB,EAAE,CAAC,CACtE,CAAC;IAEF,MAAM,sBAAsB,GAAG,aAAa,CAAC,MAAM,CAAC,CAAC,YAAY,EAAE,EAAE,CACnE,uBAAuB,CAAC,WAAW,EAAE,YAAY,CAAC,CACnD,CAAC;IAEF,OAAO;QACL,GAAG,WAAW;QACd,OAAO,EAAE,gBAAgB;QACzB,aAAa,EAAE,sBAAsB;KACtC,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,CACtC,MAA8B,EAC9B,EACE,yBAAyB,GAG1B,EACD,EAAE;IACF,MAAM,oBAAoB,GAA2B,EAAE,CAAC;IAExD,KAAK,MAAM,CAAC,WAAW,EAAE,WAAW,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE;QAC/D,2BAA2B,CAAC,WAAW,CAAC,CAAC;QACzC,oBAAoB,CAAC,WAAW,CAAC,GAAG,uBAAuB,CACzD,WAAW,EACX,WAAW,EACX,EAAE,yBAAyB,EAAE,CAC9B,CAAC;KACH;IAED,OAAO,oBAAoB,CAAC;AAC9B,CAAC,CAAC","sourcesContent":["import type { CaipChainId, Hex } from '@metamask/utils';\n\nimport { assertIsInternalScopeString, assertScopeSupported } from './assert';\nimport { isSupportedMethod, isSupportedNotification } from './supported';\nimport type {\n InternalScopeString,\n NormalizedScopeObject,\n NormalizedScopesObject,\n} from './types';\n\n/**\n * Groups a NormalizedScopesObject into two separate\n * NormalizedScopesObject with supported scopes in one\n * and unsupported scopes in the other.\n *\n * @param scopes - The NormalizedScopesObject to group.\n * @param hooks - An object containing the following properties:\n * @param hooks.isEvmChainIdSupported - A predicate that determines if an EVM chainID is supported.\n * @param hooks.isNonEvmScopeSupported - A predicate that determines if an non EVM scopeString is supported.\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @returns The supported and unsupported scopes.\n */\nexport const bucketScopesBySupport = (\n scopes: NormalizedScopesObject,\n {\n isEvmChainIdSupported,\n isNonEvmScopeSupported,\n getNonEvmSupportedMethods,\n }: {\n isEvmChainIdSupported: (chainId: Hex) => boolean;\n isNonEvmScopeSupported: (scope: CaipChainId) => boolean;\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n },\n) => {\n const supportedScopes: NormalizedScopesObject = {};\n const unsupportedScopes: NormalizedScopesObject = {};\n\n for (const [scopeString, scopeObject] of Object.entries(scopes)) {\n assertIsInternalScopeString(scopeString);\n try {\n assertScopeSupported(scopeString, scopeObject, {\n isEvmChainIdSupported,\n isNonEvmScopeSupported,\n getNonEvmSupportedMethods,\n });\n supportedScopes[scopeString] = scopeObject;\n } catch {\n unsupportedScopes[scopeString] = scopeObject;\n }\n }\n\n return { supportedScopes, unsupportedScopes };\n};\n\n/**\n * Returns a NormalizedScopeObject with\n * unsupported methods and notifications removed.\n *\n * @param scopeString - The InternalScopeString for the scopeObject.\n * @param scopeObject - The NormalizedScopeObject to filter.\n * @param hooks - An object containing the following properties:\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @returns a NormalizedScopeObject with only methods and notifications that are currently supported.\n */\nconst getSupportedScopeObject = (\n scopeString: InternalScopeString,\n scopeObject: NormalizedScopeObject,\n {\n getNonEvmSupportedMethods,\n }: {\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n },\n) => {\n const { methods, notifications } = scopeObject;\n\n const supportedMethods = methods.filter((method) =>\n isSupportedMethod(scopeString, method, { getNonEvmSupportedMethods }),\n );\n\n const supportedNotifications = notifications.filter((notification) =>\n isSupportedNotification(scopeString, notification),\n );\n\n return {\n ...scopeObject,\n methods: supportedMethods,\n notifications: supportedNotifications,\n };\n};\n\n/**\n * Returns a NormalizedScopesObject with\n * unsupported methods and notifications removed from scopeObjects.\n *\n * @param scopes - The NormalizedScopesObject to filter.\n * @param hooks - An object containing the following properties:\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @returns a NormalizedScopesObject with only methods, and notifications that are currently supported.\n */\nexport const getSupportedScopeObjects = (\n scopes: NormalizedScopesObject,\n {\n getNonEvmSupportedMethods,\n }: {\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n },\n) => {\n const filteredScopesObject: NormalizedScopesObject = {};\n\n for (const [scopeString, scopeObject] of Object.entries(scopes)) {\n assertIsInternalScopeString(scopeString);\n filteredScopesObject[scopeString] = getSupportedScopeObject(\n scopeString,\n scopeObject,\n { getNonEvmSupportedMethods },\n );\n }\n\n return filteredScopesObject;\n};\n"]}
|
@@ -0,0 +1,116 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.isSupportedNotification = exports.isSupportedMethod = exports.isSupportedAccount = exports.isSupportedScopeString = void 0;
|
4
|
+
const controller_utils_1 = require("@metamask/controller-utils");
|
5
|
+
const utils_1 = require("@metamask/utils");
|
6
|
+
const constants_1 = require("./constants.cjs");
|
7
|
+
const types_1 = require("./types.cjs");
|
8
|
+
/**
|
9
|
+
* Determines if a scope string is supported.
|
10
|
+
*
|
11
|
+
* @param scopeString - The scope string to check.
|
12
|
+
* @param hooks - An object containing the following properties:
|
13
|
+
* @param hooks.isEvmChainIdSupported - A predicate that determines if an EVM chainID is supported.
|
14
|
+
* @param hooks.isNonEvmScopeSupported - A predicate that determines if an non EVM scopeString is supported.
|
15
|
+
* @returns A boolean indicating if the scope string is supported.
|
16
|
+
*/
|
17
|
+
const isSupportedScopeString = (scopeString, { isEvmChainIdSupported, isNonEvmScopeSupported, }) => {
|
18
|
+
const { namespace, reference } = (0, types_1.parseScopeString)(scopeString);
|
19
|
+
switch (namespace) {
|
20
|
+
case utils_1.KnownCaipNamespace.Wallet:
|
21
|
+
if ((0, utils_1.isCaipChainId)(scopeString) &&
|
22
|
+
reference !== utils_1.KnownCaipNamespace.Eip155) {
|
23
|
+
return isNonEvmScopeSupported(scopeString);
|
24
|
+
}
|
25
|
+
return true;
|
26
|
+
case utils_1.KnownCaipNamespace.Eip155:
|
27
|
+
return (!reference ||
|
28
|
+
(constants_1.CaipReferenceRegexes.eip155.test(reference) &&
|
29
|
+
isEvmChainIdSupported((0, controller_utils_1.toHex)(reference))));
|
30
|
+
default:
|
31
|
+
return (0, utils_1.isCaipChainId)(scopeString)
|
32
|
+
? isNonEvmScopeSupported(scopeString)
|
33
|
+
: false;
|
34
|
+
}
|
35
|
+
};
|
36
|
+
exports.isSupportedScopeString = isSupportedScopeString;
|
37
|
+
/**
|
38
|
+
* Determines if an account is supported by the wallet (i.e. on a keyring known to the wallet).
|
39
|
+
*
|
40
|
+
* @param account - The CAIP account ID to check.
|
41
|
+
* @param hooks - An object containing the following properties:
|
42
|
+
* @param hooks.getEvmInternalAccounts - A function that returns the EVM internal accounts.
|
43
|
+
* @param hooks.getNonEvmAccountAddresses - A function that returns the supported CAIP-10 account addresses for a non EVM scope.
|
44
|
+
* @returns A boolean indicating if the account is supported by the wallet.
|
45
|
+
*/
|
46
|
+
const isSupportedAccount = (account, { getEvmInternalAccounts, getNonEvmAccountAddresses, }) => {
|
47
|
+
const { address, chainId, chain: { namespace, reference }, } = (0, utils_1.parseCaipAccountId)(account);
|
48
|
+
const isSupportedEip155Account = () => getEvmInternalAccounts().some((internalAccount) => ['eip155:eoa', 'eip155:erc4337'].includes(internalAccount.type) &&
|
49
|
+
(0, controller_utils_1.isEqualCaseInsensitive)(address, internalAccount.address));
|
50
|
+
const isSupportedNonEvmAccount = () => getNonEvmAccountAddresses(chainId).includes(account);
|
51
|
+
// We are trying to discern the type of `namespace`.
|
52
|
+
/* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */
|
53
|
+
switch (namespace) {
|
54
|
+
case utils_1.KnownCaipNamespace.Wallet:
|
55
|
+
if (reference === utils_1.KnownCaipNamespace.Eip155) {
|
56
|
+
return isSupportedEip155Account();
|
57
|
+
}
|
58
|
+
return isSupportedNonEvmAccount();
|
59
|
+
case utils_1.KnownCaipNamespace.Eip155:
|
60
|
+
return isSupportedEip155Account();
|
61
|
+
default:
|
62
|
+
return isSupportedNonEvmAccount();
|
63
|
+
}
|
64
|
+
/* eslint-enable @typescript-eslint/no-unsafe-enum-comparison */
|
65
|
+
};
|
66
|
+
exports.isSupportedAccount = isSupportedAccount;
|
67
|
+
/**
|
68
|
+
* Determines if a method is supported by the wallet.
|
69
|
+
*
|
70
|
+
* @param scopeString - The scope string to check.
|
71
|
+
* @param method - The method to check.
|
72
|
+
* @param hooks - An object containing the following properties:
|
73
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
74
|
+
* @returns A boolean indicating if the method is supported by the wallet.
|
75
|
+
*/
|
76
|
+
const isSupportedMethod = (scopeString, method, { getNonEvmSupportedMethods, }) => {
|
77
|
+
const { namespace, reference } = (0, types_1.parseScopeString)(scopeString);
|
78
|
+
if (!namespace) {
|
79
|
+
return false;
|
80
|
+
}
|
81
|
+
const isSupportedNonEvmMethod = () => (0, utils_1.isCaipChainId)(scopeString) &&
|
82
|
+
getNonEvmSupportedMethods(scopeString).includes(method);
|
83
|
+
// We are trying to discern the type of `namespace`.
|
84
|
+
/* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */
|
85
|
+
if (namespace === utils_1.KnownCaipNamespace.Wallet) {
|
86
|
+
if (!reference) {
|
87
|
+
return constants_1.KnownWalletRpcMethods.includes(method);
|
88
|
+
}
|
89
|
+
if (reference === utils_1.KnownCaipNamespace.Eip155) {
|
90
|
+
return constants_1.KnownWalletNamespaceRpcMethods[reference].includes(method);
|
91
|
+
}
|
92
|
+
return isSupportedNonEvmMethod();
|
93
|
+
}
|
94
|
+
if (namespace === utils_1.KnownCaipNamespace.Eip155) {
|
95
|
+
return constants_1.KnownRpcMethods[namespace].includes(method);
|
96
|
+
}
|
97
|
+
/* eslint-enable @typescript-eslint/no-unsafe-enum-comparison */
|
98
|
+
return isSupportedNonEvmMethod();
|
99
|
+
};
|
100
|
+
exports.isSupportedMethod = isSupportedMethod;
|
101
|
+
/**
|
102
|
+
* Determines if a notification is supported by the wallet.
|
103
|
+
*
|
104
|
+
* @param scopeString - The scope string to check.
|
105
|
+
* @param notification - The notification to check.
|
106
|
+
* @returns A boolean indicating if the notification is supported by the wallet.
|
107
|
+
*/
|
108
|
+
const isSupportedNotification = (scopeString, notification) => {
|
109
|
+
const { namespace } = (0, types_1.parseScopeString)(scopeString);
|
110
|
+
if (namespace === utils_1.KnownCaipNamespace.Eip155) {
|
111
|
+
return constants_1.KnownNotifications[namespace].includes(notification);
|
112
|
+
}
|
113
|
+
return false;
|
114
|
+
};
|
115
|
+
exports.isSupportedNotification = isSupportedNotification;
|
116
|
+
//# sourceMappingURL=supported.cjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"supported.cjs","sourceRoot":"","sources":["../../src/scope/supported.ts"],"names":[],"mappings":";;;AAAA,iEAA2E;AAE3E,2CAIyB;AAEzB,+CAMqB;AAErB,uCAA2C;AAE3C;;;;;;;;GAQG;AACI,MAAM,sBAAsB,GAAG,CACpC,WAAmB,EACnB,EACE,qBAAqB,EACrB,sBAAsB,GAIvB,EACD,EAAE;IACF,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,IAAA,wBAAgB,EAAC,WAAW,CAAC,CAAC;IAE/D,QAAQ,SAAS,EAAE;QACjB,KAAK,0BAAkB,CAAC,MAAM;YAC5B,IACE,IAAA,qBAAa,EAAC,WAAW,CAAC;gBAC1B,SAAS,KAAK,0BAAkB,CAAC,MAAM,EACvC;gBACA,OAAO,sBAAsB,CAAC,WAAW,CAAC,CAAC;aAC5C;YACD,OAAO,IAAI,CAAC;QACd,KAAK,0BAAkB,CAAC,MAAM;YAC5B,OAAO,CACL,CAAC,SAAS;gBACV,CAAC,gCAA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AC,MAAM,EAAE;QAC3C,OAAO,8BAAkB,CAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC;KAC7D;IAED,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AAXW,QAAA,uBAAuB,2BAWlC","sourcesContent":["import { toHex, isEqualCaseInsensitive } from '@metamask/controller-utils';\nimport type { CaipAccountId, CaipChainId, Hex } from '@metamask/utils';\nimport {\n isCaipChainId,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\n\nimport {\n CaipReferenceRegexes,\n KnownNotifications,\n KnownRpcMethods,\n KnownWalletNamespaceRpcMethods,\n KnownWalletRpcMethods,\n} from './constants';\nimport type { ExternalScopeString } from './types';\nimport { parseScopeString } from './types';\n\n/**\n * Determines if a scope string is supported.\n *\n * @param scopeString - The scope string to check.\n * @param hooks - An object containing the following properties:\n * @param hooks.isEvmChainIdSupported - A predicate that determines if an EVM chainID is supported.\n * @param hooks.isNonEvmScopeSupported - A predicate that determines if an non EVM scopeString is supported.\n * @returns A boolean indicating if the scope string is supported.\n */\nexport const isSupportedScopeString = (\n scopeString: string,\n {\n isEvmChainIdSupported,\n isNonEvmScopeSupported,\n }: {\n isEvmChainIdSupported: (chainId: Hex) => boolean;\n isNonEvmScopeSupported: (scope: CaipChainId) => boolean;\n },\n) => {\n const { namespace, reference } = parseScopeString(scopeString);\n\n switch (namespace) {\n case KnownCaipNamespace.Wallet:\n if (\n isCaipChainId(scopeString) &&\n reference !== KnownCaipNamespace.Eip155\n ) {\n return isNonEvmScopeSupported(scopeString);\n }\n return true;\n case KnownCaipNamespace.Eip155:\n return (\n !reference ||\n (CaipReferenceRegexes.eip155.test(reference) &&\n isEvmChainIdSupported(toHex(reference)))\n );\n default:\n return isCaipChainId(scopeString)\n ? isNonEvmScopeSupported(scopeString)\n : false;\n }\n};\n\n/**\n * Determines if an account is supported by the wallet (i.e. on a keyring known to the wallet).\n *\n * @param account - The CAIP account ID to check.\n * @param hooks - An object containing the following properties:\n * @param hooks.getEvmInternalAccounts - A function that returns the EVM internal accounts.\n * @param hooks.getNonEvmAccountAddresses - A function that returns the supported CAIP-10 account addresses for a non EVM scope.\n * @returns A boolean indicating if the account is supported by the wallet.\n */\nexport const isSupportedAccount = (\n account: CaipAccountId,\n {\n getEvmInternalAccounts,\n getNonEvmAccountAddresses,\n }: {\n getEvmInternalAccounts: () => { type: string; address: Hex }[];\n getNonEvmAccountAddresses: (scope: CaipChainId) => string[];\n },\n) => {\n const {\n address,\n chainId,\n chain: { namespace, reference },\n } = parseCaipAccountId(account);\n\n const isSupportedEip155Account = () =>\n getEvmInternalAccounts().some(\n (internalAccount) =>\n ['eip155:eoa', 'eip155:erc4337'].includes(internalAccount.type) &&\n isEqualCaseInsensitive(address, internalAccount.address),\n );\n\n const isSupportedNonEvmAccount = () =>\n getNonEvmAccountAddresses(chainId).includes(account);\n\n // We are trying to discern the type of `namespace`.\n /* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */\n switch (namespace) {\n case KnownCaipNamespace.Wallet:\n if (reference === KnownCaipNamespace.Eip155) {\n return isSupportedEip155Account();\n }\n return isSupportedNonEvmAccount();\n case KnownCaipNamespace.Eip155:\n return isSupportedEip155Account();\n default:\n return isSupportedNonEvmAccount();\n }\n /* eslint-enable @typescript-eslint/no-unsafe-enum-comparison */\n};\n\n/**\n * Determines if a method is supported by the wallet.\n *\n * @param scopeString - The scope string to check.\n * @param method - The method to check.\n * @param hooks - An object containing the following properties:\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @returns A boolean indicating if the method is supported by the wallet.\n */\nexport const isSupportedMethod = (\n scopeString: ExternalScopeString,\n method: string,\n {\n getNonEvmSupportedMethods,\n }: {\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n },\n): boolean => {\n const { namespace, reference } = parseScopeString(scopeString);\n\n if (!namespace) {\n return false;\n }\n\n const isSupportedNonEvmMethod = () =>\n isCaipChainId(scopeString) &&\n getNonEvmSupportedMethods(scopeString).includes(method);\n\n // We are trying to discern the type of `namespace`.\n /* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */\n if (namespace === KnownCaipNamespace.Wallet) {\n if (!reference) {\n return KnownWalletRpcMethods.includes(method);\n }\n\n if (reference === KnownCaipNamespace.Eip155) {\n return KnownWalletNamespaceRpcMethods[reference].includes(method);\n }\n\n return isSupportedNonEvmMethod();\n }\n\n if (namespace === KnownCaipNamespace.Eip155) {\n return KnownRpcMethods[namespace].includes(method);\n }\n /* eslint-enable @typescript-eslint/no-unsafe-enum-comparison */\n\n return isSupportedNonEvmMethod();\n};\n\n/**\n * Determines if a notification is supported by the wallet.\n *\n * @param scopeString - The scope string to check.\n * @param notification - The notification to check.\n * @returns A boolean indicating if the notification is supported by the wallet.\n */\nexport const isSupportedNotification = (\n scopeString: ExternalScopeString,\n notification: string,\n): boolean => {\n const { namespace } = parseScopeString(scopeString);\n\n if (namespace === KnownCaipNamespace.Eip155) {\n return KnownNotifications[namespace].includes(notification);\n }\n\n return false;\n};\n"]}
|
@@ -0,0 +1,52 @@
|
|
1
|
+
import type { CaipAccountId, CaipChainId, Hex } from "@metamask/utils";
|
2
|
+
import type { ExternalScopeString } from "./types.cjs";
|
3
|
+
/**
|
4
|
+
* Determines if a scope string is supported.
|
5
|
+
*
|
6
|
+
* @param scopeString - The scope string to check.
|
7
|
+
* @param hooks - An object containing the following properties:
|
8
|
+
* @param hooks.isEvmChainIdSupported - A predicate that determines if an EVM chainID is supported.
|
9
|
+
* @param hooks.isNonEvmScopeSupported - A predicate that determines if an non EVM scopeString is supported.
|
10
|
+
* @returns A boolean indicating if the scope string is supported.
|
11
|
+
*/
|
12
|
+
export declare const isSupportedScopeString: (scopeString: string, { isEvmChainIdSupported, isNonEvmScopeSupported, }: {
|
13
|
+
isEvmChainIdSupported: (chainId: Hex) => boolean;
|
14
|
+
isNonEvmScopeSupported: (scope: CaipChainId) => boolean;
|
15
|
+
}) => boolean;
|
16
|
+
/**
|
17
|
+
* Determines if an account is supported by the wallet (i.e. on a keyring known to the wallet).
|
18
|
+
*
|
19
|
+
* @param account - The CAIP account ID to check.
|
20
|
+
* @param hooks - An object containing the following properties:
|
21
|
+
* @param hooks.getEvmInternalAccounts - A function that returns the EVM internal accounts.
|
22
|
+
* @param hooks.getNonEvmAccountAddresses - A function that returns the supported CAIP-10 account addresses for a non EVM scope.
|
23
|
+
* @returns A boolean indicating if the account is supported by the wallet.
|
24
|
+
*/
|
25
|
+
export declare const isSupportedAccount: (account: CaipAccountId, { getEvmInternalAccounts, getNonEvmAccountAddresses, }: {
|
26
|
+
getEvmInternalAccounts: () => {
|
27
|
+
type: string;
|
28
|
+
address: Hex;
|
29
|
+
}[];
|
30
|
+
getNonEvmAccountAddresses: (scope: CaipChainId) => string[];
|
31
|
+
}) => boolean;
|
32
|
+
/**
|
33
|
+
* Determines if a method is supported by the wallet.
|
34
|
+
*
|
35
|
+
* @param scopeString - The scope string to check.
|
36
|
+
* @param method - The method to check.
|
37
|
+
* @param hooks - An object containing the following properties:
|
38
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
39
|
+
* @returns A boolean indicating if the method is supported by the wallet.
|
40
|
+
*/
|
41
|
+
export declare const isSupportedMethod: (scopeString: ExternalScopeString, method: string, { getNonEvmSupportedMethods, }: {
|
42
|
+
getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
|
43
|
+
}) => boolean;
|
44
|
+
/**
|
45
|
+
* Determines if a notification is supported by the wallet.
|
46
|
+
*
|
47
|
+
* @param scopeString - The scope string to check.
|
48
|
+
* @param notification - The notification to check.
|
49
|
+
* @returns A boolean indicating if the notification is supported by the wallet.
|
50
|
+
*/
|
51
|
+
export declare const isSupportedNotification: (scopeString: ExternalScopeString, notification: string) => boolean;
|
52
|
+
//# sourceMappingURL=supported.d.cts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"supported.d.cts","sourceRoot":"","sources":["../../src/scope/supported.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,GAAG,EAAE,wBAAwB;AAcvE,OAAO,KAAK,EAAE,mBAAmB,EAAE,oBAAgB;AAGnD;;;;;;;;GAQG;AACH,eAAO,MAAM,sBAAsB,gBACpB,MAAM;qCAKgB,GAAG,KAAK,OAAO;oCAChB,WAAW,KAAK,OAAO;aAyB1D,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,kBAAkB,YACpB,aAAa;4BAKI,MAAM;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,GAAG,CAAA;KAAE,EAAE;uCAC3B,WAAW,KAAK,MAAM,EAAE;aAiC9D,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,iBAAiB,gBACf,mBAAmB,UACxB,MAAM;uCAIuB,WAAW,KAAK,MAAM,EAAE;MAE5D,OA+BF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,gBACrB,mBAAmB,gBAClB,MAAM,KACnB,OAQF,CAAC"}
|
@@ -0,0 +1,52 @@
|
|
1
|
+
import type { CaipAccountId, CaipChainId, Hex } from "@metamask/utils";
|
2
|
+
import type { ExternalScopeString } from "./types.mjs";
|
3
|
+
/**
|
4
|
+
* Determines if a scope string is supported.
|
5
|
+
*
|
6
|
+
* @param scopeString - The scope string to check.
|
7
|
+
* @param hooks - An object containing the following properties:
|
8
|
+
* @param hooks.isEvmChainIdSupported - A predicate that determines if an EVM chainID is supported.
|
9
|
+
* @param hooks.isNonEvmScopeSupported - A predicate that determines if an non EVM scopeString is supported.
|
10
|
+
* @returns A boolean indicating if the scope string is supported.
|
11
|
+
*/
|
12
|
+
export declare const isSupportedScopeString: (scopeString: string, { isEvmChainIdSupported, isNonEvmScopeSupported, }: {
|
13
|
+
isEvmChainIdSupported: (chainId: Hex) => boolean;
|
14
|
+
isNonEvmScopeSupported: (scope: CaipChainId) => boolean;
|
15
|
+
}) => boolean;
|
16
|
+
/**
|
17
|
+
* Determines if an account is supported by the wallet (i.e. on a keyring known to the wallet).
|
18
|
+
*
|
19
|
+
* @param account - The CAIP account ID to check.
|
20
|
+
* @param hooks - An object containing the following properties:
|
21
|
+
* @param hooks.getEvmInternalAccounts - A function that returns the EVM internal accounts.
|
22
|
+
* @param hooks.getNonEvmAccountAddresses - A function that returns the supported CAIP-10 account addresses for a non EVM scope.
|
23
|
+
* @returns A boolean indicating if the account is supported by the wallet.
|
24
|
+
*/
|
25
|
+
export declare const isSupportedAccount: (account: CaipAccountId, { getEvmInternalAccounts, getNonEvmAccountAddresses, }: {
|
26
|
+
getEvmInternalAccounts: () => {
|
27
|
+
type: string;
|
28
|
+
address: Hex;
|
29
|
+
}[];
|
30
|
+
getNonEvmAccountAddresses: (scope: CaipChainId) => string[];
|
31
|
+
}) => boolean;
|
32
|
+
/**
|
33
|
+
* Determines if a method is supported by the wallet.
|
34
|
+
*
|
35
|
+
* @param scopeString - The scope string to check.
|
36
|
+
* @param method - The method to check.
|
37
|
+
* @param hooks - An object containing the following properties:
|
38
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
39
|
+
* @returns A boolean indicating if the method is supported by the wallet.
|
40
|
+
*/
|
41
|
+
export declare const isSupportedMethod: (scopeString: ExternalScopeString, method: string, { getNonEvmSupportedMethods, }: {
|
42
|
+
getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
|
43
|
+
}) => boolean;
|
44
|
+
/**
|
45
|
+
* Determines if a notification is supported by the wallet.
|
46
|
+
*
|
47
|
+
* @param scopeString - The scope string to check.
|
48
|
+
* @param notification - The notification to check.
|
49
|
+
* @returns A boolean indicating if the notification is supported by the wallet.
|
50
|
+
*/
|
51
|
+
export declare const isSupportedNotification: (scopeString: ExternalScopeString, notification: string) => boolean;
|
52
|
+
//# sourceMappingURL=supported.d.mts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"supported.d.mts","sourceRoot":"","sources":["../../src/scope/supported.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,WAAW,EAAE,GAAG,EAAE,wBAAwB;AAcvE,OAAO,KAAK,EAAE,mBAAmB,EAAE,oBAAgB;AAGnD;;;;;;;;GAQG;AACH,eAAO,MAAM,sBAAsB,gBACpB,MAAM;qCAKgB,GAAG,KAAK,OAAO;oCAChB,WAAW,KAAK,OAAO;aAyB1D,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,kBAAkB,YACpB,aAAa;4BAKI,MAAM;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,GAAG,CAAA;KAAE,EAAE;uCAC3B,WAAW,KAAK,MAAM,EAAE;aAiC9D,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,iBAAiB,gBACf,mBAAmB,UACxB,MAAM;uCAIuB,WAAW,KAAK,MAAM,EAAE;MAE5D,OA+BF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,gBACrB,mBAAmB,gBAClB,MAAM,KACnB,OAQF,CAAC"}
|
@@ -0,0 +1,109 @@
|
|
1
|
+
import { toHex, isEqualCaseInsensitive } from "@metamask/controller-utils";
|
2
|
+
import { isCaipChainId, KnownCaipNamespace, parseCaipAccountId } from "@metamask/utils";
|
3
|
+
import { CaipReferenceRegexes, KnownNotifications, KnownRpcMethods, KnownWalletNamespaceRpcMethods, KnownWalletRpcMethods } from "./constants.mjs";
|
4
|
+
import { parseScopeString } from "./types.mjs";
|
5
|
+
/**
|
6
|
+
* Determines if a scope string is supported.
|
7
|
+
*
|
8
|
+
* @param scopeString - The scope string to check.
|
9
|
+
* @param hooks - An object containing the following properties:
|
10
|
+
* @param hooks.isEvmChainIdSupported - A predicate that determines if an EVM chainID is supported.
|
11
|
+
* @param hooks.isNonEvmScopeSupported - A predicate that determines if an non EVM scopeString is supported.
|
12
|
+
* @returns A boolean indicating if the scope string is supported.
|
13
|
+
*/
|
14
|
+
export const isSupportedScopeString = (scopeString, { isEvmChainIdSupported, isNonEvmScopeSupported, }) => {
|
15
|
+
const { namespace, reference } = parseScopeString(scopeString);
|
16
|
+
switch (namespace) {
|
17
|
+
case KnownCaipNamespace.Wallet:
|
18
|
+
if (isCaipChainId(scopeString) &&
|
19
|
+
reference !== KnownCaipNamespace.Eip155) {
|
20
|
+
return isNonEvmScopeSupported(scopeString);
|
21
|
+
}
|
22
|
+
return true;
|
23
|
+
case KnownCaipNamespace.Eip155:
|
24
|
+
return (!reference ||
|
25
|
+
(CaipReferenceRegexes.eip155.test(reference) &&
|
26
|
+
isEvmChainIdSupported(toHex(reference))));
|
27
|
+
default:
|
28
|
+
return isCaipChainId(scopeString)
|
29
|
+
? isNonEvmScopeSupported(scopeString)
|
30
|
+
: false;
|
31
|
+
}
|
32
|
+
};
|
33
|
+
/**
|
34
|
+
* Determines if an account is supported by the wallet (i.e. on a keyring known to the wallet).
|
35
|
+
*
|
36
|
+
* @param account - The CAIP account ID to check.
|
37
|
+
* @param hooks - An object containing the following properties:
|
38
|
+
* @param hooks.getEvmInternalAccounts - A function that returns the EVM internal accounts.
|
39
|
+
* @param hooks.getNonEvmAccountAddresses - A function that returns the supported CAIP-10 account addresses for a non EVM scope.
|
40
|
+
* @returns A boolean indicating if the account is supported by the wallet.
|
41
|
+
*/
|
42
|
+
export const isSupportedAccount = (account, { getEvmInternalAccounts, getNonEvmAccountAddresses, }) => {
|
43
|
+
const { address, chainId, chain: { namespace, reference }, } = parseCaipAccountId(account);
|
44
|
+
const isSupportedEip155Account = () => getEvmInternalAccounts().some((internalAccount) => ['eip155:eoa', 'eip155:erc4337'].includes(internalAccount.type) &&
|
45
|
+
isEqualCaseInsensitive(address, internalAccount.address));
|
46
|
+
const isSupportedNonEvmAccount = () => getNonEvmAccountAddresses(chainId).includes(account);
|
47
|
+
// We are trying to discern the type of `namespace`.
|
48
|
+
/* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */
|
49
|
+
switch (namespace) {
|
50
|
+
case KnownCaipNamespace.Wallet:
|
51
|
+
if (reference === KnownCaipNamespace.Eip155) {
|
52
|
+
return isSupportedEip155Account();
|
53
|
+
}
|
54
|
+
return isSupportedNonEvmAccount();
|
55
|
+
case KnownCaipNamespace.Eip155:
|
56
|
+
return isSupportedEip155Account();
|
57
|
+
default:
|
58
|
+
return isSupportedNonEvmAccount();
|
59
|
+
}
|
60
|
+
/* eslint-enable @typescript-eslint/no-unsafe-enum-comparison */
|
61
|
+
};
|
62
|
+
/**
|
63
|
+
* Determines if a method is supported by the wallet.
|
64
|
+
*
|
65
|
+
* @param scopeString - The scope string to check.
|
66
|
+
* @param method - The method to check.
|
67
|
+
* @param hooks - An object containing the following properties:
|
68
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
69
|
+
* @returns A boolean indicating if the method is supported by the wallet.
|
70
|
+
*/
|
71
|
+
export const isSupportedMethod = (scopeString, method, { getNonEvmSupportedMethods, }) => {
|
72
|
+
const { namespace, reference } = parseScopeString(scopeString);
|
73
|
+
if (!namespace) {
|
74
|
+
return false;
|
75
|
+
}
|
76
|
+
const isSupportedNonEvmMethod = () => isCaipChainId(scopeString) &&
|
77
|
+
getNonEvmSupportedMethods(scopeString).includes(method);
|
78
|
+
// We are trying to discern the type of `namespace`.
|
79
|
+
/* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */
|
80
|
+
if (namespace === KnownCaipNamespace.Wallet) {
|
81
|
+
if (!reference) {
|
82
|
+
return KnownWalletRpcMethods.includes(method);
|
83
|
+
}
|
84
|
+
if (reference === KnownCaipNamespace.Eip155) {
|
85
|
+
return KnownWalletNamespaceRpcMethods[reference].includes(method);
|
86
|
+
}
|
87
|
+
return isSupportedNonEvmMethod();
|
88
|
+
}
|
89
|
+
if (namespace === KnownCaipNamespace.Eip155) {
|
90
|
+
return KnownRpcMethods[namespace].includes(method);
|
91
|
+
}
|
92
|
+
/* eslint-enable @typescript-eslint/no-unsafe-enum-comparison */
|
93
|
+
return isSupportedNonEvmMethod();
|
94
|
+
};
|
95
|
+
/**
|
96
|
+
* Determines if a notification is supported by the wallet.
|
97
|
+
*
|
98
|
+
* @param scopeString - The scope string to check.
|
99
|
+
* @param notification - The notification to check.
|
100
|
+
* @returns A boolean indicating if the notification is supported by the wallet.
|
101
|
+
*/
|
102
|
+
export const isSupportedNotification = (scopeString, notification) => {
|
103
|
+
const { namespace } = parseScopeString(scopeString);
|
104
|
+
if (namespace === KnownCaipNamespace.Eip155) {
|
105
|
+
return KnownNotifications[namespace].includes(notification);
|
106
|
+
}
|
107
|
+
return false;
|
108
|
+
};
|
109
|
+
//# sourceMappingURL=supported.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"supported.mjs","sourceRoot":"","sources":["../../src/scope/supported.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,sBAAsB,EAAE,mCAAmC;AAE3E,OAAO,EACL,aAAa,EACb,kBAAkB,EAClB,kBAAkB,EACnB,wBAAwB;AAEzB,OAAO,EACL,oBAAoB,EACpB,kBAAkB,EAClB,eAAe,EACf,8BAA8B,EAC9B,qBAAqB,EACtB,wBAAoB;AAErB,OAAO,EAAE,gBAAgB,EAAE,oBAAgB;AAE3C;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,CACpC,WAAmB,EACnB,EACE,qBAAqB,EACrB,sBAAsB,GAIvB,EACD,EAAE;IACF,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,gBAAgB,CAAC,WAAW,CAAC,CAAC;IAE/D,QAAQ,SAAS,EAAE;QACjB,KAAK,kBAAkB,CAAC,MAAM;YAC5B,IACE,aAAa,CAAC,WAAW,CAAC;gBAC1B,SAAS,KAAK,kBAAkB,CAAC,MAAM,EACvC;gBACA,OAAO,sBAAsB,CAAC,WAAW,CAAC,CAAC;aAC5C;YACD,OAAO,IAAI,CAAC;QACd,KAAK,kBAAkB,CAAC,MAAM;YAC5B,OAAO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AAC,YAAY,CAAC,CAAC;KAC7D;IAED,OAAO,KAAK,CAAC;AACf,CAAC,CAAC","sourcesContent":["import { toHex, isEqualCaseInsensitive } from '@metamask/controller-utils';\nimport type { CaipAccountId, CaipChainId, Hex } from '@metamask/utils';\nimport {\n isCaipChainId,\n KnownCaipNamespace,\n parseCaipAccountId,\n} from '@metamask/utils';\n\nimport {\n CaipReferenceRegexes,\n KnownNotifications,\n KnownRpcMethods,\n KnownWalletNamespaceRpcMethods,\n KnownWalletRpcMethods,\n} from './constants';\nimport type { ExternalScopeString } from './types';\nimport { parseScopeString } from './types';\n\n/**\n * Determines if a scope string is supported.\n *\n * @param scopeString - The scope string to check.\n * @param hooks - An object containing the following properties:\n * @param hooks.isEvmChainIdSupported - A predicate that determines if an EVM chainID is supported.\n * @param hooks.isNonEvmScopeSupported - A predicate that determines if an non EVM scopeString is supported.\n * @returns A boolean indicating if the scope string is supported.\n */\nexport const isSupportedScopeString = (\n scopeString: string,\n {\n isEvmChainIdSupported,\n isNonEvmScopeSupported,\n }: {\n isEvmChainIdSupported: (chainId: Hex) => boolean;\n isNonEvmScopeSupported: (scope: CaipChainId) => boolean;\n },\n) => {\n const { namespace, reference } = parseScopeString(scopeString);\n\n switch (namespace) {\n case KnownCaipNamespace.Wallet:\n if (\n isCaipChainId(scopeString) &&\n reference !== KnownCaipNamespace.Eip155\n ) {\n return isNonEvmScopeSupported(scopeString);\n }\n return true;\n case KnownCaipNamespace.Eip155:\n return (\n !reference ||\n (CaipReferenceRegexes.eip155.test(reference) &&\n isEvmChainIdSupported(toHex(reference)))\n );\n default:\n return isCaipChainId(scopeString)\n ? isNonEvmScopeSupported(scopeString)\n : false;\n }\n};\n\n/**\n * Determines if an account is supported by the wallet (i.e. on a keyring known to the wallet).\n *\n * @param account - The CAIP account ID to check.\n * @param hooks - An object containing the following properties:\n * @param hooks.getEvmInternalAccounts - A function that returns the EVM internal accounts.\n * @param hooks.getNonEvmAccountAddresses - A function that returns the supported CAIP-10 account addresses for a non EVM scope.\n * @returns A boolean indicating if the account is supported by the wallet.\n */\nexport const isSupportedAccount = (\n account: CaipAccountId,\n {\n getEvmInternalAccounts,\n getNonEvmAccountAddresses,\n }: {\n getEvmInternalAccounts: () => { type: string; address: Hex }[];\n getNonEvmAccountAddresses: (scope: CaipChainId) => string[];\n },\n) => {\n const {\n address,\n chainId,\n chain: { namespace, reference },\n } = parseCaipAccountId(account);\n\n const isSupportedEip155Account = () =>\n getEvmInternalAccounts().some(\n (internalAccount) =>\n ['eip155:eoa', 'eip155:erc4337'].includes(internalAccount.type) &&\n isEqualCaseInsensitive(address, internalAccount.address),\n );\n\n const isSupportedNonEvmAccount = () =>\n getNonEvmAccountAddresses(chainId).includes(account);\n\n // We are trying to discern the type of `namespace`.\n /* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */\n switch (namespace) {\n case KnownCaipNamespace.Wallet:\n if (reference === KnownCaipNamespace.Eip155) {\n return isSupportedEip155Account();\n }\n return isSupportedNonEvmAccount();\n case KnownCaipNamespace.Eip155:\n return isSupportedEip155Account();\n default:\n return isSupportedNonEvmAccount();\n }\n /* eslint-enable @typescript-eslint/no-unsafe-enum-comparison */\n};\n\n/**\n * Determines if a method is supported by the wallet.\n *\n * @param scopeString - The scope string to check.\n * @param method - The method to check.\n * @param hooks - An object containing the following properties:\n * @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.\n * @returns A boolean indicating if the method is supported by the wallet.\n */\nexport const isSupportedMethod = (\n scopeString: ExternalScopeString,\n method: string,\n {\n getNonEvmSupportedMethods,\n }: {\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n },\n): boolean => {\n const { namespace, reference } = parseScopeString(scopeString);\n\n if (!namespace) {\n return false;\n }\n\n const isSupportedNonEvmMethod = () =>\n isCaipChainId(scopeString) &&\n getNonEvmSupportedMethods(scopeString).includes(method);\n\n // We are trying to discern the type of `namespace`.\n /* eslint-disable @typescript-eslint/no-unsafe-enum-comparison */\n if (namespace === KnownCaipNamespace.Wallet) {\n if (!reference) {\n return KnownWalletRpcMethods.includes(method);\n }\n\n if (reference === KnownCaipNamespace.Eip155) {\n return KnownWalletNamespaceRpcMethods[reference].includes(method);\n }\n\n return isSupportedNonEvmMethod();\n }\n\n if (namespace === KnownCaipNamespace.Eip155) {\n return KnownRpcMethods[namespace].includes(method);\n }\n /* eslint-enable @typescript-eslint/no-unsafe-enum-comparison */\n\n return isSupportedNonEvmMethod();\n};\n\n/**\n * Determines if a notification is supported by the wallet.\n *\n * @param scopeString - The scope string to check.\n * @param notification - The notification to check.\n * @returns A boolean indicating if the notification is supported by the wallet.\n */\nexport const isSupportedNotification = (\n scopeString: ExternalScopeString,\n notification: string,\n): boolean => {\n const { namespace } = parseScopeString(scopeString);\n\n if (namespace === KnownCaipNamespace.Eip155) {\n return KnownNotifications[namespace].includes(notification);\n }\n\n return false;\n};\n"]}
|
@@ -0,0 +1,152 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.normalizeAndMergeScopes = exports.mergeInternalScopes = exports.mergeNormalizedScopes = exports.mergeScopeObject = exports.normalizeScope = exports.getUniqueArrayItems = void 0;
|
4
|
+
const lodash_1 = require("lodash");
|
5
|
+
const types_1 = require("./types.cjs");
|
6
|
+
/**
|
7
|
+
* Returns a list of unique items
|
8
|
+
*
|
9
|
+
* @param list - The list of items to filter
|
10
|
+
* @returns A list of unique items
|
11
|
+
*/
|
12
|
+
const getUniqueArrayItems = (list) => {
|
13
|
+
return Array.from(new Set(list));
|
14
|
+
};
|
15
|
+
exports.getUniqueArrayItems = getUniqueArrayItems;
|
16
|
+
/**
|
17
|
+
* Normalizes a ScopeString and ExternalScopeObject into a separate
|
18
|
+
* InternalScopeString and NormalizedScopeObject for each reference in the `references`
|
19
|
+
* value if defined and adds an empty `accounts` array if not defined.
|
20
|
+
*
|
21
|
+
* @param scopeString - The string representing the scope
|
22
|
+
* @param externalScopeObject - The object that defines the scope
|
23
|
+
* @returns a map of caipChainId to ScopeObjects
|
24
|
+
*/
|
25
|
+
const normalizeScope = (scopeString, externalScopeObject) => {
|
26
|
+
const { references, ...scopeObject } = externalScopeObject;
|
27
|
+
const { namespace, reference } = (0, types_1.parseScopeString)(scopeString);
|
28
|
+
const normalizedScopeObject = {
|
29
|
+
accounts: [],
|
30
|
+
...scopeObject,
|
31
|
+
};
|
32
|
+
const shouldFlatten = namespace &&
|
33
|
+
!reference &&
|
34
|
+
references !== undefined &&
|
35
|
+
references.length > 0;
|
36
|
+
if (shouldFlatten) {
|
37
|
+
return Object.fromEntries(references.map((ref) => [
|
38
|
+
`${namespace}:${ref}`,
|
39
|
+
(0, lodash_1.cloneDeep)(normalizedScopeObject),
|
40
|
+
]));
|
41
|
+
}
|
42
|
+
return { [scopeString]: normalizedScopeObject };
|
43
|
+
};
|
44
|
+
exports.normalizeScope = normalizeScope;
|
45
|
+
/**
|
46
|
+
* Merges two NormalizedScopeObjects
|
47
|
+
*
|
48
|
+
* @param scopeObjectA - The first scope object to merge.
|
49
|
+
* @param scopeObjectB - The second scope object to merge.
|
50
|
+
* @returns The merged scope object.
|
51
|
+
*/
|
52
|
+
const mergeScopeObject = (scopeObjectA, scopeObjectB) => {
|
53
|
+
const mergedScopeObject = {
|
54
|
+
methods: (0, exports.getUniqueArrayItems)([
|
55
|
+
...scopeObjectA.methods,
|
56
|
+
...scopeObjectB.methods,
|
57
|
+
]),
|
58
|
+
notifications: (0, exports.getUniqueArrayItems)([
|
59
|
+
...scopeObjectA.notifications,
|
60
|
+
...scopeObjectB.notifications,
|
61
|
+
]),
|
62
|
+
accounts: (0, exports.getUniqueArrayItems)([
|
63
|
+
...scopeObjectA.accounts,
|
64
|
+
...scopeObjectB.accounts,
|
65
|
+
]),
|
66
|
+
};
|
67
|
+
if (scopeObjectA.rpcDocuments || scopeObjectB.rpcDocuments) {
|
68
|
+
mergedScopeObject.rpcDocuments = (0, exports.getUniqueArrayItems)([
|
69
|
+
...(scopeObjectA.rpcDocuments ?? []),
|
70
|
+
...(scopeObjectB.rpcDocuments ?? []),
|
71
|
+
]);
|
72
|
+
}
|
73
|
+
if (scopeObjectA.rpcEndpoints || scopeObjectB.rpcEndpoints) {
|
74
|
+
mergedScopeObject.rpcEndpoints = (0, exports.getUniqueArrayItems)([
|
75
|
+
...(scopeObjectA.rpcEndpoints ?? []),
|
76
|
+
...(scopeObjectB.rpcEndpoints ?? []),
|
77
|
+
]);
|
78
|
+
}
|
79
|
+
return mergedScopeObject;
|
80
|
+
};
|
81
|
+
exports.mergeScopeObject = mergeScopeObject;
|
82
|
+
/**
|
83
|
+
* Merges two NormalizedScopeObjects
|
84
|
+
*
|
85
|
+
* @param scopeA - The first normalized scope object to merge.
|
86
|
+
* @param scopeB - The second normalized scope object to merge.
|
87
|
+
* @returns The merged normalized scope object from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request.
|
88
|
+
*/
|
89
|
+
const mergeNormalizedScopes = (scopeA, scopeB) => {
|
90
|
+
const scope = {};
|
91
|
+
Object.entries(scopeA).forEach(([_scopeString, scopeObjectA]) => {
|
92
|
+
// Cast needed because index type is returned as `string` by `Object.entries`
|
93
|
+
const scopeString = _scopeString;
|
94
|
+
const scopeObjectB = scopeB[scopeString];
|
95
|
+
scope[scopeString] = scopeObjectB
|
96
|
+
? (0, exports.mergeScopeObject)(scopeObjectA, scopeObjectB)
|
97
|
+
: scopeObjectA;
|
98
|
+
});
|
99
|
+
Object.entries(scopeB).forEach(([_scopeString, scopeObjectB]) => {
|
100
|
+
// Cast needed because index type is returned as `string` by `Object.entries`
|
101
|
+
const scopeString = _scopeString;
|
102
|
+
const scopeObjectA = scopeA[scopeString];
|
103
|
+
if (!scopeObjectA) {
|
104
|
+
scope[scopeString] = scopeObjectB;
|
105
|
+
}
|
106
|
+
});
|
107
|
+
return scope;
|
108
|
+
};
|
109
|
+
exports.mergeNormalizedScopes = mergeNormalizedScopes;
|
110
|
+
/**
|
111
|
+
* Merges two InternalScopeObjects
|
112
|
+
*
|
113
|
+
* @param scopeA - The first internal scope object to merge.
|
114
|
+
* @param scopeB - The second internal scope object to merge.
|
115
|
+
* @returns The merged internal scope object from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request.
|
116
|
+
*/
|
117
|
+
const mergeInternalScopes = (scopeA, scopeB) => {
|
118
|
+
const resultScope = (0, lodash_1.cloneDeep)(scopeA);
|
119
|
+
Object.entries(scopeB).forEach(([scopeString, rightScopeObject]) => {
|
120
|
+
const internalScopeString = scopeString;
|
121
|
+
const leftRequiredScopeObject = resultScope[internalScopeString];
|
122
|
+
if (!leftRequiredScopeObject) {
|
123
|
+
resultScope[internalScopeString] = rightScopeObject;
|
124
|
+
}
|
125
|
+
else {
|
126
|
+
resultScope[internalScopeString] = {
|
127
|
+
accounts: (0, exports.getUniqueArrayItems)([
|
128
|
+
...leftRequiredScopeObject.accounts,
|
129
|
+
...rightScopeObject.accounts,
|
130
|
+
]),
|
131
|
+
};
|
132
|
+
}
|
133
|
+
});
|
134
|
+
return resultScope;
|
135
|
+
};
|
136
|
+
exports.mergeInternalScopes = mergeInternalScopes;
|
137
|
+
/**
|
138
|
+
* Normalizes and merges a set of ExternalScopesObjects into a NormalizedScopesObject (i.e. a set of NormalizedScopeObjects where references are flattened).
|
139
|
+
*
|
140
|
+
* @param scopes - The external scopes to normalize and merge.
|
141
|
+
* @returns The normalized and merged scopes.
|
142
|
+
*/
|
143
|
+
const normalizeAndMergeScopes = (scopes) => {
|
144
|
+
let mergedScopes = {};
|
145
|
+
Object.keys(scopes).forEach((scopeString) => {
|
146
|
+
const normalizedScopes = (0, exports.normalizeScope)(scopeString, scopes[scopeString]);
|
147
|
+
mergedScopes = (0, exports.mergeNormalizedScopes)(mergedScopes, normalizedScopes);
|
148
|
+
});
|
149
|
+
return mergedScopes;
|
150
|
+
};
|
151
|
+
exports.normalizeAndMergeScopes = normalizeAndMergeScopes;
|
152
|
+
//# sourceMappingURL=transform.cjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"transform.cjs","sourceRoot":"","sources":["../../src/scope/transform.ts"],"names":[],"mappings":";;;AACA,mCAAmC;AASnC,uCAA2C;AAE3C;;;;;GAKG;AACI,MAAM,mBAAmB,GAAG,CAAQ,IAAa,EAAW,EAAE;IACnE,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;AACnC,CAAC,CAAC;AAFW,QAAA,mBAAmB,uBAE9B;AAEF;;;;;;;;GAQG;AACI,MAAM,cAAc,GAAG,CAC5B,WAAmB,EACnB,mBAAwC,EAChB,EAAE;IAC1B,MAAM,EAAE,UAAU,EAAE,GAAG,WAAW,EAAE,GAAG,mBAAmB,CAAC;IAC3D,MAAM,EAAE,SAAS,EAAE,SAAS,EAAE,GAAG,IAAA,wBAAgB,EAAC,WAAW,CAAC,CAAC;IAE/D,MAAM,qBAAqB,GAA0B;QACnD,QAAQ,EAAE,EAAE;QACZ,GAAG,WAAW;KACf,CAAC;IAEF,MAAM,aAAa,GACjB,SAAS;QACT,CAAC,SAAS;QACV,UAAU,KAAK,SAAS;QACxB,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC;IAExB,IAAI,aAAa,EAAE;QACjB,OAAO,MAAM,CAAC,WAAW,CACvB,UAAU,CAAC,GAAG,CAAC,CAAC,GAAkB,EAAE,EAAE,CAAC;YACrC,GAAG,SAAS,IAAI,GAAG,EAAE;YACrB,IAAA,kBAAS,EAAC,qBAAqB,CAAC;SACjC,CAAC,CACH,CAAC;KACH;IACD,OAAO,EAAE,CAAC,WAAW,CAAC,EAAE,qBAAqB,EAAE,CAAC;AAClD,CAAC,CAAC;AA3BW,QAAA,cAAc,kBA2BzB;AAEF;;;;;;GAMG;AACI,MAAM,gBAAgB,GAAG,CAC9B,YAAmC,EACnC,YAAmC,EACnC,EAAE;IACF,MAAM,iBAAiB,GAA0B;QAC/C,OAA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H,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAtBW,QAAA,mBAAmB,uBAsB9B;AAEF;;;;;GAKG;AACI,MAAM,uBAAuB,GAAG,CACrC,MAA4B,EACJ,EAAE;IAC1B,IAAI,YAAY,GAA2B,EAAE,CAAC;IAC9C,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,WAAW,EAAE,EAAE;QAC1C,MAAM,gBAAgB,GAAG,IAAA,sBAAc,EAAC,WAAW,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC;QAC1E,YAAY,GAAG,IAAA,6BAAqB,EAAC,YAAY,EAAE,gBAAgB,CAAC,CAAC;IACvE,CAAC,CAAC,CAAC;IAEH,OAAO,YAAY,CAAC;AACtB,CAAC,CAAC;AAVW,QAAA,uBAAuB,2BAUlC","sourcesContent":["import type { CaipReference } from '@metamask/utils';\nimport { cloneDeep } from 'lodash';\n\nimport type {\n ExternalScopeObject,\n ExternalScopesObject,\n InternalScopesObject,\n NormalizedScopeObject,\n NormalizedScopesObject,\n} from './types';\nimport { parseScopeString } from './types';\n\n/**\n * Returns a list of unique items\n *\n * @param list - The list of items to filter\n * @returns A list of unique items\n */\nexport const getUniqueArrayItems = <Value>(list: Value[]): Value[] => {\n return Array.from(new Set(list));\n};\n\n/**\n * Normalizes a ScopeString and ExternalScopeObject into a separate\n * InternalScopeString and NormalizedScopeObject for each reference in the `references`\n * value if defined and adds an empty `accounts` array if not defined.\n *\n * @param scopeString - The string representing the scope\n * @param externalScopeObject - The object that defines the scope\n * @returns a map of caipChainId to ScopeObjects\n */\nexport const normalizeScope = (\n scopeString: string,\n externalScopeObject: ExternalScopeObject,\n): NormalizedScopesObject => {\n const { references, ...scopeObject } = externalScopeObject;\n const { namespace, reference } = parseScopeString(scopeString);\n\n const normalizedScopeObject: NormalizedScopeObject = {\n accounts: [],\n ...scopeObject,\n };\n\n const shouldFlatten =\n namespace &&\n !reference &&\n references !== undefined &&\n references.length > 0;\n\n if (shouldFlatten) {\n return Object.fromEntries(\n references.map((ref: CaipReference) => [\n `${namespace}:${ref}`,\n cloneDeep(normalizedScopeObject),\n ]),\n );\n }\n return { [scopeString]: normalizedScopeObject };\n};\n\n/**\n * Merges two NormalizedScopeObjects\n *\n * @param scopeObjectA - The first scope object to merge.\n * @param scopeObjectB - The second scope object to merge.\n * @returns The merged scope object.\n */\nexport const mergeScopeObject = (\n scopeObjectA: NormalizedScopeObject,\n scopeObjectB: NormalizedScopeObject,\n) => {\n const mergedScopeObject: NormalizedScopeObject = {\n methods: getUniqueArrayItems([\n ...scopeObjectA.methods,\n ...scopeObjectB.methods,\n ]),\n notifications: getUniqueArrayItems([\n ...scopeObjectA.notifications,\n ...scopeObjectB.notifications,\n ]),\n accounts: getUniqueArrayItems([\n ...scopeObjectA.accounts,\n ...scopeObjectB.accounts,\n ]),\n };\n\n if (scopeObjectA.rpcDocuments || scopeObjectB.rpcDocuments) {\n mergedScopeObject.rpcDocuments = getUniqueArrayItems([\n ...(scopeObjectA.rpcDocuments ?? []),\n ...(scopeObjectB.rpcDocuments ?? []),\n ]);\n }\n\n if (scopeObjectA.rpcEndpoints || scopeObjectB.rpcEndpoints) {\n mergedScopeObject.rpcEndpoints = getUniqueArrayItems([\n ...(scopeObjectA.rpcEndpoints ?? []),\n ...(scopeObjectB.rpcEndpoints ?? []),\n ]);\n }\n\n return mergedScopeObject;\n};\n\n/**\n * Merges two NormalizedScopeObjects\n *\n * @param scopeA - The first normalized scope object to merge.\n * @param scopeB - The second normalized scope object to merge.\n * @returns The merged normalized scope object from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request.\n */\nexport const mergeNormalizedScopes = (\n scopeA: NormalizedScopesObject,\n scopeB: NormalizedScopesObject,\n): NormalizedScopesObject => {\n const scope: NormalizedScopesObject = {};\n\n Object.entries(scopeA).forEach(([_scopeString, scopeObjectA]) => {\n // Cast needed because index type is returned as `string` by `Object.entries`\n const scopeString = _scopeString as keyof typeof scopeA;\n const scopeObjectB = scopeB[scopeString];\n\n scope[scopeString] = scopeObjectB\n ? mergeScopeObject(scopeObjectA, scopeObjectB)\n : scopeObjectA;\n });\n\n Object.entries(scopeB).forEach(([_scopeString, scopeObjectB]) => {\n // Cast needed because index type is returned as `string` by `Object.entries`\n const scopeString = _scopeString as keyof typeof scopeB;\n const scopeObjectA = scopeA[scopeString];\n\n if (!scopeObjectA) {\n scope[scopeString] = scopeObjectB;\n }\n });\n\n return scope;\n};\n\n/**\n * Merges two InternalScopeObjects\n *\n * @param scopeA - The first internal scope object to merge.\n * @param scopeB - The second internal scope object to merge.\n * @returns The merged internal scope object from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request.\n */\nexport const mergeInternalScopes = (\n scopeA: InternalScopesObject,\n scopeB: InternalScopesObject,\n): InternalScopesObject => {\n const resultScope = cloneDeep(scopeA);\n\n Object.entries(scopeB).forEach(([scopeString, rightScopeObject]) => {\n const internalScopeString = scopeString as keyof typeof scopeB;\n const leftRequiredScopeObject = resultScope[internalScopeString];\n if (!leftRequiredScopeObject) {\n resultScope[internalScopeString] = rightScopeObject;\n } else {\n resultScope[internalScopeString] = {\n accounts: getUniqueArrayItems([\n ...leftRequiredScopeObject.accounts,\n ...rightScopeObject.accounts,\n ]),\n };\n }\n });\n\n return resultScope;\n};\n\n/**\n * Normalizes and merges a set of ExternalScopesObjects into a NormalizedScopesObject (i.e. a set of NormalizedScopeObjects where references are flattened).\n *\n * @param scopes - The external scopes to normalize and merge.\n * @returns The normalized and merged scopes.\n */\nexport const normalizeAndMergeScopes = (\n scopes: ExternalScopesObject,\n): NormalizedScopesObject => {\n let mergedScopes: NormalizedScopesObject = {};\n Object.keys(scopes).forEach((scopeString) => {\n const normalizedScopes = normalizeScope(scopeString, scopes[scopeString]);\n mergedScopes = mergeNormalizedScopes(mergedScopes, normalizedScopes);\n });\n\n return mergedScopes;\n};\n"]}
|