From 32ef049a026a3bc780c8982903cddbdf07eb9d1c Mon Sep 17 00:00:00 2001 From: meriadec Date: Tue, 9 Jan 2018 17:35:50 +0100 Subject: [PATCH] request wallet info from device --- src/main/ledger.js | 21 +++++++++++++++++++++ src/renderer/i18n.js | 2 +- src/renderer/initEvents.js | 16 +++++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/main/ledger.js b/src/main/ledger.js index 025b2b7c..a24fe415 100644 --- a/src/main/ledger.js +++ b/src/main/ledger.js @@ -2,6 +2,7 @@ import { ipcMain } from 'electron' import { isLedgerDevice } from 'ledgerco/lib/utils' +import ledgerco, { comm_node } from 'ledgerco' import HID from 'ledger-node-js-hid' @@ -19,6 +20,26 @@ ipcMain.on('listenDevices', event => { ) }) +async function getWalletInfos(path: string, wallet: string) { + if (wallet === 'btc') { + const comm = new comm_node(new HID.HID(path), true, 0, false) + const btc = new ledgerco.btc(comm) + const walletInfos = await btc.getWalletPublicKey_async("44'/0'/0'/0") + return walletInfos + } + 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 }) + } +}) + ipcMain.on('getDevices', event => event.sender.send('updateDevices', HID.devices().filter(isLedgerDevice)), ) diff --git a/src/renderer/i18n.js b/src/renderer/i18n.js index ad677a7f..82f635e3 100644 --- a/src/renderer/i18n.js +++ b/src/renderer/i18n.js @@ -7,7 +7,7 @@ import Backend from 'i18next-node-fs-backend' i18n.use(Backend).init({ lng: 'en', fallbackLng: 'en', - debug: true, + debug: false, backend: { loadPath: path.resolve(__dirname, '../i18n/{{lng}}/{{ns}}.yml'), }, diff --git a/src/renderer/initEvents.js b/src/renderer/initEvents.js index 8b936f9f..7ae8908e 100644 --- a/src/renderer/initEvents.js +++ b/src/renderer/initEvents.js @@ -5,10 +5,24 @@ import { ipcRenderer } from 'electron' import { devicesUpdate, deviceAdd, deviceRemove } from 'actions/devices' export default (store: Object) => { - ipcRenderer.on('updateDevices', (e, devices) => store.dispatch(devicesUpdate(devices))) + ipcRenderer.on('updateDevices', (e, devices) => { + store.dispatch(devicesUpdate(devices)) + if (devices.length) { + ipcRenderer.send('requestWalletInfos', { path: devices[0].path, wallet: 'btc' }) + } + }) + ipcRenderer.on('addDevice', (e, device) => store.dispatch(deviceAdd(device))) ipcRenderer.on('removeDevice', (e, device) => store.dispatch(deviceRemove(device))) + ipcRenderer.on('receiveWalletInfos', (e, { path, publicKey }) => { + console.log({ path, publicKey }) + }) + + ipcRenderer.on('failWalletInfos', (e, { path, err }) => { + console.log({ path, err }) + }) + // First time, we get all devices ipcRenderer.send('getDevices')