@onekeyfe/hd-core 1.1.14 → 1.1.15

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/dist/index.js CHANGED
@@ -196,7 +196,6 @@ const createCoreApi = (call) => ({
196
196
  firmwareUpdate: (connectId, params) => call(Object.assign(Object.assign({}, params), { connectId, method: 'firmwareUpdate' })),
197
197
  firmwareUpdateV2: (connectId, params) => call(Object.assign(Object.assign({}, params), { connectId, method: 'firmwareUpdateV2' })),
198
198
  firmwareUpdateV3: (connectId, params) => call(Object.assign(Object.assign({}, params), { connectId, method: 'firmwareUpdateV3' })),
199
- emmcFileWrite: (connectId, params) => call(Object.assign(Object.assign({}, params), { connectId, method: 'emmcFileWrite' })),
200
199
  promptWebDeviceAccess: params => call(Object.assign(Object.assign({}, params), { method: 'promptWebDeviceAccess' })),
201
200
  tronGetAddress: (connectId, deviceId, params) => call(Object.assign(Object.assign({}, params), { connectId, deviceId, method: 'tronGetAddress' })),
202
201
  tronSignMessage: (connectId, deviceId, params) => call(Object.assign(Object.assign({}, params), { connectId, deviceId, method: 'tronSignMessage' })),
@@ -28858,7 +28857,9 @@ class DeviceFullyUploadResource extends BaseMethod {
28858
28857
  }
28859
28858
  }
28860
28859
 
28861
- class DeviceUpdateBootloader extends BaseMethod {
28860
+ const Log$7 = getLogger(exports.LoggerNames.Method);
28861
+ const SESSION_ERROR = 'session not found';
28862
+ class FirmwareUpdateBaseMethod extends BaseMethod {
28862
28863
  constructor() {
28863
28864
  super(...arguments);
28864
28865
  this.checkPromise = null;
@@ -28870,36 +28871,341 @@ class DeviceUpdateBootloader extends BaseMethod {
28870
28871
  },
28871
28872
  }));
28872
28873
  };
28874
+ this.postProcessingMessage = (type) => {
28875
+ this.postMessage(createUiMessage(UI_REQUEST.FIRMWARE_PROCESSING, {
28876
+ type,
28877
+ }));
28878
+ };
28879
+ this.postProgressMessage = (progress, progressType) => {
28880
+ this.postMessage(createUiMessage(UI_REQUEST.FIRMWARE_PROGRESS, {
28881
+ device: this.device.toMessageObject(),
28882
+ progress,
28883
+ progressType,
28884
+ }));
28885
+ };
28886
+ }
28887
+ init() { }
28888
+ run() {
28889
+ return Promise.resolve();
28890
+ }
28891
+ isBleReconnect() {
28892
+ const env = DataManager.getSettings('env');
28893
+ return this.payload.connectId && DataManager.isBleConnect(env);
28894
+ }
28895
+ _promptDeviceInBootloaderForWebDevice() {
28896
+ return __awaiter(this, void 0, void 0, function* () {
28897
+ return new Promise((resolve, reject) => {
28898
+ if (this.device.listenerCount(DEVICE.SELECT_DEVICE_IN_BOOTLOADER_FOR_WEB_DEVICE) > 0) {
28899
+ this.device.emit(DEVICE.SELECT_DEVICE_IN_BOOTLOADER_FOR_WEB_DEVICE, this.device, (err, deviceId) => {
28900
+ if (err) {
28901
+ reject(err);
28902
+ }
28903
+ else {
28904
+ resolve(deviceId);
28905
+ }
28906
+ });
28907
+ }
28908
+ });
28909
+ });
28910
+ }
28911
+ checkDeviceToBootloader(connectId) {
28912
+ var _a, _b;
28913
+ this.checkPromise = hdShared.createDeferred();
28914
+ const env = DataManager.getSettings('env');
28915
+ const isBleReconnect = connectId && DataManager.isBleConnect(env);
28916
+ Log$7.log('FirmwareUpdateBaseMethod [checkDeviceToBootloader] isBleReconnect: ', isBleReconnect);
28917
+ let isFirstCheck = true;
28918
+ let checkCount = 0;
28919
+ let timeoutTimer;
28920
+ const isTouchOrProDevice = getDeviceType((_a = this === null || this === void 0 ? void 0 : this.device) === null || _a === void 0 ? void 0 : _a.features) === hdShared.EDeviceType.Touch ||
28921
+ getDeviceType((_b = this === null || this === void 0 ? void 0 : this.device) === null || _b === void 0 ? void 0 : _b.features) === hdShared.EDeviceType.Pro;
28922
+ const intervalTimer = setInterval(() => __awaiter(this, void 0, void 0, function* () {
28923
+ var _c, _d, _e, _f;
28924
+ checkCount += 1;
28925
+ Log$7.log('FirmwareUpdateBaseMethod [checkDeviceToBootloader] isFirstCheck: ', isFirstCheck);
28926
+ if (isTouchOrProDevice && isFirstCheck) {
28927
+ isFirstCheck = false;
28928
+ Log$7.log('FirmwareUpdateBaseMethod [checkDeviceToBootloader] wait 3000ms');
28929
+ yield wait(3000);
28930
+ }
28931
+ if (checkCount > 4 && DataManager.isWebUsbConnect(DataManager.getSettings('env'))) {
28932
+ clearInterval(intervalTimer);
28933
+ clearTimeout(timeoutTimer);
28934
+ try {
28935
+ this.postTipMessage(exports.FirmwareUpdateTipMessage.SelectDeviceInBootloaderForWebDevice);
28936
+ const confirmed = yield this._promptDeviceInBootloaderForWebDevice();
28937
+ if (confirmed) {
28938
+ yield this._checkDeviceInBootloaderMode(connectId, intervalTimer, timeoutTimer);
28939
+ }
28940
+ }
28941
+ catch (e) {
28942
+ Log$7.log('FirmwareUpdateBaseMethod [checkDeviceToBootloader] promptDeviceInBootloaderForWebDevice failed: ', e);
28943
+ (_c = this.checkPromise) === null || _c === void 0 ? void 0 : _c.reject(e);
28944
+ }
28945
+ return;
28946
+ }
28947
+ if (isBleReconnect) {
28948
+ try {
28949
+ yield ((_d = this.device.deviceConnector) === null || _d === void 0 ? void 0 : _d.acquire(this.device.originalDescriptor.id, null, true));
28950
+ yield this.device.initialize();
28951
+ if ((_e = this.device.features) === null || _e === void 0 ? void 0 : _e.bootloader_mode) {
28952
+ clearInterval(intervalTimer);
28953
+ (_f = this.checkPromise) === null || _f === void 0 ? void 0 : _f.resolve(true);
28954
+ }
28955
+ }
28956
+ catch (e) {
28957
+ Log$7.log('catch Bluetooth error when device is restarting: ', e);
28958
+ }
28959
+ }
28960
+ else {
28961
+ yield this._checkDeviceInBootloaderMode(connectId, intervalTimer, timeoutTimer);
28962
+ }
28963
+ }), isBleReconnect ? 3000 : 2000);
28964
+ timeoutTimer = setTimeout(() => {
28965
+ if (this.checkPromise) {
28966
+ clearInterval(intervalTimer);
28967
+ this.checkPromise.reject(new Error());
28968
+ }
28969
+ }, 30000);
28873
28970
  }
28971
+ _checkDeviceInBootloaderMode(connectId, intervalTimer, timeoutTimer) {
28972
+ var _a, _b, _c, _d, _e;
28973
+ return __awaiter(this, void 0, void 0, function* () {
28974
+ const deviceDiff = yield ((_a = this.device.deviceConnector) === null || _a === void 0 ? void 0 : _a.enumerate());
28975
+ const devicesDescriptor = (_b = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _b !== void 0 ? _b : [];
28976
+ const { deviceList } = yield DevicePool.getDevices(devicesDescriptor, connectId);
28977
+ if (deviceList.length === 1 && ((_d = (_c = deviceList[0]) === null || _c === void 0 ? void 0 : _c.features) === null || _d === void 0 ? void 0 : _d.bootloader_mode)) {
28978
+ this.device.updateFromCache(deviceList[0]);
28979
+ this.device.commands.disposed = false;
28980
+ if (intervalTimer)
28981
+ clearInterval(intervalTimer);
28982
+ if (timeoutTimer)
28983
+ clearTimeout(timeoutTimer);
28984
+ (_e = this.checkPromise) === null || _e === void 0 ? void 0 : _e.resolve(true);
28985
+ return true;
28986
+ }
28987
+ return false;
28988
+ });
28989
+ }
28990
+ enterBootloaderMode() {
28991
+ var _a;
28992
+ return __awaiter(this, void 0, void 0, function* () {
28993
+ const typedCall = this.device.getCommands().typedCall.bind(this.device.getCommands());
28994
+ if (this.device.features && !this.device.features.bootloader_mode) {
28995
+ const uuid = getDeviceUUID(this.device.features);
28996
+ const deviceType = getDeviceType(this.device.features);
28997
+ try {
28998
+ this.postTipMessage(exports.FirmwareUpdateTipMessage.AutoRebootToBootloader);
28999
+ const bootRes = yield typedCall('DeviceBackToBoot', 'Success');
29000
+ if (bootRes.type === 'CallMethodError') {
29001
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.FirmwareUpdateAutoEnterBootFailure);
29002
+ }
29003
+ this.postTipMessage(exports.FirmwareUpdateTipMessage.GoToBootloaderSuccess);
29004
+ this.checkDeviceToBootloader(this.payload.connectId);
29005
+ if (DeviceModelToTypes.model_classic.includes(deviceType)) {
29006
+ DevicePool.clearDeviceCache(uuid);
29007
+ }
29008
+ delete DevicePool.devicesCache[''];
29009
+ yield ((_a = this.checkPromise) === null || _a === void 0 ? void 0 : _a.promise);
29010
+ this.checkPromise = null;
29011
+ const isTouch = DeviceModelToTypes.model_touch.includes(deviceType);
29012
+ yield wait(isTouch ? 3000 : 1500);
29013
+ yield this.device.acquire();
29014
+ return true;
29015
+ }
29016
+ catch (e) {
29017
+ if (e instanceof hdShared.HardwareError) {
29018
+ return Promise.reject(e);
29019
+ }
29020
+ console.log('auto go to bootloader mode failed: ', e);
29021
+ return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.FirmwareUpdateAutoEnterBootFailure));
29022
+ }
29023
+ }
29024
+ });
29025
+ }
29026
+ startEmmcFirmwareUpdate({ path }) {
29027
+ return __awaiter(this, void 0, void 0, function* () {
29028
+ const typedCall = this.device.getCommands().typedCall.bind(this.device.getCommands());
29029
+ const updaeteResponse = yield typedCall('FirmwareUpdateEmmc', 'Success', {
29030
+ path,
29031
+ reboot_on_success: true,
29032
+ });
29033
+ if (updaeteResponse.type !== 'Success') {
29034
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.FirmwareError, 'firmware update error');
29035
+ }
29036
+ this.postTipMessage(exports.FirmwareUpdateTipMessage.FirmwareUpdating);
29037
+ });
29038
+ }
29039
+ createUpdatesFolderIfNotExists(path) {
29040
+ return __awaiter(this, void 0, void 0, function* () {
29041
+ const typedCall = this.device.getCommands().typedCall.bind(this.device.getCommands());
29042
+ yield typedCall('EmmcDirMake', 'Success', {
29043
+ path,
29044
+ });
29045
+ });
29046
+ }
29047
+ emmcCommonUpdateProcess({ payload, filePath, processedSize, totalSize, }) {
29048
+ return __awaiter(this, void 0, void 0, function* () {
29049
+ if (!filePath.startsWith('0:')) {
29050
+ throw new Error('filePath must start with 0:');
29051
+ }
29052
+ const env = DataManager.getSettings('env');
29053
+ const perPackageSize = DataManager.isBleConnect(env) ? 16 : 128;
29054
+ const chunkSize = 1024 * perPackageSize;
29055
+ const totalChunks = Math.ceil(payload.byteLength / chunkSize);
29056
+ let offset = 0;
29057
+ let currentFileProcessed = 0;
29058
+ for (let i = 0; i < totalChunks; i++) {
29059
+ const chunkStart = i * chunkSize;
29060
+ const chunkEnd = Math.min(chunkStart + chunkSize, payload.byteLength);
29061
+ const chunkLength = chunkEnd - chunkStart;
29062
+ const chunk = payload.slice(chunkStart, chunkEnd);
29063
+ const overwrite = i === 0;
29064
+ let progress;
29065
+ if (totalSize !== undefined && processedSize !== undefined) {
29066
+ currentFileProcessed = processedSize + chunkEnd;
29067
+ progress = Math.min(Math.ceil((currentFileProcessed / totalSize) * 100), 99);
29068
+ }
29069
+ else {
29070
+ progress = Math.min(Math.ceil(((i + 1) / totalChunks) * 100), 99);
29071
+ }
29072
+ const writeRes = yield this.emmcFileWriteWithRetry(filePath, chunkLength, offset, chunk, overwrite, progress);
29073
+ offset += writeRes.message.processed_byte;
29074
+ this.postProgressMessage(progress, 'transferData');
29075
+ }
29076
+ return totalSize !== undefined ? (processedSize !== null && processedSize !== void 0 ? processedSize : 0) + payload.byteLength : 0;
29077
+ });
29078
+ }
29079
+ emmcFileWriteWithRetry(filePath, chunkLength, offset, chunk, overwrite, progress) {
29080
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
29081
+ return __awaiter(this, void 0, void 0, function* () {
29082
+ const writeFunc = () => __awaiter(this, void 0, void 0, function* () {
29083
+ var _k;
29084
+ const typedCall = this.device.getCommands().typedCall.bind(this.device.getCommands());
29085
+ const writeRes = yield typedCall('EmmcFileWrite', 'EmmcFile', {
29086
+ file: {
29087
+ path: filePath,
29088
+ len: chunkLength,
29089
+ offset,
29090
+ data: chunk,
29091
+ },
29092
+ overwrite,
29093
+ append: offset !== 0,
29094
+ ui_percentage: progress,
29095
+ });
29096
+ if (writeRes.type !== 'EmmcFile') {
29097
+ if (writeRes.type === 'CallMethodError') {
29098
+ if (((_k = writeRes.message.error) !== null && _k !== void 0 ? _k : '').indexOf(SESSION_ERROR) > -1) {
29099
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, SESSION_ERROR);
29100
+ }
29101
+ }
29102
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.EmmcFileWriteFirmwareError, 'transfer data error');
29103
+ }
29104
+ return writeRes;
29105
+ });
29106
+ let retryCount = 10;
29107
+ while (retryCount > 0) {
29108
+ try {
29109
+ const result = yield writeFunc();
29110
+ return result;
29111
+ }
29112
+ catch (error) {
29113
+ Log$7.error(`emmcWrite error: `, error);
29114
+ retryCount--;
29115
+ if (retryCount === 0) {
29116
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.EmmcFileWriteFirmwareError, 'transfer data error');
29117
+ }
29118
+ const env = DataManager.getSettings('env');
29119
+ if (DataManager.isBleConnect(env)) {
29120
+ yield wait(3000);
29121
+ yield ((_a = this.device.deviceConnector) === null || _a === void 0 ? void 0 : _a.acquire(this.device.originalDescriptor.id, null, true));
29122
+ yield this.device.initialize();
29123
+ }
29124
+ else if (((_b = error === null || error === void 0 ? void 0 : error.message) === null || _b === void 0 ? void 0 : _b.indexOf(SESSION_ERROR)) > -1 ||
29125
+ ((_d = (_c = error === null || error === void 0 ? void 0 : error.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.indexOf(SESSION_ERROR)) > -1) {
29126
+ const deviceDiff = yield ((_e = this.device.deviceConnector) === null || _e === void 0 ? void 0 : _e.enumerate());
29127
+ const devicesDescriptor = (_f = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _f !== void 0 ? _f : [];
29128
+ const { deviceList } = yield DevicePool.getDevices(devicesDescriptor, undefined);
29129
+ if (deviceList.length === 1 && ((_h = (_g = deviceList[0]) === null || _g === void 0 ? void 0 : _g.features) === null || _h === void 0 ? void 0 : _h.bootloader_mode)) {
29130
+ this.device.updateFromCache(deviceList[0]);
29131
+ yield this.device.acquire();
29132
+ this.device.getCommands().mainId = (_j = this.device.mainId) !== null && _j !== void 0 ? _j : '';
29133
+ }
29134
+ }
29135
+ yield wait(2000);
29136
+ }
29137
+ }
29138
+ });
29139
+ }
29140
+ reboot(rebootType) {
29141
+ return __awaiter(this, void 0, void 0, function* () {
29142
+ const typedCall = this.device.getCommands().typedCall.bind(this.device.getCommands());
29143
+ try {
29144
+ const res = yield typedCall('Reboot', 'Success', {
29145
+ reboot_type: rebootType,
29146
+ });
29147
+ return res.message;
29148
+ }
29149
+ catch (error) {
29150
+ if (error instanceof Error &&
29151
+ (error.message.includes('device was disconnected') ||
29152
+ error.message.includes('transferIn') ||
29153
+ error.message.includes('USBDevice'))) {
29154
+ return { message: 'Device rebooted successfully' };
29155
+ }
29156
+ throw error;
29157
+ }
29158
+ });
29159
+ }
29160
+ }
29161
+
29162
+ class DeviceUpdateBootloader extends FirmwareUpdateBaseMethod {
28874
29163
  init() {
28875
29164
  this.allowDeviceMode = [UI_REQUEST.BOOTLOADER, UI_REQUEST.NOT_INITIALIZE];
28876
29165
  this.requireDeviceMode = [];
28877
29166
  this.useDevicePassphraseState = false;
28878
29167
  this.skipForceUpdateCheck = true;
28879
29168
  }
29169
+ updateBootloaderWithEmmcFileWrite(_device, binary) {
29170
+ return __awaiter(this, void 0, void 0, function* () {
29171
+ const filePath = '0:boot/bootloader.bin';
29172
+ this.postTipMessage(exports.FirmwareUpdateTipMessage.StartTransferData);
29173
+ yield this.emmcCommonUpdateProcess({
29174
+ payload: binary,
29175
+ filePath,
29176
+ });
29177
+ this.postTipMessage(exports.FirmwareUpdateTipMessage.ConfirmOnDevice);
29178
+ yield this.reboot(hdTransport.RebootType.Normal);
29179
+ this.postTipMessage(exports.FirmwareUpdateTipMessage.UpdateBootloaderSuccess);
29180
+ return true;
29181
+ });
29182
+ }
28880
29183
  updateTouchBootloader(device, features) {
28881
29184
  return __awaiter(this, void 0, void 0, function* () {
28882
- if (features && !features.bootloader_mode) {
28883
- let { binary } = this.payload;
28884
- if (!binary) {
28885
- this.postTipMessage('CheckLatestUiResource');
28886
- const resourceUrl = DataManager.getBootloaderResource(features);
28887
- if (resourceUrl) {
28888
- this.postTipMessage('DownloadLatestBootloaderResource');
28889
- const resource = yield getSysResourceBinary(resourceUrl);
28890
- this.postTipMessage('DownloadLatestBootloaderResourceSuccess');
28891
- if (resource) {
28892
- binary = resource.binary;
28893
- }
29185
+ let { binary } = this.payload;
29186
+ if (!binary) {
29187
+ this.postTipMessage(exports.FirmwareUpdateTipMessage.CheckLatestUiResource);
29188
+ const resourceUrl = features ? DataManager.getBootloaderResource(features) : null;
29189
+ if (resourceUrl) {
29190
+ this.postTipMessage(exports.FirmwareUpdateTipMessage.DownloadLatestBootloaderResource);
29191
+ const resource = yield getSysResourceBinary(resourceUrl);
29192
+ this.postTipMessage(exports.FirmwareUpdateTipMessage.DownloadLatestBootloaderResourceSuccess);
29193
+ if (resource) {
29194
+ binary = resource.binary;
28894
29195
  }
28895
29196
  }
28896
- if (!checkBootloaderLength(binary)) {
28897
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CheckDownloadFileError);
28898
- }
29197
+ }
29198
+ if (!checkBootloaderLength(binary)) {
29199
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CheckDownloadFileError);
29200
+ }
29201
+ if (features && features.bootloader_mode) {
29202
+ this.postTipMessage(exports.FirmwareUpdateTipMessage.UpdateBootloader);
29203
+ return this.updateBootloaderWithEmmcFileWrite(device, binary);
29204
+ }
29205
+ if (features && !features.bootloader_mode) {
28899
29206
  yield updateBootloader(this.device.getCommands().typedCall.bind(this.device.getCommands()), this.postMessage, device, binary);
28900
29207
  return Promise.resolve(true);
28901
29208
  }
28902
- return Promise.resolve(true);
28903
29209
  });
28904
29210
  }
28905
29211
  run() {
@@ -28977,7 +29283,7 @@ class GetNextU2FCounter extends BaseMethod {
28977
29283
  }
28978
29284
  }
28979
29285
 
28980
- const Log$7 = getLogger(exports.LoggerNames.Method);
29286
+ const Log$6 = getLogger(exports.LoggerNames.Method);
28981
29287
  class FirmwareUpdate extends BaseMethod {
28982
29288
  constructor() {
28983
29289
  super(...arguments);
@@ -29015,7 +29321,7 @@ class FirmwareUpdate extends BaseMethod {
29015
29321
  this.checkPromise = hdShared.createDeferred();
29016
29322
  const env = DataManager.getSettings('env');
29017
29323
  const isBleReconnect = connectId && DataManager.isBleConnect(env);
29018
- Log$7.log('FirmwareUpdate [checkDeviceToBootloader] isBleReconnect: ', isBleReconnect);
29324
+ Log$6.log('FirmwareUpdate [checkDeviceToBootloader] isBleReconnect: ', isBleReconnect);
29019
29325
  const intervalTimer = setInterval(() => __awaiter(this, void 0, void 0, function* () {
29020
29326
  var _a, _b, _c, _d, _e, _f, _g, _h;
29021
29327
  if (isBleReconnect) {
@@ -29028,7 +29334,7 @@ class FirmwareUpdate extends BaseMethod {
29028
29334
  }
29029
29335
  }
29030
29336
  catch (e) {
29031
- Log$7.log('catch Bluetooth error when device is restarting: ', e);
29337
+ Log$6.log('catch Bluetooth error when device is restarting: ', e);
29032
29338
  }
29033
29339
  }
29034
29340
  else {
@@ -29082,7 +29388,7 @@ class FirmwareUpdate extends BaseMethod {
29082
29388
  if (e instanceof hdShared.HardwareError) {
29083
29389
  return Promise.reject(e);
29084
29390
  }
29085
- Log$7.log('auto go to bootloader mode failed: ', e);
29391
+ Log$6.log('auto go to bootloader mode failed: ', e);
29086
29392
  return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.FirmwareUpdateAutoEnterBootFailure));
29087
29393
  }
29088
29394
  }
@@ -29118,7 +29424,7 @@ class FirmwareUpdate extends BaseMethod {
29118
29424
  }
29119
29425
  }
29120
29426
 
29121
- const Log$6 = getLogger(exports.LoggerNames.Method);
29427
+ const Log$5 = getLogger(exports.LoggerNames.Method);
29122
29428
  class FirmwareUpdateV2 extends BaseMethod {
29123
29429
  constructor() {
29124
29430
  super(...arguments);
@@ -29180,7 +29486,7 @@ class FirmwareUpdateV2 extends BaseMethod {
29180
29486
  this.checkPromise = hdShared.createDeferred();
29181
29487
  const env = DataManager.getSettings('env');
29182
29488
  const isBleReconnect = connectId && DataManager.isBleConnect(env);
29183
- Log$6.log('FirmwareUpdateV2 [checkDeviceToBootloader] isBleReconnect: ', isBleReconnect);
29489
+ Log$5.log('FirmwareUpdateV2 [checkDeviceToBootloader] isBleReconnect: ', isBleReconnect);
29184
29490
  let isFirstCheck = true;
29185
29491
  let checkCount = 0;
29186
29492
  let timeoutTimer;
@@ -29189,10 +29495,10 @@ class FirmwareUpdateV2 extends BaseMethod {
29189
29495
  const intervalTimer = setInterval(() => __awaiter(this, void 0, void 0, function* () {
29190
29496
  var _c, _d, _e, _f;
29191
29497
  checkCount += 1;
29192
- Log$6.log('FirmwareUpdateV2 [checkDeviceToBootloader] isFirstCheck: ', isFirstCheck);
29498
+ Log$5.log('FirmwareUpdateV2 [checkDeviceToBootloader] isFirstCheck: ', isFirstCheck);
29193
29499
  if (isTouchOrProDevice && isFirstCheck) {
29194
29500
  isFirstCheck = false;
29195
- Log$6.log('FirmwareUpdateV2 [checkDeviceToBootloader] wait 3000ms');
29501
+ Log$5.log('FirmwareUpdateV2 [checkDeviceToBootloader] wait 3000ms');
29196
29502
  yield wait(3000);
29197
29503
  }
29198
29504
  if (checkCount > 4 && DataManager.isWebUsbConnect(DataManager.getSettings('env'))) {
@@ -29208,7 +29514,7 @@ class FirmwareUpdateV2 extends BaseMethod {
29208
29514
  }
29209
29515
  }
29210
29516
  catch (e) {
29211
- Log$6.log('FirmwareUpdateV2 [checkDeviceToBootloader] promptDeviceInBootloaderForWebDevice failed: ', e);
29517
+ Log$5.log('FirmwareUpdateV2 [checkDeviceToBootloader] promptDeviceInBootloaderForWebDevice failed: ', e);
29212
29518
  (_c = this.checkPromise) === null || _c === void 0 ? void 0 : _c.reject(e);
29213
29519
  }
29214
29520
  return;
@@ -29223,7 +29529,7 @@ class FirmwareUpdateV2 extends BaseMethod {
29223
29529
  }
29224
29530
  }
29225
29531
  catch (e) {
29226
- Log$6.log('catch Bluetooth error when device is restarting: ', e);
29532
+ Log$5.log('catch Bluetooth error when device is restarting: ', e);
29227
29533
  }
29228
29534
  }
29229
29535
  else {
@@ -29380,297 +29686,6 @@ class FirmwareUpdateV2 extends BaseMethod {
29380
29686
  }
29381
29687
  }
29382
29688
 
29383
- const Log$5 = getLogger(exports.LoggerNames.Method);
29384
- const SESSION_ERROR = 'session not found';
29385
- class FirmwareUpdateBaseMethod extends BaseMethod {
29386
- constructor() {
29387
- super(...arguments);
29388
- this.checkPromise = null;
29389
- this.postTipMessage = (message) => {
29390
- this.postMessage(createUiMessage(UI_REQUEST.FIRMWARE_TIP, {
29391
- device: this.device.toMessageObject(),
29392
- data: {
29393
- message,
29394
- },
29395
- }));
29396
- };
29397
- this.postProcessingMessage = (type) => {
29398
- this.postMessage(createUiMessage(UI_REQUEST.FIRMWARE_PROCESSING, {
29399
- type,
29400
- }));
29401
- };
29402
- this.postProgressMessage = (progress, progressType) => {
29403
- this.postMessage(createUiMessage(UI_REQUEST.FIRMWARE_PROGRESS, {
29404
- device: this.device.toMessageObject(),
29405
- progress,
29406
- progressType,
29407
- }));
29408
- };
29409
- }
29410
- init() { }
29411
- run() {
29412
- return Promise.resolve();
29413
- }
29414
- isBleReconnect() {
29415
- const env = DataManager.getSettings('env');
29416
- return this.payload.connectId && DataManager.isBleConnect(env);
29417
- }
29418
- _promptDeviceInBootloaderForWebDevice() {
29419
- return __awaiter(this, void 0, void 0, function* () {
29420
- return new Promise((resolve, reject) => {
29421
- if (this.device.listenerCount(DEVICE.SELECT_DEVICE_IN_BOOTLOADER_FOR_WEB_DEVICE) > 0) {
29422
- this.device.emit(DEVICE.SELECT_DEVICE_IN_BOOTLOADER_FOR_WEB_DEVICE, this.device, (err, deviceId) => {
29423
- if (err) {
29424
- reject(err);
29425
- }
29426
- else {
29427
- resolve(deviceId);
29428
- }
29429
- });
29430
- }
29431
- });
29432
- });
29433
- }
29434
- checkDeviceToBootloader(connectId) {
29435
- var _a, _b;
29436
- this.checkPromise = hdShared.createDeferred();
29437
- const env = DataManager.getSettings('env');
29438
- const isBleReconnect = connectId && DataManager.isBleConnect(env);
29439
- Log$5.log('FirmwareUpdateBaseMethod [checkDeviceToBootloader] isBleReconnect: ', isBleReconnect);
29440
- let isFirstCheck = true;
29441
- let checkCount = 0;
29442
- let timeoutTimer;
29443
- const isTouchOrProDevice = getDeviceType((_a = this === null || this === void 0 ? void 0 : this.device) === null || _a === void 0 ? void 0 : _a.features) === hdShared.EDeviceType.Touch ||
29444
- getDeviceType((_b = this === null || this === void 0 ? void 0 : this.device) === null || _b === void 0 ? void 0 : _b.features) === hdShared.EDeviceType.Pro;
29445
- const intervalTimer = setInterval(() => __awaiter(this, void 0, void 0, function* () {
29446
- var _c, _d, _e, _f;
29447
- checkCount += 1;
29448
- Log$5.log('FirmwareUpdateBaseMethod [checkDeviceToBootloader] isFirstCheck: ', isFirstCheck);
29449
- if (isTouchOrProDevice && isFirstCheck) {
29450
- isFirstCheck = false;
29451
- Log$5.log('FirmwareUpdateBaseMethod [checkDeviceToBootloader] wait 3000ms');
29452
- yield wait(3000);
29453
- }
29454
- if (checkCount > 4 && DataManager.isWebUsbConnect(DataManager.getSettings('env'))) {
29455
- clearInterval(intervalTimer);
29456
- clearTimeout(timeoutTimer);
29457
- try {
29458
- this.postTipMessage(exports.FirmwareUpdateTipMessage.SelectDeviceInBootloaderForWebDevice);
29459
- const confirmed = yield this._promptDeviceInBootloaderForWebDevice();
29460
- if (confirmed) {
29461
- yield this._checkDeviceInBootloaderMode(connectId, intervalTimer, timeoutTimer);
29462
- }
29463
- }
29464
- catch (e) {
29465
- Log$5.log('FirmwareUpdateBaseMethod [checkDeviceToBootloader] promptDeviceInBootloaderForWebDevice failed: ', e);
29466
- (_c = this.checkPromise) === null || _c === void 0 ? void 0 : _c.reject(e);
29467
- }
29468
- return;
29469
- }
29470
- if (isBleReconnect) {
29471
- try {
29472
- yield ((_d = this.device.deviceConnector) === null || _d === void 0 ? void 0 : _d.acquire(this.device.originalDescriptor.id, null, true));
29473
- yield this.device.initialize();
29474
- if ((_e = this.device.features) === null || _e === void 0 ? void 0 : _e.bootloader_mode) {
29475
- clearInterval(intervalTimer);
29476
- (_f = this.checkPromise) === null || _f === void 0 ? void 0 : _f.resolve(true);
29477
- }
29478
- }
29479
- catch (e) {
29480
- Log$5.log('catch Bluetooth error when device is restarting: ', e);
29481
- }
29482
- }
29483
- else {
29484
- yield this._checkDeviceInBootloaderMode(connectId, intervalTimer, timeoutTimer);
29485
- }
29486
- }), isBleReconnect ? 3000 : 2000);
29487
- timeoutTimer = setTimeout(() => {
29488
- if (this.checkPromise) {
29489
- clearInterval(intervalTimer);
29490
- this.checkPromise.reject(new Error());
29491
- }
29492
- }, 30000);
29493
- }
29494
- _checkDeviceInBootloaderMode(connectId, intervalTimer, timeoutTimer) {
29495
- var _a, _b, _c, _d, _e;
29496
- return __awaiter(this, void 0, void 0, function* () {
29497
- const deviceDiff = yield ((_a = this.device.deviceConnector) === null || _a === void 0 ? void 0 : _a.enumerate());
29498
- const devicesDescriptor = (_b = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _b !== void 0 ? _b : [];
29499
- const { deviceList } = yield DevicePool.getDevices(devicesDescriptor, connectId);
29500
- if (deviceList.length === 1 && ((_d = (_c = deviceList[0]) === null || _c === void 0 ? void 0 : _c.features) === null || _d === void 0 ? void 0 : _d.bootloader_mode)) {
29501
- this.device.updateFromCache(deviceList[0]);
29502
- this.device.commands.disposed = false;
29503
- if (intervalTimer)
29504
- clearInterval(intervalTimer);
29505
- if (timeoutTimer)
29506
- clearTimeout(timeoutTimer);
29507
- (_e = this.checkPromise) === null || _e === void 0 ? void 0 : _e.resolve(true);
29508
- return true;
29509
- }
29510
- return false;
29511
- });
29512
- }
29513
- enterBootloaderMode() {
29514
- var _a;
29515
- return __awaiter(this, void 0, void 0, function* () {
29516
- const typedCall = this.device.getCommands().typedCall.bind(this.device.getCommands());
29517
- if (this.device.features && !this.device.features.bootloader_mode) {
29518
- const uuid = getDeviceUUID(this.device.features);
29519
- const deviceType = getDeviceType(this.device.features);
29520
- try {
29521
- this.postTipMessage(exports.FirmwareUpdateTipMessage.AutoRebootToBootloader);
29522
- const bootRes = yield typedCall('DeviceBackToBoot', 'Success');
29523
- if (bootRes.type === 'CallMethodError') {
29524
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.FirmwareUpdateAutoEnterBootFailure);
29525
- }
29526
- this.postTipMessage(exports.FirmwareUpdateTipMessage.GoToBootloaderSuccess);
29527
- this.checkDeviceToBootloader(this.payload.connectId);
29528
- if (DeviceModelToTypes.model_classic.includes(deviceType)) {
29529
- DevicePool.clearDeviceCache(uuid);
29530
- }
29531
- delete DevicePool.devicesCache[''];
29532
- yield ((_a = this.checkPromise) === null || _a === void 0 ? void 0 : _a.promise);
29533
- this.checkPromise = null;
29534
- const isTouch = DeviceModelToTypes.model_touch.includes(deviceType);
29535
- yield wait(isTouch ? 3000 : 1500);
29536
- yield this.device.acquire();
29537
- return true;
29538
- }
29539
- catch (e) {
29540
- if (e instanceof hdShared.HardwareError) {
29541
- return Promise.reject(e);
29542
- }
29543
- console.log('auto go to bootloader mode failed: ', e);
29544
- return Promise.reject(hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.FirmwareUpdateAutoEnterBootFailure));
29545
- }
29546
- }
29547
- });
29548
- }
29549
- startEmmcFirmwareUpdate({ path }) {
29550
- return __awaiter(this, void 0, void 0, function* () {
29551
- const typedCall = this.device.getCommands().typedCall.bind(this.device.getCommands());
29552
- const updaeteResponse = yield typedCall('FirmwareUpdateEmmc', 'Success', {
29553
- path,
29554
- reboot_on_success: true,
29555
- });
29556
- if (updaeteResponse.type !== 'Success') {
29557
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.FirmwareError, 'firmware update error');
29558
- }
29559
- this.postTipMessage(exports.FirmwareUpdateTipMessage.FirmwareUpdating);
29560
- });
29561
- }
29562
- createUpdatesFolderIfNotExists(path) {
29563
- return __awaiter(this, void 0, void 0, function* () {
29564
- const typedCall = this.device.getCommands().typedCall.bind(this.device.getCommands());
29565
- yield typedCall('EmmcDirMake', 'Success', {
29566
- path,
29567
- });
29568
- });
29569
- }
29570
- emmcCommonUpdateProcess({ payload, filePath, processedSize, totalSize, }) {
29571
- return __awaiter(this, void 0, void 0, function* () {
29572
- if (!filePath.startsWith('0:')) {
29573
- throw new Error('filePath must start with 0:');
29574
- }
29575
- const env = DataManager.getSettings('env');
29576
- const perPackageSize = DataManager.isBleConnect(env) ? 16 : 128;
29577
- const chunkSize = 1024 * perPackageSize;
29578
- const totalChunks = Math.ceil(payload.byteLength / chunkSize);
29579
- let offset = 0;
29580
- let currentFileProcessed = 0;
29581
- for (let i = 0; i < totalChunks; i++) {
29582
- const chunkStart = i * chunkSize;
29583
- const chunkEnd = Math.min(chunkStart + chunkSize, payload.byteLength);
29584
- const chunkLength = chunkEnd - chunkStart;
29585
- const chunk = payload.slice(chunkStart, chunkEnd);
29586
- const overwrite = i === 0;
29587
- let progress;
29588
- if (totalSize !== undefined && processedSize !== undefined) {
29589
- currentFileProcessed = processedSize + chunkEnd;
29590
- progress = Math.min(Math.ceil((currentFileProcessed / totalSize) * 100), 99);
29591
- }
29592
- else {
29593
- progress = Math.min(Math.ceil(((i + 1) / totalChunks) * 100), 99);
29594
- }
29595
- const writeRes = yield this.emmcFileWriteWithRetry(filePath, chunkLength, offset, chunk, overwrite, progress);
29596
- offset += writeRes.message.processed_byte;
29597
- this.postProgressMessage(progress, 'transferData');
29598
- }
29599
- return totalSize !== undefined ? (processedSize !== null && processedSize !== void 0 ? processedSize : 0) + payload.byteLength : 0;
29600
- });
29601
- }
29602
- emmcFileWriteWithRetry(filePath, chunkLength, offset, chunk, overwrite, progress) {
29603
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
29604
- return __awaiter(this, void 0, void 0, function* () {
29605
- const writeFunc = () => __awaiter(this, void 0, void 0, function* () {
29606
- var _k;
29607
- const typedCall = this.device.getCommands().typedCall.bind(this.device.getCommands());
29608
- const writeRes = yield typedCall('EmmcFileWrite', 'EmmcFile', {
29609
- file: {
29610
- path: filePath,
29611
- len: chunkLength,
29612
- offset,
29613
- data: chunk,
29614
- },
29615
- overwrite,
29616
- append: offset !== 0,
29617
- ui_percentage: progress,
29618
- });
29619
- if (writeRes.type !== 'EmmcFile') {
29620
- if (writeRes.type === 'CallMethodError') {
29621
- if (((_k = writeRes.message.error) !== null && _k !== void 0 ? _k : '').indexOf(SESSION_ERROR) > -1) {
29622
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, SESSION_ERROR);
29623
- }
29624
- }
29625
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.EmmcFileWriteFirmwareError, 'transfer data error');
29626
- }
29627
- return writeRes;
29628
- });
29629
- let retryCount = 10;
29630
- while (retryCount > 0) {
29631
- try {
29632
- const result = yield writeFunc();
29633
- return result;
29634
- }
29635
- catch (error) {
29636
- Log$5.error(`emmcWrite error: `, error);
29637
- retryCount--;
29638
- if (retryCount === 0) {
29639
- throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.EmmcFileWriteFirmwareError, 'transfer data error');
29640
- }
29641
- const env = DataManager.getSettings('env');
29642
- if (DataManager.isBleConnect(env)) {
29643
- yield wait(3000);
29644
- yield ((_a = this.device.deviceConnector) === null || _a === void 0 ? void 0 : _a.acquire(this.device.originalDescriptor.id, null, true));
29645
- yield this.device.initialize();
29646
- }
29647
- else if (((_b = error === null || error === void 0 ? void 0 : error.message) === null || _b === void 0 ? void 0 : _b.indexOf(SESSION_ERROR)) > -1 ||
29648
- ((_d = (_c = error === null || error === void 0 ? void 0 : error.response) === null || _c === void 0 ? void 0 : _c.data) === null || _d === void 0 ? void 0 : _d.indexOf(SESSION_ERROR)) > -1) {
29649
- const deviceDiff = yield ((_e = this.device.deviceConnector) === null || _e === void 0 ? void 0 : _e.enumerate());
29650
- const devicesDescriptor = (_f = deviceDiff === null || deviceDiff === void 0 ? void 0 : deviceDiff.descriptors) !== null && _f !== void 0 ? _f : [];
29651
- const { deviceList } = yield DevicePool.getDevices(devicesDescriptor, undefined);
29652
- if (deviceList.length === 1 && ((_h = (_g = deviceList[0]) === null || _g === void 0 ? void 0 : _g.features) === null || _h === void 0 ? void 0 : _h.bootloader_mode)) {
29653
- this.device.updateFromCache(deviceList[0]);
29654
- yield this.device.acquire();
29655
- this.device.getCommands().mainId = (_j = this.device.mainId) !== null && _j !== void 0 ? _j : '';
29656
- }
29657
- }
29658
- yield wait(2000);
29659
- }
29660
- }
29661
- });
29662
- }
29663
- reboot(rebootType) {
29664
- return __awaiter(this, void 0, void 0, function* () {
29665
- const typedCall = this.device.getCommands().typedCall.bind(this.device.getCommands());
29666
- const res = yield typedCall('Reboot', 'Success', {
29667
- reboot_type: rebootType,
29668
- });
29669
- return res.message;
29670
- });
29671
- }
29672
- }
29673
-
29674
29689
  const Log$4 = getLogger(exports.LoggerNames.Method);
29675
29690
  const MIN_UPDATE_V3_BOOTLOADER_VERSION = '2.8.0';
29676
29691
  class FirmwareUpdateV3 extends FirmwareUpdateBaseMethod {
@@ -30071,37 +30086,6 @@ class PromptWebDeviceAccess extends BaseMethod {
30071
30086
  }
30072
30087
  }
30073
30088
 
30074
- class EmmcFileWrite extends FirmwareUpdateBaseMethod {
30075
- init() {
30076
- const { payload } = this;
30077
- validateParams(payload, [
30078
- { name: 'payload', type: 'buffer', required: true },
30079
- { name: 'filePath', type: 'string', required: true },
30080
- ]);
30081
- this.allowDeviceMode = [UI_REQUEST.BOOTLOADER, UI_REQUEST.NOT_INITIALIZE];
30082
- this.useDevicePassphraseState = false;
30083
- this.params = {
30084
- payload: payload.payload,
30085
- filePath: payload.filePath,
30086
- };
30087
- }
30088
- run() {
30089
- return __awaiter(this, void 0, void 0, function* () {
30090
- const { payload, filePath } = this.params;
30091
- yield this.enterBootloaderMode();
30092
- yield this.emmcCommonUpdateProcess({ payload, filePath });
30093
- try {
30094
- yield this.reboot(hdTransport.RebootType.Normal);
30095
- }
30096
- catch (e) {
30097
- }
30098
- return {
30099
- filePath,
30100
- };
30101
- });
30102
- }
30103
- }
30104
-
30105
30089
  class CipherKeyValue extends BaseMethod {
30106
30090
  constructor() {
30107
30091
  super(...arguments);
@@ -32957,14 +32941,9 @@ class EVMSignTypedData extends BaseMethod {
32957
32941
  throw hdShared.ERRORS.TypedError('Device_InitializeFailed', 'Device initialization failed. Please try again.');
32958
32942
  }
32959
32943
  const { addressN, chainId } = this.params;
32944
+ const supportEip712OnClassic = existCapability(this.device.features, hdTransport.Enum_Capability.Capability_EthereumTypedData);
32960
32945
  const deviceType = getDeviceType(this.device.features);
32961
- if (DeviceModelToTypes.model_mini.includes(deviceType)) {
32962
- const currentVersion = getDeviceFirmwareVersion(this.device.features).join('.');
32963
- const isClassic1sOrPure = deviceType === hdShared.EDeviceType.Classic1s || deviceType === hdShared.EDeviceType.ClassicPure;
32964
- if ((isClassic1sOrPure && semver__default["default"].gte(currentVersion, '3.14.0')) ||
32965
- existCapability(this.device.features, hdTransport.Enum_Capability.Capability_EthereumTypedData)) {
32966
- return this.signTypedData();
32967
- }
32946
+ if (DeviceModelToTypes.model_mini.includes(deviceType) && !supportEip712OnClassic) {
32968
32947
  validateParams(this.params, [
32969
32948
  { name: 'domainHash', type: 'hexString', required: true },
32970
32949
  { name: 'messageHash', type: 'hexString', required: true },
@@ -38731,7 +38710,6 @@ var ApiMethods = /*#__PURE__*/Object.freeze({
38731
38710
  firmwareUpdateV2: FirmwareUpdateV2,
38732
38711
  firmwareUpdateV3: FirmwareUpdateV3,
38733
38712
  promptWebDeviceAccess: PromptWebDeviceAccess,
38734
- emmcFileWrite: EmmcFileWrite,
38735
38713
  cipherKeyValue: CipherKeyValue,
38736
38714
  allNetworkGetAddress: AllNetworkGetAddress,
38737
38715
  allNetworkGetAddressByLoop: AllNetworkGetAddressByLoop,