What is @ledgerhq/hw-transport-webhid?
@ledgerhq/hw-transport-webhid is an npm package that provides a transport layer for communicating with Ledger hardware wallets using the WebHID API. It allows developers to interact with Ledger devices directly from web applications, enabling functionalities such as sending commands to the device and receiving responses.
What are @ledgerhq/hw-transport-webhid's main functionalities?
Open Connection
This feature allows you to establish a connection with a Ledger device using the WebHID API. The code sample demonstrates how to create a transport instance and open a connection to the device.
const TransportWebHID = require('@ledgerhq/hw-transport-webhid');
async function openConnection() {
const transport = await TransportWebHID.create();
console.log('Connection opened:', transport);
}
openConnection();
Send APDU Command
This feature allows you to send an APDU command to the Ledger device. The code sample shows how to send a command and receive a response from the device.
const TransportWebHID = require('@ledgerhq/hw-transport-webhid');
async function sendCommand() {
const transport = await TransportWebHID.create();
const response = await transport.send(0xE0, 0x01, 0x00, 0x00, Buffer.from([]));
console.log('Response:', response);
}
sendCommand();
Close Connection
This feature allows you to close the connection with the Ledger device. The code sample demonstrates how to properly close the transport connection.
const TransportWebHID = require('@ledgerhq/hw-transport-webhid');
async function closeConnection() {
const transport = await TransportWebHID.create();
await transport.close();
console.log('Connection closed');
}
closeConnection();
Other packages similar to @ledgerhq/hw-transport-webhid
@ledgerhq/hw-transport-u2f
@ledgerhq/hw-transport-u2f is a similar package that uses the U2F (Universal 2nd Factor) protocol to communicate with Ledger devices. Unlike WebHID, U2F is more widely supported across different browsers but may have limitations in terms of speed and functionality compared to WebHID.
@ledgerhq/hw-transport-webusb
@ledgerhq/hw-transport-webusb provides a transport layer using the WebUSB API. It offers similar functionalities to WebHID but requires user permission to access USB devices. WebUSB can be more versatile in terms of device compatibility but may not be supported in all browsers.
@ledgerhq/hw-transport-node-hid
@ledgerhq/hw-transport-node-hid is designed for Node.js environments and uses the HID protocol to communicate with Ledger devices. It is suitable for desktop applications but not for web applications, unlike WebHID which is specifically for web use.
GitHub,
Ledger Devs Discord,
Developer Portal
@ledgerhq/hw-transport-webhid
Allows to communicate with Ledger Hardware Wallets.
[Web] (WebHID) – WebHID check browser support.
Are you adding Ledger support to your software wallet?
You may be using this package to open a USB connection between your web application and the device.
For a smooth and quick integration:
- See the developers’ documentation on the Developer Portal and
- Go on Discord to chat with developer support and the developer community.
FAQ: "DOM Exception" is triggered when creating the transport
The transport functions create()
and listen()
must be called in the context of a user interaction (like a "click" event), otherwise it fails with DOM Exception. This is by WebUSB design. You also must run on HTTPS.
How to use this transport?
Please check @ledgerhq/hw-transport-webusb
documentation because it is very similar paradigm.
Support status
WebUSB is currently only supported on Google Chrome / Chromium DEV version and by explicitly enabling chrome://flags/#enable-experimental-web-platform-features
API
Table of Contents
TransportWebHID
Extends Transport
WebHID Transport implementation
Parameters
Examples
import TransportWebHID from "@ledgerhq/hw-transport-webhid";
...
TransportWebHID.create().then(transport => ...)
close
Release the transport device
Returns Promise<void>
exchange
Exchange with the device using APDU protocol.
Parameters
Returns Promise<Buffer> a promise of apdu response
isSupported
Check if WebUSB transport is supported.
list
List the WebUSB devices that was previously authorized by the user.
listen
Actively listen to WebUSB devices and emit ONE device
that was either accepted before, if not it will trigger the native permission UI.
Important: it must be called in the context of a UI click!
Parameters
observer
Observer<DescriptorEvent<HIDDevice>>
Returns Subscription
request
Similar to create() except it will always display the device permission (even if some devices are already accepted).
openConnected
Similar to create() except it will never display the device permission (it returns a Promise<?Transport>, null if it fails to find a device).
open
Create a Ledger transport with a HIDDevice
Parameters