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AC;IAC3B,CAAC;IAED,OAAO;QACL,OAAO,EAAE;YACP;gBACE,IAAI,EAAE,cAAc,CAAC,eAAe;gBACpC,KAAK;aACN;SACF;KACF,CAAC;AACJ,CAAC;AAID;;;;;;GAMG;AACH,MAAM,UAAU,yBAAyB,CACvC,UAAiC;IAEjC,MAAM,MAAM,GAAG,UAAU,EAAE,OAAO,EAAE,IAAI,CACtC,CAAC,UAAU,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,KAAK,cAAc,CAAC,eAAe,CACL,CAAC;IAEhE,OAAO,MAAM,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;AAC7D,CAAC;AAED;;;;;GAKG;AACH,SAAS,cAAc,CAAC,MAA2B;IACjD,IAAI,CAAC,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;QACvD,MAAM,SAAS,CAAC,aAAa,CAAC;YAC5B,OAAO,EAAE,qBAAqB;SAC/B,CAAC,CAAC;IACL,CAAC;IAED,MAAM,EAAE,KAAK,EAAE,GAAG,MAAM,CAAC;IACzB,YAAY,CACV,KAAK,EACL,qBAAqB,EACrB,oCAAoC,EACpC,SAAS,CAAC,aAAa,CACxB,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,MAAM,6BAA6B,GAGtC;IACF,CAAC,cAAc,CAAC,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QAC9C,IAAI,EAAE,cAAc,CAAC,eAAe;QACpC,SAAS,EAAE,CAAC,MAA2B,EAAE,EAAE,CAAC,cAAc,CAAC,MAAM,CAAC;KACnE,CAAC;CACH,CAAC","sourcesContent":["import type {\n Caveat,\n CaveatSpecificationConstraint,\n EndowmentGetterParams,\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 type { MessengerActionsStruct } from '@metamask/snaps-utils';\nimport { MessengerScopesStruct, SnapCaveatType } from '@metamask/snaps-utils';\nimport type { Infer } from '@metamask/superstruct';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\nimport { assertStruct, hasProperty, isObject } from '@metamask/utils';\n\nimport { createGenericPermissionValidator } from './caveats';\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.Messenger;\n\ntype MessengerEndowmentSpecification = 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:messenger` returns nothing; it is intended to be used as a\n * flag by the Snaps Platform to detect whether a Snap has the capability to\n * use the messenger API.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the messenger endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n PermissionType.Endowment,\n any,\n MessengerEndowmentSpecification\n> = (_builderOptions?: unknown) => {\n return {\n permissionType: PermissionType.Endowment,\n targetName: permissionName,\n allowedCaveats: [SnapCaveatType.MessengerScopes],\n endowmentGetter: (_getterOptions?: EndowmentGetterParams) => null,\n validator: createGenericPermissionValidator([\n { type: SnapCaveatType.MessengerScopes },\n ]),\n subjectTypes: [SubjectType.Snap],\n };\n};\n\nexport const messengerEndowmentBuilder = 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 getMessengerCaveatMapper(\n value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n if (!value || !isObject(value) || Object.keys(value).length === 0) {\n return { caveats: null };\n }\n\n return {\n caveats: [\n {\n type: SnapCaveatType.MessengerScopes,\n value,\n },\n ],\n };\n}\n\nexport type MessengerActions = Infer<typeof MessengerActionsStruct>;\n\n/**\n * Getter function to get the {@link MessengerActions} 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 getMessengerCaveatActions(\n permission?: PermissionConstraint,\n): MessengerActions | null {\n const caveat = permission?.caveats?.find(\n (permCaveat) => permCaveat.type === SnapCaveatType.MessengerScopes,\n ) as Caveat<string, { actions?: MessengerActions }> | undefined;\n\n return caveat?.value.actions ? caveat.value.actions : 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') || !isObject(caveat)) {\n throw rpcErrors.invalidParams({\n message: 'Expected an object.',\n });\n }\n\n const { value } = caveat;\n assertStruct(\n value,\n MessengerScopesStruct,\n 'Invalid messenger scopes specified',\n rpcErrors.invalidParams,\n );\n}\n\nexport const messengerCaveatSpecifications: Record<\n SnapCaveatType.MessengerScopes,\n CaveatSpecificationConstraint\n> = {\n [SnapCaveatType.MessengerScopes]: Object.freeze({\n type: SnapCaveatType.MessengerScopes,\n validator: (caveat: Caveat<string, any>) => validateCaveat(caveat),\n }),\n};\n"]}