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AI,CAAa,CAAC,UAAU,EAAE,CAAC;IACvC,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,SAAS;QACb,MAAM,uBAAA,IAAI,oEAAW,MAAf,IAAI,CAAa,CAAC,SAAS,EAAE,CAAC;IACtC,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,oBAAoB;QACxB,OAAO,uBAAA,IAAI,oEAAW,MAAf,IAAI,CAAa,CAAC,oBAAoB,EAAE,CAAC;IAClD,CAAC;CAsBF;AAnMD,gDAmMC;;IAdG,IAAI,uBAAA,IAAI,+CAAqB,EAAE,CAAC;QAC9B,OAAO,uBAAA,IAAI,+CAAqB,CAAC;IACnC,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;AACxE,CAAC;IAQC,OAAO,uBAAA,IAAI,iFAAwB,MAA5B,IAAI,CAA0B,CAAC,SAAS,EAAE,CAAC;AACpD,CAAC","sourcesContent":["import type Transport from '@ledgerhq/hw-transport';\n\nimport {\n  GetPublicKeyParams,\n  GetPublicKeyResponse,\n  LedgerBridge,\n  LedgerSignMessageParams,\n  LedgerSignMessageResponse,\n  LedgerSignTransactionParams,\n  LedgerSignTransactionResponse,\n  LedgerSignTypedDataParams,\n  LedgerSignTypedDataResponse,\n} from './ledger-bridge';\nimport { MetaMaskLedgerHwAppEth } from './ledger-hw-app';\nimport { TransportMiddleware } from './ledger-transport-middleware';\nimport {\n  GetAppNameAndVersionResponse,\n  LedgerMobileBridgeOptions,\n} from './type';\n\n// MobileBridge Type will always use LedgerBridge with LedgerMobileBridgeOptions\nexport type MobileBridge = LedgerBridge<LedgerMobileBridgeOptions> & {\n  getAppNameAndVersion(): Promise<GetAppNameAndVersionResponse>;\n  openEthApp(): Promise<void>;\n  closeApps(): Promise<void>;\n};\n\n/**\n * LedgerMobileBridge is a bridge between the LedgerKeyring and the LedgerTransportMiddleware.\n */\nexport class LedgerMobileBridge implements MobileBridge {\n  readonly #transportMiddleware?: TransportMiddleware;\n\n  #opts: LedgerMobileBridgeOptions;\n\n  isDeviceConnected = false;\n\n  constructor(\n    transportMiddleware: TransportMiddleware,\n    opts: LedgerMobileBridgeOptions = {},\n  ) {\n    this.#opts = opts;\n    this.#transportMiddleware = transportMiddleware;\n  }\n\n  /**\n   * Method to initializes the keyring.\n   * Mobile ledger doesnt not require init.\n   *\n   * @returns A promise that will resolve once the bridge is initialized.\n   */\n  async init(): Promise<void> {\n    return Promise.resolve();\n  }\n\n  /**\n   * Method to destroy the keyring.\n   * It will dispose the transportmiddleware and set isDeviceConnected to false.\n   */\n  async destroy(): Promise<void> {\n    try {\n      await this.#getTransportMiddleWare().dispose();\n    } catch (error) {\n      // eslint-disable-next-line no-console\n      console.error(error);\n    }\n    this.isDeviceConnected = false;\n  }\n\n  /**\n   * Method to sign a string Message.\n   * Sending the string message to the device and returning the signed message.\n   *\n   * @param params - An object contains hdPath and message.\n   * @param params.hdPath - The BIP 32 path of the account.\n   * @param params.message - The message to sign.\n   * @returns Retrieve v, r, s from the signed message.\n   */\n  async deviceSignMessage({\n    hdPath,\n    message,\n  }: LedgerSignMessageParams): Promise<LedgerSignMessageResponse> {\n    return this.#getEthApp().signPersonalMessage(hdPath, message);\n  }\n\n  /**\n   * Method to sign a EIP712 Message.\n   * Sending the typed data message to the device and returning the signed message.\n   *\n   * @param params - An object contains hdPath, domainSeparatorHex and hashStructMessageHex.\n   * @param params.hdPath - The BIP 32 path of the account.\n   * @param params.message - The EIP712 message to sign.\n   * @returns Retrieve v, r, s from the signed message.\n   */\n  async deviceSignTypedData({\n    hdPath,\n    message,\n  }: LedgerSignTypedDataParams): Promise<LedgerSignTypedDataResponse> {\n    return this.#getEthApp().signEIP712Message(hdPath, message);\n  }\n\n  /**\n   * Method to sign a transaction\n   * Sending the hexadecimal transaction message to the device and returning the signed transaction.\n   *\n   * @param params - An object contains tx, hdPath.\n   * @param params.tx - The raw ethereum transaction in hexadecimal to sign.\n   * @param params.hdPath - The BIP 32 path of the account.\n   * @returns Retrieve v, r, s from the signed transaction.\n   */\n  async deviceSignTransaction({\n    tx,\n    hdPath,\n  }: LedgerSignTransactionParams): Promise<LedgerSignTransactionResponse> {\n    return this.#getEthApp().clearSignTransaction(hdPath, tx, {\n      externalPlugins: true,\n      erc20: true,\n      nft: true,\n    });\n  }\n\n  /**\n   * Method to retrieve the ethereum address for a given BIP 32 path.\n   *\n   * @param params - An object contains hdPath.\n   * @param params.hdPath - The BIP 32 path of the account.\n   * @returns An object contains publicKey, address and chainCode.\n   */\n  async getPublicKey({\n    hdPath,\n  }: GetPublicKeyParams): Promise<GetPublicKeyResponse> {\n    return await this.#getEthApp().getAddress(hdPath, false, true);\n  }\n\n  /**\n   * Method to retrieve the current configuration.\n   *\n   * @returns Retrieve current configuration.\n   */\n  async getOptions(): Promise<LedgerMobileBridgeOptions> {\n    return this.#opts;\n  }\n\n  /**\n   * Method to set the current configuration.\n   *\n   * @param opts - An configuration object.\n   */\n  async setOptions(opts: LedgerMobileBridgeOptions): Promise<void> {\n    this.#opts = opts;\n  }\n\n  /**\n   * Method to set the transport object to communicate with the device.\n   *\n   * @param transport - The communication interface with the Ledger hardware wallet. There are different kind of transports based on the technology (channels like U2F, HID, Bluetooth, Webusb).\n   * @returns Retrieve boolean.\n   */\n  async updateTransportMethod(transport: Transport): Promise<boolean> {\n    if (!transport.deviceModel) {\n      throw new Error('Property `deviceModel` is not defined in `transport`.');\n    }\n    if (!transport.deviceModel.id) {\n      throw new Error(\n        'Property `deviceModel.id` is not defined in `transport`.',\n      );\n    }\n    this.#getTransportMiddleWare().setTransport(transport);\n    this.isDeviceConnected = true;\n    return Promise.resolve(true);\n  }\n\n  /**\n   * Method to init eth app object on ledger device.\n   * This method is not supported on mobile.\n   */\n  async attemptMakeApp(): Promise<boolean> {\n    throw new Error('Method not supported.');\n  }\n\n  /**\n   * Method to open ethereum application on ledger device.\n   *\n   */\n  async openEthApp(): Promise<void> {\n    await this.#getEthApp().openEthApp();\n  }\n\n  /**\n   * Method to close all running application on ledger device.\n   *\n   */\n  async closeApps(): Promise<void> {\n    await this.#getEthApp().closeApps();\n  }\n\n  /**\n   * Method to retrieve the name and version of the running application in ledger device.\n   *\n   * @returns An object contains appName and version.\n   */\n  async getAppNameAndVersion(): Promise<GetAppNameAndVersionResponse> {\n    return this.#getEthApp().getAppNameAndVersion();\n  }\n\n  /**\n   * Method to retrieve the transport middleWare object.\n   *\n   * @returns The TransportMiddleware object.\n   */\n  #getTransportMiddleWare(): TransportMiddleware {\n    if (this.#transportMiddleware) {\n      return this.#transportMiddleware;\n    }\n    throw new Error('Instance `transportMiddleware` is not initialized.');\n  }\n\n  /**\n   * Method to retrieve the ledger Eth App object.\n   *\n   * @returns The ledger Eth App object.\n   */\n  #getEthApp(): MetaMaskLedgerHwAppEth {\n    return this.#getTransportMiddleWare().getEthApp();\n  }\n}\n"]}