diff --git a/.gitignore b/.gitignore index 65ca3013..40e58245 100644 --- a/.gitignore +++ b/.gitignore @@ -10,11 +10,3 @@ /build/linux/arch/src /build/linux/arch/*.tar.gz /build/linux/arch/*.tar.xz - -# TODO this should be in devs global gitignore -# it makes no sense to have it here -*.log -.DS_Store -.vscode -thumbs.db -jsconfig.json diff --git a/src/commands/getCurrentFirmware.js b/src/commands/getCurrentFirmware.js index 773220ae..d4ae0c86 100644 --- a/src/commands/getCurrentFirmware.js +++ b/src/commands/getCurrentFirmware.js @@ -4,6 +4,7 @@ import { createCommand, Command } from 'helpers/ipc' import { fromPromise } from 'rxjs/observable/fromPromise' import getCurrentFirmware from 'helpers/devices/getCurrentFirmware' +import type { FinalFirmware } from 'helpers/types' type Input = { deviceId: string | number, @@ -11,7 +12,7 @@ type Input = { provider: number, } -type Result = * +type Result = FinalFirmware const cmd: Command = createCommand('getCurrentFirmware', data => fromPromise(getCurrentFirmware(data)), diff --git a/src/commands/getDeviceInfo.js b/src/commands/getDeviceInfo.js index 67c585fd..9cbbfae1 100644 --- a/src/commands/getDeviceInfo.js +++ b/src/commands/getDeviceInfo.js @@ -5,13 +5,15 @@ import { fromPromise } from 'rxjs/observable/fromPromise' import { withDevice } from 'helpers/deviceAccess' import getDeviceInfo from 'helpers/devices/getDeviceInfo' -import type { DeviceInfo } from 'helpers/devices/getDeviceInfo' +import type { DeviceInfo } from 'helpers/types' type Input = { devicePath: string, } -const cmd: Command = createCommand('getDeviceInfo', ({ devicePath }) => +type Result = DeviceInfo + +const cmd: Command = createCommand('getDeviceInfo', ({ devicePath }) => fromPromise(withDevice(devicePath)(transport => getDeviceInfo(transport))), ) diff --git a/src/commands/getIsGenuine.js b/src/commands/getIsGenuine.js index a1bb645d..e3e163b8 100644 --- a/src/commands/getIsGenuine.js +++ b/src/commands/getIsGenuine.js @@ -2,7 +2,7 @@ import { createCommand, Command } from 'helpers/ipc' import { fromPromise } from 'rxjs/observable/fromPromise' -import type { DeviceInfo } from 'helpers/devices/getDeviceInfo' +import type { DeviceInfo } from 'helpers/types' import getIsGenuine from 'helpers/devices/getIsGenuine' import { withDevice } from 'helpers/deviceAccess' diff --git a/src/commands/getLatestFirmwareForDevice.js b/src/commands/getLatestFirmwareForDevice.js index d216d2eb..e88f6a50 100644 --- a/src/commands/getLatestFirmwareForDevice.js +++ b/src/commands/getLatestFirmwareForDevice.js @@ -2,11 +2,11 @@ import { createCommand, Command } from 'helpers/ipc' import { fromPromise } from 'rxjs/observable/fromPromise' -import type { DeviceInfo } from 'helpers/devices/getDeviceInfo' +import type { DeviceInfo, OsuFirmware } from 'helpers/types' import getLatestFirmwareForDevice from '../helpers/devices/getLatestFirmwareForDevice' -type Result = * +type Result = ?(OsuFirmware & { shouldFlashMcu: boolean }) const cmd: Command = createCommand('getLatestFirmwareForDevice', data => fromPromise(getLatestFirmwareForDevice(data)), diff --git a/src/commands/installApp.js b/src/commands/installApp.js index 7fcc7db7..cd1a2e81 100644 --- a/src/commands/installApp.js +++ b/src/commands/installApp.js @@ -6,15 +6,15 @@ import { fromPromise } from 'rxjs/observable/fromPromise' import { withDevice } from 'helpers/deviceAccess' import installApp from 'helpers/apps/installApp' -import type { LedgerScriptParams } from 'helpers/types' +import type { ApplicationVersion } from 'helpers/types' type Input = { - app: LedgerScriptParams, + app: ApplicationVersion, devicePath: string, targetId: string | number, } -type Result = * +type Result = void const cmd: Command = createCommand( 'installApp', diff --git a/src/commands/installMcu.js b/src/commands/installMcu.js index 7d85e40b..660bd546 100644 --- a/src/commands/installMcu.js +++ b/src/commands/installMcu.js @@ -10,7 +10,7 @@ type Input = { devicePath: string, } -type Result = * +type Result = void const cmd: Command = createCommand('installMcu', ({ devicePath }) => fromPromise(withDevice(devicePath)(transport => installMcu(transport))), diff --git a/src/commands/installOsuFirmware.js b/src/commands/installOsuFirmware.js index f73a76d4..83b29a39 100644 --- a/src/commands/installOsuFirmware.js +++ b/src/commands/installOsuFirmware.js @@ -14,7 +14,7 @@ type Input = { firmware: Firmware, } -type Result = * +type Result = { success: boolean } const cmd: Command = createCommand( 'installOsuFirmware', diff --git a/src/commands/listAppVersions.js b/src/commands/listAppVersions.js index 494d7168..a802b2c6 100644 --- a/src/commands/listAppVersions.js +++ b/src/commands/listAppVersions.js @@ -2,11 +2,11 @@ import { createCommand, Command } from 'helpers/ipc' import { fromPromise } from 'rxjs/observable/fromPromise' -import type { DeviceInfo } from 'helpers/devices/getDeviceInfo' +import type { DeviceInfo, ApplicationVersion } from 'helpers/types' import listAppVersions from 'helpers/apps/listAppVersions' -type Result = * +type Result = Array const cmd: Command = createCommand('listAppVersions', deviceInfo => fromPromise(listAppVersions(deviceInfo)), diff --git a/src/commands/listApps.js b/src/commands/listApps.js index f049def3..13f78609 100644 --- a/src/commands/listApps.js +++ b/src/commands/listApps.js @@ -4,10 +4,11 @@ import { createCommand, Command } from 'helpers/ipc' import { fromPromise } from 'rxjs/observable/fromPromise' import listApps from 'helpers/apps/listApps' +import type { Application } from 'helpers/types' -type Input = {} +type Input = void -type Result = * +type Result = Array const cmd: Command = createCommand('listApps', () => fromPromise(listApps())) diff --git a/src/commands/listCategories.js b/src/commands/listCategories.js index 3632a47f..641b4ad8 100644 --- a/src/commands/listCategories.js +++ b/src/commands/listCategories.js @@ -4,10 +4,11 @@ import { createCommand, Command } from 'helpers/ipc' import { fromPromise } from 'rxjs/observable/fromPromise' import listCategories from 'helpers/apps/listCategories' +import type { Category } from 'helpers/types' -type Input = {} +type Input = void -type Result = * +type Result = Array const cmd: Command = createCommand('listCategories', () => fromPromise(listCategories()), diff --git a/src/commands/shouldFlashMcu.js b/src/commands/shouldFlashMcu.js index 3bae3f04..f58c629c 100644 --- a/src/commands/shouldFlashMcu.js +++ b/src/commands/shouldFlashMcu.js @@ -2,9 +2,10 @@ import { createCommand, Command } from 'helpers/ipc' import { fromPromise } from 'rxjs/observable/fromPromise' -import type { DeviceInfo } from 'helpers/devices/getDeviceInfo' import shouldFlashMcu from 'helpers/devices/shouldFlashMcu' +import type { DeviceInfo } from 'helpers/types' + type Result = boolean const cmd: Command = createCommand('shouldFlashMcu', data => diff --git a/src/commands/uninstallApp.js b/src/commands/uninstallApp.js index 59a61a3d..9b6df8a2 100644 --- a/src/commands/uninstallApp.js +++ b/src/commands/uninstallApp.js @@ -6,20 +6,20 @@ import { withDevice } from 'helpers/deviceAccess' import uninstallApp from 'helpers/apps/uninstallApp' -import type { LedgerScriptParams } from 'helpers/types' +import type { ApplicationVersion } from 'helpers/types' type Input = { - app: LedgerScriptParams, + app: ApplicationVersion, devicePath: string, targetId: string | number, } -type Result = * +type Result = void const cmd: Command = createCommand( 'uninstallApp', - ({ devicePath, targetId, ...rest }) => - fromPromise(withDevice(devicePath)(transport => uninstallApp(transport, targetId, rest))), + ({ devicePath, targetId, ...app }) => + fromPromise(withDevice(devicePath)(transport => uninstallApp(transport, targetId, app))), ) export default cmd diff --git a/src/components/GenuineCheck.js b/src/components/GenuineCheck.js index b1012d4d..0b79cf69 100644 --- a/src/components/GenuineCheck.js +++ b/src/components/GenuineCheck.js @@ -9,7 +9,7 @@ import { delay, createCancelablePolling } from 'helpers/promise' import logger from 'logger' import type { T, Device } from 'types/common' -import type { DeviceInfo } from 'helpers/devices/getDeviceInfo' +import type { DeviceInfo } from 'helpers/types' import { GENUINE_TIMEOUT, DEVICE_INFOS_TIMEOUT, GENUINE_CACHE_DELAY } from 'config/constants' diff --git a/src/components/ManagerPage/AppSearchBar.js b/src/components/ManagerPage/AppSearchBar.js index cad33077..a9292f14 100644 --- a/src/components/ManagerPage/AppSearchBar.js +++ b/src/components/ManagerPage/AppSearchBar.js @@ -3,7 +3,7 @@ import React, { PureComponent, Fragment } from 'react' import styled from 'styled-components' -import type { LedgerScriptParams } from 'helpers/types' +import type { ApplicationVersion } from 'helpers/types' import Box from 'components/base/Box' import Space from 'components/base/Space' @@ -23,8 +23,8 @@ const CrossContainer = styled(Box).attrs({ ` 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 ae2644fe..e2f8bf1c 100644 --- a/src/components/ManagerPage/AppsList.js +++ b/src/components/ManagerPage/AppsList.js @@ -7,8 +7,7 @@ import { translate } from 'react-i18next' import { connect } from 'react-redux' import { compose } from 'redux' import type { Device, T } from 'types/common' -import type { LedgerScriptParams } from 'helpers/types' -import type { DeviceInfo } from 'helpers/devices/getDeviceInfo' +import type { Application, ApplicationVersion, DeviceInfo } from 'helpers/types' import { developerModeSelector } from 'reducers/settings' import listApps from 'commands/listApps' @@ -66,7 +65,7 @@ type Props = { type State = { status: Status, error: ?Error, - filteredAppVersionsList: LedgerScriptParams[], + filteredAppVersionsList: Array, appsLoaded: boolean, app: string, mode: Mode, @@ -102,9 +101,14 @@ class AppsList extends PureComponent { filterAppVersions = (applicationsList, compatibleAppVersionsList) => { if (!this.props.isDevMode) { - return compatibleAppVersionsList.filter( - version => applicationsList.find(e => e.id === version.app).category !== 2, - ) + return compatibleAppVersionsList.filter(version => { + const app = applicationsList.find(e => e.id === version.app) + if (app) { + return app.category !== 2 + } + + return false + }) } return compatibleAppVersionsList } @@ -112,7 +116,7 @@ class AppsList extends PureComponent { async fetchAppList() { try { const { deviceInfo } = this.props - const applicationsList = await listApps.send({}).toPromise() + const applicationsList: Array = await listApps.send().toPromise() const compatibleAppVersionsList = await listAppVersions.send(deviceInfo).toPromise() const filteredAppVersionsList = this.filterAppVersions( applicationsList, @@ -131,7 +135,7 @@ class AppsList extends PureComponent { } } - handleInstallApp = (app: LedgerScriptParams) => async () => { + handleInstallApp = (app: ApplicationVersion) => async () => { this.setState({ status: 'busy', app: app.name, mode: 'installing' }) try { const { @@ -146,7 +150,7 @@ class AppsList extends PureComponent { } } - handleUninstallApp = (app: LedgerScriptParams) => async () => { + handleUninstallApp = (app: ApplicationVersion) => async () => { this.setState({ status: 'busy', app: app.name, mode: 'uninstalling' }) try { const { diff --git a/src/components/ManagerPage/Dashboard.js b/src/components/ManagerPage/Dashboard.js index 2f04caff..3e726fab 100644 --- a/src/components/ManagerPage/Dashboard.js +++ b/src/components/ManagerPage/Dashboard.js @@ -4,7 +4,7 @@ import { translate } from 'react-i18next' import styled from 'styled-components' import type { T, Device } from 'types/common' -import type { DeviceInfo } from 'helpers/devices/getDeviceInfo' +import type { DeviceInfo } from 'helpers/types' import Box from 'components/base/Box' import Text from 'components/base/Text' diff --git a/src/components/ManagerPage/FirmwareUpdate.js b/src/components/ManagerPage/FirmwareUpdate.js index c289054c..dcaba901 100644 --- a/src/components/ManagerPage/FirmwareUpdate.js +++ b/src/components/ManagerPage/FirmwareUpdate.js @@ -9,7 +9,7 @@ import invariant from 'invariant' import type { Device, T } from 'types/common' -import type { LedgerScriptParams } from 'helpers/types' +import type { DeviceInfo, OsuFirmware } from 'helpers/types' import type { StepId } from 'components/modals/UpdateFirmware' import getLatestFirmwareForDevice from 'commands/getLatestFirmwareForDevice' @@ -19,7 +19,6 @@ import installFinalFirmware from 'commands/installFinalFirmware' import installMcu from 'commands/installMcu' import DisclaimerModal from 'components/modals/UpdateFirmware/Disclaimer' import UpdateModal from 'components/modals/UpdateFirmware' -import type { DeviceInfo } from 'helpers/devices/getDeviceInfo' import Tooltip from 'components/base/Tooltip' import Box, { Card } from 'components/base/Box' @@ -43,7 +42,7 @@ type Props = { } type State = { - latestFirmware: ?LedgerScriptParams & ?{ shouldFlashMcu: boolean }, + latestFirmware: ?OsuFirmware & ?{ shouldFlashMcu: boolean }, modal: ModalStatus, stepId: ?StepId, shouldFlash: boolean, diff --git a/src/components/ManagerPage/index.js b/src/components/ManagerPage/index.js index 956d7c90..7c9631b2 100644 --- a/src/components/ManagerPage/index.js +++ b/src/components/ManagerPage/index.js @@ -6,7 +6,7 @@ import { openURL } from 'helpers/linking' import { urls } from 'config/urls' import type { Device } from 'types/common' -import type { DeviceInfo } from 'helpers/devices/getDeviceInfo' +import type { DeviceInfo } from 'helpers/types' import Dashboard from './Dashboard' diff --git a/src/components/modals/UpdateFirmware/index.js b/src/components/modals/UpdateFirmware/index.js index b6aac554..716db238 100644 --- a/src/components/modals/UpdateFirmware/index.js +++ b/src/components/modals/UpdateFirmware/index.js @@ -10,7 +10,7 @@ import SyncSkipUnderPriority from 'components/SyncSkipUnderPriority' import type { StepProps as DefaultStepProps, Step } from 'components/base/Stepper' import type { ModalStatus } from 'components/ManagerPage/FirmwareUpdate' -import type { LedgerScriptParams } from 'helpers/types' +import type { OsuFirmware } from 'helpers/types' import { FreezeDeviceChangeEvents } from '../../ManagerPage/HookDeviceChange' import StepFullFirmwareInstall from './steps/01-step-install-full-firmware' @@ -56,7 +56,7 @@ const createSteps = ({ t, shouldFlashMcu }: { t: T, shouldFlashMcu: boolean }): return steps } -export type Firmware = LedgerScriptParams & { shouldFlashMcu: boolean } +export type Firmware = OsuFirmware & { shouldFlashMcu: boolean } export type StepProps = DefaultStepProps & { firmware: Firmware, diff --git a/src/helpers/apps/installApp.js b/src/helpers/apps/installApp.js index b5bb00f9..ce45f797 100644 --- a/src/helpers/apps/installApp.js +++ b/src/helpers/apps/installApp.js @@ -1,13 +1,12 @@ // @flow -import qs from 'qs' import type Transport from '@ledgerhq/hw-transport' -import { BASE_SOCKET_URL } from 'config/constants' import { createDeviceSocket } from 'helpers/socket' -import type { LedgerScriptParams } from 'helpers/types' +import type { ApplicationVersion } from 'helpers/types' import { createCustomErrorClass } from 'helpers/errors' +import { WS_INSTALL } from 'helpers/urls' const ManagerNotEnoughSpaceError = createCustomErrorClass('ManagerNotEnoughSpace') const ManagerDeviceLockedError = createCustomErrorClass('ManagerDeviceLocked') @@ -37,8 +36,8 @@ function remapError(promise) { export default async function installApp( transport: Transport<*>, targetId: string | number, - { app }: { app: LedgerScriptParams }, -): Promise<*> { + { app }: { app: ApplicationVersion }, +): Promise { const params = { targetId, perso: app.perso, @@ -47,6 +46,7 @@ export default async function installApp( firmwareKey: app.firmware_key, hash: app.hash, } - const url = `${BASE_SOCKET_URL}/install?${qs.stringify(params)}` - return remapError(createDeviceSocket(transport, url).toPromise()) + + const url = WS_INSTALL(params) + await remapError(createDeviceSocket(transport, url).toPromise()) } diff --git a/src/helpers/apps/listAppVersions.js b/src/helpers/apps/listAppVersions.js index b29ba127..bc48fc6a 100644 --- a/src/helpers/apps/listAppVersions.js +++ b/src/helpers/apps/listAppVersions.js @@ -1,14 +1,19 @@ // @flow import network from 'api/network' -import type { DeviceInfo } from 'helpers/devices/getDeviceInfo' +import type { DeviceInfo, DeviceVersion, FinalFirmware, ApplicationVersion } from 'helpers/types' import { APPLICATIONS_BY_DEVICE } from 'helpers/urls' import getDeviceVersion from 'helpers/devices/getDeviceVersion' import getCurrentFirmware from 'helpers/devices/getCurrentFirmware' -export default async (deviceInfo: DeviceInfo) => { - const deviceData = await getDeviceVersion(deviceInfo.targetId, deviceInfo.providerId) - const firmwareData = await getCurrentFirmware({ +type NetworkResponse = { data: { application_versions: Array } } + +export default async (deviceInfo: DeviceInfo): Promise> => { + const deviceData: DeviceVersion = await getDeviceVersion( + deviceInfo.targetId, + deviceInfo.providerId, + ) + const firmwareData: FinalFirmware = await getCurrentFirmware({ deviceId: deviceData.id, fullVersion: deviceInfo.fullVersion, provider: deviceInfo.providerId, @@ -20,6 +25,6 @@ export default async (deviceInfo: DeviceInfo) => { } const { data: { application_versions }, - } = await network({ method: 'POST', url: APPLICATIONS_BY_DEVICE, data: params }) + }: NetworkResponse = await network({ method: 'POST', url: APPLICATIONS_BY_DEVICE, data: params }) return application_versions.length > 0 ? application_versions : [] } diff --git a/src/helpers/apps/listApps.js b/src/helpers/apps/listApps.js index 372d5ad1..14fed369 100644 --- a/src/helpers/apps/listApps.js +++ b/src/helpers/apps/listApps.js @@ -2,8 +2,9 @@ import network from 'api/network' import { GET_APPLICATIONS } from 'helpers/urls' +import type { Application } from 'helpers/types' -export default async () => { +export default async (): Promise> => { const { data } = await network({ method: 'GET', url: GET_APPLICATIONS }) return data.length > 0 ? data : [] } diff --git a/src/helpers/apps/listCategories.js b/src/helpers/apps/listCategories.js index 59836572..1c903b0a 100644 --- a/src/helpers/apps/listCategories.js +++ b/src/helpers/apps/listCategories.js @@ -2,8 +2,9 @@ import network from 'api/network' import { GET_CATEGORIES } from 'helpers/urls' +import type { Category } from 'helpers/types' -export default async () => { - const { data } = await network({ method: 'GET', url: GET_CATEGORIES }) +export default async (): Promise> => { + const { data }: { data: Array } = await network({ method: 'GET', url: GET_CATEGORIES }) return data.length > 0 ? data : [] } diff --git a/src/helpers/apps/uninstallApp.js b/src/helpers/apps/uninstallApp.js index 5bf3dd68..7ce6fef3 100644 --- a/src/helpers/apps/uninstallApp.js +++ b/src/helpers/apps/uninstallApp.js @@ -1,12 +1,11 @@ // @flow -import qs from 'qs' import type Transport from '@ledgerhq/hw-transport' -import { BASE_SOCKET_URL } from 'config/constants' import { createDeviceSocket } from 'helpers/socket' -import type { LedgerScriptParams } from 'helpers/types' -import { createCustomErrorClass } from '../errors' +import type { ApplicationVersion } from 'helpers/types' +import { createCustomErrorClass } from 'helpers/errors' +import { WS_INSTALL } from 'helpers/urls' const ManagerDeviceLockedError = createCustomErrorClass('ManagerDeviceLocked') const ManagerUninstallBTCDep = createCustomErrorClass('ManagerUninstallBTCDep') @@ -30,8 +29,8 @@ function remapError(promise) { export default async function uninstallApp( transport: Transport<*>, targetId: string | number, - { app }: { app: LedgerScriptParams }, -): Promise<*> { + { app }: { app: ApplicationVersion }, +): Promise { const params = { targetId, perso: app.perso, @@ -40,6 +39,6 @@ export default async function uninstallApp( firmwareKey: app.delete_key, hash: app.hash, } - const url = `${BASE_SOCKET_URL}/install?${qs.stringify(params)}` - return remapError(createDeviceSocket(transport, url).toPromise()) + const url = WS_INSTALL(params) + await remapError(createDeviceSocket(transport, url).toPromise()) } diff --git a/src/helpers/devices/getCurrentFirmware.js b/src/helpers/devices/getCurrentFirmware.js index a98c01d5..c4ef5979 100644 --- a/src/helpers/devices/getCurrentFirmware.js +++ b/src/helpers/devices/getCurrentFirmware.js @@ -2,6 +2,7 @@ import network from 'api/network' import { GET_CURRENT_FIRMWARE } from 'helpers/urls' +import type { FinalFirmware } from 'helpers/types' type Input = { fullVersion: string, @@ -9,8 +10,8 @@ type Input = { provider: number, } -export default async (input: Input): Promise<*> => { - const { data } = await network({ +export default async (input: Input): Promise => { + const { data }: { data: FinalFirmware } = await network({ method: 'POST', url: GET_CURRENT_FIRMWARE, data: { diff --git a/src/helpers/devices/getDeviceInfo.js b/src/helpers/devices/getDeviceInfo.js index c3972e3d..24b6d7d1 100644 --- a/src/helpers/devices/getDeviceInfo.js +++ b/src/helpers/devices/getDeviceInfo.js @@ -5,17 +5,7 @@ import type Transport from '@ledgerhq/hw-transport' import getFirmwareInfo from 'helpers/firmware/getFirmwareInfo' import { FORCE_PROVIDER } from 'config/constants' -export type DeviceInfo = { - targetId: string | number, - seVersion: string, - isBootloader: boolean, - flags: string, - mcuVersion: string, - isOSU: boolean, - providerName: string, - providerId: number, - fullVersion: string, -} +import type { DeviceInfo } from 'helpers/types' const PROVIDERS = { '': 1, diff --git a/src/helpers/devices/getDeviceVersion.js b/src/helpers/devices/getDeviceVersion.js index 698bc1fa..c681cfa9 100644 --- a/src/helpers/devices/getDeviceVersion.js +++ b/src/helpers/devices/getDeviceVersion.js @@ -2,8 +2,10 @@ import { GET_DEVICE_VERSION } from 'helpers/urls' import network from 'api/network' -export default async (targetId: string | number, provider: number): Promise<*> => { - const { data } = await network({ +import type { DeviceVersion } from 'helpers/types' + +export default async (targetId: string | number, provider: number): Promise => { + const { data }: { data: DeviceVersion } = await network({ method: 'POST', url: GET_DEVICE_VERSION, data: { diff --git a/src/helpers/devices/getIsGenuine.js b/src/helpers/devices/getIsGenuine.js index 9ea7c56d..3357042b 100644 --- a/src/helpers/devices/getIsGenuine.js +++ b/src/helpers/devices/getIsGenuine.js @@ -2,23 +2,29 @@ import type Transport from '@ledgerhq/hw-transport' import { SKIP_GENUINE } from 'config/constants' import { WS_GENUINE } from 'helpers/urls' -import type { DeviceInfo } from 'helpers/devices/getDeviceInfo' +import type { DeviceInfo, FinalFirmware, DeviceVersion } from 'helpers/types' import { createDeviceSocket } from 'helpers/socket' import getCurrentFirmware from './getCurrentFirmware' import getDeviceVersion from './getDeviceVersion' export default async (transport: Transport<*>, deviceInfo: DeviceInfo): Promise => { - const deviceVersion = await getDeviceVersion(deviceInfo.targetId, deviceInfo.providerId) - const firmware = await getCurrentFirmware({ + const deviceVersion: DeviceVersion = await getDeviceVersion( + deviceInfo.targetId, + deviceInfo.providerId, + ) + + const firmware: FinalFirmware = await getCurrentFirmware({ deviceId: deviceVersion.id, fullVersion: deviceInfo.fullVersion, provider: deviceInfo.providerId, }) + const params = { targetId: deviceInfo.targetId, perso: firmware.perso, } + const url = WS_GENUINE(params) return SKIP_GENUINE ? new Promise(resolve => setTimeout(() => resolve('0000'), 1000)) diff --git a/src/helpers/devices/getLatestFirmwareForDevice.js b/src/helpers/devices/getLatestFirmwareForDevice.js index 9bbf7296..80e14af8 100644 --- a/src/helpers/devices/getLatestFirmwareForDevice.js +++ b/src/helpers/devices/getLatestFirmwareForDevice.js @@ -1,7 +1,13 @@ // @flow import network from 'api/network' import { GET_LATEST_FIRMWARE } from 'helpers/urls' -import type { DeviceInfo } from 'helpers/devices/getDeviceInfo' +import type { + DeviceInfo, + DeviceVersion, + FinalFirmware, + OsuFirmware, + McuVersion, +} from 'helpers/types' import getFinalFirmwareById from 'helpers/firmware/getFinalFirmwareById' import getMcus from 'helpers/firmware/getMcus' @@ -9,19 +15,31 @@ import getMcus from 'helpers/firmware/getMcus' import getCurrentFirmware from './getCurrentFirmware' import getDeviceVersion from './getDeviceVersion' -export default async (deviceInfo: DeviceInfo) => { +type NetworkResponse = { + data: { + result: string, + se_firmware_osu_version: OsuFirmware, + }, +} + +type Result = ?(OsuFirmware & { shouldFlashMcu: boolean }) + +export default async (deviceInfo: DeviceInfo): Promise => { // Get device infos from targetId - const deviceVersion = await getDeviceVersion(deviceInfo.targetId, deviceInfo.providerId) + const deviceVersion: DeviceVersion = await getDeviceVersion( + deviceInfo.targetId, + deviceInfo.providerId, + ) // Get firmware infos with firmware name and device version - const seFirmwareVersion = await getCurrentFirmware({ + const seFirmwareVersion: FinalFirmware = await getCurrentFirmware({ fullVersion: deviceInfo.fullVersion, deviceId: deviceVersion.id, provider: deviceInfo.providerId, }) // Fetch next possible firmware - const { data } = await network({ + const { data }: NetworkResponse = await network({ method: 'POST', url: GET_LATEST_FIRMWARE, data: { @@ -37,11 +55,13 @@ export default async (deviceInfo: DeviceInfo) => { const { se_firmware_osu_version } = data const { next_se_firmware_final_version } = se_firmware_osu_version - const seFirmwareFinalVersion = await getFinalFirmwareById(next_se_firmware_final_version) + const seFirmwareFinalVersion: FinalFirmware = await getFinalFirmwareById( + next_se_firmware_final_version, + ) - const mcus = await getMcus() + const mcus: Array = await getMcus() - const currentMcuVersionId = mcus + const currentMcuVersionId: Array = mcus .filter(mcu => mcu.name === deviceInfo.mcuVersion) .map(mcu => mcu.id) diff --git a/src/helpers/devices/shouldFlashMcu.js b/src/helpers/devices/shouldFlashMcu.js index dba76d75..1ed0a768 100644 --- a/src/helpers/devices/shouldFlashMcu.js +++ b/src/helpers/devices/shouldFlashMcu.js @@ -1,7 +1,7 @@ // @flow import network from 'api/network' import { GET_LATEST_FIRMWARE } from 'helpers/urls' -import type { DeviceInfo } from 'helpers/devices/getDeviceInfo' +import type { DeviceInfo } from 'helpers/types' import getFinalFirmwareById from 'helpers/firmware/getFinalFirmwareById' import getMcus from 'helpers/firmware/getMcus' diff --git a/src/helpers/firmware/getFirmwareInfo.js b/src/helpers/firmware/getFirmwareInfo.js index fc3033a1..b3c4a81c 100644 --- a/src/helpers/firmware/getFirmwareInfo.js +++ b/src/helpers/firmware/getFirmwareInfo.js @@ -1,6 +1,7 @@ // @flow import type Transport from '@ledgerhq/hw-transport' +import type { FirmwareInfo } from 'helpers/types' const APDUS = { GET_FIRMWARE: [0xe0, 0x01, 0x00, 0x00], @@ -12,7 +13,7 @@ const APDUS = { /** * Retrieve targetId and firmware version from device */ -export default async function getFirmwareInfo(transport: Transport<*>) { +export default async function getFirmwareInfo(transport: Transport<*>): Promise { const res = await transport.send(...APDUS.GET_FIRMWARE) const byteArray = [...res] const data = byteArray.slice(0, byteArray.length - 2) diff --git a/src/helpers/firmware/getNextMCU.js b/src/helpers/firmware/getNextMCU.js index 7565e379..3ab9337f 100644 --- a/src/helpers/firmware/getNextMCU.js +++ b/src/helpers/firmware/getNextMCU.js @@ -3,11 +3,14 @@ import network from 'api/network' import { GET_NEXT_MCU } from 'helpers/urls' import { createCustomErrorClass } from 'helpers/errors' +import type { OsuFirmware } from 'helpers/types' const LatestMCUInstalledError = createCustomErrorClass('LatestMCUInstalledError') +type NetworkResponse = { data: OsuFirmware | 'default' } + export default async (bootloaderVersion: string): Promise<*> => { - const { data } = await network({ + const { data }: NetworkResponse = await network({ method: 'POST', url: GET_NEXT_MCU, data: { diff --git a/src/helpers/firmware/installFinalFirmware.js b/src/helpers/firmware/installFinalFirmware.js index a588e0e5..be13f6c1 100644 --- a/src/helpers/firmware/installFinalFirmware.js +++ b/src/helpers/firmware/installFinalFirmware.js @@ -1,6 +1,6 @@ // @flow import type Transport from '@ledgerhq/hw-transport' -import type { DeviceInfo } from 'helpers/devices/getDeviceInfo' +import type { DeviceInfo, DeviceVersion, OsuFirmware, FinalFirmware } from 'helpers/types' import { WS_INSTALL } from 'helpers/urls' import { createDeviceSocket } from 'helpers/socket' @@ -23,19 +23,19 @@ function remapSocketError(promise) { }) } -type Result = Promise<{ success: boolean, error?: string }> +type Result = Promise<{ success: boolean }> export default async (transport: Transport<*>): Result => { try { const deviceInfo: DeviceInfo = await getDeviceInfo(transport) - const device = await getDeviceVersion(deviceInfo.targetId, deviceInfo.providerId) - const firmware = await getOsuFirmware({ + const device: DeviceVersion = await getDeviceVersion(deviceInfo.targetId, deviceInfo.providerId) + const firmware: OsuFirmware = await getOsuFirmware({ deviceId: device.id, version: deviceInfo.fullVersion, provider: deviceInfo.providerId, }) const { next_se_firmware_final_version } = firmware - const nextFirmware = await getFinalFirmwareById(next_se_firmware_final_version) + const nextFirmware: FinalFirmware = await getFinalFirmwareById(next_se_firmware_final_version) const params = { targetId: deviceInfo.targetId, diff --git a/src/helpers/firmware/installMcu.js b/src/helpers/firmware/installMcu.js index aca208c8..d0ffb39e 100644 --- a/src/helpers/firmware/installMcu.js +++ b/src/helpers/firmware/installMcu.js @@ -7,6 +7,8 @@ import getNextMCU from 'helpers/firmware/getNextMCU' import getDeviceInfo from 'helpers/devices/getDeviceInfo' import { createCustomErrorClass } from 'helpers/errors' +import type { DeviceInfo } from 'helpers/types' + const ManagerDeviceLockedError = createCustomErrorClass('ManagerDeviceLocked') function remapSocketError(promise) { @@ -20,16 +22,15 @@ function remapSocketError(promise) { }) } -type Result = Promise<*> +type Result = Promise export default async (transport: Transport<*>): Result => { - const deviceInfo = await getDeviceInfo(transport) - const { seVersion: version, targetId } = deviceInfo + const { seVersion: version, targetId }: DeviceInfo = await getDeviceInfo(transport) const nextVersion = await getNextMCU(version) const params = { targetId, version: nextVersion.name, } const url = WS_MCU(params) - return remapSocketError(createDeviceSocket(transport, url).toPromise()) + await remapSocketError(createDeviceSocket(transport, url).toPromise()) } diff --git a/src/helpers/firmware/installOsuFirmware.js b/src/helpers/firmware/installOsuFirmware.js index 603404e5..1121c340 100644 --- a/src/helpers/firmware/installOsuFirmware.js +++ b/src/helpers/firmware/installOsuFirmware.js @@ -27,7 +27,7 @@ function remapError(promise) { }) } -type Result = Promise<{ success: boolean, error?: any }> +type Result = Promise<{ success: boolean }> export default async ( transport: Transport<*>, diff --git a/src/helpers/types.js b/src/helpers/types.js index 56ac3f50..a9bea673 100644 --- a/src/helpers/types.js +++ b/src/helpers/types.js @@ -1,14 +1,134 @@ // @flow +type Id = number + export type LedgerScriptParams = { - firmware?: string, - firmware_key?: string, + firmware: string, + firmwareKey: string, delete?: string, - delete_key?: string, + deleteKey?: string, targetId?: string | number, + hash: string, + perso: string, +} + +export type DeviceInfo = { + targetId: string | number, + seVersion: string, + isBootloader: boolean, + flags: string, + mcuVersion: string, + isOSU: boolean, + providerName: string, + providerId: number, + fullVersion: string, +} + +export type DeviceVersion = { + id: Id, + name: string, + display_name: string, + target_id: string, + description: string, + device: Id, + providers: Array, + mcu_versions: Array, + se_firmware_final_versions: Array, + osu_versions: Array, + application_versions: Array, + date_creation: string, + date_last_modified: string, +} + +export type McuVersion = { + id: Id, + mcu: Id, + name: string, + description: ?string, + providers: Array, + from_bootloader_version: string, + device_versions: Array, + se_firmware_final_versions: Array, + date_creation: string, + date_last_modified: string, +} + +export type FirmwareInfo = { + targetId: Id, + seVersion: string, + flags: string, + mcuVersion: string, +} + +type BaseFirmware = { + id: Id, + name: string, + description: ?string, + display_name: ?string, + notes: ?string, + perso: string, + firmware: string, + firmware_key: string, + hash: string, + date_creation: string, + date_last_modified: string, + device_versions: Array, + providers: Array, +} + +export type OsuFirmware = BaseFirmware & { + next_se_firmware_final_version: Id, + previous_se_firmware_final_version: Array, +} + +export type FinalFirmware = BaseFirmware & { + version: string, + se_firmware: Id, + osu_versions: Array, + mcu_versions: Array, + application_versions: Array, +} + +export type ApplicationVersion = { + id: Id, name: string, version: string, + app: Id, + description: ?string, + display_name: string, icon: string, - app?: number, - hash?: string, - perso?: string, + picture: Id, + notes: ?string, + perso: string, + hash: string, + firmware: string, + firmware_key: string, + delete: string, + delete_key: string, + device_versions: Array, + se_firmware_final_versions: Array, + providers: Array, + date_creation: string, + date_last_modified: string, +} + +export type Application = { + id: Id, + name: string, + description: ?string, + application_versions: Array, + providers: Array, + category: Id, + publisher: ?Id, + date_creation: string, + date_last_modified: string, +} + +export type Category = { + id: Id, + name: string, + description: ?string, + providers: Array, + applications: Array, + date_creation: string, + date_last_modified: string, }