Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async initialize (
devices?: USBDevice[],
tryDebugLink: boolean = false,
autoConnect: boolean = true
): Promise {
if (!(window && window.navigator.usb))
throw new WebUSBNotAvailable()
const devicesToInitialize = devices || await window.navigator.usb.getDevices()
let errors = []
for (let i = 0; i < devicesToInitialize.length; i++) {
const usbDevice = devicesToInitialize[i]
if (usbDevice.vendorId !== VENDOR_ID)
continue
if (usbDevice.productId !== WEBUSB_PRODUCT_ID) {
// 🚨 Workaround for bug when an error is thrown inside for loop.
// https://github.com/rpetrich/babel-plugin-transform-async-to-promises/issues/32
errors.push(new FirmwareUpdateRequired("KeepKey", "6.1.0"))
continue
}
export async function openTransport(device: USBDevice): Promise {
if (!(window && window.navigator.usb))
throw new WebUSBNotAvailable()
try {
return await TransportWebUSB.open(device)
} catch (err) {
if (err.name === 'TransportInterfaceNotAvailable') {
throw new ConflictingApp('Ledger')
}
throw new WebUSBCouldNotInitialize('Ledger', err.message)
}
}
export async function getTransport(): Promise {
if (!(window && window.navigator.usb))
throw new WebUSBNotAvailable()
try {
return await TransportWebUSB.request()
} catch (err) {
if (err.name === 'TransportInterfaceNotAvailable') {
throw new ConflictingApp('Ledger')
}
throw new WebUSBCouldNotPair('Ledger', err.message)
}
}
export async function getFirstLedgerDevice(): Promise {
if (!(window && window.navigator.usb))
throw new WebUSBNotAvailable()
const existingDevices = await TransportWebUSB.list()
return existingDevices.length > 0 ? existingDevices[0] : null
}
public async pairDevice (serialNumber: string = undefined, tryDebugLink: boolean = false): Promise {
if (!(window && window.navigator.usb)) {
throw new WebUSBNotAvailable()
}
let device
try {
device = await window.navigator.usb.requestDevice({ filters: [
{ vendorId: VENDOR_ID, productId: WEBUSB_PRODUCT_ID, serialNumber },
{ vendorId: VENDOR_ID, productId: HID_PRODUCT_ID, serialNumber }
] })
} catch (e) {
throw new WebUSBCouldNotPair('KeepKey', e.message)
}
await this.initialize([device], tryDebugLink, /*autoConnect=*/true)
return this.keyring.get(device.serialNumber)
}