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entGetterParams,\n PermissionConstraint,\n PermissionSpecificationBuilder,\n PermissionValidatorConstraint,\n ValidPermissionSpecification,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport { ProtocolScopesStruct, SnapCaveatType } from '@metamask/snaps-utils';\nimport type { Infer } from '@metamask/superstruct';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\nimport {\n assertStruct,\n hasProperty,\n isObject,\n isPlainObject,\n} from '@metamask/utils';\n\nimport { createGenericPermissionValidator } from './caveats';\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.Protocol;\n\ntype ProtocolEndowmentSpecification = ValidPermissionSpecification<{\n permissionType: PermissionType.Endowment;\n targetName: typeof permissionName;\n endowmentGetter: (_options?: EndowmentGetterParams) => null;\n allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n validator: PermissionValidatorConstraint;\n subjectTypes: readonly SubjectType[];\n}>;\n\n/**\n * `endowment:protocol` returns nothing; it is intended to be used as a flag\n * by the client to detect whether the Snap supports the Protocol API.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the accounts chain endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n any,\n ProtocolEndowmentSpecification\n> = (_builderOptions?: unknown) => {\n return {\n permissionType: PermissionType.Endowment,\n targetName: permissionName,\n allowedCaveats: [\n SnapCaveatType.ProtocolSnapScopes,\n SnapCaveatType.MaxRequestTime,\n ],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => null,\n validator: createGenericPermissionValidator([\n { type: SnapCaveatType.ProtocolSnapScopes },\n { type: SnapCaveatType.MaxRequestTime, optional: true },\n ]),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nexport const protocolEndowmentBuilder = Object.freeze({\n targetName: permissionName,\n specificationBuilder,\n} as const);\n\n/**\n * Map a raw value from the `initialPermissions` to a caveat specification.\n * Note that this function does not do any validation, that's handled by the\n * PermissionsController when the permission is requested.\n *\n * @param value - The raw value from the `initialPermissions`.\n * @returns The caveat specification.\n */\nexport function getProtocolCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n if (!value || !isObject(value) || Object.keys(value).length === 0) {\n return { caveats: null };\n }\n\n const caveats = [];\n\n if (value.scopes) {\n caveats.push({\n type: SnapCaveatType.ProtocolSnapScopes,\n value: value.scopes,\n });\n }\n\n return { caveats: caveats as NonEmptyArray<CaveatConstraint> };\n}\n\nexport type ProtocolScopes = Infer<typeof ProtocolScopesStruct>;\n\n/**\n * Getter function to get the {@link ProtocolSnapScopes} caveat value from a\n * permission.\n *\n * @param permission - The permission to get the caveat value from.\n * @returns The caveat value.\n */\nexport function getProtocolCaveatScopes(\n permission?: PermissionConstraint,\n): ProtocolScopes | null {\n const caveat = permission?.caveats?.find(\n (permCaveat) => permCaveat.type === SnapCaveatType.ProtocolSnapScopes,\n ) as Caveat<string, ProtocolScopes> | undefined;\n\n return caveat ? caveat.value : null;\n}\n\n/**\n * Validates the type of the caveat value.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat value is invalid.\n */\nfunction validateCaveat(caveat: Caveat<string, any>): void {\n if (!hasProperty(caveat, 'value') || !isPlainObject(caveat)) {\n throw rpcErrors.invalidParams({\n message: 'Expected a plain object.',\n });\n }\n\n const { value } = caveat;\n assertStruct(\n value,\n ProtocolScopesStruct,\n 'Invalid scopes specified',\n rpcErrors.invalidParams,\n );\n}\n\nexport const protocolCaveatSpecifications: Record<\n SnapCaveatType.ProtocolSnapScopes,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.ProtocolSnapScopes]: Object.freeze({\n type: SnapCaveatType.ProtocolSnapScopes,\n validator: (caveat: Caveat<string, any>) => validateCaveat(caveat),\n }),\n};\n"]}