@onekeyfe/hd-core 1.2.0-alpha.66 → 1.2.0-alpha.67
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 +0 -313
- package/__tests__/DeviceCommands.test.ts +13 -59
- package/__tests__/DeviceEventRegistration.test.ts +0 -6
- package/__tests__/check-all-firmware-release-protocol-v2.test.ts +3 -198
- package/__tests__/connectSettings.test.ts +47 -5
- package/__tests__/device-initialize-timeout.test.ts +56 -0
- package/__tests__/device-lifecycle-events.test.ts +3 -105
- package/__tests__/device-pool-state.test.ts +0 -22
- package/__tests__/device-settings.test.ts +18 -39
- package/__tests__/device-state-contract.test.ts +0 -1
- package/__tests__/device-state-mapper.test.ts +1 -13
- package/__tests__/device-utils.test.ts +1 -48
- package/__tests__/deviceSettings.test.ts +1 -11
- package/__tests__/firmware-update/check-all-firmware-release.test.ts +175 -0
- package/__tests__/firmware-update/device-update-bootloader.test.ts +87 -0
- package/__tests__/firmware-update/firmware-artifact-source.test.ts +119 -0
- package/__tests__/firmware-update/firmware-host-binding.test.ts +70 -0
- package/__tests__/firmware-update/firmware-preparation-error.test.ts +27 -0
- package/__tests__/firmware-update/firmware-update-bootloader-poll.test.ts +200 -0
- package/__tests__/firmware-update/firmware-update-capabilities.test.ts +13 -0
- package/__tests__/firmware-update/firmware-update-plan-bootloader-identity.test.ts +394 -0
- package/__tests__/firmware-update/firmware-update-plan.test.ts +593 -0
- package/__tests__/firmware-update/firmware-update-prepared-plan.test.ts +231 -0
- package/__tests__/firmware-update/firmware-update-v2-download-before-boot.test.ts +59 -31
- package/__tests__/firmware-update/firmware-update-v3-reconnect.test.ts +58 -0
- package/__tests__/firmware-update/firmware-upload-source.test.ts +70 -0
- package/__tests__/get-device-state.test.ts +12 -132
- package/__tests__/homescreen.test.ts +1 -41
- package/__tests__/kaspa-use-tweak-pro2.test.ts +2 -2
- package/__tests__/kaspaSignTransaction.test.ts +26 -3
- package/__tests__/logBlockEvent.test.ts +1 -1
- package/__tests__/method-protocol-support.test.ts +0 -19
- package/__tests__/open-wallet-session.test.ts +37 -568
- package/__tests__/protocol-v2-unlock-policy.test.ts +37 -300
- package/__tests__/protocol-v2.test.ts +1153 -1458
- package/__tests__/protocolV2FileWrite.test.ts +0 -152
- package/__tests__/protocolV2UiInteraction.test.ts +0 -63
- package/__tests__/public-pro2-api-boundary.test.ts +0 -1
- package/__tests__/search-devices.test.ts +3 -12
- package/__tests__/tron-sign-message-init.test.ts +2 -2
- package/dist/api/BaseMethod.d.ts +1 -3
- package/dist/api/BaseMethod.d.ts.map +1 -1
- package/dist/api/CheckAllFirmwareRelease.d.ts +1 -2
- package/dist/api/CheckAllFirmwareRelease.d.ts.map +1 -1
- package/dist/api/FirmwareUpdate.d.ts.map +1 -1
- package/dist/api/FirmwareUpdateV2.d.ts +10 -0
- package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
- package/dist/api/FirmwareUpdateV3.d.ts +7 -3
- package/dist/api/FirmwareUpdateV3.d.ts.map +1 -1
- package/dist/api/FirmwareUpdateV4.d.ts +27 -13
- 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/device/DeviceUpdateBootloader.d.ts +5 -1
- package/dist/api/device/DeviceUpdateBootloader.d.ts.map +1 -1
- package/dist/api/firmware/FirmwareArtifactSource.d.ts +25 -0
- package/dist/api/firmware/FirmwareArtifactSource.d.ts.map +1 -0
- package/dist/api/firmware/FirmwareHostBinding.d.ts +6 -0
- package/dist/api/firmware/FirmwareHostBinding.d.ts.map +1 -0
- package/dist/api/firmware/FirmwarePreparationError.d.ts +3 -0
- package/dist/api/firmware/FirmwarePreparationError.d.ts.map +1 -0
- package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts +11 -4
- package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts.map +1 -1
- package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts +3 -0
- package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts.map +1 -0
- package/dist/api/firmware/FirmwareUpdatePlan.d.ts +25 -0
- package/dist/api/firmware/FirmwareUpdatePlan.d.ts.map +1 -0
- package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts +32 -0
- package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts.map +1 -0
- package/dist/api/firmware/getBinary.d.ts +7 -0
- package/dist/api/firmware/getBinary.d.ts.map +1 -1
- package/dist/api/firmware/progressThrottle.d.ts +3 -0
- package/dist/api/firmware/progressThrottle.d.ts.map +1 -0
- package/dist/api/firmware/updateBootloader.d.ts +2 -0
- package/dist/api/firmware/updateBootloader.d.ts.map +1 -1
- package/dist/api/firmware/uploadFirmware.d.ts +9 -1
- package/dist/api/firmware/uploadFirmware.d.ts.map +1 -1
- package/dist/api/helpers/protocolV2FileWrite.d.ts +0 -9
- package/dist/api/helpers/protocolV2FileWrite.d.ts.map +1 -1
- package/dist/api/index.d.ts +0 -2
- 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/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/stellar/StellarSignTransaction.d.ts +1 -1
- 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 +0 -2
- package/dist/core/deviceEventRegistration.d.ts.map +1 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/data-manager/DataManager.d.ts +4 -9
- package/dist/data-manager/DataManager.d.ts.map +1 -1
- package/dist/data-manager/TransportManager.d.ts.map +1 -1
- package/dist/data-manager/connectSettings.d.ts.map +1 -1
- package/dist/device/Device.d.ts +6 -29
- 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 +1 -2
- 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 +0 -4
- 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 +2 -4
- package/dist/events/ui-promise.d.ts.map +1 -1
- package/dist/events/ui-request.d.ts +2 -31
- package/dist/events/ui-request.d.ts.map +1 -1
- package/dist/events/ui-response.d.ts +2 -10
- package/dist/events/ui-response.d.ts.map +1 -1
- package/dist/index.d.ts +247 -243
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3773 -3054
- package/dist/inject.d.ts +1 -6
- package/dist/inject.d.ts.map +1 -1
- package/dist/lowLevelInject.d.ts.map +1 -1
- package/dist/protocols/protocol-v2/features.d.ts +4 -0
- 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/lockedError.d.ts +2 -0
- package/dist/protocols/protocol-v2/lockedError.d.ts.map +1 -0
- package/dist/protocols/protocol-v2/uiInteraction.d.ts +4 -7
- package/dist/protocols/protocol-v2/uiInteraction.d.ts.map +1 -1
- package/dist/protocols/protocol-v2/unlockPolicy.d.ts +3 -1
- package/dist/protocols/protocol-v2/unlockPolicy.d.ts.map +1 -1
- package/dist/protocols/protocol-v2/unlockRetry.d.ts +10 -0
- package/dist/protocols/protocol-v2/unlockRetry.d.ts.map +1 -0
- package/dist/protocols/protocol-v2/walletSession.d.ts +1 -6
- 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 +5 -3
- package/dist/types/api/checkAllFirmwareRelease.d.ts.map +1 -1
- package/dist/types/api/deviceUnlock.d.ts +1 -5
- package/dist/types/api/deviceUnlock.d.ts.map +1 -1
- package/dist/types/api/deviceUpdateBootloader.d.ts +6 -0
- package/dist/types/api/deviceUpdateBootloader.d.ts.map +1 -1
- package/dist/types/api/export.d.ts +4 -2
- package/dist/types/api/export.d.ts.map +1 -1
- package/dist/types/api/firmwareUpdate.d.ts +70 -4
- package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
- package/dist/types/api/firmwareUpdateCapabilities.d.ts +9 -0
- package/dist/types/api/firmwareUpdateCapabilities.d.ts.map +1 -0
- package/dist/types/api/firmwareUpdatePlan.d.ts +28 -0
- package/dist/types/api/firmwareUpdatePlan.d.ts.map +1 -0
- package/dist/types/api/firmwareUpdatePreparedPlan.d.ts +42 -0
- package/dist/types/api/firmwareUpdatePreparedPlan.d.ts.map +1 -0
- package/dist/types/api/index.d.ts +10 -7
- package/dist/types/api/index.d.ts.map +1 -1
- package/dist/types/api/protocolV2.d.ts +0 -3
- 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 +0 -1
- package/dist/types/params.d.ts.map +1 -1
- package/dist/types/settings.d.ts +21 -38
- package/dist/types/settings.d.ts.map +1 -1
- package/dist/utils/deviceFeaturesCompat.d.ts +1 -0
- package/dist/utils/deviceFeaturesCompat.d.ts.map +1 -1
- package/dist/utils/deviceFeaturesUtils.d.ts +0 -4
- 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 +0 -4
- 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/pro2Wallpaper.d.ts +0 -10
- package/dist/utils/pro2Wallpaper.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/api/BaseMethod.ts +10 -8
- package/src/api/CheckAllFirmwareRelease.ts +60 -59
- package/src/api/FirmwareUpdate.ts +15 -7
- package/src/api/FirmwareUpdateV2.ts +298 -126
- package/src/api/FirmwareUpdateV3.ts +265 -63
- package/src/api/FirmwareUpdateV4.ts +1017 -602
- package/src/api/GetPassphraseState.ts +1 -7
- package/src/api/OpenWalletSession.ts +17 -71
- package/src/api/SearchDevices.ts +1 -3
- package/src/api/allnetwork/AllNetworkGetAddress.ts +21 -21
- package/src/api/allnetwork/AllNetworkGetAddressBase.ts +11 -36
- package/src/api/device/DeviceSettings.ts +4 -5
- package/src/api/device/DeviceUnlock.ts +3 -8
- package/src/api/device/DeviceUpdateBootloader.ts +125 -6
- package/src/api/firmware/FirmwareArtifactSource.ts +278 -0
- package/src/api/firmware/FirmwareHostBinding.ts +100 -0
- package/src/api/firmware/FirmwarePreparationError.ts +12 -0
- package/src/api/firmware/FirmwareUpdateBaseMethod.ts +83 -27
- package/src/api/firmware/FirmwareUpdateCapabilities.ts +9 -0
- package/src/api/firmware/FirmwareUpdatePlan.ts +772 -0
- package/src/api/firmware/FirmwareUpdatePreparedPlan.ts +449 -0
- package/src/api/firmware/progressThrottle.ts +44 -0
- package/src/api/firmware/updateBootloader.ts +19 -4
- package/src/api/firmware/uploadFirmware.ts +144 -39
- package/src/api/helpers/protocolV2FileWrite.ts +63 -202
- package/src/api/index.ts +0 -2
- package/src/api/kaspa/KaspaGetAddress.ts +1 -1
- package/src/api/kaspa/KaspaSignTransaction.ts +13 -2
- package/src/api/polkadot/networks.ts +3 -3
- package/src/api/protocol-v2/ProtocolInfoRequest.ts +1 -3
- 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 +0 -4
- package/src/core/index.ts +111 -213
- package/src/data/messages/messages-protocol-v2.json +0 -25
- package/src/data-manager/DataManager.ts +41 -113
- package/src/data-manager/TransportManager.ts +0 -6
- package/src/data-manager/connectSettings.ts +11 -0
- package/src/device/Device.ts +49 -274
- package/src/device/DeviceCommands.ts +4 -31
- package/src/device/DeviceConnector.ts +13 -33
- package/src/device/DevicePool.ts +7 -21
- package/src/deviceProfile/buildDeviceFeatures.ts +2 -2
- package/src/deviceProfile/protocolV2DeviceIdentity.ts +0 -3
- package/src/events/device.ts +0 -4
- package/src/events/logBlockEvent.ts +0 -1
- package/src/events/ui-promise.ts +2 -4
- package/src/events/ui-request.ts +2 -36
- package/src/events/ui-response.ts +2 -12
- package/src/index.ts +5 -0
- package/src/inject.ts +24 -60
- package/src/lowLevelInject.ts +8 -7
- package/src/protocols/protocol-v2/features.ts +7 -10
- package/src/protocols/protocol-v2/firmware.ts +0 -2
- package/src/protocols/protocol-v2/lockedError.ts +10 -0
- package/src/protocols/protocol-v2/uiInteraction.ts +6 -46
- package/src/protocols/protocol-v2/unlockPolicy.ts +105 -1
- package/src/protocols/protocol-v2/unlockRetry.ts +109 -0
- package/src/protocols/protocol-v2/walletSession.ts +36 -214
- package/src/topLevelInject.ts +8 -7
- package/src/types/api/checkAllFirmwareRelease.ts +5 -3
- package/src/types/api/deviceUnlock.ts +1 -12
- package/src/types/api/deviceUpdateBootloader.ts +6 -0
- package/src/types/api/export.ts +18 -1
- package/src/types/api/firmwareUpdate.ts +78 -6
- package/src/types/api/firmwareUpdateCapabilities.ts +9 -0
- package/src/types/api/firmwareUpdatePlan.ts +38 -0
- package/src/types/api/firmwareUpdatePreparedPlan.ts +53 -0
- package/src/types/api/index.ts +14 -13
- package/src/types/api/protocolV2.ts +0 -13
- package/src/types/device.ts +3 -15
- package/src/types/params.ts +1 -7
- package/src/types/settings.ts +22 -60
- package/src/utils/deviceFeaturesCompat.ts +6 -5
- package/src/utils/deviceFeaturesUtils.ts +3 -14
- package/src/utils/deviceInfoUtils.ts +0 -2
- package/src/utils/deviceSettings.ts +0 -3
- package/src/utils/homescreen.ts +6 -32
- package/src/utils/index.ts +1 -6
- package/src/utils/pro2Wallpaper.ts +8 -35
- package/__tests__/core-initialization.test.ts +0 -23
- package/__tests__/device-connector-protocol.test.ts +0 -81
- package/__tests__/deviceUploadNft.test.ts +0 -316
- package/__tests__/pro2Nft.test.ts +0 -108
- package/__tests__/protocol-binding.test.ts +0 -89
- package/__tests__/protocol-v2-resources.test.ts +0 -340
- package/__tests__/protocol-v2-ui-lifecycle.test.ts +0 -91
- package/__tests__/uiProgressThrottle.test.ts +0 -74
- package/__tests__/uiPromiseRegistry.test.ts +0 -115
- package/dist/api/DetectDeviceConnectProtocol.d.ts +0 -7
- package/dist/api/DetectDeviceConnectProtocol.d.ts.map +0 -1
- package/dist/api/protocol-v2/DeviceUploadNft.d.ts +0 -31
- package/dist/api/protocol-v2/DeviceUploadNft.d.ts.map +0 -1
- package/dist/core/uiPromiseRegistry.d.ts +0 -6
- package/dist/core/uiPromiseRegistry.d.ts.map +0 -1
- package/dist/protocols/protocol-v2/resources.d.ts +0 -30
- package/dist/protocols/protocol-v2/resources.d.ts.map +0 -1
- package/dist/protocols/protocol-v2/unlockPolicyRunner.d.ts +0 -22
- package/dist/protocols/protocol-v2/unlockPolicyRunner.d.ts.map +0 -1
- package/dist/types/api/detectDeviceConnectProtocol.d.ts +0 -4
- package/dist/types/api/detectDeviceConnectProtocol.d.ts.map +0 -1
- package/dist/utils/pro2Nft.d.ts +0 -31
- package/dist/utils/pro2Nft.d.ts.map +0 -1
- package/dist/utils/uiProgressThrottle.d.ts +0 -3
- package/dist/utils/uiProgressThrottle.d.ts.map +0 -1
- package/src/api/DetectDeviceConnectProtocol.ts +0 -18
- package/src/api/protocol-v2/DeviceUploadNft.ts +0 -197
- package/src/core/uiPromiseRegistry.ts +0 -63
- package/src/protocols/protocol-v2/resources.ts +0 -390
- package/src/protocols/protocol-v2/unlockPolicyRunner.ts +0 -131
- package/src/types/api/detectDeviceConnectProtocol.ts +0 -6
- package/src/utils/pro2Nft.ts +0 -142
- package/src/utils/uiProgressThrottle.ts +0 -63
|
@@ -1,390 +0,0 @@
|
|
|
1
|
-
import { sha256 } from '@noble/hashes/sha256';
|
|
2
|
-
import { PROTOCOL_V2_BLE_FILE_READ_CHUNK_SIZE } from '@onekeyfe/hd-transport';
|
|
3
|
-
|
|
4
|
-
import type {
|
|
5
|
-
IProtocolV2BootResources,
|
|
6
|
-
IProtocolV2Resource,
|
|
7
|
-
IProtocolV2ResourceFile,
|
|
8
|
-
IProtocolV2ResourceType,
|
|
9
|
-
IProtocolV2Resources,
|
|
10
|
-
} from '../../types';
|
|
11
|
-
import type { DeviceCommands } from '../../device/DeviceCommands';
|
|
12
|
-
|
|
13
|
-
export const PROTOCOL_V2_RESOURCE_TYPES = [
|
|
14
|
-
'images',
|
|
15
|
-
'animation',
|
|
16
|
-
'wallpaper',
|
|
17
|
-
'translations',
|
|
18
|
-
'roobert',
|
|
19
|
-
'noto',
|
|
20
|
-
] as const satisfies readonly IProtocolV2ResourceType[];
|
|
21
|
-
|
|
22
|
-
export const PROTOCOL_V2_RESOURCE_DEVICE_PATHS: Readonly<Record<IProtocolV2ResourceType, string>> =
|
|
23
|
-
{
|
|
24
|
-
images: 'vol0:/bundles/images/images.okpkg',
|
|
25
|
-
animation: 'vol0:/bundles/images/animation.okpkg',
|
|
26
|
-
wallpaper: 'vol0:/bundles/images/wallpaper.okpkg',
|
|
27
|
-
translations: 'vol0:/bundles/translations/translations.okpkg',
|
|
28
|
-
roobert: 'vol0:/bundles/font/roobert.okpkg',
|
|
29
|
-
noto: 'vol0:/bundles/font/noto.okpkg',
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const RESOURCE_TYPE_SET = new Set<string>(PROTOCOL_V2_RESOURCE_TYPES);
|
|
33
|
-
const SHA256_HEX_LENGTH = 64;
|
|
34
|
-
const SHA3_512_HEX_LENGTH = 128;
|
|
35
|
-
const PROTOCOL_V2_OKPP_HEADER_SIZE = 0x52a0;
|
|
36
|
-
const PROTOCOL_V2_OKPP_TYPE_OFFSET = 0x08;
|
|
37
|
-
const PROTOCOL_V2_OKPP_HEADER_LENGTH_OFFSET = 0x0c;
|
|
38
|
-
const PROTOCOL_V2_OKPP_HEADER_HASH_OFFSET = 0x240;
|
|
39
|
-
const PROTOCOL_V2_OKPP_HASH_SIZE = 64;
|
|
40
|
-
const PROTOCOL_V2_RESOURCE_IDENTITY_READ_SIZE =
|
|
41
|
-
PROTOCOL_V2_OKPP_HEADER_HASH_OFFSET + PROTOCOL_V2_OKPP_HASH_SIZE;
|
|
42
|
-
const PROTOCOL_V2_MIN_FILE_READ_CHUNK_SIZE = 64;
|
|
43
|
-
export const PROTOCOL_V2_RESOURCE_INVENTORY_TIMEOUT_MS = 5 * 1000;
|
|
44
|
-
|
|
45
|
-
export type ProtocolV2ResourceInventoryItem = {
|
|
46
|
-
type: IProtocolV2ResourceType;
|
|
47
|
-
size: number;
|
|
48
|
-
headerHash: string;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export type ProtocolV2ResourceUpdateMode = 'application' | 'bootloader-recovery';
|
|
52
|
-
|
|
53
|
-
export type ProtocolV2ResourceUpdatePlan = {
|
|
54
|
-
status: 'valid' | 'outdated' | 'unknown';
|
|
55
|
-
resources: IProtocolV2Resource[];
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
function toFiniteNumber(value: unknown): number | undefined {
|
|
59
|
-
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
|
60
|
-
if (typeof value === 'string') {
|
|
61
|
-
const numeric = Number(value);
|
|
62
|
-
return Number.isFinite(numeric) ? numeric : undefined;
|
|
63
|
-
}
|
|
64
|
-
if (value && typeof value === 'object') {
|
|
65
|
-
const longLike = value as { toNumber?: () => number };
|
|
66
|
-
if (typeof longLike.toNumber === 'function') {
|
|
67
|
-
const numeric = longLike.toNumber();
|
|
68
|
-
return Number.isFinite(numeric) ? numeric : undefined;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
return undefined;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function toUint8Array(value: unknown): Uint8Array {
|
|
75
|
-
if (value instanceof Uint8Array) return value;
|
|
76
|
-
if (value instanceof ArrayBuffer) return new Uint8Array(value);
|
|
77
|
-
if (ArrayBuffer.isView(value)) {
|
|
78
|
-
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
79
|
-
}
|
|
80
|
-
if (typeof value === 'string') {
|
|
81
|
-
const hex = value.replace(/^0x/i, '');
|
|
82
|
-
if (!hex || hex.length % 2 !== 0 || /[^0-9a-f]/i.test(hex)) {
|
|
83
|
-
return new Uint8Array(0);
|
|
84
|
-
}
|
|
85
|
-
const bytes = new Uint8Array(hex.length / 2);
|
|
86
|
-
for (let index = 0; index < bytes.length; index += 1) {
|
|
87
|
-
bytes[index] = Number.parseInt(hex.slice(index * 2, index * 2 + 2), 16);
|
|
88
|
-
}
|
|
89
|
-
return bytes;
|
|
90
|
-
}
|
|
91
|
-
return new Uint8Array(0);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function readAscii(bytes: Uint8Array, offset: number, length: number): string {
|
|
95
|
-
return Array.from(bytes.slice(offset, offset + length), byte => String.fromCharCode(byte)).join(
|
|
96
|
-
''
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function parseProtocolV2ResourceHeaderHash(bytes: Uint8Array): string | undefined {
|
|
101
|
-
if (bytes.byteLength < PROTOCOL_V2_RESOURCE_IDENTITY_READ_SIZE) return undefined;
|
|
102
|
-
if (readAscii(bytes, 0, 4) !== 'OKPP') return undefined;
|
|
103
|
-
if (readAscii(bytes, PROTOCOL_V2_OKPP_TYPE_OFFSET, 4) !== 'RESC') return undefined;
|
|
104
|
-
|
|
105
|
-
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
106
|
-
if (
|
|
107
|
-
view.getUint32(PROTOCOL_V2_OKPP_HEADER_LENGTH_OFFSET, true) !== PROTOCOL_V2_OKPP_HEADER_SIZE
|
|
108
|
-
) {
|
|
109
|
-
return undefined;
|
|
110
|
-
}
|
|
111
|
-
return bytesToHex(
|
|
112
|
-
bytes.slice(
|
|
113
|
-
PROTOCOL_V2_OKPP_HEADER_HASH_OFFSET,
|
|
114
|
-
PROTOCOL_V2_OKPP_HEADER_HASH_OFFSET + PROTOCOL_V2_OKPP_HASH_SIZE
|
|
115
|
-
)
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
async function readProtocolV2ResourceIdentity({
|
|
120
|
-
commands,
|
|
121
|
-
resource,
|
|
122
|
-
chunkSize,
|
|
123
|
-
timeoutMs = PROTOCOL_V2_RESOURCE_INVENTORY_TIMEOUT_MS,
|
|
124
|
-
}: {
|
|
125
|
-
commands: Pick<DeviceCommands, 'typedCall'>;
|
|
126
|
-
resource: IProtocolV2Resource;
|
|
127
|
-
chunkSize: number;
|
|
128
|
-
timeoutMs?: number;
|
|
129
|
-
}): Promise<ProtocolV2ResourceInventoryItem | undefined> {
|
|
130
|
-
const path = PROTOCOL_V2_RESOURCE_DEVICE_PATHS[resource.type];
|
|
131
|
-
const pathInfo = await commands.typedCall(
|
|
132
|
-
'FilesystemPathInfoQuery',
|
|
133
|
-
'FilesystemPathInfo',
|
|
134
|
-
{ path },
|
|
135
|
-
{ timeoutMs }
|
|
136
|
-
);
|
|
137
|
-
const size = toFiniteNumber(pathInfo.message?.size);
|
|
138
|
-
if (
|
|
139
|
-
!pathInfo.message?.exist ||
|
|
140
|
-
pathInfo.message?.directory ||
|
|
141
|
-
!Number.isSafeInteger(size) ||
|
|
142
|
-
size !== resource.size ||
|
|
143
|
-
size < PROTOCOL_V2_OKPP_HEADER_SIZE
|
|
144
|
-
) {
|
|
145
|
-
return undefined;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const header = new Uint8Array(PROTOCOL_V2_RESOURCE_IDENTITY_READ_SIZE);
|
|
149
|
-
let offset = 0;
|
|
150
|
-
while (offset < header.byteLength) {
|
|
151
|
-
const readLength = Math.min(chunkSize, header.byteLength - offset);
|
|
152
|
-
const response = await commands.typedCall(
|
|
153
|
-
'FilesystemFileRead',
|
|
154
|
-
'FilesystemFile',
|
|
155
|
-
{
|
|
156
|
-
file: { path, offset, total_size: 0 },
|
|
157
|
-
chunk_len: readLength,
|
|
158
|
-
},
|
|
159
|
-
{ timeoutMs }
|
|
160
|
-
);
|
|
161
|
-
const data = toUint8Array(response.message?.data);
|
|
162
|
-
if (data.byteLength === 0) return undefined;
|
|
163
|
-
const copied = Math.min(data.byteLength, header.byteLength - offset);
|
|
164
|
-
header.set(data.subarray(0, copied), offset);
|
|
165
|
-
offset += copied;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const headerHash = parseProtocolV2ResourceHeaderHash(header);
|
|
169
|
-
return headerHash ? { type: resource.type, size, headerHash } : undefined;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* 使用已发布 Pro2 固件支持的文件系统消息构建资源清单。
|
|
174
|
-
* 缺失、无法读取或格式错误的文件不会进入清单,因此会被选中重写。
|
|
175
|
-
*/
|
|
176
|
-
export async function readProtocolV2ResourceInventory({
|
|
177
|
-
commands,
|
|
178
|
-
resources,
|
|
179
|
-
chunkSize = PROTOCOL_V2_BLE_FILE_READ_CHUNK_SIZE,
|
|
180
|
-
timeoutMs = PROTOCOL_V2_RESOURCE_INVENTORY_TIMEOUT_MS,
|
|
181
|
-
}: {
|
|
182
|
-
commands: Pick<DeviceCommands, 'typedCall'>;
|
|
183
|
-
resources: readonly IProtocolV2Resource[];
|
|
184
|
-
chunkSize?: number;
|
|
185
|
-
timeoutMs?: number;
|
|
186
|
-
}): Promise<ProtocolV2ResourceInventoryItem[]> {
|
|
187
|
-
const normalizedChunkSize = Number.isFinite(chunkSize)
|
|
188
|
-
? Math.max(Math.floor(chunkSize), PROTOCOL_V2_MIN_FILE_READ_CHUNK_SIZE)
|
|
189
|
-
: PROTOCOL_V2_BLE_FILE_READ_CHUNK_SIZE;
|
|
190
|
-
const inventory: ProtocolV2ResourceInventoryItem[] = [];
|
|
191
|
-
for (const resource of resources) {
|
|
192
|
-
try {
|
|
193
|
-
const item = await readProtocolV2ResourceIdentity({
|
|
194
|
-
commands,
|
|
195
|
-
resource,
|
|
196
|
-
chunkSize: normalizedChunkSize,
|
|
197
|
-
timeoutMs,
|
|
198
|
-
});
|
|
199
|
-
if (item) inventory.push(item);
|
|
200
|
-
} catch {
|
|
201
|
-
// 单个资源无法读取时按缺失处理,不阻断其余资源的增量检查。
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
return inventory;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
function normalizeHex(value: unknown, expectedLength: number, field: string): string {
|
|
208
|
-
if (typeof value !== 'string') {
|
|
209
|
-
throw new Error(`Invalid Pro2 resource ${field}: expected a hexadecimal string`);
|
|
210
|
-
}
|
|
211
|
-
const normalized = value.replace(/^0x/i, '').toLowerCase();
|
|
212
|
-
if (normalized.length !== expectedLength || !/^[0-9a-f]+$/.test(normalized)) {
|
|
213
|
-
throw new Error(
|
|
214
|
-
`Invalid Pro2 resource ${field}: expected ${expectedLength} hexadecimal characters`
|
|
215
|
-
);
|
|
216
|
-
}
|
|
217
|
-
return normalized;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
function validateResource(value: unknown, index: number): IProtocolV2Resource {
|
|
221
|
-
if (!value || typeof value !== 'object') {
|
|
222
|
-
throw new Error(`Invalid Pro2 resource at stable[${index}]`);
|
|
223
|
-
}
|
|
224
|
-
const resource = value as Partial<IProtocolV2Resource>;
|
|
225
|
-
if (typeof resource.type !== 'string' || !RESOURCE_TYPE_SET.has(resource.type)) {
|
|
226
|
-
throw new Error(`Invalid Pro2 resource type at stable[${index}]`);
|
|
227
|
-
}
|
|
228
|
-
if (typeof resource.url !== 'string' || !resource.url.startsWith('https://')) {
|
|
229
|
-
throw new Error(`Invalid Pro2 resource url at stable[${index}]`);
|
|
230
|
-
}
|
|
231
|
-
if (!Number.isSafeInteger(resource.size) || Number(resource.size) <= 0) {
|
|
232
|
-
throw new Error(`Invalid Pro2 resource size at stable[${index}]`);
|
|
233
|
-
}
|
|
234
|
-
return {
|
|
235
|
-
type: resource.type,
|
|
236
|
-
url: resource.url,
|
|
237
|
-
size: Number(resource.size),
|
|
238
|
-
fileHash: normalizeHex(resource.fileHash, SHA256_HEX_LENGTH, 'fileHash'),
|
|
239
|
-
headerHash: normalizeHex(resource.headerHash, SHA3_512_HEX_LENGTH, 'headerHash'),
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
function validateBootResources(value: unknown): IProtocolV2BootResources {
|
|
244
|
-
if (!value || typeof value !== 'object') {
|
|
245
|
-
throw new Error('Invalid Pro2 boot resources config');
|
|
246
|
-
}
|
|
247
|
-
const resource = value as Partial<IProtocolV2BootResources>;
|
|
248
|
-
if (resource.required !== false) {
|
|
249
|
-
throw new Error('Invalid Pro2 boot resources required flag: expected false');
|
|
250
|
-
}
|
|
251
|
-
if (resource.target !== 'RES') {
|
|
252
|
-
throw new Error('Invalid Pro2 boot resources target: expected RES');
|
|
253
|
-
}
|
|
254
|
-
if (
|
|
255
|
-
resource.manifestUrl !== undefined &&
|
|
256
|
-
(typeof resource.manifestUrl !== 'string' || !resource.manifestUrl.startsWith('https://'))
|
|
257
|
-
) {
|
|
258
|
-
throw new Error('Invalid Pro2 boot resources manifestUrl');
|
|
259
|
-
}
|
|
260
|
-
if (!Array.isArray(resource.files) || resource.files.length === 0) {
|
|
261
|
-
throw new Error('Invalid Pro2 boot resources files');
|
|
262
|
-
}
|
|
263
|
-
const files = resource.files.map((value, index): IProtocolV2ResourceFile => {
|
|
264
|
-
if (!value || typeof value !== 'object') {
|
|
265
|
-
throw new Error(`Invalid Pro2 boot resource file at files[${index}]`);
|
|
266
|
-
}
|
|
267
|
-
const file = value as Partial<IProtocolV2ResourceFile>;
|
|
268
|
-
if (typeof file.url !== 'string' || !file.url.startsWith('https://')) {
|
|
269
|
-
throw new Error(`Invalid Pro2 boot resource url at files[${index}]`);
|
|
270
|
-
}
|
|
271
|
-
if (
|
|
272
|
-
typeof file.devicePath !== 'string' ||
|
|
273
|
-
!file.devicePath.startsWith('vol0:/') ||
|
|
274
|
-
file.devicePath.includes('..') ||
|
|
275
|
-
file.devicePath.includes('\\')
|
|
276
|
-
) {
|
|
277
|
-
throw new Error(`Invalid Pro2 boot resource devicePath at files[${index}]`);
|
|
278
|
-
}
|
|
279
|
-
if (!Number.isSafeInteger(file.size) || Number(file.size) <= 0) {
|
|
280
|
-
throw new Error(`Invalid Pro2 boot resource size at files[${index}]`);
|
|
281
|
-
}
|
|
282
|
-
return {
|
|
283
|
-
...(typeof file.name === 'string' && file.name ? { name: file.name } : undefined),
|
|
284
|
-
url: file.url,
|
|
285
|
-
devicePath: file.devicePath,
|
|
286
|
-
size: Number(file.size),
|
|
287
|
-
fileHash: normalizeHex(file.fileHash, SHA256_HEX_LENGTH, `boot files[${index}].fileHash`),
|
|
288
|
-
};
|
|
289
|
-
});
|
|
290
|
-
if (new Set(files.map(file => file.devicePath)).size !== files.length) {
|
|
291
|
-
throw new Error('Invalid Pro2 boot resources files: duplicate devicePath');
|
|
292
|
-
}
|
|
293
|
-
return {
|
|
294
|
-
required: false,
|
|
295
|
-
target: 'RES',
|
|
296
|
-
...(resource.manifestUrl ? { manifestUrl: resource.manifestUrl } : undefined),
|
|
297
|
-
files,
|
|
298
|
-
};
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
/** Validate a complete Pro2 stable resource set from remote configuration. */
|
|
302
|
-
export function parseProtocolV2Resources(value: unknown): IProtocolV2Resources | undefined {
|
|
303
|
-
if (value === undefined) return undefined;
|
|
304
|
-
if (
|
|
305
|
-
!value ||
|
|
306
|
-
typeof value !== 'object' ||
|
|
307
|
-
!Array.isArray((value as { stable?: unknown }).stable)
|
|
308
|
-
) {
|
|
309
|
-
throw new Error('Invalid Pro2 resources config: stable must be an array');
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
const config = value as { stable: unknown[]; boot?: unknown };
|
|
313
|
-
const stable = config.stable.map(validateResource);
|
|
314
|
-
const types = new Set(stable.map(resource => resource.type));
|
|
315
|
-
if (stable.length !== PROTOCOL_V2_RESOURCE_TYPES.length || types.size !== stable.length) {
|
|
316
|
-
throw new Error('Invalid Pro2 resources config: stable must contain six unique resource types');
|
|
317
|
-
}
|
|
318
|
-
for (const type of PROTOCOL_V2_RESOURCE_TYPES) {
|
|
319
|
-
if (!types.has(type)) {
|
|
320
|
-
throw new Error(`Invalid Pro2 resources config: stable is missing ${type}`);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
const boot = config.boot === undefined ? undefined : validateBootResources(config.boot);
|
|
324
|
-
return {
|
|
325
|
-
stable: PROTOCOL_V2_RESOURCE_TYPES.map(type => {
|
|
326
|
-
const resource = stable.find(item => item.type === type);
|
|
327
|
-
if (!resource) {
|
|
328
|
-
throw new Error(`Invalid Pro2 resources config: stable is missing ${type}`);
|
|
329
|
-
}
|
|
330
|
-
return resource;
|
|
331
|
-
}),
|
|
332
|
-
...(boot ? { boot } : undefined),
|
|
333
|
-
};
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
/** 比较文件系统资源清单;恢复模式无法取得清单时回退到全量更新。 */
|
|
337
|
-
export function buildProtocolV2ResourceUpdatePlan({
|
|
338
|
-
resources,
|
|
339
|
-
inventory,
|
|
340
|
-
mode,
|
|
341
|
-
forced = false,
|
|
342
|
-
}: {
|
|
343
|
-
resources: readonly IProtocolV2Resource[];
|
|
344
|
-
inventory?: readonly ProtocolV2ResourceInventoryItem[];
|
|
345
|
-
mode: ProtocolV2ResourceUpdateMode;
|
|
346
|
-
forced?: boolean;
|
|
347
|
-
}): ProtocolV2ResourceUpdatePlan {
|
|
348
|
-
if (forced) {
|
|
349
|
-
return {
|
|
350
|
-
status: resources.length > 0 ? 'outdated' : 'valid',
|
|
351
|
-
resources: [...resources],
|
|
352
|
-
};
|
|
353
|
-
}
|
|
354
|
-
if (!inventory) {
|
|
355
|
-
return mode === 'bootloader-recovery'
|
|
356
|
-
? {
|
|
357
|
-
status: resources.length > 0 ? 'outdated' : 'valid',
|
|
358
|
-
resources: [...resources],
|
|
359
|
-
}
|
|
360
|
-
: { status: 'unknown', resources: [] };
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
const inventoryByType = new Map(inventory.map(item => [item.type, item]));
|
|
364
|
-
const changedResources = resources.filter(resource => {
|
|
365
|
-
const current = inventoryByType.get(resource.type);
|
|
366
|
-
return (
|
|
367
|
-
!current ||
|
|
368
|
-
current.size !== resource.size ||
|
|
369
|
-
current.headerHash.toLowerCase() !== resource.headerHash.toLowerCase()
|
|
370
|
-
);
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
return {
|
|
374
|
-
status: changedResources.length === 0 ? 'valid' : 'outdated',
|
|
375
|
-
resources: changedResources,
|
|
376
|
-
};
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
function bytesToHex(bytes: Uint8Array): string {
|
|
380
|
-
return Array.from(bytes, byte => byte.toString(16).padStart(2, '0')).join('');
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
/** Verify the complete downloaded file before any device mutation. */
|
|
384
|
-
export function isProtocolV2ResourceFileValid(
|
|
385
|
-
binary: ArrayBuffer,
|
|
386
|
-
resource: Pick<IProtocolV2ResourceFile, 'size' | 'fileHash'>
|
|
387
|
-
): boolean {
|
|
388
|
-
if (binary.byteLength !== resource.size) return false;
|
|
389
|
-
return bytesToHex(sha256(new Uint8Array(binary))) === resource.fileHash.toLowerCase();
|
|
390
|
-
}
|
|
@@ -1,131 +0,0 @@
|
|
|
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
|
-
const resolvePreUnlockPinType = (method: RunnableMethod) => {
|
|
51
|
-
const targetsKnownHiddenWallet =
|
|
52
|
-
method.useDevicePassphraseState &&
|
|
53
|
-
method.payload?.useEmptyPassphrase !== true &&
|
|
54
|
-
typeof method.payload?.passphraseState === 'string' &&
|
|
55
|
-
method.payload.passphraseState.length > 0;
|
|
56
|
-
|
|
57
|
-
// A hidden-wallet call may be unlocked by either its Attach PIN or the main
|
|
58
|
-
// PIN followed by passphrase selection. Wallet-session verification still
|
|
59
|
-
// rejects a different hidden wallet after the device is unlocked.
|
|
60
|
-
return targetsKnownHiddenWallet ? DeviceSessionPinType.Any : DeviceSessionPinType.Main;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
export async function runMethodWithUnlockPolicy<T = unknown>(
|
|
64
|
-
method: RunnableMethod,
|
|
65
|
-
device: Device,
|
|
66
|
-
options: RunMethodWithUnlockPolicyOptions<T> = {}
|
|
67
|
-
): Promise<T> {
|
|
68
|
-
const {
|
|
69
|
-
context = createProtocolV2UnlockContext(),
|
|
70
|
-
uiCoordinator,
|
|
71
|
-
afterStatusBeforeUnlock,
|
|
72
|
-
prepare,
|
|
73
|
-
run: configuredRun,
|
|
74
|
-
} = options;
|
|
75
|
-
const run = configuredRun ?? (() => method.run() as Promise<T>);
|
|
76
|
-
method.protocolV2UnlockContext = context;
|
|
77
|
-
|
|
78
|
-
const shouldEmitUi = isProtocolV2UiEnabled(method);
|
|
79
|
-
const shouldCoordinateUi = shouldEmitUi && uiCoordinator !== undefined;
|
|
80
|
-
const requiresPreUnlock =
|
|
81
|
-
device.isProtocolV2() &&
|
|
82
|
-
(method.useDevicePassphraseState || method.unlockPolicy === 'unlock-before-run') &&
|
|
83
|
-
!device.isBootloader?.() &&
|
|
84
|
-
!device.isRomloader?.();
|
|
85
|
-
|
|
86
|
-
if (requiresPreUnlock && !context.preflightCompleted) {
|
|
87
|
-
const status = await refreshProtocolV2DeviceStatus(device);
|
|
88
|
-
if (typeof status?.unlocked !== 'boolean') {
|
|
89
|
-
throw ERRORS.TypedError(
|
|
90
|
-
HardwareErrorCode.RuntimeError,
|
|
91
|
-
'Protocol V2 DeviceStatus did not report an explicit unlock state.'
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
await afterStatusBeforeUnlock?.();
|
|
96
|
-
|
|
97
|
-
if (!status.unlocked) {
|
|
98
|
-
const unlockInteraction: HardwareUiInteractionMeta | undefined = shouldCoordinateUi
|
|
99
|
-
? uiCoordinator.enterUnlockInteraction(method.name)
|
|
100
|
-
: undefined;
|
|
101
|
-
const unlockedStatus = await device.unlockDevice(
|
|
102
|
-
resolvePreUnlockPinType(method),
|
|
103
|
-
shouldCoordinateUi
|
|
104
|
-
? { emitUiEvent: false, interaction: unlockInteraction }
|
|
105
|
-
: {
|
|
106
|
-
source: 'unlock-coordinator',
|
|
107
|
-
reason: 'device-locked',
|
|
108
|
-
deviceOnly: true,
|
|
109
|
-
method: method.name,
|
|
110
|
-
}
|
|
111
|
-
);
|
|
112
|
-
if (unlockedStatus?.unlocked !== true) {
|
|
113
|
-
throw ERRORS.TypedError(
|
|
114
|
-
HardwareErrorCode.RuntimeError,
|
|
115
|
-
'Protocol V2 device remained locked after the unlock flow.'
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
Log.debug('Protocol V2 pre-unlock completed', { method: method.name });
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
context.preflightCompleted = true;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
await prepare?.();
|
|
125
|
-
|
|
126
|
-
if (shouldCoordinateUi) {
|
|
127
|
-
uiCoordinator.enterMethodInteraction(resolveProtocolV2UiInteraction(method));
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return run();
|
|
131
|
-
}
|
package/src/utils/pro2Nft.ts
DELETED
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
import { blake2s } from '@noble/hashes/blake2s';
|
|
2
|
-
import { bytesToHex } from '@noble/hashes/utils';
|
|
3
|
-
|
|
4
|
-
import { invalidParameter } from '../api/helpers/filesystemValidation';
|
|
5
|
-
import { encodePro2Image } from './pro2Wallpaper';
|
|
6
|
-
|
|
7
|
-
export const PRO2_NFT_IMAGE_WIDTH = 540;
|
|
8
|
-
export const PRO2_NFT_IMAGE_HEIGHT = 540;
|
|
9
|
-
export const PRO2_NFT_THUMBNAIL_WIDTH = 263;
|
|
10
|
-
export const PRO2_NFT_THUMBNAIL_HEIGHT = 263;
|
|
11
|
-
export const PRO2_NFT_DIRECTORY = 'vol1:/nft';
|
|
12
|
-
export const PRO2_NFT_DEFAULT_CHUNK_SIZE = 2048;
|
|
13
|
-
export const PRO2_NFT_DEFAULT_PACE_MS = 0;
|
|
14
|
-
export const PRO2_NFT_DEFAULT_TIMEOUT_MS = 15_000;
|
|
15
|
-
export const PRO2_NFT_MIN_CHUNK_SIZE = 64;
|
|
16
|
-
export const PRO2_NFT_MAX_CHUNK_SIZE = 2048;
|
|
17
|
-
export const PRO2_NFT_MAX_ITEMS = 10;
|
|
18
|
-
|
|
19
|
-
export type Pro2NftImage = {
|
|
20
|
-
width: number;
|
|
21
|
-
height: number;
|
|
22
|
-
rgba: Uint8Array | ArrayBuffer;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export type Pro2NftBundle = {
|
|
26
|
-
basename: string;
|
|
27
|
-
image: Uint8Array;
|
|
28
|
-
thumbnail: Uint8Array;
|
|
29
|
-
metadata: Uint8Array;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const PRO2_NFT_BASENAME_PATTERN = /^nft-[0-9a-f]{8}-[1-9][0-9]*$/;
|
|
33
|
-
|
|
34
|
-
type Pro2NftFileType = 'image' | 'thumbnail' | 'metadata';
|
|
35
|
-
|
|
36
|
-
function parsePro2NftFile(listedPath: string):
|
|
37
|
-
| {
|
|
38
|
-
basename: string;
|
|
39
|
-
fileType: Pro2NftFileType;
|
|
40
|
-
}
|
|
41
|
-
| undefined {
|
|
42
|
-
const filename = listedPath.trim().split('/').at(-1);
|
|
43
|
-
if (!filename) return undefined;
|
|
44
|
-
|
|
45
|
-
let basename: string | undefined;
|
|
46
|
-
let fileType: Pro2NftFileType | undefined;
|
|
47
|
-
if (filename.endsWith('_m.bin')) {
|
|
48
|
-
basename = filename.slice(0, -'_m.bin'.length);
|
|
49
|
-
fileType = 'thumbnail';
|
|
50
|
-
} else if (filename.endsWith('.json')) {
|
|
51
|
-
basename = filename.slice(0, -'.json'.length);
|
|
52
|
-
fileType = 'metadata';
|
|
53
|
-
} else if (filename.endsWith('.bin')) {
|
|
54
|
-
basename = filename.slice(0, -'.bin'.length);
|
|
55
|
-
fileType = 'image';
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return basename && fileType && PRO2_NFT_BASENAME_PATTERN.test(basename)
|
|
59
|
-
? { basename, fileType }
|
|
60
|
-
: undefined;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function getCompletePro2NftBasenames(childFiles?: string): Set<string> {
|
|
64
|
-
const filesByBasename = new Map<string, Set<Pro2NftFileType>>();
|
|
65
|
-
|
|
66
|
-
for (const listedPath of childFiles?.split('\n') ?? []) {
|
|
67
|
-
const file = parsePro2NftFile(listedPath);
|
|
68
|
-
if (file) {
|
|
69
|
-
const fileTypes = filesByBasename.get(file.basename) ?? new Set<Pro2NftFileType>();
|
|
70
|
-
fileTypes.add(file.fileType);
|
|
71
|
-
filesByBasename.set(file.basename, fileTypes);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return new Set(
|
|
76
|
-
[...filesByBasename.entries()]
|
|
77
|
-
.filter(([, fileTypes]) => fileTypes.size === 3)
|
|
78
|
-
.map(([basename]) => basename)
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
function utf8Length(value: string): number {
|
|
83
|
-
return new TextEncoder().encode(value).byteLength;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function assertImage(
|
|
87
|
-
name: string,
|
|
88
|
-
image: Pro2NftImage,
|
|
89
|
-
expectedWidth: number,
|
|
90
|
-
expectedHeight: number
|
|
91
|
-
) {
|
|
92
|
-
if (image.width !== expectedWidth || image.height !== expectedHeight) {
|
|
93
|
-
throw invalidParameter(
|
|
94
|
-
`Pro2 NFT ${name} dimensions must be ${expectedWidth}x${expectedHeight}.`
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
if (!(image.rgba instanceof ArrayBuffer) && !ArrayBuffer.isView(image.rgba)) {
|
|
98
|
-
throw invalidParameter(`Parameter [${name}.rgba] must be an ArrayBuffer or Uint8Array.`);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
export function buildPro2NftBundle(options: {
|
|
103
|
-
image: Pro2NftImage;
|
|
104
|
-
thumbnail: Pro2NftImage;
|
|
105
|
-
title: string;
|
|
106
|
-
subtitle: string;
|
|
107
|
-
timestampMs: number;
|
|
108
|
-
}): Pro2NftBundle {
|
|
109
|
-
const { image, thumbnail, title, subtitle, timestampMs } = options;
|
|
110
|
-
assertImage('image', image, PRO2_NFT_IMAGE_WIDTH, PRO2_NFT_IMAGE_HEIGHT);
|
|
111
|
-
assertImage('thumbnail', thumbnail, PRO2_NFT_THUMBNAIL_WIDTH, PRO2_NFT_THUMBNAIL_HEIGHT);
|
|
112
|
-
const titleLength = typeof title === 'string' ? utf8Length(title) : 0;
|
|
113
|
-
const subtitleLength =
|
|
114
|
-
typeof subtitle === 'string' ? utf8Length(subtitle) : Number.POSITIVE_INFINITY;
|
|
115
|
-
if (titleLength < 1 || titleLength > 63) {
|
|
116
|
-
throw invalidParameter('Pro2 NFT title must contain 1 to 63 UTF-8 bytes.');
|
|
117
|
-
}
|
|
118
|
-
if (subtitleLength > 95) {
|
|
119
|
-
throw invalidParameter('Pro2 NFT subtitle must contain at most 95 UTF-8 bytes.');
|
|
120
|
-
}
|
|
121
|
-
if (!Number.isSafeInteger(timestampMs) || timestampMs <= 0) {
|
|
122
|
-
throw invalidParameter('Parameter [timestampMs] must be a positive safe integer.');
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const encodedImage = encodePro2Image({ ...image, alphaMode: 'black-background' }).data;
|
|
126
|
-
const encodedThumbnail = encodePro2Image({
|
|
127
|
-
...thumbnail,
|
|
128
|
-
alphaMode: 'black-background',
|
|
129
|
-
}).data;
|
|
130
|
-
const metadata = new TextEncoder().encode(JSON.stringify({ title, subtitle }));
|
|
131
|
-
if (metadata.byteLength === 0 || metadata.byteLength > 512) {
|
|
132
|
-
throw invalidParameter('Pro2 NFT metadata must contain 1 to 512 UTF-8 bytes.');
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const hash8 = bytesToHex(blake2s(encodedImage)).slice(0, 8);
|
|
136
|
-
return {
|
|
137
|
-
basename: `nft-${hash8}-${timestampMs}`,
|
|
138
|
-
image: encodedImage,
|
|
139
|
-
thumbnail: encodedThumbnail,
|
|
140
|
-
metadata,
|
|
141
|
-
};
|
|
142
|
-
}
|