import { _disconnect } from './disconnect'
import { LedgerEvent } from './event'
import { _open } from './open'
+import { _poll } from './poll'
import { queue } from './queue'
import { _sign } from './sign'
import { _verify } from './verify'
this.#transport = TransportBLE
}
if (api === 'usb' && this.#transport !== TransportUSB) {
- this.#transport = typeof navigator.hid?.getDevices === 'function'
- ? TransportHID
- : TransportUSB
+ this.#transport = TransportUSB
}
}
const status = await queue(async () => _connect(this.#transport))
* connection updates.
*/
static async disconnect (): Promise<void> {
- queue(async () => {
+ return queue(async () => {
_disconnect()
this.#isPolling = false
this.status = 'DISCONNECTED'
* device.
*/
static async #poll (): Promise<void> {
- try {
- const isHidPaired = (await navigator.hid?.getDevices?.() ?? [])
- .some(device => device.vendorId === LEDGER_VENDOR_ID)
- const isUsbPaired = (await navigator.usb?.getDevices?.() ?? [])
- .some(device => device.vendorId === LEDGER_VENDOR_ID)
- if ((this.#transport === TransportHID && isHidPaired)
- || (this.#transport === TransportUSB && isUsbPaired)) {
- await this.connect()
- } else {
- this.status = 'DISCONNECTED'
- }
- this.#isPolling ? setTimeout(() => this.#poll(), 500) : void 0
- } catch {
- console.warn('Error polling Ledger device')
- this.status = 'DISCONNECTED'
- }
+ this.status = await queue(async () => _poll(this.#transport))
+ this.#isPolling ? setTimeout(() => this.#poll(), 500) : void 0
}
}
--- /dev/null
+//! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
+//! SPDX-License-Identifier: GPL-3.0-or-later
+
+import TransportHID from '@ledgerhq/hw-transport-webhid'
+import TransportUSB from '@ledgerhq/hw-transport-webusb'
+import { LEDGER_VENDOR_ID, Ledger, LedgerStatus, LedgerTransport } from '.'
+
+export async function _poll (transport: LedgerTransport): Promise<LedgerStatus> {
+ let status: LedgerStatus = 'DISCONNECTED'
+ try {
+ const isHidPaired = (await navigator.hid?.getDevices?.() ?? [])
+ .some(device => device.vendorId === LEDGER_VENDOR_ID)
+ const isUsbPaired = (await navigator.usb?.getDevices?.() ?? [])
+ .some(device => device.vendorId === LEDGER_VENDOR_ID)
+ if ((transport === TransportHID && isHidPaired)
+ || (transport === TransportUSB && isUsbPaired)) {
+ status = await Ledger.connect()
+ }
+ } catch {
+ console.warn('Error polling Ledger device')
+ } finally {
+ return status
+ }
+}