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/src
/build/linux/arch/*.tar.gz /build/linux/arch/*.tar.gz
/build/linux/arch/*.tar.xz /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 { fromPromise } from 'rxjs/observable/fromPromise'
import getCurrentFirmware from 'helpers/devices/getCurrentFirmware' import getCurrentFirmware from 'helpers/devices/getCurrentFirmware'
import type { FinalFirmware } from 'helpers/types'
type Input = { type Input = {
deviceId: string | number, deviceId: string | number,
@ -11,7 +12,7 @@ type Input = {
provider: number, provider: number,
} }
type Result = * type Result = FinalFirmware
const cmd: Command<Input, Result> = createCommand('getCurrentFirmware', data => const cmd: Command<Input, Result> = createCommand('getCurrentFirmware', data =>
fromPromise(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 { withDevice } from 'helpers/deviceAccess'
import getDeviceInfo from 'helpers/devices/getDeviceInfo' import getDeviceInfo from 'helpers/devices/getDeviceInfo'
import type { DeviceInfo } from 'helpers/devices/getDeviceInfo' import type { DeviceInfo } from 'helpers/types'
type Input = { type Input = {
devicePath: string, 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))), fromPromise(withDevice(devicePath)(transport => getDeviceInfo(transport))),
) )

2
src/commands/getIsGenuine.js

@ -2,7 +2,7 @@
import { createCommand, Command } from 'helpers/ipc' import { createCommand, Command } from 'helpers/ipc'
import { fromPromise } from 'rxjs/observable/fromPromise' 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 getIsGenuine from 'helpers/devices/getIsGenuine'
import { withDevice } from 'helpers/deviceAccess' import { withDevice } from 'helpers/deviceAccess'

4
src/commands/getLatestFirmwareForDevice.js

@ -2,11 +2,11 @@
import { createCommand, Command } from 'helpers/ipc' import { createCommand, Command } from 'helpers/ipc'
import { fromPromise } from 'rxjs/observable/fromPromise' 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' import getLatestFirmwareForDevice from '../helpers/devices/getLatestFirmwareForDevice'
type Result = * type Result = ?(OsuFirmware & { shouldFlashMcu: boolean })
const cmd: Command<DeviceInfo, Result> = createCommand('getLatestFirmwareForDevice', data => const cmd: Command<DeviceInfo, Result> = createCommand('getLatestFirmwareForDevice', data =>
fromPromise(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 { withDevice } from 'helpers/deviceAccess'
import installApp from 'helpers/apps/installApp' import installApp from 'helpers/apps/installApp'
import type { LedgerScriptParams } from 'helpers/types' import type { ApplicationVersion } from 'helpers/types'
type Input = { type Input = {
app: LedgerScriptParams, app: ApplicationVersion,
devicePath: string, devicePath: string,
targetId: string | number, targetId: string | number,
} }
type Result = * type Result = void
const cmd: Command<Input, Result> = createCommand( const cmd: Command<Input, Result> = createCommand(
'installApp', 'installApp',

2
src/commands/installMcu.js

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

2
src/commands/installOsuFirmware.js

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

4
src/commands/listAppVersions.js

@ -2,11 +2,11 @@
import { createCommand, Command } from 'helpers/ipc' import { createCommand, Command } from 'helpers/ipc'
import { fromPromise } from 'rxjs/observable/fromPromise' 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' import listAppVersions from 'helpers/apps/listAppVersions'
type Result = * type Result = Array<ApplicationVersion>
const cmd: Command<DeviceInfo, Result> = createCommand('listAppVersions', deviceInfo => const cmd: Command<DeviceInfo, Result> = createCommand('listAppVersions', deviceInfo =>
fromPromise(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 { fromPromise } from 'rxjs/observable/fromPromise'
import listApps from 'helpers/apps/listApps' 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())) 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 { fromPromise } from 'rxjs/observable/fromPromise'
import listCategories from 'helpers/apps/listCategories' 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', () => const cmd: Command<Input, Result> = createCommand('listCategories', () =>
fromPromise(listCategories()), fromPromise(listCategories()),

3
src/commands/shouldFlashMcu.js

@ -2,9 +2,10 @@
import { createCommand, Command } from 'helpers/ipc' import { createCommand, Command } from 'helpers/ipc'
import { fromPromise } from 'rxjs/observable/fromPromise' import { fromPromise } from 'rxjs/observable/fromPromise'
import type { DeviceInfo } from 'helpers/devices/getDeviceInfo'
import shouldFlashMcu from 'helpers/devices/shouldFlashMcu' import shouldFlashMcu from 'helpers/devices/shouldFlashMcu'
import type { DeviceInfo } from 'helpers/types'
type Result = boolean type Result = boolean
const cmd: Command<DeviceInfo, Result> = createCommand('shouldFlashMcu', data => 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 uninstallApp from 'helpers/apps/uninstallApp'
import type { LedgerScriptParams } from 'helpers/types' import type { ApplicationVersion } from 'helpers/types'
type Input = { type Input = {
app: LedgerScriptParams, app: ApplicationVersion,
devicePath: string, devicePath: string,
targetId: string | number, targetId: string | number,
} }
type Result = * type Result = void
const cmd: Command<Input, Result> = createCommand( const cmd: Command<Input, Result> = createCommand(
'uninstallApp', 'uninstallApp',
({ devicePath, targetId, ...rest }) => ({ devicePath, targetId, ...app }) =>
fromPromise(withDevice(devicePath)(transport => uninstallApp(transport, targetId, rest))), fromPromise(withDevice(devicePath)(transport => uninstallApp(transport, targetId, app))),
) )
export default cmd export default cmd

2
src/components/GenuineCheck.js

@ -9,7 +9,7 @@ import { delay, createCancelablePolling } from 'helpers/promise'
import logger from 'logger' import logger from 'logger'
import type { T, Device } from 'types/common' 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' 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 React, { PureComponent, Fragment } from 'react'
import styled from 'styled-components' import styled from 'styled-components'
import type { LedgerScriptParams } from 'helpers/types' import type { ApplicationVersion } from 'helpers/types'
import Box from 'components/base/Box' import Box from 'components/base/Box'
import Space from 'components/base/Space' import Space from 'components/base/Space'
@ -23,8 +23,8 @@ const CrossContainer = styled(Box).attrs({
` `
type Props = { type Props = {
list: Array<LedgerScriptParams>, list: Array<ApplicationVersion>,
children: (list: Array<LedgerScriptParams>) => React$Node, children: (list: Array<ApplicationVersion>) => React$Node,
} }
type State = { type State = {

22
src/components/ManagerPage/AppsList.js

@ -7,8 +7,7 @@ import { translate } from 'react-i18next'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { compose } from 'redux' import { compose } from 'redux'
import type { Device, T } from 'types/common' import type { Device, T } from 'types/common'
import type { LedgerScriptParams } from 'helpers/types' import type { Application, ApplicationVersion, DeviceInfo } from 'helpers/types'
import type { DeviceInfo } from 'helpers/devices/getDeviceInfo'
import { developerModeSelector } from 'reducers/settings' import { developerModeSelector } from 'reducers/settings'
import listApps from 'commands/listApps' import listApps from 'commands/listApps'
@ -66,7 +65,7 @@ type Props = {
type State = { type State = {
status: Status, status: Status,
error: ?Error, error: ?Error,
filteredAppVersionsList: LedgerScriptParams[], filteredAppVersionsList: Array<ApplicationVersion>,
appsLoaded: boolean, appsLoaded: boolean,
app: string, app: string,
mode: Mode, mode: Mode,
@ -102,9 +101,14 @@ class AppsList extends PureComponent<Props, State> {
filterAppVersions = (applicationsList, compatibleAppVersionsList) => { filterAppVersions = (applicationsList, compatibleAppVersionsList) => {
if (!this.props.isDevMode) { if (!this.props.isDevMode) {
return compatibleAppVersionsList.filter( return compatibleAppVersionsList.filter(version => {
version => applicationsList.find(e => e.id === version.app).category !== 2, const app = applicationsList.find(e => e.id === version.app)
) if (app) {
return app.category !== 2
}
return false
})
} }
return compatibleAppVersionsList return compatibleAppVersionsList
} }
@ -112,7 +116,7 @@ class AppsList extends PureComponent<Props, State> {
async fetchAppList() { async fetchAppList() {
try { try {
const { deviceInfo } = this.props 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 compatibleAppVersionsList = await listAppVersions.send(deviceInfo).toPromise()
const filteredAppVersionsList = this.filterAppVersions( const filteredAppVersionsList = this.filterAppVersions(
applicationsList, 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' }) this.setState({ status: 'busy', app: app.name, mode: 'installing' })
try { try {
const { 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' }) this.setState({ status: 'busy', app: app.name, mode: 'uninstalling' })
try { try {
const { const {

2
src/components/ManagerPage/Dashboard.js

@ -4,7 +4,7 @@ import { translate } from 'react-i18next'
import styled from 'styled-components' import styled from 'styled-components'
import type { T, Device } from 'types/common' 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 Box from 'components/base/Box'
import Text from 'components/base/Text' 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 { 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 type { StepId } from 'components/modals/UpdateFirmware'
import getLatestFirmwareForDevice from 'commands/getLatestFirmwareForDevice' import getLatestFirmwareForDevice from 'commands/getLatestFirmwareForDevice'
@ -19,7 +19,6 @@ import installFinalFirmware from 'commands/installFinalFirmware'
import installMcu from 'commands/installMcu' import installMcu from 'commands/installMcu'
import DisclaimerModal from 'components/modals/UpdateFirmware/Disclaimer' import DisclaimerModal from 'components/modals/UpdateFirmware/Disclaimer'
import UpdateModal from 'components/modals/UpdateFirmware' import UpdateModal from 'components/modals/UpdateFirmware'
import type { DeviceInfo } from 'helpers/devices/getDeviceInfo'
import Tooltip from 'components/base/Tooltip' import Tooltip from 'components/base/Tooltip'
import Box, { Card } from 'components/base/Box' import Box, { Card } from 'components/base/Box'
@ -43,7 +42,7 @@ type Props = {
} }
type State = { type State = {
latestFirmware: ?LedgerScriptParams & ?{ shouldFlashMcu: boolean }, latestFirmware: ?OsuFirmware & ?{ shouldFlashMcu: boolean },
modal: ModalStatus, modal: ModalStatus,
stepId: ?StepId, stepId: ?StepId,
shouldFlash: boolean, shouldFlash: boolean,

2
src/components/ManagerPage/index.js

@ -6,7 +6,7 @@ import { openURL } from 'helpers/linking'
import { urls } from 'config/urls' import { urls } from 'config/urls'
import type { Device } from 'types/common' import type { Device } from 'types/common'
import type { DeviceInfo } from 'helpers/devices/getDeviceInfo' import type { DeviceInfo } from 'helpers/types'
import Dashboard from './Dashboard' 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 { StepProps as DefaultStepProps, Step } from 'components/base/Stepper'
import type { ModalStatus } from 'components/ManagerPage/FirmwareUpdate' 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 { FreezeDeviceChangeEvents } from '../../ManagerPage/HookDeviceChange'
import StepFullFirmwareInstall from './steps/01-step-install-full-firmware' import StepFullFirmwareInstall from './steps/01-step-install-full-firmware'
@ -56,7 +56,7 @@ const createSteps = ({ t, shouldFlashMcu }: { t: T, shouldFlashMcu: boolean }):
return steps return steps
} }
export type Firmware = LedgerScriptParams & { shouldFlashMcu: boolean } export type Firmware = OsuFirmware & { shouldFlashMcu: boolean }
export type StepProps = DefaultStepProps & { export type StepProps = DefaultStepProps & {
firmware: Firmware, firmware: Firmware,

14
src/helpers/apps/installApp.js

@ -1,13 +1,12 @@
// @flow // @flow
import qs from 'qs'
import type Transport from '@ledgerhq/hw-transport' import type Transport from '@ledgerhq/hw-transport'
import { BASE_SOCKET_URL } from 'config/constants'
import { createDeviceSocket } from 'helpers/socket' import { createDeviceSocket } from 'helpers/socket'
import type { LedgerScriptParams } from 'helpers/types' import type { ApplicationVersion } from 'helpers/types'
import { createCustomErrorClass } from 'helpers/errors' import { createCustomErrorClass } from 'helpers/errors'
import { WS_INSTALL } from 'helpers/urls'
const ManagerNotEnoughSpaceError = createCustomErrorClass('ManagerNotEnoughSpace') const ManagerNotEnoughSpaceError = createCustomErrorClass('ManagerNotEnoughSpace')
const ManagerDeviceLockedError = createCustomErrorClass('ManagerDeviceLocked') const ManagerDeviceLockedError = createCustomErrorClass('ManagerDeviceLocked')
@ -37,8 +36,8 @@ function remapError(promise) {
export default async function installApp( export default async function installApp(
transport: Transport<*>, transport: Transport<*>,
targetId: string | number, targetId: string | number,
{ app }: { app: LedgerScriptParams }, { app }: { app: ApplicationVersion },
): Promise<*> { ): Promise<void> {
const params = { const params = {
targetId, targetId,
perso: app.perso, perso: app.perso,
@ -47,6 +46,7 @@ export default async function installApp(
firmwareKey: app.firmware_key, firmwareKey: app.firmware_key,
hash: app.hash, 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 // @flow
import network from 'api/network' 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 { APPLICATIONS_BY_DEVICE } from 'helpers/urls'
import getDeviceVersion from 'helpers/devices/getDeviceVersion' import getDeviceVersion from 'helpers/devices/getDeviceVersion'
import getCurrentFirmware from 'helpers/devices/getCurrentFirmware' import getCurrentFirmware from 'helpers/devices/getCurrentFirmware'
export default async (deviceInfo: DeviceInfo) => { type NetworkResponse = { data: { application_versions: Array<ApplicationVersion> } }
const deviceData = await getDeviceVersion(deviceInfo.targetId, deviceInfo.providerId)
const firmwareData = await getCurrentFirmware({ 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, deviceId: deviceData.id,
fullVersion: deviceInfo.fullVersion, fullVersion: deviceInfo.fullVersion,
provider: deviceInfo.providerId, provider: deviceInfo.providerId,
@ -20,6 +25,6 @@ export default async (deviceInfo: DeviceInfo) => {
} }
const { const {
data: { application_versions }, 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 : [] return application_versions.length > 0 ? application_versions : []
} }

3
src/helpers/apps/listApps.js

@ -2,8 +2,9 @@
import network from 'api/network' import network from 'api/network'
import { GET_APPLICATIONS } from 'helpers/urls' 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 }) const { data } = await network({ method: 'GET', url: GET_APPLICATIONS })
return data.length > 0 ? data : [] return data.length > 0 ? data : []
} }

5
src/helpers/apps/listCategories.js

@ -2,8 +2,9 @@
import network from 'api/network' import network from 'api/network'
import { GET_CATEGORIES } from 'helpers/urls' import { GET_CATEGORIES } from 'helpers/urls'
import type { Category } from 'helpers/types'
export default async () => { export default async (): Promise<Array<Category>> => {
const { data } = await network({ method: 'GET', url: GET_CATEGORIES }) const { data }: { data: Array<Category> } = await network({ method: 'GET', url: GET_CATEGORIES })
return data.length > 0 ? data : [] return data.length > 0 ? data : []
} }

15
src/helpers/apps/uninstallApp.js

@ -1,12 +1,11 @@
// @flow // @flow
import qs from 'qs'
import type Transport from '@ledgerhq/hw-transport' import type Transport from '@ledgerhq/hw-transport'
import { BASE_SOCKET_URL } from 'config/constants'
import { createDeviceSocket } from 'helpers/socket' import { createDeviceSocket } from 'helpers/socket'
import type { LedgerScriptParams } from 'helpers/types' import type { ApplicationVersion } from 'helpers/types'
import { createCustomErrorClass } from '../errors' import { createCustomErrorClass } from 'helpers/errors'
import { WS_INSTALL } from 'helpers/urls'
const ManagerDeviceLockedError = createCustomErrorClass('ManagerDeviceLocked') const ManagerDeviceLockedError = createCustomErrorClass('ManagerDeviceLocked')
const ManagerUninstallBTCDep = createCustomErrorClass('ManagerUninstallBTCDep') const ManagerUninstallBTCDep = createCustomErrorClass('ManagerUninstallBTCDep')
@ -30,8 +29,8 @@ function remapError(promise) {
export default async function uninstallApp( export default async function uninstallApp(
transport: Transport<*>, transport: Transport<*>,
targetId: string | number, targetId: string | number,
{ app }: { app: LedgerScriptParams }, { app }: { app: ApplicationVersion },
): Promise<*> { ): Promise<void> {
const params = { const params = {
targetId, targetId,
perso: app.perso, perso: app.perso,
@ -40,6 +39,6 @@ export default async function uninstallApp(
firmwareKey: app.delete_key, firmwareKey: app.delete_key,
hash: app.hash, hash: app.hash,
} }
const url = `${BASE_SOCKET_URL}/install?${qs.stringify(params)}` const url = WS_INSTALL(params)
return remapError(createDeviceSocket(transport, url).toPromise()) await remapError(createDeviceSocket(transport, url).toPromise())
} }

5
src/helpers/devices/getCurrentFirmware.js

@ -2,6 +2,7 @@
import network from 'api/network' import network from 'api/network'
import { GET_CURRENT_FIRMWARE } from 'helpers/urls' import { GET_CURRENT_FIRMWARE } from 'helpers/urls'
import type { FinalFirmware } from 'helpers/types'
type Input = { type Input = {
fullVersion: string, fullVersion: string,
@ -9,8 +10,8 @@ type Input = {
provider: number, provider: number,
} }
export default async (input: Input): Promise<*> => { export default async (input: Input): Promise<FinalFirmware> => {
const { data } = await network({ const { data }: { data: FinalFirmware } = await network({
method: 'POST', method: 'POST',
url: GET_CURRENT_FIRMWARE, url: GET_CURRENT_FIRMWARE,
data: { 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 getFirmwareInfo from 'helpers/firmware/getFirmwareInfo'
import { FORCE_PROVIDER } from 'config/constants' import { FORCE_PROVIDER } from 'config/constants'
export type DeviceInfo = { import type { DeviceInfo } from 'helpers/types'
targetId: string | number,
seVersion: string,
isBootloader: boolean,
flags: string,
mcuVersion: string,
isOSU: boolean,
providerName: string,
providerId: number,
fullVersion: string,
}
const PROVIDERS = { const PROVIDERS = {
'': 1, '': 1,

6
src/helpers/devices/getDeviceVersion.js

@ -2,8 +2,10 @@
import { GET_DEVICE_VERSION } from 'helpers/urls' import { GET_DEVICE_VERSION } from 'helpers/urls'
import network from 'api/network' import network from 'api/network'
export default async (targetId: string | number, provider: number): Promise<*> => { import type { DeviceVersion } from 'helpers/types'
const { data } = await network({
export default async (targetId: string | number, provider: number): Promise<DeviceVersion> => {
const { data }: { data: DeviceVersion } = await network({
method: 'POST', method: 'POST',
url: GET_DEVICE_VERSION, url: GET_DEVICE_VERSION,
data: { data: {

12
src/helpers/devices/getIsGenuine.js

@ -2,23 +2,29 @@
import type Transport from '@ledgerhq/hw-transport' import type Transport from '@ledgerhq/hw-transport'
import { SKIP_GENUINE } from 'config/constants' import { SKIP_GENUINE } from 'config/constants'
import { WS_GENUINE } from 'helpers/urls' 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 { createDeviceSocket } from 'helpers/socket'
import getCurrentFirmware from './getCurrentFirmware' import getCurrentFirmware from './getCurrentFirmware'
import getDeviceVersion from './getDeviceVersion' import getDeviceVersion from './getDeviceVersion'
export default async (transport: Transport<*>, deviceInfo: DeviceInfo): Promise<string> => { export default async (transport: Transport<*>, deviceInfo: DeviceInfo): Promise<string> => {
const deviceVersion = await getDeviceVersion(deviceInfo.targetId, deviceInfo.providerId) const deviceVersion: DeviceVersion = await getDeviceVersion(
const firmware = await getCurrentFirmware({ deviceInfo.targetId,
deviceInfo.providerId,
)
const firmware: FinalFirmware = await getCurrentFirmware({
deviceId: deviceVersion.id, deviceId: deviceVersion.id,
fullVersion: deviceInfo.fullVersion, fullVersion: deviceInfo.fullVersion,
provider: deviceInfo.providerId, provider: deviceInfo.providerId,
}) })
const params = { const params = {
targetId: deviceInfo.targetId, targetId: deviceInfo.targetId,
perso: firmware.perso, perso: firmware.perso,
} }
const url = WS_GENUINE(params) const url = WS_GENUINE(params)
return SKIP_GENUINE return SKIP_GENUINE
? new Promise(resolve => setTimeout(() => resolve('0000'), 1000)) ? new Promise(resolve => setTimeout(() => resolve('0000'), 1000))

36
src/helpers/devices/getLatestFirmwareForDevice.js

@ -1,7 +1,13 @@
// @flow // @flow
import network from 'api/network' import network from 'api/network'
import { GET_LATEST_FIRMWARE } from 'helpers/urls' 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 getFinalFirmwareById from 'helpers/firmware/getFinalFirmwareById'
import getMcus from 'helpers/firmware/getMcus' import getMcus from 'helpers/firmware/getMcus'
@ -9,19 +15,31 @@ import getMcus from 'helpers/firmware/getMcus'
import getCurrentFirmware from './getCurrentFirmware' import getCurrentFirmware from './getCurrentFirmware'
import getDeviceVersion from './getDeviceVersion' 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 // 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 // Get firmware infos with firmware name and device version
const seFirmwareVersion = await getCurrentFirmware({ const seFirmwareVersion: FinalFirmware = await getCurrentFirmware({
fullVersion: deviceInfo.fullVersion, fullVersion: deviceInfo.fullVersion,
deviceId: deviceVersion.id, deviceId: deviceVersion.id,
provider: deviceInfo.providerId, provider: deviceInfo.providerId,
}) })
// Fetch next possible firmware // Fetch next possible firmware
const { data } = await network({ const { data }: NetworkResponse = await network({
method: 'POST', method: 'POST',
url: GET_LATEST_FIRMWARE, url: GET_LATEST_FIRMWARE,
data: { data: {
@ -37,11 +55,13 @@ export default async (deviceInfo: DeviceInfo) => {
const { se_firmware_osu_version } = data const { se_firmware_osu_version } = data
const { next_se_firmware_final_version } = se_firmware_osu_version 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) .filter(mcu => mcu.name === deviceInfo.mcuVersion)
.map(mcu => mcu.id) .map(mcu => mcu.id)

2
src/helpers/devices/shouldFlashMcu.js

@ -1,7 +1,7 @@
// @flow // @flow
import network from 'api/network' import network from 'api/network'
import { GET_LATEST_FIRMWARE } from 'helpers/urls' 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 getFinalFirmwareById from 'helpers/firmware/getFinalFirmwareById'
import getMcus from 'helpers/firmware/getMcus' import getMcus from 'helpers/firmware/getMcus'

3
src/helpers/firmware/getFirmwareInfo.js

@ -1,6 +1,7 @@
// @flow // @flow
import type Transport from '@ledgerhq/hw-transport' import type Transport from '@ledgerhq/hw-transport'
import type { FirmwareInfo } from 'helpers/types'
const APDUS = { const APDUS = {
GET_FIRMWARE: [0xe0, 0x01, 0x00, 0x00], GET_FIRMWARE: [0xe0, 0x01, 0x00, 0x00],
@ -12,7 +13,7 @@ const APDUS = {
/** /**
* Retrieve targetId and firmware version from device * 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 res = await transport.send(...APDUS.GET_FIRMWARE)
const byteArray = [...res] const byteArray = [...res]
const data = byteArray.slice(0, byteArray.length - 2) 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 { GET_NEXT_MCU } from 'helpers/urls'
import { createCustomErrorClass } from 'helpers/errors' import { createCustomErrorClass } from 'helpers/errors'
import type { OsuFirmware } from 'helpers/types'
const LatestMCUInstalledError = createCustomErrorClass('LatestMCUInstalledError') const LatestMCUInstalledError = createCustomErrorClass('LatestMCUInstalledError')
type NetworkResponse = { data: OsuFirmware | 'default' }
export default async (bootloaderVersion: string): Promise<*> => { export default async (bootloaderVersion: string): Promise<*> => {
const { data } = await network({ const { data }: NetworkResponse = await network({
method: 'POST', method: 'POST',
url: GET_NEXT_MCU, url: GET_NEXT_MCU,
data: { data: {

10
src/helpers/firmware/installFinalFirmware.js

@ -1,6 +1,6 @@
// @flow // @flow
import type Transport from '@ledgerhq/hw-transport' 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 { WS_INSTALL } from 'helpers/urls'
import { createDeviceSocket } from 'helpers/socket' 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 => { export default async (transport: Transport<*>): Result => {
try { try {
const deviceInfo: DeviceInfo = await getDeviceInfo(transport) const deviceInfo: DeviceInfo = await getDeviceInfo(transport)
const device = await getDeviceVersion(deviceInfo.targetId, deviceInfo.providerId) const device: DeviceVersion = await getDeviceVersion(deviceInfo.targetId, deviceInfo.providerId)
const firmware = await getOsuFirmware({ const firmware: OsuFirmware = await getOsuFirmware({
deviceId: device.id, deviceId: device.id,
version: deviceInfo.fullVersion, version: deviceInfo.fullVersion,
provider: deviceInfo.providerId, provider: deviceInfo.providerId,
}) })
const { next_se_firmware_final_version } = firmware 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 = { const params = {
targetId: deviceInfo.targetId, 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 getDeviceInfo from 'helpers/devices/getDeviceInfo'
import { createCustomErrorClass } from 'helpers/errors' import { createCustomErrorClass } from 'helpers/errors'
import type { DeviceInfo } from 'helpers/types'
const ManagerDeviceLockedError = createCustomErrorClass('ManagerDeviceLocked') const ManagerDeviceLockedError = createCustomErrorClass('ManagerDeviceLocked')
function remapSocketError(promise) { function remapSocketError(promise) {
@ -20,16 +22,15 @@ function remapSocketError(promise) {
}) })
} }
type Result = Promise<*> type Result = Promise<void>
export default async (transport: Transport<*>): Result => { export default async (transport: Transport<*>): Result => {
const deviceInfo = await getDeviceInfo(transport) const { seVersion: version, targetId }: DeviceInfo = await getDeviceInfo(transport)
const { seVersion: version, targetId } = deviceInfo
const nextVersion = await getNextMCU(version) const nextVersion = await getNextMCU(version)
const params = { const params = {
targetId, targetId,
version: nextVersion.name, version: nextVersion.name,
} }
const url = WS_MCU(params) 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 ( export default async (
transport: Transport<*>, transport: Transport<*>,

132
src/helpers/types.js

@ -1,14 +1,134 @@
// @flow // @flow
type Id = number
export type LedgerScriptParams = { export type LedgerScriptParams = {
firmware?: string, firmware: string,
firmware_key?: string, firmwareKey: string,
delete?: string, delete?: string,
delete_key?: string, deleteKey?: string,
targetId?: string | number, 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, name: string,
version: string, version: string,
app: Id,
description: ?string,
display_name: string,
icon: string, icon: string,
app?: number, picture: Id,
hash?: string, notes: ?string,
perso?: 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