|
|
@ -6,20 +6,6 @@ import ledgerco, { comm_node } from 'ledgerco' |
|
|
|
|
|
|
|
import HID from 'ledger-node-js-hid' |
|
|
|
|
|
|
|
ipcMain.on('listenDevices', event => { |
|
|
|
HID.listenDevices.start() |
|
|
|
|
|
|
|
HID.listenDevices.events.on( |
|
|
|
'add', |
|
|
|
device => isLedgerDevice(device) && event.sender.send('addDevice', device), |
|
|
|
) |
|
|
|
|
|
|
|
HID.listenDevices.events.on( |
|
|
|
'remove', |
|
|
|
device => isLedgerDevice(device) && event.sender.send('removeDevice', device), |
|
|
|
) |
|
|
|
}) |
|
|
|
|
|
|
|
async function getWalletInfos(path: string, wallet: string) { |
|
|
|
if (wallet === 'btc') { |
|
|
|
const comm = new comm_node(new HID.HID(path), true, 0, false) |
|
|
@ -30,16 +16,45 @@ async function getWalletInfos(path: string, wallet: string) { |
|
|
|
throw new Error('invalid wallet') |
|
|
|
} |
|
|
|
|
|
|
|
ipcMain.on('requestWalletInfos', async (event: *, payload) => { |
|
|
|
const { path, wallet } = payload |
|
|
|
try { |
|
|
|
const publicKey = await getWalletInfos(path, wallet) |
|
|
|
event.sender.send('receiveWalletInfos', { path, publicKey }) |
|
|
|
} catch (err) { |
|
|
|
event.sender.send('failWalletInfos', { path, err: err.stack }) |
|
|
|
const handlers = { |
|
|
|
listenDevices: send => { |
|
|
|
HID.listenDevices.start() |
|
|
|
HID.listenDevices.events.on( |
|
|
|
'add', |
|
|
|
device => isLedgerDevice(device) && send('addDevice', device), |
|
|
|
) |
|
|
|
HID.listenDevices.events.on( |
|
|
|
'remove', |
|
|
|
device => isLedgerDevice(device) && send('removeDevice', device), |
|
|
|
) |
|
|
|
}, |
|
|
|
requestWalletInfos: async (send, { path, wallet }) => { |
|
|
|
try { |
|
|
|
const publicKey = await getWalletInfos(path, wallet) |
|
|
|
send('receiveWalletInfos', { path, publicKey }) |
|
|
|
} catch (err) { |
|
|
|
send('failWalletInfos', { path, err: err.stack }) |
|
|
|
} |
|
|
|
}, |
|
|
|
getDevices: send => { |
|
|
|
send('updateDevices', HID.devices().filter(isLedgerDevice)) |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
ipcMain.on('msg', (event: *, payload) => { |
|
|
|
const { type, data } = payload |
|
|
|
|
|
|
|
const handler = handlers[type] |
|
|
|
if (!handler) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
const send = (msgType: string, data: *) => { |
|
|
|
event.sender.send('msg', { |
|
|
|
type: msgType, |
|
|
|
data, |
|
|
|
}) |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
ipcMain.on('getDevices', event => |
|
|
|
event.sender.send('updateDevices', HID.devices().filter(isLedgerDevice)), |
|
|
|
) |
|
|
|
handler(send, data) |
|
|
|
}) |
|
|
|