Browse Source

Merge pull request #685 from amougel/feature/getDeviceInfo

Refactoring
master
Gaëtan Renaudeau 7 years ago
committed by GitHub
parent
commit
0e354da916
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      src/commands/getCurrentFirmware.js
  2. 10
      src/commands/getDeviceInfo.js
  3. 12
      src/commands/getIsGenuine.js
  4. 8
      src/commands/getLatestFirmwareForDevice.js
  5. 8
      src/commands/installFinalFirmware.js
  6. 8
      src/commands/listApps.js
  7. 2
      src/components/EnsureDeviceApp.js
  8. 7
      src/components/ManagerPage/AppsList.js
  9. 5
      src/components/ManagerPage/Dashboard.js
  10. 12
      src/components/ManagerPage/FirmwareFinalUpdate.js
  11. 23
      src/components/ManagerPage/FirmwareUpdate.js
  12. 9
      src/components/ManagerPage/FlashMcu.js
  13. 10
      src/components/ManagerPage/index.js
  14. 7
      src/components/Workflow/EnsureDashboard.js
  15. 17
      src/components/Workflow/EnsureGenuine.js
  16. 14
      src/components/Workflow/index.js
  17. 11
      src/helpers/apps/listApps.js
  18. 22
      src/helpers/common.js
  19. 8
      src/helpers/devices/getCurrentFirmware.js
  20. 70
      src/helpers/devices/getDeviceInfo.js
  21. 3
      src/helpers/devices/getDeviceVersion.js
  22. 18
      src/helpers/devices/getIsGenuine.js
  23. 20
      src/helpers/devices/getLatestFirmwareForDevice.js
  24. 2
      src/helpers/devices/getOsuFirmware.js
  25. 4
      src/helpers/devices/isDashboardOpen.js
  26. 14
      src/helpers/firmware/installFinalFirmware.js
  27. 5
      src/helpers/urls.js

3
src/commands/getCurrentFirmware.js

@ -7,7 +7,8 @@ import getCurrentFirmware from 'helpers/devices/getCurrentFirmware'
type Input = { type Input = {
deviceId: string | number, deviceId: string | number,
version: string, fullVersion: string,
provider: number,
} }
type Result = * type Result = *

10
src/commands/getDeviceInfo.js

@ -5,19 +5,13 @@ 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'
type Input = { type Input = {
devicePath: string, devicePath: string,
} }
type Result = { const cmd: Command<Input, DeviceInfo> = createCommand('getDeviceInfo', ({ devicePath }) =>
targetId: number | string,
version: string,
final: boolean,
mcu: boolean,
}
const cmd: Command<Input, Result> = createCommand('getDeviceInfo', ({ devicePath }) =>
fromPromise(withDevice(devicePath)(transport => getDeviceInfo(transport))), fromPromise(withDevice(devicePath)(transport => getDeviceInfo(transport))),
) )

12
src/commands/getIsGenuine.js

@ -2,23 +2,19 @@
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 getIsGenuine from 'helpers/devices/getIsGenuine' import getIsGenuine from 'helpers/devices/getIsGenuine'
import { withDevice } from 'helpers/deviceAccess' import { withDevice } from 'helpers/deviceAccess'
type Input = { type Input = {
devicePath: string, devicePath: string,
targetId: string | number, deviceInfo: DeviceInfo,
version: string,
} }
type Result = string type Result = string
const cmd: Command<Input, Result> = createCommand( const cmd: Command<Input, Result> = createCommand('getIsGenuine', ({ devicePath, deviceInfo }) =>
'getIsGenuine', fromPromise(withDevice(devicePath)(transport => getIsGenuine(transport, deviceInfo))),
({ devicePath, targetId, version }) =>
fromPromise(
withDevice(devicePath)(transport => getIsGenuine(transport, { targetId, version })),
),
) )
export default cmd export default cmd

8
src/commands/getLatestFirmwareForDevice.js

@ -2,17 +2,13 @@
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 getLatestFirmwareForDevice from '../helpers/devices/getLatestFirmwareForDevice' import getLatestFirmwareForDevice from '../helpers/devices/getLatestFirmwareForDevice'
type Input = {
targetId: string | number,
version: string,
}
type Result = * type Result = *
const cmd: Command<Input, Result> = createCommand('getLatestFirmwareForDevice', data => const cmd: Command<DeviceInfo, Result> = createCommand('getLatestFirmwareForDevice', data =>
fromPromise(getLatestFirmwareForDevice(data)), fromPromise(getLatestFirmwareForDevice(data)),
) )

8
src/commands/installFinalFirmware.js

@ -3,13 +3,13 @@
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 { withDevice } from 'helpers/deviceAccess' import { withDevice } from 'helpers/deviceAccess'
import type { DeviceInfo } from 'helpers/devices/getDeviceInfo'
import installFinalFirmware from 'helpers/firmware/installFinalFirmware' import installFinalFirmware from 'helpers/firmware/installFinalFirmware'
type Input = { type Input = {
devicePath: string, devicePath: string,
targetId: string | number, deviceInfo: DeviceInfo,
version: string,
} }
type Result = { type Result = {
@ -18,8 +18,8 @@ type Result = {
const cmd: Command<Input, Result> = createCommand( const cmd: Command<Input, Result> = createCommand(
'installFinalFirmware', 'installFinalFirmware',
({ devicePath, ...rest }) => ({ devicePath, deviceInfo }) =>
fromPromise(withDevice(devicePath)(transport => installFinalFirmware(transport, { ...rest }))), fromPromise(withDevice(devicePath)(transport => installFinalFirmware(transport, deviceInfo))),
) )
export default cmd export default cmd

8
src/commands/listApps.js

@ -7,13 +7,15 @@ import listApps from 'helpers/apps/listApps'
type Input = { type Input = {
targetId: string | number, targetId: string | number,
version: string, fullVersion: string,
provider: number,
} }
type Result = * type Result = *
const cmd: Command<Input, Result> = createCommand('listApps', ({ targetId, version }) => const cmd: Command<Input, Result> = createCommand(
fromPromise(listApps(targetId, version)), 'listApps',
({ targetId, fullVersion, provider }) => fromPromise(listApps(targetId, fullVersion, provider)),
) )
export default cmd export default cmd

2
src/components/EnsureDeviceApp.js

@ -159,7 +159,7 @@ class EnsureDeviceApp extends PureComponent<Props, State> {
} }
} else { } else {
logger.warn('EnsureDeviceApp for using dashboard is DEPRECATED !!!') logger.warn('EnsureDeviceApp for using dashboard is DEPRECATED !!!')
// FIXME REMOVE THIS ! should use EnsureDashboard dedicated component. // TODO: FIXME REMOVE THIS ! should use EnsureDashboard dedicated component.
const isDashboard = isDashboardOpen.send({ devicePath: deviceSelected.path }).toPromise() const isDashboard = isDashboardOpen.send({ devicePath: deviceSelected.path }).toPromise()
if (!isDashboard) { if (!isDashboard) {

7
src/components/ManagerPage/AppsList.js

@ -48,7 +48,8 @@ type Props = {
device: Device, device: Device,
targetId: string | number, targetId: string | number,
t: T, t: T,
version: string, fullVersion: string,
provider: number,
} }
type State = { type State = {
@ -82,8 +83,8 @@ class AppsList extends PureComponent<Props, State> {
async fetchAppList() { async fetchAppList() {
try { try {
const { targetId, version } = this.props const { targetId, fullVersion, provider } = this.props
const appsList = await listApps.send({ targetId, version }).toPromise() const appsList = await listApps.send({ targetId, fullVersion, provider }).toPromise()
if (!this._unmounted) { if (!this._unmounted) {
this.setState({ appsList, status: 'idle', appsLoaded: true }) this.setState({ appsList, status: 'idle', appsLoaded: true })
} }

5
src/components/ManagerPage/Dashboard.js

@ -34,10 +34,7 @@ const Dashboard = ({ device, deviceInfo, t }: Props) => (
</Text> </Text>
</Box> </Box>
<Box mt={5}> <Box mt={5}>
<FirmwareUpdate <FirmwareUpdate deviceInfo={deviceInfo} device={device} />
infos={{ targetId: deviceInfo.targetId, version: deviceInfo.version }}
device={device}
/>
</Box> </Box>
<Box mt={5}> <Box mt={5}>
<AppsList device={device} targetId={deviceInfo.targetId} version={deviceInfo.version} /> <AppsList device={device} targetId={deviceInfo.targetId} version={deviceInfo.version} />

12
src/components/ManagerPage/FirmwareFinalUpdate.js

@ -3,6 +3,7 @@
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import { translate } from 'react-i18next' import { translate } from 'react-i18next'
import logger from 'logger' import logger from 'logger'
import type { DeviceInfo } from 'helpers/devices/getDeviceInfo'
import type { Device, T } from 'types/common' import type { Device, T } from 'types/common'
@ -11,15 +12,10 @@ import installFinalFirmware from 'commands/installFinalFirmware'
import Box, { Card } from 'components/base/Box' import Box, { Card } from 'components/base/Box'
// import Button from 'components/base/Button' // import Button from 'components/base/Button'
type DeviceInfos = {
targetId: number,
version: string,
}
type Props = { type Props = {
t: T, t: T,
device: Device, device: Device,
infos: DeviceInfos, deviceInfo: DeviceInfo,
} }
type State = {} type State = {}
@ -35,9 +31,9 @@ class FirmwareFinalUpdate extends PureComponent<Props, State> {
installFinalFirmware = async () => { installFinalFirmware = async () => {
try { try {
const { device, infos } = this.props const { device, deviceInfo } = this.props
const { success } = await installFinalFirmware const { success } = await installFinalFirmware
.send({ devicePath: device.path, targetId: infos.targetId, version: infos.version }) .send({ devicePath: device.path, deviceInfo })
.toPromise() .toPromise()
if (success) { if (success) {
this.setState() this.setState()

23
src/components/ManagerPage/FirmwareUpdate.js

@ -14,6 +14,7 @@ import type { LedgerScriptParams } from 'helpers/common'
import getLatestFirmwareForDevice from 'commands/getLatestFirmwareForDevice' import getLatestFirmwareForDevice from 'commands/getLatestFirmwareForDevice'
import installOsuFirmware from 'commands/installOsuFirmware' import installOsuFirmware from 'commands/installOsuFirmware'
import type { DeviceInfo } from 'helpers/devices/getDeviceInfo'
import Box, { Card } from 'components/base/Box' import Box, { Card } from 'components/base/Box'
import Text from 'components/base/Text' import Text from 'components/base/Text'
@ -32,17 +33,12 @@ let CACHED_LATEST_FIRMWARE = null
export const getCleanVersion = (input: string): string => export const getCleanVersion = (input: string): string =>
input.endsWith('-osu') ? input.replace('-osu', '') : input input.endsWith('-osu') ? input.replace('-osu', '') : input
type DeviceInfos = {
targetId: number | string,
version: string,
}
type ModalStatus = 'closed' | 'disclaimer' | 'installing' | 'error' | 'success' type ModalStatus = 'closed' | 'disclaimer' | 'installing' | 'error' | 'success'
type Props = { type Props = {
t: T, t: T,
device: Device, device: Device,
infos: DeviceInfos, deviceInfo: DeviceInfo,
} }
type State = { type State = {
@ -73,12 +69,9 @@ class FirmwareUpdate extends PureComponent<Props, State> {
_unmounting = false _unmounting = false
fetchLatestFirmware = async () => { fetchLatestFirmware = async () => {
const { infos } = this.props const { deviceInfo } = this.props
const latestFirmware = const latestFirmware =
CACHED_LATEST_FIRMWARE || CACHED_LATEST_FIRMWARE || (await getLatestFirmwareForDevice.send(deviceInfo).toPromise())
(await getLatestFirmwareForDevice
.send({ targetId: infos.targetId, version: infos.version })
.toPromise())
if ( if (
!isEmpty(latestFirmware) && !isEmpty(latestFirmware) &&
!isEqual(this.state.latestFirmware, latestFirmware) && !isEqual(this.state.latestFirmware, latestFirmware) &&
@ -92,14 +85,14 @@ class FirmwareUpdate extends PureComponent<Props, State> {
installFirmware = async () => { installFirmware = async () => {
try { try {
const { latestFirmware } = this.state const { latestFirmware } = this.state
const { infos } = this.props const { deviceInfo } = this.props
invariant(latestFirmware, 'did not find a new firmware or firmware is not set') invariant(latestFirmware, 'did not find a new firmware or firmware is not set')
const { const {
device: { path: devicePath }, device: { path: devicePath },
} = this.props } = this.props
this.setState({ modal: 'installing' }) this.setState({ modal: 'installing' })
const { success } = await installOsuFirmware const { success } = await installOsuFirmware
.send({ devicePath, firmware: latestFirmware, targetId: infos.targetId }) .send({ devicePath, firmware: latestFirmware, targetId: deviceInfo.targetId })
.toPromise() .toPromise()
if (success) { if (success) {
this.fetchLatestFirmware() this.fetchLatestFirmware()
@ -150,7 +143,7 @@ class FirmwareUpdate extends PureComponent<Props, State> {
} }
render() { render() {
const { infos, t } = this.props const { deviceInfo, t } = this.props
const { latestFirmware, modal } = this.state const { latestFirmware, modal } = this.state
return ( return (
@ -170,7 +163,7 @@ class FirmwareUpdate extends PureComponent<Props, State> {
</Box> </Box>
<Text ff="Open Sans|SemiBold" fontSize={2}> <Text ff="Open Sans|SemiBold" fontSize={2}>
{t('app:manager.firmware.installed', { {t('app:manager.firmware.installed', {
version: infos.version, version: deviceInfo.fullVersion,
})} })}
</Text> </Text>
</Box> </Box>

9
src/components/ManagerPage/FlashMcu.js

@ -4,12 +4,7 @@ import React, { PureComponent } from 'react'
import type { Device } from 'types/common' import type { Device } from 'types/common'
import installMcu from 'commands/installMcu' import installMcu from 'commands/installMcu'
type DeviceInfo = { import type { DeviceInfo } from 'helpers/devices/getDeviceInfo'
targetId: number | string,
version: string,
final: boolean,
mcu: boolean,
}
type Props = { type Props = {
device: Device, device: Device,
@ -35,7 +30,7 @@ class FlashMcu extends PureComponent<Props, State> {
.send({ .send({
devicePath: device.path, devicePath: device.path,
targetId: deviceInfo.targetId, targetId: deviceInfo.targetId,
version: deviceInfo.version, version: deviceInfo.seVersion,
}) })
.toPromise() .toPromise()
this.setState({ flashing: false }) this.setState({ flashing: false })

10
src/components/ManagerPage/index.js

@ -4,19 +4,13 @@
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import type { Device } from 'types/common' import type { Device } from 'types/common'
import type { DeviceInfo } from 'helpers/devices/getDeviceInfo'
import Workflow from 'components/Workflow' import Workflow from 'components/Workflow'
import WorkflowWithIcon from 'components/Workflow/WorkflowWithIcon' import WorkflowWithIcon from 'components/Workflow/WorkflowWithIcon'
import Dashboard from './Dashboard' import Dashboard from './Dashboard'
import FlashMcu from './FlashMcu' import FlashMcu from './FlashMcu'
type DeviceInfo = {
targetId: number | string,
version: string,
final: boolean,
mcu: boolean,
}
type Error = { type Error = {
message: string, message: string,
stack: string, stack: string,
@ -27,7 +21,7 @@ class ManagerPage extends PureComponent<*, *> {
return ( return (
<Workflow <Workflow
renderFinalUpdate={(device: Device, deviceInfo: DeviceInfo) => ( renderFinalUpdate={(device: Device, deviceInfo: DeviceInfo) => (
<p>UPDATE FINAL FIRMARE (TEMPLATE + ACTION WIP) {deviceInfo.final}</p> <p>UPDATE FINAL FIRMARE (TEMPLATE + ACTION WIP) {deviceInfo.isOSU}</p>
)} )}
renderMcuUpdate={(device: Device, deviceInfo: DeviceInfo) => ( renderMcuUpdate={(device: Device, deviceInfo: DeviceInfo) => (
<FlashMcu device={device} deviceInfo={deviceInfo} /> <FlashMcu device={device} deviceInfo={deviceInfo} />

7
src/components/Workflow/EnsureDashboard.js

@ -7,12 +7,7 @@ import type { Device } from 'types/common'
import getDeviceInfo from 'commands/getDeviceInfo' import getDeviceInfo from 'commands/getDeviceInfo'
type DeviceInfo = { import type { DeviceInfo } from 'helpers/devices/getDeviceInfo'
targetId: number | string,
version: string,
final: boolean,
mcu: boolean,
}
type Error = { type Error = {
message: string, message: string,

17
src/components/Workflow/EnsureGenuine.js

@ -5,6 +5,7 @@ import isEqual from 'lodash/isEqual'
import { GENUINE_TIMEOUT } from 'config/constants' import { GENUINE_TIMEOUT } from 'config/constants'
import type { Device } from 'types/common' import type { Device } from 'types/common'
import type { DeviceInfo } from 'helpers/devices/getDeviceInfo'
import getIsGenuine from 'commands/getIsGenuine' import getIsGenuine from 'commands/getIsGenuine'
@ -13,14 +14,9 @@ type Error = {
stack: string, stack: string,
} }
type DeviceInfos = {
targetId: number | string,
version: string,
}
type Props = { type Props = {
device: ?Device, device: ?Device,
infos: ?DeviceInfos, deviceInfo: ?DeviceInfo,
children: (isGenuine: ?boolean, error: ?Error) => *, children: (isGenuine: ?boolean, error: ?Error) => *,
} }
@ -56,12 +52,15 @@ class EnsureGenuine extends PureComponent<Props, State> {
_unmounting = false _unmounting = false
async checkIsGenuine() { async checkIsGenuine() {
const { device, infos } = this.props const { device, deviceInfo } = this.props
if (device && infos && !this._checking) { if (device && deviceInfo && !this._checking) {
this._checking = true this._checking = true
try { try {
const res = await getIsGenuine const res = await getIsGenuine
.send({ devicePath: device.path, targetId: infos.targetId, version: infos.version }) .send({
devicePath: device.path,
deviceInfo,
})
.pipe(timeout(GENUINE_TIMEOUT)) .pipe(timeout(GENUINE_TIMEOUT))
.toPromise() .toPromise()
if (this._unmounting) return if (this._unmounting) return

14
src/components/Workflow/index.js

@ -1,6 +1,7 @@
// @flow // @flow
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import type { DeviceInfo } from 'helpers/devices/getDeviceInfo'
import type { Node } from 'react' import type { Node } from 'react'
import type { Device } from 'types/common' import type { Device } from 'types/common'
@ -9,13 +10,6 @@ import EnsureDevice from './EnsureDevice'
import EnsureDashboard from './EnsureDashboard' import EnsureDashboard from './EnsureDashboard'
import EnsureGenuine from './EnsureGenuine' import EnsureGenuine from './EnsureGenuine'
type DeviceInfo = {
targetId: number | string,
version: string,
final: boolean,
mcu: boolean,
}
type Error = { type Error = {
message: string, message: string,
stack: string, stack: string,
@ -55,16 +49,16 @@ class Workflow extends PureComponent<Props, State> {
{(device: Device) => ( {(device: Device) => (
<EnsureDashboard device={device}> <EnsureDashboard device={device}>
{(deviceInfo: ?DeviceInfo, dashboardError: ?Error) => { {(deviceInfo: ?DeviceInfo, dashboardError: ?Error) => {
if (deviceInfo && deviceInfo.mcu && renderMcuUpdate) { if (deviceInfo && deviceInfo.isBootloader && renderMcuUpdate) {
return renderMcuUpdate(device, deviceInfo) return renderMcuUpdate(device, deviceInfo)
} }
if (deviceInfo && deviceInfo.final && renderFinalUpdate) { if (deviceInfo && deviceInfo.isOSU && renderFinalUpdate) {
return renderFinalUpdate(device, deviceInfo) return renderFinalUpdate(device, deviceInfo)
} }
return ( return (
<EnsureGenuine device={device} infos={deviceInfo}> <EnsureGenuine device={device} deviceInfo={deviceInfo}>
{(isGenuine: ?boolean, genuineError: ?Error) => { {(isGenuine: ?boolean, genuineError: ?Error) => {
if (dashboardError || genuineError) { if (dashboardError || genuineError) {
return renderError return renderError

11
src/helpers/apps/listApps.js

@ -5,11 +5,14 @@ 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 (targetId: string | number, version: string) => { export default async (targetId: string | number, fullVersion: string, provider: number) => {
try { try {
const provider = 1 const deviceData = await getDeviceVersion(targetId, provider)
const deviceData = await getDeviceVersion(targetId) const firmwareData = await getCurrentFirmware({
const firmwareData = await getCurrentFirmware({ deviceId: deviceData.id, version }) deviceId: deviceData.id,
fullVersion,
provider,
})
const params = { const params = {
provider, provider,
current_se_firmware_final_version: firmwareData.id, current_se_firmware_final_version: firmwareData.id,

22
src/helpers/common.js

@ -32,9 +32,25 @@ export async function getFirmwareInfo(transport: Transport<*>) {
const data = byteArray.slice(0, byteArray.length - 2) const data = byteArray.slice(0, byteArray.length - 2)
const targetIdStr = Buffer.from(data.slice(0, 4)) const targetIdStr = Buffer.from(data.slice(0, 4))
const targetId = targetIdStr.readUIntBE(0, 4) const targetId = targetIdStr.readUIntBE(0, 4)
const versionLength = data[4] const seVersionLength = data[4]
const version = Buffer.from(data.slice(5, 5 + versionLength)).toString() const seVersion = Buffer.from(data.slice(5, 5 + seVersionLength)).toString()
return { targetId, version } const flagsLength = data[5 + seVersionLength]
const flags = Buffer.from(
data.slice(5 + seVersionLength + 1, 5 + seVersionLength + 1 + flagsLength),
).toString()
const mcuVersionLength = data[5 + seVersionLength + 1 + flagsLength]
let mcuVersion = Buffer.from(
data.slice(
7 + seVersionLength + flagsLength,
7 + seVersionLength + flagsLength + mcuVersionLength,
),
)
if (mcuVersion[mcuVersion.length - 1] === 0) {
mcuVersion = mcuVersion.slice(0, mcuVersion.length - 1)
}
mcuVersion = mcuVersion.toString()
return { targetId, seVersion, flags, mcuVersion }
} catch (err) { } catch (err) {
const error = new Error(err.message) const error = new Error(err.message)
error.stack = err.stack error.stack = err.stack

8
src/helpers/devices/getCurrentFirmware.js

@ -4,21 +4,21 @@ import network from 'api/network'
import { GET_CURRENT_FIRMWARE } from 'helpers/urls' import { GET_CURRENT_FIRMWARE } from 'helpers/urls'
type Input = { type Input = {
version: string, fullVersion: string,
deviceId: string | number, deviceId: string | number,
provider: number,
} }
let error let error
export default async (input: Input): Promise<*> => { export default async (input: Input): Promise<*> => {
try { try {
const provider = 1
const { data } = await network({ const { data } = await network({
method: 'POST', method: 'POST',
url: GET_CURRENT_FIRMWARE, url: GET_CURRENT_FIRMWARE,
data: { data: {
device_version: input.deviceId, device_version: input.deviceId,
version_name: input.version, version_name: input.fullVersion,
provider, provider: input.provider,
}, },
}) })
return data return data

70
src/helpers/devices/getDeviceInfo.js

@ -4,23 +4,65 @@ import type Transport from '@ledgerhq/hw-transport'
import { getFirmwareInfo } from 'helpers/common' import { getFirmwareInfo } from 'helpers/common'
type Result = { export type DeviceInfo = {
targetId: string | number, targetId: string | number,
version: string, seVersion: string,
mcu: boolean, isBootloader: boolean,
final: boolean, flags: string,
mcuVersion: string,
isOSU: boolean,
providerName: string,
providerId: number,
fullVersion: string,
} }
export default async (transport: Transport<*>): Promise<Result> => { // prettier-ignore
try { const DETECT_CLUBCOIN = [
const { targetId, version } = await getFirmwareInfo(transport) [0xe0, 0x04, 0x00, 0x00, Buffer.from([0x31, 0x10, 0x00, 0x02])],
const finalReady = version.endsWith('-osu') [0xe0, 0x50, 0x00, 0x00, Buffer.from([0xe4, 0x6c, 0x4c, 0x71, 0x8b, 0xc8, 0x7f, 0xb7])],
const mcuReady = targetId === 0x01000001 [0xe0, 0x51, 0x80, 0x00, Buffer.from([0x41, 0x04, 0xc9, 0x8c, 0xa0, 0x99, 0x53, 0x47, 0x2b, 0x36, 0x06, 0x1e, 0x0e, 0x40, 0xc9, 0x3d, 0x50, 0x52, 0x34, 0x09, 0x0e, 0xfd, 0x74, 0xf1, 0xd7, 0xa2, 0x93, 0xe8, 0x28, 0x15, 0x9a, 0x97, 0x71, 0x1b, 0x33, 0xd1, 0x8a, 0xfc, 0x17, 0xad, 0x15, 0x6e, 0xae, 0xd9, 0x9c, 0xf4, 0x3b, 0x20, 0xe1, 0x5d, 0x64, 0xaf, 0x39, 0xa5, 0x51, 0x3b, 0x4e, 0x3c, 0x5f, 0x43, 0x17, 0xe6, 0x42, 0x70, 0x2f, 0x05, 0x47, 0x30, 0x45, 0x02, 0x21, 0x00, 0xf1, 0xd2, 0xb8, 0x34, 0x99, 0x4a, 0x0c, 0x1f, 0x25, 0xea, 0x20, 0xcf, 0x33, 0xe3, 0x2b, 0xd0, 0x6b, 0xcf, 0x7c, 0x42, 0x4a, 0x02, 0xee, 0xe8, 0xf6, 0x96, 0x99, 0x20, 0xe1, 0xe8, 0xc2, 0xb3, 0x02, 0x20, 0x63, 0x2d, 0x19, 0xbd, 0x30, 0xab, 0x20, 0x76, 0x18, 0x78, 0x78, 0xae, 0xaa, 0x0f, 0x4d, 0x48, 0x04, 0x01, 0x32, 0x79, 0xd0, 0x16, 0xde, 0xca, 0x66, 0x93, 0xf3, 0x7b, 0x4e, 0x50, 0x7f, 0x43])],
]
return { targetId, version, final: finalReady, mcu: mcuReady } const PROVIDERS = {
} catch (err) { '': 1,
const error = Error(err.message) das: 2,
error.stack = err.stack club: 3,
throw error shitcoins: 4,
}
export default async (transport: Transport<*>): Promise<DeviceInfo> => {
const res = await getFirmwareInfo(transport)
let { seVersion } = res
const { targetId, mcuVersion, flags } = res
if (seVersion === '1.2') {
try {
for (let i = 0; i < DETECT_CLUBCOIN.length; i++) {
const instructions = DETECT_CLUBCOIN[i]
await transport.send(...instructions)
}
seVersion = '1.2.0-club'
} catch (e) {
seVersion = '1.2.0'
}
}
const parsedVersion =
seVersion.match(/([0-9]+.[0-9])+(.[0-9]+)?((?!-osu)-([a-z]+))?(-osu)?/) || []
const isOSU = typeof parsedVersion[5] !== 'undefined'
const providerName = parsedVersion[4] || ''
const providerId = PROVIDERS[providerName]
const isBootloader = targetId === 0x01000001
const majMin = parsedVersion[1]
const patch = parsedVersion[2] || '.0'
const fullVersion = `${majMin}${patch}${providerName ? `-${providerName}` : ''}`
return {
targetId,
seVersion: majMin + patch,
isOSU,
mcuVersion,
isBootloader,
providerName,
providerId,
flags,
fullVersion,
} }
} }

3
src/helpers/devices/getDeviceVersion.js

@ -2,8 +2,7 @@
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): Promise<*> => { export default async (targetId: string | number, provider: number): Promise<*> => {
const provider = 1
const { data } = await network({ const { data } = await network({
method: 'POST', method: 'POST',
url: GET_DEVICE_VERSION, url: GET_DEVICE_VERSION,

18
src/helpers/devices/getIsGenuine.js

@ -2,21 +2,21 @@
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 { 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 ( export default async (transport: Transport<*>, deviceInfo: DeviceInfo): Promise<string> => {
transport: Transport<*>, const deviceVersion = await getDeviceVersion(deviceInfo.targetId, deviceInfo.providerId)
app: { targetId: string | number, version: string }, const firmware = await getCurrentFirmware({
): Promise<string> => { deviceId: deviceVersion.id,
const { targetId, version } = app fullVersion: deviceInfo.fullVersion,
const device = await getDeviceVersion(app.targetId) provider: deviceInfo.providerId,
const firmware = await getCurrentFirmware({ deviceId: device.id, version }) })
const params = { const params = {
targetId, targetId: deviceInfo.targetId,
version,
perso: firmware.perso, perso: firmware.perso,
} }
const url = WS_GENUINE(params) const url = WS_GENUINE(params)

20
src/helpers/devices/getLatestFirmwareForDevice.js

@ -1,24 +1,22 @@
// @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 getCurrentFirmware from './getCurrentFirmware' import getCurrentFirmware from './getCurrentFirmware'
import getDeviceVersion from './getDeviceVersion' import getDeviceVersion from './getDeviceVersion'
type Input = { export default async (deviceInfo: DeviceInfo) => {
version: string,
targetId: string | number,
}
export default async (input: Input) => {
try { try {
const provider = 1
const { targetId, version } = input
// Get device infos from targetId // Get device infos from targetId
const deviceVersion = await getDeviceVersion(targetId) const 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({ version, deviceId: deviceVersion.id }) const seFirmwareVersion = await getCurrentFirmware({
fullVersion: deviceInfo.fullVersion,
deviceId: deviceVersion.id,
provider: deviceInfo.providerId,
})
// Fetch next possible firmware // Fetch next possible firmware
const { data } = await network({ const { data } = await network({
@ -27,7 +25,7 @@ export default async (input: Input) => {
data: { data: {
current_se_firmware_final_version: seFirmwareVersion.id, current_se_firmware_final_version: seFirmwareVersion.id,
device_version: deviceVersion.id, device_version: deviceVersion.id,
provider, provider: deviceInfo.providerId,
}, },
}) })

2
src/helpers/devices/getOsuFirmware.js

@ -15,7 +15,7 @@ export default async (input: Input): Promise<*> => {
url: GET_CURRENT_OSU, url: GET_CURRENT_OSU,
data: { data: {
device_version: input.deviceId, device_version: input.deviceId,
version_name: input.version, version_name: `${input.version}-osu`,
provider, provider,
}, },
}) })

4
src/helpers/devices/isDashboardOpen.js

@ -8,8 +8,8 @@ type Result = boolean
export default async (transport: Transport<*>): Promise<Result> => { export default async (transport: Transport<*>): Promise<Result> => {
try { try {
const { targetId, version } = await getFirmwareInfo(transport) const { targetId, seVersion } = await getFirmwareInfo(transport)
if (targetId && version) { if (targetId && seVersion) {
return true return true
} }

14
src/helpers/firmware/installFinalFirmware.js

@ -1,5 +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 { WS_INSTALL } from 'helpers/urls' import { WS_INSTALL } from 'helpers/urls'
import { createDeviceSocket } from 'helpers/socket' import { createDeviceSocket } from 'helpers/socket'
@ -7,22 +8,17 @@ import getDeviceVersion from 'helpers/devices/getDeviceVersion'
import getOsuFirmware from 'helpers/devices/getOsuFirmware' import getOsuFirmware from 'helpers/devices/getOsuFirmware'
import getFinalFirmwareById from './getFinalFirmwareById' import getFinalFirmwareById from './getFinalFirmwareById'
type Input = {
targetId: number | string,
version: string,
}
type Result = * type Result = *
export default async (transport: Transport<*>, app: Input): Result => { export default async (transport: Transport<*>, deviceInfo: DeviceInfo): Result => {
try { try {
const { targetId, version } = app const device = await getDeviceVersion(deviceInfo.targetId, deviceInfo.providerId)
const device = await getDeviceVersion(targetId) const firmware = await getOsuFirmware({ deviceId: device.id, version: deviceInfo.fullVersion })
const firmware = await getOsuFirmware({ deviceId: device.id, version })
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 = await getFinalFirmwareById(next_se_firmware_final_version)
const params = { const params = {
targetId, targetId: deviceInfo.targetId,
...nextFirmware, ...nextFirmware,
firmwareKey: nextFirmware.firmware_key, firmwareKey: nextFirmware.firmware_key,
} }

5
src/helpers/urls.js

@ -23,7 +23,10 @@ export const GET_LATEST_FIRMWARE: string = managerUrlbuilder('get_latest_firmwar
export const GET_NEXT_MCU: string = managerUrlbuilder('mcu_versions_bootloader') export const GET_NEXT_MCU: string = managerUrlbuilder('mcu_versions_bootloader')
export const WS_INSTALL: (arg: LedgerScriptParams) => string = wsURLBuilder('install') export const WS_INSTALL: (arg: LedgerScriptParams) => string = wsURLBuilder('install')
export const WS_GENUINE: (arg: { targetId: string | number }) => string = wsURLBuilder('genuine') export const WS_GENUINE: (arg: {
targetId: string | number,
perso: string,
}) => string = wsURLBuilder('genuine')
export const WS_MCU: (arg: { targetId: string | number, version: string }) => string = wsURLBuilder( export const WS_MCU: (arg: { targetId: string | number, version: string }) => string = wsURLBuilder(
'mcu', 'mcu',
) )

Loading…
Cancel
Save