@onekeyfe/hd-core 1.2.0-alpha.50 → 1.2.0-alpha.51
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 -78
- package/__tests__/DeviceEventRegistration.test.ts +0 -6
- package/__tests__/check-all-firmware-release-protocol-v2.test.ts +3 -111
- 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__/firmware-update/check-all-firmware-release.test.ts +175 -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.test.ts +593 -0
- package/__tests__/firmware-update/firmware-update-prepared-plan.test.ts +213 -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__/logBlockEvent.test.ts +1 -1
- package/__tests__/method-protocol-support.test.ts +0 -19
- package/__tests__/open-wallet-session.test.ts +33 -446
- package/__tests__/protocol-v2-unlock-policy.test.ts +35 -196
- package/__tests__/protocol-v2.test.ts +1032 -1056
- 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/dist/api/BaseMethod.d.ts +1 -3
- package/dist/api/BaseMethod.d.ts.map +1 -1
- package/dist/api/CheckAllFirmwareRelease.d.ts.map +1 -1
- package/dist/api/FirmwareUpdate.d.ts.map +1 -1
- package/dist/api/FirmwareUpdateV2.d.ts +11 -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 +25 -9
- 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/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 +9 -0
- 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 +30 -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 -2
- 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/protocol-v2/ProtocolInfoRequest.d.ts.map +1 -1
- package/dist/api/stellar/StellarSignTransaction.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 +2 -4
- 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 +6 -29
- package/dist/device/Device.d.ts.map +1 -1
- package/dist/device/DeviceCommands.d.ts +4 -4
- package/dist/device/DevicePool.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 +230 -203
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3445 -2419
- 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.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 -4
- 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 +4 -1
- package/dist/types/api/checkAllFirmwareRelease.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 -1
- package/dist/types/api/export.d.ts.map +1 -1
- package/dist/types/api/firmwareUpdate.d.ts +70 -2
- 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/params.d.ts +0 -1
- package/dist/types/params.d.ts.map +1 -1
- package/dist/types/settings.d.ts +13 -22
- package/dist/types/settings.d.ts.map +1 -1
- package/dist/utils/deviceFeaturesUtils.d.ts +0 -2
- package/dist/utils/deviceFeaturesUtils.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 +53 -41
- package/src/api/FirmwareUpdate.ts +15 -7
- package/src/api/FirmwareUpdateV2.ts +313 -126
- package/src/api/FirmwareUpdateV3.ts +265 -63
- package/src/api/FirmwareUpdateV4.ts +937 -430
- package/src/api/GetPassphraseState.ts +1 -7
- package/src/api/OpenWalletSession.ts +17 -54
- package/src/api/SearchDevices.ts +1 -3
- package/src/api/allnetwork/AllNetworkGetAddressBase.ts +11 -32
- package/src/api/device/DeviceUnlock.ts +1 -1
- package/src/api/device/DeviceUpdateBootloader.ts +122 -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 -16
- package/src/api/firmware/FirmwareUpdateCapabilities.ts +9 -0
- package/src/api/firmware/FirmwareUpdatePlan.ts +745 -0
- package/src/api/firmware/FirmwareUpdatePreparedPlan.ts +392 -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 +4 -11
- package/src/api/index.ts +0 -2
- package/src/api/protocol-v2/ProtocolInfoRequest.ts +1 -3
- package/src/core/deviceEventRegistration.ts +0 -4
- package/src/core/index.ts +83 -167
- package/src/data/messages/messages-protocol-v2.json +0 -25
- package/src/data-manager/DataManager.ts +38 -67
- package/src/data-manager/connectSettings.ts +11 -0
- package/src/device/Device.ts +42 -257
- package/src/device/DevicePool.ts +2 -7
- 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 +2 -9
- 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 +30 -169
- package/src/topLevelInject.ts +8 -7
- package/src/types/api/checkAllFirmwareRelease.ts +4 -1
- package/src/types/api/deviceUpdateBootloader.ts +6 -0
- package/src/types/api/export.ts +18 -0
- package/src/types/api/firmwareUpdate.ts +76 -3
- 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/params.ts +1 -7
- package/src/types/settings.ts +13 -42
- package/src/utils/deviceFeaturesUtils.ts +0 -4
- package/src/utils/homescreen.ts +6 -32
- package/src/utils/index.ts +1 -6
- package/src/utils/pro2Wallpaper.ts +8 -35
- package/__tests__/deviceUploadNft.test.ts +0 -293
- package/__tests__/pro2Nft.test.ts +0 -108
- package/__tests__/protocol-binding.test.ts +0 -89
- package/__tests__/protocol-v2-resources.test.ts +0 -284
- package/__tests__/protocol-v2-ui-lifecycle.test.ts +0 -91
- package/__tests__/uiProgressThrottle.test.ts +0 -74
- package/__tests__/uiPromiseRegistry.test.ts +0 -86
- 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 -4
- 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 -189
- package/src/core/uiPromiseRegistry.ts +0 -41
- package/src/protocols/protocol-v2/resources.ts +0 -359
- package/src/protocols/protocol-v2/unlockPolicyRunner.ts +0 -124
- package/src/types/api/detectDeviceConnectProtocol.ts +0 -7
- package/src/utils/pro2Nft.ts +0 -142
- package/src/utils/uiProgressThrottle.ts +0 -63
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
|
|
2
|
-
|
|
3
|
-
import { LoggerNames, getLogger } from '../../utils';
|
|
4
|
-
import { isProtocolV2UiEnabled, resolveProtocolV2UiInteraction } from './uiInteraction';
|
|
5
|
-
import { refreshProtocolV2DeviceStatus } from './walletSession';
|
|
6
|
-
|
|
7
|
-
import type { BaseMethod } from '../../api/BaseMethod';
|
|
8
|
-
import type { Device } from '../../device/Device';
|
|
9
|
-
import type { HardwareUiInteractionMeta } from '../../events';
|
|
10
|
-
import type { ProtocolV2UiInteractionCoordinator } from './uiInteraction';
|
|
11
|
-
|
|
12
|
-
const Log = getLogger(LoggerNames.Core);
|
|
13
|
-
|
|
14
|
-
export type ProtocolV2UnlockContext = {
|
|
15
|
-
preflightCompleted: boolean;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export const createProtocolV2UnlockContext = (): ProtocolV2UnlockContext => ({
|
|
19
|
-
preflightCompleted: false,
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
type RunnableMethod = Pick<
|
|
23
|
-
BaseMethod,
|
|
24
|
-
| 'run'
|
|
25
|
-
| 'unlockPolicy'
|
|
26
|
-
| 'useDevicePassphraseState'
|
|
27
|
-
| 'protocolV2UiInteraction'
|
|
28
|
-
| 'protocolV2UiMode'
|
|
29
|
-
| 'params'
|
|
30
|
-
| 'payload'
|
|
31
|
-
> & {
|
|
32
|
-
name?: string;
|
|
33
|
-
protocolV2UnlockContext?: ProtocolV2UnlockContext;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
type UiInteractionCoordinator = Pick<
|
|
37
|
-
ProtocolV2UiInteractionCoordinator,
|
|
38
|
-
'enterMethodInteraction' | 'enterUnlockInteraction'
|
|
39
|
-
>;
|
|
40
|
-
|
|
41
|
-
type RunMethodWithUnlockPolicyOptions<T> = {
|
|
42
|
-
context?: ProtocolV2UnlockContext;
|
|
43
|
-
uiCoordinator?: UiInteractionCoordinator;
|
|
44
|
-
afterStatusBeforeUnlock?: () => void | Promise<void>;
|
|
45
|
-
prepare?: () => Promise<void>;
|
|
46
|
-
run?: () => Promise<T>;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
export async function runMethodWithUnlockPolicy<T = unknown>(
|
|
50
|
-
method: RunnableMethod,
|
|
51
|
-
device: Device,
|
|
52
|
-
options: RunMethodWithUnlockPolicyOptions<T> = {}
|
|
53
|
-
): Promise<T> {
|
|
54
|
-
const {
|
|
55
|
-
context = createProtocolV2UnlockContext(),
|
|
56
|
-
uiCoordinator,
|
|
57
|
-
afterStatusBeforeUnlock,
|
|
58
|
-
prepare,
|
|
59
|
-
run: configuredRun,
|
|
60
|
-
} = options;
|
|
61
|
-
const run = configuredRun ?? (() => method.run() as Promise<T>);
|
|
62
|
-
method.protocolV2UnlockContext = context;
|
|
63
|
-
|
|
64
|
-
const shouldEmitUi = isProtocolV2UiEnabled(method);
|
|
65
|
-
const shouldCoordinateUi = shouldEmitUi && uiCoordinator !== undefined;
|
|
66
|
-
const requiresPreUnlock =
|
|
67
|
-
device.isProtocolV2() &&
|
|
68
|
-
(method.useDevicePassphraseState || method.unlockPolicy === 'unlock-before-run') &&
|
|
69
|
-
!device.isBootloader?.() &&
|
|
70
|
-
!device.isRomloader?.();
|
|
71
|
-
|
|
72
|
-
if (requiresPreUnlock && !context.preflightCompleted) {
|
|
73
|
-
const status = await refreshProtocolV2DeviceStatus(device);
|
|
74
|
-
if (typeof status?.unlocked !== 'boolean') {
|
|
75
|
-
throw ERRORS.TypedError(
|
|
76
|
-
HardwareErrorCode.RuntimeError,
|
|
77
|
-
'Protocol V2 DeviceStatus did not report an explicit unlock state.'
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
await afterStatusBeforeUnlock?.();
|
|
82
|
-
|
|
83
|
-
const isStandardWalletRequest =
|
|
84
|
-
method.unlockPolicy !== 'unlock-before-run' &&
|
|
85
|
-
method.useDevicePassphraseState &&
|
|
86
|
-
method.payload?.useEmptyPassphrase === true;
|
|
87
|
-
const standardWalletSessionOwnsUnlock =
|
|
88
|
-
isStandardWalletRequest && status.passphraseProtection === true;
|
|
89
|
-
|
|
90
|
-
if (!status.unlocked && !standardWalletSessionOwnsUnlock) {
|
|
91
|
-
const unlockInteraction: HardwareUiInteractionMeta | undefined = shouldCoordinateUi
|
|
92
|
-
? uiCoordinator.enterUnlockInteraction(method.name)
|
|
93
|
-
: undefined;
|
|
94
|
-
const unlockedStatus = await device.unlockDevice(
|
|
95
|
-
undefined,
|
|
96
|
-
shouldCoordinateUi
|
|
97
|
-
? { emitUiEvent: false, interaction: unlockInteraction }
|
|
98
|
-
: {
|
|
99
|
-
source: 'unlock-coordinator',
|
|
100
|
-
reason: 'device-locked',
|
|
101
|
-
deviceOnly: true,
|
|
102
|
-
method: method.name,
|
|
103
|
-
}
|
|
104
|
-
);
|
|
105
|
-
if (unlockedStatus?.unlocked !== true) {
|
|
106
|
-
throw ERRORS.TypedError(
|
|
107
|
-
HardwareErrorCode.RuntimeError,
|
|
108
|
-
'Protocol V2 device remained locked after the unlock flow.'
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
Log.debug('Protocol V2 pre-unlock completed', { method: method.name });
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
context.preflightCompleted = true;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
await prepare?.();
|
|
118
|
-
|
|
119
|
-
if (shouldCoordinateUi) {
|
|
120
|
-
uiCoordinator.enterMethodInteraction(resolveProtocolV2UiInteraction(method));
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return run();
|
|
124
|
-
}
|
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
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
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
|
-
};
|