<img src="https://user-images.githubusercontent.com/4631227/191834116-59cf590e-25cc-4956-ae5c-812ea464f324.png" height="100" />

[GitHub](https://github.com/LedgerHQ/ledger-live/),
[Ledger Devs Discord](https://developers.ledger.com/discord-pro),
[Developer Portal](https://developers.ledger.com/)

## @ledgerhq/hw-transport-node-hid-singleton

Allows to communicate with Ledger Hardware Wallets.

**\[Node]**/Electron **(HID)** – uses `node-hid` and `node-usb`. Keep transport opened and re-use it as a singleton, one device at a time on a computer but more robust implementation.

***

## Are you adding Ledger support to your software wallet?

You may be using this package to open a USB connection between your desktop application and the device.

For a smooth and quick integration:

*   See the developers’ documentation on the [Developer Portal](https://developers.ledger.com/docs/transport/overview/) and
*   Go on [Discord](https://developers.ledger.com/discord-pro/) to chat with developer support and the developer community.

***

## API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

#### Table of Contents

*   [TransportNodeHidSingleton](#transportnodehidsingleton)
    *   [Parameters](#parameters)
    *   [Examples](#examples)
    *   [exchange](#exchange)
        *   [Parameters](#parameters-1)
    *   [close](#close)
    *   [isSupported](#issupported)
    *   [list](#list)
    *   [listen](#listen)
        *   [Parameters](#parameters-2)
    *   [setDisconnectAfterInactivityTimeout](#setdisconnectafterinactivitytimeout)
    *   [disconnect](#disconnect)
    *   [open](#open)
        *   [Parameters](#parameters-3)
*   [onDisconnect](#ondisconnect)

### TransportNodeHidSingleton

**Extends TransportNodeHidNoEvents**

node-hid Transport implementation

#### Parameters

*   `device` **HID.HID**&#x20;
*   `$1` **{context: TraceContext?}**  (optional, default `{}`)

    *   `$1.context` &#x20;

#### Examples

```javascript
import TransportNodeHid from "@ledgerhq/hw-transport-node-hid-singleton";
...
TransportNodeHid.create().then(transport => ...)
```

#### exchange

Exchanges with the device using APDU protocol

##### Parameters

*   `apdu` **[Buffer](https://nodejs.org/api/buffer.html)**&#x20;

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[Buffer](https://nodejs.org/api/buffer.html)>** a promise of apdu response

#### close

Closes the transport instance by triggering a disconnection after some inactivity (no new `open`).

Intentionally not disconnecting the device/closing the hid connection directly:
The HID connection will only be closed after some inactivity.

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\<void>**&#x20;

#### isSupported

#### list

#### listen

##### Parameters

*   `observer` **Observer\<ListenDescriptorEvent>**&#x20;

Returns **Subscription**&#x20;

#### setDisconnectAfterInactivityTimeout

Disconnects device from singleton instance after some inactivity (no new `open`).

Currently, there is only one transport instance (for only one device connected via USB).

#### disconnect

Disconnects from the HID device associated to the transport singleton.

If you want to try to re-use the same transport instance at the next action (when calling `open` again), you can use
the transport instance `close` method: it will only enable a disconnect after some inactivity.

#### open

Connects to the first Ledger device connected via USB

Reusing the same TransportNodeHidSingleton instance until a disconnection happens.
Pitfall: this implementation only handles 1 device connected via USB

Legacy: `_descriptor` is needed to follow the Transport definition

##### Parameters

*   `_descriptor` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**&#x20;
*   `_timeoutMs` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?**&#x20;
*   `context` **TraceContext?**&#x20;

Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)<[TransportNodeHidSingleton](#transportnodehidsingleton)>**&#x20;

### onDisconnect

Disconnect event received from the transport instance.

It could be after a disconnection coming from the HID library (e.g. device unplugged) or from the transport instance itself (e.g. close).
Clearing the singleton instance.
Currently, only 1 device at a time is supported.
