diff --git a/src/actions/devices.js b/src/actions/devices.js index e61923f0..751d4b4c 100644 --- a/src/actions/devices.js +++ b/src/actions/devices.js @@ -1,6 +1,6 @@ // @flow -import type { Device, Devices } from 'types/common' +import type { Device } from 'types/common' export type SetCurrentDevice = (Device | null) => { type: string, payload: Device | null } export const setCurrentDevice: SetCurrentDevice = payload => ({ @@ -19,9 +19,3 @@ export const removeDevice: RemoveDevice = payload => ({ type: 'REMOVE_DEVICE', payload, }) - -type UpdateDevices = Devices => { type: string, payload: Devices } -export const updateDevices: UpdateDevices = payload => ({ - type: 'UPDATE_DEVICES', - payload, -}) diff --git a/src/components/DeviceCheckAddress.js b/src/components/DeviceCheckAddress.js index 5551f7fd..485972d4 100644 --- a/src/components/DeviceCheckAddress.js +++ b/src/components/DeviceCheckAddress.js @@ -37,14 +37,14 @@ class CheckAddress extends PureComponent { handleMsgEvent = (e: any, { type }: { type: string }) => { const { onCheck } = this.props - if (type === 'wallet.verifyAddress.success') { + if (type === 'accounts.verifyAddress.success') { this.setState({ isVerified: true, }) onCheck(true) } - if (type === 'wallet.verifyAddress.fail') { + if (type === 'accounts.verifyAddress.fail') { this.setState({ isVerified: false, }) @@ -53,7 +53,7 @@ class CheckAddress extends PureComponent { } verifyAddress = ({ device, account }: { device: Device, account: Account }) => - sendEvent('usb', 'wallet.verifyAddress', { + sendEvent('accounts', 'verifyAddress', { pathDevice: device.path, path: account.path, segwit: account.path.startsWith("49'"), // TODO: store segwit info in account diff --git a/src/components/DeviceConnect/index.js b/src/components/DeviceConnect/index.js index 7373804a..fa267e99 100644 --- a/src/components/DeviceConnect/index.js +++ b/src/components/DeviceConnect/index.js @@ -5,7 +5,7 @@ import styled from 'styled-components' import { Trans, translate } from 'react-i18next' import type { CryptoCurrency } from '@ledgerhq/live-common/lib/types' -import type { T, Device, Devices } from 'types/common' +import type { T, Device } from 'types/common' import noop from 'lodash/noop' @@ -139,7 +139,7 @@ type Props = { accountName: null | string, appOpened: null | 'success' | 'fail', currency: CryptoCurrency, - devices: Devices, + devices: Device[], deviceSelected: Device | null, onChangeDevice: Function, t: T, diff --git a/src/components/DeviceMonit/index.js b/src/components/DeviceMonit/index.js index 893fbbb7..c40084b3 100644 --- a/src/components/DeviceMonit/index.js +++ b/src/components/DeviceMonit/index.js @@ -80,7 +80,7 @@ class DeviceMonit extends PureComponent { return } - sendEvent('usb', 'wallet.checkIfAppOpened', { + sendEvent('devices', 'checkIfAppOpened', { devicePath: currentDevice.path, accountPath: account.path, accountAddress: account.address, @@ -97,12 +97,12 @@ class DeviceMonit extends PureComponent { } handleMsgEvent = (e, { type }) => { - if (type === 'wallet.checkIfAppOpened.success') { + if (type === 'devices.checkIfAppOpened.success') { this.handleStatusChange('appOpened') clearTimeout(this._timeout) } - if (type === 'wallet.checkIfAppOpened.fail') { + if (type === 'devices.checkIfAppOpened.fail') { this._timeout = setTimeout(this.checkAppOpened, 1e3) } } diff --git a/src/components/DeviceMonitNew/index.js b/src/components/DeviceMonitNew/index.js index bb04197f..d264c18c 100644 --- a/src/components/DeviceMonitNew/index.js +++ b/src/components/DeviceMonitNew/index.js @@ -5,7 +5,7 @@ import { connect } from 'react-redux' import { ipcRenderer } from 'electron' import type { Account, CryptoCurrency } from '@ledgerhq/live-common/lib/types' -import type { Device, Devices } from 'types/common' +import type { Device } from 'types/common' import { sendEvent } from 'renderer/events' import { getDevices } from 'reducers/devices' @@ -20,14 +20,14 @@ type OwnProps = { render?: ({ appStatus: AppStatus, currency: CryptoCurrency, - devices: Devices, + devices: Device[], deviceSelected: ?Device, deviceStatus: DeviceStatus, }) => React$Element<*>, } type Props = OwnProps & { - devices: Devices, + devices: Device[], } type DeviceStatus = 'unconnected' | 'connected' @@ -111,7 +111,7 @@ class DeviceMonit extends PureComponent { } } - sendEvent('usb', 'wallet.checkIfAppOpened', { + sendEvent('devices', 'checkIfAppOpened', { devicePath: deviceSelected.path, ...options, }) @@ -134,12 +134,12 @@ class DeviceMonit extends PureComponent { return } - if (type === 'wallet.checkIfAppOpened.success' && deviceSelected.path === data.devicePath) { + if (type === 'devices.checkIfAppOpened.success' && deviceSelected.path === data.devicePath) { this.handleStatusChange(deviceStatus, 'success') this._timeout = setTimeout(this.checkAppOpened, 1e3) } - if (type === 'wallet.checkIfAppOpened.fail' && deviceSelected.path === data.devicePath) { + if (type === 'devices.checkIfAppOpened.fail' && deviceSelected.path === data.devicePath) { this.handleStatusChange(deviceStatus, 'fail') this._timeout = setTimeout(this.checkAppOpened, 1e3) } diff --git a/src/components/ManagerPage/AppsList.js b/src/components/ManagerPage/AppsList.js index 02e3c67e..77d06ecb 100644 --- a/src/components/ManagerPage/AppsList.js +++ b/src/components/ManagerPage/AppsList.js @@ -4,7 +4,7 @@ import React, { PureComponent } from 'react' import styled from 'styled-components' import { translate } from 'react-i18next' -import { runJob } from 'renderer/events' +import runJob from 'renderer/runJob' import Box from 'components/base/Box' import Modal, { ModalBody } from 'components/base/Modal' @@ -74,11 +74,12 @@ class AppsList extends PureComponent { _unmounted = false async fetchList() { + console.log(`fetching app list`) const appsList = CACHED_APPS || (await runJob({ - channel: 'usb', - job: 'manager.listApps', + channel: 'manager', + job: 'listApps', successResponse: 'manager.listAppsSuccess', errorResponse: 'manager.listAppsError', })) diff --git a/src/components/ManagerPage/DeviceInfos.js b/src/components/ManagerPage/DeviceInfos.js index 8b8ec5d8..aae54293 100644 --- a/src/components/ManagerPage/DeviceInfos.js +++ b/src/components/ManagerPage/DeviceInfos.js @@ -2,7 +2,8 @@ import React, { PureComponent } from 'react' -import { runJob } from 'renderer/events' +import runJob from 'renderer/runJob' + import Text from 'components/base/Text' import Box, { Card } from 'components/base/Box' import Button from 'components/base/Button' @@ -33,10 +34,10 @@ class DeviceInfos extends PureComponent { device: { path: devicePath }, } = this.props const memoryInfos = await runJob({ - channel: 'usb', - job: 'manager.getMemInfos', - successResponse: 'device.getMemInfosSuccess', - errorResponse: 'device.getMemInfosError', + channel: 'manager', + job: 'getMemInfos', + successResponse: 'manager.getMemInfosSuccess', + errorResponse: 'manager.getMemInfosError', data: { devicePath }, }) this.setState({ memoryInfos, isLoading: false }) diff --git a/src/components/modals/AddAccount/index.js b/src/components/modals/AddAccount/index.js index 0e39e527..b632a88c 100644 --- a/src/components/modals/AddAccount/index.js +++ b/src/components/modals/AddAccount/index.js @@ -20,7 +20,7 @@ import { decodeAccount, } from 'reducers/accounts' -import { runJob } from 'renderer/events' +import runJob from 'renderer/runJob' import { addAccount, updateAccount } from 'actions/accounts' @@ -120,10 +120,10 @@ class AddAccountModal extends PureComponent { try { // scan every account for given currency and device await runJob({ - channel: 'usb', - job: 'wallet.scanAccountsOnDevice', - successResponse: 'wallet.scanAccountsOnDevice.success', - errorResponse: 'wallet.scanAccountsOnDevice.fail', + channel: 'accounts', + job: 'scan', + successResponse: 'accounts.scanAccountsOnDevice.success', + errorResponse: 'accounts.scanAccountsOnDevice.fail', data: { devicePath: deviceSelected.path, currencyId: currency.id, @@ -186,7 +186,7 @@ class AddAccountModal extends PureComponent { handleMsgEvent = (e, { data, type }) => { const { addAccount, existingAccounts } = this.props - if (type === 'wallet.scanAccountsOnDevice.accountScanned') { + if (type === 'accounts.scanAccountsOnDevice.accountScanned') { // create Account from AccountRaw account scanned on device const account = { ...decodeAccount(data), diff --git a/src/internals/usb/wallet/accounts.js b/src/internals/accounts/helpers.js similarity index 100% rename from src/internals/usb/wallet/accounts.js rename to src/internals/accounts/helpers.js diff --git a/src/internals/accounts/index.js b/src/internals/accounts/index.js index 3a7b6968..2d3837e7 100644 --- a/src/internals/accounts/index.js +++ b/src/internals/accounts/index.js @@ -1 +1,69 @@ -export sync from './sync' +// @flow + +import CommNodeHid from '@ledgerhq/hw-transport-node-hid' + +import type { IPCSend } from 'types/electron' + +import scanAccountsOnDevice from './scanAccountsOnDevice' +import { verifyAddress, getFreshReceiveAddress } from './helpers' + +import sync from './sync' + +export default { + sync, + scan: async ( + send: IPCSend, + { + devicePath, + currencyId, + }: { + devicePath: string, + currencyId: string, + }, + ) => { + try { + send('accounts.scanAccountsOnDevice.start', { pid: process.pid }, { kill: false }) + const accounts = await scanAccountsOnDevice({ + devicePath, + currencyId, + onAccountScanned: account => { + send('accounts.scanAccountsOnDevice.accountScanned', account, { kill: false }) + }, + }) + send('accounts.scanAccountsOnDevice.success', accounts) + } catch (err) { + send('accounts.scanAccountsOnDevice.fail', err) + } + }, + + getFreshReceiveAddress: async ( + send: IPCSend, + { + currencyId, + accountIndex, + }: { + currencyId: string, + accountIndex: number, + }, + ) => { + try { + const freshAddress = await getFreshReceiveAddress({ currencyId, accountIndex }) + send('accounts.getFreshReceiveAddress.success', freshAddress) + } catch (err) { + send('accounts.getFreshReceiveAddress.fail', err) + } + }, + + verifyAddress: async ( + send: IPCSend, + { pathDevice, path }: { pathDevice: string, path: string }, + ) => { + const transport = await CommNodeHid.open(pathDevice) + try { + await verifyAddress({ transport, path }) + send('accounts.verifyAddress.success') + } catch (err) { + send('accounts.verifyAddress.fail') + } + }, +} diff --git a/src/internals/usb/wallet/scanAccountsOnDevice.js b/src/internals/accounts/scanAccountsOnDevice.js similarity index 100% rename from src/internals/usb/wallet/scanAccountsOnDevice.js rename to src/internals/accounts/scanAccountsOnDevice.js diff --git a/src/internals/accounts/sync.js b/src/internals/accounts/sync.js index 6cb471b8..67281475 100644 --- a/src/internals/accounts/sync.js +++ b/src/internals/accounts/sync.js @@ -1,7 +1,7 @@ // @flow -export default (send: Function) => ({ - all: () => { - setTimeout(() => send('accounts.sync.success'), 5e3) - }, -}) +import type { IPCSend } from 'types/electron' + +export default (send: IPCSend) => { + setTimeout(() => send('accounts.sync.success'), 5e3) +} diff --git a/src/internals/devices/checkIfAppOpened.js b/src/internals/devices/checkIfAppOpened.js new file mode 100644 index 00000000..4e2eb4e2 --- /dev/null +++ b/src/internals/devices/checkIfAppOpened.js @@ -0,0 +1,44 @@ +// @flow + +import CommNodeHid from '@ledgerhq/hw-transport-node-hid' +import Btc from '@ledgerhq/hw-app-btc' + +import type { IPCSend } from 'types/electron' + +import { getPath } from 'internals/accounts/helpers' + +export default async ( + send: IPCSend, + { + currencyId, + devicePath, + accountPath, + accountAddress, + segwit = true, + }: { + currencyId?: string, + devicePath: string, + accountPath: string, + accountAddress: string, + segwit: boolean, + }, +) => { + try { + const transport = await CommNodeHid.open(devicePath) + const btc = new Btc(transport) + if (accountPath) { + const { bitcoinAddress } = await btc.getWalletPublicKey(accountPath, false, segwit) + if (bitcoinAddress === accountAddress) { + send('devices.checkIfAppOpened.success', { devicePath }) + } else { + throw new Error('Address is different') + } + } + if (currencyId) { + await btc.getWalletPublicKey(getPath({ currencyId, segwit }), false, segwit) + send('devices.checkIfAppOpened.success', { devicePath }) + } + } catch (err) { + send('devices.checkIfAppOpened.fail', { devicePath }) + } +} diff --git a/src/internals/devices/index.js b/src/internals/devices/index.js new file mode 100644 index 00000000..008738f3 --- /dev/null +++ b/src/internals/devices/index.js @@ -0,0 +1,2 @@ +export listen from './listen' +export checkIfAppOpened from './checkIfAppOpened' diff --git a/src/internals/devices/listen.js b/src/internals/devices/listen.js new file mode 100644 index 00000000..5c5c68e4 --- /dev/null +++ b/src/internals/devices/listen.js @@ -0,0 +1,24 @@ +// @flow + +import CommNodeHid from '@ledgerhq/hw-transport-node-hid' +import noop from 'lodash/noop' + +import type { IPCSend } from 'types/electron' + +export default (send: IPCSend) => { + CommNodeHid.listen({ + error: noop, + complete: noop, + next: async e => { + if (!e.device) { + return + } + if (e.type === 'add') { + send('device.add', e.device, { kill: false }) + } + if (e.type === 'remove') { + send('device.remove', e.device, { kill: false }) + } + }, + }) +} diff --git a/src/internals/index.js b/src/internals/index.js index 5f60476c..d7ea0a4a 100644 --- a/src/internals/index.js +++ b/src/internals/index.js @@ -17,23 +17,21 @@ function sendEvent(type: string, data: any, options: Object = { kill: true }) { } // $FlowFixMe -const func = require(`./${FORK_TYPE}`) // eslint-disable-line import/no-dynamic-require - -const handlers = Object.keys(func).reduce((result, key) => { - result[key] = func[key](sendEvent) - return result -}, {}) +let handlers = require(`./${FORK_TYPE}`) // eslint-disable-line import/no-dynamic-require +// handle babel export object syntax +if (handlers.default) { + handlers = handlers.default +} -const onMessage = payload => { +process.on('message', payload => { const { type, data } = payload const handler = objectPath.get(handlers, type) if (!handler) { + console.warn(`No handler found for ${type}`) return } - handler(data) -} - -process.on('message', onMessage) + handler(sendEvent, data) +}) if (__DEV__ || DEV_TOOLS) { cpuUsage(cpuPercent => diff --git a/src/internals/usb/manager/constants.js b/src/internals/manager/constants.js similarity index 100% rename from src/internals/usb/manager/constants.js rename to src/internals/manager/constants.js diff --git a/src/internals/manager/getMemInfos.js b/src/internals/manager/getMemInfos.js new file mode 100644 index 00000000..34845888 --- /dev/null +++ b/src/internals/manager/getMemInfos.js @@ -0,0 +1,12 @@ +// @flow + +import type { IPCSend } from 'types/electron' + +import { createTransportHandler, getMemInfos } from './helpers' + +export default (send: IPCSend, data: any) => + createTransportHandler(send, { + action: getMemInfos, + successResponse: 'manager.getMemInfosSuccess', + errorResponse: 'manager.getMemInfosError', + })(data) diff --git a/src/internals/usb/manager/helpers.js b/src/internals/manager/helpers.js similarity index 100% rename from src/internals/usb/manager/helpers.js rename to src/internals/manager/helpers.js diff --git a/src/internals/manager/index.js b/src/internals/manager/index.js new file mode 100644 index 00000000..d00dd73f --- /dev/null +++ b/src/internals/manager/index.js @@ -0,0 +1,22 @@ +// @flow + +/** + * Manager + * ------- + * + * xXx + * xXx + * xXx + * xxxXxxx + * xxXxx + * xXx + * xX x Xx + * xX Xx + * xxXXXXXXXxx + * + */ + +export { default as getMemInfos } from './getMemInfos' +export { default as installApp } from './installApp' +export { default as listApps } from './listApps' +export { default as uninstallApp } from './uninstallApp' diff --git a/src/internals/manager/installApp.js b/src/internals/manager/installApp.js new file mode 100644 index 00000000..566ed9fe --- /dev/null +++ b/src/internals/manager/installApp.js @@ -0,0 +1,12 @@ +// @flow + +import type { IPCSend } from 'types/electron' + +import { createTransportHandler, installApp } from './helpers' + +export default (send: IPCSend, data: any) => + createTransportHandler(send, { + action: installApp, + successResponse: 'manager.appInstalled', + errorResponse: 'manager.appInstallError', + })(data) diff --git a/src/internals/manager/listApps.js b/src/internals/manager/listApps.js new file mode 100644 index 00000000..2aceeab4 --- /dev/null +++ b/src/internals/manager/listApps.js @@ -0,0 +1,14 @@ +// @flow + +import axios from 'axios' + +import type { IPCSend } from 'types/electron' + +export default async (send: IPCSend) => { + try { + const { data } = await axios.get('https://api.ledgerwallet.com/update/applications') + send('manager.listAppsSuccess', data['nanos-1.4']) + } catch (err) { + send('manager.listAppsError', { message: err.message, stack: err.stack }) + } +} diff --git a/src/internals/manager/uninstallApp.js b/src/internals/manager/uninstallApp.js new file mode 100644 index 00000000..233f8953 --- /dev/null +++ b/src/internals/manager/uninstallApp.js @@ -0,0 +1,12 @@ +// @flow + +import type { IPCSend } from 'types/electron' + +import { createTransportHandler, uninstallApp } from './helpers' + +export default (send: IPCSend, data: any) => + createTransportHandler(send, { + action: uninstallApp, + successResponse: 'manager.appUninstalled', + errorResponse: 'manager.appUninstallError', + })(data) diff --git a/src/internals/usb/devices.js b/src/internals/usb/devices.js deleted file mode 100644 index 96ff8b11..00000000 --- a/src/internals/usb/devices.js +++ /dev/null @@ -1,28 +0,0 @@ -// @flow - -import CommNodeHid from '@ledgerhq/hw-transport-node-hid' -import noop from 'lodash/noop' - -import type { IPCSend } from 'types/electron' - -export default (send: IPCSend) => ({ - listen: () => { - CommNodeHid.listen({ - error: noop, - complete: noop, - next: async e => { - if (!e.device) { - return - } - - if (e.type === 'add') { - send('device.add', e.device, { kill: false }) - } - - if (e.type === 'remove') { - send('device.remove', e.device, { kill: false }) - } - }, - }) - }, -}) diff --git a/src/internals/usb/index.js b/src/internals/usb/index.js deleted file mode 100644 index 460de078..00000000 --- a/src/internals/usb/index.js +++ /dev/null @@ -1,3 +0,0 @@ -export devices from './devices' -export wallet from './wallet' -export manager from './manager' diff --git a/src/internals/usb/manager/index.js b/src/internals/usb/manager/index.js deleted file mode 100644 index cac154a0..00000000 --- a/src/internals/usb/manager/index.js +++ /dev/null @@ -1,50 +0,0 @@ -// @flow - -/** - * Manager - * ------- - * - * xXx - * xXx - * xXx - * xxxXxxx - * xxXxx - * xXx - * xX x Xx - * xX Xx - * xxXXXXXXXxx - * - */ - -import type { IPCSend } from 'types/electron' -import axios from 'axios' -import { createTransportHandler, installApp, uninstallApp, getMemInfos } from './helpers' - -export default (send: IPCSend) => ({ - installApp: createTransportHandler(send, { - action: installApp, - successResponse: 'device.appInstalled', - errorResponse: 'device.appInstallError', - }), - - uninstallApp: createTransportHandler(send, { - action: uninstallApp, - successResponse: 'device.appUninstalled', - errorResponse: 'device.appUninstallError', - }), - - getMemInfos: createTransportHandler(send, { - action: getMemInfos, - successResponse: 'device.getMemInfosSuccess', - errorResponse: 'device.getMemInfosError', - }), - - listApps: async () => { - try { - const { data } = await axios.get('https://api.ledgerwallet.com/update/applications') - send('manager.listAppsSuccess', data['nanos-1.4']) - } catch (err) { - send('manager.listAppsError', { message: err.message, stack: err.stack }) - } - }, -}) diff --git a/src/internals/usb/wallet/index.js b/src/internals/usb/wallet/index.js deleted file mode 100644 index e288b164..00000000 --- a/src/internals/usb/wallet/index.js +++ /dev/null @@ -1,105 +0,0 @@ -// @flow - -import CommNodeHid from '@ledgerhq/hw-transport-node-hid' -import Btc from '@ledgerhq/hw-app-btc' - -import { getPath, verifyAddress, getFreshReceiveAddress } from './accounts' -import scanAccountsOnDevice from './scanAccountsOnDevice' - -export default (sendEvent: Function) => ({ - /** - * Scan all the accounts for the given device and currency and returns them - */ - scanAccountsOnDevice: async ({ - devicePath, - currencyId, - }: { - devicePath: string, - currencyId: string, - }) => { - try { - sendEvent('wallet.scanAccountsOnDevice.start', { pid: process.pid }, { kill: false }) - const accounts = await scanAccountsOnDevice({ - devicePath, - currencyId, - onAccountScanned: account => { - sendEvent('wallet.scanAccountsOnDevice.accountScanned', account, { kill: false }) - }, - }) - sendEvent('wallet.scanAccountsOnDevice.success', accounts) - } catch (err) { - sendEvent('wallet.scanAccountsOnDevice.fail', err) - } - }, - getAccounts: async ({ - pathDevice, - currencyId, - currentAccounts, - }: { - pathDevice: string, - currencyId: string, - currentAccounts: Array, - }) => { - console.warn( - `NOT IMPLEMENTED: getting account for ${pathDevice} ${currencyId} ${currentAccounts.length}`, - ) - }, - getFreshReceiveAddress: async ({ - currencyId, - accountIndex, - }: { - currencyId: string, - accountIndex: number, - }) => { - try { - console.log(accountIndex) - const freshAddress = await getFreshReceiveAddress({ currencyId, accountIndex }) - sendEvent('wallet.getFreshReceiveAddress.success', freshAddress) - } catch (err) { - sendEvent('wallet.getFreshReceiveAddress.fail', err) - } - }, - verifyAddress: async ({ pathDevice, path }: { pathDevice: string, path: string }) => { - const transport = await CommNodeHid.open(pathDevice) - - try { - await verifyAddress({ transport, path }) - - sendEvent('wallet.verifyAddress.success') - } catch (err) { - sendEvent('wallet.verifyAddress.fail') - } - }, - checkIfAppOpened: async ({ - currencyId, - devicePath, - accountPath, - accountAddress, - segwit = true, - }: { - currencyId?: string, - devicePath: string, - accountPath: string, - accountAddress: string, - segwit: boolean, - }) => { - try { - const transport = await CommNodeHid.open(devicePath) - const btc = new Btc(transport) - if (accountPath) { - const { bitcoinAddress } = await btc.getWalletPublicKey(accountPath, false, segwit) - if (bitcoinAddress === accountAddress) { - sendEvent('wallet.checkIfAppOpened.success', { devicePath }) - } else { - throw new Error('Address is different') - } - } - if (currencyId) { - await btc.getWalletPublicKey(getPath({ currencyId, segwit }), false, segwit) - sendEvent('wallet.checkIfAppOpened.success', { devicePath }) - } - } catch (err) { - sendEvent('wallet.checkIfAppOpened.fail', { devicePath }) - } - }, -}) diff --git a/src/main/bridge.js b/src/main/bridge.js index 7d1da5c2..c84da7d7 100644 --- a/src/main/bridge.js +++ b/src/main/bridge.js @@ -25,7 +25,7 @@ function sendEventToWindow(name, { type, data }) { } } -function onForkChannel(forkType, callType) { +function onForkChannel(forkType) { return (event: any, payload) => { const { type, data } = payload @@ -51,12 +51,7 @@ function onForkChannel(forkType, callType) { if (options.window) { sendEventToWindow(options.window, { type, data }) } else { - if (callType === 'async') { - event.sender.send('msg', { type, data }) - } - if (callType === 'sync') { - event.returnValue = { type, data } - } + event.sender.send('msg', { type, data }) } if (options.kill && compute) { kill() @@ -71,8 +66,9 @@ function onForkChannel(forkType, callType) { } // Forwards every `type` messages to another process -ipcMain.on('usb', onForkChannel('usb', 'async')) -ipcMain.on('accounts', onForkChannel('accounts', 'async')) +ipcMain.on('devices', onForkChannel('devices')) +ipcMain.on('accounts', onForkChannel('accounts')) +ipcMain.on('manager', onForkChannel('manager')) ipcMain.on('clean-processes', cleanProcesses) @@ -94,6 +90,7 @@ ipcMain.on('msg', (event: any, payload) => { const { type, data } = payload const handler = objectPath.get(handlers, type) if (!handler) { + console.warn(`No handler found for ${type}`) return } const send = (type: string, data: *) => event.sender.send('msg', { type, data }) diff --git a/src/reducers/devices.js b/src/reducers/devices.js index f0254240..ed2154da 100644 --- a/src/reducers/devices.js +++ b/src/reducers/devices.js @@ -2,11 +2,11 @@ import { handleActions } from 'redux-actions' -import type { Device, Devices } from 'types/common' +import type { Device } from 'types/common' export type DevicesState = { currentDevice: Device | null, - devices: Devices, + devices: Device[], } const state: DevicesState = { @@ -20,11 +20,6 @@ function setCurrentDevice(state) { } const handlers: Object = { - UPDATE_DEVICES: (state: DevicesState, { payload: devices }: { payload: Devices }) => - setCurrentDevice({ - ...state, - devices, - }), ADD_DEVICE: (state: DevicesState, { payload: device }: { payload: Device }) => setCurrentDevice({ ...state, diff --git a/src/renderer/events.js b/src/renderer/events.js index af7331cf..0a702eed 100644 --- a/src/renderer/events.js +++ b/src/renderer/events.js @@ -14,7 +14,7 @@ import { isLocked } from 'reducers/application' import { setUpdateStatus } from 'reducers/update' import { updateAccount } from 'actions/accounts' -import { updateDevices, addDevice, removeDevice } from 'actions/devices' +import { addDevice, removeDevice } from 'actions/devices' import i18n from 'renderer/i18n/electron' @@ -41,37 +41,6 @@ export function sendEvent(channel: string, msgType: string, data: any) { }) } -export function runJob({ - channel, - job, - successResponse, - errorResponse, - data, -}: { - channel: string, - job: string, - successResponse: string, - errorResponse: string, - data?: any, -}): Promise { - return new Promise((resolve, reject) => { - ipcRenderer.send(channel, { type: job, data }) - ipcRenderer.on('msg', handler) - function handler(e, res) { - const { type, data } = res - if (![successResponse, errorResponse].includes(type)) { - return - } - ipcRenderer.removeListener('msg', handler) - if (type === successResponse) { - resolve(data) - } else if (type === errorResponse) { - reject(data) - } - } - }) -} - export function sendSyncEvent(channel: string, msgType: string, data: any): any { return ipcRenderer.sendSync(`${channel}:sync`, { type: msgType, @@ -79,35 +48,6 @@ export function sendSyncEvent(channel: string, msgType: string, data: any): any }) } -export function startSyncAccounts(accounts: Account[]) { - d.sync('Sync accounts - start') - syncAccountsInProgress = true - sendEvent('accounts', 'sync.all', { - accounts: accounts.map(account => { - const { id, currency, rootPath, addresses, index, operations } = account - return { - id, - currencyId: currency.id, - allAddresses: addresses, - currentIndex: index, - rootPath, - operations, - } - }), - }) -} - -export function stopSyncAccounts() { - d.sync('Sync accounts - stop') - syncAccountsInProgress = false - clearTimeout(syncAccountsTimeout) -} - -export function checkUpdates() { - d.update('Update - check') - setTimeout(() => sendEvent('msg', 'updater.init'), CHECK_UPDATE_DELAY) -} - export default ({ store, locked }: { store: Object, locked: boolean }) => { const handlers = { dispatch: ({ type, payload }) => store.dispatch({ type, payload }), @@ -170,11 +110,6 @@ export default ({ store, locked }: { store: Object, locked: boolean }) => { }, }, }, - devices: { - update: devices => { - store.dispatch(updateDevices(devices)) - }, - }, device: { add: device => { d.device('Device - add') @@ -208,7 +143,7 @@ export default ({ store, locked }: { store: Object, locked: boolean }) => { ipcRenderer.send('clean-processes') // Start detection when we plug/unplug devices - sendEvent('usb', 'devices.listen') + sendEvent('devices', 'listen') const state = store.getState() @@ -226,3 +161,32 @@ export default ({ store, locked }: { store: Object, locked: boolean }) => { checkUpdates() } } + +export function startSyncAccounts(accounts: Account[]) { + d.sync('Sync accounts - start') + syncAccountsInProgress = true + sendEvent('accounts', 'sync', { + accounts: accounts.map(account => { + const { id, currency, rootPath, addresses, index, operations } = account + return { + id, + currencyId: currency.id, + allAddresses: addresses, + currentIndex: index, + rootPath, + operations, + } + }), + }) +} + +export function stopSyncAccounts() { + d.sync('Sync accounts - stop') + syncAccountsInProgress = false + clearTimeout(syncAccountsTimeout) +} + +export function checkUpdates() { + d.update('Update - check') + setTimeout(() => sendEvent('msg', 'updater.init'), CHECK_UPDATE_DELAY) +} diff --git a/src/renderer/runJob.js b/src/renderer/runJob.js new file mode 100644 index 00000000..fa136b48 --- /dev/null +++ b/src/renderer/runJob.js @@ -0,0 +1,34 @@ +// @flow + +import { ipcRenderer } from 'electron' + +export default function runJob({ + channel, + job, + successResponse, + errorResponse, + data, +}: { + channel: string, + job: string, + successResponse: string, + errorResponse: string, + data?: any, +}): Promise { + return new Promise((resolve, reject) => { + ipcRenderer.send(channel, { type: job, data }) + ipcRenderer.on('msg', handler) + function handler(e, res) { + const { type, data } = res + if (![successResponse, errorResponse].includes(type)) { + return + } + ipcRenderer.removeListener('msg', handler) + if (type === successResponse) { + resolve(data) + } else if (type === errorResponse) { + reject(data) + } + } + }) +} diff --git a/src/types/common.js b/src/types/common.js index 0e39aeae..9305887e 100644 --- a/src/types/common.js +++ b/src/types/common.js @@ -8,8 +8,6 @@ export type Device = { vendorId: string, } -export type Devices = Array - // -------------------- Settings export type CurrencySettings = {