@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":"assert.cjs","sourceRoot":"","sources":["../../src/scope/assert.ts"],"names":[],"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":["import {\n type CaipChainId,\n hasProperty,\n isCaipAccountId,\n isCaipChainId,\n isCaipNamespace,\n isCaipReference,\n KnownCaipNamespace,\n type Hex,\n} from '@metamask/utils';\n\nimport { Caip25Errors } from './errors';\nimport {\n isSupportedMethod,\n isSupportedNotification,\n isSupportedScopeString,\n} from './supported';\nimport type {\n ExternalScopeObject,\n ExternalScopesObject,\n ExternalScopeString,\n InternalScopeObject,\n InternalScopesObject,\n InternalScopeString,\n NormalizedScopeObject,\n NormalizedScopesObject,\n} from './types';\n\n/**\n * Asserts that a scope string and its associated scope object are supported.\n *\n * @param scopeString - The scope string against which to assert support.\n * @param scopeObject - The scope object against which to assert support.\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 */\nexport const assertScopeSupported = (\n scopeString: string,\n scopeObject: NormalizedScopeObject,\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 { methods, notifications } = scopeObject;\n if (\n !isSupportedScopeString(scopeString, {\n isEvmChainIdSupported,\n isNonEvmScopeSupported,\n })\n ) {\n throw Caip25Errors.requestedChainsNotSupportedError();\n }\n\n const allMethodsSupported = methods.every((method) =>\n isSupportedMethod(scopeString, method, { getNonEvmSupportedMethods }),\n );\n\n if (!allMethodsSupported) {\n throw Caip25Errors.requestedMethodsNotSupportedError();\n }\n\n if (\n notifications &&\n !notifications.every((notification) =>\n isSupportedNotification(scopeString, notification),\n )\n ) {\n throw Caip25Errors.requestedNotificationsNotSupportedError();\n }\n};\n\n/**\n * Asserts that all scope strings and their associated scope objects are supported.\n *\n * @param scopes - The scopes object against which to assert support.\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 */\nexport const assertScopesSupported = (\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 for (const [scopeString, scopeObject] of Object.entries(scopes)) {\n assertScopeSupported(scopeString, scopeObject, {\n isEvmChainIdSupported,\n isNonEvmScopeSupported,\n getNonEvmSupportedMethods,\n });\n }\n};\n/**\n * Asserts that an object is a valid ExternalScopeObject.\n *\n * @param obj - The object to assert.\n */\nfunction assertIsExternalScopeObject(\n obj: unknown,\n): asserts obj is ExternalScopeObject {\n if (typeof obj !== 'object' || obj === null) {\n throw new Error('ExternalScopeObject must be an object');\n }\n\n if (hasProperty(obj, 'references')) {\n if (\n !Array.isArray(obj.references) ||\n !obj.references.every(isCaipReference)\n ) {\n throw new Error(\n 'ExternalScopeObject.references must be an array of CaipReference',\n );\n }\n }\n\n if (hasProperty(obj, 'accounts')) {\n if (!Array.isArray(obj.accounts) || !obj.accounts.every(isCaipAccountId)) {\n throw new Error(\n 'ExternalScopeObject.accounts must be an array of CaipAccountId',\n );\n }\n }\n\n if (hasProperty(obj, 'methods')) {\n if (\n !Array.isArray(obj.methods) ||\n !obj.methods.every((method) => typeof method === 'string')\n ) {\n throw new Error(\n 'ExternalScopeObject.methods must be an array of strings',\n );\n }\n }\n\n if (hasProperty(obj, 'notifications')) {\n if (\n !Array.isArray(obj.notifications) ||\n !obj.notifications.every(\n (notification) => typeof notification === 'string',\n )\n ) {\n throw new Error(\n 'ExternalScopeObject.notifications must be an array of strings',\n );\n }\n }\n\n if (hasProperty(obj, 'rpcDocuments')) {\n if (\n !Array.isArray(obj.rpcDocuments) ||\n !obj.rpcDocuments.every((doc) => typeof doc === 'string')\n ) {\n throw new Error(\n 'ExternalScopeObject.rpcDocuments must be an array of strings',\n );\n }\n }\n\n if (hasProperty(obj, 'rpcEndpoints')) {\n if (\n !Array.isArray(obj.rpcEndpoints) ||\n !obj.rpcEndpoints.every((endpoint) => typeof endpoint === 'string')\n ) {\n throw new Error(\n 'ExternalScopeObject.rpcEndpoints must be an array of strings',\n );\n }\n }\n}\n\n/**\n * Asserts that a scope string is a valid ExternalScopeString.\n *\n * @param scopeString - The scope string to assert.\n */\nfunction assertIsExternalScopeString(\n scopeString: unknown,\n): asserts scopeString is ExternalScopeString {\n if (\n typeof scopeString !== 'string' ||\n (!isCaipNamespace(scopeString) && !isCaipChainId(scopeString))\n ) {\n throw new Error('scopeString is not a valid ExternalScopeString');\n }\n}\n\n/**\n * Asserts that an object is a valid ExternalScopesObject.\n *\n * @param obj - The object to assert.\n */\nexport function assertIsExternalScopesObject(\n obj: unknown,\n): asserts obj is ExternalScopesObject {\n if (typeof obj !== 'object' || obj === null) {\n throw new Error('ExternalScopesObject must be an object');\n }\n\n for (const [scopeString, scopeObject] of Object.entries(obj)) {\n assertIsExternalScopeString(scopeString);\n assertIsExternalScopeObject(scopeObject);\n }\n}\n\n/**\n * Asserts that an object is a valid InternalScopeObject.\n *\n * @param obj - The object to assert.\n */\nfunction assertIsInternalScopeObject(\n obj: unknown,\n): asserts obj is InternalScopeObject {\n if (typeof obj !== 'object' || obj === null) {\n throw new Error('InternalScopeObject must be an object');\n }\n\n if (\n !hasProperty(obj, 'accounts') ||\n !Array.isArray(obj.accounts) ||\n !obj.accounts.every(isCaipAccountId)\n ) {\n throw new Error(\n 'InternalScopeObject.accounts must be an array of CaipAccountId',\n );\n }\n}\n\n/**\n * Asserts that a scope string is a valid InternalScopeString.\n *\n * @param scopeString - The scope string to assert.\n */\nexport function assertIsInternalScopeString(\n scopeString: unknown,\n): asserts scopeString is InternalScopeString {\n if (\n typeof scopeString !== 'string' ||\n // `InternalScopeString` is defined as either `KnownCaipNamespace.Wallet` or\n // `CaipChainId`, so our conditions intentionally match the type.\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n (scopeString !== KnownCaipNamespace.Wallet && !isCaipChainId(scopeString))\n ) {\n throw new Error('scopeString is not a valid InternalScopeString');\n }\n}\n\n/**\n * Asserts that an object is a valid InternalScopesObject.\n *\n * @param obj - The object to assert.\n */\nexport function assertIsInternalScopesObject(\n obj: unknown,\n): asserts obj is InternalScopesObject {\n if (typeof obj !== 'object' || obj === null) {\n throw new Error('InternalScopesObject must be an object');\n }\n\n for (const [scopeString, scopeObject] of Object.entries(obj)) {\n assertIsInternalScopeString(scopeString);\n assertIsInternalScopeObject(scopeObject);\n }\n}\n"]}
|
@@ -0,0 +1,50 @@
|
|
1
|
+
import { type CaipChainId, type Hex } from "@metamask/utils";
|
2
|
+
import type { ExternalScopesObject, InternalScopesObject, InternalScopeString, NormalizedScopeObject, NormalizedScopesObject } from "./types.cjs";
|
3
|
+
/**
|
4
|
+
* Asserts that a scope string and its associated scope object are supported.
|
5
|
+
*
|
6
|
+
* @param scopeString - The scope string against which to assert support.
|
7
|
+
* @param scopeObject - The scope object against which to assert support.
|
8
|
+
* @param hooks - An object containing the following properties:
|
9
|
+
* @param hooks.isEvmChainIdSupported - A predicate that determines if an EVM chainID is supported.
|
10
|
+
* @param hooks.isNonEvmScopeSupported - A predicate that determines if an non EVM scopeString is supported.
|
11
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
12
|
+
*/
|
13
|
+
export declare const assertScopeSupported: (scopeString: string, scopeObject: NormalizedScopeObject, { isEvmChainIdSupported, isNonEvmScopeSupported, getNonEvmSupportedMethods, }: {
|
14
|
+
isEvmChainIdSupported: (chainId: Hex) => boolean;
|
15
|
+
isNonEvmScopeSupported: (scope: CaipChainId) => boolean;
|
16
|
+
getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
|
17
|
+
}) => void;
|
18
|
+
/**
|
19
|
+
* Asserts that all scope strings and their associated scope objects are supported.
|
20
|
+
*
|
21
|
+
* @param scopes - The scopes object against which to assert support.
|
22
|
+
* @param hooks - An object containing the following properties:
|
23
|
+
* @param hooks.isEvmChainIdSupported - A predicate that determines if an EVM chainID is supported.
|
24
|
+
* @param hooks.isNonEvmScopeSupported - A predicate that determines if an non EVM scopeString is supported.
|
25
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
26
|
+
*/
|
27
|
+
export declare const assertScopesSupported: (scopes: NormalizedScopesObject, { isEvmChainIdSupported, isNonEvmScopeSupported, getNonEvmSupportedMethods, }: {
|
28
|
+
isEvmChainIdSupported: (chainId: Hex) => boolean;
|
29
|
+
isNonEvmScopeSupported: (scope: CaipChainId) => boolean;
|
30
|
+
getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
|
31
|
+
}) => void;
|
32
|
+
/**
|
33
|
+
* Asserts that an object is a valid ExternalScopesObject.
|
34
|
+
*
|
35
|
+
* @param obj - The object to assert.
|
36
|
+
*/
|
37
|
+
export declare function assertIsExternalScopesObject(obj: unknown): asserts obj is ExternalScopesObject;
|
38
|
+
/**
|
39
|
+
* Asserts that a scope string is a valid InternalScopeString.
|
40
|
+
*
|
41
|
+
* @param scopeString - The scope string to assert.
|
42
|
+
*/
|
43
|
+
export declare function assertIsInternalScopeString(scopeString: unknown): asserts scopeString is InternalScopeString;
|
44
|
+
/**
|
45
|
+
* Asserts that an object is a valid InternalScopesObject.
|
46
|
+
*
|
47
|
+
* @param obj - The object to assert.
|
48
|
+
*/
|
49
|
+
export declare function assertIsInternalScopesObject(obj: unknown): asserts obj is InternalScopesObject;
|
50
|
+
//# sourceMappingURL=assert.d.cts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"assert.d.cts","sourceRoot":"","sources":["../../src/scope/assert.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,WAAW,EAOhB,KAAK,GAAG,EACT,wBAAwB;AAQzB,OAAO,KAAK,EAEV,oBAAoB,EAGpB,oBAAoB,EACpB,mBAAmB,EACnB,qBAAqB,EACrB,sBAAsB,EACvB,oBAAgB;AAEjB;;;;;;;;;GASG;AACH,eAAO,MAAM,oBAAoB,gBAClB,MAAM,eACN,qBAAqB;qCAMC,GAAG,KAAK,OAAO;oCAChB,WAAW,KAAK,OAAO;uCACpB,WAAW,KAAK,MAAM,EAAE;UA6B9D,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,qBAAqB,WACxB,sBAAsB;qCAMK,GAAG,KAAK,OAAO;oCAChB,WAAW,KAAK,OAAO;uCACpB,WAAW,KAAK,MAAM,EAAE;UAU9D,CAAC;AA+FF;;;;GAIG;AACH,wBAAgB,4BAA4B,CAC1C,GAAG,EAAE,OAAO,GACX,OAAO,CAAC,GAAG,IAAI,oBAAoB,CASrC;AAyBD;;;;GAIG;AACH,wBAAgB,2BAA2B,CACzC,WAAW,EAAE,OAAO,GACnB,OAAO,CAAC,WAAW,IAAI,mBAAmB,CAU5C;AAED;;;;GAIG;AACH,wBAAgB,4BAA4B,CAC1C,GAAG,EAAE,OAAO,GACX,OAAO,CAAC,GAAG,IAAI,oBAAoB,CASrC"}
|
@@ -0,0 +1,50 @@
|
|
1
|
+
import { type CaipChainId, type Hex } from "@metamask/utils";
|
2
|
+
import type { ExternalScopesObject, InternalScopesObject, InternalScopeString, NormalizedScopeObject, NormalizedScopesObject } from "./types.mjs";
|
3
|
+
/**
|
4
|
+
* Asserts that a scope string and its associated scope object are supported.
|
5
|
+
*
|
6
|
+
* @param scopeString - The scope string against which to assert support.
|
7
|
+
* @param scopeObject - The scope object against which to assert support.
|
8
|
+
* @param hooks - An object containing the following properties:
|
9
|
+
* @param hooks.isEvmChainIdSupported - A predicate that determines if an EVM chainID is supported.
|
10
|
+
* @param hooks.isNonEvmScopeSupported - A predicate that determines if an non EVM scopeString is supported.
|
11
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
12
|
+
*/
|
13
|
+
export declare const assertScopeSupported: (scopeString: string, scopeObject: NormalizedScopeObject, { isEvmChainIdSupported, isNonEvmScopeSupported, getNonEvmSupportedMethods, }: {
|
14
|
+
isEvmChainIdSupported: (chainId: Hex) => boolean;
|
15
|
+
isNonEvmScopeSupported: (scope: CaipChainId) => boolean;
|
16
|
+
getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
|
17
|
+
}) => void;
|
18
|
+
/**
|
19
|
+
* Asserts that all scope strings and their associated scope objects are supported.
|
20
|
+
*
|
21
|
+
* @param scopes - The scopes object against which to assert support.
|
22
|
+
* @param hooks - An object containing the following properties:
|
23
|
+
* @param hooks.isEvmChainIdSupported - A predicate that determines if an EVM chainID is supported.
|
24
|
+
* @param hooks.isNonEvmScopeSupported - A predicate that determines if an non EVM scopeString is supported.
|
25
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
26
|
+
*/
|
27
|
+
export declare const assertScopesSupported: (scopes: NormalizedScopesObject, { isEvmChainIdSupported, isNonEvmScopeSupported, getNonEvmSupportedMethods, }: {
|
28
|
+
isEvmChainIdSupported: (chainId: Hex) => boolean;
|
29
|
+
isNonEvmScopeSupported: (scope: CaipChainId) => boolean;
|
30
|
+
getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
|
31
|
+
}) => void;
|
32
|
+
/**
|
33
|
+
* Asserts that an object is a valid ExternalScopesObject.
|
34
|
+
*
|
35
|
+
* @param obj - The object to assert.
|
36
|
+
*/
|
37
|
+
export declare function assertIsExternalScopesObject(obj: unknown): asserts obj is ExternalScopesObject;
|
38
|
+
/**
|
39
|
+
* Asserts that a scope string is a valid InternalScopeString.
|
40
|
+
*
|
41
|
+
* @param scopeString - The scope string to assert.
|
42
|
+
*/
|
43
|
+
export declare function assertIsInternalScopeString(scopeString: unknown): asserts scopeString is InternalScopeString;
|
44
|
+
/**
|
45
|
+
* Asserts that an object is a valid InternalScopesObject.
|
46
|
+
*
|
47
|
+
* @param obj - The object to assert.
|
48
|
+
*/
|
49
|
+
export declare function assertIsInternalScopesObject(obj: unknown): asserts obj is InternalScopesObject;
|
50
|
+
//# sourceMappingURL=assert.d.mts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"assert.d.mts","sourceRoot":"","sources":["../../src/scope/assert.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,WAAW,EAOhB,KAAK,GAAG,EACT,wBAAwB;AAQzB,OAAO,KAAK,EAEV,oBAAoB,EAGpB,oBAAoB,EACpB,mBAAmB,EACnB,qBAAqB,EACrB,sBAAsB,EACvB,oBAAgB;AAEjB;;;;;;;;;GASG;AACH,eAAO,MAAM,oBAAoB,gBAClB,MAAM,eACN,qBAAqB;qCAMC,GAAG,KAAK,OAAO;oCAChB,WAAW,KAAK,OAAO;uCACpB,WAAW,KAAK,MAAM,EAAE;UA6B9D,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,qBAAqB,WACxB,sBAAsB;qCAMK,GAAG,KAAK,OAAO;oCAChB,WAAW,KAAK,OAAO;uCACpB,WAAW,KAAK,MAAM,EAAE;UAU9D,CAAC;AA+FF;;;;GAIG;AACH,wBAAgB,4BAA4B,CAC1C,GAAG,EAAE,OAAO,GACX,OAAO,CAAC,GAAG,IAAI,oBAAoB,CASrC;AAyBD;;;;GAIG;AACH,wBAAgB,2BAA2B,CACzC,WAAW,EAAE,OAAO,GACnB,OAAO,CAAC,WAAW,IAAI,mBAAmB,CAU5C;AAED;;;;GAIG;AACH,wBAAgB,4BAA4B,CAC1C,GAAG,EAAE,OAAO,GACX,OAAO,CAAC,GAAG,IAAI,oBAAoB,CASrC"}
|
@@ -0,0 +1,162 @@
|
|
1
|
+
import { hasProperty, isCaipAccountId, isCaipChainId, isCaipNamespace, isCaipReference, KnownCaipNamespace } from "@metamask/utils";
|
2
|
+
import { Caip25Errors } from "./errors.mjs";
|
3
|
+
import { isSupportedMethod, isSupportedNotification, isSupportedScopeString } from "./supported.mjs";
|
4
|
+
/**
|
5
|
+
* Asserts that a scope string and its associated scope object are supported.
|
6
|
+
*
|
7
|
+
* @param scopeString - The scope string against which to assert support.
|
8
|
+
* @param scopeObject - The scope object against which to assert support.
|
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
|
+
*/
|
14
|
+
export const assertScopeSupported = (scopeString, scopeObject, { isEvmChainIdSupported, isNonEvmScopeSupported, getNonEvmSupportedMethods, }) => {
|
15
|
+
const { methods, notifications } = scopeObject;
|
16
|
+
if (!isSupportedScopeString(scopeString, {
|
17
|
+
isEvmChainIdSupported,
|
18
|
+
isNonEvmScopeSupported,
|
19
|
+
})) {
|
20
|
+
throw Caip25Errors.requestedChainsNotSupportedError();
|
21
|
+
}
|
22
|
+
const allMethodsSupported = methods.every((method) => isSupportedMethod(scopeString, method, { getNonEvmSupportedMethods }));
|
23
|
+
if (!allMethodsSupported) {
|
24
|
+
throw Caip25Errors.requestedMethodsNotSupportedError();
|
25
|
+
}
|
26
|
+
if (notifications &&
|
27
|
+
!notifications.every((notification) => isSupportedNotification(scopeString, notification))) {
|
28
|
+
throw Caip25Errors.requestedNotificationsNotSupportedError();
|
29
|
+
}
|
30
|
+
};
|
31
|
+
/**
|
32
|
+
* Asserts that all scope strings and their associated scope objects are supported.
|
33
|
+
*
|
34
|
+
* @param scopes - The scopes object against which to assert support.
|
35
|
+
* @param hooks - An object containing the following properties:
|
36
|
+
* @param hooks.isEvmChainIdSupported - A predicate that determines if an EVM chainID is supported.
|
37
|
+
* @param hooks.isNonEvmScopeSupported - A predicate that determines if an non EVM scopeString is supported.
|
38
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
39
|
+
*/
|
40
|
+
export const assertScopesSupported = (scopes, { isEvmChainIdSupported, isNonEvmScopeSupported, getNonEvmSupportedMethods, }) => {
|
41
|
+
for (const [scopeString, scopeObject] of Object.entries(scopes)) {
|
42
|
+
assertScopeSupported(scopeString, scopeObject, {
|
43
|
+
isEvmChainIdSupported,
|
44
|
+
isNonEvmScopeSupported,
|
45
|
+
getNonEvmSupportedMethods,
|
46
|
+
});
|
47
|
+
}
|
48
|
+
};
|
49
|
+
/**
|
50
|
+
* Asserts that an object is a valid ExternalScopeObject.
|
51
|
+
*
|
52
|
+
* @param obj - The object to assert.
|
53
|
+
*/
|
54
|
+
function assertIsExternalScopeObject(obj) {
|
55
|
+
if (typeof obj !== 'object' || obj === null) {
|
56
|
+
throw new Error('ExternalScopeObject must be an object');
|
57
|
+
}
|
58
|
+
if (hasProperty(obj, 'references')) {
|
59
|
+
if (!Array.isArray(obj.references) ||
|
60
|
+
!obj.references.every(isCaipReference)) {
|
61
|
+
throw new Error('ExternalScopeObject.references must be an array of CaipReference');
|
62
|
+
}
|
63
|
+
}
|
64
|
+
if (hasProperty(obj, 'accounts')) {
|
65
|
+
if (!Array.isArray(obj.accounts) || !obj.accounts.every(isCaipAccountId)) {
|
66
|
+
throw new Error('ExternalScopeObject.accounts must be an array of CaipAccountId');
|
67
|
+
}
|
68
|
+
}
|
69
|
+
if (hasProperty(obj, 'methods')) {
|
70
|
+
if (!Array.isArray(obj.methods) ||
|
71
|
+
!obj.methods.every((method) => typeof method === 'string')) {
|
72
|
+
throw new Error('ExternalScopeObject.methods must be an array of strings');
|
73
|
+
}
|
74
|
+
}
|
75
|
+
if (hasProperty(obj, 'notifications')) {
|
76
|
+
if (!Array.isArray(obj.notifications) ||
|
77
|
+
!obj.notifications.every((notification) => typeof notification === 'string')) {
|
78
|
+
throw new Error('ExternalScopeObject.notifications must be an array of strings');
|
79
|
+
}
|
80
|
+
}
|
81
|
+
if (hasProperty(obj, 'rpcDocuments')) {
|
82
|
+
if (!Array.isArray(obj.rpcDocuments) ||
|
83
|
+
!obj.rpcDocuments.every((doc) => typeof doc === 'string')) {
|
84
|
+
throw new Error('ExternalScopeObject.rpcDocuments must be an array of strings');
|
85
|
+
}
|
86
|
+
}
|
87
|
+
if (hasProperty(obj, 'rpcEndpoints')) {
|
88
|
+
if (!Array.isArray(obj.rpcEndpoints) ||
|
89
|
+
!obj.rpcEndpoints.every((endpoint) => typeof endpoint === 'string')) {
|
90
|
+
throw new Error('ExternalScopeObject.rpcEndpoints must be an array of strings');
|
91
|
+
}
|
92
|
+
}
|
93
|
+
}
|
94
|
+
/**
|
95
|
+
* Asserts that a scope string is a valid ExternalScopeString.
|
96
|
+
*
|
97
|
+
* @param scopeString - The scope string to assert.
|
98
|
+
*/
|
99
|
+
function assertIsExternalScopeString(scopeString) {
|
100
|
+
if (typeof scopeString !== 'string' ||
|
101
|
+
(!isCaipNamespace(scopeString) && !isCaipChainId(scopeString))) {
|
102
|
+
throw new Error('scopeString is not a valid ExternalScopeString');
|
103
|
+
}
|
104
|
+
}
|
105
|
+
/**
|
106
|
+
* Asserts that an object is a valid ExternalScopesObject.
|
107
|
+
*
|
108
|
+
* @param obj - The object to assert.
|
109
|
+
*/
|
110
|
+
export function assertIsExternalScopesObject(obj) {
|
111
|
+
if (typeof obj !== 'object' || obj === null) {
|
112
|
+
throw new Error('ExternalScopesObject must be an object');
|
113
|
+
}
|
114
|
+
for (const [scopeString, scopeObject] of Object.entries(obj)) {
|
115
|
+
assertIsExternalScopeString(scopeString);
|
116
|
+
assertIsExternalScopeObject(scopeObject);
|
117
|
+
}
|
118
|
+
}
|
119
|
+
/**
|
120
|
+
* Asserts that an object is a valid InternalScopeObject.
|
121
|
+
*
|
122
|
+
* @param obj - The object to assert.
|
123
|
+
*/
|
124
|
+
function assertIsInternalScopeObject(obj) {
|
125
|
+
if (typeof obj !== 'object' || obj === null) {
|
126
|
+
throw new Error('InternalScopeObject must be an object');
|
127
|
+
}
|
128
|
+
if (!hasProperty(obj, 'accounts') ||
|
129
|
+
!Array.isArray(obj.accounts) ||
|
130
|
+
!obj.accounts.every(isCaipAccountId)) {
|
131
|
+
throw new Error('InternalScopeObject.accounts must be an array of CaipAccountId');
|
132
|
+
}
|
133
|
+
}
|
134
|
+
/**
|
135
|
+
* Asserts that a scope string is a valid InternalScopeString.
|
136
|
+
*
|
137
|
+
* @param scopeString - The scope string to assert.
|
138
|
+
*/
|
139
|
+
export function assertIsInternalScopeString(scopeString) {
|
140
|
+
if (typeof scopeString !== 'string' ||
|
141
|
+
// `InternalScopeString` is defined as either `KnownCaipNamespace.Wallet` or
|
142
|
+
// `CaipChainId`, so our conditions intentionally match the type.
|
143
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison
|
144
|
+
(scopeString !== KnownCaipNamespace.Wallet && !isCaipChainId(scopeString))) {
|
145
|
+
throw new Error('scopeString is not a valid InternalScopeString');
|
146
|
+
}
|
147
|
+
}
|
148
|
+
/**
|
149
|
+
* Asserts that an object is a valid InternalScopesObject.
|
150
|
+
*
|
151
|
+
* @param obj - The object to assert.
|
152
|
+
*/
|
153
|
+
export function assertIsInternalScopesObject(obj) {
|
154
|
+
if (typeof obj !== 'object' || obj === null) {
|
155
|
+
throw new Error('InternalScopesObject must be an object');
|
156
|
+
}
|
157
|
+
for (const [scopeString, scopeObject] of Object.entries(obj)) {
|
158
|
+
assertIsInternalScopeString(scopeString);
|
159
|
+
assertIsInternalScopeObject(scopeObject);
|
160
|
+
}
|
161
|
+
}
|
162
|
+
//# sourceMappingURL=assert.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"assert.mjs","sourceRoot":"","sources":["../../src/scope/assert.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,WAAW,EACX,eAAe,EACf,aAAa,EACb,eAAe,EACf,eAAe,EACf,kBAA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\n type Hex,\n} from '@metamask/utils';\n\nimport { Caip25Errors } from './errors';\nimport {\n isSupportedMethod,\n isSupportedNotification,\n isSupportedScopeString,\n} from './supported';\nimport type {\n ExternalScopeObject,\n ExternalScopesObject,\n ExternalScopeString,\n InternalScopeObject,\n InternalScopesObject,\n InternalScopeString,\n NormalizedScopeObject,\n NormalizedScopesObject,\n} from './types';\n\n/**\n * Asserts that a scope string and its associated scope object are supported.\n *\n * @param scopeString - The scope string against which to assert support.\n * @param scopeObject - The scope object against which to assert support.\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 */\nexport const assertScopeSupported = (\n scopeString: string,\n scopeObject: NormalizedScopeObject,\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 { methods, notifications } = scopeObject;\n if (\n !isSupportedScopeString(scopeString, {\n isEvmChainIdSupported,\n isNonEvmScopeSupported,\n })\n ) {\n throw Caip25Errors.requestedChainsNotSupportedError();\n }\n\n const allMethodsSupported = methods.every((method) =>\n isSupportedMethod(scopeString, method, { getNonEvmSupportedMethods }),\n );\n\n if (!allMethodsSupported) {\n throw Caip25Errors.requestedMethodsNotSupportedError();\n }\n\n if (\n notifications &&\n !notifications.every((notification) =>\n isSupportedNotification(scopeString, notification),\n )\n ) {\n throw Caip25Errors.requestedNotificationsNotSupportedError();\n }\n};\n\n/**\n * Asserts that all scope strings and their associated scope objects are supported.\n *\n * @param scopes - The scopes object against which to assert support.\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 */\nexport const assertScopesSupported = (\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 for (const [scopeString, scopeObject] of Object.entries(scopes)) {\n assertScopeSupported(scopeString, scopeObject, {\n isEvmChainIdSupported,\n isNonEvmScopeSupported,\n getNonEvmSupportedMethods,\n });\n }\n};\n/**\n * Asserts that an object is a valid ExternalScopeObject.\n *\n * @param obj - The object to assert.\n */\nfunction assertIsExternalScopeObject(\n obj: unknown,\n): asserts obj is ExternalScopeObject {\n if (typeof obj !== 'object' || obj === null) {\n throw new Error('ExternalScopeObject must be an object');\n }\n\n if (hasProperty(obj, 'references')) {\n if (\n !Array.isArray(obj.references) ||\n !obj.references.every(isCaipReference)\n ) {\n throw new Error(\n 'ExternalScopeObject.references must be an array of CaipReference',\n );\n }\n }\n\n if (hasProperty(obj, 'accounts')) {\n if (!Array.isArray(obj.accounts) || !obj.accounts.every(isCaipAccountId)) {\n throw new Error(\n 'ExternalScopeObject.accounts must be an array of CaipAccountId',\n );\n }\n }\n\n if (hasProperty(obj, 'methods')) {\n if (\n !Array.isArray(obj.methods) ||\n !obj.methods.every((method) => typeof method === 'string')\n ) {\n throw new Error(\n 'ExternalScopeObject.methods must be an array of strings',\n );\n }\n }\n\n if (hasProperty(obj, 'notifications')) {\n if (\n !Array.isArray(obj.notifications) ||\n !obj.notifications.every(\n (notification) => typeof notification === 'string',\n )\n ) {\n throw new Error(\n 'ExternalScopeObject.notifications must be an array of strings',\n );\n }\n }\n\n if (hasProperty(obj, 'rpcDocuments')) {\n if (\n !Array.isArray(obj.rpcDocuments) ||\n !obj.rpcDocuments.every((doc) => typeof doc === 'string')\n ) {\n throw new Error(\n 'ExternalScopeObject.rpcDocuments must be an array of strings',\n );\n }\n }\n\n if (hasProperty(obj, 'rpcEndpoints')) {\n if (\n !Array.isArray(obj.rpcEndpoints) ||\n !obj.rpcEndpoints.every((endpoint) => typeof endpoint === 'string')\n ) {\n throw new Error(\n 'ExternalScopeObject.rpcEndpoints must be an array of strings',\n );\n }\n }\n}\n\n/**\n * Asserts that a scope string is a valid ExternalScopeString.\n *\n * @param scopeString - The scope string to assert.\n */\nfunction assertIsExternalScopeString(\n scopeString: unknown,\n): asserts scopeString is ExternalScopeString {\n if (\n typeof scopeString !== 'string' ||\n (!isCaipNamespace(scopeString) && !isCaipChainId(scopeString))\n ) {\n throw new Error('scopeString is not a valid ExternalScopeString');\n }\n}\n\n/**\n * Asserts that an object is a valid ExternalScopesObject.\n *\n * @param obj - The object to assert.\n */\nexport function assertIsExternalScopesObject(\n obj: unknown,\n): asserts obj is ExternalScopesObject {\n if (typeof obj !== 'object' || obj === null) {\n throw new Error('ExternalScopesObject must be an object');\n }\n\n for (const [scopeString, scopeObject] of Object.entries(obj)) {\n assertIsExternalScopeString(scopeString);\n assertIsExternalScopeObject(scopeObject);\n }\n}\n\n/**\n * Asserts that an object is a valid InternalScopeObject.\n *\n * @param obj - The object to assert.\n */\nfunction assertIsInternalScopeObject(\n obj: unknown,\n): asserts obj is InternalScopeObject {\n if (typeof obj !== 'object' || obj === null) {\n throw new Error('InternalScopeObject must be an object');\n }\n\n if (\n !hasProperty(obj, 'accounts') ||\n !Array.isArray(obj.accounts) ||\n !obj.accounts.every(isCaipAccountId)\n ) {\n throw new Error(\n 'InternalScopeObject.accounts must be an array of CaipAccountId',\n );\n }\n}\n\n/**\n * Asserts that a scope string is a valid InternalScopeString.\n *\n * @param scopeString - The scope string to assert.\n */\nexport function assertIsInternalScopeString(\n scopeString: unknown,\n): asserts scopeString is InternalScopeString {\n if (\n typeof scopeString !== 'string' ||\n // `InternalScopeString` is defined as either `KnownCaipNamespace.Wallet` or\n // `CaipChainId`, so our conditions intentionally match the type.\n // eslint-disable-next-line @typescript-eslint/no-unsafe-enum-comparison\n (scopeString !== KnownCaipNamespace.Wallet && !isCaipChainId(scopeString))\n ) {\n throw new Error('scopeString is not a valid InternalScopeString');\n }\n}\n\n/**\n * Asserts that an object is a valid InternalScopesObject.\n *\n * @param obj - The object to assert.\n */\nexport function assertIsInternalScopesObject(\n obj: unknown,\n): asserts obj is InternalScopesObject {\n if (typeof obj !== 'object' || obj === null) {\n throw new Error('InternalScopesObject must be an object');\n }\n\n for (const [scopeString, scopeObject] of Object.entries(obj)) {\n assertIsInternalScopeString(scopeString);\n assertIsInternalScopeObject(scopeObject);\n }\n}\n"]}
|
@@ -0,0 +1,51 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.bucketScopes = exports.validateAndNormalizeScopes = void 0;
|
4
|
+
const filter_1 = require("./filter.cjs");
|
5
|
+
const transform_1 = require("./transform.cjs");
|
6
|
+
const validation_1 = require("./validation.cjs");
|
7
|
+
/**
|
8
|
+
* Validates and normalizes a set of scopes according to the [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) spec.
|
9
|
+
*
|
10
|
+
* @param requiredScopes - The required scopes to validate and normalize.
|
11
|
+
* @param optionalScopes - The optional scopes to validate and normalize.
|
12
|
+
* @returns An object containing the normalized required scopes and normalized optional scopes.
|
13
|
+
*/
|
14
|
+
const validateAndNormalizeScopes = (requiredScopes, optionalScopes) => {
|
15
|
+
const { validRequiredScopes, validOptionalScopes } = (0, validation_1.getValidScopes)(requiredScopes, optionalScopes);
|
16
|
+
const normalizedRequiredScopes = (0, transform_1.normalizeAndMergeScopes)(validRequiredScopes);
|
17
|
+
const normalizedOptionalScopes = (0, transform_1.normalizeAndMergeScopes)(validOptionalScopes);
|
18
|
+
return {
|
19
|
+
normalizedRequiredScopes,
|
20
|
+
normalizedOptionalScopes,
|
21
|
+
};
|
22
|
+
};
|
23
|
+
exports.validateAndNormalizeScopes = validateAndNormalizeScopes;
|
24
|
+
/**
|
25
|
+
* Groups a NormalizedScopesObject into three separate
|
26
|
+
* NormalizedScopesObjects for supported scopes,
|
27
|
+
* supportable scopes, and unsupportable scopes.
|
28
|
+
*
|
29
|
+
* @param scopes - The NormalizedScopesObject to group.
|
30
|
+
* @param hooks - The hooks.
|
31
|
+
* @param hooks.isEvmChainIdSupported - A helper that returns true if an eth chainId is currently supported by the wallet.
|
32
|
+
* @param hooks.isEvmChainIdSupportable - A helper that returns true if an eth chainId could be supported by the wallet.
|
33
|
+
* @param hooks.isNonEvmScopeSupported - A predicate that determines if an non EVM scopeString is supported.
|
34
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
35
|
+
* @returns an object with three NormalizedScopesObjects separated by support.
|
36
|
+
*/
|
37
|
+
const bucketScopes = (scopes, { isEvmChainIdSupported, isEvmChainIdSupportable, isNonEvmScopeSupported, getNonEvmSupportedMethods, }) => {
|
38
|
+
const { supportedScopes, unsupportedScopes: maybeSupportableScopes } = (0, filter_1.bucketScopesBySupport)(scopes, {
|
39
|
+
isEvmChainIdSupported,
|
40
|
+
isNonEvmScopeSupported,
|
41
|
+
getNonEvmSupportedMethods,
|
42
|
+
});
|
43
|
+
const { supportedScopes: supportableScopes, unsupportedScopes: unsupportableScopes, } = (0, filter_1.bucketScopesBySupport)(maybeSupportableScopes, {
|
44
|
+
isEvmChainIdSupported: isEvmChainIdSupportable,
|
45
|
+
isNonEvmScopeSupported,
|
46
|
+
getNonEvmSupportedMethods,
|
47
|
+
});
|
48
|
+
return { supportedScopes, supportableScopes, unsupportableScopes };
|
49
|
+
};
|
50
|
+
exports.bucketScopes = bucketScopes;
|
51
|
+
//# sourceMappingURL=authorization.cjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"authorization.cjs","sourceRoot":"","sources":["../../src/scope/authorization.ts"],"names":[],"mappings":";;;AAEA,yCAAiD;AACjD,+CAAsD;AAMtD,iDAA8C;AAmB9C;;;;;;GAMG;AACI,MAAM,0BAA0B,GAAG,CACxC,cAAoC,EACpC,cAAoC,EAIpC,EAAE;IACF,MAAM,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,GAAG,IAAA,2BAAc,EACjE,cAAc,EACd,cAAc,CACf,CAAC;IAEF,MAAM,wBAAwB,GAAG,IAAA,mCAAuB,EAAC,mBAAmB,CAAC,CAAC;IAC9E,MAAM,wBAAwB,GAAG,IAAA,mCAAuB,EAAC,mBAAmB,CAAC,CAAC;IAE9E,OAAO;QACL,wBAAwB;QACxB,wBAAwB;KACzB,CAAC;AACJ,CAAC,CAAC;AAnBW,QAAA,0BAA0B,8BAmBrC;AAEF;;;;;;;;;;;;GAYG;AACI,MAAM,YAAY,GAAG,CAC1B,MAA8B,EAC9B,EACE,qBAAqB,EACrB,uBAAuB,EACvB,sBAAsB,EACtB,yBAAyB,GAM1B,EAKD,EAAE;IACF,MAAM,EAAE,eAAe,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,GAClE,IAAA,8BAAqB,EAAC,MAAM,EAAE;QAC5B,qBAAqB;QACrB,sBAAsB;QACtB,yBAAyB;KAC1B,CAAC,CAAC;IAEL,MAAM,EACJ,eAAe,EAAE,iBAAiB,EAClC,iBAAiB,EAAE,mBAAmB,GACvC,GAAG,IAAA,8BAAqB,EAAC,sBAAsB,EAAE;QAChD,qBAAqB,EAAE,uBAAuB;QAC9C,sBAAsB;QACtB,yBAAyB;KAC1B,CAAC,CAAC;IAEH,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,CAAC;AACrE,CAAC,CAAC;AAnCW,QAAA,YAAY,gBAmCvB","sourcesContent":["import type { CaipChainId, Hex, Json } from '@metamask/utils';\n\nimport { bucketScopesBySupport } from './filter';\nimport { normalizeAndMergeScopes } from './transform';\nimport type {\n ExternalScopesObject,\n ExternalScopeString,\n NormalizedScopesObject,\n} from './types';\nimport { getValidScopes } from './validation';\n\n/**\n * Represents the parameters of a [CAIP-25](https://chainagnostic.org/CAIPs/caip-25) request.\n */\nexport type Caip25Authorization = (\n | {\n requiredScopes: ExternalScopesObject;\n optionalScopes?: ExternalScopesObject;\n }\n | {\n requiredScopes?: ExternalScopesObject;\n optionalScopes: ExternalScopesObject;\n }\n) & {\n sessionProperties?: Record<string, Json>;\n scopedProperties?: Record<ExternalScopeString, Json>;\n};\n\n/**\n * Validates and normalizes a set of scopes according to the [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) spec.\n *\n * @param requiredScopes - The required scopes to validate and normalize.\n * @param optionalScopes - The optional scopes to validate and normalize.\n * @returns An object containing the normalized required scopes and normalized optional scopes.\n */\nexport const validateAndNormalizeScopes = (\n requiredScopes: ExternalScopesObject,\n optionalScopes: ExternalScopesObject,\n): {\n normalizedRequiredScopes: NormalizedScopesObject;\n normalizedOptionalScopes: NormalizedScopesObject;\n} => {\n const { validRequiredScopes, validOptionalScopes } = getValidScopes(\n requiredScopes,\n optionalScopes,\n );\n\n const normalizedRequiredScopes = normalizeAndMergeScopes(validRequiredScopes);\n const normalizedOptionalScopes = normalizeAndMergeScopes(validOptionalScopes);\n\n return {\n normalizedRequiredScopes,\n normalizedOptionalScopes,\n };\n};\n\n/**\n * Groups a NormalizedScopesObject into three separate\n * NormalizedScopesObjects for supported scopes,\n * supportable scopes, and unsupportable scopes.\n *\n * @param scopes - The NormalizedScopesObject to group.\n * @param hooks - The hooks.\n * @param hooks.isEvmChainIdSupported - A helper that returns true if an eth chainId is currently supported by the wallet.\n * @param hooks.isEvmChainIdSupportable - A helper that returns true if an eth chainId could be supported by the wallet.\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 an object with three NormalizedScopesObjects separated by support.\n */\nexport const bucketScopes = (\n scopes: NormalizedScopesObject,\n {\n isEvmChainIdSupported,\n isEvmChainIdSupportable,\n isNonEvmScopeSupported,\n getNonEvmSupportedMethods,\n }: {\n isEvmChainIdSupported: (chainId: Hex) => boolean;\n isEvmChainIdSupportable: (chainId: Hex) => boolean;\n isNonEvmScopeSupported: (scope: CaipChainId) => boolean;\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n },\n): {\n supportedScopes: NormalizedScopesObject;\n supportableScopes: NormalizedScopesObject;\n unsupportableScopes: NormalizedScopesObject;\n} => {\n const { supportedScopes, unsupportedScopes: maybeSupportableScopes } =\n bucketScopesBySupport(scopes, {\n isEvmChainIdSupported,\n isNonEvmScopeSupported,\n getNonEvmSupportedMethods,\n });\n\n const {\n supportedScopes: supportableScopes,\n unsupportedScopes: unsupportableScopes,\n } = bucketScopesBySupport(maybeSupportableScopes, {\n isEvmChainIdSupported: isEvmChainIdSupportable,\n isNonEvmScopeSupported,\n getNonEvmSupportedMethods,\n });\n\n return { supportedScopes, supportableScopes, unsupportableScopes };\n};\n"]}
|
@@ -0,0 +1,50 @@
|
|
1
|
+
import type { CaipChainId, Hex, Json } from "@metamask/utils";
|
2
|
+
import type { ExternalScopesObject, ExternalScopeString, NormalizedScopesObject } from "./types.cjs";
|
3
|
+
/**
|
4
|
+
* Represents the parameters of a [CAIP-25](https://chainagnostic.org/CAIPs/caip-25) request.
|
5
|
+
*/
|
6
|
+
export type Caip25Authorization = ({
|
7
|
+
requiredScopes: ExternalScopesObject;
|
8
|
+
optionalScopes?: ExternalScopesObject;
|
9
|
+
} | {
|
10
|
+
requiredScopes?: ExternalScopesObject;
|
11
|
+
optionalScopes: ExternalScopesObject;
|
12
|
+
}) & {
|
13
|
+
sessionProperties?: Record<string, Json>;
|
14
|
+
scopedProperties?: Record<ExternalScopeString, Json>;
|
15
|
+
};
|
16
|
+
/**
|
17
|
+
* Validates and normalizes a set of scopes according to the [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) spec.
|
18
|
+
*
|
19
|
+
* @param requiredScopes - The required scopes to validate and normalize.
|
20
|
+
* @param optionalScopes - The optional scopes to validate and normalize.
|
21
|
+
* @returns An object containing the normalized required scopes and normalized optional scopes.
|
22
|
+
*/
|
23
|
+
export declare const validateAndNormalizeScopes: (requiredScopes: ExternalScopesObject, optionalScopes: ExternalScopesObject) => {
|
24
|
+
normalizedRequiredScopes: NormalizedScopesObject;
|
25
|
+
normalizedOptionalScopes: NormalizedScopesObject;
|
26
|
+
};
|
27
|
+
/**
|
28
|
+
* Groups a NormalizedScopesObject into three separate
|
29
|
+
* NormalizedScopesObjects for supported scopes,
|
30
|
+
* supportable scopes, and unsupportable scopes.
|
31
|
+
*
|
32
|
+
* @param scopes - The NormalizedScopesObject to group.
|
33
|
+
* @param hooks - The hooks.
|
34
|
+
* @param hooks.isEvmChainIdSupported - A helper that returns true if an eth chainId is currently supported by the wallet.
|
35
|
+
* @param hooks.isEvmChainIdSupportable - A helper that returns true if an eth chainId could be supported by the wallet.
|
36
|
+
* @param hooks.isNonEvmScopeSupported - A predicate that determines if an non EVM scopeString is supported.
|
37
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
38
|
+
* @returns an object with three NormalizedScopesObjects separated by support.
|
39
|
+
*/
|
40
|
+
export declare const bucketScopes: (scopes: NormalizedScopesObject, { isEvmChainIdSupported, isEvmChainIdSupportable, isNonEvmScopeSupported, getNonEvmSupportedMethods, }: {
|
41
|
+
isEvmChainIdSupported: (chainId: Hex) => boolean;
|
42
|
+
isEvmChainIdSupportable: (chainId: Hex) => boolean;
|
43
|
+
isNonEvmScopeSupported: (scope: CaipChainId) => boolean;
|
44
|
+
getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
|
45
|
+
}) => {
|
46
|
+
supportedScopes: NormalizedScopesObject;
|
47
|
+
supportableScopes: NormalizedScopesObject;
|
48
|
+
unsupportableScopes: NormalizedScopesObject;
|
49
|
+
};
|
50
|
+
//# sourceMappingURL=authorization.d.cts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"authorization.d.cts","sourceRoot":"","sources":["../../src/scope/authorization.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,GAAG,EAAE,IAAI,EAAE,wBAAwB;AAI9D,OAAO,KAAK,EACV,oBAAoB,EACpB,mBAAmB,EACnB,sBAAsB,EACvB,oBAAgB;AAGjB;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,CAC9B;IACE,cAAc,EAAE,oBAAoB,CAAC;IACrC,cAAc,CAAC,EAAE,oBAAoB,CAAC;CACvC,GACD;IACE,cAAc,CAAC,EAAE,oBAAoB,CAAC;IACtC,cAAc,EAAE,oBAAoB,CAAC;CACtC,CACJ,GAAG;IACF,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACzC,gBAAgB,CAAC,EAAE,MAAM,CAAC,mBAAmB,EAAE,IAAI,CAAC,CAAC;CACtD,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,0BAA0B,mBACrB,oBAAoB,kBACpB,oBAAoB;8BAEV,sBAAsB;8BACtB,sBAAsB;CAcjD,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,YAAY,WACf,sBAAsB;qCAOK,GAAG,KAAK,OAAO;uCACb,GAAG,KAAK,OAAO;oCAClB,WAAW,KAAK,OAAO;uCACpB,WAAW,KAAK,MAAM,EAAE;;qBAG5C,sBAAsB;uBACpB,sBAAsB;yBACpB,sBAAsB;CAmB5C,CAAC"}
|
@@ -0,0 +1,50 @@
|
|
1
|
+
import type { CaipChainId, Hex, Json } from "@metamask/utils";
|
2
|
+
import type { ExternalScopesObject, ExternalScopeString, NormalizedScopesObject } from "./types.mjs";
|
3
|
+
/**
|
4
|
+
* Represents the parameters of a [CAIP-25](https://chainagnostic.org/CAIPs/caip-25) request.
|
5
|
+
*/
|
6
|
+
export type Caip25Authorization = ({
|
7
|
+
requiredScopes: ExternalScopesObject;
|
8
|
+
optionalScopes?: ExternalScopesObject;
|
9
|
+
} | {
|
10
|
+
requiredScopes?: ExternalScopesObject;
|
11
|
+
optionalScopes: ExternalScopesObject;
|
12
|
+
}) & {
|
13
|
+
sessionProperties?: Record<string, Json>;
|
14
|
+
scopedProperties?: Record<ExternalScopeString, Json>;
|
15
|
+
};
|
16
|
+
/**
|
17
|
+
* Validates and normalizes a set of scopes according to the [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) spec.
|
18
|
+
*
|
19
|
+
* @param requiredScopes - The required scopes to validate and normalize.
|
20
|
+
* @param optionalScopes - The optional scopes to validate and normalize.
|
21
|
+
* @returns An object containing the normalized required scopes and normalized optional scopes.
|
22
|
+
*/
|
23
|
+
export declare const validateAndNormalizeScopes: (requiredScopes: ExternalScopesObject, optionalScopes: ExternalScopesObject) => {
|
24
|
+
normalizedRequiredScopes: NormalizedScopesObject;
|
25
|
+
normalizedOptionalScopes: NormalizedScopesObject;
|
26
|
+
};
|
27
|
+
/**
|
28
|
+
* Groups a NormalizedScopesObject into three separate
|
29
|
+
* NormalizedScopesObjects for supported scopes,
|
30
|
+
* supportable scopes, and unsupportable scopes.
|
31
|
+
*
|
32
|
+
* @param scopes - The NormalizedScopesObject to group.
|
33
|
+
* @param hooks - The hooks.
|
34
|
+
* @param hooks.isEvmChainIdSupported - A helper that returns true if an eth chainId is currently supported by the wallet.
|
35
|
+
* @param hooks.isEvmChainIdSupportable - A helper that returns true if an eth chainId could be supported by the wallet.
|
36
|
+
* @param hooks.isNonEvmScopeSupported - A predicate that determines if an non EVM scopeString is supported.
|
37
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
38
|
+
* @returns an object with three NormalizedScopesObjects separated by support.
|
39
|
+
*/
|
40
|
+
export declare const bucketScopes: (scopes: NormalizedScopesObject, { isEvmChainIdSupported, isEvmChainIdSupportable, isNonEvmScopeSupported, getNonEvmSupportedMethods, }: {
|
41
|
+
isEvmChainIdSupported: (chainId: Hex) => boolean;
|
42
|
+
isEvmChainIdSupportable: (chainId: Hex) => boolean;
|
43
|
+
isNonEvmScopeSupported: (scope: CaipChainId) => boolean;
|
44
|
+
getNonEvmSupportedMethods: (scope: CaipChainId) => string[];
|
45
|
+
}) => {
|
46
|
+
supportedScopes: NormalizedScopesObject;
|
47
|
+
supportableScopes: NormalizedScopesObject;
|
48
|
+
unsupportableScopes: NormalizedScopesObject;
|
49
|
+
};
|
50
|
+
//# sourceMappingURL=authorization.d.mts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"authorization.d.mts","sourceRoot":"","sources":["../../src/scope/authorization.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,GAAG,EAAE,IAAI,EAAE,wBAAwB;AAI9D,OAAO,KAAK,EACV,oBAAoB,EACpB,mBAAmB,EACnB,sBAAsB,EACvB,oBAAgB;AAGjB;;GAEG;AACH,MAAM,MAAM,mBAAmB,GAAG,CAC9B;IACE,cAAc,EAAE,oBAAoB,CAAC;IACrC,cAAc,CAAC,EAAE,oBAAoB,CAAC;CACvC,GACD;IACE,cAAc,CAAC,EAAE,oBAAoB,CAAC;IACtC,cAAc,EAAE,oBAAoB,CAAC;CACtC,CACJ,GAAG;IACF,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACzC,gBAAgB,CAAC,EAAE,MAAM,CAAC,mBAAmB,EAAE,IAAI,CAAC,CAAC;CACtD,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,0BAA0B,mBACrB,oBAAoB,kBACpB,oBAAoB;8BAEV,sBAAsB;8BACtB,sBAAsB;CAcjD,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,YAAY,WACf,sBAAsB;qCAOK,GAAG,KAAK,OAAO;uCACb,GAAG,KAAK,OAAO;oCAClB,WAAW,KAAK,OAAO;uCACpB,WAAW,KAAK,MAAM,EAAE;;qBAG5C,sBAAsB;uBACpB,sBAAsB;yBACpB,sBAAsB;CAmB5C,CAAC"}
|
@@ -0,0 +1,46 @@
|
|
1
|
+
import { bucketScopesBySupport } from "./filter.mjs";
|
2
|
+
import { normalizeAndMergeScopes } from "./transform.mjs";
|
3
|
+
import { getValidScopes } from "./validation.mjs";
|
4
|
+
/**
|
5
|
+
* Validates and normalizes a set of scopes according to the [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) spec.
|
6
|
+
*
|
7
|
+
* @param requiredScopes - The required scopes to validate and normalize.
|
8
|
+
* @param optionalScopes - The optional scopes to validate and normalize.
|
9
|
+
* @returns An object containing the normalized required scopes and normalized optional scopes.
|
10
|
+
*/
|
11
|
+
export const validateAndNormalizeScopes = (requiredScopes, optionalScopes) => {
|
12
|
+
const { validRequiredScopes, validOptionalScopes } = getValidScopes(requiredScopes, optionalScopes);
|
13
|
+
const normalizedRequiredScopes = normalizeAndMergeScopes(validRequiredScopes);
|
14
|
+
const normalizedOptionalScopes = normalizeAndMergeScopes(validOptionalScopes);
|
15
|
+
return {
|
16
|
+
normalizedRequiredScopes,
|
17
|
+
normalizedOptionalScopes,
|
18
|
+
};
|
19
|
+
};
|
20
|
+
/**
|
21
|
+
* Groups a NormalizedScopesObject into three separate
|
22
|
+
* NormalizedScopesObjects for supported scopes,
|
23
|
+
* supportable scopes, and unsupportable scopes.
|
24
|
+
*
|
25
|
+
* @param scopes - The NormalizedScopesObject to group.
|
26
|
+
* @param hooks - The hooks.
|
27
|
+
* @param hooks.isEvmChainIdSupported - A helper that returns true if an eth chainId is currently supported by the wallet.
|
28
|
+
* @param hooks.isEvmChainIdSupportable - A helper that returns true if an eth chainId could be supported by the wallet.
|
29
|
+
* @param hooks.isNonEvmScopeSupported - A predicate that determines if an non EVM scopeString is supported.
|
30
|
+
* @param hooks.getNonEvmSupportedMethods - A function that returns the supported methods for a non EVM scope.
|
31
|
+
* @returns an object with three NormalizedScopesObjects separated by support.
|
32
|
+
*/
|
33
|
+
export const bucketScopes = (scopes, { isEvmChainIdSupported, isEvmChainIdSupportable, isNonEvmScopeSupported, getNonEvmSupportedMethods, }) => {
|
34
|
+
const { supportedScopes, unsupportedScopes: maybeSupportableScopes } = bucketScopesBySupport(scopes, {
|
35
|
+
isEvmChainIdSupported,
|
36
|
+
isNonEvmScopeSupported,
|
37
|
+
getNonEvmSupportedMethods,
|
38
|
+
});
|
39
|
+
const { supportedScopes: supportableScopes, unsupportedScopes: unsupportableScopes, } = bucketScopesBySupport(maybeSupportableScopes, {
|
40
|
+
isEvmChainIdSupported: isEvmChainIdSupportable,
|
41
|
+
isNonEvmScopeSupported,
|
42
|
+
getNonEvmSupportedMethods,
|
43
|
+
});
|
44
|
+
return { supportedScopes, supportableScopes, unsupportableScopes };
|
45
|
+
};
|
46
|
+
//# sourceMappingURL=authorization.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"authorization.mjs","sourceRoot":"","sources":["../../src/scope/authorization.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,qBAAiB;AACjD,OAAO,EAAE,uBAAuB,EAAE,wBAAoB;AAMtD,OAAO,EAAE,cAAc,EAAE,yBAAqB;AAmB9C;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,CACxC,cAAoC,EACpC,cAAoC,EAIpC,EAAE;IACF,MAAM,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,GAAG,cAAc,CACjE,cAAc,EACd,cAAc,CACf,CAAC;IAEF,MAAM,wBAAwB,GAAG,uBAAuB,CAAC,mBAAmB,CAAC,CAAC;IAC9E,MAAM,wBAAwB,GAAG,uBAAuB,CAAC,mBAAmB,CAAC,CAAC;IAE9E,OAAO;QACL,wBAAwB;QACxB,wBAAwB;KACzB,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,MAA8B,EAC9B,EACE,qBAAqB,EACrB,uBAAuB,EACvB,sBAAsB,EACtB,yBAAyB,GAM1B,EAKD,EAAE;IACF,MAAM,EAAE,eAAe,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,GAClE,qBAAqB,CAAC,MAAM,EAAE;QAC5B,qBAAqB;QACrB,sBAAsB;QACtB,yBAAyB;KAC1B,CAAC,CAAC;IAEL,MAAM,EACJ,eAAe,EAAE,iBAAiB,EAClC,iBAAiB,EAAE,mBAAmB,GACvC,GAAG,qBAAqB,CAAC,sBAAsB,EAAE;QAChD,qBAAqB,EAAE,uBAAuB;QAC9C,sBAAsB;QACtB,yBAAyB;KAC1B,CAAC,CAAC;IAEH,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,CAAC;AACrE,CAAC,CAAC","sourcesContent":["import type { CaipChainId, Hex, Json } from '@metamask/utils';\n\nimport { bucketScopesBySupport } from './filter';\nimport { normalizeAndMergeScopes } from './transform';\nimport type {\n ExternalScopesObject,\n ExternalScopeString,\n NormalizedScopesObject,\n} from './types';\nimport { getValidScopes } from './validation';\n\n/**\n * Represents the parameters of a [CAIP-25](https://chainagnostic.org/CAIPs/caip-25) request.\n */\nexport type Caip25Authorization = (\n | {\n requiredScopes: ExternalScopesObject;\n optionalScopes?: ExternalScopesObject;\n }\n | {\n requiredScopes?: ExternalScopesObject;\n optionalScopes: ExternalScopesObject;\n }\n) & {\n sessionProperties?: Record<string, Json>;\n scopedProperties?: Record<ExternalScopeString, Json>;\n};\n\n/**\n * Validates and normalizes a set of scopes according to the [CAIP-217](https://chainagnostic.org/CAIPs/caip-217) spec.\n *\n * @param requiredScopes - The required scopes to validate and normalize.\n * @param optionalScopes - The optional scopes to validate and normalize.\n * @returns An object containing the normalized required scopes and normalized optional scopes.\n */\nexport const validateAndNormalizeScopes = (\n requiredScopes: ExternalScopesObject,\n optionalScopes: ExternalScopesObject,\n): {\n normalizedRequiredScopes: NormalizedScopesObject;\n normalizedOptionalScopes: NormalizedScopesObject;\n} => {\n const { validRequiredScopes, validOptionalScopes } = getValidScopes(\n requiredScopes,\n optionalScopes,\n );\n\n const normalizedRequiredScopes = normalizeAndMergeScopes(validRequiredScopes);\n const normalizedOptionalScopes = normalizeAndMergeScopes(validOptionalScopes);\n\n return {\n normalizedRequiredScopes,\n normalizedOptionalScopes,\n };\n};\n\n/**\n * Groups a NormalizedScopesObject into three separate\n * NormalizedScopesObjects for supported scopes,\n * supportable scopes, and unsupportable scopes.\n *\n * @param scopes - The NormalizedScopesObject to group.\n * @param hooks - The hooks.\n * @param hooks.isEvmChainIdSupported - A helper that returns true if an eth chainId is currently supported by the wallet.\n * @param hooks.isEvmChainIdSupportable - A helper that returns true if an eth chainId could be supported by the wallet.\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 an object with three NormalizedScopesObjects separated by support.\n */\nexport const bucketScopes = (\n scopes: NormalizedScopesObject,\n {\n isEvmChainIdSupported,\n isEvmChainIdSupportable,\n isNonEvmScopeSupported,\n getNonEvmSupportedMethods,\n }: {\n isEvmChainIdSupported: (chainId: Hex) => boolean;\n isEvmChainIdSupportable: (chainId: Hex) => boolean;\n isNonEvmScopeSupported: (scope: CaipChainId) => boolean;\n getNonEvmSupportedMethods: (scope: CaipChainId) => string[];\n },\n): {\n supportedScopes: NormalizedScopesObject;\n supportableScopes: NormalizedScopesObject;\n unsupportableScopes: NormalizedScopesObject;\n} => {\n const { supportedScopes, unsupportedScopes: maybeSupportableScopes } =\n bucketScopesBySupport(scopes, {\n isEvmChainIdSupported,\n isNonEvmScopeSupported,\n getNonEvmSupportedMethods,\n });\n\n const {\n supportedScopes: supportableScopes,\n unsupportedScopes: unsupportableScopes,\n } = bucketScopesBySupport(maybeSupportableScopes, {\n isEvmChainIdSupported: isEvmChainIdSupportable,\n isNonEvmScopeSupported,\n getNonEvmSupportedMethods,\n });\n\n return { supportedScopes, supportableScopes, unsupportableScopes };\n};\n"]}
|