From 0d68f8a7ab6701c3a6b7938dc6e2d3ec18514337 Mon Sep 17 00:00:00 2001 From: "Valentin D. Pinkman" Date: Mon, 18 Jun 2018 17:43:34 +0200 Subject: [PATCH 1/5] add mcu flash call --- src/commands/installMcu.js | 24 ++++------ src/components/ManagerPage/FlashMcu.js | 48 ++++++++++++++++++++ src/components/ManagerPage/index.js | 7 +-- src/components/Workflow/index.js | 63 ++++++++++++++------------ src/helpers/firmware/installMcu.js | 17 +++++-- 5 files changed, 108 insertions(+), 51 deletions(-) diff --git a/src/commands/installMcu.js b/src/commands/installMcu.js index c16387dc..fcc70eb3 100644 --- a/src/commands/installMcu.js +++ b/src/commands/installMcu.js @@ -3,24 +3,20 @@ import { createCommand, Command } from 'helpers/ipc' import { fromPromise } from 'rxjs/observable/fromPromise' -// import { withDevice } from 'helpers/deviceAccess' +import { withDevice } from 'helpers/deviceAccess' import installMcu from 'helpers/firmware/installMcu' -// type Input = { -// devicePath: string, -// firmware: Object, -// } +type Input = { + devicePath: string, + targetId: string | number, +} -// type Result = { -// targetId: number | string, -// version: string, -// final: boolean, -// mcu: boolean, -// } - -type Input = * type Result = * -const cmd: Command = createCommand('installMcu', () => fromPromise(installMcu())) +const cmd: Command = createCommand('installMcu', ({ devicePath, targetId }) => + fromPromise( + withDevice(devicePath)(transport => installMcu(transport, { targetId, version: '1.5' })), + ), +) export default cmd diff --git a/src/components/ManagerPage/FlashMcu.js b/src/components/ManagerPage/FlashMcu.js index e69de29b..a9cd2d5a 100644 --- a/src/components/ManagerPage/FlashMcu.js +++ b/src/components/ManagerPage/FlashMcu.js @@ -0,0 +1,48 @@ +// @flow +import React, { PureComponent } from 'react' + +import type { Device } from 'types/common' +import installMcu from 'commands/installMcu' + +type DeviceInfo = { + targetId: number | string, + version: string, + final: boolean, + mcu: boolean, +} + +type Props = { + device: Device, + deviceInfo: DeviceInfo, +} + +type State = { + flashing: boolean, +} + +class FlashMcu extends PureComponent { + state = { + flashing: false, + } + + componentDidMount() { + this.flasMCU() + } + + flasMCU = async () => { + const { device, deviceInfo } = this.props + const { flashing } = this.state + + if (!flashing) { + this.setState(state => ({ ...state, flashing: true })) + await installMcu.send({ devicePath: device.path, targetId: deviceInfo.targetId }).toPromise() + this.setState(state => ({ ...state, flashing: false })) + } + } + + render() { + return
Flashing MCU
+ } +} + +export default FlashMcu diff --git a/src/components/ManagerPage/index.js b/src/components/ManagerPage/index.js index 6b1b48a7..11209e93 100644 --- a/src/components/ManagerPage/index.js +++ b/src/components/ManagerPage/index.js @@ -9,6 +9,7 @@ import type { Device } from 'types/common' import Workflow from 'components/Workflow' import WorkflowWithIcon from 'components/Workflow/WorkflowWithIcon' import Dashboard from './Dashboard' +import FlashMcu from './FlashMcu' type DeviceInfo = { targetId: number | string, @@ -25,11 +26,11 @@ type Error = { function ManagerPage(): Node { return ( ( + renderFinalUpdate={(device: Device, deviceInfo: DeviceInfo) => (

UPDATE FINAL FIRMARE (TEMPLATE + ACTION WIP) {deviceInfo.final}

)} - renderMcuUpdate={(deviceInfo: DeviceInfo) => ( -

FLASH MCU (TEMPLATE + ACTION WIP) {deviceInfo.mcu}

+ renderMcuUpdate={(device: Device, deviceInfo: DeviceInfo) => ( + )} renderDashboard={(device: Device, deviceInfo: DeviceInfo) => ( diff --git a/src/components/Workflow/index.js b/src/components/Workflow/index.js index 9c5edc0a..794fa7f6 100644 --- a/src/components/Workflow/index.js +++ b/src/components/Workflow/index.js @@ -31,8 +31,8 @@ type Props = { genuineError: ?Error, }, ) => Node, - renderMcuUpdate?: (deviceInfo: DeviceInfo) => Node, - renderFinalUpdate?: (deviceInfo: DeviceInfo) => Node, + renderMcuUpdate?: (device: Device, deviceInfo: DeviceInfo) => Node, + renderFinalUpdate?: (device: Device, deviceInfo: DeviceInfo) => Node, renderDashboard?: (device: Device, deviceInfo: DeviceInfo, isGenuine: boolean) => Node, renderError?: (dashboardError: ?Error, genuineError: ?Error) => Node, } @@ -52,37 +52,42 @@ class Workflow extends PureComponent { {(device: Device) => ( - {(deviceInfo: ?DeviceInfo, dashboardError: ?Error) => ( - - {(isGenuine: ?boolean, genuineError: ?Error) => { - if (dashboardError || genuineError) { - return renderError - ? renderError(dashboardError, genuineError) - : renderDefault(device, deviceInfo, isGenuine, { - genuineError, - dashboardError, - }) - } + {(deviceInfo: ?DeviceInfo, dashboardError: ?Error) => { - if (deviceInfo && deviceInfo.mcu && renderMcuUpdate) { - return renderMcuUpdate(deviceInfo) - } + if (deviceInfo && deviceInfo.mcu && renderMcuUpdate) { + return renderMcuUpdate(device, deviceInfo) + } - if (deviceInfo && deviceInfo.final && renderFinalUpdate) { - return renderFinalUpdate(deviceInfo) - } + if (deviceInfo && deviceInfo.final && renderFinalUpdate) { + return renderFinalUpdate(device, deviceInfo) + } - if (isGenuine && deviceInfo && device && !dashboardError && !genuineError) { - if (renderDashboard) return renderDashboard(device, deviceInfo, isGenuine) - } + return ( + + {(isGenuine: ?boolean, genuineError: ?Error) => { + if (dashboardError || genuineError) { + return renderError + ? renderError(dashboardError, genuineError) + : renderDefault(device, deviceInfo, isGenuine, { + genuineError, + dashboardError, + }) + } - return renderDefault(device, deviceInfo, isGenuine, { - genuineError, - dashboardError, - }) - }} - - )} + if (isGenuine && deviceInfo && device && !dashboardError && !genuineError) { + if (onGenuineCheck) onGenuineCheck(isGenuine) + + if (renderDashboard) return renderDashboard(device, deviceInfo, isGenuine) + } + + return renderDefault(device, deviceInfo, isGenuine, { + genuineError, + dashboardError, + }) + }} + + ) + }} )} diff --git a/src/helpers/firmware/installMcu.js b/src/helpers/firmware/installMcu.js index e44f4d47..3a0b6499 100644 --- a/src/helpers/firmware/installMcu.js +++ b/src/helpers/firmware/installMcu.js @@ -1,8 +1,15 @@ // @flow +import type Transport from '@ledgerhq/hw-transport' +import qs from 'qs' +import { MANAGER_API_URL } from 'helpers/constants' +import { createDeviceSocket } from 'helpers/socket' -type Result = Promise +type Result = Promise<*> -// TODO: IMPLEMENTATION FOR FLASHING FIRMWARE -// GETTING APDUS FROM SERVER -// SEND THE APDUS TO DEVICE -export default async (): Result => new Promise(resolve => resolve(true)) +export default async ( + transport: Transport<*>, + params: { targetId: string | number, version: string }, +): Result => { + const url = `${MANAGER_API_URL}/mcu?${qs.stringify(params)}` + return createDeviceSocket(transport, url).toPromise() +} From d47a4e099922117b9956081ea76e2e0e1287e143 Mon Sep 17 00:00:00 2001 From: "Valentin D. Pinkman" Date: Mon, 18 Jun 2018 18:13:47 +0200 Subject: [PATCH 2/5] removed calls to createSocketDialog for the new createDeviceSocket --- src/components/Workflow/index.js | 1 + src/helpers/apps/installApp.js | 9 ++++++--- src/helpers/apps/uninstallApp.js | 9 ++++++--- src/helpers/devices/getIsGenuine.js | 16 ++++++++++------ src/helpers/firmware/installFinalFirmware.js | 13 ++++++++----- src/helpers/firmware/installMcu.js | 3 ++- src/helpers/firmware/installOsuFirmware.js | 9 ++++++--- 7 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/components/Workflow/index.js b/src/components/Workflow/index.js index 794fa7f6..0fe0cbbf 100644 --- a/src/components/Workflow/index.js +++ b/src/components/Workflow/index.js @@ -53,6 +53,7 @@ class Workflow extends PureComponent { {(device: Device) => ( {(deviceInfo: ?DeviceInfo, dashboardError: ?Error) => { + console.log('deviceInfo', deviceInfo) if (deviceInfo && deviceInfo.mcu && renderMcuUpdate) { return renderMcuUpdate(device, deviceInfo) diff --git a/src/helpers/apps/installApp.js b/src/helpers/apps/installApp.js index 1998cfb5..89373c42 100644 --- a/src/helpers/apps/installApp.js +++ b/src/helpers/apps/installApp.js @@ -1,8 +1,10 @@ // @flow - +import qs from 'qs' import type Transport from '@ledgerhq/hw-transport' -import { createSocketDialog } from 'helpers/common' +import { BASE_SOCKET_URL } from 'helpers/constants' +import { createDeviceSocket } from 'helpers/socket' + import type { LedgerScriptParams } from 'helpers/common' /** @@ -12,5 +14,6 @@ export default async function installApp( transport: Transport<*>, { appParams }: { appParams: LedgerScriptParams }, ): Promise<*> { - return createSocketDialog(transport, '/install', appParams) + const url = `${BASE_SOCKET_URL}/install?${qs.stringify(appParams)}` + return createDeviceSocket(transport, url).toPromise() } diff --git a/src/helpers/apps/uninstallApp.js b/src/helpers/apps/uninstallApp.js index c67c029c..113cd29c 100644 --- a/src/helpers/apps/uninstallApp.js +++ b/src/helpers/apps/uninstallApp.js @@ -1,8 +1,10 @@ // @flow - +import qs from 'qs' import type Transport from '@ledgerhq/hw-transport' -import { createSocketDialog } from 'helpers/common' +import { BASE_SOCKET_URL } from 'helpers/constants' +import { createDeviceSocket } from 'helpers/socket' + import type { LedgerScriptParams } from 'helpers/common' /** @@ -17,5 +19,6 @@ export default async function uninstallApp( firmware: appParams.delete, firmwareKey: appParams.deleteKey, } - return createSocketDialog(transport, '/install', params) + const url = `${BASE_SOCKET_URL}/install?${qs.stringify(params)}` + return createDeviceSocket(transport, url).toPromise() } diff --git a/src/helpers/devices/getIsGenuine.js b/src/helpers/devices/getIsGenuine.js index ad82420f..6e500142 100644 --- a/src/helpers/devices/getIsGenuine.js +++ b/src/helpers/devices/getIsGenuine.js @@ -1,12 +1,16 @@ // @flow +import qs from 'qs' import type Transport from '@ledgerhq/hw-transport' -import { createSocketDialog } from 'helpers/common' -import { SKIP_GENUINE } from 'config/constants' +import { SKIP_GENUINE, MANAGER_API_BASE } from 'config/constants' + +import { createDeviceSocket } from 'helpers/socket' export default async ( transport: Transport<*>, - { targetId }: { targetId: string | number }, -): Promise => - SKIP_GENUINE + params: { targetId: string | number }, +): Promise => { + const url = `${MANAGER_API_BASE}/genuine?${qs.stringify(params)}` + return SKIP_GENUINE ? new Promise(resolve => setTimeout(() => resolve('0000'), 1000)) - : createSocketDialog(transport, '/genuine', { targetId }, true) + : createDeviceSocket(transport, url).toPromise() +} diff --git a/src/helpers/firmware/installFinalFirmware.js b/src/helpers/firmware/installFinalFirmware.js index 43a57435..9247646d 100644 --- a/src/helpers/firmware/installFinalFirmware.js +++ b/src/helpers/firmware/installFinalFirmware.js @@ -1,18 +1,21 @@ // @flow - +import qs from 'qs' import type Transport from '@ledgerhq/hw-transport' -import { createSocketDialog, buildParamsFromFirmware } from 'helpers/common' +import { BASE_SOCKET_URL } from 'helpers/constants' +import { createDeviceSocket } from 'helpers/socket' +import { buildParamsFromFirmware } from 'helpers/common' type Input = Object type Result = * -const buildOsuParams = buildParamsFromFirmware('final') +const buildFinalParams = buildParamsFromFirmware('final') export default async (transport: Transport<*>, firmware: Input): Result => { try { - const osuData = buildOsuParams(firmware) - await createSocketDialog(transport, '/install', osuData) + const finalData = buildFinalParams(firmware) + const url = `${BASE_SOCKET_URL}/install?${qs.stringify(finalData)}` + await createDeviceSocket(transport, url).toPromise() return { success: true } } catch (err) { const error = Error(err.message) diff --git a/src/helpers/firmware/installMcu.js b/src/helpers/firmware/installMcu.js index 3a0b6499..bb9a9d81 100644 --- a/src/helpers/firmware/installMcu.js +++ b/src/helpers/firmware/installMcu.js @@ -1,6 +1,7 @@ // @flow -import type Transport from '@ledgerhq/hw-transport' import qs from 'qs' +import type Transport from '@ledgerhq/hw-transport' + import { MANAGER_API_URL } from 'helpers/constants' import { createDeviceSocket } from 'helpers/socket' diff --git a/src/helpers/firmware/installOsuFirmware.js b/src/helpers/firmware/installOsuFirmware.js index c213cb58..24c26b91 100644 --- a/src/helpers/firmware/installOsuFirmware.js +++ b/src/helpers/firmware/installOsuFirmware.js @@ -1,8 +1,10 @@ // @flow - +import qs from 'qs' import type Transport from '@ledgerhq/hw-transport' -import { createSocketDialog, buildParamsFromFirmware } from 'helpers/common' +import { BASE_SOCKET_URL } from 'helpers/constants' +import { createDeviceSocket } from 'helpers/socket' +import { buildParamsFromFirmware } from 'helpers/common' type Input = Object @@ -13,7 +15,8 @@ const buildOsuParams = buildParamsFromFirmware('osu') export default async (transport: Transport<*>, firmware: Input): Result => { try { const osuData = buildOsuParams(firmware) - await createSocketDialog(transport, '/install', osuData) + const url = `${BASE_SOCKET_URL}/install?${qs.stringify(osuData)}` + await createDeviceSocket(transport, url).toPromise() return { success: true } } catch (err) { const error = Error(err.message) From 843675acdc36ba3cf6a9dabd89aac21dcd969db6 Mon Sep 17 00:00:00 2001 From: "Valentin D. Pinkman" Date: Tue, 19 Jun 2018 14:34:09 +0200 Subject: [PATCH 3/5] connected app un/install to new api --- src/commands/installApp.js | 9 +++-- src/commands/listApps.js | 5 +-- src/commands/uninstallApp.js | 9 +++-- src/components/ManagerPage/AppSearchBar.js | 17 +++------ src/components/ManagerPage/AppsList.js | 37 ++++++++------------ src/components/ManagerPage/Dashboard.js | 6 ++-- src/components/Workflow/index.js | 4 +-- src/helpers/apps/installApp.js | 12 +++++-- src/helpers/apps/listApps.js | 35 +++++++++++------- src/helpers/apps/uninstallApp.js | 14 ++++---- src/helpers/common.js | 8 +++-- src/helpers/devices/getIsGenuine.js | 4 +-- src/helpers/firmware/installFinalFirmware.js | 4 +-- src/helpers/firmware/installMcu.js | 4 +-- src/helpers/firmware/installOsuFirmware.js | 4 +-- src/helpers/urls.js | 18 ++++++++++ 16 files changed, 110 insertions(+), 80 deletions(-) create mode 100644 src/helpers/urls.js diff --git a/src/commands/installApp.js b/src/commands/installApp.js index c4f1df13..88844ae9 100644 --- a/src/commands/installApp.js +++ b/src/commands/installApp.js @@ -9,14 +9,17 @@ import installApp from 'helpers/apps/installApp' import type { LedgerScriptParams } from 'helpers/common' type Input = { - appParams: LedgerScriptParams, + app: LedgerScriptParams, devicePath: string, + targetId: string | number, } type Result = * -const cmd: Command = createCommand('installApp', ({ devicePath, ...rest }) => - fromPromise(withDevice(devicePath)(transport => installApp(transport, rest))), +const cmd: Command = createCommand( + 'installApp', + ({ devicePath, targetId, ...app }) => + fromPromise(withDevice(devicePath)(transport => installApp(transport, targetId, app))), ) export default cmd diff --git a/src/commands/listApps.js b/src/commands/listApps.js index b600aef4..e4d5a153 100644 --- a/src/commands/listApps.js +++ b/src/commands/listApps.js @@ -7,12 +7,13 @@ import listApps from 'helpers/apps/listApps' type Input = { targetId: string | number, + version: string, } type Result = * -const cmd: Command = createCommand('listApps', ({ targetId }) => - fromPromise(listApps(targetId)), +const cmd: Command = createCommand('listApps', ({ targetId, version }) => + fromPromise(listApps(targetId, version)), ) export default cmd diff --git a/src/commands/uninstallApp.js b/src/commands/uninstallApp.js index e8e3b3f7..573439fa 100644 --- a/src/commands/uninstallApp.js +++ b/src/commands/uninstallApp.js @@ -9,14 +9,17 @@ import uninstallApp from 'helpers/apps/uninstallApp' import type { LedgerScriptParams } from 'helpers/common' type Input = { - appParams: LedgerScriptParams, + app: LedgerScriptParams, devicePath: string, + targetId: string | number, } type Result = * -const cmd: Command = createCommand('uninstallApp', ({ devicePath, ...rest }) => - fromPromise(withDevice(devicePath)(transport => uninstallApp(transport, rest))), +const cmd: Command = createCommand( + 'uninstallApp', + ({ devicePath, targetId, ...rest }) => + fromPromise(withDevice(devicePath)(transport => uninstallApp(transport, targetId, rest))), ) export default cmd diff --git a/src/components/ManagerPage/AppSearchBar.js b/src/components/ManagerPage/AppSearchBar.js index 4dcdd81b..e728a91a 100644 --- a/src/components/ManagerPage/AppSearchBar.js +++ b/src/components/ManagerPage/AppSearchBar.js @@ -4,6 +4,8 @@ import styled from 'styled-components' import { color, fontSize, space } from 'styled-system' import fontFamily from 'styles/styled/fontFamily' +import type { LedgerScriptParams } from 'helpers/common' + import { ff } from 'styles/helpers' import Box from 'components/base/Box' @@ -12,20 +14,9 @@ import Search from 'components/base/Search' import SearchIcon from 'icons/Search' import CrossIcon from 'icons/Cross' -type LedgerApp = { - name: string, - version: string, - icon: string, - app: Object, - bolos_version: { - min: number, - max: number, - }, -} - type Props = { - list: Array, - children: (list: Array) => React$Node, + list: Array, + children: (list: Array) => React$Node, } type State = { diff --git a/src/components/ManagerPage/AppsList.js b/src/components/ManagerPage/AppsList.js index 7e07312b..2333f396 100644 --- a/src/components/ManagerPage/AppsList.js +++ b/src/components/ManagerPage/AppsList.js @@ -6,6 +6,7 @@ import styled from 'styled-components' import { translate } from 'react-i18next' import type { Device, T } from 'types/common' +import type { LedgerScriptParams } from 'helpers/common' import listApps from 'commands/listApps' import installApp from 'commands/installApp' @@ -43,27 +44,17 @@ const ICONS_FALLBACK = { type Status = 'loading' | 'idle' | 'busy' | 'success' | 'error' type Mode = 'home' | 'installing' | 'uninstalling' -type LedgerApp = { - name: string, - version: string, - icon: string, - app: Object, - bolos_version: { - min: number, - max: number, - }, -} - type Props = { device: Device, targetId: string | number, t: T, + version: string, } type State = { status: Status, error: string | null, - appsList: LedgerApp[], + appsList: LedgerScriptParams[] | Array<*>, app: string, mode: Mode, } @@ -89,8 +80,8 @@ class AppsList extends PureComponent { async fetchAppList() { try { - const { targetId } = this.props - const appsList = CACHED_APPS || (await listApps.send({ targetId }).toPromise()) + const { targetId, version } = this.props + const appsList = CACHED_APPS || (await listApps.send({ targetId, version }).toPromise()) CACHED_APPS = appsList if (!this._unmounted) { this.setState({ appsList, status: 'idle' }) @@ -100,14 +91,14 @@ class AppsList extends PureComponent { } } - handleInstallApp = (args: { app: any, name: string }) => async () => { - const { app: appParams, name } = args - this.setState({ status: 'busy', app: name, mode: 'installing' }) + handleInstallApp = (app: LedgerScriptParams) => async () => { + this.setState({ status: 'busy', app: app.name, mode: 'installing' }) try { const { device: { path: devicePath }, + targetId, } = this.props - const data = { appParams, devicePath } + const data = { app, devicePath, targetId } await installApp.send(data).toPromise() this.setState({ status: 'success', app: '' }) } catch (err) { @@ -115,14 +106,14 @@ class AppsList extends PureComponent { } } - handleUninstallApp = (args: { app: any, name: string }) => async () => { - const { app: appParams, name } = args - this.setState({ status: 'busy', app: name, mode: 'uninstalling' }) + handleUninstallApp = (app: LedgerScriptParams) => async () => { + this.setState({ status: 'busy', app: app.name, mode: 'uninstalling' }) try { const { device: { path: devicePath }, + targetId, } = this.props - const data = { appParams, devicePath } + const data = { app, devicePath, targetId } await uninstallApp.send(data).toPromise() this.setState({ status: 'success', app: '' }) } catch (err) { @@ -192,7 +183,7 @@ class AppsList extends PureComponent { {items.map(c => ( ( - + /> */} - + ) diff --git a/src/components/Workflow/index.js b/src/components/Workflow/index.js index 0fe0cbbf..e3db974f 100644 --- a/src/components/Workflow/index.js +++ b/src/components/Workflow/index.js @@ -34,6 +34,7 @@ type Props = { renderMcuUpdate?: (device: Device, deviceInfo: DeviceInfo) => Node, renderFinalUpdate?: (device: Device, deviceInfo: DeviceInfo) => Node, renderDashboard?: (device: Device, deviceInfo: DeviceInfo, isGenuine: boolean) => Node, + onGenuineCheck?: (isGenuine: boolean) => void, renderError?: (dashboardError: ?Error, genuineError: ?Error) => Node, } type State = {} @@ -47,14 +48,13 @@ class Workflow extends PureComponent { renderMcuUpdate, renderError, renderDefault, + onGenuineCheck, } = this.props return ( {(device: Device) => ( {(deviceInfo: ?DeviceInfo, dashboardError: ?Error) => { - console.log('deviceInfo', deviceInfo) - if (deviceInfo && deviceInfo.mcu && renderMcuUpdate) { return renderMcuUpdate(device, deviceInfo) } diff --git a/src/helpers/apps/installApp.js b/src/helpers/apps/installApp.js index 89373c42..2183d639 100644 --- a/src/helpers/apps/installApp.js +++ b/src/helpers/apps/installApp.js @@ -2,7 +2,7 @@ import qs from 'qs' import type Transport from '@ledgerhq/hw-transport' -import { BASE_SOCKET_URL } from 'helpers/constants' +import { BASE_SOCKET_URL_SECURE } from 'config/constants' import { createDeviceSocket } from 'helpers/socket' import type { LedgerScriptParams } from 'helpers/common' @@ -12,8 +12,14 @@ import type { LedgerScriptParams } from 'helpers/common' */ export default async function installApp( transport: Transport<*>, - { appParams }: { appParams: LedgerScriptParams }, + targetId: string | number, + { app }: { app: LedgerScriptParams }, ): Promise<*> { - const url = `${BASE_SOCKET_URL}/install?${qs.stringify(appParams)}` + const params = { + targetId, + ...app, + firmwareKey: app.firmware_key, + } + const url = `${BASE_SOCKET_URL_SECURE}/install?${qs.stringify(params)}` return createDeviceSocket(transport, url).toPromise() } diff --git a/src/helpers/apps/listApps.js b/src/helpers/apps/listApps.js index becd8373..55ba298d 100644 --- a/src/helpers/apps/listApps.js +++ b/src/helpers/apps/listApps.js @@ -1,20 +1,31 @@ // @flow import axios from 'axios' -import { MANAGER_API_BASE } from 'config/constants' +import { + DEVICE_VERSION_BY_TARGET_ID, + APPLICATIONS_BY_DEVICE, + FIRMWARE_FINAL_VERSIONS_NAME, +} from 'helpers/urls' -export default async (targetId: string | number) => { +export default async (targetId: string | number, version: string) => { try { - const { data: deviceData } = await axios.get( - `${MANAGER_API_BASE}/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'] + const provider = 1 + const { data: deviceData } = await axios.post(DEVICE_VERSION_BY_TARGET_ID, { + provider, + target_id: targetId, + }) + const { data: firmwareData } = await axios.post(FIRMWARE_FINAL_VERSIONS_NAME, { + device_version: deviceData.id, + se_firmware_name: version, + }) + const { + data: { application_versions }, + } = await axios.post(APPLICATIONS_BY_DEVICE, { + providers: [1], + current_se_firmware_final_version: firmwareData.id, + device_version: deviceData.id, + }) + return application_versions.length > 0 ? application_versions : [] } catch (err) { const error = Error(err.message) error.stack = err.stack diff --git a/src/helpers/apps/uninstallApp.js b/src/helpers/apps/uninstallApp.js index 113cd29c..570e361c 100644 --- a/src/helpers/apps/uninstallApp.js +++ b/src/helpers/apps/uninstallApp.js @@ -2,7 +2,7 @@ import qs from 'qs' import type Transport from '@ledgerhq/hw-transport' -import { BASE_SOCKET_URL } from 'helpers/constants' +import { BASE_SOCKET_URL_SECURE } from 'config/constants' import { createDeviceSocket } from 'helpers/socket' import type { LedgerScriptParams } from 'helpers/common' @@ -12,13 +12,15 @@ import type { LedgerScriptParams } from 'helpers/common' */ export default async function uninstallApp( transport: Transport<*>, - { appParams }: { appParams: LedgerScriptParams }, + targetId: string | number, + { app }: { app: LedgerScriptParams }, ): Promise<*> { const params = { - ...appParams, - firmware: appParams.delete, - firmwareKey: appParams.deleteKey, + targetId, + ...app, + firmware: app.delete, + firmwareKey: app.delete_key, } - const url = `${BASE_SOCKET_URL}/install?${qs.stringify(params)}` + const url = `${BASE_SOCKET_URL_SECURE}/install?${qs.stringify(params)}` return createDeviceSocket(transport, url).toPromise() } diff --git a/src/helpers/common.js b/src/helpers/common.js index 34b34f09..6966af72 100644 --- a/src/helpers/common.js +++ b/src/helpers/common.js @@ -16,10 +16,14 @@ const APDUS = { export type LedgerScriptParams = { firmware?: string, - firmwareKey?: string, + firmware_key?: string, delete?: string, - deleteKey?: string, + delete_key?: string, targetId?: string | number, + name: string, + version: string, + icon: string, + app?: number, } type FirmwareUpdateType = 'osu' | 'final' diff --git a/src/helpers/devices/getIsGenuine.js b/src/helpers/devices/getIsGenuine.js index 6e500142..86f9d70f 100644 --- a/src/helpers/devices/getIsGenuine.js +++ b/src/helpers/devices/getIsGenuine.js @@ -1,7 +1,7 @@ // @flow import qs from 'qs' import type Transport from '@ledgerhq/hw-transport' -import { SKIP_GENUINE, MANAGER_API_BASE } from 'config/constants' +import { SKIP_GENUINE, BASE_SOCKET_URL_SECURE } from 'config/constants' import { createDeviceSocket } from 'helpers/socket' @@ -9,7 +9,7 @@ export default async ( transport: Transport<*>, params: { targetId: string | number }, ): Promise => { - const url = `${MANAGER_API_BASE}/genuine?${qs.stringify(params)}` + const url = `${BASE_SOCKET_URL_SECURE}/genuine?${qs.stringify(params)}` return SKIP_GENUINE ? new Promise(resolve => setTimeout(() => resolve('0000'), 1000)) : createDeviceSocket(transport, url).toPromise() diff --git a/src/helpers/firmware/installFinalFirmware.js b/src/helpers/firmware/installFinalFirmware.js index 9247646d..7eed7932 100644 --- a/src/helpers/firmware/installFinalFirmware.js +++ b/src/helpers/firmware/installFinalFirmware.js @@ -2,7 +2,7 @@ import qs from 'qs' import type Transport from '@ledgerhq/hw-transport' -import { BASE_SOCKET_URL } from 'helpers/constants' +import { BASE_SOCKET_URL_SECURE } from 'config/constants' import { createDeviceSocket } from 'helpers/socket' import { buildParamsFromFirmware } from 'helpers/common' @@ -14,7 +14,7 @@ const buildFinalParams = buildParamsFromFirmware('final') export default async (transport: Transport<*>, firmware: Input): Result => { try { const finalData = buildFinalParams(firmware) - const url = `${BASE_SOCKET_URL}/install?${qs.stringify(finalData)}` + const url = `${BASE_SOCKET_URL_SECURE}/install?${qs.stringify(finalData)}` await createDeviceSocket(transport, url).toPromise() return { success: true } } catch (err) { diff --git a/src/helpers/firmware/installMcu.js b/src/helpers/firmware/installMcu.js index bb9a9d81..56fe5448 100644 --- a/src/helpers/firmware/installMcu.js +++ b/src/helpers/firmware/installMcu.js @@ -2,7 +2,7 @@ import qs from 'qs' import type Transport from '@ledgerhq/hw-transport' -import { MANAGER_API_URL } from 'helpers/constants' +import { MANAGER_API_BASE } from 'config/constants' import { createDeviceSocket } from 'helpers/socket' type Result = Promise<*> @@ -11,6 +11,6 @@ export default async ( transport: Transport<*>, params: { targetId: string | number, version: string }, ): Result => { - const url = `${MANAGER_API_URL}/mcu?${qs.stringify(params)}` + const url = `${MANAGER_API_BASE}/mcu?${qs.stringify(params)}` return createDeviceSocket(transport, url).toPromise() } diff --git a/src/helpers/firmware/installOsuFirmware.js b/src/helpers/firmware/installOsuFirmware.js index 24c26b91..d86eb5ce 100644 --- a/src/helpers/firmware/installOsuFirmware.js +++ b/src/helpers/firmware/installOsuFirmware.js @@ -2,7 +2,7 @@ import qs from 'qs' import type Transport from '@ledgerhq/hw-transport' -import { BASE_SOCKET_URL } from 'helpers/constants' +import { BASE_SOCKET_URL_SECURE } from 'config/constants' import { createDeviceSocket } from 'helpers/socket' import { buildParamsFromFirmware } from 'helpers/common' @@ -15,7 +15,7 @@ const buildOsuParams = buildParamsFromFirmware('osu') export default async (transport: Transport<*>, firmware: Input): Result => { try { const osuData = buildOsuParams(firmware) - const url = `${BASE_SOCKET_URL}/install?${qs.stringify(osuData)}` + const url = `${BASE_SOCKET_URL_SECURE}/install?${qs.stringify(osuData)}` await createDeviceSocket(transport, url).toPromise() return { success: true } } catch (err) { diff --git a/src/helpers/urls.js b/src/helpers/urls.js new file mode 100644 index 00000000..0f322997 --- /dev/null +++ b/src/helpers/urls.js @@ -0,0 +1,18 @@ +// @flow +import qs from 'qs' + +import { MANAGER_API_BASE, BASE_SOCKET_URL_SECURE } from 'config/constants' + +const urlBuilder = (base: string) => (endpoint: string): string => `${base}/${endpoint}` +const managerUrlbuilder = urlBuilder(MANAGER_API_BASE) + +const wsURLBuilder = (endpoint: string) => (params?: Object) => + `${BASE_SOCKET_URL_SECURE}/${endpoint}${params ? `?${qs.stringify(params)}` : ''}` + +export const DEVICE_VERSION_BY_TARGET_ID = managerUrlbuilder('device_versions_target_id') +export const APPLICATIONS_BY_DEVICE = managerUrlbuilder('get_apps') +export const FIRMWARE_FINAL_VERSIONS_NAME = managerUrlbuilder('firmware_final_versions_name') + +export const WS_INSTALL = wsURLBuilder('install') +export const WS_GENUINE = wsURLBuilder('genuine') +export const WS_MCU = wsURLBuilder('genuine') From e352ffad66e070aafe1bc0bc1974ab7c555c8275 Mon Sep 17 00:00:00 2001 From: "Valentin D. Pinkman" Date: Tue, 19 Jun 2018 16:24:44 +0200 Subject: [PATCH 4/5] added fetch last firmware, get device infos and get current firmare commands --- src/commands/getCurrentFirmware.js | 19 +++++++++ src/commands/getFirmwareInfo.js | 19 --------- src/commands/index.js | 4 +- src/components/ManagerPage/Dashboard.js | 4 +- src/components/ManagerPage/FirmwareUpdate.js | 4 +- .../ManagerPage/UpdateFirmwareButton.js | 5 ++- src/components/TriggerOnMount/index.js | 15 ------- src/helpers/apps/listApps.js | 20 +++------ src/helpers/common.js | 41 ------------------- src/helpers/devices/getCurrentFirmware.js | 26 ++++++++++++ src/helpers/devices/getDeviceVersion.js | 19 +++++++++ src/helpers/devices/getFirmwareInfo.js | 34 --------------- src/helpers/devices/getIsGenuine.js | 5 ++- .../devices/getLatestFirmwareForDevice.js | 37 ++++++++--------- src/helpers/devices/getMemInfo.js | 7 ++-- src/helpers/firmware/installFinalFirmware.js | 9 +--- src/helpers/firmware/installOsuFirmware.js | 9 +--- src/helpers/urls.js | 15 ++++--- 18 files changed, 118 insertions(+), 174 deletions(-) create mode 100644 src/commands/getCurrentFirmware.js delete mode 100644 src/commands/getFirmwareInfo.js delete mode 100644 src/components/TriggerOnMount/index.js create mode 100644 src/helpers/devices/getCurrentFirmware.js create mode 100644 src/helpers/devices/getDeviceVersion.js delete mode 100644 src/helpers/devices/getFirmwareInfo.js diff --git a/src/commands/getCurrentFirmware.js b/src/commands/getCurrentFirmware.js new file mode 100644 index 00000000..d0dfc329 --- /dev/null +++ b/src/commands/getCurrentFirmware.js @@ -0,0 +1,19 @@ +// @flow + +import { createCommand, Command } from 'helpers/ipc' +import { fromPromise } from 'rxjs/observable/fromPromise' + +import getCurrentFirmware from 'helpers/devices/getCurrentFirmware' + +type Input = { + deviceId: string | number, + version: string, +} + +type Result = * + +const cmd: Command = createCommand('getCurrentFirmware', data => + fromPromise(getCurrentFirmware(data)), +) + +export default cmd diff --git a/src/commands/getFirmwareInfo.js b/src/commands/getFirmwareInfo.js deleted file mode 100644 index dd1bdd42..00000000 --- a/src/commands/getFirmwareInfo.js +++ /dev/null @@ -1,19 +0,0 @@ -// @flow - -import { createCommand, Command } from 'helpers/ipc' -import { fromPromise } from 'rxjs/observable/fromPromise' - -import getFirmwareInfo from 'helpers/devices/getFirmwareInfo' - -type Input = { - targetId: string | number, - version: string, -} - -type Result = * - -const cmd: Command = createCommand('getFirmwareInfo', data => - fromPromise(getFirmwareInfo(data)), -) - -export default cmd diff --git a/src/commands/index.js b/src/commands/index.js index e32c6639..7f37b4bb 100644 --- a/src/commands/index.js +++ b/src/commands/index.js @@ -5,7 +5,7 @@ import type { Command } from 'helpers/ipc' import getAddress from 'commands/getAddress' import getDeviceInfo from 'commands/getDeviceInfo' -import getFirmwareInfo from 'commands/getFirmwareInfo' +import getCurrentFirmware from 'commands/getCurrentFirmware' import getIsGenuine from 'commands/getIsGenuine' import getLatestFirmwareForDevice from 'commands/getLatestFirmwareForDevice' import getMemInfo from 'commands/getMemInfo' @@ -32,7 +32,7 @@ import uninstallApp from 'commands/uninstallApp' const all: Array> = [ getAddress, getDeviceInfo, - getFirmwareInfo, + getCurrentFirmware, getIsGenuine, getLatestFirmwareForDevice, getMemInfo, diff --git a/src/components/ManagerPage/Dashboard.js b/src/components/ManagerPage/Dashboard.js index d41a58ab..981a48d3 100644 --- a/src/components/ManagerPage/Dashboard.js +++ b/src/components/ManagerPage/Dashboard.js @@ -34,13 +34,13 @@ const Dashboard = ({ device, deviceInfo, t }: Props) => ( - {/* */} + /> diff --git a/src/components/ManagerPage/FirmwareUpdate.js b/src/components/ManagerPage/FirmwareUpdate.js index 61954fab..c71c75e4 100644 --- a/src/components/ManagerPage/FirmwareUpdate.js +++ b/src/components/ManagerPage/FirmwareUpdate.js @@ -119,7 +119,9 @@ class FirmwareUpdate extends PureComponent { - {t('app:manager.firmware.installed', { version: infos.version })} + {t('app:manager.firmware.installed', { + version: infos.version, + })} diff --git a/src/components/ManagerPage/UpdateFirmwareButton.js b/src/components/ManagerPage/UpdateFirmwareButton.js index 5feea0e3..f5b4c562 100644 --- a/src/components/ManagerPage/UpdateFirmwareButton.js +++ b/src/components/ManagerPage/UpdateFirmwareButton.js @@ -18,11 +18,14 @@ type Props = { installFirmware: () => void, } +const getCleanVersion = (input: string): string => + input.endsWith('-osu') ? input.replace('-osu', '') : input + const UpdateFirmwareButton = ({ t, firmware, installFirmware }: Props) => firmware ? ( - {t('app:manager.firmware.latest', { version: firmware.name })} + {t('app:manager.firmware.latest', { version: getCleanVersion(firmware.name) })}