Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
HardwareWalletLedger.init = () => {
HardwareWalletLedger.unsubscribeTransport = Transport.listen({next: async (e) => {
const uid = hash(JSON.stringify({
manufacturer: e.manufacturer,
product : e.product ,
productId : e.productId ,
release : e.release ,
serialNumber: e.serialNumber,
vendorId : e.vendorId ,
})).toString('base64').substr(0, 8);
// console.log(uid, e)
try {
switch(e.type) {
case('add'): {
if(!HardwareWalletLedger.listOfLedgers.has(uid)) HardwareWalletLedger.listOfLedgers.set(uid, new HardwareWalletLedger(uid, e.device));
await HardwareWalletLedger.listOfLedgers.get(uid)._changeState(HWW_STATE.ONLINE, e.descriptor);
break;
}
constructor() {
// Electron main window to send command to renderer process
this.mainWindow = null;
// Ledger transport
this.transport = null;
// Timeout for ledger connection
this.timeout = 10000;
// If hathor app is opened on ledger
this.hathorAppOpened = false;
this.subscriptor = Transport.listen({
next: e => {
if (e.type === 'add') {
// All events called here we check if we are in the hathor app
this.getVersion();
} else if (e.type === 'remove') {
// This is called in some situations
// - Usb disconnected
// - When user quits an app
// - When user opens an app
if (this.hathorAppOpened) {
// If was opened we schedule a setTimeout
// to lock the wallet in case it continues closed
setTimeout(() => {
if (!this.hathorAppOpened) {
this.mainWindow.webContents.send("ledger:closed");
}
function waitForDevices(onDevice) {
console.log(`> Waiting for device...`)
CommNodeHid.listen({
error: () => {},
complete: () => {},
next: async e => {
if (!e.device) {
return
}
if (e.type === 'add') {
console.log(`> Detected ${e.device.manufacturer} ${e.device.product}`)
onDevice(e.device)
}
if (e.type === 'remove') {
console.log(`removed ${JSON.stringify(e)}`)
}
},
})
}
return new Promise(resolve => {
const sub = Transport.listen({
next: async e => {
if (e.type === "add") {
sub.unsubscribe();
resolve(e);
}
},
error: error => {
console.log("GOT ERROR", error);
},
complete: () => {
console.log("DONE");
}
});
});
}
action.interval = setInterval(() => {
if (sub === null) {
sub = Transport.listen({
next: e => {
const { ledger } = store.getState();
store.dispatch({
type: types.DEVICE_CONNECTED,
devicePath: e.device.path
});
if (e.type === 'add' && (ledger.devicePath !== e.device.path && ledger.publicKey === null)) {
sub.unsubscribe();
sub = null;
Transport.open(e.device.path).then(transport => {
transport.setDebugMode(true);
const api = new Api(transport);
api.getPublicKey("44'/194'/0'/0/0").then(result => {
transport.close();
static async remove(device){
openedTransport = null;
transport = Transport.listen({next:({type, device}) => this[type](device)});
}
}
static setup(){
transport = Transport.listen({next:({type, device}) => this[type](device)});
}
return (dispatch: () => void, getState) => {
const currentState = getState();
dispatch({
type: types.APP_LEDGER_CONNECTION_STATUS,
deviceConnected: !!currentState.ledger.transport
});
if (getState().ledger.subscriber !== null) {
return;
}
const subscriber = Transport.listen({
next: event => {
if (event.type === 'add') {
if (getState().ledger.devicePath === null) {
if (
getState().ledger.devicePath !== event.device.path &&
getState().ledger.application === null
) {
dispatch(stopListen());
Transport.open(event.device.path)
.then(transport => {
if (process.env.NODE_ENV === 'development') {
transport.setDebugMode(true);
}
dispatch({
return (dispatch: () => void, getState) => {
hardwareLedger.destroy();
if (getState().ledger.subscriber !== null) {
return;
}
dispatch({
type: types.HARDWARE_LEDGER_LISTEN_INIT
});
if (process.env.NODE_ENV === 'development') {
Transport.setListenDevicesDebug(true);
}
const subscriber = Transport.listen({
complete: () => dispatch(handleComplete()),
error: (error) => dispatch(handleError(error)),
next: (event) => dispatch(handleEvent(event)),
});
dispatch({
payload: {
subscriber
},
type: types.HARDWARE_LEDGER_LISTEN_START,
});
};
}