From 37d99b0a94610b1be0c66fefca2db3cb456331cf Mon Sep 17 00:00:00 2001 From: Peter Sanderson Date: Thu, 28 Nov 2024 12:15:22 +0100 Subject: [PATCH] temp: mocks for pairing flow --- packages/suite-desktop-api/src/api.ts | 2 +- .../src/modules/bluetooth.ts | 80 ++++++++++++++++--- 2 files changed, 68 insertions(+), 14 deletions(-) diff --git a/packages/suite-desktop-api/src/api.ts b/packages/suite-desktop-api/src/api.ts index cefff7e692ff..5aa90d164fd9 100644 --- a/packages/suite-desktop-api/src/api.ts +++ b/packages/suite-desktop-api/src/api.ts @@ -83,7 +83,7 @@ export interface RendererChannels { 'bluetooth/event': any; 'bluetooth/adapter-event': boolean; 'bluetooth/select-device-event': ElectronBluetoothDevice[]; - 'bluetooth/pair-device-event': { paired: boolean; pin: string }; + 'bluetooth/pair-device-event': { uuid: string, paired: boolean; pin: string }; 'bluetooth/connect-device-event': any; } diff --git a/packages/suite-desktop-core/src/modules/bluetooth.ts b/packages/suite-desktop-core/src/modules/bluetooth.ts index 6828079df39b..f7a13206a4a8 100644 --- a/packages/suite-desktop-core/src/modules/bluetooth.ts +++ b/packages/suite-desktop-core/src/modules/bluetooth.ts @@ -73,21 +73,60 @@ export const init: ModuleInit = ({ mainWindowProxy }) => { mainWindowProxy.getInstance()?.webContents.send('bluetooth/pair-device-event', event); }); - const result = await api.sendMessage('connect_device', uuid); - console.warn('Connect result', result); + // Todo: enter pairing mode + const linuxPin = { uuid: 'hci0/dev_E1_43_47_BA_6A_69', paired: false, pin: '' }; + const windowsPin = { uuid: 'hci0/dev_E1_43_47_BA_6A_69', paired: false, pin: '123456' }; + + // UI: + // - click button -> "Connecting" + // - "pair-device-event" -> pairing -> show PIN Modal + // - (Win/Linux) "pair-device-event" -> paired -> hide PIN Modal + // - "connect-device-event":start -> connecting -> hide PIN Modal (for Mac) + // - "connect-device-event" :done -> connected, but ...connect-connecting again + + // 1. [Win, Lin, Mac] In case of windows, this is where we get PIN, on Mac we may not get this + await new Promise(resolve => setTimeout(resolve, 2000)); + mainWindowProxy.getInstance()?.webContents.send('bluetooth/pair-device-event', windowsPin); + + // 2. [Win, Lin] Simulates that user confirmed PIN on the device + await new Promise(resolve => setTimeout(resolve, 4000)); + mainWindowProxy.getInstance()?.webContents.send('bluetooth/pair-device-event', { + uuid: 'hci0/dev_E1_43_47_BA_6A_69', + paired: true, + pin: '', + }); - api.disconnect(); + // 3. [Win, Lin, Mac] Simulates that device is starting to connect + await new Promise(resolve => setTimeout(resolve, 2000)); + mainWindowProxy.getInstance()?.webContents.send('bluetooth/connect-device-event', { + uuid: 'hci0/dev_E1_43_47_BA_6A_69', + status: 'connecting', + }); - if (result !== true) { - console.warn('ERROR!', result); - // mainWindowProxy - // .getInstance() - // ?.webContents.send('bluetooth/connect-device-event', { phase: 'error' }); + // 4. [Win, Lin, Mac] Simulates that device is starting to connect + await new Promise(resolve => setTimeout(resolve, 2000)); + mainWindowProxy.getInstance()?.webContents.send('bluetooth/connect-device-event', { + uuid: 'hci0/dev_E1_43_47_BA_6A_69', + status: 'done', + }); - return { success: false, error: result }; - } else { - return { success: true }; - } + return { success: true }; + + // const result = await api.sendMessage('connect_device', uuid); + // console.warn('Connect result', result); + // + // api.disconnect(); + // + // if (result !== true) { + // console.warn('ERROR!', result); + // // mainWindowProxy + // // .getInstance() + // // ?.webContents.send('bluetooth/connect-device-event', { phase: 'error' }); + // + // return { success: false, error: result }; + // } else { + // return { success: true }; + // } }); ipcMain.handle('bluetooth/forget-device', async (_, uuid) => { @@ -143,7 +182,22 @@ export const init: ModuleInit = ({ mainWindowProxy }) => { const devices = await api.sendMessage('start_scan'); - emitSelect({ devices }); + emitSelect({ + devices: [ + { + name: 'TrezorZephyr', + internal_model: 1, + model_variant: 3, + uuid: 'hci0/dev_E1_43_47_BA_6A_69', + connected: false, + timestamp: 1732787043, + rssi: 0, + pairing_mode: false, + paired: true, + }, + ...devices, + ], + }); // const clear = () => { // ipcMain.removeAllListeners('bluetooth/stop-scan');