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;AACU,QAAA,2BAA2B,GAGpC;IACF,CAAC,4BAAc,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QAC1C,IAAI,EAAE,4BAAc,CAAC,WAAW;QAChC,SAAS,EAAE,CAAC,MAAM,EAAE,EAAE,CAAC,qBAAqB,CAAC,MAAM,CAAC;KACrD,CAAC;CACH,CAAC","sourcesContent":["import type {\n  PermissionSpecificationBuilder,\n  EndowmentGetterParams,\n  ValidPermissionSpecification,\n  PermissionConstraint,\n  Caveat,\n  CaveatSpecificationConstraint,\n} from '@metamask/permission-controller';\nimport { PermissionType, SubjectType } from '@metamask/permission-controller';\nimport { rpcErrors } from '@metamask/rpc-errors';\nimport type { CronjobSpecification } from '@metamask/snaps-utils';\nimport {\n  SnapCaveatType,\n  isCronjobSpecificationArray,\n} from '@metamask/snaps-utils';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\nimport { assert, hasProperty, isObject, isPlainObject } from '@metamask/utils';\n\nimport { createGenericPermissionValidator } from './caveats';\nimport { SnapEndowments } from './enum';\n\nconst permissionName = SnapEndowments.Cronjob;\n\ntype CronjobEndowmentSpecification = ValidPermissionSpecification<{\n  permissionType: PermissionType.Endowment;\n  targetName: typeof permissionName;\n  endowmentGetter: (_options?: any) => null;\n  allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n}>;\n\n/**\n * `endowment:cronjob` returns nothing; it is intended to be used as a flag to determine whether the snap wants to run cronjobs.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the cronjob endowment.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n  PermissionType.Endowment,\n  any,\n  CronjobEndowmentSpecification\n> = (_builderOptions?: any) => {\n  return {\n    permissionType: PermissionType.Endowment,\n    targetName: permissionName,\n    allowedCaveats: [SnapCaveatType.SnapCronjob],\n    endowmentGetter: (_getterOptions?: EndowmentGetterParams) => null,\n    subjectTypes: [SubjectType.Snap],\n    validator: createGenericPermissionValidator([\n      { type: SnapCaveatType.SnapCronjob, optional: true },\n      { type: SnapCaveatType.MaxRequestTime, optional: true },\n    ]),\n  };\n};\n\nexport const cronjobEndowmentBuilder = 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 getCronjobCaveatMapper(\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.SnapCronjob,\n        value,\n      },\n    ],\n  };\n}\n\n/**\n * Getter function to get the cronjobs from a permission.\n *\n * This does basic validation of the caveat, but does not validate the type or\n * value of the namespaces object itself, as this is handled by the\n * `PermissionsController` when the permission is requested.\n *\n * @param permission - The permission to get the keyring namespaces from.\n * @returns The cronjobs, or `null` if the permission does not have a\n * cronjob caveat.\n */\nexport function getCronjobCaveatJobs(\n  permission?: PermissionConstraint,\n): CronjobSpecification[] | null {\n  if (!permission?.caveats) {\n    return null;\n  }\n\n  assert(permission.caveats.length === 1);\n  assert(permission.caveats[0].type === SnapCaveatType.SnapCronjob);\n\n  const caveat = permission.caveats[0] as Caveat<string, { jobs: Json[] }>;\n\n  return (caveat.value?.jobs as CronjobSpecification[]) ?? null;\n}\n\n/**\n * Validate the cronjob specification values associated with a caveat.\n * This validates that the value is a non-empty array with valid\n * cronjob expression and request object.\n *\n * @param caveat - The caveat to validate.\n * @throws If the value is invalid.\n */\nexport function validateCronjobCaveat(caveat: Caveat<string, any>) {\n  if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n    throw rpcErrors.invalidParams({\n      message: 'Expected a plain object.',\n    });\n  }\n\n  const { value } = caveat;\n\n  if (!hasProperty(value, 'jobs') || !isPlainObject(value)) {\n    throw rpcErrors.invalidParams({\n      message: 'Expected a plain object.',\n    });\n  }\n\n  if (!isCronjobSpecificationArray(value.jobs)) {\n    throw rpcErrors.invalidParams({\n      message: 'Expected a valid cronjob specification array.',\n    });\n  }\n}\n\n/**\n * Caveat specification for the Cronjob.\n */\nexport const cronjobCaveatSpecifications: Record<\n  SnapCaveatType.SnapCronjob,\n  CaveatSpecificationConstraint\n> = {\n  [SnapCaveatType.SnapCronjob]: Object.freeze({\n    type: SnapCaveatType.SnapCronjob,\n    validator: (caveat) => validateCronjobCaveat(caveat),\n  }),\n};\n"]}