@metamask-previews/chain-agnostic-permission 0.7.1-preview-3636a128 → 0.8.0-preview-c6e5eb7
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 +9 -1
- package/dist/caip25Permission.cjs +4 -4
- package/dist/caip25Permission.cjs.map +1 -1
- package/dist/caip25Permission.mjs +2 -2
- package/dist/caip25Permission.mjs.map +1 -1
- package/dist/index.cjs +22 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +3 -3
- package/dist/index.mjs.map +1 -1
- package/dist/{adapters/caip-permission-adapter-accounts.cjs → operators/caip-permission-operator-accounts.cjs} +8 -1
- package/dist/{adapters/caip-permission-adapter-accounts.cjs.map → operators/caip-permission-operator-accounts.cjs.map} +1 -1
- package/dist/{adapters/caip-permission-adapter-accounts.d.cts → operators/caip-permission-operator-accounts.d.cts} +1 -1
- package/dist/operators/caip-permission-operator-accounts.d.cts.map +1 -0
- package/dist/{adapters/caip-permission-adapter-accounts.d.mts → operators/caip-permission-operator-accounts.d.mts} +1 -1
- package/dist/operators/caip-permission-operator-accounts.d.mts.map +1 -0
- package/dist/{adapters/caip-permission-adapter-accounts.mjs → operators/caip-permission-operator-accounts.mjs} +8 -1
- package/dist/operators/caip-permission-operator-accounts.mjs.map +1 -0
- package/dist/{adapters/caip-permission-adapter-permittedChains.cjs → operators/caip-permission-operator-permittedChains.cjs} +1 -1
- package/dist/operators/caip-permission-operator-permittedChains.cjs.map +1 -0
- package/dist/{adapters/caip-permission-adapter-permittedChains.d.cts → operators/caip-permission-operator-permittedChains.d.cts} +1 -1
- package/dist/operators/caip-permission-operator-permittedChains.d.cts.map +1 -0
- package/dist/{adapters/caip-permission-adapter-permittedChains.d.mts → operators/caip-permission-operator-permittedChains.d.mts} +1 -1
- package/dist/operators/caip-permission-operator-permittedChains.d.mts.map +1 -0
- package/dist/{adapters/caip-permission-adapter-permittedChains.mjs → operators/caip-permission-operator-permittedChains.mjs} +1 -1
- package/dist/operators/caip-permission-operator-permittedChains.mjs.map +1 -0
- package/dist/{adapters/caip-permission-adapter-session-scopes.cjs → operators/caip-permission-operator-session-scopes.cjs} +1 -1
- package/dist/operators/caip-permission-operator-session-scopes.cjs.map +1 -0
- package/dist/{adapters/caip-permission-adapter-session-scopes.d.cts → operators/caip-permission-operator-session-scopes.d.cts} +1 -1
- package/dist/operators/caip-permission-operator-session-scopes.d.cts.map +1 -0
- package/dist/{adapters/caip-permission-adapter-session-scopes.d.mts → operators/caip-permission-operator-session-scopes.d.mts} +1 -1
- package/dist/operators/caip-permission-operator-session-scopes.d.mts.map +1 -0
- package/dist/{adapters/caip-permission-adapter-session-scopes.mjs → operators/caip-permission-operator-session-scopes.mjs} +1 -1
- package/dist/operators/caip-permission-operator-session-scopes.mjs.map +1 -0
- package/package.json +2 -2
- package/dist/adapters/caip-permission-adapter-accounts.d.cts.map +0 -1
- package/dist/adapters/caip-permission-adapter-accounts.d.mts.map +0 -1
- package/dist/adapters/caip-permission-adapter-accounts.mjs.map +0 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.cjs.map +0 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.d.cts.map +0 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.d.mts.map +0 -1
- package/dist/adapters/caip-permission-adapter-permittedChains.mjs.map +0 -1
- package/dist/adapters/caip-permission-adapter-session-scopes.cjs.map +0 -1
- package/dist/adapters/caip-permission-adapter-session-scopes.d.cts.map +0 -1
- package/dist/adapters/caip-permission-adapter-session-scopes.d.mts.map +0 -1
- package/dist/adapters/caip-permission-adapter-session-scopes.mjs.map +0 -1
package/CHANGELOG.md
CHANGED
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
7
7
|
|
8
8
|
## [Unreleased]
|
9
9
|
|
10
|
+
## [0.8.0]
|
11
|
+
|
12
|
+
### Changed
|
13
|
+
|
14
|
+
- `isInternalAccountInPermittedAccountIds` now returns `false` when passed an `InternalAccount` in which `scopes` is `undefined` ([#6000](https://github.com/MetaMask/core/pull/6000))
|
15
|
+
- Bump `@metamask/network-controller` to `^24.0.0` ([#5999](https://github.com/MetaMask/core/pull/5999))
|
16
|
+
|
10
17
|
## [0.7.1]
|
11
18
|
|
12
19
|
### Changed
|
@@ -97,7 +104,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
97
104
|
|
98
105
|
- Initial release
|
99
106
|
|
100
|
-
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.
|
107
|
+
[Unreleased]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.8.0...HEAD
|
108
|
+
[0.8.0]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.7.1...@metamask/chain-agnostic-permission@0.8.0
|
101
109
|
[0.7.1]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.7.0...@metamask/chain-agnostic-permission@0.7.1
|
102
110
|
[0.7.0]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.6.0...@metamask/chain-agnostic-permission@0.7.0
|
103
111
|
[0.6.0]: https://github.com/MetaMask/core/compare/@metamask/chain-agnostic-permission@0.5.0...@metamask/chain-agnostic-permission@0.6.0
|
@@ -4,8 +4,8 @@ exports.getCaip25CaveatFromPermission = exports.generateCaip25Caveat = exports.C
|
|
4
4
|
const permission_controller_1 = require("@metamask/permission-controller");
|
5
5
|
const utils_1 = require("@metamask/utils");
|
6
6
|
const lodash_1 = require("lodash");
|
7
|
-
const
|
8
|
-
const
|
7
|
+
const caip_permission_operator_accounts_1 = require("./operators/caip-permission-operator-accounts.cjs");
|
8
|
+
const caip_permission_operator_permittedChains_1 = require("./operators/caip-permission-operator-permittedChains.cjs");
|
9
9
|
const assert_1 = require("./scope/assert.cjs");
|
10
10
|
const supported_1 = require("./scope/supported.cjs");
|
11
11
|
const transform_1 = require("./scope/transform.cjs");
|
@@ -307,8 +307,8 @@ function removeScope(caip25CaveatValue, targetScopeString) {
|
|
307
307
|
* @returns The updated CAIP-25 caveat value with the permitted accounts and chainIds set.
|
308
308
|
*/
|
309
309
|
const generateCaip25Caveat = (caip25CaveatValue, accountAddresses, chainIds) => {
|
310
|
-
const caveatValueWithChains = (0,
|
311
|
-
const caveatValueWithAccounts = (0,
|
310
|
+
const caveatValueWithChains = (0, caip_permission_operator_permittedChains_1.setChainIdsInCaip25CaveatValue)(caip25CaveatValue, chainIds);
|
311
|
+
const caveatValueWithAccounts = (0, caip_permission_operator_accounts_1.setNonSCACaipAccountIdsInCaip25CaveatValue)(caveatValueWithChains, accountAddresses);
|
312
312
|
return {
|
313
313
|
[exports.Caip25EndowmentPermissionName]: {
|
314
314
|
caveats: [
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"caip25Permission.cjs","sourceRoot":"","sources":["../src/caip25Permission.ts"],"names":[],"mappings":";;;AASA,2EAGyC;AAEzC,2CAOyB;AACzB,mCAA4C;AAE5C,sGAAyG;AACzG,oHAAoG;AACpG,+CAA8D;AAC9D,qDAI2B;AAC3B,qDAAwD;AACxD,6CAKuB;AAcvB;;GAEG;AACU,QAAA,gBAAgB,GAAG,kBAAkB,CAAC;AAEnD;;GAEG;AACU,QAAA,6BAA6B,GAAG,kBAAkB,CAAC;AAEhE;;;;;GAKG;AACI,MAAM,kBAAkB,GAAG,CAAC,KAAwB,EAAE,EAAE;IAC7D,OAAO;QACL,IAAI,EAAE,wBAAgB;QACtB,KAAK;KACN,CAAC;AACJ,CAAC,CAAC;AALW,QAAA,kBAAkB,sBAK7B;AASF;;;;;;;GAOG;AACH,SAAgB,8BAA8B,CAC5C,aAAgC,EAChC,WAA8B,EAC9B,WAAgD;IAEhD,MAAM,IAAI,GAAG,IAAA,kBAAS,EAAC,aAAa,CAAC,CAAC;IAE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C;IACC,OAAO,gBAAgB,EAAE,OAAO,CAAC,IAAI,CACnC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,KAAM,wBAA2B,CAM7C,CAAC;AAChB,CAAC;AApBD,sEAoBC","sourcesContent":["import type { NetworkClientId } from '@metamask/network-controller';\nimport type {\n PermissionSpecificationBuilder,\n EndowmentGetterParams,\n ValidPermissionSpecification,\n PermissionValidatorConstraint,\n PermissionConstraint,\n EndowmentCaveatSpecificationConstraint,\n} from '@metamask/permission-controller';\nimport {\n CaveatMutatorOperation,\n PermissionType,\n} from '@metamask/permission-controller';\nimport type { CaipAccountId, CaipChainId, Json } from '@metamask/utils';\nimport {\n hasProperty,\n KnownCaipNamespace,\n parseCaipAccountId,\n isObject,\n type Hex,\n type NonEmptyArray,\n} from '@metamask/utils';\nimport { cloneDeep, isEqual } from 'lodash';\n\nimport { setNonSCACaipAccountIdsInCaip25CaveatValue } from './adapters/caip-permission-adapter-accounts';\nimport { setChainIdsInCaip25CaveatValue } from './adapters/caip-permission-adapter-permittedChains';\nimport { assertIsInternalScopesObject } from './scope/assert';\nimport {\n isSupportedAccount,\n isSupportedScopeString,\n isSupportedSessionProperty,\n} from './scope/supported';\nimport { mergeInternalScopes } from './scope/transform';\nimport {\n parseScopeString,\n type ExternalScopeString,\n type InternalScopeObject,\n type InternalScopesObject,\n} from './scope/types';\n\n/**\n * The CAIP-25 permission caveat value.\n * This permission contains the required and optional scopes and session properties from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request that initiated the permission session.\n * It also contains a boolean (isMultichainOrigin) indicating if the permission session is multichain, which may be needed to determine implicit permissioning.\n */\nexport type Caip25CaveatValue = {\n requiredScopes: InternalScopesObject;\n optionalScopes: InternalScopesObject;\n sessionProperties: Record<string, Json>;\n isMultichainOrigin: boolean;\n};\n\n/**\n * The name of the CAIP-25 permission caveat.\n */\nexport const Caip25CaveatType = 'authorizedScopes';\n\n/**\n * The target name of the CAIP-25 endowment permission.\n */\nexport const Caip25EndowmentPermissionName = 'endowment:caip25';\n\n/**\n * Creates a CAIP-25 permission caveat.\n *\n * @param value - The CAIP-25 permission caveat value.\n * @returns The CAIP-25 permission caveat (now including the type).\n */\nexport const createCaip25Caveat = (value: Caip25CaveatValue) => {\n return {\n type: Caip25CaveatType,\n value,\n };\n};\n\ntype Caip25EndowmentCaveatSpecificationBuilderOptions = {\n findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId;\n listAccounts: () => { type: string; address: Hex }[];\n isNonEvmScopeSupported: (scope: CaipChainId) => boolean;\n getNonEvmAccountAddresses: (scope: CaipChainId) => string[];\n};\n\n/**\n * Calculates the difference between two provided CAIP-25 permission caveat values, but only considering a single scope property at a time.\n *\n * @param originalValue - The existing CAIP-25 permission caveat value.\n * @param mergedValue - The result from merging existing and incoming CAIP-25 permission caveat values.\n * @param scopeToDiff - The required or optional scopes from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request.\n * @returns The difference between original and merged CAIP-25 permission caveat values.\n */\nexport function diffScopesForCaip25CaveatValue(\n originalValue: Caip25CaveatValue,\n mergedValue: Caip25CaveatValue,\n scopeToDiff: 'optionalScopes' | 'requiredScopes',\n): Caip25CaveatValue {\n const diff = cloneDeep(originalValue);\n\n const mergedScopeToDiff = mergedValue[scopeToDiff];\n for (const [scopeString, mergedScopeObject] of Object.entries(\n mergedScopeToDiff,\n )) {\n const internalScopeString = scopeString as keyof typeof mergedScopeToDiff;\n const originalScopeObject = diff[scopeToDiff][internalScopeString];\n\n if (originalScopeObject) {\n const newAccounts = mergedScopeObject.accounts.filter(\n (account) => !originalScopeObject?.accounts.includes(account),\n );\n if (newAccounts.length > 0) {\n diff[scopeToDiff][internalScopeString] = {\n accounts: newAccounts,\n };\n continue;\n }\n delete diff[scopeToDiff][internalScopeString];\n } else {\n diff[scopeToDiff][internalScopeString] = mergedScopeObject;\n }\n }\n\n return diff;\n}\n\n/**\n * Checks if every account in the given scopes object is supported.\n *\n * @param scopesObject - The scopes object to iterate over.\n * @param listAccounts - The hook for getting internalAccount objects for all evm accounts.\n * @param getNonEvmAccountAddresses - The hook that returns the supported CAIP-10 account addresses for a non EVM scope.\n * addresses.\n * @returns True if every account in the scopes object is supported, false otherwise.\n */\nfunction isEveryAccountInScopesObjectSupported(\n scopesObject: InternalScopesObject,\n listAccounts: () => { type: string; address: Hex }[],\n getNonEvmAccountAddresses: (scope: CaipChainId) => string[],\n) {\n return Object.values(scopesObject).every((scopeObject) =>\n scopeObject.accounts.every((account) =>\n isSupportedAccount(account, {\n getEvmInternalAccounts: listAccounts,\n getNonEvmAccountAddresses,\n }),\n ),\n );\n}\n\n/**\n * Helper that returns a `authorizedScopes` CAIP-25 caveat specification\n * that can be passed into the PermissionController constructor.\n *\n * @param options - The specification builder options.\n * @param options.findNetworkClientIdByChainId - The hook for getting the networkClientId that serves a chainId.\n * @param options.listAccounts - The hook for getting internalAccount objects for all evm accounts.\n * @param options.isNonEvmScopeSupported - The hook that determines if an non EVM scopeString is supported.\n * @param options.getNonEvmAccountAddresses - The hook that returns the supported CAIP-10 account addresses for a non EVM scope.\n * @returns The specification for the `caip25` caveat.\n */\nexport const caip25CaveatBuilder = ({\n findNetworkClientIdByChainId,\n listAccounts,\n isNonEvmScopeSupported,\n getNonEvmAccountAddresses,\n}: Caip25EndowmentCaveatSpecificationBuilderOptions): EndowmentCaveatSpecificationConstraint &\n Required<\n Pick<EndowmentCaveatSpecificationConstraint, 'validator' | 'merger'>\n > => {\n return {\n type: Caip25CaveatType,\n validator: (\n caveat: { type: typeof Caip25CaveatType; value: unknown },\n _origin?: string,\n _target?: string,\n ) => {\n if (\n !caveat.value ||\n !hasProperty(caveat.value, 'requiredScopes') ||\n !hasProperty(caveat.value, 'optionalScopes') ||\n !hasProperty(caveat.value, 'isMultichainOrigin') ||\n !hasProperty(caveat.value, 'sessionProperties') ||\n typeof caveat.value.isMultichainOrigin !== 'boolean' ||\n !isObject(caveat.value.sessionProperties)\n ) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received invalid value for caveat of type \"${Caip25CaveatType}\".`,\n );\n }\n\n const { requiredScopes, optionalScopes, sessionProperties } =\n caveat.value;\n\n const allSessionPropertiesSupported = Object.keys(\n sessionProperties,\n ).every((sessionProperty) => isSupportedSessionProperty(sessionProperty));\n\n if (!allSessionPropertiesSupported) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received unknown session property(s) for caveat of type \"${Caip25CaveatType}\".`,\n );\n }\n\n assertIsInternalScopesObject(requiredScopes);\n assertIsInternalScopesObject(optionalScopes);\n\n if (\n Object.keys(requiredScopes).length === 0 &&\n Object.keys(optionalScopes).length === 0\n ) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received no scopes for caveat of type \"${Caip25CaveatType}\".`,\n );\n }\n\n const isEvmChainIdSupported = (chainId: Hex) => {\n try {\n findNetworkClientIdByChainId(chainId);\n return true;\n } catch {\n return false;\n }\n };\n\n const unsupportedScopes = Object.keys({\n ...requiredScopes,\n ...optionalScopes,\n }).filter(\n (scopeString) =>\n !isSupportedScopeString(scopeString, {\n isEvmChainIdSupported,\n isNonEvmScopeSupported,\n }),\n );\n\n if (unsupportedScopes.length > 0) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received scopeString value(s): ${unsupportedScopes.join(', ')} for caveat of type \"${Caip25CaveatType}\" that are not supported by the wallet.`,\n );\n }\n\n const allRequiredAccountsSupported =\n isEveryAccountInScopesObjectSupported(\n requiredScopes,\n listAccounts,\n getNonEvmAccountAddresses,\n );\n const allOptionalAccountsSupported =\n isEveryAccountInScopesObjectSupported(\n optionalScopes,\n listAccounts,\n getNonEvmAccountAddresses,\n );\n if (!allRequiredAccountsSupported || !allOptionalAccountsSupported) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received account value(s) for caveat of type \"${Caip25CaveatType}\" that are not supported by the wallet.`,\n );\n }\n },\n merger: (\n leftValue: Caip25CaveatValue,\n rightValue: Caip25CaveatValue,\n ): [Caip25CaveatValue, Caip25CaveatValue] => {\n const mergedRequiredScopes = mergeInternalScopes(\n leftValue.requiredScopes,\n rightValue.requiredScopes,\n );\n const mergedOptionalScopes = mergeInternalScopes(\n leftValue.optionalScopes,\n rightValue.optionalScopes,\n );\n\n const mergedSessionProperties = {\n ...leftValue.sessionProperties,\n ...rightValue.sessionProperties,\n };\n\n const mergedValue: Caip25CaveatValue = {\n requiredScopes: mergedRequiredScopes,\n optionalScopes: mergedOptionalScopes,\n sessionProperties: mergedSessionProperties,\n isMultichainOrigin: leftValue.isMultichainOrigin,\n };\n\n const partialDiff = diffScopesForCaip25CaveatValue(\n leftValue,\n mergedValue,\n 'requiredScopes',\n );\n\n const diff = diffScopesForCaip25CaveatValue(\n partialDiff,\n mergedValue,\n 'optionalScopes',\n );\n\n return [mergedValue, diff];\n },\n };\n};\n\ntype Caip25EndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetName: typeof Caip25EndowmentPermissionName;\n endowmentGetter: (_options?: EndowmentGetterParams) => null;\n validator: PermissionValidatorConstraint;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * Helper that returns a `endowment:caip25` specification that\n * can be passed into the PermissionController constructor.\n *\n * @returns The specification for the `caip25` endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n Record<never, never>,\n Caip25EndowmentSpecification\n> = () => {\n return {\n permissionType: PermissionType.Endowment,\n targetName: Caip25EndowmentPermissionName,\n allowedCaveats: [Caip25CaveatType],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => null,\n validator: (permission: PermissionConstraint) => {\n if (\n permission.caveats?.length !== 1 ||\n permission.caveats?.[0]?.type !== Caip25CaveatType\n ) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Invalid caveats. There must be a single caveat of type \"${Caip25CaveatType}\".`,\n );\n }\n },\n };\n};\n\n/**\n * The `caip25` endowment specification builder. Passed to the\n * `PermissionController` for constructing and validating the\n * `endowment:caip25` permission.\n */\nexport const caip25EndowmentBuilder = Object.freeze({\n targetName: Caip25EndowmentPermissionName,\n specificationBuilder,\n} as const);\n\n/**\n * Factories that construct caveat mutator functions that are passed to\n * PermissionController.updatePermissionsByCaveat.\n */\nexport const Caip25CaveatMutators = {\n [Caip25CaveatType]: {\n removeScope,\n removeAccount,\n },\n};\n\n/**\n * Removes the account from the scope object.\n *\n * @param targetAddress - The address to remove from the scope object.\n * @returns A function that removes the account from the scope object.\n */\nfunction removeAccountFilterFn(targetAddress: string) {\n return (account: CaipAccountId) => {\n const parsed = parseCaipAccountId(account);\n return parsed.address !== targetAddress;\n };\n}\n\n/**\n * Removes the account from the scope object.\n *\n * @param scopeObject - The scope object to remove the account from.\n * @param targetAddress - The address to remove from the scope object.\n */\nfunction removeAccountFromScopeObject(\n scopeObject: InternalScopeObject,\n targetAddress: string,\n) {\n if (scopeObject.accounts) {\n scopeObject.accounts = scopeObject.accounts.filter(\n removeAccountFilterFn(targetAddress),\n );\n }\n}\n\n/**\n * Removes the target account from the scope object.\n *\n * @param caip25CaveatValue - The CAIP-25 permission caveat value from which to remove the account (across all chain scopes).\n * @param targetAddress - The address to remove from the scope object. Not a CAIP-10 formatted address because it will be removed across each chain scope.\n * @returns The updated scope object.\n */\nfunction removeAccount(\n caip25CaveatValue: Caip25CaveatValue,\n targetAddress: Hex,\n) {\n const updatedCaveatValue = cloneDeep(caip25CaveatValue);\n\n [\n updatedCaveatValue.requiredScopes,\n updatedCaveatValue.optionalScopes,\n ].forEach((scopes) => {\n Object.entries(scopes).forEach(([, scopeObject]) => {\n removeAccountFromScopeObject(scopeObject, targetAddress);\n });\n });\n\n const noChange = isEqual(updatedCaveatValue, caip25CaveatValue);\n\n if (noChange) {\n return {\n operation: CaveatMutatorOperation.Noop,\n };\n }\n\n const hasAccounts = [\n ...Object.values(updatedCaveatValue.requiredScopes),\n ...Object.values(updatedCaveatValue.optionalScopes),\n ].some(({ accounts }) => accounts.length > 0);\n\n if (hasAccounts) {\n return {\n operation: CaveatMutatorOperation.UpdateValue,\n value: updatedCaveatValue,\n };\n }\n\n return {\n operation: CaveatMutatorOperation.RevokePermission,\n };\n}\n\n/**\n * Removes the target scope from the value arrays of the given\n * `endowment:caip25` caveat. No-ops if the target scopeString is not in\n * the existing scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 permission caveat value to remove the scope from.\n * @param targetScopeString - The scope that is being removed.\n * @returns The updated CAIP-25 permission caveat value.\n */\nfunction removeScope(\n caip25CaveatValue: Caip25CaveatValue,\n targetScopeString: ExternalScopeString,\n) {\n const newRequiredScopes = Object.entries(\n caip25CaveatValue.requiredScopes,\n ).filter(([scope]) => scope !== targetScopeString);\n const newOptionalScopes = Object.entries(\n caip25CaveatValue.optionalScopes,\n ).filter(([scope]) => {\n return scope !== targetScopeString;\n });\n\n const requiredScopesRemoved =\n newRequiredScopes.length !==\n Object.keys(caip25CaveatValue.requiredScopes).length;\n const optionalScopesRemoved =\n newOptionalScopes.length !==\n Object.keys(caip25CaveatValue.optionalScopes).length;\n\n if (!requiredScopesRemoved && !optionalScopesRemoved) {\n return {\n operation: CaveatMutatorOperation.Noop,\n };\n }\n\n const updatedCaveatValue = {\n ...caip25CaveatValue,\n requiredScopes: Object.fromEntries(newRequiredScopes),\n optionalScopes: Object.fromEntries(newOptionalScopes),\n };\n\n const hasNonWalletScopes = [...newRequiredScopes, ...newOptionalScopes].some(\n ([scopeString]) => {\n const { namespace } = parseScopeString(scopeString);\n return namespace !== KnownCaipNamespace.Wallet;\n },\n );\n\n if (hasNonWalletScopes) {\n return {\n operation: CaveatMutatorOperation.UpdateValue,\n value: updatedCaveatValue,\n };\n }\n\n return {\n operation: CaveatMutatorOperation.RevokePermission,\n };\n}\n\n/**\n * Modifies the requested CAIP-25 permissions object after UI confirmation.\n *\n * @param caip25CaveatValue - The requested CAIP-25 caveat value to modify.\n * @param accountAddresses - The list of permitted eth addresses.\n * @param chainIds - The list of permitted eth chainIds.\n * @returns The updated CAIP-25 caveat value with the permitted accounts and chainIds set.\n */\nexport const generateCaip25Caveat = (\n caip25CaveatValue: Caip25CaveatValue,\n accountAddresses: CaipAccountId[],\n chainIds: CaipChainId[],\n): {\n [Caip25EndowmentPermissionName]: {\n caveats: [{ type: string; value: Caip25CaveatValue }];\n };\n} => {\n const caveatValueWithChains = setChainIdsInCaip25CaveatValue(\n caip25CaveatValue,\n chainIds,\n );\n\n const caveatValueWithAccounts = setNonSCACaipAccountIdsInCaip25CaveatValue(\n caveatValueWithChains,\n accountAddresses,\n );\n\n return {\n [Caip25EndowmentPermissionName]: {\n caveats: [\n {\n type: Caip25CaveatType,\n value: caveatValueWithAccounts,\n },\n ],\n },\n };\n};\n\n/**\n * Helper to get the CAIP-25 caveat from a permission\n *\n * @param [caip25Permission] - The CAIP-25 permission object\n * @param caip25Permission.caveats - The caveats of the CAIP-25 permission\n * @returns The CAIP-25 caveat or undefined if not found\n */\nexport function getCaip25CaveatFromPermission(caip25Permission?: {\n caveats: (\n | {\n type: string;\n value: unknown;\n }\n | {\n type: typeof Caip25CaveatType;\n value: Caip25CaveatValue;\n }\n )[];\n}) {\n return caip25Permission?.caveats.find(\n (caveat) => caveat.type === (Caip25CaveatType as string),\n ) as\n | {\n type: typeof Caip25CaveatType;\n value: Caip25CaveatValue;\n }\n | undefined;\n}\n"]}
|
1
|
+
{"version":3,"file":"caip25Permission.cjs","sourceRoot":"","sources":["../src/caip25Permission.ts"],"names":[],"mappings":";;;AASA,2EAGyC;AAEzC,2CAOyB;AACzB,mCAA4C;AAE5C,yGAA2G;AAC3G,uHAAsG;AACtG,+CAA8D;AAC9D,qDAI2B;AAC3B,qDAAwD;AACxD,6CAKuB;AAcvB;;GAEG;AACU,QAAA,gBAAgB,GAAG,kBAAkB,CAAC;AAEnD;;GAEG;AACU,QAAA,6BAA6B,GAAG,kBAAkB,CAAC;AAEhE;;;;;GAKG;AACI,MAAM,kBAAkB,GAAG,CAAC,K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gBAAgB,CACjB,CAAC;IAEF,OAAO;QACL,CAAC,qCAA6B,CAAC,EAAE;YAC/B,OAAO,EAAE;gBACP;oBACE,IAAI,EAAE,wBAAgB;oBACtB,KAAK,EAAE,uBAAuB;iBAC/B;aACF;SACF;KACF,CAAC;AACJ,CAAC,CAAC;AA7BW,QAAA,oBAAoB,wBA6B/B;AAEF;;;;;;GAMG;AACH,SAAgB,6BAA6B,CAAC,gBAW7C;IACC,OAAO,gBAAgB,EAAE,OAAO,CAAC,IAAI,CACnC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,KAAM,wBAA2B,CAM7C,CAAC;AAChB,CAAC;AApBD,sEAoBC","sourcesContent":["import type { NetworkClientId } from '@metamask/network-controller';\nimport type {\n PermissionSpecificationBuilder,\n EndowmentGetterParams,\n ValidPermissionSpecification,\n PermissionValidatorConstraint,\n PermissionConstraint,\n EndowmentCaveatSpecificationConstraint,\n} from '@metamask/permission-controller';\nimport {\n CaveatMutatorOperation,\n PermissionType,\n} from '@metamask/permission-controller';\nimport type { CaipAccountId, CaipChainId, Json } from '@metamask/utils';\nimport {\n hasProperty,\n KnownCaipNamespace,\n parseCaipAccountId,\n isObject,\n type Hex,\n type NonEmptyArray,\n} from '@metamask/utils';\nimport { cloneDeep, isEqual } from 'lodash';\n\nimport { setNonSCACaipAccountIdsInCaip25CaveatValue } from './operators/caip-permission-operator-accounts';\nimport { setChainIdsInCaip25CaveatValue } from './operators/caip-permission-operator-permittedChains';\nimport { assertIsInternalScopesObject } from './scope/assert';\nimport {\n isSupportedAccount,\n isSupportedScopeString,\n isSupportedSessionProperty,\n} from './scope/supported';\nimport { mergeInternalScopes } from './scope/transform';\nimport {\n parseScopeString,\n type ExternalScopeString,\n type InternalScopeObject,\n type InternalScopesObject,\n} from './scope/types';\n\n/**\n * The CAIP-25 permission caveat value.\n * This permission contains the required and optional scopes and session properties from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request that initiated the permission session.\n * It also contains a boolean (isMultichainOrigin) indicating if the permission session is multichain, which may be needed to determine implicit permissioning.\n */\nexport type Caip25CaveatValue = {\n requiredScopes: InternalScopesObject;\n optionalScopes: InternalScopesObject;\n sessionProperties: Record<string, Json>;\n isMultichainOrigin: boolean;\n};\n\n/**\n * The name of the CAIP-25 permission caveat.\n */\nexport const Caip25CaveatType = 'authorizedScopes';\n\n/**\n * The target name of the CAIP-25 endowment permission.\n */\nexport const Caip25EndowmentPermissionName = 'endowment:caip25';\n\n/**\n * Creates a CAIP-25 permission caveat.\n *\n * @param value - The CAIP-25 permission caveat value.\n * @returns The CAIP-25 permission caveat (now including the type).\n */\nexport const createCaip25Caveat = (value: Caip25CaveatValue) => {\n return {\n type: Caip25CaveatType,\n value,\n };\n};\n\ntype Caip25EndowmentCaveatSpecificationBuilderOptions = {\n findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId;\n listAccounts: () => { type: string; address: Hex }[];\n isNonEvmScopeSupported: (scope: CaipChainId) => boolean;\n getNonEvmAccountAddresses: (scope: CaipChainId) => string[];\n};\n\n/**\n * Calculates the difference between two provided CAIP-25 permission caveat values, but only considering a single scope property at a time.\n *\n * @param originalValue - The existing CAIP-25 permission caveat value.\n * @param mergedValue - The result from merging existing and incoming CAIP-25 permission caveat values.\n * @param scopeToDiff - The required or optional scopes from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request.\n * @returns The difference between original and merged CAIP-25 permission caveat values.\n */\nexport function diffScopesForCaip25CaveatValue(\n originalValue: Caip25CaveatValue,\n mergedValue: Caip25CaveatValue,\n scopeToDiff: 'optionalScopes' | 'requiredScopes',\n): Caip25CaveatValue {\n const diff = cloneDeep(originalValue);\n\n const mergedScopeToDiff = mergedValue[scopeToDiff];\n for (const [scopeString, mergedScopeObject] of Object.entries(\n mergedScopeToDiff,\n )) {\n const internalScopeString = scopeString as keyof typeof mergedScopeToDiff;\n const originalScopeObject = diff[scopeToDiff][internalScopeString];\n\n if (originalScopeObject) {\n const newAccounts = mergedScopeObject.accounts.filter(\n (account) => !originalScopeObject?.accounts.includes(account),\n );\n if (newAccounts.length > 0) {\n diff[scopeToDiff][internalScopeString] = {\n accounts: newAccounts,\n };\n continue;\n }\n delete diff[scopeToDiff][internalScopeString];\n } else {\n diff[scopeToDiff][internalScopeString] = mergedScopeObject;\n }\n }\n\n return diff;\n}\n\n/**\n * Checks if every account in the given scopes object is supported.\n *\n * @param scopesObject - The scopes object to iterate over.\n * @param listAccounts - The hook for getting internalAccount objects for all evm accounts.\n * @param getNonEvmAccountAddresses - The hook that returns the supported CAIP-10 account addresses for a non EVM scope.\n * addresses.\n * @returns True if every account in the scopes object is supported, false otherwise.\n */\nfunction isEveryAccountInScopesObjectSupported(\n scopesObject: InternalScopesObject,\n listAccounts: () => { type: string; address: Hex }[],\n getNonEvmAccountAddresses: (scope: CaipChainId) => string[],\n) {\n return Object.values(scopesObject).every((scopeObject) =>\n scopeObject.accounts.every((account) =>\n isSupportedAccount(account, {\n getEvmInternalAccounts: listAccounts,\n getNonEvmAccountAddresses,\n }),\n ),\n );\n}\n\n/**\n * Helper that returns a `authorizedScopes` CAIP-25 caveat specification\n * that can be passed into the PermissionController constructor.\n *\n * @param options - The specification builder options.\n * @param options.findNetworkClientIdByChainId - The hook for getting the networkClientId that serves a chainId.\n * @param options.listAccounts - The hook for getting internalAccount objects for all evm accounts.\n * @param options.isNonEvmScopeSupported - The hook that determines if an non EVM scopeString is supported.\n * @param options.getNonEvmAccountAddresses - The hook that returns the supported CAIP-10 account addresses for a non EVM scope.\n * @returns The specification for the `caip25` caveat.\n */\nexport const caip25CaveatBuilder = ({\n findNetworkClientIdByChainId,\n listAccounts,\n isNonEvmScopeSupported,\n getNonEvmAccountAddresses,\n}: Caip25EndowmentCaveatSpecificationBuilderOptions): EndowmentCaveatSpecificationConstraint &\n Required<\n Pick<EndowmentCaveatSpecificationConstraint, 'validator' | 'merger'>\n > => {\n return {\n type: Caip25CaveatType,\n validator: (\n caveat: { type: typeof Caip25CaveatType; value: unknown },\n _origin?: string,\n _target?: string,\n ) => {\n if (\n !caveat.value ||\n !hasProperty(caveat.value, 'requiredScopes') ||\n !hasProperty(caveat.value, 'optionalScopes') ||\n !hasProperty(caveat.value, 'isMultichainOrigin') ||\n !hasProperty(caveat.value, 'sessionProperties') ||\n typeof caveat.value.isMultichainOrigin !== 'boolean' ||\n !isObject(caveat.value.sessionProperties)\n ) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received invalid value for caveat of type \"${Caip25CaveatType}\".`,\n );\n }\n\n const { requiredScopes, optionalScopes, sessionProperties } =\n caveat.value;\n\n const allSessionPropertiesSupported = Object.keys(\n sessionProperties,\n ).every((sessionProperty) => isSupportedSessionProperty(sessionProperty));\n\n if (!allSessionPropertiesSupported) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received unknown session property(s) for caveat of type \"${Caip25CaveatType}\".`,\n );\n }\n\n assertIsInternalScopesObject(requiredScopes);\n assertIsInternalScopesObject(optionalScopes);\n\n if (\n Object.keys(requiredScopes).length === 0 &&\n Object.keys(optionalScopes).length === 0\n ) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received no scopes for caveat of type \"${Caip25CaveatType}\".`,\n );\n }\n\n const isEvmChainIdSupported = (chainId: Hex) => {\n try {\n findNetworkClientIdByChainId(chainId);\n return true;\n } catch {\n return false;\n }\n };\n\n const unsupportedScopes = Object.keys({\n ...requiredScopes,\n ...optionalScopes,\n }).filter(\n (scopeString) =>\n !isSupportedScopeString(scopeString, {\n isEvmChainIdSupported,\n isNonEvmScopeSupported,\n }),\n );\n\n if (unsupportedScopes.length > 0) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received scopeString value(s): ${unsupportedScopes.join(', ')} for caveat of type \"${Caip25CaveatType}\" that are not supported by the wallet.`,\n );\n }\n\n const allRequiredAccountsSupported =\n isEveryAccountInScopesObjectSupported(\n requiredScopes,\n listAccounts,\n getNonEvmAccountAddresses,\n );\n const allOptionalAccountsSupported =\n isEveryAccountInScopesObjectSupported(\n optionalScopes,\n listAccounts,\n getNonEvmAccountAddresses,\n );\n if (!allRequiredAccountsSupported || !allOptionalAccountsSupported) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received account value(s) for caveat of type \"${Caip25CaveatType}\" that are not supported by the wallet.`,\n );\n }\n },\n merger: (\n leftValue: Caip25CaveatValue,\n rightValue: Caip25CaveatValue,\n ): [Caip25CaveatValue, Caip25CaveatValue] => {\n const mergedRequiredScopes = mergeInternalScopes(\n leftValue.requiredScopes,\n rightValue.requiredScopes,\n );\n const mergedOptionalScopes = mergeInternalScopes(\n leftValue.optionalScopes,\n rightValue.optionalScopes,\n );\n\n const mergedSessionProperties = {\n ...leftValue.sessionProperties,\n ...rightValue.sessionProperties,\n };\n\n const mergedValue: Caip25CaveatValue = {\n requiredScopes: mergedRequiredScopes,\n optionalScopes: mergedOptionalScopes,\n sessionProperties: mergedSessionProperties,\n isMultichainOrigin: leftValue.isMultichainOrigin,\n };\n\n const partialDiff = diffScopesForCaip25CaveatValue(\n leftValue,\n mergedValue,\n 'requiredScopes',\n );\n\n const diff = diffScopesForCaip25CaveatValue(\n partialDiff,\n mergedValue,\n 'optionalScopes',\n );\n\n return [mergedValue, diff];\n },\n };\n};\n\ntype Caip25EndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetName: typeof Caip25EndowmentPermissionName;\n endowmentGetter: (_options?: EndowmentGetterParams) => null;\n validator: PermissionValidatorConstraint;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * Helper that returns a `endowment:caip25` specification that\n * can be passed into the PermissionController constructor.\n *\n * @returns The specification for the `caip25` endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n Record<never, never>,\n Caip25EndowmentSpecification\n> = () => {\n return {\n permissionType: PermissionType.Endowment,\n targetName: Caip25EndowmentPermissionName,\n allowedCaveats: [Caip25CaveatType],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => null,\n validator: (permission: PermissionConstraint) => {\n if (\n permission.caveats?.length !== 1 ||\n permission.caveats?.[0]?.type !== Caip25CaveatType\n ) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Invalid caveats. There must be a single caveat of type \"${Caip25CaveatType}\".`,\n );\n }\n },\n };\n};\n\n/**\n * The `caip25` endowment specification builder. Passed to the\n * `PermissionController` for constructing and validating the\n * `endowment:caip25` permission.\n */\nexport const caip25EndowmentBuilder = Object.freeze({\n targetName: Caip25EndowmentPermissionName,\n specificationBuilder,\n} as const);\n\n/**\n * Factories that construct caveat mutator functions that are passed to\n * PermissionController.updatePermissionsByCaveat.\n */\nexport const Caip25CaveatMutators = {\n [Caip25CaveatType]: {\n removeScope,\n removeAccount,\n },\n};\n\n/**\n * Removes the account from the scope object.\n *\n * @param targetAddress - The address to remove from the scope object.\n * @returns A function that removes the account from the scope object.\n */\nfunction removeAccountFilterFn(targetAddress: string) {\n return (account: CaipAccountId) => {\n const parsed = parseCaipAccountId(account);\n return parsed.address !== targetAddress;\n };\n}\n\n/**\n * Removes the account from the scope object.\n *\n * @param scopeObject - The scope object to remove the account from.\n * @param targetAddress - The address to remove from the scope object.\n */\nfunction removeAccountFromScopeObject(\n scopeObject: InternalScopeObject,\n targetAddress: string,\n) {\n if (scopeObject.accounts) {\n scopeObject.accounts = scopeObject.accounts.filter(\n removeAccountFilterFn(targetAddress),\n );\n }\n}\n\n/**\n * Removes the target account from the scope object.\n *\n * @param caip25CaveatValue - The CAIP-25 permission caveat value from which to remove the account (across all chain scopes).\n * @param targetAddress - The address to remove from the scope object. Not a CAIP-10 formatted address because it will be removed across each chain scope.\n * @returns The updated scope object.\n */\nfunction removeAccount(\n caip25CaveatValue: Caip25CaveatValue,\n targetAddress: Hex,\n) {\n const updatedCaveatValue = cloneDeep(caip25CaveatValue);\n\n [\n updatedCaveatValue.requiredScopes,\n updatedCaveatValue.optionalScopes,\n ].forEach((scopes) => {\n Object.entries(scopes).forEach(([, scopeObject]) => {\n removeAccountFromScopeObject(scopeObject, targetAddress);\n });\n });\n\n const noChange = isEqual(updatedCaveatValue, caip25CaveatValue);\n\n if (noChange) {\n return {\n operation: CaveatMutatorOperation.Noop,\n };\n }\n\n const hasAccounts = [\n ...Object.values(updatedCaveatValue.requiredScopes),\n ...Object.values(updatedCaveatValue.optionalScopes),\n ].some(({ accounts }) => accounts.length > 0);\n\n if (hasAccounts) {\n return {\n operation: CaveatMutatorOperation.UpdateValue,\n value: updatedCaveatValue,\n };\n }\n\n return {\n operation: CaveatMutatorOperation.RevokePermission,\n };\n}\n\n/**\n * Removes the target scope from the value arrays of the given\n * `endowment:caip25` caveat. No-ops if the target scopeString is not in\n * the existing scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 permission caveat value to remove the scope from.\n * @param targetScopeString - The scope that is being removed.\n * @returns The updated CAIP-25 permission caveat value.\n */\nfunction removeScope(\n caip25CaveatValue: Caip25CaveatValue,\n targetScopeString: ExternalScopeString,\n) {\n const newRequiredScopes = Object.entries(\n caip25CaveatValue.requiredScopes,\n ).filter(([scope]) => scope !== targetScopeString);\n const newOptionalScopes = Object.entries(\n caip25CaveatValue.optionalScopes,\n ).filter(([scope]) => {\n return scope !== targetScopeString;\n });\n\n const requiredScopesRemoved =\n newRequiredScopes.length !==\n Object.keys(caip25CaveatValue.requiredScopes).length;\n const optionalScopesRemoved =\n newOptionalScopes.length !==\n Object.keys(caip25CaveatValue.optionalScopes).length;\n\n if (!requiredScopesRemoved && !optionalScopesRemoved) {\n return {\n operation: CaveatMutatorOperation.Noop,\n };\n }\n\n const updatedCaveatValue = {\n ...caip25CaveatValue,\n requiredScopes: Object.fromEntries(newRequiredScopes),\n optionalScopes: Object.fromEntries(newOptionalScopes),\n };\n\n const hasNonWalletScopes = [...newRequiredScopes, ...newOptionalScopes].some(\n ([scopeString]) => {\n const { namespace } = parseScopeString(scopeString);\n return namespace !== KnownCaipNamespace.Wallet;\n },\n );\n\n if (hasNonWalletScopes) {\n return {\n operation: CaveatMutatorOperation.UpdateValue,\n value: updatedCaveatValue,\n };\n }\n\n return {\n operation: CaveatMutatorOperation.RevokePermission,\n };\n}\n\n/**\n * Modifies the requested CAIP-25 permissions object after UI confirmation.\n *\n * @param caip25CaveatValue - The requested CAIP-25 caveat value to modify.\n * @param accountAddresses - The list of permitted eth addresses.\n * @param chainIds - The list of permitted eth chainIds.\n * @returns The updated CAIP-25 caveat value with the permitted accounts and chainIds set.\n */\nexport const generateCaip25Caveat = (\n caip25CaveatValue: Caip25CaveatValue,\n accountAddresses: CaipAccountId[],\n chainIds: CaipChainId[],\n): {\n [Caip25EndowmentPermissionName]: {\n caveats: [{ type: string; value: Caip25CaveatValue }];\n };\n} => {\n const caveatValueWithChains = setChainIdsInCaip25CaveatValue(\n caip25CaveatValue,\n chainIds,\n );\n\n const caveatValueWithAccounts = setNonSCACaipAccountIdsInCaip25CaveatValue(\n caveatValueWithChains,\n accountAddresses,\n );\n\n return {\n [Caip25EndowmentPermissionName]: {\n caveats: [\n {\n type: Caip25CaveatType,\n value: caveatValueWithAccounts,\n },\n ],\n },\n };\n};\n\n/**\n * Helper to get the CAIP-25 caveat from a permission\n *\n * @param [caip25Permission] - The CAIP-25 permission object\n * @param caip25Permission.caveats - The caveats of the CAIP-25 permission\n * @returns The CAIP-25 caveat or undefined if not found\n */\nexport function getCaip25CaveatFromPermission(caip25Permission?: {\n caveats: (\n | {\n type: string;\n value: unknown;\n }\n | {\n type: typeof Caip25CaveatType;\n value: Caip25CaveatValue;\n }\n )[];\n}) {\n return caip25Permission?.caveats.find(\n (caveat) => caveat.type === (Caip25CaveatType as string),\n ) as\n | {\n type: typeof Caip25CaveatType;\n value: Caip25CaveatValue;\n }\n | undefined;\n}\n"]}
|
@@ -2,8 +2,8 @@ import { CaveatMutatorOperation, PermissionType } from "@metamask/permission-con
|
|
2
2
|
import { hasProperty, KnownCaipNamespace, parseCaipAccountId, isObject } from "@metamask/utils";
|
3
3
|
import $lodash from "lodash";
|
4
4
|
const { cloneDeep, isEqual } = $lodash;
|
5
|
-
import { setNonSCACaipAccountIdsInCaip25CaveatValue } from "./
|
6
|
-
import { setChainIdsInCaip25CaveatValue } from "./
|
5
|
+
import { setNonSCACaipAccountIdsInCaip25CaveatValue } from "./operators/caip-permission-operator-accounts.mjs";
|
6
|
+
import { setChainIdsInCaip25CaveatValue } from "./operators/caip-permission-operator-permittedChains.mjs";
|
7
7
|
import { assertIsInternalScopesObject } from "./scope/assert.mjs";
|
8
8
|
import { isSupportedAccount, isSupportedScopeString, isSupportedSessionProperty } from "./scope/supported.mjs";
|
9
9
|
import { mergeInternalScopes } from "./scope/transform.mjs";
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"caip25Permission.mjs","sourceRoot":"","sources":["../src/caip25Permission.ts"],"names":[],"mappings":"AASA,OAAO,EACL,sBAAsB,EACtB,cAAc,EACf,wCAAwC;AAEzC,OAAO,EACL,WAAW,EACX,kBAAkB,EAClB,kBAAkB,EAClB,QAAQ,EAGT,wBAAwB;;;AAGzB,OAAO,EAAE,0CAA0C,EAAE,wDAAoD;AACzG,OAAO,EAAE,8BAA8B,EAAE,+DAA2D;AACpG,OAAO,EAAE,4BAA4B,EAAE,2BAAuB;AAC9D,OAAO,EACL,kBAAkB,EAClB,sBAAsB,EACtB,0BAA0B,EAC3B,8BAA0B;AAC3B,OAAO,EAAE,mBAAmB,EAAE,8BAA0B;AACxD,OAAO,EACL,gBAAgB,EAIjB,0BAAsB;AAcvB;;GAEG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,kBAAkB,CAAC;AAEnD;;GAEG;AACH,MAAM,CAAC,MAAM,6BAA6B,GAAG,kBAAkB,CAAC;AAEhE;;;;;GAKG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,KAAwB,EAAE,EAAE;IAC7D,OAAO;QACL,IAAI,EAAE,gBAAgB;QACtB,KAAK;KACN,CAAC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6BAA6B,CAAC,gBAW7C;IACC,OAAO,gBAAgB,EAAE,OAAO,CAAC,IAAI,CACnC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,KAAM,gBAA2B,CAM7C,CAAC;AAChB,CAAC","sourcesContent":["import type { NetworkClientId } from '@metamask/network-controller';\nimport type {\n PermissionSpecificationBuilder,\n EndowmentGetterParams,\n ValidPermissionSpecification,\n PermissionValidatorConstraint,\n PermissionConstraint,\n EndowmentCaveatSpecificationConstraint,\n} from '@metamask/permission-controller';\nimport {\n CaveatMutatorOperation,\n PermissionType,\n} from '@metamask/permission-controller';\nimport type { CaipAccountId, CaipChainId, Json } from '@metamask/utils';\nimport {\n hasProperty,\n KnownCaipNamespace,\n parseCaipAccountId,\n isObject,\n type Hex,\n type NonEmptyArray,\n} from '@metamask/utils';\nimport { cloneDeep, isEqual } from 'lodash';\n\nimport { setNonSCACaipAccountIdsInCaip25CaveatValue } from './adapters/caip-permission-adapter-accounts';\nimport { setChainIdsInCaip25CaveatValue } from './adapters/caip-permission-adapter-permittedChains';\nimport { assertIsInternalScopesObject } from './scope/assert';\nimport {\n isSupportedAccount,\n isSupportedScopeString,\n isSupportedSessionProperty,\n} from './scope/supported';\nimport { mergeInternalScopes } from './scope/transform';\nimport {\n parseScopeString,\n type ExternalScopeString,\n type InternalScopeObject,\n type InternalScopesObject,\n} from './scope/types';\n\n/**\n * The CAIP-25 permission caveat value.\n * This permission contains the required and optional scopes and session properties from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request that initiated the permission session.\n * It also contains a boolean (isMultichainOrigin) indicating if the permission session is multichain, which may be needed to determine implicit permissioning.\n */\nexport type Caip25CaveatValue = {\n requiredScopes: InternalScopesObject;\n optionalScopes: InternalScopesObject;\n sessionProperties: Record<string, Json>;\n isMultichainOrigin: boolean;\n};\n\n/**\n * The name of the CAIP-25 permission caveat.\n */\nexport const Caip25CaveatType = 'authorizedScopes';\n\n/**\n * The target name of the CAIP-25 endowment permission.\n */\nexport const Caip25EndowmentPermissionName = 'endowment:caip25';\n\n/**\n * Creates a CAIP-25 permission caveat.\n *\n * @param value - The CAIP-25 permission caveat value.\n * @returns The CAIP-25 permission caveat (now including the type).\n */\nexport const createCaip25Caveat = (value: Caip25CaveatValue) => {\n return {\n type: Caip25CaveatType,\n value,\n };\n};\n\ntype Caip25EndowmentCaveatSpecificationBuilderOptions = {\n findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId;\n listAccounts: () => { type: string; address: Hex }[];\n isNonEvmScopeSupported: (scope: CaipChainId) => boolean;\n getNonEvmAccountAddresses: (scope: CaipChainId) => string[];\n};\n\n/**\n * Calculates the difference between two provided CAIP-25 permission caveat values, but only considering a single scope property at a time.\n *\n * @param originalValue - The existing CAIP-25 permission caveat value.\n * @param mergedValue - The result from merging existing and incoming CAIP-25 permission caveat values.\n * @param scopeToDiff - The required or optional scopes from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request.\n * @returns The difference between original and merged CAIP-25 permission caveat values.\n */\nexport function diffScopesForCaip25CaveatValue(\n originalValue: Caip25CaveatValue,\n mergedValue: Caip25CaveatValue,\n scopeToDiff: 'optionalScopes' | 'requiredScopes',\n): Caip25CaveatValue {\n const diff = cloneDeep(originalValue);\n\n const mergedScopeToDiff = mergedValue[scopeToDiff];\n for (const [scopeString, mergedScopeObject] of Object.entries(\n mergedScopeToDiff,\n )) {\n const internalScopeString = scopeString as keyof typeof mergedScopeToDiff;\n const originalScopeObject = diff[scopeToDiff][internalScopeString];\n\n if (originalScopeObject) {\n const newAccounts = mergedScopeObject.accounts.filter(\n (account) => !originalScopeObject?.accounts.includes(account),\n );\n if (newAccounts.length > 0) {\n diff[scopeToDiff][internalScopeString] = {\n accounts: newAccounts,\n };\n continue;\n }\n delete diff[scopeToDiff][internalScopeString];\n } else {\n diff[scopeToDiff][internalScopeString] = mergedScopeObject;\n }\n }\n\n return diff;\n}\n\n/**\n * Checks if every account in the given scopes object is supported.\n *\n * @param scopesObject - The scopes object to iterate over.\n * @param listAccounts - The hook for getting internalAccount objects for all evm accounts.\n * @param getNonEvmAccountAddresses - The hook that returns the supported CAIP-10 account addresses for a non EVM scope.\n * addresses.\n * @returns True if every account in the scopes object is supported, false otherwise.\n */\nfunction isEveryAccountInScopesObjectSupported(\n scopesObject: InternalScopesObject,\n listAccounts: () => { type: string; address: Hex }[],\n getNonEvmAccountAddresses: (scope: CaipChainId) => string[],\n) {\n return Object.values(scopesObject).every((scopeObject) =>\n scopeObject.accounts.every((account) =>\n isSupportedAccount(account, {\n getEvmInternalAccounts: listAccounts,\n getNonEvmAccountAddresses,\n }),\n ),\n );\n}\n\n/**\n * Helper that returns a `authorizedScopes` CAIP-25 caveat specification\n * that can be passed into the PermissionController constructor.\n *\n * @param options - The specification builder options.\n * @param options.findNetworkClientIdByChainId - The hook for getting the networkClientId that serves a chainId.\n * @param options.listAccounts - The hook for getting internalAccount objects for all evm accounts.\n * @param options.isNonEvmScopeSupported - The hook that determines if an non EVM scopeString is supported.\n * @param options.getNonEvmAccountAddresses - The hook that returns the supported CAIP-10 account addresses for a non EVM scope.\n * @returns The specification for the `caip25` caveat.\n */\nexport const caip25CaveatBuilder = ({\n findNetworkClientIdByChainId,\n listAccounts,\n isNonEvmScopeSupported,\n getNonEvmAccountAddresses,\n}: Caip25EndowmentCaveatSpecificationBuilderOptions): EndowmentCaveatSpecificationConstraint &\n Required<\n Pick<EndowmentCaveatSpecificationConstraint, 'validator' | 'merger'>\n > => {\n return {\n type: Caip25CaveatType,\n validator: (\n caveat: { type: typeof Caip25CaveatType; value: unknown },\n _origin?: string,\n _target?: string,\n ) => {\n if (\n !caveat.value ||\n !hasProperty(caveat.value, 'requiredScopes') ||\n !hasProperty(caveat.value, 'optionalScopes') ||\n !hasProperty(caveat.value, 'isMultichainOrigin') ||\n !hasProperty(caveat.value, 'sessionProperties') ||\n typeof caveat.value.isMultichainOrigin !== 'boolean' ||\n !isObject(caveat.value.sessionProperties)\n ) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received invalid value for caveat of type \"${Caip25CaveatType}\".`,\n );\n }\n\n const { requiredScopes, optionalScopes, sessionProperties } =\n caveat.value;\n\n const allSessionPropertiesSupported = Object.keys(\n sessionProperties,\n ).every((sessionProperty) => isSupportedSessionProperty(sessionProperty));\n\n if (!allSessionPropertiesSupported) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received unknown session property(s) for caveat of type \"${Caip25CaveatType}\".`,\n );\n }\n\n assertIsInternalScopesObject(requiredScopes);\n assertIsInternalScopesObject(optionalScopes);\n\n if (\n Object.keys(requiredScopes).length === 0 &&\n Object.keys(optionalScopes).length === 0\n ) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received no scopes for caveat of type \"${Caip25CaveatType}\".`,\n );\n }\n\n const isEvmChainIdSupported = (chainId: Hex) => {\n try {\n findNetworkClientIdByChainId(chainId);\n return true;\n } catch {\n return false;\n }\n };\n\n const unsupportedScopes = Object.keys({\n ...requiredScopes,\n ...optionalScopes,\n }).filter(\n (scopeString) =>\n !isSupportedScopeString(scopeString, {\n isEvmChainIdSupported,\n isNonEvmScopeSupported,\n }),\n );\n\n if (unsupportedScopes.length > 0) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received scopeString value(s): ${unsupportedScopes.join(', ')} for caveat of type \"${Caip25CaveatType}\" that are not supported by the wallet.`,\n );\n }\n\n const allRequiredAccountsSupported =\n isEveryAccountInScopesObjectSupported(\n requiredScopes,\n listAccounts,\n getNonEvmAccountAddresses,\n );\n const allOptionalAccountsSupported =\n isEveryAccountInScopesObjectSupported(\n optionalScopes,\n listAccounts,\n getNonEvmAccountAddresses,\n );\n if (!allRequiredAccountsSupported || !allOptionalAccountsSupported) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received account value(s) for caveat of type \"${Caip25CaveatType}\" that are not supported by the wallet.`,\n );\n }\n },\n merger: (\n leftValue: Caip25CaveatValue,\n rightValue: Caip25CaveatValue,\n ): [Caip25CaveatValue, Caip25CaveatValue] => {\n const mergedRequiredScopes = mergeInternalScopes(\n leftValue.requiredScopes,\n rightValue.requiredScopes,\n );\n const mergedOptionalScopes = mergeInternalScopes(\n leftValue.optionalScopes,\n rightValue.optionalScopes,\n );\n\n const mergedSessionProperties = {\n ...leftValue.sessionProperties,\n ...rightValue.sessionProperties,\n };\n\n const mergedValue: Caip25CaveatValue = {\n requiredScopes: mergedRequiredScopes,\n optionalScopes: mergedOptionalScopes,\n sessionProperties: mergedSessionProperties,\n isMultichainOrigin: leftValue.isMultichainOrigin,\n };\n\n const partialDiff = diffScopesForCaip25CaveatValue(\n leftValue,\n mergedValue,\n 'requiredScopes',\n );\n\n const diff = diffScopesForCaip25CaveatValue(\n partialDiff,\n mergedValue,\n 'optionalScopes',\n );\n\n return [mergedValue, diff];\n },\n };\n};\n\ntype Caip25EndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetName: typeof Caip25EndowmentPermissionName;\n endowmentGetter: (_options?: EndowmentGetterParams) => null;\n validator: PermissionValidatorConstraint;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * Helper that returns a `endowment:caip25` specification that\n * can be passed into the PermissionController constructor.\n *\n * @returns The specification for the `caip25` endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n Record<never, never>,\n Caip25EndowmentSpecification\n> = () => {\n return {\n permissionType: PermissionType.Endowment,\n targetName: Caip25EndowmentPermissionName,\n allowedCaveats: [Caip25CaveatType],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => null,\n validator: (permission: PermissionConstraint) => {\n if (\n permission.caveats?.length !== 1 ||\n permission.caveats?.[0]?.type !== Caip25CaveatType\n ) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Invalid caveats. There must be a single caveat of type \"${Caip25CaveatType}\".`,\n );\n }\n },\n };\n};\n\n/**\n * The `caip25` endowment specification builder. Passed to the\n * `PermissionController` for constructing and validating the\n * `endowment:caip25` permission.\n */\nexport const caip25EndowmentBuilder = Object.freeze({\n targetName: Caip25EndowmentPermissionName,\n specificationBuilder,\n} as const);\n\n/**\n * Factories that construct caveat mutator functions that are passed to\n * PermissionController.updatePermissionsByCaveat.\n */\nexport const Caip25CaveatMutators = {\n [Caip25CaveatType]: {\n removeScope,\n removeAccount,\n },\n};\n\n/**\n * Removes the account from the scope object.\n *\n * @param targetAddress - The address to remove from the scope object.\n * @returns A function that removes the account from the scope object.\n */\nfunction removeAccountFilterFn(targetAddress: string) {\n return (account: CaipAccountId) => {\n const parsed = parseCaipAccountId(account);\n return parsed.address !== targetAddress;\n };\n}\n\n/**\n * Removes the account from the scope object.\n *\n * @param scopeObject - The scope object to remove the account from.\n * @param targetAddress - The address to remove from the scope object.\n */\nfunction removeAccountFromScopeObject(\n scopeObject: InternalScopeObject,\n targetAddress: string,\n) {\n if (scopeObject.accounts) {\n scopeObject.accounts = scopeObject.accounts.filter(\n removeAccountFilterFn(targetAddress),\n );\n }\n}\n\n/**\n * Removes the target account from the scope object.\n *\n * @param caip25CaveatValue - The CAIP-25 permission caveat value from which to remove the account (across all chain scopes).\n * @param targetAddress - The address to remove from the scope object. Not a CAIP-10 formatted address because it will be removed across each chain scope.\n * @returns The updated scope object.\n */\nfunction removeAccount(\n caip25CaveatValue: Caip25CaveatValue,\n targetAddress: Hex,\n) {\n const updatedCaveatValue = cloneDeep(caip25CaveatValue);\n\n [\n updatedCaveatValue.requiredScopes,\n updatedCaveatValue.optionalScopes,\n ].forEach((scopes) => {\n Object.entries(scopes).forEach(([, scopeObject]) => {\n removeAccountFromScopeObject(scopeObject, targetAddress);\n });\n });\n\n const noChange = isEqual(updatedCaveatValue, caip25CaveatValue);\n\n if (noChange) {\n return {\n operation: CaveatMutatorOperation.Noop,\n };\n }\n\n const hasAccounts = [\n ...Object.values(updatedCaveatValue.requiredScopes),\n ...Object.values(updatedCaveatValue.optionalScopes),\n ].some(({ accounts }) => accounts.length > 0);\n\n if (hasAccounts) {\n return {\n operation: CaveatMutatorOperation.UpdateValue,\n value: updatedCaveatValue,\n };\n }\n\n return {\n operation: CaveatMutatorOperation.RevokePermission,\n };\n}\n\n/**\n * Removes the target scope from the value arrays of the given\n * `endowment:caip25` caveat. No-ops if the target scopeString is not in\n * the existing scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 permission caveat value to remove the scope from.\n * @param targetScopeString - The scope that is being removed.\n * @returns The updated CAIP-25 permission caveat value.\n */\nfunction removeScope(\n caip25CaveatValue: Caip25CaveatValue,\n targetScopeString: ExternalScopeString,\n) {\n const newRequiredScopes = Object.entries(\n caip25CaveatValue.requiredScopes,\n ).filter(([scope]) => scope !== targetScopeString);\n const newOptionalScopes = Object.entries(\n caip25CaveatValue.optionalScopes,\n ).filter(([scope]) => {\n return scope !== targetScopeString;\n });\n\n const requiredScopesRemoved =\n newRequiredScopes.length !==\n Object.keys(caip25CaveatValue.requiredScopes).length;\n const optionalScopesRemoved =\n newOptionalScopes.length !==\n Object.keys(caip25CaveatValue.optionalScopes).length;\n\n if (!requiredScopesRemoved && !optionalScopesRemoved) {\n return {\n operation: CaveatMutatorOperation.Noop,\n };\n }\n\n const updatedCaveatValue = {\n ...caip25CaveatValue,\n requiredScopes: Object.fromEntries(newRequiredScopes),\n optionalScopes: Object.fromEntries(newOptionalScopes),\n };\n\n const hasNonWalletScopes = [...newRequiredScopes, ...newOptionalScopes].some(\n ([scopeString]) => {\n const { namespace } = parseScopeString(scopeString);\n return namespace !== KnownCaipNamespace.Wallet;\n },\n );\n\n if (hasNonWalletScopes) {\n return {\n operation: CaveatMutatorOperation.UpdateValue,\n value: updatedCaveatValue,\n };\n }\n\n return {\n operation: CaveatMutatorOperation.RevokePermission,\n };\n}\n\n/**\n * Modifies the requested CAIP-25 permissions object after UI confirmation.\n *\n * @param caip25CaveatValue - The requested CAIP-25 caveat value to modify.\n * @param accountAddresses - The list of permitted eth addresses.\n * @param chainIds - The list of permitted eth chainIds.\n * @returns The updated CAIP-25 caveat value with the permitted accounts and chainIds set.\n */\nexport const generateCaip25Caveat = (\n caip25CaveatValue: Caip25CaveatValue,\n accountAddresses: CaipAccountId[],\n chainIds: CaipChainId[],\n): {\n [Caip25EndowmentPermissionName]: {\n caveats: [{ type: string; value: Caip25CaveatValue }];\n };\n} => {\n const caveatValueWithChains = setChainIdsInCaip25CaveatValue(\n caip25CaveatValue,\n chainIds,\n );\n\n const caveatValueWithAccounts = setNonSCACaipAccountIdsInCaip25CaveatValue(\n caveatValueWithChains,\n accountAddresses,\n );\n\n return {\n [Caip25EndowmentPermissionName]: {\n caveats: [\n {\n type: Caip25CaveatType,\n value: caveatValueWithAccounts,\n },\n ],\n },\n };\n};\n\n/**\n * Helper to get the CAIP-25 caveat from a permission\n *\n * @param [caip25Permission] - The CAIP-25 permission object\n * @param caip25Permission.caveats - The caveats of the CAIP-25 permission\n * @returns The CAIP-25 caveat or undefined if not found\n */\nexport function getCaip25CaveatFromPermission(caip25Permission?: {\n caveats: (\n | {\n type: string;\n value: unknown;\n }\n | {\n type: typeof Caip25CaveatType;\n value: Caip25CaveatValue;\n }\n )[];\n}) {\n return caip25Permission?.caveats.find(\n (caveat) => caveat.type === (Caip25CaveatType as string),\n ) as\n | {\n type: typeof Caip25CaveatType;\n value: Caip25CaveatValue;\n }\n | undefined;\n}\n"]}
|
1
|
+
{"version":3,"file":"caip25Permission.mjs","sourceRoot":"","sources":["../src/caip25Permission.ts"],"names":[],"mappings":"AASA,OAAO,EACL,sBAAsB,EACtB,cAAc,EACf,wCAAwC;AAEzC,OAAO,EACL,WAAW,EACX,kBAAkB,EAClB,kBAAkB,EAClB,QAAQ,EAGT,wBAAwB;;;AAGzB,OAAO,EAAE,0CAA0C,EAAE,0DAAsD;AAC3G,OAAO,EAAE,8BAA8B,EAAE,iEAA6D;AACtG,OAAO,EAAE,4BAA4B,EAAE,2BAAuB;AAC9D,OAAO,EACL,kBAAkB,EAClB,s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ACpC,gBAAiC,EACjC,QAAuB,EAKvB,EAAE;IACF,MAAM,qBAAqB,GAAG,8BAA8B,CAC1D,iBAAiB,EACjB,QAAQ,CACT,CAAC;IAEF,MAAM,uBAAuB,GAAG,0CAA0C,CACxE,qBAAqB,EACrB,gBAAgB,CACjB,CAAC;IAEF,OAAO;QACL,CAAC,6BAA6B,CAAC,EAAE;YAC/B,OAAO,EAAE;gBACP;oBACE,IAAI,EAAE,gBAAgB;oBACtB,KAAK,EAAE,uBAAuB;iBAC/B;aACF;SACF;KACF,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,UAAU,6BAA6B,CAAC,gBAW7C;IACC,OAAO,gBAAgB,EAAE,OAAO,CAAC,IAAI,CACnC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,KAAM,gBAA2B,CAM7C,CAAC;AAChB,CAAC","sourcesContent":["import type { NetworkClientId } from '@metamask/network-controller';\nimport type {\n PermissionSpecificationBuilder,\n EndowmentGetterParams,\n ValidPermissionSpecification,\n PermissionValidatorConstraint,\n PermissionConstraint,\n EndowmentCaveatSpecificationConstraint,\n} from '@metamask/permission-controller';\nimport {\n CaveatMutatorOperation,\n PermissionType,\n} from '@metamask/permission-controller';\nimport type { CaipAccountId, CaipChainId, Json } from '@metamask/utils';\nimport {\n hasProperty,\n KnownCaipNamespace,\n parseCaipAccountId,\n isObject,\n type Hex,\n type NonEmptyArray,\n} from '@metamask/utils';\nimport { cloneDeep, isEqual } from 'lodash';\n\nimport { setNonSCACaipAccountIdsInCaip25CaveatValue } from './operators/caip-permission-operator-accounts';\nimport { setChainIdsInCaip25CaveatValue } from './operators/caip-permission-operator-permittedChains';\nimport { assertIsInternalScopesObject } from './scope/assert';\nimport {\n isSupportedAccount,\n isSupportedScopeString,\n isSupportedSessionProperty,\n} from './scope/supported';\nimport { mergeInternalScopes } from './scope/transform';\nimport {\n parseScopeString,\n type ExternalScopeString,\n type InternalScopeObject,\n type InternalScopesObject,\n} from './scope/types';\n\n/**\n * The CAIP-25 permission caveat value.\n * This permission contains the required and optional scopes and session properties from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request that initiated the permission session.\n * It also contains a boolean (isMultichainOrigin) indicating if the permission session is multichain, which may be needed to determine implicit permissioning.\n */\nexport type Caip25CaveatValue = {\n requiredScopes: InternalScopesObject;\n optionalScopes: InternalScopesObject;\n sessionProperties: Record<string, Json>;\n isMultichainOrigin: boolean;\n};\n\n/**\n * The name of the CAIP-25 permission caveat.\n */\nexport const Caip25CaveatType = 'authorizedScopes';\n\n/**\n * The target name of the CAIP-25 endowment permission.\n */\nexport const Caip25EndowmentPermissionName = 'endowment:caip25';\n\n/**\n * Creates a CAIP-25 permission caveat.\n *\n * @param value - The CAIP-25 permission caveat value.\n * @returns The CAIP-25 permission caveat (now including the type).\n */\nexport const createCaip25Caveat = (value: Caip25CaveatValue) => {\n return {\n type: Caip25CaveatType,\n value,\n };\n};\n\ntype Caip25EndowmentCaveatSpecificationBuilderOptions = {\n findNetworkClientIdByChainId: (chainId: Hex) => NetworkClientId;\n listAccounts: () => { type: string; address: Hex }[];\n isNonEvmScopeSupported: (scope: CaipChainId) => boolean;\n getNonEvmAccountAddresses: (scope: CaipChainId) => string[];\n};\n\n/**\n * Calculates the difference between two provided CAIP-25 permission caveat values, but only considering a single scope property at a time.\n *\n * @param originalValue - The existing CAIP-25 permission caveat value.\n * @param mergedValue - The result from merging existing and incoming CAIP-25 permission caveat values.\n * @param scopeToDiff - The required or optional scopes from the [CAIP-25](https://github.com/ChainAgnostic/CAIPs/blob/main/CAIPs/caip-25.md) request.\n * @returns The difference between original and merged CAIP-25 permission caveat values.\n */\nexport function diffScopesForCaip25CaveatValue(\n originalValue: Caip25CaveatValue,\n mergedValue: Caip25CaveatValue,\n scopeToDiff: 'optionalScopes' | 'requiredScopes',\n): Caip25CaveatValue {\n const diff = cloneDeep(originalValue);\n\n const mergedScopeToDiff = mergedValue[scopeToDiff];\n for (const [scopeString, mergedScopeObject] of Object.entries(\n mergedScopeToDiff,\n )) {\n const internalScopeString = scopeString as keyof typeof mergedScopeToDiff;\n const originalScopeObject = diff[scopeToDiff][internalScopeString];\n\n if (originalScopeObject) {\n const newAccounts = mergedScopeObject.accounts.filter(\n (account) => !originalScopeObject?.accounts.includes(account),\n );\n if (newAccounts.length > 0) {\n diff[scopeToDiff][internalScopeString] = {\n accounts: newAccounts,\n };\n continue;\n }\n delete diff[scopeToDiff][internalScopeString];\n } else {\n diff[scopeToDiff][internalScopeString] = mergedScopeObject;\n }\n }\n\n return diff;\n}\n\n/**\n * Checks if every account in the given scopes object is supported.\n *\n * @param scopesObject - The scopes object to iterate over.\n * @param listAccounts - The hook for getting internalAccount objects for all evm accounts.\n * @param getNonEvmAccountAddresses - The hook that returns the supported CAIP-10 account addresses for a non EVM scope.\n * addresses.\n * @returns True if every account in the scopes object is supported, false otherwise.\n */\nfunction isEveryAccountInScopesObjectSupported(\n scopesObject: InternalScopesObject,\n listAccounts: () => { type: string; address: Hex }[],\n getNonEvmAccountAddresses: (scope: CaipChainId) => string[],\n) {\n return Object.values(scopesObject).every((scopeObject) =>\n scopeObject.accounts.every((account) =>\n isSupportedAccount(account, {\n getEvmInternalAccounts: listAccounts,\n getNonEvmAccountAddresses,\n }),\n ),\n );\n}\n\n/**\n * Helper that returns a `authorizedScopes` CAIP-25 caveat specification\n * that can be passed into the PermissionController constructor.\n *\n * @param options - The specification builder options.\n * @param options.findNetworkClientIdByChainId - The hook for getting the networkClientId that serves a chainId.\n * @param options.listAccounts - The hook for getting internalAccount objects for all evm accounts.\n * @param options.isNonEvmScopeSupported - The hook that determines if an non EVM scopeString is supported.\n * @param options.getNonEvmAccountAddresses - The hook that returns the supported CAIP-10 account addresses for a non EVM scope.\n * @returns The specification for the `caip25` caveat.\n */\nexport const caip25CaveatBuilder = ({\n findNetworkClientIdByChainId,\n listAccounts,\n isNonEvmScopeSupported,\n getNonEvmAccountAddresses,\n}: Caip25EndowmentCaveatSpecificationBuilderOptions): EndowmentCaveatSpecificationConstraint &\n Required<\n Pick<EndowmentCaveatSpecificationConstraint, 'validator' | 'merger'>\n > => {\n return {\n type: Caip25CaveatType,\n validator: (\n caveat: { type: typeof Caip25CaveatType; value: unknown },\n _origin?: string,\n _target?: string,\n ) => {\n if (\n !caveat.value ||\n !hasProperty(caveat.value, 'requiredScopes') ||\n !hasProperty(caveat.value, 'optionalScopes') ||\n !hasProperty(caveat.value, 'isMultichainOrigin') ||\n !hasProperty(caveat.value, 'sessionProperties') ||\n typeof caveat.value.isMultichainOrigin !== 'boolean' ||\n !isObject(caveat.value.sessionProperties)\n ) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received invalid value for caveat of type \"${Caip25CaveatType}\".`,\n );\n }\n\n const { requiredScopes, optionalScopes, sessionProperties } =\n caveat.value;\n\n const allSessionPropertiesSupported = Object.keys(\n sessionProperties,\n ).every((sessionProperty) => isSupportedSessionProperty(sessionProperty));\n\n if (!allSessionPropertiesSupported) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received unknown session property(s) for caveat of type \"${Caip25CaveatType}\".`,\n );\n }\n\n assertIsInternalScopesObject(requiredScopes);\n assertIsInternalScopesObject(optionalScopes);\n\n if (\n Object.keys(requiredScopes).length === 0 &&\n Object.keys(optionalScopes).length === 0\n ) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received no scopes for caveat of type \"${Caip25CaveatType}\".`,\n );\n }\n\n const isEvmChainIdSupported = (chainId: Hex) => {\n try {\n findNetworkClientIdByChainId(chainId);\n return true;\n } catch {\n return false;\n }\n };\n\n const unsupportedScopes = Object.keys({\n ...requiredScopes,\n ...optionalScopes,\n }).filter(\n (scopeString) =>\n !isSupportedScopeString(scopeString, {\n isEvmChainIdSupported,\n isNonEvmScopeSupported,\n }),\n );\n\n if (unsupportedScopes.length > 0) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received scopeString value(s): ${unsupportedScopes.join(', ')} for caveat of type \"${Caip25CaveatType}\" that are not supported by the wallet.`,\n );\n }\n\n const allRequiredAccountsSupported =\n isEveryAccountInScopesObjectSupported(\n requiredScopes,\n listAccounts,\n getNonEvmAccountAddresses,\n );\n const allOptionalAccountsSupported =\n isEveryAccountInScopesObjectSupported(\n optionalScopes,\n listAccounts,\n getNonEvmAccountAddresses,\n );\n if (!allRequiredAccountsSupported || !allOptionalAccountsSupported) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Received account value(s) for caveat of type \"${Caip25CaveatType}\" that are not supported by the wallet.`,\n );\n }\n },\n merger: (\n leftValue: Caip25CaveatValue,\n rightValue: Caip25CaveatValue,\n ): [Caip25CaveatValue, Caip25CaveatValue] => {\n const mergedRequiredScopes = mergeInternalScopes(\n leftValue.requiredScopes,\n rightValue.requiredScopes,\n );\n const mergedOptionalScopes = mergeInternalScopes(\n leftValue.optionalScopes,\n rightValue.optionalScopes,\n );\n\n const mergedSessionProperties = {\n ...leftValue.sessionProperties,\n ...rightValue.sessionProperties,\n };\n\n const mergedValue: Caip25CaveatValue = {\n requiredScopes: mergedRequiredScopes,\n optionalScopes: mergedOptionalScopes,\n sessionProperties: mergedSessionProperties,\n isMultichainOrigin: leftValue.isMultichainOrigin,\n };\n\n const partialDiff = diffScopesForCaip25CaveatValue(\n leftValue,\n mergedValue,\n 'requiredScopes',\n );\n\n const diff = diffScopesForCaip25CaveatValue(\n partialDiff,\n mergedValue,\n 'optionalScopes',\n );\n\n return [mergedValue, diff];\n },\n };\n};\n\ntype Caip25EndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetName: typeof Caip25EndowmentPermissionName;\n endowmentGetter: (_options?: EndowmentGetterParams) => null;\n validator: PermissionValidatorConstraint;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * Helper that returns a `endowment:caip25` specification that\n * can be passed into the PermissionController constructor.\n *\n * @returns The specification for the `caip25` endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n Record<never, never>,\n Caip25EndowmentSpecification\n> = () => {\n return {\n permissionType: PermissionType.Endowment,\n targetName: Caip25EndowmentPermissionName,\n allowedCaveats: [Caip25CaveatType],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => null,\n validator: (permission: PermissionConstraint) => {\n if (\n permission.caveats?.length !== 1 ||\n permission.caveats?.[0]?.type !== Caip25CaveatType\n ) {\n throw new Error(\n `${Caip25EndowmentPermissionName} error: Invalid caveats. There must be a single caveat of type \"${Caip25CaveatType}\".`,\n );\n }\n },\n };\n};\n\n/**\n * The `caip25` endowment specification builder. Passed to the\n * `PermissionController` for constructing and validating the\n * `endowment:caip25` permission.\n */\nexport const caip25EndowmentBuilder = Object.freeze({\n targetName: Caip25EndowmentPermissionName,\n specificationBuilder,\n} as const);\n\n/**\n * Factories that construct caveat mutator functions that are passed to\n * PermissionController.updatePermissionsByCaveat.\n */\nexport const Caip25CaveatMutators = {\n [Caip25CaveatType]: {\n removeScope,\n removeAccount,\n },\n};\n\n/**\n * Removes the account from the scope object.\n *\n * @param targetAddress - The address to remove from the scope object.\n * @returns A function that removes the account from the scope object.\n */\nfunction removeAccountFilterFn(targetAddress: string) {\n return (account: CaipAccountId) => {\n const parsed = parseCaipAccountId(account);\n return parsed.address !== targetAddress;\n };\n}\n\n/**\n * Removes the account from the scope object.\n *\n * @param scopeObject - The scope object to remove the account from.\n * @param targetAddress - The address to remove from the scope object.\n */\nfunction removeAccountFromScopeObject(\n scopeObject: InternalScopeObject,\n targetAddress: string,\n) {\n if (scopeObject.accounts) {\n scopeObject.accounts = scopeObject.accounts.filter(\n removeAccountFilterFn(targetAddress),\n );\n }\n}\n\n/**\n * Removes the target account from the scope object.\n *\n * @param caip25CaveatValue - The CAIP-25 permission caveat value from which to remove the account (across all chain scopes).\n * @param targetAddress - The address to remove from the scope object. Not a CAIP-10 formatted address because it will be removed across each chain scope.\n * @returns The updated scope object.\n */\nfunction removeAccount(\n caip25CaveatValue: Caip25CaveatValue,\n targetAddress: Hex,\n) {\n const updatedCaveatValue = cloneDeep(caip25CaveatValue);\n\n [\n updatedCaveatValue.requiredScopes,\n updatedCaveatValue.optionalScopes,\n ].forEach((scopes) => {\n Object.entries(scopes).forEach(([, scopeObject]) => {\n removeAccountFromScopeObject(scopeObject, targetAddress);\n });\n });\n\n const noChange = isEqual(updatedCaveatValue, caip25CaveatValue);\n\n if (noChange) {\n return {\n operation: CaveatMutatorOperation.Noop,\n };\n }\n\n const hasAccounts = [\n ...Object.values(updatedCaveatValue.requiredScopes),\n ...Object.values(updatedCaveatValue.optionalScopes),\n ].some(({ accounts }) => accounts.length > 0);\n\n if (hasAccounts) {\n return {\n operation: CaveatMutatorOperation.UpdateValue,\n value: updatedCaveatValue,\n };\n }\n\n return {\n operation: CaveatMutatorOperation.RevokePermission,\n };\n}\n\n/**\n * Removes the target scope from the value arrays of the given\n * `endowment:caip25` caveat. No-ops if the target scopeString is not in\n * the existing scopes.\n *\n * @param caip25CaveatValue - The CAIP-25 permission caveat value to remove the scope from.\n * @param targetScopeString - The scope that is being removed.\n * @returns The updated CAIP-25 permission caveat value.\n */\nfunction removeScope(\n caip25CaveatValue: Caip25CaveatValue,\n targetScopeString: ExternalScopeString,\n) {\n const newRequiredScopes = Object.entries(\n caip25CaveatValue.requiredScopes,\n ).filter(([scope]) => scope !== targetScopeString);\n const newOptionalScopes = Object.entries(\n caip25CaveatValue.optionalScopes,\n ).filter(([scope]) => {\n return scope !== targetScopeString;\n });\n\n const requiredScopesRemoved =\n newRequiredScopes.length !==\n Object.keys(caip25CaveatValue.requiredScopes).length;\n const optionalScopesRemoved =\n newOptionalScopes.length !==\n Object.keys(caip25CaveatValue.optionalScopes).length;\n\n if (!requiredScopesRemoved && !optionalScopesRemoved) {\n return {\n operation: CaveatMutatorOperation.Noop,\n };\n }\n\n const updatedCaveatValue = {\n ...caip25CaveatValue,\n requiredScopes: Object.fromEntries(newRequiredScopes),\n optionalScopes: Object.fromEntries(newOptionalScopes),\n };\n\n const hasNonWalletScopes = [...newRequiredScopes, ...newOptionalScopes].some(\n ([scopeString]) => {\n const { namespace } = parseScopeString(scopeString);\n return namespace !== KnownCaipNamespace.Wallet;\n },\n );\n\n if (hasNonWalletScopes) {\n return {\n operation: CaveatMutatorOperation.UpdateValue,\n value: updatedCaveatValue,\n };\n }\n\n return {\n operation: CaveatMutatorOperation.RevokePermission,\n };\n}\n\n/**\n * Modifies the requested CAIP-25 permissions object after UI confirmation.\n *\n * @param caip25CaveatValue - The requested CAIP-25 caveat value to modify.\n * @param accountAddresses - The list of permitted eth addresses.\n * @param chainIds - The list of permitted eth chainIds.\n * @returns The updated CAIP-25 caveat value with the permitted accounts and chainIds set.\n */\nexport const generateCaip25Caveat = (\n caip25CaveatValue: Caip25CaveatValue,\n accountAddresses: CaipAccountId[],\n chainIds: CaipChainId[],\n): {\n [Caip25EndowmentPermissionName]: {\n caveats: [{ type: string; value: Caip25CaveatValue }];\n };\n} => {\n const caveatValueWithChains = setChainIdsInCaip25CaveatValue(\n caip25CaveatValue,\n chainIds,\n );\n\n const caveatValueWithAccounts = setNonSCACaipAccountIdsInCaip25CaveatValue(\n caveatValueWithChains,\n accountAddresses,\n );\n\n return {\n [Caip25EndowmentPermissionName]: {\n caveats: [\n {\n type: Caip25CaveatType,\n value: caveatValueWithAccounts,\n },\n ],\n },\n };\n};\n\n/**\n * Helper to get the CAIP-25 caveat from a permission\n *\n * @param [caip25Permission] - The CAIP-25 permission object\n * @param caip25Permission.caveats - The caveats of the CAIP-25 permission\n * @returns The CAIP-25 caveat or undefined if not found\n */\nexport function getCaip25CaveatFromPermission(caip25Permission?: {\n caveats: (\n | {\n type: string;\n value: unknown;\n }\n | {\n type: typeof Caip25CaveatType;\n value: Caip25CaveatValue;\n }\n )[];\n}) {\n return caip25Permission?.caveats.find(\n (caveat) => caveat.type === (Caip25CaveatType as string),\n ) as\n | {\n type: typeof Caip25CaveatType;\n value: Caip25CaveatValue;\n }\n | undefined;\n}\n"]}
|
package/dist/index.cjs
CHANGED
@@ -1,28 +1,28 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.Caip25Errors = exports.KnownSessionProperties = exports.getCaip25CaveatFromPermission = exports.generateCaip25Caveat = exports.Caip25CaveatMutators = exports.caip25EndowmentBuilder = exports.Caip25EndowmentPermissionName = exports.createCaip25Caveat = exports.Caip25CaveatType = exports.caip25CaveatBuilder = exports.normalizeAndMergeScopes = exports.mergeInternalScopes = exports.mergeNormalizedScopes = exports.mergeScopeObject = exports.normalizeScope = exports.getUniqueArrayItems = exports.parseScopeString = exports.getSupportedScopeObjects = exports.isKnownSessionPropertyValue = exports.KnownWalletScopeString = exports.KnownNotifications = exports.KnownWalletNamespaceRpcMethods = exports.KnownRpcMethods = exports.KnownWalletRpcMethods = exports.assertIsInternalScopeString = exports.isNamespaceInScopesObject = exports.bucketScopes = exports.validateAndNormalizeScopes = exports.getPermittedAccountsForScopes = exports.getSessionScopes = exports.getInternalScopesObject = exports.getAllScopesFromScopesObjects = exports.getAllScopesFromCaip25CaveatValue = exports.getAllScopesFromPermission = exports.getAllNamespacesFromCaip25CaveatValue = exports.addCaipChainIdInCaip25CaveatValue = exports.setChainIdsInCaip25CaveatValue = exports.setPermittedEthChainIds = exports.addPermittedEthChainId = exports.getPermittedEthChainIds = exports.isCaipAccountIdInPermittedAccountIds = exports.isInternalAccountInPermittedAccountIds = exports.getCaipAccountIdsFromCaip25CaveatValue = exports.getCaipAccountIdsFromScopesObjects = exports.setNonSCACaipAccountIdsInCaip25CaveatValue = exports.setEthAccounts = exports.getEthAccounts = void 0;
|
4
|
-
var
|
5
|
-
Object.defineProperty(exports, "getEthAccounts", { enumerable: true, get: function () { return
|
6
|
-
Object.defineProperty(exports, "setEthAccounts", { enumerable: true, get: function () { return
|
7
|
-
Object.defineProperty(exports, "setNonSCACaipAccountIdsInCaip25CaveatValue", { enumerable: true, get: function () { return
|
8
|
-
Object.defineProperty(exports, "getCaipAccountIdsFromScopesObjects", { enumerable: true, get: function () { return
|
9
|
-
Object.defineProperty(exports, "getCaipAccountIdsFromCaip25CaveatValue", { enumerable: true, get: function () { return
|
10
|
-
Object.defineProperty(exports, "isInternalAccountInPermittedAccountIds", { enumerable: true, get: function () { return
|
11
|
-
Object.defineProperty(exports, "isCaipAccountIdInPermittedAccountIds", { enumerable: true, get: function () { return
|
12
|
-
var
|
13
|
-
Object.defineProperty(exports, "getPermittedEthChainIds", { enumerable: true, get: function () { return
|
14
|
-
Object.defineProperty(exports, "addPermittedEthChainId", { enumerable: true, get: function () { return
|
15
|
-
Object.defineProperty(exports, "setPermittedEthChainIds", { enumerable: true, get: function () { return
|
16
|
-
Object.defineProperty(exports, "setChainIdsInCaip25CaveatValue", { enumerable: true, get: function () { return
|
17
|
-
Object.defineProperty(exports, "addCaipChainIdInCaip25CaveatValue", { enumerable: true, get: function () { return
|
18
|
-
Object.defineProperty(exports, "getAllNamespacesFromCaip25CaveatValue", { enumerable: true, get: function () { return
|
19
|
-
Object.defineProperty(exports, "getAllScopesFromPermission", { enumerable: true, get: function () { return
|
20
|
-
Object.defineProperty(exports, "getAllScopesFromCaip25CaveatValue", { enumerable: true, get: function () { return
|
21
|
-
Object.defineProperty(exports, "getAllScopesFromScopesObjects", { enumerable: true, get: function () { return
|
22
|
-
var
|
23
|
-
Object.defineProperty(exports, "getInternalScopesObject", { enumerable: true, get: function () { return
|
24
|
-
Object.defineProperty(exports, "getSessionScopes", { enumerable: true, get: function () { return
|
25
|
-
Object.defineProperty(exports, "getPermittedAccountsForScopes", { enumerable: true, get: function () { return
|
4
|
+
var caip_permission_operator_accounts_1 = require("./operators/caip-permission-operator-accounts.cjs");
|
5
|
+
Object.defineProperty(exports, "getEthAccounts", { enumerable: true, get: function () { return caip_permission_operator_accounts_1.getEthAccounts; } });
|
6
|
+
Object.defineProperty(exports, "setEthAccounts", { enumerable: true, get: function () { return caip_permission_operator_accounts_1.setEthAccounts; } });
|
7
|
+
Object.defineProperty(exports, "setNonSCACaipAccountIdsInCaip25CaveatValue", { enumerable: true, get: function () { return caip_permission_operator_accounts_1.setNonSCACaipAccountIdsInCaip25CaveatValue; } });
|
8
|
+
Object.defineProperty(exports, "getCaipAccountIdsFromScopesObjects", { enumerable: true, get: function () { return caip_permission_operator_accounts_1.getCaipAccountIdsFromScopesObjects; } });
|
9
|
+
Object.defineProperty(exports, "getCaipAccountIdsFromCaip25CaveatValue", { enumerable: true, get: function () { return caip_permission_operator_accounts_1.getCaipAccountIdsFromCaip25CaveatValue; } });
|
10
|
+
Object.defineProperty(exports, "isInternalAccountInPermittedAccountIds", { enumerable: true, get: function () { return caip_permission_operator_accounts_1.isInternalAccountInPermittedAccountIds; } });
|
11
|
+
Object.defineProperty(exports, "isCaipAccountIdInPermittedAccountIds", { enumerable: true, get: function () { return caip_permission_operator_accounts_1.isCaipAccountIdInPermittedAccountIds; } });
|
12
|
+
var caip_permission_operator_permittedChains_1 = require("./operators/caip-permission-operator-permittedChains.cjs");
|
13
|
+
Object.defineProperty(exports, "getPermittedEthChainIds", { enumerable: true, get: function () { return caip_permission_operator_permittedChains_1.getPermittedEthChainIds; } });
|
14
|
+
Object.defineProperty(exports, "addPermittedEthChainId", { enumerable: true, get: function () { return caip_permission_operator_permittedChains_1.addPermittedEthChainId; } });
|
15
|
+
Object.defineProperty(exports, "setPermittedEthChainIds", { enumerable: true, get: function () { return caip_permission_operator_permittedChains_1.setPermittedEthChainIds; } });
|
16
|
+
Object.defineProperty(exports, "setChainIdsInCaip25CaveatValue", { enumerable: true, get: function () { return caip_permission_operator_permittedChains_1.setChainIdsInCaip25CaveatValue; } });
|
17
|
+
Object.defineProperty(exports, "addCaipChainIdInCaip25CaveatValue", { enumerable: true, get: function () { return caip_permission_operator_permittedChains_1.addCaipChainIdInCaip25CaveatValue; } });
|
18
|
+
Object.defineProperty(exports, "getAllNamespacesFromCaip25CaveatValue", { enumerable: true, get: function () { return caip_permission_operator_permittedChains_1.getAllNamespacesFromCaip25CaveatValue; } });
|
19
|
+
Object.defineProperty(exports, "getAllScopesFromPermission", { enumerable: true, get: function () { return caip_permission_operator_permittedChains_1.getAllScopesFromPermission; } });
|
20
|
+
Object.defineProperty(exports, "getAllScopesFromCaip25CaveatValue", { enumerable: true, get: function () { return caip_permission_operator_permittedChains_1.getAllScopesFromCaip25CaveatValue; } });
|
21
|
+
Object.defineProperty(exports, "getAllScopesFromScopesObjects", { enumerable: true, get: function () { return caip_permission_operator_permittedChains_1.getAllScopesFromScopesObjects; } });
|
22
|
+
var caip_permission_operator_session_scopes_1 = require("./operators/caip-permission-operator-session-scopes.cjs");
|
23
|
+
Object.defineProperty(exports, "getInternalScopesObject", { enumerable: true, get: function () { return caip_permission_operator_session_scopes_1.getInternalScopesObject; } });
|
24
|
+
Object.defineProperty(exports, "getSessionScopes", { enumerable: true, get: function () { return caip_permission_operator_session_scopes_1.getSessionScopes; } });
|
25
|
+
Object.defineProperty(exports, "getPermittedAccountsForScopes", { enumerable: true, get: function () { return caip_permission_operator_session_scopes_1.getPermittedAccountsForScopes; } });
|
26
26
|
var authorization_1 = require("./scope/authorization.cjs");
|
27
27
|
Object.defineProperty(exports, "validateAndNormalizeScopes", { enumerable: true, get: function () { return authorization_1.validateAndNormalizeScopes; } });
|
28
28
|
Object.defineProperty(exports, "bucketScopes", { enumerable: true, get: function () { return authorization_1.bucketScopes; } });
|
package/dist/index.cjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,uGAQuD;AAPrD,mIAAA,cAAc,OAAA;AACd,mIAAA,cAAc,OAAA;AACd,+JAAA,0CAA0C,OAAA;AAC1C,uJAAA,kCAAkC,OAAA;AAClC,2JAAA,sCAAsC,OAAA;AACtC,2JAAA,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A;AAE/B,mDAA2D;AAAlD,mHAAA,sBAAsB,OAAA;AAC/B,6CAA8C;AAArC,sGAAA,YAAY,OAAA","sourcesContent":["export {\n getEthAccounts,\n setEthAccounts,\n setNonSCACaipAccountIdsInCaip25CaveatValue,\n getCaipAccountIdsFromScopesObjects,\n getCaipAccountIdsFromCaip25CaveatValue,\n isInternalAccountInPermittedAccountIds,\n isCaipAccountIdInPermittedAccountIds,\n} from './operators/caip-permission-operator-accounts';\nexport {\n getPermittedEthChainIds,\n addPermittedEthChainId,\n setPermittedEthChainIds,\n setChainIdsInCaip25CaveatValue,\n addCaipChainIdInCaip25CaveatValue,\n getAllNamespacesFromCaip25CaveatValue,\n getAllScopesFromPermission,\n getAllScopesFromCaip25CaveatValue,\n getAllScopesFromScopesObjects,\n} from './operators/caip-permission-operator-permittedChains';\nexport {\n getInternalScopesObject,\n getSessionScopes,\n getPermittedAccountsForScopes,\n} from './operators/caip-permission-operator-session-scopes';\nexport type { Caip25Authorization } from './scope/authorization';\nexport {\n validateAndNormalizeScopes,\n bucketScopes,\n isNamespaceInScopesObject,\n} from './scope/authorization';\nexport { assertIsInternalScopeString } from './scope/assert';\nexport {\n KnownWalletRpcMethods,\n KnownRpcMethods,\n KnownWalletNamespaceRpcMethods,\n KnownNotifications,\n KnownWalletScopeString,\n isKnownSessionPropertyValue,\n} from './scope/constants';\nexport { getSupportedScopeObjects } from './scope/filter';\nexport type {\n ExternalScopeString,\n ExternalScopeObject,\n ExternalScopesObject,\n InternalScopeString,\n InternalScopeObject,\n InternalScopesObject,\n NormalizedScopeObject,\n NormalizedScopesObject,\n ScopedProperties,\n NonWalletKnownCaipNamespace,\n} from './scope/types';\nexport { parseScopeString } from './scope/types';\nexport {\n getUniqueArrayItems,\n normalizeScope,\n mergeScopeObject,\n mergeNormalizedScopes,\n mergeInternalScopes,\n normalizeAndMergeScopes,\n} from './scope/transform';\n\nexport type { Caip25CaveatValue } from './caip25Permission';\nexport {\n caip25CaveatBuilder,\n Caip25CaveatType,\n createCaip25Caveat,\n Caip25EndowmentPermissionName,\n caip25EndowmentBuilder,\n Caip25CaveatMutators,\n generateCaip25Caveat,\n getCaip25CaveatFromPermission,\n} from './caip25Permission';\nexport { KnownSessionProperties } from './scope/constants';\nexport { Caip25Errors } from './scope/errors';\n"]}
|
package/dist/index.d.cts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
export { getEthAccounts, setEthAccounts, setNonSCACaipAccountIdsInCaip25CaveatValue, getCaipAccountIdsFromScopesObjects, getCaipAccountIdsFromCaip25CaveatValue, isInternalAccountInPermittedAccountIds, isCaipAccountIdInPermittedAccountIds, } from "./
|
2
|
-
export { getPermittedEthChainIds, addPermittedEthChainId, setPermittedEthChainIds, setChainIdsInCaip25CaveatValue, addCaipChainIdInCaip25CaveatValue, getAllNamespacesFromCaip25CaveatValue, getAllScopesFromPermission, getAllScopesFromCaip25CaveatValue, getAllScopesFromScopesObjects, } from "./
|
3
|
-
export { getInternalScopesObject, getSessionScopes, getPermittedAccountsForScopes, } from "./
|
1
|
+
export { getEthAccounts, setEthAccounts, setNonSCACaipAccountIdsInCaip25CaveatValue, getCaipAccountIdsFromScopesObjects, getCaipAccountIdsFromCaip25CaveatValue, isInternalAccountInPermittedAccountIds, isCaipAccountIdInPermittedAccountIds, } from "./operators/caip-permission-operator-accounts.cjs";
|
2
|
+
export { getPermittedEthChainIds, addPermittedEthChainId, setPermittedEthChainIds, setChainIdsInCaip25CaveatValue, addCaipChainIdInCaip25CaveatValue, getAllNamespacesFromCaip25CaveatValue, getAllScopesFromPermission, getAllScopesFromCaip25CaveatValue, getAllScopesFromScopesObjects, } from "./operators/caip-permission-operator-permittedChains.cjs";
|
3
|
+
export { getInternalScopesObject, getSessionScopes, getPermittedAccountsForScopes, } from "./operators/caip-permission-operator-session-scopes.cjs";
|
4
4
|
export type { Caip25Authorization } from "./scope/authorization.cjs";
|
5
5
|
export { validateAndNormalizeScopes, bucketScopes, isNamespaceInScopesObject, } from "./scope/authorization.cjs";
|
6
6
|
export { assertIsInternalScopeString } from "./scope/assert.cjs";
|
package/dist/index.d.cts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,cAAc,EACd,0CAA0C,EAC1C,kCAAkC,EAClC,sCAAsC,EACtC,sCAAsC,EACtC,oCAAoC,GACrC,
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,cAAc,EACd,0CAA0C,EAC1C,kCAAkC,EAClC,sCAAsC,EACtC,sCAAsC,EACtC,oCAAoC,GACrC,0DAAsD;AACvD,OAAO,EACL,uBAAuB,EACvB,sBAAsB,EACtB,uBAAuB,EACvB,8BAA8B,EAC9B,iCAAiC,EACjC,qCAAqC,EACrC,0BAA0B,EAC1B,iCAAiC,EACjC,6BAA6B,GAC9B,iEAA6D;AAC9D,OAAO,EACL,uBAAuB,EACvB,gBAAgB,EAChB,6BAA6B,GAC9B,gEAA4D;AAC7D,YAAY,EAAE,mBAAmB,EAAE,kCAA8B;AACjE,OAAO,EACL,0BAA0B,EAC1B,YAAY,EACZ,yBAAyB,GAC1B,kCAA8B;AAC/B,OAAO,EAAE,2BAA2B,EAAE,2BAAuB;AAC7D,OAAO,EACL,qBAAqB,EACrB,eAAe,EACf,8BAA8B,EAC9B,kBAAkB,EAClB,sBAAsB,EACtB,2BAA2B,GAC5B,8BAA0B;AAC3B,OAAO,EAAE,wBAAwB,EAAE,2BAAuB;AAC1D,YAAY,EACV,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,EACpB,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,EACpB,qBAAqB,EACrB,sBAAsB,EACtB,gBAAgB,EAChB,2BAA2B,GAC5B,0BAAsB;AACvB,OAAO,EAAE,gBAAgB,EAAE,0BAAsB;AACjD,OAAO,EACL,mBAAmB,EACnB,cAAc,EACd,gBAAgB,EAChB,qBAAqB,EACrB,mBAAmB,EACnB,uBAAuB,GACxB,8BAA0B;AAE3B,YAAY,EAAE,iBAAiB,EAAE,+BAA2B;AAC5D,OAAO,EACL,mBAAmB,EACnB,gBAAgB,EAChB,kBAAkB,EAClB,6BAA6B,EAC7B,sBAAsB,EACtB,oBAAoB,EACpB,oBAAoB,EACpB,6BAA6B,GAC9B,+BAA2B;AAC5B,OAAO,EAAE,sBAAsB,EAAE,8BAA0B;AAC3D,OAAO,EAAE,YAAY,EAAE,2BAAuB"}
|
package/dist/index.d.mts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
export { getEthAccounts, setEthAccounts, setNonSCACaipAccountIdsInCaip25CaveatValue, getCaipAccountIdsFromScopesObjects, getCaipAccountIdsFromCaip25CaveatValue, isInternalAccountInPermittedAccountIds, isCaipAccountIdInPermittedAccountIds, } from "./
|
2
|
-
export { getPermittedEthChainIds, addPermittedEthChainId, setPermittedEthChainIds, setChainIdsInCaip25CaveatValue, addCaipChainIdInCaip25CaveatValue, getAllNamespacesFromCaip25CaveatValue, getAllScopesFromPermission, getAllScopesFromCaip25CaveatValue, getAllScopesFromScopesObjects, } from "./
|
3
|
-
export { getInternalScopesObject, getSessionScopes, getPermittedAccountsForScopes, } from "./
|
1
|
+
export { getEthAccounts, setEthAccounts, setNonSCACaipAccountIdsInCaip25CaveatValue, getCaipAccountIdsFromScopesObjects, getCaipAccountIdsFromCaip25CaveatValue, isInternalAccountInPermittedAccountIds, isCaipAccountIdInPermittedAccountIds, } from "./operators/caip-permission-operator-accounts.mjs";
|
2
|
+
export { getPermittedEthChainIds, addPermittedEthChainId, setPermittedEthChainIds, setChainIdsInCaip25CaveatValue, addCaipChainIdInCaip25CaveatValue, getAllNamespacesFromCaip25CaveatValue, getAllScopesFromPermission, getAllScopesFromCaip25CaveatValue, getAllScopesFromScopesObjects, } from "./operators/caip-permission-operator-permittedChains.mjs";
|
3
|
+
export { getInternalScopesObject, getSessionScopes, getPermittedAccountsForScopes, } from "./operators/caip-permission-operator-session-scopes.mjs";
|
4
4
|
export type { Caip25Authorization } from "./scope/authorization.mjs";
|
5
5
|
export { validateAndNormalizeScopes, bucketScopes, isNamespaceInScopesObject, } from "./scope/authorization.mjs";
|
6
6
|
export { assertIsInternalScopeString } from "./scope/assert.mjs";
|
package/dist/index.d.mts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,cAAc,EACd,0CAA0C,EAC1C,kCAAkC,EAClC,sCAAsC,EACtC,sCAAsC,EACtC,oCAAoC,GACrC,
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,cAAc,EACd,0CAA0C,EAC1C,kCAAkC,EAClC,sCAAsC,EACtC,sCAAsC,EACtC,oCAAoC,GACrC,0DAAsD;AACvD,OAAO,EACL,uBAAuB,EACvB,sBAAsB,EACtB,uBAAuB,EACvB,8BAA8B,EAC9B,iCAAiC,EACjC,qCAAqC,EACrC,0BAA0B,EAC1B,iCAAiC,EACjC,6BAA6B,GAC9B,iEAA6D;AAC9D,OAAO,EACL,uBAAuB,EACvB,gBAAgB,EAChB,6BAA6B,GAC9B,gEAA4D;AAC7D,YAAY,EAAE,mBAAmB,EAAE,kCAA8B;AACjE,OAAO,EACL,0BAA0B,EAC1B,YAAY,EACZ,yBAAyB,GAC1B,kCAA8B;AAC/B,OAAO,EAAE,2BAA2B,EAAE,2BAAuB;AAC7D,OAAO,EACL,qBAAqB,EACrB,eAAe,EACf,8BAA8B,EAC9B,kBAAkB,EAClB,sBAAsB,EACtB,2BAA2B,GAC5B,8BAA0B;AAC3B,OAAO,EAAE,wBAAwB,EAAE,2BAAuB;AAC1D,YAAY,EACV,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,EACpB,mBAAmB,EACnB,mBAAmB,EACnB,oBAAoB,EACpB,qBAAqB,EACrB,sBAAsB,EACtB,gBAAgB,EAChB,2BAA2B,GAC5B,0BAAsB;AACvB,OAAO,EAAE,gBAAgB,EAAE,0BAAsB;AACjD,OAAO,EACL,mBAAmB,EACnB,cAAc,EACd,gBAAgB,EAChB,qBAAqB,EACrB,mBAAmB,EACnB,uBAAuB,GACxB,8BAA0B;AAE3B,YAAY,EAAE,iBAAiB,EAAE,+BAA2B;AAC5D,OAAO,EACL,mBAAmB,EACnB,gBAAgB,EAChB,kBAAkB,EAClB,6BAA6B,EAC7B,sBAAsB,EACtB,oBAAoB,EACpB,oBAAoB,EACpB,6BAA6B,GAC9B,+BAA2B;AAC5B,OAAO,EAAE,sBAAsB,EAAE,8BAA0B;AAC3D,OAAO,EAAE,YAAY,EAAE,2BAAuB"}
|
package/dist/index.mjs
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
export { getEthAccounts, setEthAccounts, setNonSCACaipAccountIdsInCaip25CaveatValue, getCaipAccountIdsFromScopesObjects, getCaipAccountIdsFromCaip25CaveatValue, isInternalAccountInPermittedAccountIds, isCaipAccountIdInPermittedAccountIds } from "./
|
2
|
-
export { getPermittedEthChainIds, addPermittedEthChainId, setPermittedEthChainIds, setChainIdsInCaip25CaveatValue, addCaipChainIdInCaip25CaveatValue, getAllNamespacesFromCaip25CaveatValue, getAllScopesFromPermission, getAllScopesFromCaip25CaveatValue, getAllScopesFromScopesObjects } from "./
|
3
|
-
export { getInternalScopesObject, getSessionScopes, getPermittedAccountsForScopes } from "./
|
1
|
+
export { getEthAccounts, setEthAccounts, setNonSCACaipAccountIdsInCaip25CaveatValue, getCaipAccountIdsFromScopesObjects, getCaipAccountIdsFromCaip25CaveatValue, isInternalAccountInPermittedAccountIds, isCaipAccountIdInPermittedAccountIds } from "./operators/caip-permission-operator-accounts.mjs";
|
2
|
+
export { getPermittedEthChainIds, addPermittedEthChainId, setPermittedEthChainIds, setChainIdsInCaip25CaveatValue, addCaipChainIdInCaip25CaveatValue, getAllNamespacesFromCaip25CaveatValue, getAllScopesFromPermission, getAllScopesFromCaip25CaveatValue, getAllScopesFromScopesObjects } from "./operators/caip-permission-operator-permittedChains.mjs";
|
3
|
+
export { getInternalScopesObject, getSessionScopes, getPermittedAccountsForScopes } from "./operators/caip-permission-operator-session-scopes.mjs";
|
4
4
|
export { validateAndNormalizeScopes, bucketScopes, isNamespaceInScopesObject } from "./scope/authorization.mjs";
|
5
5
|
export { assertIsInternalScopeString } from "./scope/assert.mjs";
|
6
6
|
export { KnownWalletRpcMethods, KnownRpcMethods, KnownWalletNamespaceRpcMethods, KnownNotifications, KnownWalletScopeString, isKnownSessionPropertyValue } from "./scope/constants.mjs";
|
package/dist/index.mjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,cAAc,EACd,0CAA0C,EAC1C,kCAAkC,EAClC,sCAAsC,EACtC,sCAAsC,EACtC,oCAAoC,EACrC,
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EACd,cAAc,EACd,0CAA0C,EAC1C,kCAAkC,EAClC,sCAAsC,EACtC,sCAAsC,EACtC,oCAAoC,EACrC,0DAAsD;AACvD,OAAO,EACL,uBAAuB,EACvB,sBAAsB,EACtB,uBAAuB,EACvB,8BAA8B,EAC9B,iCAAiC,EACjC,qCAAqC,EACrC,0BAA0B,EAC1B,iCAAiC,EACjC,6BAA6B,EAC9B,iEAA6D;AAC9D,OAAO,EACL,uBAAuB,EACvB,gBAAgB,EAChB,6BAA6B,EAC9B,gEAA4D;AAE7D,OAAO,EACL,0BAA0B,EAC1B,YAAY,EACZ,yBAAyB,EAC1B,kCAA8B;AAC/B,OAAO,EAAE,2BAA2B,EAAE,2BAAuB;AAC7D,OAAO,EACL,qBAAqB,EACrB,eAAe,EACf,8BAA8B,EAC9B,kBAAkB,EAClB,sBAAsB,EACtB,2BAA2B,EAC5B,8BAA0B;AAC3B,OAAO,EAAE,wBAAwB,EAAE,2BAAuB;AAa1D,OAAO,EAAE,gBAAgB,EAAE,0BAAsB;AACjD,OAAO,EACL,mBAAmB,EACnB,cAAc,EACd,gBAAgB,EAChB,qBAAqB,EACrB,mBAAmB,EACnB,uBAAuB,EACxB,8BAA0B;AAG3B,OAAO,EACL,mBAAmB,EACnB,gBAAgB,EAChB,kBAAkB,EAClB,6BAA6B,EAC7B,sBAAsB,EACtB,oBAAoB,EACpB,oBAAoB,EACpB,6BAA6B,EAC9B,+BAA2B;AAC5B,OAAO,EAAE,sBAAsB,EAAE,8BAA0B;AAC3D,OAAO,EAAE,YAAY,EAAE,2BAAuB","sourcesContent":["export {\n getEthAccounts,\n setEthAccounts,\n setNonSCACaipAccountIdsInCaip25CaveatValue,\n getCaipAccountIdsFromScopesObjects,\n getCaipAccountIdsFromCaip25CaveatValue,\n isInternalAccountInPermittedAccountIds,\n isCaipAccountIdInPermittedAccountIds,\n} from './operators/caip-permission-operator-accounts';\nexport {\n getPermittedEthChainIds,\n addPermittedEthChainId,\n setPermittedEthChainIds,\n setChainIdsInCaip25CaveatValue,\n addCaipChainIdInCaip25CaveatValue,\n getAllNamespacesFromCaip25CaveatValue,\n getAllScopesFromPermission,\n getAllScopesFromCaip25CaveatValue,\n getAllScopesFromScopesObjects,\n} from './operators/caip-permission-operator-permittedChains';\nexport {\n getInternalScopesObject,\n getSessionScopes,\n getPermittedAccountsForScopes,\n} from './operators/caip-permission-operator-session-scopes';\nexport type { Caip25Authorization } from './scope/authorization';\nexport {\n validateAndNormalizeScopes,\n bucketScopes,\n isNamespaceInScopesObject,\n} from './scope/authorization';\nexport { assertIsInternalScopeString } from './scope/assert';\nexport {\n KnownWalletRpcMethods,\n KnownRpcMethods,\n KnownWalletNamespaceRpcMethods,\n KnownNotifications,\n KnownWalletScopeString,\n isKnownSessionPropertyValue,\n} from './scope/constants';\nexport { getSupportedScopeObjects } from './scope/filter';\nexport type {\n ExternalScopeString,\n ExternalScopeObject,\n ExternalScopesObject,\n InternalScopeString,\n InternalScopeObject,\n InternalScopesObject,\n NormalizedScopeObject,\n NormalizedScopesObject,\n ScopedProperties,\n NonWalletKnownCaipNamespace,\n} from './scope/types';\nexport { parseScopeString } from './scope/types';\nexport {\n getUniqueArrayItems,\n normalizeScope,\n mergeScopeObject,\n mergeNormalizedScopes,\n mergeInternalScopes,\n normalizeAndMergeScopes,\n} from './scope/transform';\n\nexport type { Caip25CaveatValue } from './caip25Permission';\nexport {\n caip25CaveatBuilder,\n Caip25CaveatType,\n createCaip25Caveat,\n Caip25EndowmentPermissionName,\n caip25EndowmentBuilder,\n Caip25CaveatMutators,\n generateCaip25Caveat,\n getCaip25CaveatFromPermission,\n} from './caip25Permission';\nexport { KnownSessionProperties } from './scope/constants';\nexport { Caip25Errors } from './scope/errors';\n"]}
|
@@ -268,6 +268,13 @@ function isAddressWithParsedScopesInPermittedAccountIds(address, parsedAccountSc
|
|
268
268
|
* @returns True if the account is connected to any permitted account
|
269
269
|
*/
|
270
270
|
function isInternalAccountInPermittedAccountIds(internalAccount, permittedAccounts) {
|
271
|
+
// temporary fix for the issue where the internal account has no scopes and or scopes is undefined
|
272
|
+
// TODO: remove this once the bug is fixed (tracked here: https://github.com/MetaMask/accounts-planning/issues/941)
|
273
|
+
// there is currently a bug where an account associated with a snap can fail to add scopes to the internal account in time
|
274
|
+
// before we attempt to access this state
|
275
|
+
if (!internalAccount?.scopes?.length) {
|
276
|
+
return false;
|
277
|
+
}
|
271
278
|
const parsedInteralAccountScopes = internalAccount.scopes.map((scope) => {
|
272
279
|
return (0, types_1.parseScopeString)(scope);
|
273
280
|
});
|
@@ -287,4 +294,4 @@ function isCaipAccountIdInPermittedAccountIds(accountId, permittedAccounts) {
|
|
287
294
|
return isAddressWithParsedScopesInPermittedAccountIds(address, [chain], permittedAccounts);
|
288
295
|
}
|
289
296
|
exports.isCaipAccountIdInPermittedAccountIds = isCaipAccountIdInPermittedAccountIds;
|
290
|
-
//# sourceMappingURL=caip-permission-
|
297
|
+
//# sourceMappingURL=caip-permission-operator-accounts.cjs.map
|