-
-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement Bluetooth Onboarding UI feat: different 'Not the Trezor you are looking for' UI feat: implement Bluetooth Onboarding UI 2 feat: implement Bluetooth Onboarding UI feat: different 'Not the Trezor you are looking for' UI feat: implement Bluetooth Onboarding UI 2 feat: use CollapsibleBox component in Bluetooth UI fix: after rebase enable BT onboarding WIP: transport name breaking change aftr rebase feat: implement suite-common bluetooth code into Suite
- Loading branch information
1 parent
33919a6
commit bd1861f
Showing
36 changed files
with
1,086 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
packages/suite/src/actions/bluetooth/__tests__/remapKnownDevicesForLinux.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { BluetoothDevice } from '@trezor/transport-bluetooth'; | ||
|
||
import { remapKnownDevicesForLinux } from '../remapKnownDevicesForLinux'; | ||
|
||
const nearbyDeviceA: BluetoothDevice = { | ||
id: 'New-Id-A', | ||
data: [], | ||
name: 'Trezor A', | ||
lastUpdatedTimestamp: 1, | ||
macAddress: 'Address-Trezor-A-Staying-Same', | ||
connected: false, | ||
paired: false, | ||
rssi: 0, | ||
}; | ||
|
||
const nearbyDeviceC: BluetoothDevice = { | ||
id: 'C', | ||
data: [], | ||
name: 'Trezor C', | ||
lastUpdatedTimestamp: 1, | ||
macAddress: 'Address-Trezor-C', | ||
connected: false, | ||
paired: false, | ||
rssi: 0, | ||
}; | ||
|
||
const knownDeviceB: BluetoothDevice = { | ||
id: 'B', | ||
data: [], | ||
name: 'Trezor A', | ||
lastUpdatedTimestamp: 1, | ||
macAddress: 'Address-Trezor-B', | ||
connected: false, | ||
paired: false, | ||
rssi: 0, | ||
}; | ||
|
||
const knownDeviceA: BluetoothDevice = { | ||
id: 'Original-Id A', | ||
data: [], | ||
name: 'Trezor B', | ||
lastUpdatedTimestamp: 2, | ||
macAddress: 'Address-Trezor-A-Staying-Same', | ||
connected: false, | ||
paired: false, | ||
rssi: 0, | ||
}; | ||
|
||
describe(remapKnownDevicesForLinux.name, () => { | ||
it('remaps the changed id of the device, while leaving the others intact', () => { | ||
const result = remapKnownDevicesForLinux({ | ||
nearbyDevices: [nearbyDeviceA, nearbyDeviceC], | ||
knownDevices: [knownDeviceA, knownDeviceB], | ||
}); | ||
|
||
expect(result).toEqual([ | ||
{ | ||
address: 'Address-Trezor-A-Staying-Same', | ||
connected: false, | ||
data: [], | ||
id: 'New-Id-A', | ||
lastUpdatedTimestamp: 2, | ||
name: 'Trezor B', | ||
paired: false, | ||
rssi: 0, | ||
}, | ||
knownDeviceB, // Is kept as it is | ||
]); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
packages/suite/src/actions/bluetooth/bluetoothConnectDeviceThunk.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { BLUETOOTH_PREFIX, bluetoothActions } from '@suite-common/bluetooth'; | ||
import { createThunk } from '@suite-common/redux-utils'; | ||
import { notificationsActions } from '@suite-common/toast-notifications'; | ||
import { bluetoothIpc } from '@trezor/transport-bluetooth'; | ||
|
||
type BluetoothConnectDeviceThunkResult = { | ||
success: boolean; | ||
}; | ||
|
||
export const bluetoothConnectDeviceThunk = createThunk< | ||
BluetoothConnectDeviceThunkResult, | ||
{ id: string }, | ||
void | ||
>( | ||
`${BLUETOOTH_PREFIX}/bluetoothConnectDeviceThunk`, | ||
async ({ id }, { fulfillWithValue, dispatch }) => { | ||
const result = await bluetoothIpc.connectDevice(id); | ||
|
||
if (!result.success) { | ||
dispatch( | ||
bluetoothActions.connectDeviceEventAction({ | ||
id, | ||
connectionStatus: { type: 'error', error: result.error }, | ||
}), | ||
); | ||
dispatch( | ||
notificationsActions.addToast({ | ||
type: 'error', | ||
error: result.error, | ||
}), | ||
); | ||
} else { | ||
dispatch( | ||
bluetoothActions.connectDeviceEventAction({ | ||
id, | ||
connectionStatus: { type: 'connected' }, | ||
}), | ||
); | ||
} | ||
|
||
return fulfillWithValue({ success: result.success }); | ||
}, | ||
); |
13 changes: 13 additions & 0 deletions
13
packages/suite/src/actions/bluetooth/bluetoothStartScanningThunk.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { BLUETOOTH_PREFIX, bluetoothActions } from '@suite-common/bluetooth'; | ||
import { createThunk } from '@suite-common/redux-utils'; | ||
import { bluetoothIpc } from '@trezor/transport-bluetooth'; | ||
|
||
export const bluetoothStartScanningThunk = createThunk<void, void, void>( | ||
`${BLUETOOTH_PREFIX}/bluetoothStartScanningThunk`, | ||
(_, { dispatch }) => { | ||
dispatch(bluetoothActions.scanStatusAction({ status: 'running' })); | ||
// This can fail, but if there is an error we already got it from `adapter-event` | ||
// and user is informed about it (bluetooth turned-off, ...) | ||
bluetoothIpc.startScan(); | ||
}, | ||
); |
12 changes: 12 additions & 0 deletions
12
packages/suite/src/actions/bluetooth/bluetoothStopScanningThunk.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { BLUETOOTH_PREFIX, bluetoothActions } from '@suite-common/bluetooth'; | ||
import { createThunk } from '@suite-common/redux-utils'; | ||
import { bluetoothIpc } from '@trezor/transport-bluetooth'; | ||
|
||
export const bluetoothStopScanningThunk = createThunk<void, void, void>( | ||
`${BLUETOOTH_PREFIX}/bluetoothStopScanningThunk`, | ||
(_, { dispatch }) => { | ||
dispatch(bluetoothActions.scanStatusAction({ status: 'idle' })); | ||
// This can fail, but there is nothing we can do about it | ||
bluetoothIpc.stopScan(); | ||
}, | ||
); |
62 changes: 62 additions & 0 deletions
62
packages/suite/src/actions/bluetooth/initBluetoothThunk.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { BLUETOOTH_PREFIX, bluetoothActions, selectKnownDevices } from '@suite-common/bluetooth'; | ||
import { createThunk } from '@suite-common/redux-utils/'; | ||
import { BluetoothDevice, DeviceConnectionStatus, bluetoothIpc } from '@trezor/transport-bluetooth'; | ||
import { Without } from '@trezor/type-utils'; | ||
|
||
import { remapKnownDevicesForLinux } from './remapKnownDevicesForLinux'; | ||
import { selectSuiteFlags } from '../../reducers/suite/suiteReducer'; | ||
|
||
type DeviceConnectionStatusWithOptionalId = Without<DeviceConnectionStatus, 'id'> & { | ||
id?: string; | ||
}; | ||
|
||
export const initBluetoothThunk = createThunk<void, void, void>( | ||
`${BLUETOOTH_PREFIX}/initBluetoothThunk`, | ||
async (_, { dispatch, getState }) => { | ||
const { isBluetoothEnabled } = selectSuiteFlags(getState()); | ||
|
||
if (!isBluetoothEnabled) { | ||
return; | ||
} | ||
|
||
bluetoothIpc.on('adapter-event', isPowered => { | ||
console.warn('adapter-event', isPowered); | ||
dispatch(bluetoothActions.adapterEventAction({ isPowered })); | ||
}); | ||
|
||
bluetoothIpc.on('device-list-update', nearbyDevices => { | ||
console.warn('device-list-update', nearbyDevices); | ||
|
||
const knownDevices = selectKnownDevices<BluetoothDevice>(getState()); | ||
|
||
const remappedKnownDevices = remapKnownDevicesForLinux({ | ||
knownDevices, | ||
nearbyDevices, | ||
}); | ||
|
||
dispatch( | ||
bluetoothActions.knownDevicesUpdateAction({ knownDevices: remappedKnownDevices }), | ||
); | ||
dispatch(bluetoothActions.nearbyDevicesUpdateAction({ nearbyDevices })); | ||
}); | ||
|
||
bluetoothIpc.on('device-connection-status', connectionStatus => { | ||
console.warn('device-connection-status', connectionStatus); | ||
const copyConnectionStatus: DeviceConnectionStatusWithOptionalId = { | ||
...connectionStatus, | ||
}; | ||
delete copyConnectionStatus.id; // So we dont pollute redux store | ||
|
||
dispatch( | ||
bluetoothActions.connectDeviceEventAction({ | ||
id: connectionStatus.id, | ||
connectionStatus: copyConnectionStatus, | ||
}), | ||
); | ||
}); | ||
|
||
// TODO: this should be called after trezor/connect init? | ||
const knownDevices = selectKnownDevices<BluetoothDevice>(getState()); | ||
await bluetoothIpc.init({ knownDevices }); | ||
}, | ||
); |
28 changes: 28 additions & 0 deletions
28
packages/suite/src/actions/bluetooth/remapKnownDevicesForLinux.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { BluetoothDevice } from '@trezor/transport-bluetooth'; | ||
|
||
type RemapKnownDevicesForLinuxParams = { | ||
knownDevices: BluetoothDevice[]; | ||
nearbyDevices: BluetoothDevice[]; | ||
}; | ||
|
||
/** | ||
* On linux, when bluetooth adapter is turned off/on again, the paired | ||
* devices will get different `id`, but `address` will remain the same. | ||
* | ||
* Therefore, we have to remap the knownDevices to change the `id`. | ||
*/ | ||
export const remapKnownDevicesForLinux = ({ | ||
knownDevices, | ||
nearbyDevices, | ||
}: RemapKnownDevicesForLinuxParams): BluetoothDevice[] => | ||
knownDevices.map(knownDevice => { | ||
const nearbyDeviceWithSameAddress = nearbyDevices.find( | ||
nearbyDevice => | ||
nearbyDevice.macAddress === knownDevice.macAddress && | ||
nearbyDevice.id !== knownDevice.id, | ||
); | ||
|
||
return nearbyDeviceWithSameAddress | ||
? { ...knownDevice, id: nearbyDeviceWithSameAddress.id } | ||
: knownDevice; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.