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AAe;IACzC,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,MAAM,EAAE,8BAA8B,CAAC,CAAC;IACxD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,WAAW,EAAE,CAAC;YACjC,MAAM,SAAS,CAAC,aAAa,CAAC;gBAC5B,OAAO,EAAE,mBAAmB,KAAK,CAAC,OAAO,GAAG;aAC7C,CAAC,CAAC;QACL,CAAC;QACD,0BAA0B;QAC1B,MAAM,SAAS,CAAC,QAAQ,EAAE,CAAC;IAC7B,CAAC;AACH,CAAC","sourcesContent":["import type {\n  JsonRpcEngineEndCallback,\n  MethodHandler,\n} from '@metamask/json-rpc-engine';\nimport type { Messenger } from '@metamask/messenger';\nimport type { PermissionControllerHasPermissionAction } from '@metamask/permission-controller';\nimport { providerErrors, rpcErrors } from '@metamask/rpc-errors';\nimport type {\n  CloseWebSocketParams,\n  CloseWebSocketResult,\n} from '@metamask/snaps-sdk';\nimport type { InferMatching } from '@metamask/snaps-utils';\nimport { create, object, string, StructError } from '@metamask/superstruct';\nimport type { PendingJsonRpcResponse } from '@metamask/utils';\n\nimport { SnapEndowments } from '../endowments';\nimport type {\n  JsonRpcRequestWithOrigin,\n  WebSocketServiceCloseAction,\n} from '../types';\n\nexport type CloseWebSocketMethodActions =\n  | PermissionControllerHasPermissionAction\n  | WebSocketServiceCloseAction;\n\nconst CloseWebSocketParametersStruct = object({\n  id: string(),\n});\n\nexport type CloseWebSocketParameters = InferMatching<\n  typeof CloseWebSocketParametersStruct,\n  CloseWebSocketParams\n>;\n\n/**\n * Closes a WebSocket connection that was previously opened with\n * [`snap_openWebSocket`](https://docs.metamask.io/snaps/reference/snaps-api/snap_openwebsocket).\n *\n * @example\n * ```json name=\"Manifest\"\n * {\n *   \"initialPermissions\": {\n *     \"endowment:network-access\": {}\n *   }\n * }\n * ```\n * ```ts name=\"Usage\"\n * await snap.request({\n *   method: 'snap_closeWebSocket',\n *   params: { id: 'websocket-1' },\n * });\n * ```\n */\nexport const closeWebSocketHandler = {\n  implementation: closeWebSocketImplementation,\n  actionNames: ['PermissionController:hasPermission', 'WebSocketService:close'],\n} satisfies MethodHandler<\n  never,\n  CloseWebSocketMethodActions,\n  CloseWebSocketParams,\n  CloseWebSocketResult,\n  { origin: string }\n>;\n\n/**\n * The `snap_closeWebSocket` method implementation.\n *\n * @param req - The JSON-RPC request object.\n * @param res - The JSON-RPC response object.\n * @param _next - The `json-rpc-engine` \"next\" callback. Not used by this function.\n * @param end - The `json-rpc-engine` \"end\" callback.\n * @param _hooks - The RPC method hooks. Not used by this function.\n * @param messenger - The messenger used to call controller actions.\n * @returns Nothing.\n */\nfunction closeWebSocketImplementation(\n  req: JsonRpcRequestWithOrigin<CloseWebSocketParameters>,\n  res: PendingJsonRpcResponse<CloseWebSocketResult>,\n  _next: unknown,\n  end: JsonRpcEngineEndCallback,\n  _hooks: never,\n  messenger: Messenger<string, CloseWebSocketMethodActions>,\n): void {\n  const { params, origin } = req;\n\n  if (\n    !messenger.call(\n      'PermissionController:hasPermission',\n      origin,\n      SnapEndowments.NetworkAccess,\n    )\n  ) {\n    return end(providerErrors.unauthorized());\n  }\n\n  try {\n    const { id } = getValidatedParams(params);\n    messenger.call('WebSocketService:close', origin, id);\n    res.result = null;\n  } catch (error) {\n    return end(error);\n  }\n\n  return end();\n}\n\n/**\n * Validates the parameters for the snap_closeWebSocket method.\n *\n * @param params - Parameters to validate.\n * @returns Validated parameters.\n * @throws Throws RPC error if validation fails.\n */\nfunction getValidatedParams(params: unknown): CloseWebSocketParameters {\n  try {\n    return create(params, CloseWebSocketParametersStruct);\n  } catch (error) {\n    if (error instanceof StructError) {\n      throw rpcErrors.invalidParams({\n        message: `Invalid params: ${error.message}.`,\n      });\n    }\n    /* istanbul ignore next */\n    throw rpcErrors.internal();\n  }\n}\n"]}