@onekeyfe/hd-core 1.2.0-alpha.54 → 1.2.0-alpha.55
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 +231 -0
- package/__tests__/DeviceCommands.test.ts +45 -0
- package/__tests__/DeviceEventRegistration.test.ts +6 -0
- package/__tests__/check-all-firmware-release-protocol-v2.test.ts +198 -3
- package/__tests__/connectSettings.test.ts +5 -47
- package/__tests__/device-lifecycle-events.test.ts +105 -3
- package/__tests__/deviceUploadNft.test.ts +293 -0
- package/__tests__/firmware-update/firmware-update-v2-download-before-boot.test.ts +31 -59
- package/__tests__/get-device-state.test.ts +132 -12
- package/__tests__/homescreen.test.ts +41 -1
- 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 +286 -0
- package/__tests__/protocol-v2-ui-lifecycle.test.ts +91 -0
- package/__tests__/protocol-v2-unlock-policy.test.ts +201 -35
- package/__tests__/protocol-v2.test.ts +1350 -1012
- 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__/uiProgressThrottle.test.ts +74 -0
- package/__tests__/uiPromiseRegistry.test.ts +86 -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 +0 -11
- package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
- package/dist/api/FirmwareUpdateV3.d.ts +3 -7
- package/dist/api/FirmwareUpdateV3.d.ts.map +1 -1
- package/dist/api/FirmwareUpdateV4.d.ts +11 -25
- 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/DeviceUnlock.d.ts +2 -2
- package/dist/api/device/DeviceUnlock.d.ts.map +1 -1
- package/dist/api/device/DeviceUpdateBootloader.d.ts +1 -5
- package/dist/api/device/DeviceUpdateBootloader.d.ts.map +1 -1
- package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts +0 -9
- package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts.map +1 -1
- package/dist/api/firmware/getBinary.d.ts +0 -7
- package/dist/api/firmware/getBinary.d.ts.map +1 -1
- package/dist/api/firmware/updateBootloader.d.ts +0 -2
- package/dist/api/firmware/updateBootloader.d.ts.map +1 -1
- package/dist/api/firmware/uploadFirmware.d.ts +1 -9
- package/dist/api/firmware/uploadFirmware.d.ts.map +1 -1
- package/dist/api/helpers/protocolV2FileWrite.d.ts +2 -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/KaspaSignTransaction.d.ts.map +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/stellar/StellarSignTransaction.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 +4 -0
- package/dist/core/uiPromiseRegistry.d.ts.map +1 -0
- package/dist/data-manager/DataManager.d.ts +4 -2
- package/dist/data-manager/DataManager.d.ts.map +1 -1
- package/dist/data-manager/connectSettings.d.ts.map +1 -1
- package/dist/device/Device.d.ts +29 -6
- 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/DevicePool.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 +214 -231
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2616 -3527
- 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/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 +2 -4
- 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/deviceUpdateBootloader.d.ts +0 -6
- package/dist/types/api/deviceUpdateBootloader.d.ts.map +1 -1
- package/dist/types/api/export.d.ts +2 -4
- package/dist/types/api/export.d.ts.map +1 -1
- package/dist/types/api/firmwareUpdate.d.ts +2 -70
- package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
- package/dist/types/api/index.d.ts +7 -10
- 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.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 +22 -13
- package/dist/types/settings.d.ts.map +1 -1
- package/dist/utils/deviceFeaturesCompat.d.ts +0 -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/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 +54 -57
- package/src/api/DetectDeviceConnectProtocol.ts +18 -0
- package/src/api/FirmwareUpdate.ts +7 -15
- package/src/api/FirmwareUpdateV2.ts +126 -316
- package/src/api/FirmwareUpdateV3.ts +63 -265
- package/src/api/FirmwareUpdateV4.ts +569 -965
- package/src/api/GetPassphraseState.ts +7 -1
- package/src/api/OpenWalletSession.ts +70 -17
- package/src/api/SearchDevices.ts +3 -1
- package/src/api/allnetwork/AllNetworkGetAddress.ts +21 -21
- package/src/api/allnetwork/AllNetworkGetAddressBase.ts +34 -11
- package/src/api/device/DeviceUnlock.ts +8 -3
- package/src/api/device/DeviceUpdateBootloader.ts +6 -122
- package/src/api/firmware/FirmwareUpdateBaseMethod.ts +16 -83
- package/src/api/firmware/updateBootloader.ts +4 -19
- package/src/api/firmware/uploadFirmware.ts +39 -144
- package/src/api/helpers/protocolV2FileWrite.ts +11 -4
- package/src/api/index.ts +2 -0
- package/src/api/kaspa/KaspaSignTransaction.ts +1 -12
- package/src/api/protocol-v2/DeviceUploadNft.ts +189 -0
- package/src/api/protocol-v2/ProtocolInfoRequest.ts +3 -1
- package/src/api/protocol-v2/helpers.ts +1 -0
- package/src/core/deviceEventRegistration.ts +4 -0
- package/src/core/index.ts +167 -83
- package/src/core/uiPromiseRegistry.ts +41 -0
- package/src/data/messages/messages-protocol-v2.json +25 -0
- package/src/data-manager/DataManager.ts +71 -39
- package/src/data-manager/connectSettings.ts +0 -11
- package/src/device/Device.ts +259 -43
- package/src/device/DeviceCommands.ts +7 -1
- package/src/device/DevicePool.ts +7 -2
- 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/index.ts +0 -5
- package/src/inject.ts +60 -24
- package/src/lowLevelInject.ts +7 -8
- package/src/protocols/protocol-v2/features.ts +10 -7
- package/src/protocols/protocol-v2/resources.ts +359 -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 +118 -0
- package/src/protocols/protocol-v2/walletSession.ts +214 -36
- package/src/topLevelInject.ts +7 -8
- package/src/types/api/checkAllFirmwareRelease.ts +2 -4
- package/src/types/api/detectDeviceConnectProtocol.ts +7 -0
- package/src/types/api/deviceUnlock.ts +12 -1
- package/src/types/api/deviceUpdateBootloader.ts +0 -6
- package/src/types/api/export.ts +1 -18
- package/src/types/api/firmwareUpdate.ts +3 -76
- package/src/types/api/index.ts +13 -14
- package/src/types/api/protocolV2.ts +13 -0
- package/src/types/device.ts +3 -0
- package/src/types/params.ts +7 -1
- package/src/types/settings.ts +42 -13
- package/src/utils/deviceFeaturesCompat.ts +0 -6
- package/src/utils/deviceFeaturesUtils.ts +8 -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/__tests__/device-initialize-timeout.test.ts +0 -56
- package/__tests__/firmware-update/check-all-firmware-release.test.ts +0 -175
- package/__tests__/firmware-update/firmware-artifact-source.test.ts +0 -119
- package/__tests__/firmware-update/firmware-host-binding.test.ts +0 -70
- package/__tests__/firmware-update/firmware-preparation-error.test.ts +0 -27
- package/__tests__/firmware-update/firmware-update-bootloader-poll.test.ts +0 -200
- package/__tests__/firmware-update/firmware-update-capabilities.test.ts +0 -13
- package/__tests__/firmware-update/firmware-update-plan-bootloader-identity.test.ts +0 -294
- package/__tests__/firmware-update/firmware-update-plan.test.ts +0 -593
- package/__tests__/firmware-update/firmware-update-prepared-plan.test.ts +0 -213
- package/__tests__/firmware-update/firmware-update-v3-reconnect.test.ts +0 -58
- package/__tests__/firmware-update/firmware-upload-source.test.ts +0 -70
- package/dist/api/firmware/FirmwareArtifactSource.d.ts +0 -25
- package/dist/api/firmware/FirmwareArtifactSource.d.ts.map +0 -1
- package/dist/api/firmware/FirmwareHostBinding.d.ts +0 -6
- package/dist/api/firmware/FirmwareHostBinding.d.ts.map +0 -1
- package/dist/api/firmware/FirmwarePreparationError.d.ts +0 -3
- package/dist/api/firmware/FirmwarePreparationError.d.ts.map +0 -1
- package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts +0 -3
- package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts.map +0 -1
- package/dist/api/firmware/FirmwareUpdatePlan.d.ts +0 -25
- package/dist/api/firmware/FirmwareUpdatePlan.d.ts.map +0 -1
- package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts +0 -32
- package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts.map +0 -1
- 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/dist/types/api/firmwareUpdateCapabilities.d.ts +0 -9
- package/dist/types/api/firmwareUpdateCapabilities.d.ts.map +0 -1
- package/dist/types/api/firmwareUpdatePlan.d.ts +0 -28
- package/dist/types/api/firmwareUpdatePlan.d.ts.map +0 -1
- package/dist/types/api/firmwareUpdatePreparedPlan.d.ts +0 -42
- package/dist/types/api/firmwareUpdatePreparedPlan.d.ts.map +0 -1
- package/src/api/firmware/FirmwareArtifactSource.ts +0 -278
- package/src/api/firmware/FirmwareHostBinding.ts +0 -100
- package/src/api/firmware/FirmwarePreparationError.ts +0 -12
- package/src/api/firmware/FirmwareUpdateCapabilities.ts +0 -9
- package/src/api/firmware/FirmwareUpdatePlan.ts +0 -772
- package/src/api/firmware/FirmwareUpdatePreparedPlan.ts +0 -415
- 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
- package/src/types/api/firmwareUpdateCapabilities.ts +0 -9
- package/src/types/api/firmwareUpdatePlan.ts +0 -38
- package/src/types/api/firmwareUpdatePreparedPlan.ts +0 -53
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { UI_REQUEST, createUiMessage } from '../../events';
|
|
2
2
|
|
|
3
|
-
import type { CoreMessage, ProtocolV2UiCompletion } from '../../events';
|
|
3
|
+
import type { CoreMessage, HardwareUiInteractionMeta, ProtocolV2UiCompletion } from '../../events';
|
|
4
4
|
import type { Device } from '../../device/Device';
|
|
5
5
|
import type { KnownDevice } from '../../types';
|
|
6
6
|
|
|
@@ -28,7 +28,15 @@ type ProtocolV2InteractionMethod = {
|
|
|
28
28
|
protocolV2UiInteraction?: ProtocolV2InteractionDescriptor;
|
|
29
29
|
};
|
|
30
30
|
|
|
31
|
-
type InteractionDevice = Pick<Device, 'isProtocolV2' | 'toMessageObject'
|
|
31
|
+
type InteractionDevice = Pick<Device, 'isProtocolV2' | 'toMessageObject'> &
|
|
32
|
+
Partial<
|
|
33
|
+
Pick<
|
|
34
|
+
Device,
|
|
35
|
+
| 'createProtocolV2UiPhaseMetadata'
|
|
36
|
+
| 'finishProtocolV2UiInteraction'
|
|
37
|
+
| 'hasOpenProtocolV2UiInteraction'
|
|
38
|
+
>
|
|
39
|
+
>;
|
|
32
40
|
type PostMessage = (message: CoreMessage) => void;
|
|
33
41
|
|
|
34
42
|
export const isProtocolV2UiEnabled = (method: { protocolV2UiMode?: 'auto' | 'none' }) =>
|
|
@@ -156,12 +164,17 @@ export class ProtocolV2UiInteractionCoordinator {
|
|
|
156
164
|
this.methodInteraction = interaction;
|
|
157
165
|
if (!interaction) return;
|
|
158
166
|
const { request, ...metadata } = interaction;
|
|
167
|
+
const interactionMeta = this.device.createProtocolV2UiPhaseMetadata?.(
|
|
168
|
+
request === 'pin' ? 'pin' : 'button',
|
|
169
|
+
'start'
|
|
170
|
+
);
|
|
159
171
|
|
|
160
172
|
this.emit(
|
|
161
173
|
request === 'pin' ? UI_REQUEST.REQUEST_PIN : UI_REQUEST.REQUEST_BUTTON,
|
|
162
174
|
{
|
|
163
175
|
device: this.device.toMessageObject() as KnownDevice,
|
|
164
176
|
...metadata,
|
|
177
|
+
...(interactionMeta ? { interaction: interactionMeta } : {}),
|
|
165
178
|
},
|
|
166
179
|
`method:${request}:${interaction.reason}:${interaction.page ?? ''}:${
|
|
167
180
|
interaction.operation ?? ''
|
|
@@ -169,7 +182,8 @@ export class ProtocolV2UiInteractionCoordinator {
|
|
|
169
182
|
);
|
|
170
183
|
}
|
|
171
184
|
|
|
172
|
-
enterUnlockInteraction(method?: string) {
|
|
185
|
+
enterUnlockInteraction(method?: string): HardwareUiInteractionMeta | undefined {
|
|
186
|
+
const interaction = this.device.createProtocolV2UiPhaseMetadata?.('pin', 'start');
|
|
173
187
|
this.emit(
|
|
174
188
|
UI_REQUEST.REQUEST_PIN,
|
|
175
189
|
{
|
|
@@ -178,19 +192,45 @@ export class ProtocolV2UiInteractionCoordinator {
|
|
|
178
192
|
reason: 'device-locked',
|
|
179
193
|
deviceOnly: true,
|
|
180
194
|
method,
|
|
195
|
+
...(interaction ? { interaction } : {}),
|
|
181
196
|
},
|
|
182
197
|
`unlock:${method ?? ''}`
|
|
183
198
|
);
|
|
199
|
+
return interaction;
|
|
184
200
|
}
|
|
185
201
|
|
|
186
202
|
resumeMethodInteraction() {
|
|
187
203
|
this.enterMethodInteraction(this.methodInteraction);
|
|
188
204
|
}
|
|
189
205
|
|
|
190
|
-
close(
|
|
191
|
-
|
|
206
|
+
close(
|
|
207
|
+
options: {
|
|
208
|
+
ensureOperationClose?: boolean;
|
|
209
|
+
protocolV2Operation?: boolean;
|
|
210
|
+
} = {}
|
|
211
|
+
): boolean {
|
|
212
|
+
if (
|
|
213
|
+
(!options.protocolV2Operation && !this.device.isProtocolV2()) ||
|
|
214
|
+
(!options.ensureOperationClose &&
|
|
215
|
+
!this.opened &&
|
|
216
|
+
!this.device.hasOpenProtocolV2UiInteraction?.()) ||
|
|
217
|
+
this.closed
|
|
218
|
+
)
|
|
219
|
+
return false;
|
|
192
220
|
this.closed = true;
|
|
193
|
-
this.
|
|
221
|
+
const interaction = this.device.finishProtocolV2UiInteraction?.('succeeded', {
|
|
222
|
+
ensureMetadata: options.ensureOperationClose,
|
|
223
|
+
});
|
|
224
|
+
const device = this.device.toMessageObject();
|
|
225
|
+
this.postMessage(
|
|
226
|
+
interaction
|
|
227
|
+
? createUiMessage(UI_REQUEST.CLOSE_UI_WINDOW, {
|
|
228
|
+
...interaction,
|
|
229
|
+
...(device ? { device } : {}),
|
|
230
|
+
})
|
|
231
|
+
: createUiMessage(UI_REQUEST.CLOSE_UI_WINDOW)
|
|
232
|
+
);
|
|
233
|
+
return true;
|
|
194
234
|
}
|
|
195
235
|
|
|
196
236
|
private emit(
|
|
@@ -1,105 +1 @@
|
|
|
1
|
-
export type UnlockPolicy = 'none' | 'unlock-before-run'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Protocol V2 methods that are safe to replay after a locked response.
|
|
5
|
-
*
|
|
6
|
-
* Keep this as an exact allowlist. Name-pattern matching can silently make a new
|
|
7
|
-
* state-changing method replayable when it happens to share a suffix.
|
|
8
|
-
*/
|
|
9
|
-
export const PROTOCOL_V2_RETRY_ON_LOCKED_METHODS = [
|
|
10
|
-
'cipherKeyValue',
|
|
11
|
-
'allNetworkGetAddress',
|
|
12
|
-
'allNetworkGetAddressByLoop',
|
|
13
|
-
'btcGetAddress',
|
|
14
|
-
'btcGetPublicKey',
|
|
15
|
-
'btcSignMessage',
|
|
16
|
-
'btcSignPsbt',
|
|
17
|
-
'btcSignTransaction',
|
|
18
|
-
'btcVerifyMessage',
|
|
19
|
-
'confluxGetAddress',
|
|
20
|
-
'confluxSignMessage',
|
|
21
|
-
'confluxSignTransaction',
|
|
22
|
-
'evmGetAddress',
|
|
23
|
-
'evmGetPublicKey',
|
|
24
|
-
'evmSignMessage',
|
|
25
|
-
'evmSignTransaction',
|
|
26
|
-
'evmSignTypedData',
|
|
27
|
-
'evmVerifyMessage',
|
|
28
|
-
'starcoinGetAddress',
|
|
29
|
-
'starcoinGetPublicKey',
|
|
30
|
-
'starcoinSignMessage',
|
|
31
|
-
'starcoinSignTransaction',
|
|
32
|
-
'starcoinVerifyMessage',
|
|
33
|
-
'nemGetAddress',
|
|
34
|
-
'nemSignTransaction',
|
|
35
|
-
'solGetAddress',
|
|
36
|
-
'solSignTransaction',
|
|
37
|
-
'solSignOffchainMessage',
|
|
38
|
-
'solSignMessage',
|
|
39
|
-
'stellarGetAddress',
|
|
40
|
-
'stellarSignTransaction',
|
|
41
|
-
'tronGetAddress',
|
|
42
|
-
'tronSignMessage',
|
|
43
|
-
'tronSignTransaction',
|
|
44
|
-
'nearGetAddress',
|
|
45
|
-
'nearSignTransaction',
|
|
46
|
-
'aptosGetAddress',
|
|
47
|
-
'aptosGetPublicKey',
|
|
48
|
-
'aptosSignTransaction',
|
|
49
|
-
'aptosSignMessage',
|
|
50
|
-
'aptosSignInMessage',
|
|
51
|
-
'algoGetAddress',
|
|
52
|
-
'algoSignTransaction',
|
|
53
|
-
'cosmosGetAddress',
|
|
54
|
-
'cosmosGetPublicKey',
|
|
55
|
-
'cosmosSignTransaction',
|
|
56
|
-
'xrpGetAddress',
|
|
57
|
-
'xrpSignTransaction',
|
|
58
|
-
'suiGetAddress',
|
|
59
|
-
'suiGetPublicKey',
|
|
60
|
-
'suiSignMessage',
|
|
61
|
-
'suiSignTransaction',
|
|
62
|
-
'cardanoGetAddress',
|
|
63
|
-
'cardanoGetPublicKey',
|
|
64
|
-
'cardanoSignTransaction',
|
|
65
|
-
'cardanoSignMessage',
|
|
66
|
-
'filecoinGetAddress',
|
|
67
|
-
'filecoinSignTransaction',
|
|
68
|
-
'polkadotGetAddress',
|
|
69
|
-
'polkadotSignTransaction',
|
|
70
|
-
'kaspaGetAddress',
|
|
71
|
-
'kaspaSignTransaction',
|
|
72
|
-
'nexaGetAddress',
|
|
73
|
-
'nexaSignTransaction',
|
|
74
|
-
'nostrGetPublicKey',
|
|
75
|
-
'nostrSignEvent',
|
|
76
|
-
'nostrEncryptMessage',
|
|
77
|
-
'nostrDecryptMessage',
|
|
78
|
-
'nostrSignSchnorr',
|
|
79
|
-
'lnurlAuth',
|
|
80
|
-
'nervosGetAddress',
|
|
81
|
-
'nervosSignTransaction',
|
|
82
|
-
'dnxGetAddress',
|
|
83
|
-
'dnxSignTransaction',
|
|
84
|
-
'tonGetAddress',
|
|
85
|
-
'tonSignMessage',
|
|
86
|
-
'tonSignProof',
|
|
87
|
-
'tonSignData',
|
|
88
|
-
'scdoGetAddress',
|
|
89
|
-
'scdoSignTransaction',
|
|
90
|
-
'scdoSignMessage',
|
|
91
|
-
'alephiumGetAddress',
|
|
92
|
-
'alephiumSignTransaction',
|
|
93
|
-
'alephiumSignMessage',
|
|
94
|
-
'benfenGetAddress',
|
|
95
|
-
'benfenGetPublicKey',
|
|
96
|
-
'benfenSignMessage',
|
|
97
|
-
'benfenSignTransaction',
|
|
98
|
-
'neoGetAddress',
|
|
99
|
-
'neoSignTransaction',
|
|
100
|
-
] as const;
|
|
101
|
-
|
|
102
|
-
const retryOnLockedMethods = new Set<string>(PROTOCOL_V2_RETRY_ON_LOCKED_METHODS);
|
|
103
|
-
|
|
104
|
-
export const getProtocolV2UnlockPolicy = (methodName: string): UnlockPolicy =>
|
|
105
|
-
retryOnLockedMethods.has(methodName) ? 'retry-on-locked' : 'none';
|
|
1
|
+
export type UnlockPolicy = 'none' | 'unlock-before-run';
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
2
|
+
import { DeviceSessionPinType } from '@onekeyfe/hd-transport';
|
|
3
|
+
|
|
4
|
+
import { LoggerNames, getLogger } from '../../utils';
|
|
5
|
+
import { isProtocolV2UiEnabled, resolveProtocolV2UiInteraction } from './uiInteraction';
|
|
6
|
+
import { refreshProtocolV2DeviceStatus } from './walletSession';
|
|
7
|
+
|
|
8
|
+
import type { BaseMethod } from '../../api/BaseMethod';
|
|
9
|
+
import type { Device } from '../../device/Device';
|
|
10
|
+
import type { HardwareUiInteractionMeta } from '../../events';
|
|
11
|
+
import type { ProtocolV2UiInteractionCoordinator } from './uiInteraction';
|
|
12
|
+
|
|
13
|
+
const Log = getLogger(LoggerNames.Core);
|
|
14
|
+
|
|
15
|
+
export type ProtocolV2UnlockContext = {
|
|
16
|
+
preflightCompleted: boolean;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const createProtocolV2UnlockContext = (): ProtocolV2UnlockContext => ({
|
|
20
|
+
preflightCompleted: false,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
type RunnableMethod = Pick<
|
|
24
|
+
BaseMethod,
|
|
25
|
+
| 'run'
|
|
26
|
+
| 'unlockPolicy'
|
|
27
|
+
| 'useDevicePassphraseState'
|
|
28
|
+
| 'protocolV2UiInteraction'
|
|
29
|
+
| 'protocolV2UiMode'
|
|
30
|
+
| 'params'
|
|
31
|
+
| 'payload'
|
|
32
|
+
> & {
|
|
33
|
+
name?: string;
|
|
34
|
+
protocolV2UnlockContext?: ProtocolV2UnlockContext;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
type UiInteractionCoordinator = Pick<
|
|
38
|
+
ProtocolV2UiInteractionCoordinator,
|
|
39
|
+
'enterMethodInteraction' | 'enterUnlockInteraction'
|
|
40
|
+
>;
|
|
41
|
+
|
|
42
|
+
type RunMethodWithUnlockPolicyOptions<T> = {
|
|
43
|
+
context?: ProtocolV2UnlockContext;
|
|
44
|
+
uiCoordinator?: UiInteractionCoordinator;
|
|
45
|
+
afterStatusBeforeUnlock?: () => void | Promise<void>;
|
|
46
|
+
prepare?: () => Promise<void>;
|
|
47
|
+
run?: () => Promise<T>;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export async function runMethodWithUnlockPolicy<T = unknown>(
|
|
51
|
+
method: RunnableMethod,
|
|
52
|
+
device: Device,
|
|
53
|
+
options: RunMethodWithUnlockPolicyOptions<T> = {}
|
|
54
|
+
): Promise<T> {
|
|
55
|
+
const {
|
|
56
|
+
context = createProtocolV2UnlockContext(),
|
|
57
|
+
uiCoordinator,
|
|
58
|
+
afterStatusBeforeUnlock,
|
|
59
|
+
prepare,
|
|
60
|
+
run: configuredRun,
|
|
61
|
+
} = options;
|
|
62
|
+
const run = configuredRun ?? (() => method.run() as Promise<T>);
|
|
63
|
+
method.protocolV2UnlockContext = context;
|
|
64
|
+
|
|
65
|
+
const shouldEmitUi = isProtocolV2UiEnabled(method);
|
|
66
|
+
const shouldCoordinateUi = shouldEmitUi && uiCoordinator !== undefined;
|
|
67
|
+
const requiresPreUnlock =
|
|
68
|
+
device.isProtocolV2() &&
|
|
69
|
+
(method.useDevicePassphraseState || method.unlockPolicy === 'unlock-before-run') &&
|
|
70
|
+
!device.isBootloader?.() &&
|
|
71
|
+
!device.isRomloader?.();
|
|
72
|
+
|
|
73
|
+
if (requiresPreUnlock && !context.preflightCompleted) {
|
|
74
|
+
const status = await refreshProtocolV2DeviceStatus(device);
|
|
75
|
+
if (typeof status?.unlocked !== 'boolean') {
|
|
76
|
+
throw ERRORS.TypedError(
|
|
77
|
+
HardwareErrorCode.RuntimeError,
|
|
78
|
+
'Protocol V2 DeviceStatus did not report an explicit unlock state.'
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
await afterStatusBeforeUnlock?.();
|
|
83
|
+
|
|
84
|
+
if (!status.unlocked) {
|
|
85
|
+
const unlockInteraction: HardwareUiInteractionMeta | undefined = shouldCoordinateUi
|
|
86
|
+
? uiCoordinator.enterUnlockInteraction(method.name)
|
|
87
|
+
: undefined;
|
|
88
|
+
const unlockedStatus = await device.unlockDevice(
|
|
89
|
+
DeviceSessionPinType.Main,
|
|
90
|
+
shouldCoordinateUi
|
|
91
|
+
? { emitUiEvent: false, interaction: unlockInteraction }
|
|
92
|
+
: {
|
|
93
|
+
source: 'unlock-coordinator',
|
|
94
|
+
reason: 'device-locked',
|
|
95
|
+
deviceOnly: true,
|
|
96
|
+
method: method.name,
|
|
97
|
+
}
|
|
98
|
+
);
|
|
99
|
+
if (unlockedStatus?.unlocked !== true) {
|
|
100
|
+
throw ERRORS.TypedError(
|
|
101
|
+
HardwareErrorCode.RuntimeError,
|
|
102
|
+
'Protocol V2 device remained locked after the unlock flow.'
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
Log.debug('Protocol V2 pre-unlock completed', { method: method.name });
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
context.preflightCompleted = true;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
await prepare?.();
|
|
112
|
+
|
|
113
|
+
if (shouldCoordinateUi) {
|
|
114
|
+
uiCoordinator.enterMethodInteraction(resolveProtocolV2UiInteraction(method));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return run();
|
|
118
|
+
}
|