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'
type Input = {
appParams: LedgerScriptParams,
app: LedgerScriptParams,
devicePath: string,
targetId: string | number,
}
type Result = *
const cmd: Command<Input, Result> = createCommand('installApp', ({ devicePath, ...rest }) =>
fromPromise(withDevice(devicePath)(transport => installApp(transport, rest))),
const cmd: Command<Input, Result> = createCommand(
'installApp',
({ devicePath, targetId, ...app }) =>
fromPromise(withDevice(devicePath)(transport => installApp(transport, targetId, app))),
)
export default cmd

5
src/commands/listApps.js

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

9
src/commands/uninstallApp.js

@ -9,14 +9,17 @@ import uninstallApp from 'helpers/apps/uninstallApp'
import type { LedgerScriptParams } from 'helpers/common'
type Input = {
appParams: LedgerScriptParams,
app: LedgerScriptParams,
devicePath: string,
targetId: string | number,
}
type Result = *
const cmd: Command<Input, Result> = createCommand('uninstallApp', ({ devicePath, ...rest }) =>
fromPromise(withDevice(devicePath)(transport => uninstallApp(transport, rest))),
const cmd: Command<Input, Result> = createCommand(
'uninstallApp',
({ devicePath, targetId, ...rest }) =>
fromPromise(withDevice(devicePath)(transport => uninstallApp(transport, targetId, rest))),
)
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 fontFamily from 'styles/styled/fontFamily'
import type { LedgerScriptParams } from 'helpers/common'
import { ff } from 'styles/helpers'
import Box from 'components/base/Box'
@ -12,20 +14,9 @@ import Search from 'components/base/Search'
import SearchIcon from 'icons/Search'
import CrossIcon from 'icons/Cross'
type LedgerApp = {
name: string,
version: string,
icon: string,
app: Object,
bolos_version: {
min: number,
max: number,
},
}
type Props = {
list: Array<LedgerApp>,
children: (list: Array<LedgerApp>) => React$Node,
list: Array<LedgerScriptParams>,
children: (list: Array<LedgerScriptParams>) => React$Node,
}
type State = {

37
src/components/ManagerPage/AppsList.js

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

6
src/components/ManagerPage/Dashboard.js

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

4
src/components/Workflow/index.js

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

12
src/helpers/apps/installApp.js

@ -2,7 +2,7 @@
import qs from 'qs'
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 type { LedgerScriptParams } from 'helpers/common'
@ -12,8 +12,14 @@ import type { LedgerScriptParams } from 'helpers/common'
*/
export default async function installApp(
transport: Transport<*>,
{ appParams }: { appParams: LedgerScriptParams },
targetId: string | number,
{ app }: { app: LedgerScriptParams },
): 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()
}

35
src/helpers/apps/listApps.js

@ -1,20 +1,31 @@
// @flow
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 {
const { data: deviceData } = await axios.get(
`${MANAGER_API_BASE}/device_versions_target_id/${targetId}`,
)
const { data } = await axios.get('https://api.ledgerwallet.com/update/applications')
if (deviceData.name in data) {
return data[deviceData.name]
}
return data['nanos-1.4']
const provider = 1
const { data: deviceData } = await axios.post(DEVICE_VERSION_BY_TARGET_ID, {
provider,
target_id: targetId,
})
const { data: firmwareData } = await axios.post(FIRMWARE_FINAL_VERSIONS_NAME, {
device_version: deviceData.id,
se_firmware_name: version,
})
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) {
const error = Error(err.message)
error.stack = err.stack

14
src/helpers/apps/uninstallApp.js

@ -2,7 +2,7 @@
import qs from 'qs'
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 type { LedgerScriptParams } from 'helpers/common'
@ -12,13 +12,15 @@ import type { LedgerScriptParams } from 'helpers/common'
*/
export default async function uninstallApp(
transport: Transport<*>,
{ appParams }: { appParams: LedgerScriptParams },
targetId: string | number,
{ app }: { app: LedgerScriptParams },
): Promise<*> {
const params = {
...appParams,
firmware: appParams.delete,
firmwareKey: appParams.deleteKey,
targetId,
...app,
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()
}

8
src/helpers/common.js

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

4
src/helpers/devices/getIsGenuine.js

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

4
src/helpers/firmware/installFinalFirmware.js

@ -2,7 +2,7 @@
import qs from 'qs'
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 { buildParamsFromFirmware } from 'helpers/common'
@ -14,7 +14,7 @@ const buildFinalParams = buildParamsFromFirmware('final')
export default async (transport: Transport<*>, firmware: Input): Result => {
try {
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()
return { success: true }
} catch (err) {

4
src/helpers/firmware/installMcu.js

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

4
src/helpers/firmware/installOsuFirmware.js

@ -2,7 +2,7 @@
import qs from 'qs'
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 { buildParamsFromFirmware } from 'helpers/common'
@ -15,7 +15,7 @@ const buildOsuParams = buildParamsFromFirmware('osu')
export default async (transport: Transport<*>, firmware: Input): Result => {
try {
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()
return { success: true }
} 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