@onekeyfe/hd-core 1.2.0-alpha.23 → 1.2.0-alpha.24

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.
Files changed (131) hide show
  1. package/__tests__/DeviceCommands.test.ts +35 -0
  2. package/__tests__/connectSettings.test.ts +5 -47
  3. package/__tests__/device-lifecycle-events.test.ts +59 -0
  4. package/__tests__/firmware-update/firmware-update-v2-download-before-boot.test.ts +30 -39
  5. package/__tests__/open-wallet-session.test.ts +192 -129
  6. package/__tests__/protocol-v2-ping.test.ts +28 -0
  7. package/__tests__/protocol-v2.test.ts +338 -376
  8. package/__tests__/public-pro2-api-boundary.test.ts +22 -0
  9. package/dist/api/CheckAllFirmwareRelease.d.ts.map +1 -1
  10. package/dist/api/FirmwareUpdateV2.d.ts +0 -11
  11. package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
  12. package/dist/api/FirmwareUpdateV3.d.ts +3 -7
  13. package/dist/api/FirmwareUpdateV3.d.ts.map +1 -1
  14. package/dist/api/FirmwareUpdateV4.d.ts +5 -21
  15. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
  16. package/dist/api/OpenWalletSession.d.ts +1 -8
  17. package/dist/api/OpenWalletSession.d.ts.map +1 -1
  18. package/dist/api/device/DeviceUpdateBootloader.d.ts +1 -5
  19. package/dist/api/device/DeviceUpdateBootloader.d.ts.map +1 -1
  20. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts +0 -7
  21. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts.map +1 -1
  22. package/dist/api/firmware/getBinary.d.ts +0 -7
  23. package/dist/api/firmware/getBinary.d.ts.map +1 -1
  24. package/dist/api/firmware/updateBootloader.d.ts +0 -2
  25. package/dist/api/firmware/updateBootloader.d.ts.map +1 -1
  26. package/dist/api/firmware/uploadFirmware.d.ts +1 -9
  27. package/dist/api/firmware/uploadFirmware.d.ts.map +1 -1
  28. package/dist/api/index.d.ts +1 -0
  29. package/dist/api/index.d.ts.map +1 -1
  30. package/dist/api/protocol-v2/Ping.d.ts +2 -0
  31. package/dist/api/protocol-v2/Ping.d.ts.map +1 -1
  32. package/dist/api/stellar/StellarSignTransaction.d.ts +1 -1
  33. package/dist/core/index.d.ts.map +1 -1
  34. package/dist/data-manager/DataManager.d.ts +0 -1
  35. package/dist/data-manager/DataManager.d.ts.map +1 -1
  36. package/dist/data-manager/connectSettings.d.ts.map +1 -1
  37. package/dist/device/Device.d.ts.map +1 -1
  38. package/dist/device/DeviceCommands.d.ts.map +1 -1
  39. package/dist/index.d.ts +89 -273
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +783 -2443
  42. package/dist/inject.d.ts.map +1 -1
  43. package/dist/lowLevelInject.d.ts.map +1 -1
  44. package/dist/protocols/protocol-v2/unlockRetry.d.ts +2 -3
  45. package/dist/protocols/protocol-v2/unlockRetry.d.ts.map +1 -1
  46. package/dist/protocols/protocol-v2/walletSession.d.ts +12 -0
  47. package/dist/protocols/protocol-v2/walletSession.d.ts.map +1 -1
  48. package/dist/topLevelInject.d.ts.map +1 -1
  49. package/dist/types/api/checkAllFirmwareRelease.d.ts +0 -3
  50. package/dist/types/api/checkAllFirmwareRelease.d.ts.map +1 -1
  51. package/dist/types/api/deviceUpdateBootloader.d.ts +0 -6
  52. package/dist/types/api/deviceUpdateBootloader.d.ts.map +1 -1
  53. package/dist/types/api/export.d.ts +1 -4
  54. package/dist/types/api/export.d.ts.map +1 -1
  55. package/dist/types/api/firmwareUpdate.d.ts +0 -69
  56. package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
  57. package/dist/types/api/index.d.ts +4 -10
  58. package/dist/types/api/index.d.ts.map +1 -1
  59. package/dist/types/api/openWalletSession.d.ts +0 -12
  60. package/dist/types/api/openWalletSession.d.ts.map +1 -1
  61. package/dist/types/api/protocolV2.d.ts +4 -0
  62. package/dist/types/api/protocolV2.d.ts.map +1 -1
  63. package/dist/types/settings.d.ts +0 -13
  64. package/dist/types/settings.d.ts.map +1 -1
  65. package/dist/utils/deviceInfoUtils.d.ts.map +1 -1
  66. package/dist/utils/patch.d.ts +1 -1
  67. package/dist/utils/patch.d.ts.map +1 -1
  68. package/package.json +4 -4
  69. package/src/api/CheckAllFirmwareRelease.ts +3 -23
  70. package/src/api/FirmwareUpdateV2.ts +120 -292
  71. package/src/api/FirmwareUpdateV3.ts +55 -256
  72. package/src/api/FirmwareUpdateV4.ts +297 -757
  73. package/src/api/OpenWalletSession.ts +23 -86
  74. package/src/api/device/DeviceUpdateBootloader.ts +6 -122
  75. package/src/api/firmware/FirmwareUpdateBaseMethod.ts +0 -50
  76. package/src/api/firmware/updateBootloader.ts +4 -19
  77. package/src/api/firmware/uploadFirmware.ts +22 -140
  78. package/src/api/index.ts +1 -0
  79. package/src/api/protocol-v2/Ping.ts +32 -1
  80. package/src/core/index.ts +39 -7
  81. package/src/data/messages/messages-protocol-v2.json +33 -1
  82. package/src/data-manager/DataManager.ts +33 -33
  83. package/src/data-manager/connectSettings.ts +0 -11
  84. package/src/device/Device.ts +7 -9
  85. package/src/device/DeviceCommands.ts +19 -0
  86. package/src/index.ts +0 -5
  87. package/src/inject.ts +4 -22
  88. package/src/lowLevelInject.ts +1 -5
  89. package/src/protocols/protocol-v2/unlockRetry.ts +20 -3
  90. package/src/protocols/protocol-v2/walletSession.ts +26 -8
  91. package/src/topLevelInject.ts +1 -5
  92. package/src/types/api/checkAllFirmwareRelease.ts +0 -3
  93. package/src/types/api/deviceUpdateBootloader.ts +0 -6
  94. package/src/types/api/export.ts +0 -18
  95. package/src/types/api/firmwareUpdate.ts +0 -76
  96. package/src/types/api/index.ts +3 -14
  97. package/src/types/api/openWalletSession.ts +0 -19
  98. package/src/types/api/protocolV2.ts +10 -0
  99. package/src/types/settings.ts +0 -13
  100. package/src/utils/deviceInfoUtils.ts +15 -3
  101. package/__tests__/firmware-update/check-all-firmware-release.test.ts +0 -86
  102. package/__tests__/firmware-update/firmware-artifact-source.test.ts +0 -119
  103. package/__tests__/firmware-update/firmware-host-binding.test.ts +0 -70
  104. package/__tests__/firmware-update/firmware-update-capabilities.test.ts +0 -13
  105. package/__tests__/firmware-update/firmware-update-plan.test.ts +0 -326
  106. package/__tests__/firmware-update/firmware-update-prepared-plan.test.ts +0 -213
  107. package/__tests__/firmware-update/firmware-upload-source.test.ts +0 -70
  108. package/dist/api/firmware/FirmwareArtifactSource.d.ts +0 -25
  109. package/dist/api/firmware/FirmwareArtifactSource.d.ts.map +0 -1
  110. package/dist/api/firmware/FirmwareHostBinding.d.ts +0 -6
  111. package/dist/api/firmware/FirmwareHostBinding.d.ts.map +0 -1
  112. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts +0 -3
  113. package/dist/api/firmware/FirmwareUpdateCapabilities.d.ts.map +0 -1
  114. package/dist/api/firmware/FirmwareUpdatePlan.d.ts +0 -23
  115. package/dist/api/firmware/FirmwareUpdatePlan.d.ts.map +0 -1
  116. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts +0 -30
  117. package/dist/api/firmware/FirmwareUpdatePreparedPlan.d.ts.map +0 -1
  118. package/dist/types/api/firmwareUpdateCapabilities.d.ts +0 -9
  119. package/dist/types/api/firmwareUpdateCapabilities.d.ts.map +0 -1
  120. package/dist/types/api/firmwareUpdatePlan.d.ts +0 -27
  121. package/dist/types/api/firmwareUpdatePlan.d.ts.map +0 -1
  122. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts +0 -42
  123. package/dist/types/api/firmwareUpdatePreparedPlan.d.ts.map +0 -1
  124. package/src/api/firmware/FirmwareArtifactSource.ts +0 -278
  125. package/src/api/firmware/FirmwareHostBinding.ts +0 -100
  126. package/src/api/firmware/FirmwareUpdateCapabilities.ts +0 -9
  127. package/src/api/firmware/FirmwareUpdatePlan.ts +0 -621
  128. package/src/api/firmware/FirmwareUpdatePreparedPlan.ts +0 -392
  129. package/src/types/api/firmwareUpdateCapabilities.ts +0 -9
  130. package/src/types/api/firmwareUpdatePlan.ts +0 -36
  131. package/src/types/api/firmwareUpdatePreparedPlan.ts +0 -53
@@ -21,13 +21,6 @@ const requiredString = (value: unknown, name: string) => {
21
21
  return value.trim();
22
22
  };
23
23
 
24
- const optionalBoolean = (value: unknown, name: string) => {
25
- if (value !== undefined && typeof value !== 'boolean') {
26
- throw invalidParameter(`Parameter [${name}] must be a boolean.`);
27
- }
28
- return value;
29
- };
30
-
31
24
  const wasResumed = (session: unknown) =>
32
25
  !!session &&
33
26
  typeof session === 'object' &&
@@ -53,72 +46,31 @@ const requireHiddenWalletResponse = (session: {
53
46
  };
54
47
  };
55
48
 
56
- type NormalizedOpenWalletSessionParams = OpenWalletSessionParams & {
57
- legacySessionToClear?: {
58
- deviceId?: string;
59
- passphraseState: string;
60
- };
61
- };
62
-
63
- const normalizeParams = (payload: Record<string, unknown>): NormalizedOpenWalletSessionParams => {
64
- if (payload.mode !== undefined) {
65
- if (
66
- payload.mode !== 'standard' &&
67
- payload.mode !== 'select-hidden' &&
68
- payload.mode !== 'resume-hidden'
69
- ) {
70
- throw invalidParameter(
71
- 'Parameter [mode] must be one of standard, select-hidden, or resume-hidden.'
72
- );
73
- }
74
- if (payload.useEmptyPassphrase !== undefined || payload.initSession !== undefined) {
75
- throw invalidParameter(
76
- 'Parameters [useEmptyPassphrase] and [initSession] cannot be used with [mode].'
77
- );
78
- }
79
- if (payload.mode === 'standard' || payload.mode === 'select-hidden') {
80
- if (payload.deviceId !== undefined || payload.passphraseState !== undefined) {
81
- throw invalidParameter(
82
- 'Parameters [deviceId] and [passphraseState] are only allowed with mode [resume-hidden].'
83
- );
84
- }
85
- return { mode: payload.mode };
86
- }
87
- return {
88
- mode: 'resume-hidden',
89
- deviceId: requiredString(payload.deviceId, 'deviceId'),
90
- passphraseState: requiredString(payload.passphraseState, 'passphraseState'),
91
- };
49
+ const normalizeParams = (payload: Record<string, unknown>): OpenWalletSessionParams => {
50
+ if (payload.mode === undefined) {
51
+ throw invalidParameter('Parameter [mode] is required.');
92
52
  }
93
-
94
- const useEmptyPassphrase = optionalBoolean(payload.useEmptyPassphrase, 'useEmptyPassphrase');
95
- const initSession = optionalBoolean(payload.initSession, 'initSession');
96
- if (useEmptyPassphrase === true) {
97
- return { mode: 'standard' };
53
+ if (
54
+ payload.mode !== 'standard' &&
55
+ payload.mode !== 'select-hidden' &&
56
+ payload.mode !== 'resume-hidden'
57
+ ) {
58
+ throw invalidParameter(
59
+ 'Parameter [mode] must be one of standard, select-hidden, or resume-hidden.'
60
+ );
98
61
  }
99
- if (initSession === true) {
100
- const deviceId =
101
- payload.deviceId === undefined ? undefined : requiredString(payload.deviceId, 'deviceId');
102
- const passphraseState =
103
- payload.passphraseState === undefined
104
- ? undefined
105
- : requiredString(payload.passphraseState, 'passphraseState');
106
- return {
107
- mode: 'select-hidden',
108
- ...(passphraseState
109
- ? {
110
- legacySessionToClear: {
111
- deviceId,
112
- passphraseState,
113
- },
114
- }
115
- : {}),
116
- };
62
+ if (payload.useEmptyPassphrase !== undefined || payload.initSession !== undefined) {
63
+ throw invalidParameter(
64
+ 'Legacy parameters [useEmptyPassphrase] and [initSession] are not supported by openWalletSession.'
65
+ );
117
66
  }
118
-
119
- const hasWalletBinding = payload.deviceId !== undefined || payload.passphraseState !== undefined;
120
- if (!hasWalletBinding) {
121
- return { mode: 'select-hidden' };
67
+ if (payload.mode === 'standard' || payload.mode === 'select-hidden') {
68
+ if (payload.deviceId !== undefined || payload.passphraseState !== undefined) {
69
+ throw invalidParameter(
70
+ 'Parameters [deviceId] and [passphraseState] are only allowed with mode [resume-hidden].'
71
+ );
72
+ }
73
+ return { mode: payload.mode };
122
74
  }
123
75
  return {
124
76
  mode: 'resume-hidden',
@@ -127,7 +79,7 @@ const normalizeParams = (payload: Record<string, unknown>): NormalizedOpenWallet
127
79
  };
128
80
  };
129
81
 
130
- export default class OpenWalletSession extends BaseMethod<NormalizedOpenWalletSessionParams> {
82
+ export default class OpenWalletSession extends BaseMethod<OpenWalletSessionParams> {
131
83
  init() {
132
84
  this.useDevicePassphraseState = false;
133
85
  this.skipForceUpdateCheck = true;
@@ -139,33 +91,18 @@ export default class OpenWalletSession extends BaseMethod<NormalizedOpenWalletSe
139
91
  const isProtocolV2 = this.device.isProtocolV2();
140
92
  const state = await this.device.getDeviceState({ refreshSections: ['status'] });
141
93
  let currentDeviceId = state.identity.deviceId;
142
- const { legacySessionToClear } = this.params;
143
94
  const requireDeviceId = () => {
144
95
  if (!currentDeviceId) {
145
96
  throw ERRORS.TypedError(HardwareErrorCode.DeviceInitializeFailed);
146
97
  }
147
98
  return currentDeviceId;
148
99
  };
149
- const clearLegacySession = () => {
150
- const deviceId = requireDeviceId();
151
- if (
152
- legacySessionToClear &&
153
- (!legacySessionToClear.deviceId || legacySessionToClear.deviceId === deviceId)
154
- ) {
155
- deviceWalletSessionStore.delete(deviceId, legacySessionToClear.passphraseState);
156
- }
157
- };
158
100
  const refreshProtocolV2DeviceId = async () => {
159
101
  const refreshedState = await this.device.getDeviceState({ refreshSections: ['status'] });
160
102
  currentDeviceId = refreshedState.identity.deviceId;
161
- clearLegacySession();
162
103
  return requireDeviceId();
163
104
  };
164
105
 
165
- if (!isProtocolV2) {
166
- clearLegacySession();
167
- }
168
-
169
106
  const protocol = isProtocolV2 ? 'V2' : 'V1';
170
107
 
171
108
  if (this.params.mode === 'standard') {
@@ -5,23 +5,15 @@ import { UI_REQUEST } from '../../constants/ui-request';
5
5
  import { FirmwareUpdateTipMessage } from '../../events/ui-request';
6
6
  import { FirmwareUpdateBaseMethod } from '../firmware/FirmwareUpdateBaseMethod';
7
7
  import { getSysResourceBinary } from '../firmware/getBinary';
8
- import { updateBootloader, updateBootloaderFromSource } from '../firmware/uploadFirmware';
8
+ import { updateBootloader } from '../firmware/uploadFirmware';
9
9
  import { DeviceModelToTypes } from '../../types';
10
10
  import { DataManager } from '../../data-manager';
11
- import { checkBootloaderLength, checkBootloaderSourceLength } from '../firmware/updateBootloader';
12
- import { openFirmwareByteSource } from '../firmware/FirmwareArtifactSource';
13
- import { resolveFirmwareUpdateHostBinding } from '../firmware/FirmwareHostBinding';
14
- import {
15
- assertFirmwareUpdatePreparedPlanBinding,
16
- assertFirmwareUpdatePreparedPlanDeviceIdentity,
17
- } from '../firmware/FirmwareUpdatePreparedPlan';
18
- import { getDeviceUUID } from '../../utils';
11
+ import { checkBootloaderLength } from '../firmware/updateBootloader';
19
12
 
20
13
  import type { DeviceUpdateBootloaderParams } from '../../types/api/deviceUpdateBootloader';
21
14
  import type { EFirmwareType } from '@onekeyfe/hd-shared';
22
15
  import type { Device } from '../../device/Device';
23
16
  import type { Features } from '../../types';
24
- import type { FirmwareByteSource } from '../firmware/FirmwareArtifactSource';
25
17
 
26
18
  export default class DeviceUpdateBootloader extends FirmwareUpdateBaseMethod<any> {
27
19
  init() {
@@ -51,68 +43,17 @@ export default class DeviceUpdateBootloader extends FirmwareUpdateBaseMethod<any
51
43
  return true;
52
44
  }
53
45
 
54
- async updateBootloaderWithEmmcFileWriteFromSource(_device: Device, source: FirmwareByteSource) {
55
- const filePath = '0:boot/bootloader.bin';
56
-
57
- this.postTipMessage(FirmwareUpdateTipMessage.StartTransferData);
58
- await this.emmcCommonUpdateProcessFromSource({
59
- source,
60
- filePath,
61
- });
62
- this.postTipMessage(FirmwareUpdateTipMessage.ConfirmOnDevice);
63
- await this.reboot(RebootType.Normal);
64
- this.postTipMessage(FirmwareUpdateTipMessage.UpdateBootloaderSuccess);
65
- return true;
66
- }
67
-
68
46
  async updateTouchBootloader({
69
47
  device,
70
48
  features,
71
49
  firmwareType,
72
- binary: preparedBinary,
73
- source: preparedSource,
74
50
  }: {
75
51
  device: Device;
76
52
  features?: Features;
77
53
  firmwareType: EFirmwareType;
78
- binary?: ArrayBuffer;
79
- source?: FirmwareByteSource;
80
54
  }) {
81
- if (preparedSource) {
82
- if (!(await checkBootloaderSourceLength(preparedSource))) {
83
- throw ERRORS.TypedError(HardwareErrorCode.CheckDownloadFileError);
84
- }
85
- if (features && device.isBootloader()) {
86
- this.postTipMessage(FirmwareUpdateTipMessage.UpdateBootloader);
87
- const result = await this.updateBootloaderWithEmmcFileWriteFromSource(
88
- device,
89
- preparedSource
90
- );
91
- return result;
92
- }
93
- if (features && !device.isBootloader()) {
94
- await updateBootloaderFromSource(
95
- this.device.getCommands().typedCall.bind(this.device.getCommands()),
96
- this.postMessage,
97
- device,
98
- preparedSource
99
- );
100
- return true;
101
- }
102
- }
103
-
104
- let binary = preparedBinary;
55
+ let { binary } = this.payload;
105
56
  if (!binary) {
106
- if (DataManager.getSettings('firmwareManifestMode') === 'external-only') {
107
- throw ERRORS.TypedError(
108
- HardwareErrorCode.RuntimeError,
109
- 'Bootloader must be prepared by the external firmware host',
110
- {
111
- firmwareUpdateCode: 'FirmwareArtifactsNotPrepared',
112
- artifactName: 'bootloader',
113
- }
114
- );
115
- }
116
57
  this.postTipMessage(FirmwareUpdateTipMessage.CheckLatestUiResource);
117
58
  const resourceUrl = features
118
59
  ? DataManager.getBootloaderResource(features, firmwareType)
@@ -127,7 +68,7 @@ export default class DeviceUpdateBootloader extends FirmwareUpdateBaseMethod<any
127
68
  }
128
69
  }
129
70
 
130
- if (!binary || !checkBootloaderLength(binary)) {
71
+ if (!checkBootloaderLength(binary)) {
131
72
  throw ERRORS.TypedError(HardwareErrorCode.CheckDownloadFileError);
132
73
  }
133
74
 
@@ -135,8 +76,7 @@ export default class DeviceUpdateBootloader extends FirmwareUpdateBaseMethod<any
135
76
  if (features && device.isBootloader()) {
136
77
  // Use emmcFileWrite + reboot logic for bootloader mode
137
78
  this.postTipMessage(FirmwareUpdateTipMessage.UpdateBootloader);
138
- const result = await this.updateBootloaderWithEmmcFileWrite(device, binary);
139
- return result;
79
+ return this.updateBootloaderWithEmmcFileWrite(device, binary);
140
80
  }
141
81
 
142
82
  if (features && !device.isBootloader()) {
@@ -156,69 +96,13 @@ export default class DeviceUpdateBootloader extends FirmwareUpdateBaseMethod<any
156
96
  const { features } = device;
157
97
 
158
98
  const payload = this.payload as DeviceUpdateBootloaderParams;
159
- const hostBinding = payload.artifact
160
- ? resolveFirmwareUpdateHostBinding(payload.hostBindingGeneration)
161
- : undefined;
162
- const executionParams = {
163
- ...payload,
164
- artifactReader: hostBinding?.artifactReader ?? payload.artifactReader,
165
- };
166
- if (payload.artifact) {
167
- assertFirmwareUpdatePreparedPlanDeviceIdentity({
168
- preparedPlan: payload.preparedPlan,
169
- deviceIdentity: getDeviceUUID(features) || undefined,
170
- });
171
- assertFirmwareUpdatePreparedPlanBinding({
172
- preparedPlan: payload.preparedPlan,
173
- executor: 'v2',
174
- scopeTargets: ['bootloader'],
175
- bindings: [
176
- {
177
- target: 'bootloader',
178
- artifact: payload.artifact,
179
- },
180
- ],
181
- });
182
- const source = await openFirmwareByteSource({
183
- artifact: payload.artifact,
184
- reader: executionParams.artifactReader,
185
- });
186
- if (!source) {
187
- throw ERRORS.TypedError(
188
- HardwareErrorCode.RuntimeError,
189
- 'Bootloader artifact is not prepared'
190
- );
191
- }
192
- try {
193
- const deviceType = device.getCurrentDeviceType();
194
- const deviceFirmwareType = device.getCurrentFirmwareType();
195
- const firmwareType = payload.firmwareType ?? deviceFirmwareType;
196
- if (DeviceModelToTypes.model_touch.includes(deviceType)) {
197
- return await this.updateTouchBootloader({
198
- device,
199
- features,
200
- firmwareType,
201
- source,
202
- });
203
- }
204
- return true;
205
- } finally {
206
- await source.close();
207
- }
208
- }
209
99
 
210
- const { binary } = payload;
211
100
  const deviceType = device.getCurrentDeviceType();
212
101
  const deviceFirmwareType = device.getCurrentFirmwareType();
213
102
  const firmwareType = payload.firmwareType ?? deviceFirmwareType;
214
103
 
215
104
  if (DeviceModelToTypes.model_touch.includes(deviceType)) {
216
- return this.updateTouchBootloader({
217
- device,
218
- features,
219
- firmwareType,
220
- binary,
221
- });
105
+ return this.updateTouchBootloader({ device, features, firmwareType });
222
106
  }
223
107
 
224
108
  return Promise.resolve(true);
@@ -14,7 +14,6 @@ import { DataManager } from '../../data-manager';
14
14
  import { BaseMethod } from '../BaseMethod';
15
15
  import { DEVICE } from '../../events';
16
16
  import { createFirmwareProgressThrottle } from './progressThrottle';
17
- import { writeFirmwareByteSource } from './FirmwareArtifactSource';
18
17
 
19
18
  import type {
20
19
  IFirmwareUpdateProgressType,
@@ -25,7 +24,6 @@ import type { RebootType } from '@onekeyfe/hd-transport';
25
24
  import type { Deferred } from '@onekeyfe/hd-shared';
26
25
  import type { KnownDevice } from '../../types';
27
26
  import type { TypedResponseMessage } from '../../device/DeviceCommands';
28
- import type { FirmwareByteSource } from './FirmwareArtifactSource';
29
27
 
30
28
  const Log = getLogger(LoggerNames.Method);
31
29
  const SESSION_ERROR = 'session not found';
@@ -389,54 +387,6 @@ export class FirmwareUpdateBaseMethod<Params> extends BaseMethod<Params> {
389
387
  return totalSize !== undefined ? (processedSize ?? 0) + payload.byteLength : 0;
390
388
  }
391
389
 
392
- async emmcCommonUpdateProcessFromSource({
393
- source,
394
- filePath,
395
- processedSize,
396
- totalSize,
397
- }: {
398
- source: FirmwareByteSource;
399
- filePath: string;
400
- processedSize?: number;
401
- totalSize?: number;
402
- }) {
403
- if (!filePath.startsWith('0:')) {
404
- throw new Error('filePath must start with 0:');
405
- }
406
- const env = DataManager.getSettings('env');
407
- const chunkSize = 1024 * (DataManager.isBleConnect(env) ? 16 : 128);
408
-
409
- await writeFirmwareByteSource({
410
- source,
411
- chunkSize,
412
- write: async ({ data, sourceOffset, length, first }) => {
413
- const progress =
414
- totalSize !== undefined && processedSize !== undefined
415
- ? Math.min(Math.ceil(((processedSize + sourceOffset + length) / totalSize) * 100), 99)
416
- : Math.min(Math.ceil(((sourceOffset + length) / source.size) * 100), 99);
417
- const writeRes = await this.emmcFileWriteWithRetry(
418
- filePath,
419
- length,
420
- sourceOffset,
421
- data,
422
- first,
423
- progress
424
- );
425
- if (!writeRes) {
426
- throw ERRORS.TypedError(
427
- HardwareErrorCode.EmmcFileWriteFirmwareError,
428
- 'transfer data error'
429
- );
430
- }
431
- const processedBytes = Number(writeRes.message.processed_byte);
432
- this.postProgressMessage(progress, 'transferData');
433
- return Number.isFinite(processedBytes) ? processedBytes : length;
434
- },
435
- });
436
-
437
- return totalSize !== undefined ? (processedSize ?? 0) + source.size : 0;
438
- }
439
-
440
390
  async emmcFileWriteWithRetry(
441
391
  filePath: string,
442
392
  chunkLength: number,
@@ -8,7 +8,6 @@ import { shouldUpdateBootloaderForClassicAndMini } from './bootloaderHelper';
8
8
 
9
9
  import type { Features } from '../../types';
10
10
  import type { EFirmwareType } from '@onekeyfe/hd-shared';
11
- import type { FirmwareByteSource } from './FirmwareArtifactSource';
12
11
 
13
12
  export function checkNeedUpdateBootForTouch(features: Features, firmwareType: EFirmwareType) {
14
13
  const deviceType = getDeviceType(features);
@@ -63,7 +62,8 @@ export function checkNeedUpdateBootForClassicAndMini({
63
62
  }
64
63
 
65
64
  const INIT_DATA_CHUNK_SIZE = 16 * 1024;
66
- const readBootloaderLength = (chunk: Uint8Array) => {
65
+ export function checkBootloaderLength(data: ArrayBuffer) {
66
+ const chunk = new Uint8Array(data.slice(0, Math.min(INIT_DATA_CHUNK_SIZE, data.byteLength)));
67
67
  const buffer = ByteBuffer.wrap(chunk, undefined, undefined, true);
68
68
  buffer.LE();
69
69
  // byte 'O', 'K', 'T', 'B'
@@ -77,21 +77,6 @@ const readBootloaderLength = (chunk: Uint8Array) => {
77
77
  buffer.readUint32();
78
78
  // codelen
79
79
  const codelen = buffer.readUint32();
80
- return hdrlen + codelen;
81
- };
82
-
83
- export function checkBootloaderLength(data: ArrayBuffer) {
84
- if (data.byteLength < 16) {
85
- return false;
86
- }
87
- const chunk = new Uint8Array(data.slice(0, Math.min(INIT_DATA_CHUNK_SIZE, data.byteLength)));
88
- return readBootloaderLength(chunk) === data.byteLength;
80
+ const bootloaderLength = hdrlen + codelen;
81
+ return bootloaderLength === data.byteLength;
89
82
  }
90
-
91
- export const checkBootloaderSourceLength = async (source: FirmwareByteSource) => {
92
- if (source.size < 16) {
93
- return false;
94
- }
95
- const chunk = new Uint8Array(await source.readAt(0, Math.min(INIT_DATA_CHUNK_SIZE, source.size)));
96
- return readBootloaderLength(chunk) === source.size;
97
- };