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;;;;;GAOG;AACH,SAAgB,kBAAkB,CAChC,KAAW;IAEX,OAAO;QACL,OAAO,EAAE;YACP;gBACE,IAAI,EAAE,4BAAc,CAAC,SAAS;gBAC9B,KAAK;aACN;SACF;KACF,CAAC;AACJ,CAAC;AAXD,gDAWC;AAED;;;;;;GAMG;AACH,SAAgB,mBAAmB,CACjC,UAAiC;IAEjC,MAAM,OAAO,GAAG,UAAU,EAAE,OAAO,EAAE,MAAM,CACzC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,4BAAc,CAAC,SAAS,CACrD,CAAC;IACF,IAAA,cAAM,EAAC,OAAO,CAAC,CAAC;IAChB,IAAA,cAAM,EAAC,OAAO,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC;IAE7B,MAAM,MAAM,GAAG,OAAO,CAAC,CAAC,CAA+B,CAAC;IACxD,OAAO,MAAM,CAAC,KAAK,CAAC;AACtB,CAAC;AAXD,kDAWC;AAEY,QAAA,uBAAuB,GAGhC;IACF,CAAC,4BAAc,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC;QACxC,IAAI,EAAE,4BAAc,CAAC,SAAS;QAC9B,SAAS,EAAE,CAAC,MAA2B,EAAE,EAAE,CAAC,qBAAqB,CAAC,MAAM,CAAC;KAC1E,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 { RpcOrigins } from '@metamask/snaps-utils';\nimport { assertIsRpcOrigins, SnapCaveatType } from '@metamask/snaps-utils';\nimport type { Json, NonEmptyArray } from '@metamask/utils';\nimport { hasProperty, isPlainObject, assert } from '@metamask/utils';\n\nimport { createGenericPermissionValidator } from './caveats';\nimport { SnapEndowments } from './enum';\n\nconst targetName = SnapEndowments.Rpc;\n\ntype RpcSpecification = ValidPermissionSpecification<{\n  permissionType: PermissionType.Endowment;\n  targetName: typeof targetName;\n  endowmentGetter: (_options?: any) => null;\n  allowedCaveats: Readonly<NonEmptyArray<string>> | null;\n  validator: PermissionValidatorConstraint;\n  subjectTypes: readonly SubjectType[];\n}>;\n\n// TODO: Either fix this lint violation or explain why it's necessary to\n//  ignore.\n// eslint-disable-next-line @typescript-eslint/no-empty-object-type\ntype RpcSpecificationBuilderOptions = {\n  // Empty for now.\n};\n\n/**\n * The specification builder for the JSON-RPC endowment permission.\n *\n * @param _builderOptions - Optional specification builder options.\n * @returns The specification for the JSON-RPC endowment permission.\n */\nconst specificationBuilder: PermissionSpecificationBuilder<\n  PermissionType.Endowment,\n  RpcSpecificationBuilderOptions,\n  RpcSpecification\n> = (_builderOptions?: any): RpcSpecification => {\n  return {\n    permissionType: PermissionType.Endowment,\n    targetName,\n    allowedCaveats: [SnapCaveatType.RpcOrigin, SnapCaveatType.MaxRequestTime],\n    endowmentGetter: (_getterOptions?: EndowmentGetterParams) => null,\n    validator: createGenericPermissionValidator([\n      { type: SnapCaveatType.RpcOrigin },\n      { type: SnapCaveatType.MaxRequestTime, optional: true },\n    ]),\n    subjectTypes: [SubjectType.Snap],\n  };\n};\n\nexport const rpcEndowmentBuilder = Object.freeze({\n  targetName,\n  specificationBuilder,\n} as const);\n\n/**\n * Validate the value of a caveat. This does not validate the type of the\n * caveat itself, only the value of the caveat.\n *\n * @param caveat - The caveat to validate.\n * @throws If the caveat value is invalid.\n */\nfunction validateCaveatOrigins(caveat: Caveat<string, any>) {\n  if (!hasProperty(caveat, 'value') || !isPlainObject(caveat.value)) {\n    throw rpcErrors.invalidParams({\n      message: 'Invalid JSON-RPC origins: Expected a plain object.',\n    });\n  }\n\n  const { value } = caveat;\n  assertIsRpcOrigins(value, rpcErrors.invalidParams);\n}\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 getRpcCaveatMapper(\n  value: Json,\n): Pick<PermissionConstraint, 'caveats'> {\n  return {\n    caveats: [\n      {\n        type: SnapCaveatType.RpcOrigin,\n        value,\n      },\n    ],\n  };\n}\n\n/**\n * Getter function to get the {@link RpcOrigins} caveat value from a permission.\n *\n * @param permission - The permission to get the caveat value from.\n * @returns The caveat value.\n * @throws If the permission does not have a valid {@link RpcOrigins} caveat.\n */\nexport function getRpcCaveatOrigins(\n  permission?: PermissionConstraint,\n): RpcOrigins | null {\n  const caveats = permission?.caveats?.filter(\n    (caveat) => caveat.type === SnapCaveatType.RpcOrigin,\n  );\n  assert(caveats);\n  assert(caveats.length === 1);\n\n  const caveat = caveats[0] as Caveat<string, RpcOrigins>;\n  return caveat.value;\n}\n\nexport const rpcCaveatSpecifications: Record<\n  SnapCaveatType.RpcOrigin,\n  CaveatSpecificationConstraint\n> = {\n  [SnapCaveatType.RpcOrigin]: Object.freeze({\n    type: SnapCaveatType.RpcOrigin,\n    validator: (caveat: Caveat<string, any>) => validateCaveatOrigins(caveat),\n  }),\n};\n"]}