Browse Source

connected app un/install to new api

master
Valentin D. Pinkman 7 years ago
parent
commit
843675acdc
No known key found for this signature in database GPG Key ID: E7D110669FFB8D3E
  1. 9
      src/commands/installApp.js
  2. 5
      src/commands/listApps.js
  3. 9
      src/commands/uninstallApp.js
  4. 17
      src/components/ManagerPage/AppSearchBar.js
  5. 37
      src/components/ManagerPage/AppsList.js
  6. 6
      src/components/ManagerPage/Dashboard.js
  7. 4
      src/components/Workflow/index.js
  8. 12
      src/helpers/apps/installApp.js
  9. 35
      src/helpers/apps/listApps.js
  10. 14
      src/helpers/apps/uninstallApp.js
  11. 8
      src/helpers/common.js
  12. 4
      src/helpers/devices/getIsGenuine.js
  13. 4
      src/helpers/firmware/installFinalFirmware.js
  14. 4
      src/helpers/firmware/installMcu.js
  15. 4
      src/helpers/firmware/installOsuFirmware.js
  16. 18
      src/helpers/urls.js

9
src/commands/installApp.js

@ -9,14 +9,17 @@ import installApp from 'helpers/apps/installApp'
import type { LedgerScriptParams } from 'helpers/common' import type { LedgerScriptParams } from 'helpers/common'
type Input = { type Input = {
appParams: LedgerScriptParams, app: LedgerScriptParams,
devicePath: string, devicePath: string,
targetId: string | number,
} }
type Result = * type Result = *
const cmd: Command<Input, Result> = createCommand('installApp', ({ devicePath, ...rest }) => const cmd: Command<Input, Result> = createCommand(
fromPromise(withDevice(devicePath)(transport => installApp(transport, rest))), 'installApp',
({ devicePath, targetId, ...app }) =>
fromPromise(withDevice(devicePath)(transport => installApp(transport, targetId, app))),
) )
export default cmd export default cmd

5
src/commands/listApps.js

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

9
src/commands/uninstallApp.js

@ -9,14 +9,17 @@ import uninstallApp from 'helpers/apps/uninstallApp'
import type { LedgerScriptParams } from 'helpers/common' import type { LedgerScriptParams } from 'helpers/common'
type Input = { type Input = {
appParams: LedgerScriptParams, app: LedgerScriptParams,
devicePath: string, devicePath: string,
targetId: string | number,
} }
type Result = * type Result = *
const cmd: Command<Input, Result> = createCommand('uninstallApp', ({ devicePath, ...rest }) => const cmd: Command<Input, Result> = createCommand(
fromPromise(withDevice(devicePath)(transport => uninstallApp(transport, rest))), 'uninstallApp',
({ devicePath, targetId, ...rest }) =>
fromPromise(withDevice(devicePath)(transport => uninstallApp(transport, targetId, rest))),
) )
export default cmd export default cmd

17
src/components/ManagerPage/AppSearchBar.js

@ -4,6 +4,8 @@ import styled from 'styled-components'
import { color, fontSize, space } from 'styled-system' import { color, fontSize, space } from 'styled-system'
import fontFamily from 'styles/styled/fontFamily' import fontFamily from 'styles/styled/fontFamily'
import type { LedgerScriptParams } from 'helpers/common'
import { ff } from 'styles/helpers' import { ff } from 'styles/helpers'
import Box from 'components/base/Box' import Box from 'components/base/Box'
@ -12,20 +14,9 @@ import Search from 'components/base/Search'
import SearchIcon from 'icons/Search' import SearchIcon from 'icons/Search'
import CrossIcon from 'icons/Cross' import CrossIcon from 'icons/Cross'
type LedgerApp = {
name: string,
version: string,
icon: string,
app: Object,
bolos_version: {
min: number,
max: number,
},
}
type Props = { type Props = {
list: Array<LedgerApp>, list: Array<LedgerScriptParams>,
children: (list: Array<LedgerApp>) => React$Node, children: (list: Array<LedgerScriptParams>) => React$Node,
} }
type State = { type State = {

37
src/components/ManagerPage/AppsList.js

@ -6,6 +6,7 @@ import styled from 'styled-components'
import { translate } from 'react-i18next' import { translate } from 'react-i18next'
import type { Device, T } from 'types/common' import type { Device, T } from 'types/common'
import type { LedgerScriptParams } from 'helpers/common'
import listApps from 'commands/listApps' import listApps from 'commands/listApps'
import installApp from 'commands/installApp' import installApp from 'commands/installApp'
@ -43,27 +44,17 @@ const ICONS_FALLBACK = {
type Status = 'loading' | 'idle' | 'busy' | 'success' | 'error' type Status = 'loading' | 'idle' | 'busy' | 'success' | 'error'
type Mode = 'home' | 'installing' | 'uninstalling' type Mode = 'home' | 'installing' | 'uninstalling'
type LedgerApp = {
name: string,
version: string,
icon: string,
app: Object,
bolos_version: {
min: number,
max: number,
},
}
type Props = { type Props = {
device: Device, device: Device,
targetId: string | number, targetId: string | number,
t: T, t: T,
version: string,
} }
type State = { type State = {
status: Status, status: Status,
error: string | null, error: string | null,
appsList: LedgerApp[], appsList: LedgerScriptParams[] | Array<*>,
app: string, app: string,
mode: Mode, mode: Mode,
} }
@ -89,8 +80,8 @@ class AppsList extends PureComponent<Props, State> {
async fetchAppList() { async fetchAppList() {
try { try {
const { targetId } = this.props const { targetId, version } = this.props
const appsList = CACHED_APPS || (await listApps.send({ targetId }).toPromise()) const appsList = CACHED_APPS || (await listApps.send({ targetId, version }).toPromise())
CACHED_APPS = appsList CACHED_APPS = appsList
if (!this._unmounted) { if (!this._unmounted) {
this.setState({ appsList, status: 'idle' }) this.setState({ appsList, status: 'idle' })
@ -100,14 +91,14 @@ class AppsList extends PureComponent<Props, State> {
} }
} }
handleInstallApp = (args: { app: any, name: string }) => async () => { handleInstallApp = (app: LedgerScriptParams) => async () => {
const { app: appParams, name } = args this.setState({ status: 'busy', app: app.name, mode: 'installing' })
this.setState({ status: 'busy', app: name, mode: 'installing' })
try { try {
const { const {
device: { path: devicePath }, device: { path: devicePath },
targetId,
} = this.props } = this.props
const data = { appParams, devicePath } const data = { app, devicePath, targetId }
await installApp.send(data).toPromise() await installApp.send(data).toPromise()
this.setState({ status: 'success', app: '' }) this.setState({ status: 'success', app: '' })
} catch (err) { } catch (err) {
@ -115,14 +106,14 @@ class AppsList extends PureComponent<Props, State> {
} }
} }
handleUninstallApp = (args: { app: any, name: string }) => async () => { handleUninstallApp = (app: LedgerScriptParams) => async () => {
const { app: appParams, name } = args this.setState({ status: 'busy', app: app.name, mode: 'uninstalling' })
this.setState({ status: 'busy', app: name, mode: 'uninstalling' })
try { try {
const { const {
device: { path: devicePath }, device: { path: devicePath },
targetId,
} = this.props } = this.props
const data = { appParams, devicePath } const data = { app, devicePath, targetId }
await uninstallApp.send(data).toPromise() await uninstallApp.send(data).toPromise()
this.setState({ status: 'success', app: '' }) this.setState({ status: 'success', app: '' })
} catch (err) { } catch (err) {
@ -192,7 +183,7 @@ class AppsList extends PureComponent<Props, State> {
<List> <List>
{items.map(c => ( {items.map(c => (
<ManagerApp <ManagerApp
key={`${c.name}_${c.version}_${c.bolos_version.min}`} key={`${c.name}_${c.version}`}
name={c.name} name={c.name}
version={`Version ${c.version}`} version={`Version ${c.version}`}
icon={ICONS_FALLBACK[c.icon] || c.icon} icon={ICONS_FALLBACK[c.icon] || c.icon}

6
src/components/ManagerPage/Dashboard.js

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

4
src/components/Workflow/index.js

@ -34,6 +34,7 @@ type Props = {
renderMcuUpdate?: (device: Device, deviceInfo: DeviceInfo) => Node, renderMcuUpdate?: (device: Device, deviceInfo: DeviceInfo) => Node,
renderFinalUpdate?: (device: Device, deviceInfo: DeviceInfo) => Node, renderFinalUpdate?: (device: Device, deviceInfo: DeviceInfo) => Node,
renderDashboard?: (device: Device, deviceInfo: DeviceInfo, isGenuine: boolean) => Node, renderDashboard?: (device: Device, deviceInfo: DeviceInfo, isGenuine: boolean) => Node,
onGenuineCheck?: (isGenuine: boolean) => void,
renderError?: (dashboardError: ?Error, genuineError: ?Error) => Node, renderError?: (dashboardError: ?Error, genuineError: ?Error) => Node,
} }
type State = {} type State = {}
@ -47,14 +48,13 @@ class Workflow extends PureComponent<Props, State> {
renderMcuUpdate, renderMcuUpdate,
renderError, renderError,
renderDefault, renderDefault,
onGenuineCheck,
} = this.props } = this.props
return ( return (
<EnsureDevice> <EnsureDevice>
{(device: Device) => ( {(device: Device) => (
<EnsureDashboard device={device}> <EnsureDashboard device={device}>
{(deviceInfo: ?DeviceInfo, dashboardError: ?Error) => { {(deviceInfo: ?DeviceInfo, dashboardError: ?Error) => {
console.log('deviceInfo', deviceInfo)
if (deviceInfo && deviceInfo.mcu && renderMcuUpdate) { if (deviceInfo && deviceInfo.mcu && renderMcuUpdate) {
return renderMcuUpdate(device, deviceInfo) return renderMcuUpdate(device, deviceInfo)
} }

12
src/helpers/apps/installApp.js

@ -2,7 +2,7 @@
import qs from 'qs' import qs from 'qs'
import type Transport from '@ledgerhq/hw-transport' import type Transport from '@ledgerhq/hw-transport'
import { BASE_SOCKET_URL } from 'helpers/constants' import { BASE_SOCKET_URL_SECURE } from 'config/constants'
import { createDeviceSocket } from 'helpers/socket' import { createDeviceSocket } from 'helpers/socket'
import type { LedgerScriptParams } from 'helpers/common' import type { LedgerScriptParams } from 'helpers/common'
@ -12,8 +12,14 @@ import type { LedgerScriptParams } from 'helpers/common'
*/ */
export default async function installApp( export default async function installApp(
transport: Transport<*>, transport: Transport<*>,
{ appParams }: { appParams: LedgerScriptParams }, targetId: string | number,
{ app }: { app: LedgerScriptParams },
): Promise<*> { ): Promise<*> {
const url = `${BASE_SOCKET_URL}/install?${qs.stringify(appParams)}` const params = {
targetId,
...app,
firmwareKey: app.firmware_key,
}
const url = `${BASE_SOCKET_URL_SECURE}/install?${qs.stringify(params)}`
return createDeviceSocket(transport, url).toPromise() return createDeviceSocket(transport, url).toPromise()
} }

35
src/helpers/apps/listApps.js

@ -1,20 +1,31 @@
// @flow // @flow
import axios from 'axios' import axios from 'axios'
import { MANAGER_API_BASE } from 'config/constants' import {
DEVICE_VERSION_BY_TARGET_ID,
APPLICATIONS_BY_DEVICE,
FIRMWARE_FINAL_VERSIONS_NAME,
} from 'helpers/urls'
export default async (targetId: string | number) => { export default async (targetId: string | number, version: string) => {
try { try {
const { data: deviceData } = await axios.get( const provider = 1
`${MANAGER_API_BASE}/device_versions_target_id/${targetId}`, const { data: deviceData } = await axios.post(DEVICE_VERSION_BY_TARGET_ID, {
) provider,
const { data } = await axios.get('https://api.ledgerwallet.com/update/applications') target_id: targetId,
})
if (deviceData.name in data) { const { data: firmwareData } = await axios.post(FIRMWARE_FINAL_VERSIONS_NAME, {
return data[deviceData.name] device_version: deviceData.id,
} se_firmware_name: version,
})
return data['nanos-1.4'] const {
data: { application_versions },
} = await axios.post(APPLICATIONS_BY_DEVICE, {
providers: [1],
current_se_firmware_final_version: firmwareData.id,
device_version: deviceData.id,
})
return application_versions.length > 0 ? application_versions : []
} catch (err) { } catch (err) {
const error = Error(err.message) const error = Error(err.message)
error.stack = err.stack error.stack = err.stack

14
src/helpers/apps/uninstallApp.js

@ -2,7 +2,7 @@
import qs from 'qs' import qs from 'qs'
import type Transport from '@ledgerhq/hw-transport' import type Transport from '@ledgerhq/hw-transport'
import { BASE_SOCKET_URL } from 'helpers/constants' import { BASE_SOCKET_URL_SECURE } from 'config/constants'
import { createDeviceSocket } from 'helpers/socket' import { createDeviceSocket } from 'helpers/socket'
import type { LedgerScriptParams } from 'helpers/common' import type { LedgerScriptParams } from 'helpers/common'
@ -12,13 +12,15 @@ import type { LedgerScriptParams } from 'helpers/common'
*/ */
export default async function uninstallApp( export default async function uninstallApp(
transport: Transport<*>, transport: Transport<*>,
{ appParams }: { appParams: LedgerScriptParams }, targetId: string | number,
{ app }: { app: LedgerScriptParams },
): Promise<*> { ): Promise<*> {
const params = { const params = {
...appParams, targetId,
firmware: appParams.delete, ...app,
firmwareKey: appParams.deleteKey, firmware: app.delete,
firmwareKey: app.delete_key,
} }
const url = `${BASE_SOCKET_URL}/install?${qs.stringify(params)}` const url = `${BASE_SOCKET_URL_SECURE}/install?${qs.stringify(params)}`
return createDeviceSocket(transport, url).toPromise() return createDeviceSocket(transport, url).toPromise()
} }

8
src/helpers/common.js

@ -16,10 +16,14 @@ const APDUS = {
export type LedgerScriptParams = { export type LedgerScriptParams = {
firmware?: string, firmware?: string,
firmwareKey?: string, firmware_key?: string,
delete?: string, delete?: string,
deleteKey?: string, delete_key?: string,
targetId?: string | number, targetId?: string | number,
name: string,
version: string,
icon: string,
app?: number,
} }
type FirmwareUpdateType = 'osu' | 'final' type FirmwareUpdateType = 'osu' | 'final'

4
src/helpers/devices/getIsGenuine.js

@ -1,7 +1,7 @@
// @flow // @flow
import qs from 'qs' import qs from 'qs'
import type Transport from '@ledgerhq/hw-transport' import type Transport from '@ledgerhq/hw-transport'
import { SKIP_GENUINE, MANAGER_API_BASE } from 'config/constants' import { SKIP_GENUINE, BASE_SOCKET_URL_SECURE } from 'config/constants'
import { createDeviceSocket } from 'helpers/socket' import { createDeviceSocket } from 'helpers/socket'
@ -9,7 +9,7 @@ export default async (
transport: Transport<*>, transport: Transport<*>,
params: { targetId: string | number }, params: { targetId: string | number },
): Promise<string> => { ): Promise<string> => {
const url = `${MANAGER_API_BASE}/genuine?${qs.stringify(params)}` const url = `${BASE_SOCKET_URL_SECURE}/genuine?${qs.stringify(params)}`
return SKIP_GENUINE return SKIP_GENUINE
? new Promise(resolve => setTimeout(() => resolve('0000'), 1000)) ? new Promise(resolve => setTimeout(() => resolve('0000'), 1000))
: createDeviceSocket(transport, url).toPromise() : createDeviceSocket(transport, url).toPromise()

4
src/helpers/firmware/installFinalFirmware.js

@ -2,7 +2,7 @@
import qs from 'qs' import qs from 'qs'
import type Transport from '@ledgerhq/hw-transport' import type Transport from '@ledgerhq/hw-transport'
import { BASE_SOCKET_URL } from 'helpers/constants' import { BASE_SOCKET_URL_SECURE } from 'config/constants'
import { createDeviceSocket } from 'helpers/socket' import { createDeviceSocket } from 'helpers/socket'
import { buildParamsFromFirmware } from 'helpers/common' import { buildParamsFromFirmware } from 'helpers/common'
@ -14,7 +14,7 @@ const buildFinalParams = buildParamsFromFirmware('final')
export default async (transport: Transport<*>, firmware: Input): Result => { export default async (transport: Transport<*>, firmware: Input): Result => {
try { try {
const finalData = buildFinalParams(firmware) const finalData = buildFinalParams(firmware)
const url = `${BASE_SOCKET_URL}/install?${qs.stringify(finalData)}` const url = `${BASE_SOCKET_URL_SECURE}/install?${qs.stringify(finalData)}`
await createDeviceSocket(transport, url).toPromise() await createDeviceSocket(transport, url).toPromise()
return { success: true } return { success: true }
} catch (err) { } catch (err) {

4
src/helpers/firmware/installMcu.js

@ -2,7 +2,7 @@
import qs from 'qs' import qs from 'qs'
import type Transport from '@ledgerhq/hw-transport' import type Transport from '@ledgerhq/hw-transport'
import { MANAGER_API_URL } from 'helpers/constants' import { MANAGER_API_BASE } from 'config/constants'
import { createDeviceSocket } from 'helpers/socket' import { createDeviceSocket } from 'helpers/socket'
type Result = Promise<*> type Result = Promise<*>
@ -11,6 +11,6 @@ export default async (
transport: Transport<*>, transport: Transport<*>,
params: { targetId: string | number, version: string }, params: { targetId: string | number, version: string },
): Result => { ): Result => {
const url = `${MANAGER_API_URL}/mcu?${qs.stringify(params)}` const url = `${MANAGER_API_BASE}/mcu?${qs.stringify(params)}`
return createDeviceSocket(transport, url).toPromise() return createDeviceSocket(transport, url).toPromise()
} }

4
src/helpers/firmware/installOsuFirmware.js

@ -2,7 +2,7 @@
import qs from 'qs' import qs from 'qs'
import type Transport from '@ledgerhq/hw-transport' import type Transport from '@ledgerhq/hw-transport'
import { BASE_SOCKET_URL } from 'helpers/constants' import { BASE_SOCKET_URL_SECURE } from 'config/constants'
import { createDeviceSocket } from 'helpers/socket' import { createDeviceSocket } from 'helpers/socket'
import { buildParamsFromFirmware } from 'helpers/common' import { buildParamsFromFirmware } from 'helpers/common'
@ -15,7 +15,7 @@ const buildOsuParams = buildParamsFromFirmware('osu')
export default async (transport: Transport<*>, firmware: Input): Result => { export default async (transport: Transport<*>, firmware: Input): Result => {
try { try {
const osuData = buildOsuParams(firmware) const osuData = buildOsuParams(firmware)
const url = `${BASE_SOCKET_URL}/install?${qs.stringify(osuData)}` const url = `${BASE_SOCKET_URL_SECURE}/install?${qs.stringify(osuData)}`
await createDeviceSocket(transport, url).toPromise() await createDeviceSocket(transport, url).toPromise()
return { success: true } return { success: true }
} catch (err) { } catch (err) {

18
src/helpers/urls.js

@ -0,0 +1,18 @@
// @flow
import qs from 'qs'
import { MANAGER_API_BASE, BASE_SOCKET_URL_SECURE } from 'config/constants'
const urlBuilder = (base: string) => (endpoint: string): string => `${base}/${endpoint}`
const managerUrlbuilder = urlBuilder(MANAGER_API_BASE)
const wsURLBuilder = (endpoint: string) => (params?: Object) =>
`${BASE_SOCKET_URL_SECURE}/${endpoint}${params ? `?${qs.stringify(params)}` : ''}`
export const DEVICE_VERSION_BY_TARGET_ID = managerUrlbuilder('device_versions_target_id')
export const APPLICATIONS_BY_DEVICE = managerUrlbuilder('get_apps')
export const FIRMWARE_FINAL_VERSIONS_NAME = managerUrlbuilder('firmware_final_versions_name')
export const WS_INSTALL = wsURLBuilder('install')
export const WS_GENUINE = wsURLBuilder('genuine')
export const WS_MCU = wsURLBuilder('genuine')
Loading…
Cancel
Save