From 90fbaf4c0fb4998150aea2c9b6858909e37ede1b Mon Sep 17 00:00:00 2001 From: "Valentin D. Pinkman" Date: Mon, 28 May 2018 17:41:14 +0200 Subject: [PATCH 1/2] check for device type before returning app list --- src/commands/installOsuFirmware.js | 2 +- src/commands/listApps.js | 9 ++-- src/components/ManagerPage/AppsList.js | 15 ++++-- src/components/ManagerPage/index.js | 72 ++++++++++++-------------- src/helpers/apps/listApps.js | 17 +++++- src/internals/devices/index.js | 2 + src/internals/manager/index.js | 3 +- 7 files changed, 68 insertions(+), 52 deletions(-) diff --git a/src/commands/installOsuFirmware.js b/src/commands/installOsuFirmware.js index f5110b13..8d4a6f4f 100644 --- a/src/commands/installOsuFirmware.js +++ b/src/commands/installOsuFirmware.js @@ -2,8 +2,8 @@ import { createCommand, Command } from 'helpers/ipc' import { fromPromise } from 'rxjs/observable/fromPromise' -import { withDevice } from 'helpers/deviceAccess' +import { withDevice } from 'helpers/deviceAccess' import installOsuFirmware from 'helpers/firmware/installOsuFirmware' type Input = { diff --git a/src/commands/listApps.js b/src/commands/listApps.js index a6659cb4..68831174 100644 --- a/src/commands/listApps.js +++ b/src/commands/listApps.js @@ -3,13 +3,16 @@ import { createCommand, Command } from 'helpers/ipc' import { fromPromise } from 'rxjs/observable/fromPromise' +import { withDevice } from 'helpers/deviceAccess' import listApps from 'helpers/apps/listApps' -type Input = * +type Input = { + devicePath: string, +} type Result = * -const cmd: Command = createCommand('manager', 'listApps', () => - fromPromise(listApps()), +const cmd: Command = createCommand('devices', 'listApps', ({ devicePath }) => + fromPromise(withDevice(devicePath)(transport => listApps(transport))), ) export default cmd diff --git a/src/components/ManagerPage/AppsList.js b/src/components/ManagerPage/AppsList.js index 66d8dd88..279ddf0f 100644 --- a/src/components/ManagerPage/AppsList.js +++ b/src/components/ManagerPage/AppsList.js @@ -69,10 +69,17 @@ class AppsList extends PureComponent { _unmounted = false async fetchAppList() { - const appsList = CACHED_APPS || (await listApps.send().toPromise()) - CACHED_APPS = appsList - if (!this._unmounted) { - this.setState({ appsList, status: 'idle' }) + try { + const { + device: { path: devicePath }, + } = this.props + const appsList = CACHED_APPS || (await listApps.send({ devicePath }).toPromise()) + CACHED_APPS = appsList + if (!this._unmounted) { + this.setState({ appsList, status: 'idle' }) + } + } catch (err) { + this.setState({ status: 'error', error: err.message }) } } diff --git a/src/components/ManagerPage/index.js b/src/components/ManagerPage/index.js index 88d5b9b7..0e1c616a 100644 --- a/src/components/ManagerPage/index.js +++ b/src/components/ManagerPage/index.js @@ -1,8 +1,9 @@ // @flow -import React, { Component, Fragment } from 'react' +import React, { Fragment } from 'react' import { translate } from 'react-i18next' +import type { Node } from 'react' import type { T } from 'types/common' import AppsList from './AppsList' @@ -16,44 +17,35 @@ type Props = { t: T, } -type State = {} - -class ManagerPage extends Component { - render() { - const { t } = this.props - - return ( - - - {device => ( - - {deviceInfo => ( - - {deviceInfo.mcu && bootloader mode} - {deviceInfo.final && osu mode} - - {!deviceInfo.mcu && - !deviceInfo.final && ( - - - - - )} - - )} - +const ManagerPage = ({ t }: Props): Node => ( + + + {device => ( + + {deviceInfo => ( + + {deviceInfo.mcu && bootloader mode} + {deviceInfo.final && osu mode} + + {!deviceInfo.mcu && + !deviceInfo.final && ( + + + + + )} + )} - - - ) - } -} - + + )} + + +) export default translate()(ManagerPage) diff --git a/src/helpers/apps/listApps.js b/src/helpers/apps/listApps.js index dce2b9e3..50f73837 100644 --- a/src/helpers/apps/listApps.js +++ b/src/helpers/apps/listApps.js @@ -1,10 +1,23 @@ // @flow - import axios from 'axios' +import type Transport from '@ledgerhq/hw-transport' + +import { getFirmwareInfo } from 'helpers/common' + +const { API_BASE_URL } = process.env -export default async () => { +export default async (transport: Transport<*>) => { try { + const { targetId } = await getFirmwareInfo(transport) + const { data: deviceData } = await axios.get( + `${API_BASE_URL}/device_versions_target_id/${targetId}`, + ) const { data } = await axios.get('https://api.ledgerwallet.com/update/applications') + + if (deviceData.name in data) { + return data[deviceData.name] + } + return data['nanos-1.4'] } catch (err) { const error = Error(err.message) diff --git a/src/internals/devices/index.js b/src/internals/devices/index.js index 7500c00a..4f830a2e 100644 --- a/src/internals/devices/index.js +++ b/src/internals/devices/index.js @@ -15,6 +15,7 @@ import uninstallApp from 'commands/uninstallApp' import installOsuFirmware from 'commands/installOsuFirmware' import installFinalFirmware from 'commands/installFinalFirmware' import installMcu from 'commands/installMcu' +import listApps from 'commands/listApps' export const commands: Array> = [ getAddress, @@ -31,4 +32,5 @@ export const commands: Array> = [ installOsuFirmware, installFinalFirmware, installMcu, + listApps, ] diff --git a/src/internals/manager/index.js b/src/internals/manager/index.js index 7f7a94af..cb51bae3 100644 --- a/src/internals/manager/index.js +++ b/src/internals/manager/index.js @@ -1,7 +1,6 @@ // @flow import type { Command } from 'helpers/ipc' -import listApps from 'commands/listApps' import getMemInfo from 'commands/getMemInfo' /** @@ -20,4 +19,4 @@ import getMemInfo from 'commands/getMemInfo' * */ -export const commands: Array> = [listApps, getMemInfo] +export const commands: Array> = [getMemInfo] From 833ee4a52cdb6fd4d423221775093ed9aca90bdd Mon Sep 17 00:00:00 2001 From: "Valentin D. Pinkman" Date: Mon, 28 May 2018 17:48:32 +0200 Subject: [PATCH 2/2] removed reading infos form device when fetching app list --- src/commands/listApps.js | 8 ++++---- src/components/ManagerPage/AppsList.js | 7 +++---- src/components/ManagerPage/index.js | 8 ++++---- src/helpers/apps/listApps.js | 6 +----- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/commands/listApps.js b/src/commands/listApps.js index 68831174..ecffac64 100644 --- a/src/commands/listApps.js +++ b/src/commands/listApps.js @@ -3,16 +3,16 @@ import { createCommand, Command } from 'helpers/ipc' import { fromPromise } from 'rxjs/observable/fromPromise' -import { withDevice } from 'helpers/deviceAccess' import listApps from 'helpers/apps/listApps' type Input = { - devicePath: string, + targetId: string | number, } + type Result = * -const cmd: Command = createCommand('devices', 'listApps', ({ devicePath }) => - fromPromise(withDevice(devicePath)(transport => listApps(transport))), +const cmd: Command = createCommand('devices', 'listApps', ({ targetId }) => + fromPromise(listApps(targetId)), ) export default cmd diff --git a/src/components/ManagerPage/AppsList.js b/src/components/ManagerPage/AppsList.js index 279ddf0f..4388182d 100644 --- a/src/components/ManagerPage/AppsList.js +++ b/src/components/ManagerPage/AppsList.js @@ -42,6 +42,7 @@ type LedgerApp = { type Props = { device: Device, + targetId: string | number, t: T, } @@ -70,10 +71,8 @@ class AppsList extends PureComponent { async fetchAppList() { try { - const { - device: { path: devicePath }, - } = this.props - const appsList = CACHED_APPS || (await listApps.send({ devicePath }).toPromise()) + const { targetId } = this.props + const appsList = CACHED_APPS || (await listApps.send({ targetId }).toPromise()) CACHED_APPS = appsList if (!this._unmounted) { this.setState({ appsList, status: 'idle' }) diff --git a/src/components/ManagerPage/index.js b/src/components/ManagerPage/index.js index 0e1c616a..cc89c859 100644 --- a/src/components/ManagerPage/index.js +++ b/src/components/ManagerPage/index.js @@ -8,7 +8,7 @@ import type { T } from 'types/common' import AppsList from './AppsList' // import DeviceInfos from './DeviceInfos' -import FirmwareUpdate from './FirmwareUpdate' +// import FirmwareUpdate from './FirmwareUpdate' import EnsureDevice from './EnsureDevice' import EnsureDashboard from './EnsureDashboard' import EnsureGenuine from './EnsureGenuine' @@ -30,15 +30,15 @@ const ManagerPage = ({ t }: Props): Node => ( {!deviceInfo.mcu && !deviceInfo.final && ( - - + /> */} + )} diff --git a/src/helpers/apps/listApps.js b/src/helpers/apps/listApps.js index 50f73837..d42ca916 100644 --- a/src/helpers/apps/listApps.js +++ b/src/helpers/apps/listApps.js @@ -1,14 +1,10 @@ // @flow import axios from 'axios' -import type Transport from '@ledgerhq/hw-transport' - -import { getFirmwareInfo } from 'helpers/common' const { API_BASE_URL } = process.env -export default async (transport: Transport<*>) => { +export default async (targetId: string | number) => { try { - const { targetId } = await getFirmwareInfo(transport) const { data: deviceData } = await axios.get( `${API_BASE_URL}/device_versions_target_id/${targetId}`, )