@onekeyfe/hd-core 0.1.56 → 0.1.57

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 (55) hide show
  1. package/dist/api/CheckBLEFirmwareRelease.d.ts +1 -1
  2. package/dist/api/CheckFirmwareRelease.d.ts +1 -1
  3. package/dist/api/FirmwareUpdateV2.d.ts +3 -0
  4. package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
  5. package/dist/api/aptos/AptosGetAddress.d.ts.map +1 -1
  6. package/dist/api/device/DeviceUploadResource.d.ts +18 -0
  7. package/dist/api/device/DeviceUploadResource.d.ts.map +1 -0
  8. package/dist/api/device/DeviceVerify.d.ts.map +1 -1
  9. package/dist/api/firmware/getBinary.d.ts +3 -11
  10. package/dist/api/firmware/getBinary.d.ts.map +1 -1
  11. package/dist/api/firmware/releaseHelper.d.ts +2 -2
  12. package/dist/api/firmware/uploadFirmware.d.ts +5 -1
  13. package/dist/api/firmware/uploadFirmware.d.ts.map +1 -1
  14. package/dist/api/index.d.ts +1 -0
  15. package/dist/api/index.d.ts.map +1 -1
  16. package/dist/data-manager/DataManager.d.ts +3 -2
  17. package/dist/data-manager/DataManager.d.ts.map +1 -1
  18. package/dist/index.d.ts +24 -12
  19. package/dist/index.js +279 -66
  20. package/dist/inject.d.ts.map +1 -1
  21. package/dist/types/api/confluxSignTransaction.d.ts +1 -1
  22. package/dist/types/api/confluxSignTransaction.d.ts.map +1 -1
  23. package/dist/types/api/deviceUploadResource.d.ts +11 -0
  24. package/dist/types/api/deviceUploadResource.d.ts.map +1 -0
  25. package/dist/types/api/export.d.ts +2 -0
  26. package/dist/types/api/export.d.ts.map +1 -1
  27. package/dist/types/api/firmwareUpdate.d.ts +5 -5
  28. package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
  29. package/dist/types/api/index.d.ts +2 -0
  30. package/dist/types/api/index.d.ts.map +1 -1
  31. package/dist/types/settings.d.ts +1 -0
  32. package/dist/types/settings.d.ts.map +1 -1
  33. package/dist/utils/release.d.ts +4 -1
  34. package/dist/utils/release.d.ts.map +1 -1
  35. package/package.json +8 -8
  36. package/src/api/FirmwareUpdateV2.ts +62 -10
  37. package/src/api/aptos/AptosGetAddress.ts +4 -3
  38. package/src/api/conflux/ConfluxSignMessage.ts +1 -1
  39. package/src/api/conflux/ConfluxSignMessageCIP23.ts +1 -1
  40. package/src/api/conflux/ConfluxSignTransaction.ts +2 -2
  41. package/src/api/device/DeviceUploadResource.ts +104 -0
  42. package/src/api/device/DeviceVerify.ts +3 -2
  43. package/src/api/firmware/getBinary.ts +16 -3
  44. package/src/api/firmware/releaseHelper.ts +2 -2
  45. package/src/api/firmware/uploadFirmware.ts +100 -1
  46. package/src/api/index.ts +1 -0
  47. package/src/data-manager/DataManager.ts +35 -5
  48. package/src/inject.ts +2 -0
  49. package/src/types/api/confluxSignTransaction.ts +1 -1
  50. package/src/types/api/deviceUploadResource.ts +15 -0
  51. package/src/types/api/export.ts +2 -0
  52. package/src/types/api/firmwareUpdate.ts +5 -5
  53. package/src/types/api/index.ts +2 -0
  54. package/src/types/settings.ts +2 -0
  55. package/src/utils/release.ts +18 -1
@@ -1,9 +1,16 @@
1
+ import { blake2s } from '@noble/hashes/blake2s';
2
+ import JSZip from 'jszip';
1
3
  import { ERRORS, HardwareErrorCode } from '@onekeyfe/hd-shared';
4
+ import { Success } from '@onekeyfe/hd-transport';
5
+ import { wait, getLogger, LoggerNames } from '../../utils/index';
2
6
  import { DEVICE, CoreMessage, createUiMessage, UI_REQUEST } from '../../events';
3
7
  import { PROTO } from '../../constants';
4
8
  import type { Device } from '../../device/Device';
5
- import type { TypedCall } from '../../device/DeviceCommands';
9
+ import type { TypedCall, TypedResponseMessage } from '../../device/DeviceCommands';
6
10
  import { KnownDevice } from '../../types';
11
+ import { bytesToHex } from '../helpers/hexUtils';
12
+
13
+ const Log = getLogger(LoggerNames.Device);
7
14
 
8
15
  const postConfirmationMessage = (device: Device) => {
9
16
  // only if firmware is already installed. fresh device does not require button confirmation
@@ -40,6 +47,13 @@ const postProgressTip = (
40
47
  );
41
48
  };
42
49
 
50
+ export const waitBleInstall = async (updateType: string) => {
51
+ if (updateType === 'ble') {
52
+ // wait for device install
53
+ await wait(10 * 1000);
54
+ }
55
+ };
56
+
43
57
  export const uploadFirmware = async (
44
58
  updateType: 'firmware' | 'ble',
45
59
  typedCall: TypedCall,
@@ -58,6 +72,8 @@ export const uploadFirmware = async (
58
72
  payload,
59
73
  });
60
74
  postProgressMessage(device, 100, postMessage);
75
+
76
+ await waitBleInstall(updateType);
61
77
  return message;
62
78
  }
63
79
 
@@ -85,8 +101,91 @@ export const uploadFirmware = async (
85
101
  }
86
102
 
87
103
  postProgressMessage(device, 100, postMessage);
104
+
105
+ await waitBleInstall(updateType);
88
106
  return response.message;
89
107
  }
90
108
 
91
109
  throw ERRORS.TypedError(HardwareErrorCode.RuntimeError, 'uploadFirmware: unknown major_version');
92
110
  };
111
+
112
+ const processResourceRequest = async (
113
+ typedCall: TypedCall,
114
+ res: TypedResponseMessage<'ResourceRequest'> | TypedResponseMessage<'Success'>,
115
+ data: ArrayBuffer
116
+ ): Promise<Success> => {
117
+ if (res.type === 'Success') {
118
+ return res.message;
119
+ }
120
+
121
+ const { offset, data_length } = res.message;
122
+
123
+ if (offset === undefined) {
124
+ throw new Error('offset is undefined');
125
+ }
126
+
127
+ const payload = new Uint8Array(
128
+ data.slice(offset, Math.min(offset + data_length, data.byteLength))
129
+ );
130
+ const digest = blake2s(payload);
131
+
132
+ const resourceAckParams = {
133
+ data_chunk: bytesToHex(payload),
134
+ hash: bytesToHex(digest),
135
+ };
136
+
137
+ const response = await typedCall('ResourceAck', ['ResourceRequest', 'Success'], {
138
+ ...resourceAckParams,
139
+ });
140
+ return processResourceRequest(typedCall, response, data);
141
+ };
142
+
143
+ // Fixed size
144
+ const INIT_DATA_CHUNK_SIZE = 16 * 1024;
145
+ export const updateResource = async (typedCall: TypedCall, fileName: string, data: ArrayBuffer) => {
146
+ const chunk = new Uint8Array(data.slice(0, Math.min(INIT_DATA_CHUNK_SIZE, data.byteLength)));
147
+ const digest = blake2s(chunk);
148
+
149
+ const res = await typedCall('ResourceUpdate', ['ResourceRequest', 'Success'], {
150
+ file_name: fileName,
151
+ data_length: data.byteLength,
152
+ initial_data_chunk: bytesToHex(chunk),
153
+ hash: bytesToHex(digest),
154
+ });
155
+
156
+ return processResourceRequest(typedCall, res, data);
157
+ };
158
+
159
+ export const updateResources = async (
160
+ typedCall: TypedCall,
161
+ postMessage: (message: CoreMessage) => void,
162
+ device: Device,
163
+ source: ArrayBuffer
164
+ ) => {
165
+ postProgressTip(device, 'UpdateSysResource', postMessage);
166
+
167
+ const zipData = await JSZip.loadAsync(source);
168
+ const files = Object.entries(zipData.files);
169
+
170
+ let progress = 0;
171
+ const stepProgress = 100 / files.length;
172
+
173
+ for (const [fileName, file] of files) {
174
+ const name = fileName.split('/').pop();
175
+ if (!file.dir && fileName.indexOf('__MACOSX') === -1 && name) {
176
+ const data = await file.async('arraybuffer');
177
+ try {
178
+ await updateResource(typedCall, name, data);
179
+ } catch (error) {
180
+ Log.error(error);
181
+ }
182
+ }
183
+
184
+ progress += stepProgress;
185
+ postProgressMessage(device, Math.floor(progress), postMessage);
186
+ }
187
+
188
+ postProgressMessage(device, 100, postMessage);
189
+ postProgressTip(device, 'UpdateSysResourceSuccess', postMessage);
190
+ return true;
191
+ };
package/src/api/index.ts CHANGED
@@ -14,6 +14,7 @@ export { default as deviceRecovery } from './device/DeviceRecovery';
14
14
  export { default as deviceReset } from './device/DeviceReset';
15
15
  export { default as deviceSettings } from './device/DeviceSettings';
16
16
  export { default as deviceUpdateReboot } from './device/DeviceUpdateReboot';
17
+ export { default as deviceUploadResource } from './device/DeviceUploadResource';
17
18
  export { default as deviceSupportFeatures } from './device/DeviceSupportFeatures';
18
19
  export { default as deviceVerify } from './device/DeviceVerify';
19
20
  export { default as deviceWipe } from './device/DeviceWipe';
@@ -18,7 +18,7 @@ import type {
18
18
  IDeviceBLEFirmwareStatus,
19
19
  ITransportStatus,
20
20
  } from '../types';
21
- import { getReleaseChangelog, getReleaseStatus } from '../utils/release';
21
+ import { getReleaseChangelog, getReleaseStatus, findLatestRelease } from '../utils/release';
22
22
 
23
23
  export default class DataManager {
24
24
  static deviceMap: DeviceTypeMap = {
@@ -64,6 +64,25 @@ export default class DataManager {
64
64
  return getReleaseStatus(targetDeviceConfigList, currentVersion);
65
65
  };
66
66
 
67
+ /**
68
+ * Touch、Pro System UI Resource Update
69
+ * ** Interval upgrade is not considered **
70
+ */
71
+ static getSysResourcesLatestRelease = (features: Features) => {
72
+ const deviceType = getDeviceType(features);
73
+ const deviceFirmwareVersion = getDeviceFirmwareVersion(features);
74
+
75
+ if (deviceType !== 'pro' && deviceType !== 'touch') return undefined;
76
+
77
+ const targetDeviceConfigList = this.deviceMap[deviceType]?.firmware ?? [];
78
+ const currentVersion = deviceFirmwareVersion.join('.');
79
+ const targetDeviceConfig = targetDeviceConfigList.filter(
80
+ item => semver.gt(item.version.join('.'), currentVersion) && !!item.resource
81
+ );
82
+
83
+ return findLatestRelease(targetDeviceConfig)?.resource;
84
+ };
85
+
67
86
  static getFirmwareChangelog = (features: Features) => {
68
87
  const deviceType = getDeviceType(features);
69
88
  const deviceFirmwareVersion = getDeviceFirmwareVersion(features);
@@ -80,10 +99,21 @@ export default class DataManager {
80
99
  return getReleaseChangelog(targetDeviceConfigList, currentVersion);
81
100
  };
82
101
 
83
- static getFirmwareLeatestRelease = (features: Features) => {
102
+ static getFirmwareLatestRelease = (features: Features) => {
84
103
  const deviceType = getDeviceType(features);
85
104
  const targetDeviceConfigList = this.deviceMap[deviceType]?.firmware ?? [];
86
- return targetDeviceConfigList[targetDeviceConfigList.length - 1];
105
+
106
+ const target = findLatestRelease(targetDeviceConfigList);
107
+ if (!target) return target;
108
+
109
+ if (!target.resource) {
110
+ const resource = this.getSysResourcesLatestRelease(features);
111
+ return {
112
+ ...target,
113
+ resource,
114
+ };
115
+ }
116
+ return target;
87
117
  };
88
118
 
89
119
  static getBLEFirmwareStatus = (features: Features): IDeviceBLEFirmwareStatus => {
@@ -113,10 +143,10 @@ export default class DataManager {
113
143
  return getReleaseChangelog(targetDeviceConfigList, currentVersion);
114
144
  };
115
145
 
116
- static getBleFirmwareLeatestRelease = (features: Features) => {
146
+ static getBleFirmwareLatestRelease = (features: Features) => {
117
147
  const deviceType = getDeviceType(features);
118
148
  const targetDeviceConfigList = this.deviceMap[deviceType]?.ble ?? [];
119
- return targetDeviceConfigList[targetDeviceConfigList.length - 1];
149
+ return findLatestRelease(targetDeviceConfigList);
120
150
  };
121
151
 
122
152
  static getTransportStatus = (localVersion: string): ITransportStatus => {
package/src/inject.ts CHANGED
@@ -85,6 +85,8 @@ export const inject = ({
85
85
  deviceReset: (connectId, params) => call({ ...params, connectId, method: 'deviceReset' }),
86
86
  deviceSettings: (connectId, params) => call({ ...params, connectId, method: 'deviceSettings' }),
87
87
  deviceUpdateReboot: connectId => call({ connectId, method: 'deviceUpdateReboot' }),
88
+ deviceUploadResource: (connectId, params) =>
89
+ call({ ...params, connectId, method: 'deviceUploadResource' }),
88
90
  deviceSupportFeatures: connectId => call({ connectId, method: 'deviceSupportFeatures' }),
89
91
  deviceVerify: (connectId, params) => call({ ...params, connectId, method: 'deviceVerify' }),
90
92
  deviceWipe: connectId => call({ connectId, method: 'deviceWipe' }),
@@ -28,5 +28,5 @@ export type ConfluxSignTransactionParams = {
28
28
  export declare function confluxSignTransaction(
29
29
  connectId: string,
30
30
  deviceId: string,
31
- params: CommonParams & ConfluxTransaction
31
+ params: CommonParams & ConfluxSignTransactionParams
32
32
  ): Response<ConfluxSignedTx>;
@@ -0,0 +1,15 @@
1
+ import { ResourceType, Success } from '@onekeyfe/hd-transport';
2
+ import type { CommonParams, Response } from '../params';
3
+
4
+ export type DeviceUploadResourceParams = {
5
+ suffix: string;
6
+ dataHex: string;
7
+ thumbnailDataHex: string;
8
+ resType: ResourceType;
9
+ nftMetaData: string;
10
+ };
11
+
12
+ export declare function deviceUploadResource(
13
+ connectId: string,
14
+ params: CommonParams & DeviceUploadResourceParams
15
+ ): Response<Success>;
@@ -20,6 +20,8 @@ export type { DeviceResetParams } from './deviceReset';
20
20
  export type { DeviceSettingsParams } from './deviceSettings';
21
21
  export type { DeviceVerifyParams, DeviceVerifySignature } from './deviceVerify';
22
22
  export type { DeviceSupportFeatures } from './deviceSupportFeatures';
23
+ export type { DeviceUploadResourceParams } from './deviceUploadResource';
24
+ export type { FirmwareUpdateParams, FirmwareUpdateBinaryParams } from './firmwareUpdate';
23
25
 
24
26
  export type { EVMAddress, EVMGetAddressParams } from './evmGetAddress';
25
27
  export type { EVMPublicKey, EVMGetPublicKeyParams } from './evmGetPublicKey';
@@ -1,21 +1,21 @@
1
1
  import type { PROTO } from '../../constants';
2
2
  import type { Params, Response } from '../params';
3
3
 
4
- export interface FirmwareUpdateBinary {
4
+ export interface FirmwareUpdateBinaryParams {
5
5
  binary: ArrayBuffer;
6
6
  }
7
7
 
8
- export interface FirmwareUpdate {
9
- version: number[];
8
+ export interface FirmwareUpdateParams {
9
+ version?: number[];
10
10
  btcOnly?: boolean;
11
11
  updateType: 'firmware' | 'ble';
12
12
  }
13
13
 
14
14
  export declare function firmwareUpdate(
15
15
  connectId: string | undefined,
16
- params: Params<FirmwareUpdate>
16
+ params: Params<FirmwareUpdateParams>
17
17
  ): Response<PROTO.Success>;
18
18
  export declare function firmwareUpdate(
19
19
  connectId: string | undefined,
20
- params: Params<FirmwareUpdateBinary>
20
+ params: Params<FirmwareUpdateBinaryParams>
21
21
  ): Response<PROTO.Success>;
@@ -24,6 +24,7 @@ import { deviceChangePin } from './deviceChangePin';
24
24
  import { deviceSettings } from './deviceSettings';
25
25
  import { deviceFlags } from './deviceFlags';
26
26
  import { deviceUpdateReboot } from './deviceUpdateReboot';
27
+ import { deviceUploadResource } from './deviceUploadResource';
27
28
  import { deviceSupportFeatures } from './deviceSupportFeatures';
28
29
 
29
30
  import { cipherKeyValue } from './cipherKeyValue';
@@ -110,6 +111,7 @@ export type CoreApi = {
110
111
  deviceReset: typeof deviceReset;
111
112
  deviceSettings: typeof deviceSettings;
112
113
  deviceUpdateReboot: typeof deviceUpdateReboot;
114
+ deviceUploadResource: typeof deviceUploadResource;
113
115
  deviceSupportFeatures: typeof deviceSupportFeatures;
114
116
  deviceVerify: typeof deviceVerify;
115
117
  deviceWipe: typeof deviceWipe;
@@ -27,6 +27,8 @@ export type ILocale = 'zh-CN' | 'en-US';
27
27
  export type IFirmwareReleaseInfo = {
28
28
  required: boolean;
29
29
  url: string;
30
+ /** Firmware UI resource */
31
+ resource?: string;
30
32
  fingerprint: string;
31
33
  version: IVersionArray;
32
34
  changelog: {
@@ -1,5 +1,10 @@
1
1
  import semver from 'semver';
2
- import { IBLEFirmwareReleaseInfo, IDeviceFirmwareStatus, IFirmwareReleaseInfo } from '../types';
2
+ import {
3
+ IBLEFirmwareReleaseInfo,
4
+ IDeviceFirmwareStatus,
5
+ IFirmwareReleaseInfo,
6
+ IVersionArray,
7
+ } from '../types';
3
8
 
4
9
  export const getReleaseStatus = (
5
10
  releases: (IFirmwareReleaseInfo | IBLEFirmwareReleaseInfo)[],
@@ -22,3 +27,15 @@ export const getReleaseChangelog = (
22
27
  const newVersions = releases.filter(r => semver.gt(r.version.join('.'), currentVersion));
23
28
  return newVersions.map(r => r.changelog);
24
29
  };
30
+
31
+ export const findLatestRelease = <T extends { version: IVersionArray }>(
32
+ releases: T[]
33
+ ): T | undefined => {
34
+ let leastRelease = releases[0];
35
+ releases.forEach(release => {
36
+ if (semver.gt(release.version.join('.'), leastRelease.version.join('.'))) {
37
+ leastRelease = release;
38
+ }
39
+ });
40
+ return leastRelease;
41
+ };