Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
};
});
// Initialize each of the adapters
await Promise.all(
adapters.map(async ({ name, adapter, config }) => {
console.debug(`[HDWallet] - ✅ Initializing ${name} adapter`, config);
try {
return await adapter.initialize(/* undefined, false, false*/);
} catch (err) {
console.error(err.message);
}
})
);
this.keyring.on(["*", "*", Events.CONNECT], this.handleDeviceConnect);
this.keyring.on(["*", "*", Events.DISCONNECT], this.handleDeviceDisconnect);
// This will fail out of sync if device is connected/disconnected
const pairedDevices = this.keyring.wallets;
this.adapters = adapters;
this.setState({
pairedDevices
});
}
if (device.vendorId !== VENDOR_ID) { continue }
// remove last connected ledger from keyring since we don't have unique identifier
if (!device.deviceID) {
device.deviceID = 'u2f-ledger'
await this.keyring.remove(device.deviceID)
}
if (this.keyring.wallets[device.deviceID]) { continue }
const ledgerTransport = await TransportU2F.open()
const wallet = createLedger(new LedgerU2FTransport(device, ledgerTransport, this.keyring))
this.keyring.add(wallet, device.deviceID)
this.keyring.emit(["Ledger", device.deviceID, Events.CONNECT], device.deviceID)
}
return Object.keys(this.keyring.wallets).length
}
private async handleConnectWebUSBLedger(e: USBConnectionEvent): Promise {
if (e.device.vendorId !== VENDOR_ID) return
this.connectTimestamp = e.timeStamp
try {
await this.initialize(e.device)
this.keyring.emit([e.device.manufacturerName, e.device.productName, Events.CONNECT], e.device.serialNumber)
} catch(error) {
this.keyring.emit([e.device.manufacturerName, e.device.productName, Events.FAILURE], [e.device.serialNumber, {message: { code: error.type, ...error}}])
} finally {
await timeout(APP_NAVIGATION_DELAY) // Allow connection to happen as fast as possible, but still give time to detect for app navigation before resetting timestamp
this.connectTimestamp = 0
}
}
.then(() => this.keyring.emit([device.productName, device.serialNumber, Events.CONNECT], device.serialNumber))
.catch(console.error)
private async handleConnectTrezor (event: any): Promise {
const { payload: { path, features: { device_id } } } = event
try {
await this.addDevice(device_id, path)
this.connectCacheFeatures(event)
this.keyring.emit(['Trezor', device_id, Events.CONNECT], device_id)
} catch (e) {
console.error(e)
}
}
public async pairDevice (): Promise {
const metamask = window && window['ethereum']
if (!metamask) return null
metamask.on('accountsChanged', (accts) => {
console.log('accounts changed to: ', accts)
})
const wallet = new MetaMaskHDWallet(metamask)
await wallet.initialize()
const deviceId = await wallet.getDeviceID()
this.keyring.add(wallet, deviceId)
this.keyring.emit(["MetaMask", deviceId, Events.CONNECT], deviceId)
return wallet
}
private async pairPortisDevice(): Promise {
this.portis = new Portis(this.portisAppId, 'mainnet')
const wallet = new PortisHDWallet(this.portis)
await wallet.initialize()
const deviceId = await wallet.getDeviceID()
this.keyring.add(wallet, deviceId)
this.currentDeviceId = deviceId
this.keyring.emit(["Portis", deviceId, Events.CONNECT], deviceId)
const watchForInactivity = () => {
let time
const resetTimer = () => {
clearTimeout(time)
time = setTimeout(() => {
window.onload = null
document.onmousemove = null
document.onkeypress = null
clearTimeout(time)
this.portis.logout()
}, INACTIVITY_LOGOUT_TIME)
}
window.onload = resetTimer
document.onmousemove = resetTimer
document.onkeypress = resetTimer
if (isKeepKey(wallet)) {
console.log("try connect debuglink")
await wallet.transport.tryConnectDebugLink()
}
await wallet.initialize()
}
window['wallet'] = wallet
})
wallet = keyring.get()
window['wallet'] = wallet
if (wallet) {
let deviceID = wallet.getDeviceID()
$keyring.val(deviceID).change()
}
keyring.on(['*', '*', Events.CONNECT], async (deviceId) => {
await deviceConnected(deviceId)
})
keyring.on(['*', '*', Events.DISCONNECT], async (deviceId) => {
$keyring.find(`option[value="${deviceId}"]`).remove()
})
})()
.then(() => () =>
this.keyring.emit([device.product, deviceID, Events.CONNECT], deviceID)
)
private async handleConnectWebUSBKeepKey (e: USBConnectionEvent): Promise {
try {
await this.initialize([e.device])
this.keyring.emit([e.device.productName, e.device.serialNumber, Events.CONNECT], e.device.serialNumber)
} catch(error) {
this.keyring.emit([e.device.productName, e.device.serialNumber, Events.FAILURE], [e.device.serialNumber, {message: { code: error.type, ...error}}])
}
}