Browse Source

Merge pull request #1353 from valpinkman/flow-types-commands

add typing to all commands and api calls. fixes #1348
master
Valentin D. Pinkman 7 years ago
committed by GitHub
parent
commit
d7db8b211a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      .gitignore
  2. 3
      src/commands/getCurrentFirmware.js
  3. 6
      src/commands/getDeviceInfo.js
  4. 2
      src/commands/getIsGenuine.js
  5. 4
      src/commands/getLatestFirmwareForDevice.js
  6. 6
      src/commands/installApp.js
  7. 2
      src/commands/installMcu.js
  8. 2
      src/commands/installOsuFirmware.js
  9. 4
      src/commands/listAppVersions.js
  10. 5
      src/commands/listApps.js
  11. 5
      src/commands/listCategories.js
  12. 3
      src/commands/shouldFlashMcu.js
  13. 10
      src/commands/uninstallApp.js
  14. 2
      src/components/GenuineCheck.js
  15. 6
      src/components/ManagerPage/AppSearchBar.js
  16. 22
      src/components/ManagerPage/AppsList.js
  17. 2
      src/components/ManagerPage/Dashboard.js
  18. 5
      src/components/ManagerPage/FirmwareUpdate.js
  19. 2
      src/components/ManagerPage/index.js
  20. 4
      src/components/modals/UpdateFirmware/index.js
  21. 14
      src/helpers/apps/installApp.js
  22. 15
      src/helpers/apps/listAppVersions.js
  23. 3
      src/helpers/apps/listApps.js
  24. 5
      src/helpers/apps/listCategories.js
  25. 15
      src/helpers/apps/uninstallApp.js
  26. 5
      src/helpers/devices/getCurrentFirmware.js
  27. 12
      src/helpers/devices/getDeviceInfo.js
  28. 6
      src/helpers/devices/getDeviceVersion.js
  29. 12
      src/helpers/devices/getIsGenuine.js
  30. 36
      src/helpers/devices/getLatestFirmwareForDevice.js
  31. 2
      src/helpers/devices/shouldFlashMcu.js
  32. 3
      src/helpers/firmware/getFirmwareInfo.js
  33. 5
      src/helpers/firmware/getNextMCU.js
  34. 10
      src/helpers/firmware/installFinalFirmware.js
  35. 9
      src/helpers/firmware/installMcu.js
  36. 2
      src/helpers/firmware/installOsuFirmware.js
  37. 132
      src/helpers/types.js

8
.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

3
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<Input, Result> = createCommand('getCurrentFirmware', data =>
fromPromise(getCurrentFirmware(data)),

6
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<Input, DeviceInfo> = createCommand('getDeviceInfo', ({ devicePath }) =>
type Result = DeviceInfo
const cmd: Command<Input, Result> = createCommand('getDeviceInfo', ({ devicePath }) =>
fromPromise(withDevice(devicePath)(transport => getDeviceInfo(transport))),
)

2
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'

4
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<DeviceInfo, Result> = createCommand('getLatestFirmwareForDevice', data =>
fromPromise(getLatestFirmwareForDevice(data)),

6
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<Input, Result> = createCommand(
'installApp',

2
src/commands/installMcu.js

@ -10,7 +10,7 @@ type Input = {
devicePath: string,
}
type Result = *
type Result = void
const cmd: Command<Input, Result> = createCommand('installMcu', ({ devicePath }) =>
fromPromise(withDevice(devicePath)(transport => installMcu(transport))),

2
src/commands/installOsuFirmware.js

@ -14,7 +14,7 @@ type Input = {
firmware: Firmware,
}
type Result = *
type Result = { success: boolean }
const cmd: Command<Input, Result> = createCommand(
'installOsuFirmware',

4
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<ApplicationVersion>
const cmd: Command<DeviceInfo, Result> = createCommand('listAppVersions', deviceInfo =>
fromPromise(listAppVersions(deviceInfo)),

5
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<Application>
const cmd: Command<Input, Result> = createCommand('listApps', () => fromPromise(listApps()))

5
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<Category>
const cmd: Command<Input, Result> = createCommand('listCategories', () =>
fromPromise(listCategories()),

3
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<DeviceInfo, Result> = createCommand('shouldFlashMcu', data =>

10
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<Input, Result> = 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

2
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'

6
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<LedgerScriptParams>,
children: (list: Array<LedgerScriptParams>) => React$Node,
list: Array<ApplicationVersion>,
children: (list: Array<ApplicationVersion>) => React$Node,
}
type State = {

22
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<ApplicationVersion>,
appsLoaded: boolean,
app: string,
mode: Mode,
@ -102,9 +101,14 @@ class AppsList extends PureComponent<Props, State> {
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<Props, State> {
async fetchAppList() {
try {
const { deviceInfo } = this.props
const applicationsList = await listApps.send({}).toPromise()
const applicationsList: Array<Application> = await listApps.send().toPromise()
const compatibleAppVersionsList = await listAppVersions.send(deviceInfo).toPromise()
const filteredAppVersionsList = this.filterAppVersions(
applicationsList,
@ -131,7 +135,7 @@ class AppsList extends PureComponent<Props, State> {
}
}
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<Props, State> {
}
}
handleUninstallApp = (app: LedgerScriptParams) => async () => {
handleUninstallApp = (app: ApplicationVersion) => async () => {
this.setState({ status: 'busy', app: app.name, mode: 'uninstalling' })
try {
const {

2
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'

5
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,

2
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'

4
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,

14
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<void> {
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())
}

15
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<ApplicationVersion> } }
export default async (deviceInfo: DeviceInfo): Promise<Array<ApplicationVersion>> => {
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 : []
}

3
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<Array<Application>> => {
const { data } = await network({ method: 'GET', url: GET_APPLICATIONS })
return data.length > 0 ? data : []
}

5
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<Array<Category>> => {
const { data }: { data: Array<Category> } = await network({ method: 'GET', url: GET_CATEGORIES })
return data.length > 0 ? data : []
}

15
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<void> {
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())
}

5
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<FinalFirmware> => {
const { data }: { data: FinalFirmware } = await network({
method: 'POST',
url: GET_CURRENT_FIRMWARE,
data: {

12
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,

6
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<DeviceVersion> => {
const { data }: { data: DeviceVersion } = await network({
method: 'POST',
url: GET_DEVICE_VERSION,
data: {

12
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<string> => {
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))

36
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<Result> => {
// 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<McuVersion> = await getMcus()
const currentMcuVersionId = mcus
const currentMcuVersionId: Array<number> = mcus
.filter(mcu => mcu.name === deviceInfo.mcuVersion)
.map(mcu => mcu.id)

2
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'

3
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<FirmwareInfo> {
const res = await transport.send(...APDUS.GET_FIRMWARE)
const byteArray = [...res]
const data = byteArray.slice(0, byteArray.length - 2)

5
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: {

10
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,

9
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<void>
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())
}

2
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<*>,

132
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<Id>,
mcu_versions: Array<Id>,
se_firmware_final_versions: Array<Id>,
osu_versions: Array<Id>,
application_versions: Array<Id>,
date_creation: string,
date_last_modified: string,
}
export type McuVersion = {
id: Id,
mcu: Id,
name: string,
description: ?string,
providers: Array<Id>,
from_bootloader_version: string,
device_versions: Array<Id>,
se_firmware_final_versions: Array<Id>,
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<Id>,
providers: Array<Id>,
}
export type OsuFirmware = BaseFirmware & {
next_se_firmware_final_version: Id,
previous_se_firmware_final_version: Array<Id>,
}
export type FinalFirmware = BaseFirmware & {
version: string,
se_firmware: Id,
osu_versions: Array<OsuFirmware>,
mcu_versions: Array<Id>,
application_versions: Array<Id>,
}
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<Id>,
se_firmware_final_versions: Array<Id>,
providers: Array<Id>,
date_creation: string,
date_last_modified: string,
}
export type Application = {
id: Id,
name: string,
description: ?string,
application_versions: Array<ApplicationVersion>,
providers: Array<Id>,
category: Id,
publisher: ?Id,
date_creation: string,
date_last_modified: string,
}
export type Category = {
id: Id,
name: string,
description: ?string,
providers: Array<Id>,
applications: Array<Id>,
date_creation: string,
date_last_modified: string,
}

Loading…
Cancel
Save