@ledgerhq/hw-transport-node-hid-singleton 6.25.1-alpha.3 → 6.26.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/TransportNodeHid.js
CHANGED
|
@@ -16,7 +16,11 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
16
16
|
})();
|
|
17
17
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
18
18
|
if (k2 === undefined) k2 = k;
|
|
19
|
-
Object.
|
|
19
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
20
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
21
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
22
|
+
}
|
|
23
|
+
Object.defineProperty(o, k2, desc);
|
|
20
24
|
}) : (function(o, m, k, k2) {
|
|
21
25
|
if (k2 === undefined) k2 = k;
|
|
22
26
|
o[k2] = m[k];
|
|
@@ -185,7 +189,7 @@ var TransportNodeHidSingleton = /** @class */ (function (_super) {
|
|
|
185
189
|
});
|
|
186
190
|
};
|
|
187
191
|
TransportNodeHidSingleton.prototype.setAllowAutoDisconnect = function (allow) {
|
|
188
|
-
this.preventAutoDisconnect = allow;
|
|
192
|
+
this.preventAutoDisconnect = !allow;
|
|
189
193
|
};
|
|
190
194
|
/**
|
|
191
195
|
* Exchange with the device using APDU protocol.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TransportNodeHid.js","sourceRoot":"","sources":["../src/TransportNodeHid.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"TransportNodeHid.js","sourceRoot":"","sources":["../src/TransportNodeHid.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,sDAA2B;AAC3B,yGAEkD;AAMlD,uC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ACL,CAAC,CAAC;QAEF,IAAM,QAAQ,GAAG,UAAC,MAAM;YACtB,IAAM,WAAW,GAAG,IAAA,8BAAoB,EAAC,MAAM,CAAC,SAAS,CAAC,CAAC;YAC3D,QAAQ,CAAC,IAAI,CAAC;gBACZ,IAAI,EAAE,QAAQ;gBACd,UAAU,EAAE,EAAE;gBACd,WAAW,aAAA;gBACX,MAAM,EAAE;oBACN,IAAI,EAAE,MAAM,CAAC,UAAU;iBACxB;aACF,CAAC,CAAC;QACL,CAAC,CAAC;QAEF,IAAM,IAAI,GAAG,IAAA,6BAAa,EAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;QAE5C,SAAS,WAAW;YAClB,IAAI,EAAE,CAAC;YACP,YAAY,GAAG,IAAI,CAAC;QACtB,CAAC;QAED,OAAO;YACL,WAAW,aAAA;SACZ,CAAC;IACJ,CAAC,CAAC;IA0FJ,gCAAC;CAAA,AA7JD,CAAuD,2CAAwB,GA6J9E;qBA7JoB,yBAAyB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ledgerhq/hw-transport-node-hid-singleton",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.26.0",
|
|
4
4
|
"description": "Ledger Hardware Wallet Node implementation of the communication layer, using node-hid and usb-detection",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Ledger",
|
|
@@ -29,8 +29,8 @@
|
|
|
29
29
|
"dependencies": {
|
|
30
30
|
"@ledgerhq/devices": "^6.24.1",
|
|
31
31
|
"@ledgerhq/errors": "^6.10.0",
|
|
32
|
-
"@ledgerhq/hw-transport": "^6.
|
|
33
|
-
"@ledgerhq/hw-transport-node-hid-noevents": "^6.
|
|
32
|
+
"@ledgerhq/hw-transport": "^6.24.1",
|
|
33
|
+
"@ledgerhq/hw-transport-node-hid-noevents": "^6.24.1",
|
|
34
34
|
"@ledgerhq/logs": "^6.10.0",
|
|
35
35
|
"lodash": "^4.17.21",
|
|
36
36
|
"node-hid": "2.1.1",
|
|
@@ -42,5 +42,5 @@
|
|
|
42
42
|
"watch": "bash ../../script/watch.sh",
|
|
43
43
|
"doc": "bash ../../script/doc.sh"
|
|
44
44
|
},
|
|
45
|
-
"gitHead": "
|
|
45
|
+
"gitHead": "5231dbe6373e3786e4d286ae17b7f88ebfd236d1"
|
|
46
46
|
}
|
package/src/TransportNodeHid.ts
CHANGED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
//@flow
|
|
2
|
-
|
|
3
|
-
import HID from "node-hid";
|
|
4
|
-
import TransportNodeHidNoEvents, {
|
|
5
|
-
getDevices,
|
|
6
|
-
} from "@ledgerhq/hw-transport-node-hid-noevents";
|
|
7
|
-
import type {
|
|
8
|
-
Observer,
|
|
9
|
-
DescriptorEvent,
|
|
10
|
-
Subscription,
|
|
11
|
-
} from "@ledgerhq/hw-transport";
|
|
12
|
-
import { log } from "@ledgerhq/logs";
|
|
13
|
-
import { identifyUSBProductId } from "@ledgerhq/devices";
|
|
14
|
-
import { CantOpenDevice } from "@ledgerhq/errors";
|
|
15
|
-
import { listenDevices } from "./listenDevices";
|
|
16
|
-
|
|
17
|
-
let transportInstance;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* node-hid Transport implementation
|
|
21
|
-
* @example
|
|
22
|
-
* import TransportNodeHid from "@ledgerhq/hw-transport-node-hid-singleton";
|
|
23
|
-
* ...
|
|
24
|
-
* TransportNodeHid.create().then(transport => ...)
|
|
25
|
-
*/
|
|
26
|
-
export default class TransportNodeHidSingleton extends TransportNodeHidNoEvents {
|
|
27
|
-
/**
|
|
28
|
-
*
|
|
29
|
-
*/
|
|
30
|
-
static isSupported = TransportNodeHidNoEvents.isSupported;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
*
|
|
34
|
-
*/
|
|
35
|
-
static list = TransportNodeHidNoEvents.list;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
*/
|
|
39
|
-
static listen = (observer: Observer<DescriptorEvent<*>>): Subscription => {
|
|
40
|
-
let unsubscribed;
|
|
41
|
-
Promise.resolve(getDevices()).then((devices) => {
|
|
42
|
-
// this needs to run asynchronously so the subscription is defined during this phase
|
|
43
|
-
for (const device of devices) {
|
|
44
|
-
if (!unsubscribed) {
|
|
45
|
-
const deviceModel = identifyUSBProductId(device.productId);
|
|
46
|
-
observer.next({
|
|
47
|
-
type: "add",
|
|
48
|
-
descriptor: "",
|
|
49
|
-
device: { name: device.deviceName },
|
|
50
|
-
deviceModel,
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
const onAdd = (device) => {
|
|
57
|
-
const deviceModel = identifyUSBProductId(device.productId);
|
|
58
|
-
observer.next({
|
|
59
|
-
type: "add",
|
|
60
|
-
descriptor: "",
|
|
61
|
-
deviceModel,
|
|
62
|
-
device: { name: device.deviceName },
|
|
63
|
-
});
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
const onRemove = (device) => {
|
|
67
|
-
const deviceModel = identifyUSBProductId(device.productId);
|
|
68
|
-
observer.next({
|
|
69
|
-
type: "remove",
|
|
70
|
-
descriptor: "",
|
|
71
|
-
deviceModel,
|
|
72
|
-
device: { name: device.deviceName },
|
|
73
|
-
});
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
const stop = listenDevices(onAdd, onRemove);
|
|
77
|
-
|
|
78
|
-
function unsubscribe() {
|
|
79
|
-
stop();
|
|
80
|
-
unsubscribed = true;
|
|
81
|
-
}
|
|
82
|
-
return { unsubscribe };
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* globally disconnect the transport singleton
|
|
87
|
-
*/
|
|
88
|
-
static async disconnect() {
|
|
89
|
-
if (transportInstance) {
|
|
90
|
-
transportInstance.device.close();
|
|
91
|
-
transportInstance.emit("disconnect");
|
|
92
|
-
transportInstance = null;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* if path="" is not provided, the library will take the first device
|
|
98
|
-
*/
|
|
99
|
-
static open(): Promise<TransportNodeHidSingleton> {
|
|
100
|
-
return Promise.resolve().then(() => {
|
|
101
|
-
if (transportInstance) {
|
|
102
|
-
log("hid-verbose", "reusing opened transport instance");
|
|
103
|
-
return transportInstance;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const device = getDevices()[0];
|
|
107
|
-
if (!device) throw new CantOpenDevice("no device found");
|
|
108
|
-
log("hid-verbose", "new HID transport");
|
|
109
|
-
transportInstance = new TransportNodeHidSingleton(
|
|
110
|
-
new HID.HID(device.path)
|
|
111
|
-
);
|
|
112
|
-
const unlisten = listenDevices(
|
|
113
|
-
() => {},
|
|
114
|
-
() => {
|
|
115
|
-
// assume any ledger disconnection concerns current transport
|
|
116
|
-
if (transportInstance) {
|
|
117
|
-
transportInstance.emit("disconnect");
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
);
|
|
121
|
-
const onDisconnect = () => {
|
|
122
|
-
if (!transportInstance) return;
|
|
123
|
-
log("hid-verbose", "transport instance was disconnected");
|
|
124
|
-
transportInstance.off("disconnect", onDisconnect);
|
|
125
|
-
transportInstance = null;
|
|
126
|
-
unlisten();
|
|
127
|
-
};
|
|
128
|
-
transportInstance.on("disconnect", onDisconnect);
|
|
129
|
-
|
|
130
|
-
return transportInstance;
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
close() {
|
|
135
|
-
// intentionally, a close will not effectively close the hid connection
|
|
136
|
-
return Promise.resolve();
|
|
137
|
-
}
|
|
138
|
-
}
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
import usbDetect from "usb-detection";
|
|
3
|
-
import { ledgerUSBVendorId } from "@ledgerhq/devices";
|
|
4
|
-
import { log } from "@ledgerhq/logs";
|
|
5
|
-
|
|
6
|
-
export type Device = {
|
|
7
|
-
locationId: number,
|
|
8
|
-
vendorId: number,
|
|
9
|
-
productId: number,
|
|
10
|
-
deviceName: string,
|
|
11
|
-
manufacturer: string,
|
|
12
|
-
serialNumber: string,
|
|
13
|
-
deviceAddress: number,
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
const deviceToLog = ({ productId, locationId, deviceAddress }) =>
|
|
17
|
-
`productId=${productId} locationId=${locationId} deviceAddress=${deviceAddress}`;
|
|
18
|
-
|
|
19
|
-
let usbDebounce = 1000;
|
|
20
|
-
|
|
21
|
-
export const setUsbDebounce = (n: number) => {
|
|
22
|
-
usbDebounce = n;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
let monitoring = false;
|
|
26
|
-
const monitor = () => {
|
|
27
|
-
if (!monitoring) {
|
|
28
|
-
monitoring = true;
|
|
29
|
-
usbDetect.startMonitoring();
|
|
30
|
-
}
|
|
31
|
-
return () => {};
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
// No better way for now. see https://github.com/LedgerHQ/ledgerjs/issues/434
|
|
35
|
-
process.on("exit", () => {
|
|
36
|
-
if (monitoring) {
|
|
37
|
-
// redeem the monitoring so the process can be terminated.
|
|
38
|
-
usbDetect.stopMonitoring();
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
export const listenDevices = (
|
|
43
|
-
onAdd: (Device) => void,
|
|
44
|
-
onRemove: (Device) => void
|
|
45
|
-
) => {
|
|
46
|
-
const unmonitor = monitor();
|
|
47
|
-
|
|
48
|
-
const addEvent = "add:" + ledgerUSBVendorId;
|
|
49
|
-
const removeEvent = "remove:" + ledgerUSBVendorId;
|
|
50
|
-
|
|
51
|
-
let timeout;
|
|
52
|
-
|
|
53
|
-
const add = (device) => {
|
|
54
|
-
log("usb-detection", "add: " + deviceToLog(device));
|
|
55
|
-
if (!timeout) {
|
|
56
|
-
// a time is needed for the device to actually be connectable over HID..
|
|
57
|
-
// we also take this time to not emit the device yet and potentially cancel it if a remove happens.
|
|
58
|
-
timeout = setTimeout(() => {
|
|
59
|
-
onAdd(device);
|
|
60
|
-
timeout = null;
|
|
61
|
-
}, usbDebounce);
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
const remove = (device) => {
|
|
66
|
-
log("usb-detection", "remove: " + deviceToLog(device));
|
|
67
|
-
if (timeout) {
|
|
68
|
-
clearTimeout(timeout);
|
|
69
|
-
timeout = null;
|
|
70
|
-
} else {
|
|
71
|
-
onRemove(device);
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
usbDetect.on(addEvent, add);
|
|
76
|
-
usbDetect.on(removeEvent, remove);
|
|
77
|
-
|
|
78
|
-
return () => {
|
|
79
|
-
if (timeout) clearTimeout(timeout);
|
|
80
|
-
usbDetect.off(addEvent, add);
|
|
81
|
-
usbDetect.off(removeEvent, remove);
|
|
82
|
-
unmonitor();
|
|
83
|
-
};
|
|
84
|
-
};
|