diff --git a/src/commands/getIsGenuine.js b/src/commands/getIsGenuine.js index 25f94d13..8b9cfa2e 100644 --- a/src/commands/getIsGenuine.js +++ b/src/commands/getIsGenuine.js @@ -4,10 +4,13 @@ import { createCommand, Command } from 'helpers/ipc' import { fromPromise } from 'rxjs/observable/fromPromise' import getIsGenuine from 'helpers/devices/getIsGenuine' +import { withDevice } from 'helpers/deviceAccess' type Input = * -type Result = boolean +type Result = string -const cmd: Command = createCommand('getIsGenuine', () => fromPromise(getIsGenuine())) +const cmd: Command = createCommand('getIsGenuine', ({ devicePath, targetId }) => + fromPromise(withDevice(devicePath)(transport => getIsGenuine(transport, { targetId }))), +) export default cmd diff --git a/src/components/ManagerPage/EnsureGenuine.js b/src/components/ManagerPage/EnsureGenuine.js index 2de56aeb..1391e163 100644 --- a/src/components/ManagerPage/EnsureGenuine.js +++ b/src/components/ManagerPage/EnsureGenuine.js @@ -12,8 +12,14 @@ type Error = { stack: string, } +type DeviceInfos = { + targetId: number | string, + version: string, +} + type Props = { device: ?Device, + infos: ?DeviceInfos, children: (isGenuine: ?boolean, error: ?Error) => Node, } @@ -49,12 +55,15 @@ class EnsureGenuine extends PureComponent { _unmounting = false async checkIsGenuine() { - const { device } = this.props - if (device && !this._checking) { + const { device, infos } = this.props + if (device && infos && !this._checking) { this._checking = true try { - const isGenuine = await getIsGenuine.send().toPromise() - if (!this.state.genuine || this.state.error) { + const res = await getIsGenuine + .send({ devicePath: device.path, targetId: infos.targetId }) + .toPromise() + const isGenuine = res === '0000' + if ((!this.state.genuine || this.state.error) && isGenuine) { !this._unmounting && this.setState({ genuine: isGenuine, error: null }) } } catch (err) { diff --git a/src/components/ManagerPage/Workflow.js b/src/components/ManagerPage/Workflow.js index 1b42771f..e52f701a 100644 --- a/src/components/ManagerPage/Workflow.js +++ b/src/components/ManagerPage/Workflow.js @@ -52,7 +52,7 @@ class Workflow extends PureComponent { {(device: Device) => ( {(deviceInfo: ?DeviceInfo, dashboardError: ?Error) => ( - + {(isGenuine: ?boolean, genuineError: ?Error) => { if (dashboardError || genuineError) { return renderError diff --git a/src/helpers/apps/installApp.js b/src/helpers/apps/installApp.js index 465dc18f..2cdd04a3 100644 --- a/src/helpers/apps/installApp.js +++ b/src/helpers/apps/installApp.js @@ -12,5 +12,5 @@ export default async function installApp( transport: Transport<*>, { appParams }: { appParams: LedgerScriptParams }, ): Promise { - return createSocketDialog(transport, '/update/install', appParams) + return createSocketDialog(transport, '/install', appParams) } diff --git a/src/helpers/apps/uninstallApp.js b/src/helpers/apps/uninstallApp.js index af3c23b2..c9f4fc44 100644 --- a/src/helpers/apps/uninstallApp.js +++ b/src/helpers/apps/uninstallApp.js @@ -17,5 +17,5 @@ export default async function uninstallApp( firmware: appParams.delete, firmwareKey: appParams.deleteKey, } - return createSocketDialog(transport, '/update/install', params) + return createSocketDialog(transport, '/install', params) } diff --git a/src/helpers/common.js b/src/helpers/common.js index 52ebbe06..7d96e48b 100644 --- a/src/helpers/common.js +++ b/src/helpers/common.js @@ -5,7 +5,7 @@ import Websocket from 'ws' import qs from 'qs' import type Transport from '@ledgerhq/hw-transport' -import { BASE_SOCKET_URL, APDUS } from './constants' +import { BASE_SOCKET_URL, APDUS, MANAGER_API_URL } from './constants' type WebsocketType = { send: (string, any) => void, @@ -24,34 +24,11 @@ export type LedgerScriptParams = { firmwareKey?: string, delete?: string, deleteKey?: string, + targetId?: string | number, } type FirmwareUpdateType = 'osu' | 'final' -// /** -// * Install an app on the device -// */ -// export async function installApp( -// transport: Transport<*>, -// { appParams }: { appParams: LedgerScriptParams }, -// ): Promise { -// return createSocketDialog(transport, '/update/install', appParams) -// } - -/** - * Uninstall an app on the device - */ -export async function uninstallApp( - transport: Transport<*>, - { appParams }: { appParams: LedgerScriptParams }, -): Promise { - return createSocketDialog(transport, '/update/install', { - ...appParams, - firmware: appParams.delete, - firmwareKey: appParams.deleteKey, - }) -} - export async function getMemInfos(transport: Transport<*>): Promise { const { targetId } = await getFirmwareInfo(transport) // Dont ask me about this `perso_11`: I don't know. But we need it. @@ -119,11 +96,14 @@ export async function createSocketDialog( transport: Transport<*>, endpoint: string, params: LedgerScriptParams, + managerUrl: boolean = false, ) { return new Promise(async (resolve, reject) => { try { let lastData - const url = `${BASE_SOCKET_URL}${endpoint}?${qs.stringify(params)}` + const url = `${managerUrl ? MANAGER_API_URL : BASE_SOCKET_URL}${endpoint}?${qs.stringify( + params, + )}` log('WS CONNECTING', url) const ws: WebsocketType = new Websocket(url) @@ -142,6 +122,8 @@ export async function createSocketDialog( success: msg => { if (msg.data) { lastData = msg.data + } else if (msg.result) { + lastData = msg.result } }, error: msg => { diff --git a/src/helpers/constants.js b/src/helpers/constants.js index becafa8f..254ede23 100644 --- a/src/helpers/constants.js +++ b/src/helpers/constants.js @@ -1,10 +1,8 @@ // Socket endpoint -export const BASE_SOCKET_URL = 'ws://api.ledgerwallet.com' -export const BASE_SOCKET_URL_TEMP = 'ws://manager.ledger.fr:3500' +export const BASE_SOCKET_URL = 'ws://api.ledgerwallet.com/update' +export const MANAGER_API_URL = 'wss://api.ledgerwallet.com/update' export const API_BASE_URL = process.env.API_BASE_URL || 'https://beta.manager.live.ledger.fr/api' -// If you want to test locally with https://github.com/LedgerHQ/ledger-update-python-api -// export const BASE_SOCKET_URL = 'ws://localhost:3001/update' // List of APDUS export const APDUS = { diff --git a/src/helpers/deviceAccess.js b/src/helpers/deviceAccess.js index f2210b64..7d857f4a 100644 --- a/src/helpers/deviceAccess.js +++ b/src/helpers/deviceAccess.js @@ -19,7 +19,8 @@ export const withDevice: WithDevice = devicePath => { return job => takeSemaphorePromise(sem, async () => { const t = await retry(() => TransportNodeHid.open(devicePath), { maxRetry: 1 }) - if (process.env.DEBUG_DEVICE) t.setDebugMode(true) + + if (process.env.DEBUG_DEVICE > 0) t.setDebugMode(true) try { const res = await job(t) // $FlowFixMe diff --git a/src/helpers/devices/getIsGenuine.js b/src/helpers/devices/getIsGenuine.js index 0c546e26..13bfdffc 100644 --- a/src/helpers/devices/getIsGenuine.js +++ b/src/helpers/devices/getIsGenuine.js @@ -1,6 +1,11 @@ // @flow +import type Transport from '@ledgerhq/hw-transport' +import { createSocketDialog } from 'helpers/common' -// import type Transport from '@ledgerhq/hw-transport' - -export default async (/* transport: Transport<*> */) => - new Promise(resolve => setTimeout(() => resolve(true), 1000)) +export default async ( + transport: Transport<*>, + { targetId }: { targetId: string | number }, +): Promise<*> => + process.env.SKIP_GENUINE > 0 + ? new Promise(resolve => setTimeout(() => resolve('0000'), 1000)) + : createSocketDialog(transport, '/genuine', { targetId }, true) diff --git a/src/helpers/firmware/installFinalFirmware.js b/src/helpers/firmware/installFinalFirmware.js index 3c46e27d..43a57435 100644 --- a/src/helpers/firmware/installFinalFirmware.js +++ b/src/helpers/firmware/installFinalFirmware.js @@ -12,7 +12,7 @@ const buildOsuParams = buildParamsFromFirmware('final') export default async (transport: Transport<*>, firmware: Input): Result => { try { const osuData = buildOsuParams(firmware) - await createSocketDialog(transport, '/update/install', osuData) + await createSocketDialog(transport, '/install', osuData) return { success: true } } catch (err) { const error = Error(err.message) diff --git a/src/helpers/firmware/installOsuFirmware.js b/src/helpers/firmware/installOsuFirmware.js index 5a53fdc0..c213cb58 100644 --- a/src/helpers/firmware/installOsuFirmware.js +++ b/src/helpers/firmware/installOsuFirmware.js @@ -13,7 +13,7 @@ const buildOsuParams = buildParamsFromFirmware('osu') export default async (transport: Transport<*>, firmware: Input): Result => { try { const osuData = buildOsuParams(firmware) - await createSocketDialog(transport, '/update/install', osuData) + await createSocketDialog(transport, '/install', osuData) return { success: true } } catch (err) { const error = Error(err.message)