Skip to content

Commit d2759a5

Browse files
authored
fix: switch firmware using new tips (#633)
1 parent 7411fcd commit d2759a5

File tree

25 files changed

+162
-65
lines changed

25 files changed

+162
-65
lines changed

packages/connect-examples/electron-example/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "hardware-example",
33
"productName": "HardwareExample",
44
"executableName": "onekey-hardware-example",
5-
"version": "1.1.21-alpha.2",
5+
"version": "1.1.21-alpha.3",
66
"author": "OneKey",
77
"description": "End-to-end encrypted workspaces for teams",
88
"main": "dist/index.js",
@@ -22,7 +22,7 @@
2222
"ts:check": "yarn tsc --noEmit"
2323
},
2424
"dependencies": {
25-
"@onekeyfe/hd-transport-electron": "1.1.21-alpha.2",
25+
"@onekeyfe/hd-transport-electron": "1.1.21-alpha.3",
2626
"@stoprocent/noble": "2.3.4",
2727
"debug": "4.3.4",
2828
"electron-is-dev": "^3.0.1",

packages/connect-examples/expo-example/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "expo-example",
3-
"version": "1.1.21-alpha.2",
3+
"version": "1.1.21-alpha.3",
44
"scripts": {
55
"start": "cross-env CONNECT_SRC=https://localhost:8087/ yarn expo start --dev-client",
66
"android": "yarn expo run:android",
@@ -19,10 +19,10 @@
1919
"@noble/ed25519": "^2.1.0",
2020
"@noble/hashes": "^1.3.3",
2121
"@noble/secp256k1": "^1.7.1",
22-
"@onekeyfe/hd-ble-sdk": "1.1.21-alpha.2",
23-
"@onekeyfe/hd-common-connect-sdk": "1.1.21-alpha.2",
24-
"@onekeyfe/hd-core": "1.1.21-alpha.2",
25-
"@onekeyfe/hd-web-sdk": "1.1.21-alpha.2",
22+
"@onekeyfe/hd-ble-sdk": "1.1.21-alpha.3",
23+
"@onekeyfe/hd-common-connect-sdk": "1.1.21-alpha.3",
24+
"@onekeyfe/hd-core": "1.1.21-alpha.3",
25+
"@onekeyfe/hd-web-sdk": "1.1.21-alpha.3",
2626
"@onekeyfe/react-native-ble-utils": "^0.1.3",
2727
"@polkadot/util-crypto": "13.1.1",
2828
"@react-native-async-storage/async-storage": "1.21.0",

packages/connect-examples/expo-example/src/components/HandleSDKEvents.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export default function HandleSDKEvents() {
4444
const { sdk: SDK, lowLevelSDK: HardwareLowLevelSDK, type } = useContext(HardwareSDKContext);
4545
const [showPinInput, setShowPinInput] = useState(false);
4646
const [showWebUsbAuthorize, setShowWebUsbAuthorize] = useState(false);
47+
const [webUsbResponseType, setWebUsbResponseType] = useState<
48+
| typeof UI_RESPONSE.SELECT_DEVICE_IN_BOOTLOADER_FOR_WEB_DEVICE
49+
| typeof UI_RESPONSE.SELECT_DEVICE_FOR_SWITCH_FIRMWARE_WEB_DEVICE
50+
>(UI_RESPONSE.SELECT_DEVICE_IN_BOOTLOADER_FOR_WEB_DEVICE);
4751
const [showBluetoothPermission, setShowBluetoothPermission] = useState(false);
4852
const [bluetoothErrorType, setBluetoothErrorType] = useState<BluetoothErrorType>('permission');
4953

@@ -73,13 +77,13 @@ export default function HandleSDKEvents() {
7377
(device: USBDevice) => {
7478
console.log('webUsbSuccess: ', device);
7579
SDK?.uiResponse({
76-
type: UI_RESPONSE.SELECT_DEVICE_IN_BOOTLOADER_FOR_WEB_DEVICE,
80+
type: webUsbResponseType,
7781
payload: {
7882
deviceId: device.serialNumber ?? '',
7983
},
8084
});
8185
},
82-
[SDK]
86+
[SDK, webUsbResponseType]
8387
);
8488

8589
const onWebUsbCancel = useCallback(() => {
@@ -155,6 +159,11 @@ export default function HandleSDKEvents() {
155159
}, 2000);
156160
}
157161
if (message.type === UI_REQUEST.REQUEST_DEVICE_IN_BOOTLOADER_FOR_WEB_DEVICE) {
162+
setWebUsbResponseType(UI_RESPONSE.SELECT_DEVICE_IN_BOOTLOADER_FOR_WEB_DEVICE);
163+
setShowWebUsbAuthorize(true);
164+
}
165+
if (message.type === UI_REQUEST.REQUEST_DEVICE_FOR_SWITCH_FIRMWARE_WEB_DEVICE) {
166+
setWebUsbResponseType(UI_RESPONSE.SELECT_DEVICE_FOR_SWITCH_FIRMWARE_WEB_DEVICE);
158167
setShowWebUsbAuthorize(true);
159168
}
160169
if (message.type === UI_REQUEST.BLUETOOTH_POWERED_OFF) {

packages/connect-examples/expo-playground/app/components/providers/SDKProvider.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ export const SDKProvider: React.FC<SDKProviderProps> = ({ children }) => {
5353
const { setDeviceAction, clearDeviceAction, updateSdkInitState } = useDeviceStore();
5454
const initializationRef = useRef<boolean>(false);
5555
const [webUsbModalOpen, setWebUsbModalOpen] = React.useState(false);
56+
const [webUsbResponseType, setWebUsbResponseType] = React.useState<
57+
| typeof UI_RESPONSE.SELECT_DEVICE_IN_BOOTLOADER_FOR_WEB_DEVICE
58+
| typeof UI_RESPONSE.SELECT_DEVICE_FOR_SWITCH_FIRMWARE_WEB_DEVICE
59+
>(UI_RESPONSE.SELECT_DEVICE_IN_BOOTLOADER_FOR_WEB_DEVICE);
5660
const lastSdkRef = useRef<CoreApi | null>(null);
5761

5862
const setupSDKEventListeners = useCallback(
@@ -108,6 +112,12 @@ export const SDKProvider: React.FC<SDKProviderProps> = ({ children }) => {
108112

109113
case UI_REQUEST.REQUEST_DEVICE_IN_BOOTLOADER_FOR_WEB_DEVICE: {
110114
// Open modal; actual requestDevice() will be called in button onClick handler to satisfy user gesture
115+
setWebUsbResponseType(UI_RESPONSE.SELECT_DEVICE_IN_BOOTLOADER_FOR_WEB_DEVICE);
116+
setWebUsbModalOpen(true);
117+
break;
118+
}
119+
case UI_REQUEST.REQUEST_DEVICE_FOR_SWITCH_FIRMWARE_WEB_DEVICE: {
120+
setWebUsbResponseType(UI_RESPONSE.SELECT_DEVICE_FOR_SWITCH_FIRMWARE_WEB_DEVICE);
111121
setWebUsbModalOpen(true);
112122
break;
113123
}
@@ -214,13 +224,13 @@ export const SDKProvider: React.FC<SDKProviderProps> = ({ children }) => {
214224
open={webUsbModalOpen}
215225
onOpenChange={setWebUsbModalOpen}
216226
onSuccess={device => {
217-
logInfo('WebUSB device selected for bootloader (modal)', {
227+
logInfo('WebUSB device selected (modal)', {
218228
serialNumber: device?.serialNumber ?? '',
219229
vendorId: device?.vendorId,
220230
productId: device?.productId,
221231
});
222232
lastSdkRef.current?.uiResponse({
223-
type: UI_RESPONSE.SELECT_DEVICE_IN_BOOTLOADER_FOR_WEB_DEVICE,
233+
type: webUsbResponseType,
224234
payload: { deviceId: device?.serialNumber ?? '' },
225235
});
226236
}}

packages/connect-examples/expo-playground/app/components/ui/DeviceActionAnimation.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ const DeviceActionAnimation: React.FC<DeviceActionAnimationProps> = ({
8686

8787
case UI_REQUEST.REQUEST_PIN:
8888
case UI_REQUEST.REQUEST_DEVICE_IN_BOOTLOADER_FOR_WEB_DEVICE:
89+
case UI_REQUEST.REQUEST_DEVICE_FOR_SWITCH_FIRMWARE_WEB_DEVICE:
8990
switch (deviceModel) {
9091
case 'classic':
9192
return enterPinOnClassic;

packages/connect-examples/expo-playground/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "onekey-hardware-playground",
3-
"version": "1.1.21-alpha.2",
3+
"version": "1.1.21-alpha.3",
44
"private": true,
55
"sideEffects": [
66
"app/utils/shim.js",
@@ -17,9 +17,9 @@
1717
},
1818
"dependencies": {
1919
"@noble/hashes": "^1.8.0",
20-
"@onekeyfe/hd-common-connect-sdk": "1.1.21-alpha.2",
21-
"@onekeyfe/hd-core": "1.1.21-alpha.2",
22-
"@onekeyfe/hd-shared": "1.1.21-alpha.2",
20+
"@onekeyfe/hd-common-connect-sdk": "1.1.21-alpha.3",
21+
"@onekeyfe/hd-core": "1.1.21-alpha.3",
22+
"@onekeyfe/hd-shared": "1.1.21-alpha.3",
2323
"@radix-ui/react-checkbox": "^1.3.2",
2424
"@radix-ui/react-dialog": "^1.1.14",
2525
"@radix-ui/react-dropdown-menu": "^2.1.15",

packages/core/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@onekeyfe/hd-core",
3-
"version": "1.1.21-alpha.2",
3+
"version": "1.1.21-alpha.3",
44
"description": "Core processes and APIs for communicating with OneKey hardware devices.",
55
"author": "OneKey",
66
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
@@ -25,8 +25,8 @@
2525
"url": "https://github.com/OneKeyHQ/hardware-js-sdk/issues"
2626
},
2727
"dependencies": {
28-
"@onekeyfe/hd-shared": "1.1.21-alpha.2",
29-
"@onekeyfe/hd-transport": "1.1.21-alpha.2",
28+
"@onekeyfe/hd-shared": "1.1.21-alpha.3",
29+
"@onekeyfe/hd-transport": "1.1.21-alpha.3",
3030
"axios": "1.12.2",
3131
"bignumber.js": "^9.0.2",
3232
"bytebuffer": "^5.0.1",

packages/core/src/api/FirmwareUpdateV3.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export const MIN_UPDATE_V3_BOOTLOADER_VERSION = '2.8.0';
4141
export default class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod<FirmwareUpdateV3Params> {
4242
checkPromise: Deferred<any> | null = null;
4343

44+
private isSwitchFirmware = false;
45+
4446
init() {
4547
this.allowDeviceMode = [UI_REQUEST.BOOTLOADER, UI_REQUEST.NOT_INITIALIZE];
4648
this.requireDeviceMode = [];
@@ -91,6 +93,7 @@ export default class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod<FirmwareU
9193

9294
const deviceFirmwareType = getFirmwareType(features);
9395
const firmwareType = this.params.firmwareType ?? deviceFirmwareType;
96+
this.isSwitchFirmware = firmwareType !== deviceFirmwareType;
9497

9598
let resourceBinary: ArrayBuffer | null = null;
9699
let fwBinaryMap: { fileName: string; binary: ArrayBuffer }[] = [];
@@ -468,6 +471,17 @@ export default class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod<FirmwareU
468471
return '';
469472
}
470473

474+
private canPromptWebUsbSwitchFirmwareReconnect(): boolean {
475+
if (!this.isSwitchFirmware) {
476+
return false;
477+
}
478+
return (
479+
DataManager.isBrowserWebUsb(DataManager.getSettings('env')) &&
480+
!this.payload.skipWebDevicePrompt &&
481+
this.device.listenerCount(DEVICE.SELECT_DEVICE_FOR_SWITCH_FIRMWARE_WEB_DEVICE) > 0
482+
);
483+
}
484+
471485
/**
472486
* @description Reconnect device - While update with bootloader, it will reconnect device
473487
* @param {number} timeout - The timeout for the reconnection
@@ -503,17 +517,14 @@ export default class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod<FirmwareU
503517
const deviceDiff = await this.device.deviceConnector?.enumerate();
504518
const devicesDescriptor = deviceDiff?.descriptors ?? [];
505519

506-
const canPromptWebUsbBootloader =
507-
DataManager.isBrowserWebUsb(DataManager.getSettings('env')) &&
508-
!this.payload.skipWebDevicePrompt &&
509-
this.device.listenerCount(DEVICE.SELECT_DEVICE_IN_BOOTLOADER_FOR_WEB_DEVICE) > 0;
520+
const canPromptSwitchFirmwareReconnect = this.canPromptWebUsbSwitchFirmwareReconnect();
510521

511-
if (canPromptWebUsbBootloader) {
522+
if (canPromptSwitchFirmwareReconnect) {
512523
webUsbCheckCount += 1;
513524
if (webUsbCheckCount > 4) {
514-
this.postTipMessage(FirmwareUpdateTipMessage.SelectDeviceInBootloaderForWebDevice);
525+
this.postTipMessage(FirmwareUpdateTipMessage.SwitchFirmwareReconnectDevice);
515526
try {
516-
await this._promptDeviceInBootloaderForWebDevice();
527+
await this._promptDeviceForSwitchFirmwareWebDevice();
517528
} catch (e) {
518529
Log.log('WebUSB re-authorization failed: ', e);
519530
}

packages/core/src/api/firmware/FirmwareUpdateBaseMethod.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,24 @@ export class FirmwareUpdateBaseMethod<Params> extends BaseMethod<Params> {
110110
});
111111
}
112112

113+
protected async _promptDeviceForSwitchFirmwareWebDevice() {
114+
return new Promise((resolve, reject) => {
115+
if (this.device.listenerCount(DEVICE.SELECT_DEVICE_FOR_SWITCH_FIRMWARE_WEB_DEVICE) > 0) {
116+
this.device.emit(
117+
DEVICE.SELECT_DEVICE_FOR_SWITCH_FIRMWARE_WEB_DEVICE,
118+
this.device,
119+
(err, deviceId) => {
120+
if (err) {
121+
reject(err);
122+
} else {
123+
resolve(deviceId);
124+
}
125+
}
126+
);
127+
}
128+
});
129+
}
130+
113131
checkDeviceToBootloader(connectId: string | undefined) {
114132
this.checkPromise = createDeferred();
115133
const env = DataManager.getSettings('env');

packages/core/src/core/index.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ const onCallDevice = async (
323323
DEVICE.SELECT_DEVICE_IN_BOOTLOADER_FOR_WEB_DEVICE,
324324
onSelectDeviceInBootloaderForWebDeviceHandler
325325
);
326+
device.on(
327+
DEVICE.SELECT_DEVICE_FOR_SWITCH_FIRMWARE_WEB_DEVICE,
328+
onSelectDeviceForSwitchFirmwareWebDeviceHandler
329+
);
326330

327331
try {
328332
if (method.connectId) {
@@ -1098,6 +1102,23 @@ const onSelectDeviceInBootloaderForWebDeviceHandler = async (
10981102
callback(null, uiResp.payload.deviceId);
10991103
};
11001104

1105+
const onSelectDeviceForSwitchFirmwareWebDeviceHandler = async (
1106+
...[device, callback]: [...DeviceEvents['select_device_for_switch_firmware_web_device']]
1107+
) => {
1108+
Log.debug('onSelectDeviceForSwitchFirmwareWebDeviceHandler');
1109+
const uiPromise = createUiPromise(
1110+
UI_RESPONSE.SELECT_DEVICE_FOR_SWITCH_FIRMWARE_WEB_DEVICE,
1111+
device
1112+
);
1113+
postMessage(
1114+
createUiMessage(UI_REQUEST.REQUEST_DEVICE_FOR_SWITCH_FIRMWARE_WEB_DEVICE, {
1115+
device: device.toMessageObject() as KnownDevice,
1116+
})
1117+
);
1118+
const uiResp = await uiPromise.promise;
1119+
callback(null, uiResp.payload.deviceId);
1120+
};
1121+
11011122
/**
11021123
* Emit message to listener (parent).
11031124
* Clear method reference from _callMethods
@@ -1169,7 +1190,8 @@ export default class Core extends EventEmitter {
11691190
switch (message.type) {
11701191
case UI_RESPONSE.RECEIVE_PIN:
11711192
case UI_RESPONSE.RECEIVE_PASSPHRASE:
1172-
case UI_RESPONSE.SELECT_DEVICE_IN_BOOTLOADER_FOR_WEB_DEVICE: {
1193+
case UI_RESPONSE.SELECT_DEVICE_IN_BOOTLOADER_FOR_WEB_DEVICE:
1194+
case UI_RESPONSE.SELECT_DEVICE_FOR_SWITCH_FIRMWARE_WEB_DEVICE: {
11731195
const uiPromise = findUiPromise(message.type);
11741196
if (uiPromise) {
11751197
Log.log('receive UI Response: ', message.type);

0 commit comments

Comments
 (0)