-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
296 lines (220 loc) · 6.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
'use strict';
/* eslint-env browser */
const assert = require('bsert');
const EventEmitter = require('events');
const {LedgerHSD, USB} = require('../../lib/hsd-ledger-browser');
const {Device} = USB;
const KeyRing = require('hsd/lib/primitives/keyring');
const usb = navigator.usb;
if (!usb) {
alert('Could not find WebUSB.');
throw new Error('Could not find WebUSB.');
}
/**
* @param {Device[]} devices
* @param {Device?} selected
* @param {Device?} device
*/
class DeviceManager extends EventEmitter {
constructor() {
super();
this.devices = new Set();
this.wusbToDevice = new Map();
this.selected = null;
// Callbacks for event listener to clean up later.
this._addDevice = null;
this._removeDevice = null;
}
bind() {
this._addDevice = async (event) => {
const device = Device.fromDevice(event.device);
await this.addDevice(device);
this.emit('connect', device);
};
this._removeDevice = async (event) => {
const device = Device.fromDevice(event.device);
await this.removeDevice(device);
this.emit('disconnect', device);
};
usb.addEventListener('connect', this._addDevice);
usb.addEventListener('disconnect', this._removeDevice);
}
unbind() {
assert(this._addDevice);
assert(this._removeDevice);
usb.removeEventListener('connect', this._addDevice);
usb.removeEventListener('disconnect', this._removeDevice);
this._addDevice = null;
this._removeDevice = null;
}
async open() {
const devices = await Device.getDevices();
for (const device of devices)
await this.addDevice(device);
this.bind();
}
async close() {
this.unbind();
this.reset();
}
reset() {
this.devices = new Set();
this.wusbToDevice = new Map();
this.selected = null;
}
async addDevice(device) {
assert(device instanceof Device, 'Could not add device.');
if (this.wusbToDevice.has(device.device))
return this.wusbToDevice.get(device.device);
this.wusbToDevice.set(device.device, device);
this.devices.add(device);
return device;
}
async removeDevice(device) {
assert(device.device, 'Could not remove device.');
if (!Device.isLedgerDevice(device.device))
return;
const mappedDevice = this.wusbToDevice.get(device.device);
if (!mappedDevice)
return;
if (this.selected && this.selected.device === mappedDevice.device)
await this.closeDevice(this.selected);
this.devices.delete(mappedDevice);
this.wusbToDevice.delete(mappedDevice.device);
return;
}
getDevices() {
return this.devices.values();
}
/**
* Only User Action can have an access to this.
* Otherwise this will fail.
*/
async requestDevice() {
const device = await Device.requestDevice();
return this.addDevice(device);
}
async openDevice(device, timeout = 20000) {
assert(!this.selected, 'Other device already in use.');
assert(this.devices.has(device), 'Could not find device.');
this.selected = device;
device.set({ timeout });
try {
await this.selected.open();
this.emit('device open', this.selected);
} catch (e) {
console.error(e);
this.selected = null;
}
return this.selected;
}
async closeDevice(device) {
assert(this.selected, 'No device in use.');
assert(this.devices.has(device), 'Could not find device.');
assert(this.selected === device, 'Can not close closed device.');
if (this.selected.opened)
await this.selected.close();
this.selected = null;
}
}
const manager = new DeviceManager();
const chooseBtn = document.getElementById('choose');
const chosenDiv = document.getElementById('chosen');
const devicesDiv = document.getElementById('devices');
manager.on('connect', renderManager);
manager.on('disconnect', renderManager);
chooseBtn.addEventListener('click', async () => {
const device = await manager.requestDevice();
await manager.openDevice(device);
renderManager();
});
global.addEventListener('load', async () => {
await manager.open();
renderManager();
});
// We rerender all the time..
// Use framework or something.
function renderManager() {
const selected = manager.selected;
const devices = manager.getDevices();
renderChosen(chosenDiv, manager, selected);
renderDevices(devicesDiv, manager, devices);
}
function renderDevices(element, manager, devices) {
removeChildren(element);
for (const device of devices) {
renderDevice(element, manager, device);
}
}
function renderDevice(element, manager, device) {
const container = document.createElement('div');
const name = document.createElement('span');
const choose = document.createElement('button');
choose.innerText = 'Open.';
name.innerText = deviceInfoMini(device);
// we don't clean up listeners.. too much headache
choose.addEventListener('click', async () => {
await manager.openDevice(device);
renderManager();
});
container.appendChild(name);
container.appendChild(choose);
element.appendChild(container);
}
function renderChosen(element, manager, device) {
removeChildren(element);
if (!device)
return;
const closeBtn = document.createElement('button');
closeBtn.innerText = 'Close.';
closeBtn.addEventListener('click', async function close() {
await manager.closeDevice(device);
closeBtn.removeEventListener('click', close);
renderManager();
});
const pubkeyBtn = document.createElement('button');
pubkeyBtn.innerText = 'Get public key';
pubkeyBtn.addEventListener('click', async () => {
const device = manager.selected;
if (!device) {
alert('Could not find device.');
return;
}
const ledger = new LedgerHSD({ device });
const accountKey = await ledger.getAccountXPUB(0);
const pubkeyInformation = `
Account: m/44'/0'/0'
xpub: ${accountKey.xpubkey()}
First Receive Address: ${deriveAddress(accountKey, 0, 0, 'main')}
First Change Address: ${deriveAddress(accountKey, 1, 0, 'main')}
`;
const pubkeyElement = document.createElement('span');
pubkeyElement.innerText = pubkeyInformation;
element.appendChild(pubkeyElement);
});
const information = document.createElement('span');
information.innerText = deviceInfoAll(device);
element.appendChild(information);
element.appendChild(closeBtn);
element.appendChild(pubkeyBtn);
}
function deviceInfoMini(device) {
return `${device.manufacturerName} - ${device.productName}`;
}
function deviceInfoAll(device) {
return `VendorID: ${device.vendorId},
ProductID: ${device.productId},
Manufacturer: ${device.manufacturerName},
Product Name: ${device.productName},
Serial Number: ${device.serialNumber}
`;
}
function removeChildren(element) {
while (element.firstChild)
element.removeChild(element.firstChild);
}
function deriveAddress(hd, change, index, network) {
const pubkey = hd.derive(change).derive(index);
const keyring = KeyRing.fromPublic(pubkey.publicKey, network);
return keyring.getAddress().toString();
}