diff --git a/src/internals/usb/devices.js b/src/internals/usb/devices.js index abd40bc7..e735149c 100644 --- a/src/internals/usb/devices.js +++ b/src/internals/usb/devices.js @@ -1,11 +1,26 @@ +// @flow + import CommNodeHid from '@ledgerhq/hw-transport-node-hid' +import noop from 'lodash/noop' +import type Transport from '@ledgerhq/hw-transport' + +import { APDUS } from 'internals/usb/manager/constants' +import type { IPCSend } from 'types/electron' -export default send => ({ +export default (send: IPCSend) => ({ listen: () => { CommNodeHid.listen({ - next: e => { + error: noop, + complete: noop, + next: async e => { + if (!e.device) { + return + } if (e.type === 'add') { - send('device.add', e.device, { kill: false }) + const isValid = await isValidHIDDevice(e.device.path) + if (isValid) { + send('device.add', e.device, { kill: false }) + } } if (e.type === 'remove') { @@ -15,3 +30,17 @@ export default send => ({ }) }, }) + +/** + * Attempt to get firmware infos from device + * If it fails, we consider it is an invalid device + */ +async function isValidHIDDevice(devicePath: string): Promise { + try { + const transport: Transport<*> = await CommNodeHid.open(devicePath) + await transport.send(...APDUS.GET_FIRMWARE) + return true + } catch (err) { + return false + } +} diff --git a/src/types/electron.js b/src/types/electron.js index 3c81cc0c..268fc00b 100644 --- a/src/types/electron.js +++ b/src/types/electron.js @@ -1,3 +1,3 @@ // @flow -export type IPCSend = (string, any) => void +export type IPCSend = (string, any, any) => void