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ngine } from '@metamask/json-rpc-engine';\nimport { JsonRpcEngineV2 } from '@metamask/json-rpc-engine/v2';\nimport type {\n  HandleOptions,\n  ContextConstraint,\n  MiddlewareContext,\n} from '@metamask/json-rpc-engine/v2';\nimport type {\n  Json,\n  JsonRpcId,\n  JsonRpcParams,\n  JsonRpcSuccess,\n  JsonRpcRequest,\n  JsonRpcVersion2,\n} from '@metamask/utils';\nimport { nanoid } from 'nanoid';\n\n/**\n * A JSON-RPC request conforming to the EIP-1193 specification.\n */\ntype Eip1193Request<Params extends JsonRpcParams = JsonRpcParams> = {\n  id?: JsonRpcId;\n  jsonrpc?: JsonRpcVersion2;\n  method: string;\n  params?: Params;\n};\n\ntype Options<\n  Request extends JsonRpcRequest = JsonRpcRequest,\n  Context extends ContextConstraint = MiddlewareContext,\n> = {\n  engine: JsonRpcEngine | JsonRpcEngineV2<Request, Context>;\n};\n\n/**\n * An Ethereum provider.\n *\n * This provider loosely follows conventions that pre-date EIP-1193.\n * It is not compliant with any Ethereum provider standard.\n */\nexport class InternalProvider<\n  Context extends ContextConstraint = MiddlewareContext,\n> {\n  readonly #engine: JsonRpcEngineV2<JsonRpcRequest, Context>;\n\n  /**\n   * Construct a InternalProvider from a JSON-RPC server or legacy engine.\n   *\n   * @param options - Options.\n   * @param options.engine - The JSON-RPC engine used to process requests.\n   */\n  constructor({ engine }: Options<JsonRpcRequest, Context>) {\n    this.#engine =\n      'push' in engine\n        ? JsonRpcEngineV2.create({\n            middleware: [asV2Middleware<JsonRpcParams, JsonRpcRequest>(engine)],\n          })\n        : engine;\n  }\n\n  /**\n   * Send a provider request asynchronously.\n   *\n   * @param eip1193Request - The request to send.\n   * @param options - The options for the request operation.\n   * @param options.context - The context to include with the request.\n   * @returns The JSON-RPC response.\n   */\n  async request<Params extends JsonRpcParams, Result extends Json>(\n    eip1193Request: Eip1193Request<Params>,\n    options?: HandleOptions<Context>,\n  ): Promise<Result> {\n    const jsonRpcRequest =\n      convertEip1193RequestToJsonRpcRequest(eip1193Request);\n    return (await this.#handle<Result>(jsonRpcRequest, options)).result;\n  }\n\n  /**\n   * Send a provider request asynchronously.\n   *\n   * This method serves the same purpose as `request`. It only exists for\n   * legacy reasons.\n   *\n   * @param eip1193Request - The request to send.\n   * @param callback - A function that is called upon the success or failure of the request.\n   * @deprecated Use {@link request} instead. This method is retained solely for backwards\n   * compatibility with certain libraries.\n   */\n  sendAsync = <Params extends JsonRpcParams>(\n    eip1193Request: Eip1193Request<Params>,\n    // Non-polluting `any` that acts like a constraint.\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    callback: (error: unknown, providerRes?: any) => void,\n  ): void => {\n    const jsonRpcRequest =\n      convertEip1193RequestToJsonRpcRequest(eip1193Request);\n    this.#handleWithCallback(jsonRpcRequest, callback);\n  };\n\n  /**\n   * Send a provider request asynchronously.\n   *\n   * This method serves the same purpose as `request`. It only exists for\n   * legacy reasons.\n   *\n   * @param eip1193Request - The request to send.\n   * @param callback - A function that is called upon the success or failure of the request.\n   * @deprecated Use {@link request} instead. This method is retained solely for backwards\n   * compatibility with certain libraries.\n   */\n  send = <Params extends JsonRpcParams>(\n    eip1193Request: Eip1193Request<Params>,\n    // TODO: Replace `any` with type\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    callback: (error: unknown, providerRes?: any) => void,\n  ): void => {\n    if (typeof callback !== 'function') {\n      throw new Error('Must provide callback to \"send\" method.');\n    }\n    const jsonRpcRequest =\n      convertEip1193RequestToJsonRpcRequest(eip1193Request);\n    this.#handleWithCallback(jsonRpcRequest, callback);\n  };\n\n  readonly #handle = async <Result extends Json>(\n    jsonRpcRequest: JsonRpcRequest,\n    options?: HandleOptions<Context>,\n  ): Promise<JsonRpcSuccess<Result>> => {\n    const { id, jsonrpc } = jsonRpcRequest;\n    // The `result` typecast is unsafe, but we need it to preserve the provider's\n    // public interface, which allows you to unsafely typecast results.\n    const result = (await this.#engine.handle(\n      jsonRpcRequest,\n      options,\n    )) as unknown as Result;\n\n    return {\n      id,\n      jsonrpc,\n      result,\n    };\n  };\n\n  readonly #handleWithCallback = (\n    jsonRpcRequest: JsonRpcRequest,\n    callback: (error: unknown, providerRes?: unknown) => void,\n  ): void => {\n    /* eslint-disable promise/no-callback-in-promise */\n    this.#handle(jsonRpcRequest)\n      // A resolution will always be a successful response\n      .then((response) => callback(null, response))\n      .catch((error) => {\n        callback(error);\n      });\n    /* eslint-enable promise/no-callback-in-promise */\n  };\n}\n\n/**\n * Convert an EIP-1193 request to a JSON-RPC request.\n *\n * @param eip1193Request - The EIP-1193 request to convert.\n * @returns The JSON-RPC request.\n */\nexport function convertEip1193RequestToJsonRpcRequest(\n  eip1193Request: Eip1193Request,\n): JsonRpcRequest {\n  const { id = nanoid(), jsonrpc = '2.0', method, params } = eip1193Request;\n\n  return params\n    ? {\n        id,\n        jsonrpc,\n        method,\n        params,\n      }\n    : {\n        id,\n        jsonrpc,\n        method,\n      };\n}\n"]}