@onekeyfe/hd-core 1.2.0-alpha.67 → 1.2.0-alpha.68
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/AllNetworkGetAddressBase.tracing.test.ts +313 -0
- package/__tests__/DeviceCommands.test.ts +59 -13
- package/__tests__/DeviceEventRegistration.test.ts +6 -0
- package/__tests__/check-all-firmware-release-protocol-v2.test.ts +198 -3
- package/__tests__/core-initialization.test.ts +23 -0
- package/__tests__/device-connector-protocol.test.ts +81 -0
- package/__tests__/device-lifecycle-events.test.ts +105 -3
- package/__tests__/device-pool-state.test.ts +22 -0
- package/__tests__/device-settings.test.ts +39 -18
- package/__tests__/device-state-contract.test.ts +1 -0
- package/__tests__/device-state-mapper.test.ts +13 -1
- package/__tests__/device-utils.test.ts +48 -1
- package/__tests__/deviceSettings.test.ts +11 -1
- package/__tests__/deviceUploadNft.test.ts +316 -0
- package/__tests__/firmware-update/firmware-update-v3-reconnect.test.ts +5 -0
- package/__tests__/get-device-state.test.ts +132 -12
- package/__tests__/homescreen.test.ts +41 -1
- package/__tests__/kaspa-use-tweak-pro2.test.ts +2 -2
- package/__tests__/kaspaSignTransaction.test.ts +3 -26
- package/__tests__/logBlockEvent.test.ts +1 -1
- package/__tests__/method-protocol-support.test.ts +19 -0
- package/__tests__/open-wallet-session.test.ts +568 -37
- package/__tests__/pro2Nft.test.ts +108 -0
- package/__tests__/protocol-binding.test.ts +89 -0
- package/__tests__/protocol-v2-resources.test.ts +340 -0
- package/__tests__/protocol-v2-ui-lifecycle.test.ts +91 -0
- package/__tests__/protocol-v2-unlock-policy.test.ts +300 -37
- package/__tests__/protocol-v2.test.ts +1449 -867
- package/__tests__/protocolV2FileWrite.test.ts +152 -0
- package/__tests__/protocolV2UiInteraction.test.ts +63 -0
- package/__tests__/public-pro2-api-boundary.test.ts +1 -0
- package/__tests__/search-devices.test.ts +12 -3
- package/__tests__/tron-sign-message-init.test.ts +2 -2
- package/__tests__/uiProgressThrottle.test.ts +74 -0
- package/__tests__/uiPromiseRegistry.test.ts +115 -0
- package/dist/api/BaseMethod.d.ts +3 -1
- package/dist/api/BaseMethod.d.ts.map +1 -1
- package/dist/api/CheckAllFirmwareRelease.d.ts +2 -1
- package/dist/api/CheckAllFirmwareRelease.d.ts.map +1 -1
- package/dist/api/DetectDeviceConnectProtocol.d.ts +7 -0
- package/dist/api/DetectDeviceConnectProtocol.d.ts.map +1 -0
- package/dist/api/FirmwareUpdate.d.ts.map +1 -1
- package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
- package/dist/api/FirmwareUpdateV3.d.ts.map +1 -1
- package/dist/api/FirmwareUpdateV4.d.ts +11 -4
- package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
- package/dist/api/GetPassphraseState.d.ts.map +1 -1
- package/dist/api/OpenWalletSession.d.ts.map +1 -1
- package/dist/api/SearchDevices.d.ts.map +1 -1
- package/dist/api/allnetwork/AllNetworkGetAddressBase.d.ts.map +1 -1
- package/dist/api/device/DeviceSettings.d.ts.map +1 -1
- package/dist/api/device/DeviceUnlock.d.ts +2 -2
- package/dist/api/device/DeviceUnlock.d.ts.map +1 -1
- package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts +4 -3
- package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts.map +1 -1
- package/dist/api/firmware/uploadFirmware.d.ts.map +1 -1
- package/dist/api/helpers/protocolV2FileWrite.d.ts +9 -0
- package/dist/api/helpers/protocolV2FileWrite.d.ts.map +1 -1
- package/dist/api/index.d.ts +2 -0
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/kaspa/KaspaGetAddress.d.ts +1 -1
- package/dist/api/kaspa/KaspaSignTransaction.d.ts +1 -1
- package/dist/api/kaspa/KaspaSignTransaction.d.ts.map +1 -1
- package/dist/api/polkadot/PolkadotGetAddress.d.ts +1 -1
- package/dist/api/polkadot/networks.d.ts +1 -1
- package/dist/api/protocol-v2/DeviceUploadNft.d.ts +31 -0
- package/dist/api/protocol-v2/DeviceUploadNft.d.ts.map +1 -0
- package/dist/api/protocol-v2/ProtocolInfoRequest.d.ts.map +1 -1
- package/dist/api/protocol-v2/helpers.d.ts.map +1 -1
- package/dist/api/solana/SolSignMessage.d.ts +1 -1
- package/dist/api/solana/SolSignOffchainMessage.d.ts +1 -1
- package/dist/api/solana/SolSignTransaction.d.ts +2 -2
- package/dist/api/sui/SuiGetAddress.d.ts +1 -1
- package/dist/api/sui/SuiGetPublicKey.d.ts +1 -1
- package/dist/api/sui/SuiSignMessage.d.ts +1 -1
- package/dist/api/sui/SuiSignTransaction.d.ts +1 -1
- package/dist/api/ton/TonGetAddress.d.ts +1 -1
- package/dist/api/ton/TonSignMessage.d.ts +1 -1
- package/dist/api/ton/TonSignProof.d.ts +1 -1
- package/dist/api/tron/TronSignMessage.d.ts +2 -2
- package/dist/api/tron/TronSignTransaction.d.ts +1 -1
- package/dist/core/deviceEventRegistration.d.ts +2 -0
- package/dist/core/deviceEventRegistration.d.ts.map +1 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/uiPromiseRegistry.d.ts +6 -0
- package/dist/core/uiPromiseRegistry.d.ts.map +1 -0
- package/dist/data-manager/DataManager.d.ts +9 -3
- package/dist/data-manager/DataManager.d.ts.map +1 -1
- package/dist/data-manager/TransportManager.d.ts.map +1 -1
- package/dist/device/Device.d.ts +29 -5
- package/dist/device/Device.d.ts.map +1 -1
- package/dist/device/DeviceCommands.d.ts +4 -4
- package/dist/device/DeviceCommands.d.ts.map +1 -1
- package/dist/device/DeviceConnector.d.ts +2 -1
- package/dist/device/DeviceConnector.d.ts.map +1 -1
- package/dist/device/DevicePool.d.ts.map +1 -1
- package/dist/deviceProfile/protocolV2DeviceIdentity.d.ts.map +1 -1
- package/dist/events/device.d.ts +4 -0
- package/dist/events/device.d.ts.map +1 -1
- package/dist/events/logBlockEvent.d.ts.map +1 -1
- package/dist/events/ui-promise.d.ts +4 -2
- package/dist/events/ui-promise.d.ts.map +1 -1
- package/dist/events/ui-request.d.ts +31 -2
- package/dist/events/ui-request.d.ts.map +1 -1
- package/dist/events/ui-response.d.ts +10 -2
- package/dist/events/ui-response.d.ts.map +1 -1
- package/dist/index.d.ts +243 -69
- package/dist/index.js +2563 -1217
- package/dist/inject.d.ts +6 -1
- package/dist/inject.d.ts.map +1 -1
- package/dist/lowLevelInject.d.ts.map +1 -1
- package/dist/protocols/protocol-v2/features.d.ts +0 -4
- package/dist/protocols/protocol-v2/features.d.ts.map +1 -1
- package/dist/protocols/protocol-v2/firmware.d.ts.map +1 -1
- package/dist/protocols/protocol-v2/resources.d.ts +30 -0
- package/dist/protocols/protocol-v2/resources.d.ts.map +1 -0
- package/dist/protocols/protocol-v2/uiInteraction.d.ts +7 -4
- package/dist/protocols/protocol-v2/uiInteraction.d.ts.map +1 -1
- package/dist/protocols/protocol-v2/unlockPolicy.d.ts +1 -3
- package/dist/protocols/protocol-v2/unlockPolicy.d.ts.map +1 -1
- package/dist/protocols/protocol-v2/unlockPolicyRunner.d.ts +22 -0
- package/dist/protocols/protocol-v2/unlockPolicyRunner.d.ts.map +1 -0
- package/dist/protocols/protocol-v2/walletSession.d.ts +6 -1
- package/dist/protocols/protocol-v2/walletSession.d.ts.map +1 -1
- package/dist/topLevelInject.d.ts.map +1 -1
- package/dist/types/api/checkAllFirmwareRelease.d.ts +3 -1
- package/dist/types/api/checkAllFirmwareRelease.d.ts.map +1 -1
- package/dist/types/api/detectDeviceConnectProtocol.d.ts +4 -0
- package/dist/types/api/detectDeviceConnectProtocol.d.ts.map +1 -0
- package/dist/types/api/deviceUnlock.d.ts +5 -1
- package/dist/types/api/deviceUnlock.d.ts.map +1 -1
- package/dist/types/api/export.d.ts +1 -0
- package/dist/types/api/export.d.ts.map +1 -1
- package/dist/types/api/firmwareUpdate.d.ts +4 -2
- package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
- package/dist/types/api/index.d.ts +6 -1
- package/dist/types/api/index.d.ts.map +1 -1
- package/dist/types/api/protocolV2.d.ts +3 -0
- package/dist/types/api/protocolV2.d.ts.map +1 -1
- package/dist/types/device.d.ts +2 -2
- package/dist/types/device.d.ts.map +1 -1
- package/dist/types/params.d.ts +1 -0
- package/dist/types/params.d.ts.map +1 -1
- package/dist/types/settings.d.ts +38 -8
- package/dist/types/settings.d.ts.map +1 -1
- package/dist/utils/deviceFeaturesCompat.d.ts.map +1 -1
- package/dist/utils/deviceFeaturesUtils.d.ts +4 -0
- package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
- package/dist/utils/deviceInfoUtils.d.ts.map +1 -1
- package/dist/utils/deviceSettings.d.ts.map +1 -1
- package/dist/utils/homescreen.d.ts +4 -0
- package/dist/utils/homescreen.d.ts.map +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/patch.d.ts +1 -1
- package/dist/utils/patch.d.ts.map +1 -1
- package/dist/utils/pro2Nft.d.ts +31 -0
- package/dist/utils/pro2Nft.d.ts.map +1 -0
- package/dist/utils/pro2Wallpaper.d.ts +10 -0
- package/dist/utils/pro2Wallpaper.d.ts.map +1 -1
- package/dist/utils/uiProgressThrottle.d.ts +3 -0
- package/dist/utils/uiProgressThrottle.d.ts.map +1 -0
- package/package.json +4 -4
- package/src/api/BaseMethod.ts +8 -10
- package/src/api/CheckAllFirmwareRelease.ts +61 -11
- package/src/api/DetectDeviceConnectProtocol.ts +18 -0
- package/src/api/FirmwareUpdate.ts +6 -2
- package/src/api/FirmwareUpdateV2.ts +5 -2
- package/src/api/FirmwareUpdateV3.ts +6 -2
- package/src/api/FirmwareUpdateV4.ts +463 -258
- package/src/api/GetPassphraseState.ts +7 -1
- package/src/api/OpenWalletSession.ts +71 -17
- package/src/api/SearchDevices.ts +3 -1
- package/src/api/allnetwork/AllNetworkGetAddress.ts +21 -21
- package/src/api/allnetwork/AllNetworkGetAddressBase.ts +36 -11
- package/src/api/device/DeviceSettings.ts +5 -4
- package/src/api/device/DeviceUnlock.ts +8 -3
- package/src/api/firmware/FirmwareUpdateBaseMethod.ts +27 -12
- package/src/api/firmware/uploadFirmware.ts +17 -4
- package/src/api/helpers/protocolV2FileWrite.ts +202 -63
- package/src/api/index.ts +2 -0
- package/src/api/kaspa/KaspaGetAddress.ts +1 -1
- package/src/api/kaspa/KaspaSignTransaction.ts +2 -13
- package/src/api/polkadot/networks.ts +3 -3
- package/src/api/protocol-v2/DeviceUploadNft.ts +197 -0
- package/src/api/protocol-v2/ProtocolInfoRequest.ts +3 -1
- package/src/api/protocol-v2/helpers.ts +1 -1
- package/src/api/solana/SolSignMessage.ts +1 -1
- package/src/api/solana/SolSignOffchainMessage.ts +1 -1
- package/src/api/solana/SolSignTransaction.ts +2 -2
- package/src/api/sui/SuiGetAddress.ts +1 -1
- package/src/api/sui/SuiGetPublicKey.ts +1 -1
- package/src/api/sui/SuiSignMessage.ts +1 -1
- package/src/api/sui/SuiSignTransaction.ts +1 -1
- package/src/api/ton/TonGetAddress.ts +1 -1
- package/src/api/ton/TonSignMessage.ts +1 -1
- package/src/api/ton/TonSignProof.ts +1 -1
- package/src/api/tron/TronSignMessage.ts +2 -2
- package/src/api/tron/TronSignTransaction.ts +1 -1
- package/src/core/deviceEventRegistration.ts +4 -0
- package/src/core/index.ts +213 -111
- package/src/core/uiPromiseRegistry.ts +63 -0
- package/src/data/messages/messages-protocol-v2.json +25 -0
- package/src/data-manager/DataManager.ts +88 -11
- package/src/data-manager/TransportManager.ts +6 -0
- package/src/device/Device.ts +273 -43
- package/src/device/DeviceCommands.ts +31 -4
- package/src/device/DeviceConnector.ts +33 -13
- package/src/device/DevicePool.ts +21 -7
- package/src/deviceProfile/buildDeviceFeatures.ts +2 -2
- package/src/deviceProfile/protocolV2DeviceIdentity.ts +3 -0
- package/src/events/device.ts +4 -0
- package/src/events/logBlockEvent.ts +1 -0
- package/src/events/ui-promise.ts +4 -2
- package/src/events/ui-request.ts +36 -2
- package/src/events/ui-response.ts +12 -2
- package/src/inject.ts +58 -2
- package/src/lowLevelInject.ts +6 -3
- package/src/protocols/protocol-v2/features.ts +10 -7
- package/src/protocols/protocol-v2/firmware.ts +2 -0
- package/src/protocols/protocol-v2/resources.ts +390 -0
- package/src/protocols/protocol-v2/uiInteraction.ts +46 -6
- package/src/protocols/protocol-v2/unlockPolicy.ts +1 -105
- package/src/protocols/protocol-v2/unlockPolicyRunner.ts +131 -0
- package/src/protocols/protocol-v2/walletSession.ts +214 -36
- package/src/topLevelInject.ts +6 -3
- package/src/types/api/checkAllFirmwareRelease.ts +3 -1
- package/src/types/api/detectDeviceConnectProtocol.ts +6 -0
- package/src/types/api/deviceUnlock.ts +12 -1
- package/src/types/api/export.ts +1 -0
- package/src/types/api/firmwareUpdate.ts +6 -3
- package/src/types/api/index.ts +13 -0
- package/src/types/api/protocolV2.ts +13 -0
- package/src/types/device.ts +15 -3
- package/src/types/params.ts +7 -1
- package/src/types/settings.ts +60 -9
- package/src/utils/deviceFeaturesCompat.ts +5 -0
- package/src/utils/deviceFeaturesUtils.ts +14 -3
- package/src/utils/deviceInfoUtils.ts +2 -0
- package/src/utils/deviceSettings.ts +3 -0
- package/src/utils/homescreen.ts +32 -6
- package/src/utils/index.ts +6 -1
- package/src/utils/pro2Nft.ts +142 -0
- package/src/utils/pro2Wallpaper.ts +35 -8
- package/src/utils/uiProgressThrottle.ts +63 -0
- package/dist/api/firmware/progressThrottle.d.ts +0 -3
- package/dist/api/firmware/progressThrottle.d.ts.map +0 -1
- package/dist/protocols/protocol-v2/lockedError.d.ts +0 -2
- package/dist/protocols/protocol-v2/lockedError.d.ts.map +0 -1
- package/dist/protocols/protocol-v2/unlockRetry.d.ts +0 -10
- package/dist/protocols/protocol-v2/unlockRetry.d.ts.map +0 -1
- package/src/api/firmware/progressThrottle.ts +0 -44
- package/src/protocols/protocol-v2/lockedError.ts +0 -10
- package/src/protocols/protocol-v2/unlockRetry.ts +0 -109
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { UI_EVENT, UI_REQUEST } from '../events/ui-request';
|
|
2
|
+
|
|
3
|
+
import type { CoreMessage } from '../events';
|
|
4
|
+
|
|
5
|
+
const DEFAULT_UI_PROGRESS_INTERVAL_MS = 250;
|
|
6
|
+
|
|
7
|
+
type UiProgressThrottleState = {
|
|
8
|
+
lastEmittedAt: number;
|
|
9
|
+
lastProgress?: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const normalizeProgress = (progress: unknown) => {
|
|
13
|
+
if (typeof progress !== 'number' || !Number.isFinite(progress)) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return Math.max(0, Math.min(100, Math.floor(progress)));
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const createUiProgressMessageFilter = (
|
|
21
|
+
intervalMs = DEFAULT_UI_PROGRESS_INTERVAL_MS,
|
|
22
|
+
now = Date.now
|
|
23
|
+
) => {
|
|
24
|
+
const stateByType = new Map<string, UiProgressThrottleState>();
|
|
25
|
+
|
|
26
|
+
return (message: CoreMessage) => {
|
|
27
|
+
if (
|
|
28
|
+
!('event' in message) ||
|
|
29
|
+
message.event !== UI_EVENT ||
|
|
30
|
+
(message.type !== UI_REQUEST.DEVICE_PROGRESS && message.type !== UI_REQUEST.FIRMWARE_PROGRESS)
|
|
31
|
+
) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const key =
|
|
36
|
+
message.type === UI_REQUEST.FIRMWARE_PROGRESS
|
|
37
|
+
? `${message.type}:${message.payload.progressType}`
|
|
38
|
+
: message.type;
|
|
39
|
+
const progress = normalizeProgress(message.payload.progress);
|
|
40
|
+
const currentTime = now();
|
|
41
|
+
const state = stateByType.get(key);
|
|
42
|
+
|
|
43
|
+
if (!state) {
|
|
44
|
+
stateByType.set(key, { lastEmittedAt: currentTime, lastProgress: progress });
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const isBoundary = progress === 0 || progress === 100;
|
|
49
|
+
if (isBoundary && progress === state.lastProgress) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const startsNewPhase =
|
|
54
|
+
progress !== undefined && state.lastProgress !== undefined && progress < state.lastProgress;
|
|
55
|
+
|
|
56
|
+
if (!isBoundary && !startsNewPhase && currentTime - state.lastEmittedAt < intervalMs) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
stateByType.set(key, { lastEmittedAt: currentTime, lastProgress: progress });
|
|
61
|
+
return true;
|
|
62
|
+
};
|
|
63
|
+
};
|
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import type { IFirmwareUpdateProgressType } from '../../events/ui-request';
|
|
2
|
-
export declare const createFirmwareProgressThrottle: (intervalMs?: number) => (progress: number, progressType: IFirmwareUpdateProgressType) => boolean;
|
|
3
|
-
//# sourceMappingURL=progressThrottle.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"progressThrottle.d.ts","sourceRoot":"","sources":["../../../src/api/firmware/progressThrottle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,yBAAyB,CAAC;AAS3E,eAAO,MAAM,8BAA8B,sCAKvB,MAAM,gBAAgB,2BAA2B,YA6BpE,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"lockedError.d.ts","sourceRoot":"","sources":["../../../src/protocols/protocol-v2/lockedError.ts"],"names":[],"mappings":"AAEA,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAO3D"}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { BaseMethod } from '../../api/BaseMethod';
|
|
2
|
-
import type { Device } from '../../device/Device';
|
|
3
|
-
import type { ProtocolV2UiInteractionCoordinator } from './uiInteraction';
|
|
4
|
-
type RunnableMethod = Pick<BaseMethod, 'run' | 'unlockPolicy' | 'protocolV2UiInteraction' | 'protocolV2UiMode' | 'params' | 'payload' | 'useDevicePassphraseState'> & {
|
|
5
|
-
name?: string;
|
|
6
|
-
};
|
|
7
|
-
type UiInteractionCoordinator = Pick<ProtocolV2UiInteractionCoordinator, 'enterMethodInteraction' | 'enterUnlockInteraction' | 'resumeMethodInteraction'>;
|
|
8
|
-
export declare function runMethodWithUnlockRetry(method: RunnableMethod, device: Device, uiCoordinator?: UiInteractionCoordinator): Promise<any>;
|
|
9
|
-
export {};
|
|
10
|
-
//# sourceMappingURL=unlockRetry.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"unlockRetry.d.ts","sourceRoot":"","sources":["../../../src/protocols/protocol-v2/unlockRetry.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,KAAK,EAAE,kCAAkC,EAAE,MAAM,iBAAiB,CAAC;AAI1E,KAAK,cAAc,GAAG,IAAI,CACxB,UAAU,EACR,KAAK,GACL,cAAc,GACd,yBAAyB,GACzB,kBAAkB,GAClB,QAAQ,GACR,SAAS,GACT,0BAA0B,CAC7B,GAAG;IAAE,IAAI,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AACtB,KAAK,wBAAwB,GAAG,IAAI,CAClC,kCAAkC,EAClC,wBAAwB,GAAG,wBAAwB,GAAG,yBAAyB,CAChF,CAAC;AAkBF,wBAAsB,wBAAwB,CAC5C,MAAM,EAAE,cAAc,EACtB,MAAM,EAAE,MAAM,EACd,aAAa,CAAC,EAAE,wBAAwB,gBA+DzC"}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import type { IFirmwareUpdateProgressType } from '../../events/ui-request';
|
|
2
|
-
|
|
3
|
-
const DEFAULT_FIRMWARE_PROGRESS_INTERVAL_MS = 250;
|
|
4
|
-
|
|
5
|
-
type FirmwareProgressThrottleState = {
|
|
6
|
-
lastEmittedAt: number;
|
|
7
|
-
lastProgress?: number;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export const createFirmwareProgressThrottle = (
|
|
11
|
-
intervalMs = DEFAULT_FIRMWARE_PROGRESS_INTERVAL_MS
|
|
12
|
-
) => {
|
|
13
|
-
const stateByType = new Map<IFirmwareUpdateProgressType, FirmwareProgressThrottleState>();
|
|
14
|
-
|
|
15
|
-
return (progress: number, progressType: IFirmwareUpdateProgressType) => {
|
|
16
|
-
if (progressType !== 'transferData') {
|
|
17
|
-
return true;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const normalizedProgress = Math.max(0, Math.min(100, Math.floor(progress)));
|
|
21
|
-
if (normalizedProgress === 0 || normalizedProgress === 100) {
|
|
22
|
-
stateByType.set(progressType, {
|
|
23
|
-
lastEmittedAt: Date.now(),
|
|
24
|
-
lastProgress: normalizedProgress,
|
|
25
|
-
});
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const now = Date.now();
|
|
30
|
-
const state = stateByType.get(progressType);
|
|
31
|
-
if (
|
|
32
|
-
state &&
|
|
33
|
-
(state.lastProgress === normalizedProgress || now - state.lastEmittedAt < intervalMs)
|
|
34
|
-
) {
|
|
35
|
-
return false;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
stateByType.set(progressType, {
|
|
39
|
-
lastEmittedAt: now,
|
|
40
|
-
lastProgress: normalizedProgress,
|
|
41
|
-
});
|
|
42
|
-
return true;
|
|
43
|
-
};
|
|
44
|
-
};
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
2
|
-
|
|
3
|
-
export function isDeviceLockedError(error: unknown): boolean {
|
|
4
|
-
return (
|
|
5
|
-
typeof error === 'object' &&
|
|
6
|
-
error !== null &&
|
|
7
|
-
'errorCode' in error &&
|
|
8
|
-
error.errorCode === HardwareErrorCode.DeviceLocked
|
|
9
|
-
);
|
|
10
|
-
}
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { LoggerNames, getLogger } from '../../utils';
|
|
2
|
-
import { isDeviceLockedError } from './lockedError';
|
|
3
|
-
import { isProtocolV2UiEnabled, resolveProtocolV2UiInteraction } from './uiInteraction';
|
|
4
|
-
import { refreshProtocolV2DeviceStatus, restoreProtocolV2WalletSession } from './walletSession';
|
|
5
|
-
|
|
6
|
-
import type { BaseMethod } from '../../api/BaseMethod';
|
|
7
|
-
import type { Device } from '../../device/Device';
|
|
8
|
-
import type { ProtocolV2UiInteractionCoordinator } from './uiInteraction';
|
|
9
|
-
|
|
10
|
-
const Log = getLogger(LoggerNames.Core);
|
|
11
|
-
|
|
12
|
-
type RunnableMethod = Pick<
|
|
13
|
-
BaseMethod,
|
|
14
|
-
| 'run'
|
|
15
|
-
| 'unlockPolicy'
|
|
16
|
-
| 'protocolV2UiInteraction'
|
|
17
|
-
| 'protocolV2UiMode'
|
|
18
|
-
| 'params'
|
|
19
|
-
| 'payload'
|
|
20
|
-
| 'useDevicePassphraseState'
|
|
21
|
-
> & { name?: string };
|
|
22
|
-
type UiInteractionCoordinator = Pick<
|
|
23
|
-
ProtocolV2UiInteractionCoordinator,
|
|
24
|
-
'enterMethodInteraction' | 'enterUnlockInteraction' | 'resumeMethodInteraction'
|
|
25
|
-
>;
|
|
26
|
-
|
|
27
|
-
const restoreExpectedWalletSessionAfterUnlock = async (method: RunnableMethod, device: Device) => {
|
|
28
|
-
const expectedPassphraseState = method.payload?.useEmptyPassphrase
|
|
29
|
-
? undefined
|
|
30
|
-
: method.payload?.passphraseState ?? device.passphraseState;
|
|
31
|
-
if (
|
|
32
|
-
!method.useDevicePassphraseState ||
|
|
33
|
-
typeof expectedPassphraseState !== 'string' ||
|
|
34
|
-
expectedPassphraseState.length === 0
|
|
35
|
-
) {
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
await restoreProtocolV2WalletSession(device, expectedPassphraseState);
|
|
40
|
-
Log.debug('Protocol V2 wallet session restored after unlock', { method: method.name });
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export async function runMethodWithUnlockRetry(
|
|
44
|
-
method: RunnableMethod,
|
|
45
|
-
device: Device,
|
|
46
|
-
uiCoordinator?: UiInteractionCoordinator
|
|
47
|
-
) {
|
|
48
|
-
const shouldEmitUi = isProtocolV2UiEnabled(method);
|
|
49
|
-
const isProtocolV2 = device.isProtocolV2();
|
|
50
|
-
const requiresFreshStatus =
|
|
51
|
-
isProtocolV2 &&
|
|
52
|
-
method.unlockPolicy === 'unlock-before-run' &&
|
|
53
|
-
!device.isBootloader?.() &&
|
|
54
|
-
!device.isRomloader?.();
|
|
55
|
-
|
|
56
|
-
if (requiresFreshStatus) {
|
|
57
|
-
await refreshProtocolV2DeviceStatus(device);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
const shouldUnlockBeforeRun =
|
|
61
|
-
isProtocolV2 &&
|
|
62
|
-
method.unlockPolicy !== 'none' &&
|
|
63
|
-
!device.isBootloader?.() &&
|
|
64
|
-
!device.isRomloader?.() &&
|
|
65
|
-
device.features?.unlocked === false;
|
|
66
|
-
|
|
67
|
-
if (shouldUnlockBeforeRun) {
|
|
68
|
-
if (shouldEmitUi) {
|
|
69
|
-
uiCoordinator?.enterUnlockInteraction(method.name);
|
|
70
|
-
}
|
|
71
|
-
await device.unlockDevice();
|
|
72
|
-
Log.debug('Protocol V2 pre-unlock completed', { method: method.name });
|
|
73
|
-
await restoreExpectedWalletSessionAfterUnlock(method, device);
|
|
74
|
-
if (shouldEmitUi) {
|
|
75
|
-
uiCoordinator?.enterMethodInteraction(resolveProtocolV2UiInteraction(method));
|
|
76
|
-
}
|
|
77
|
-
return method.run();
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
if (shouldEmitUi) {
|
|
81
|
-
uiCoordinator?.enterMethodInteraction(resolveProtocolV2UiInteraction(method));
|
|
82
|
-
}
|
|
83
|
-
try {
|
|
84
|
-
return await method.run();
|
|
85
|
-
} catch (error) {
|
|
86
|
-
if (!isProtocolV2 || method.unlockPolicy !== 'retry-on-locked' || !isDeviceLockedError(error)) {
|
|
87
|
-
throw error;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
Log.debug('Protocol V2 unlock retry triggered', { method: method.name });
|
|
91
|
-
if (shouldEmitUi) {
|
|
92
|
-
uiCoordinator?.enterUnlockInteraction(method.name);
|
|
93
|
-
}
|
|
94
|
-
await device.unlockDevice();
|
|
95
|
-
Log.debug('Protocol V2 unlock completed', { method: method.name });
|
|
96
|
-
await restoreExpectedWalletSessionAfterUnlock(method, device);
|
|
97
|
-
if (shouldEmitUi) {
|
|
98
|
-
uiCoordinator?.resumeMethodInteraction();
|
|
99
|
-
}
|
|
100
|
-
try {
|
|
101
|
-
const response = await method.run();
|
|
102
|
-
Log.debug('Protocol V2 method retry completed', { method: method.name, success: true });
|
|
103
|
-
return response;
|
|
104
|
-
} catch (retryError) {
|
|
105
|
-
Log.debug('Protocol V2 method retry completed', { method: method.name, success: false });
|
|
106
|
-
throw retryError;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}
|