1 | <img src="https://user-images.githubusercontent.com/4631227/191834116-59cf590e-25cc-4956-ae5c-812ea464f324.png" height="100" />
|
2 |
|
3 | [GitHub](https://github.com/LedgerHQ/ledger-live/),
|
4 | [Ledger Devs Discord](https://developers.ledger.com/discord-pro),
|
5 | [Developer Portal](https://developers.ledger.com/)
|
6 |
|
7 | ## @ledgerhq/hw-transport-webusb
|
8 |
|
9 | Allows to communicate with Ledger Hardware Wallets.
|
10 |
|
11 | **\[Web]** **(WebUSB)** – WebUSB [check browser support](https://caniuse.com/webusb).
|
12 |
|
13 | ***
|
14 |
|
15 | ## Are you adding Ledger support to your software wallet?
|
16 |
|
17 | You may be using this package to open a USB connection between your web application and the device.
|
18 |
|
19 | For a smooth and quick integration:
|
20 |
|
21 | * See the developers’ documentation on the [Developer Portal](https://developers.ledger.com/docs/transport/overview/) and
|
22 | * Go on [Discord](https://developers.ledger.com/discord-pro/) to chat with developer support and the developer community.
|
23 |
|
24 | ***
|
25 |
|
26 | ### FAQ: "DOM Exception" is triggered when creating the transport
|
27 |
|
28 | 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.
|
29 |
|
30 | ### How to use this transport regarding WebUSB paradigm?
|
31 |
|
32 | In WebUSB, we have a "permission native" modal that appears when we need to "request" a device. This is required at-least-once for the user to accept, and then we can open the transport without triggering this modal. However, in both cases, it must happen in context of a click like explain above. Our current implementation trade off is to abstract this out and only trigger the permission modal if no device are listed. This might change in the future.
|
33 |
|
34 | In terms of UX, there are two classical use cases:
|
35 |
|
36 | 1. you only need the device at key times, like once to get the address. once to sign a transaction,...
|
37 | 2. your app lifecycle requires that you need to access the device at the beginning and/or at any time (like you want to ping with getAddress to get the wallet address)
|
38 |
|
39 | in (1) case, you can just do your logic in each button (Get Address / Sign Transaction) time (create it, do the logic, close it).
|
40 | in (2) case, you will need to have a Connect button that appear when you don’t have the connection yet. And you need to hook to the “disconnect” event to potentially make the UI reflect that and require user to click again on that Connect button, because you can’t automatically `create()`/`open()` again.
|
41 |
|
42 | ### Support status
|
43 |
|
44 | WebUSB is currently only supported on Google Chrome / Chromium.
|
45 |
|
46 | * In Linux, user need to install the [specific udev rules](https://raw.githubusercontent.com/LedgerHQ/udev-rules/master/add_udev_rules.sh)
|
47 | * In Mac, it should work.
|
48 | * In Windows, [WebUSB does not work out of the box](https://github.com/WICG/webusb/issues/143) but you can fix it with [Zadig](https://zadig.akeo.ie/).
|
49 | * In Android Chrome it works.
|
50 |
|
51 | ## API
|
52 |
|
53 |
|
54 |
|
55 | #### Table of Contents
|
56 |
|
57 | * [TransportWebUSB](#transportwebusb)
|
58 | * [Parameters](#parameters)
|
59 | * [Examples](#examples)
|
60 | * [close](#close)
|
61 | * [exchange](#exchange)
|
62 | * [Parameters](#parameters-1)
|
63 | * [isSupported](#issupported)
|
64 | * [list](#list)
|
65 | * [listen](#listen)
|
66 | * [Parameters](#parameters-2)
|
67 | * [request](#request)
|
68 | * [openConnected](#openconnected)
|
69 | * [open](#open)
|
70 | * [Parameters](#parameters-3)
|
71 |
|
72 | ### TransportWebUSB
|
73 |
|
74 | **Extends Transport**
|
75 |
|
76 | WebUSB Transport implementation
|
77 |
|
78 | #### Parameters
|
79 |
|
80 | * `device` **USBDevice** 
|
81 | * `interfaceNumber` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** 
|
82 |
|
83 | #### Examples
|
84 |
|
85 | ```javascript
|
86 | import TransportWebUSB from "@ledgerhq/hw-transport-webusb";
|
87 | ...
|
88 | TransportWebUSB.create().then(transport => ...)
|
89 | ```
|
90 |
|
91 | #### close
|
92 |
|
93 | Release the transport device
|
94 |
|
95 | Returns **[Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\<void>** 
|
96 |
|
97 | #### exchange
|
98 |
|
99 | Exchange with the device using APDU protocol.
|
100 |
|
101 | ##### Parameters
|
102 |
|
103 | * `apdu` **[Buffer](https://nodejs.org/api/buffer.html)** 
|
104 |
|
105 | 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
|
106 |
|
107 | #### isSupported
|
108 |
|
109 | Check if WebUSB transport is supported.
|
110 |
|
111 | #### list
|
112 |
|
113 | List the WebUSB devices that was previously authorized by the user.
|
114 |
|
115 | #### listen
|
116 |
|
117 | Actively listen to WebUSB devices and emit ONE device
|
118 | that was either accepted before, if not it will trigger the native permission UI.
|
119 |
|
120 | Important: it must be called in the context of a UI click!
|
121 |
|
122 | ##### Parameters
|
123 |
|
124 | * `observer` **Observer\<DescriptorEvent\<USBDevice>>** 
|
125 |
|
126 | Returns **Subscription** 
|
127 |
|
128 | #### request
|
129 |
|
130 | Similar to create() except it will always display the device permission (even if some devices are already accepted).
|
131 |
|
132 | #### openConnected
|
133 |
|
134 | Similar to create() except it will never display the device permission (it returns a Promise\<?Transport>, null if it fails to find a device).
|
135 |
|
136 | #### open
|
137 |
|
138 | Create a Ledger transport with a USBDevice
|
139 |
|
140 | ##### Parameters
|
141 |
|
142 | * `device` **USBDevice** 
|