Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return Promise.resolve().then(() => {
if (transportInstance) {
log("hid-verbose", "reusing opened transport instance");
return transportInstance;
}
const device = getDevices()[0];
if (!device) throw new CantOpenDevice("no device found");
log("hid-verbose", "new HID transport");
transportInstance = new TransportNodeHidSingleton(
new HID.HID(device.path)
);
const unlisten = listenDevices(
() => {},
() => {
// assume any ledger disconnection concerns current transport
if (transportInstance) {
transportInstance.emit("disconnect");
}
}
);
const onDisconnect = () => {
if (!transportInstance) return;
static listen = (observer: Observer>): Subscription => {
let unsubscribed;
Promise.resolve(getDevices()).then(devices => {
// this needs to run asynchronously so the subscription is defined during this phase
for (const device of devices) {
if (!unsubscribed) {
const deviceModel = identifyUSBProductId(device.productId);
observer.next({
type: "add",
descriptor: "",
device: { name: device.deviceName },
deviceModel
});
}
}
});
const onAdd = device => {
const deviceModel = identifyUSBProductId(device.productId);
static listen = (
observer: Observer>
): Subscription => {
let unsubscribed = false;
Promise.resolve(getDevices()).then(devices => {
// this needs to run asynchronously so the subscription is defined during this phase
for (const device of devices) {
if (!unsubscribed) {
const descriptor: string = device.path;
const deviceModel = identifyUSBProductId(device.productId);
observer.next({ type: "add", descriptor, device, deviceModel });
}
}
});
const { events, stop } = listenDevices(
listenDevicesDebounce,
listenDevicesPollingSkip
);
const onAdd = device => {
if (unsubscribed || !device) return;
import { listenDevices } from "./listenDevices";
let transportInstance;
/**
* node-hid Transport implementation
* @example
* import TransportNodeHid from "@ledgerhq/hw-transport-node-hid-singleton";
* ...
* TransportNodeHid.create().then(transport => ...)
*/
export default class TransportNodeHidSingleton extends TransportNodeHidNoEvents {
/**
*
*/
static isSupported = TransportNodeHidNoEvents.isSupported;
/**
*
*/
static list = TransportNodeHidNoEvents.list;
/**
*/
static listen = (observer: Observer>): Subscription => {
let unsubscribed;
Promise.resolve(getDevices()).then(devices => {
// this needs to run asynchronously so the subscription is defined during this phase
for (const device of devices) {
if (!unsubscribed) {
const deviceModel = identifyUSBProductId(device.productId);
observer.next({
* node-hid Transport implementation
* @example
* import TransportNodeHid from "@ledgerhq/hw-transport-node-hid-singleton";
* ...
* TransportNodeHid.create().then(transport => ...)
*/
export default class TransportNodeHidSingleton extends TransportNodeHidNoEvents {
/**
*
*/
static isSupported = TransportNodeHidNoEvents.isSupported;
/**
*
*/
static list = TransportNodeHidNoEvents.list;
/**
*/
static listen = (observer: Observer>): Subscription => {
let unsubscribed;
Promise.resolve(getDevices()).then(devices => {
// this needs to run asynchronously so the subscription is defined during this phase
for (const device of devices) {
if (!unsubscribed) {
const deviceModel = identifyUSBProductId(device.productId);
observer.next({
type: "add",
descriptor: "",
device: { name: device.deviceName },
deviceModel
});
const poll = () => {
if (!listenDevicesPollingSkip()) {
log("hid-listen", "Polling for added or removed devices");
let changeFound = false;
const currentDevices = getFlatDevices();
const newDevices = currentDevices.filter(d => !lastDevices.includes(d));
if (newDevices.length > 0) {
log("hid-listen", "New device found:", newDevices);
listDevices = getDevices();
events.emit("add", getDeviceByPaths(newDevices));
changeFound = true;
} else {
log("hid-listen", "No new device found");
}
const removeDevices = lastDevices.filter(
d => !currentDevices.includes(d)
);
if (removeDevices.length > 0) {
log("hid-listen", "Removed device found:", removeDevices);
events.emit("remove", getDeviceByPaths(removeDevices));
listDevices = listDevices.filter(
return Promise.resolve().then(() => {
if (path) {
return new TransportNodeHid(new HID.HID(path));
}
const device = getDevices()[0];
if (!device) throw new TransportError("NoDevice", "NoDevice");
return new TransportNodeHid(new HID.HID(device.path));
});
}
export default (
delay: number,
listenDevicesPollingSkip: () => boolean
): ({
events: EventEmitter,
stop: () => void
}) => {
const events = new EventEmitter();
events.setMaxListeners(0);
let listDevices = getDevices();
const flatDevice = d => d.path;
const getFlatDevices = () => [
...new Set(getDevices().map(d => flatDevice(d)))
];
const getDeviceByPaths = paths =>
listDevices.find(d => paths.includes(flatDevice(d)));
let lastDevices = getFlatDevices();
const poll = () => {
if (!listenDevicesPollingSkip()) {
log("hid-listen", "Polling for added or removed devices");
const getFlatDevices = () => [
...new Set(getDevices().map(d => flatDevice(d)))
];
async function getEthApp() {
try {
if (!transport) {
transport = await TransportNodeHid.create();
transport.on('disconnect', () => (transport = null));
}
return new LedgerEth(transport);
} catch (err) {
if (err && err.name === 'TransportError') {
throw new Error('ENCLAVE_LEDGER_IN_USE');
}
if (err && err.message && err.message.includes('cannot open device with path')) {
throw new Error('ENCLAVE_LEDGER_IN_USE');
}
throw err;
}
}