@onekeyfe/hd-core 1.2.2-alpha.100 → 1.2.2-alpha.3

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 (69) hide show
  1. package/__tests__/AllNetworkGetAddressBase.tracing.test.ts +20 -4
  2. package/__tests__/DeviceCommands.test.ts +13 -0
  3. package/__tests__/check-all-firmware-release-protocol-v2.test.ts +0 -2
  4. package/__tests__/check-firmware-release-protocol-v2.test.ts +0 -2
  5. package/__tests__/device-lifecycle-events.test.ts +19 -0
  6. package/__tests__/deviceUploadNft.test.ts +68 -0
  7. package/__tests__/evmSignTypedData.test.ts +42 -0
  8. package/__tests__/firmware-update/firmware-update-v4-install-poll.test.ts +825 -12
  9. package/__tests__/open-wallet-session.test.ts +144 -37
  10. package/__tests__/pro2HostAssetPackage.test.ts +58 -0
  11. package/__tests__/protocol-v2-resources.test.ts +7 -4
  12. package/__tests__/protocol-v2-unlock-policy.test.ts +35 -1
  13. package/__tests__/protocol-v2.test.ts +720 -51
  14. package/dist/api/FirmwareUpdateV4.d.ts +4 -0
  15. package/dist/api/FirmwareUpdateV4.d.ts.map +1 -1
  16. package/dist/api/OpenWalletSession.d.ts.map +1 -1
  17. package/dist/api/allnetwork/AllNetworkGetAddressBase.d.ts.map +1 -1
  18. package/dist/api/evm/EVMSignTypedData.d.ts.map +1 -1
  19. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts +2 -2
  20. package/dist/api/firmware/FirmwareUpdateBaseMethod.d.ts.map +1 -1
  21. package/dist/api/firmware/getBinary.d.ts +2 -2
  22. package/dist/api/protocol-v2/DeviceGetFirmwareUpdateStatus.d.ts.map +1 -1
  23. package/dist/api/protocol-v2/DeviceUploadNft.d.ts.map +1 -1
  24. package/dist/api/protocol-v2/DeviceUploadWallpaper.d.ts.map +1 -1
  25. package/dist/api/protocol-v2/helpers.d.ts.map +1 -1
  26. package/dist/core/index.d.ts +1 -0
  27. package/dist/core/index.d.ts.map +1 -1
  28. package/dist/device/Device.d.ts +1 -1
  29. package/dist/device/Device.d.ts.map +1 -1
  30. package/dist/device/DeviceConnector.d.ts +1 -1
  31. package/dist/device/DeviceConnector.d.ts.map +1 -1
  32. package/dist/events/ui-request.d.ts +3 -0
  33. package/dist/events/ui-request.d.ts.map +1 -1
  34. package/dist/index.d.ts +21 -5
  35. package/dist/index.js +2288 -1439
  36. package/dist/protocols/protocol-v2/resources.d.ts.map +1 -1
  37. package/dist/protocols/protocol-v2/unlockPolicyRunner.d.ts +2 -0
  38. package/dist/protocols/protocol-v2/unlockPolicyRunner.d.ts.map +1 -1
  39. package/dist/protocols/protocol-v2/walletSession.d.ts +3 -0
  40. package/dist/protocols/protocol-v2/walletSession.d.ts.map +1 -1
  41. package/dist/types/settings.d.ts +2 -2
  42. package/dist/types/settings.d.ts.map +1 -1
  43. package/dist/utils/deviceFeaturesUtils.d.ts +3 -0
  44. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  45. package/dist/utils/patch.d.ts +1 -1
  46. package/dist/utils/patch.d.ts.map +1 -1
  47. package/dist/utils/pro2HostAssetPackage.d.ts +8 -0
  48. package/dist/utils/pro2HostAssetPackage.d.ts.map +1 -0
  49. package/package.json +4 -4
  50. package/src/api/FirmwareUpdateV4.ts +434 -75
  51. package/src/api/OpenWalletSession.ts +39 -9
  52. package/src/api/allnetwork/AllNetworkGetAddressBase.ts +5 -2
  53. package/src/api/evm/EVMSignTypedData.ts +1 -0
  54. package/src/api/firmware/FirmwareUpdateBaseMethod.ts +9 -3
  55. package/src/api/protocol-v2/DeviceGetFirmwareUpdateStatus.ts +7 -1
  56. package/src/api/protocol-v2/DeviceUploadNft.ts +31 -8
  57. package/src/api/protocol-v2/DeviceUploadWallpaper.ts +27 -8
  58. package/src/api/protocol-v2/helpers.ts +8 -0
  59. package/src/core/index.ts +130 -14
  60. package/src/data/messages/messages-protocol-v2.json +1541 -1332
  61. package/src/device/Device.ts +3 -1
  62. package/src/device/DeviceConnector.ts +7 -3
  63. package/src/events/ui-request.ts +3 -0
  64. package/src/protocols/protocol-v2/resources.ts +9 -1
  65. package/src/protocols/protocol-v2/unlockPolicyRunner.ts +8 -1
  66. package/src/protocols/protocol-v2/walletSession.ts +88 -50
  67. package/src/types/settings.ts +4 -2
  68. package/src/utils/deviceFeaturesUtils.ts +4 -0
  69. package/src/utils/pro2HostAssetPackage.ts +354 -0
@@ -0,0 +1,354 @@
1
+ import { sha3_512 } from '@noble/hashes/sha3';
2
+ import semver from 'semver';
3
+
4
+ /**
5
+ * Builds the unsigned host-asset package consumed by Pro2 firmware:
6
+ *
7
+ * OKPP RESOURCE container
8
+ * └── OKAR archive
9
+ * └── independently compressed raw LZ4 blocks
10
+ *
11
+ * OKPP and OKAR are OneKey formats. Their constants mirror firmware-pro2's
12
+ * payload_package headers; the LZ4 bytes inside each block follow the standard
13
+ * raw block format and deliberately do not use an LZ4 frame or size prefix.
14
+ */
15
+
16
+ /* eslint-disable no-bitwise */
17
+
18
+ export const PRO2_HOST_ASSET_PACKAGE_MIN_VERSION = '1.0.1';
19
+
20
+ // OKPP container layout. The fixed header has seven empty signature slots even
21
+ // for a host-generated unsigned package.
22
+ const CONTAINER_HEADER_SIZE = 0x5f90;
23
+ const CONTAINER_HEADER_HASH_INPUT_LENGTH = 0x240;
24
+ const CONTAINER_HASH_SECTION_OFFSET = 0x200;
25
+ const CONTAINER_SIGNATURE_ALGORITHM_OFFSET = 0x408;
26
+ const CONTAINER_HEADER_MAGIC = 0x50504b4f;
27
+ const CONTAINER_HEADER_VERSION = 1;
28
+ const CONTAINER_RESOURCE_TYPE_MAGIC = 0x43534552;
29
+ const CONTAINER_ED25519_SIGNATURE_ALGORITHM = 0x71717171;
30
+ const HOST_ASSET_PACKAGE_MAX_SIZE = 4 * 1024 * 1024;
31
+
32
+ // OKAR archive layout.
33
+ const ARCHIVE_MAGIC = 0x52414b4f;
34
+ const ARCHIVE_VERSION = 1;
35
+ const ARCHIVE_HEADER_SIZE = 42;
36
+ const ARCHIVE_ENTRY_SIZE = 296;
37
+ const ARCHIVE_ENTRY_NAME_MAX_LENGTH = 255;
38
+ const ARCHIVE_COMPRESS_LZ4_BLOCKED = 1;
39
+ const ARCHIVE_ALIGNMENT = 4;
40
+ const LZ4_BLOCK_SIZE_LOG2 = 12;
41
+
42
+ export type Pro2HostAssetPackageEntry = {
43
+ name: string;
44
+ data: Uint8Array;
45
+ };
46
+
47
+ type EncodedArchiveEntry = Pro2HostAssetPackageEntry & {
48
+ nameBytes: Uint8Array;
49
+ compressed: Uint8Array;
50
+ offset: number;
51
+ };
52
+
53
+ export function supportsPro2HostAssetPackage(firmwareVersion: string | undefined): boolean {
54
+ return Boolean(
55
+ firmwareVersion &&
56
+ semver.valid(firmwareVersion) &&
57
+ semver.gte(firmwareVersion, PRO2_HOST_ASSET_PACKAGE_MIN_VERSION)
58
+ );
59
+ }
60
+
61
+ function concatBytes(parts: Uint8Array[]): Uint8Array {
62
+ const output = new Uint8Array(parts.reduce((length, part) => length + part.byteLength, 0));
63
+ let offset = 0;
64
+ for (const part of parts) {
65
+ output.set(part, offset);
66
+ offset += part.byteLength;
67
+ }
68
+ return output;
69
+ }
70
+
71
+ // Raw LZ4 block encoder
72
+ // ---------------------
73
+ //
74
+ // This encoder is adapted from lz4-lite 1.1.2 and intentionally kept local so
75
+ // every SDK runtime uses the same dependency-free implementation. Keep this
76
+ // section isolated from the OneKey package writer below. Firmware requires raw
77
+ // blocks here; replacing it with an LZ4 frame encoder is not compatible.
78
+ //
79
+ // MIT License
80
+ // Copyright (c) 2026 Alexander Vukov
81
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
82
+ // of this software and associated documentation files (the "Software"), to deal
83
+ // in the Software without restriction, including without limitation the rights
84
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
85
+ // copies of the Software, and to permit persons to whom the Software is
86
+ // furnished to do so, subject to the following conditions:
87
+ // The above copyright notice and this permission notice shall be included in all
88
+ // copies or substantial portions of the Software.
89
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
90
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
91
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
92
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
93
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
94
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
95
+ // SOFTWARE.
96
+
97
+ const LZ4_MIN_MATCH = 4;
98
+ const LZ4_LAST_LITERALS = 5;
99
+ const LZ4_MATCH_FIND_LIMIT = 12;
100
+ const LZ4_MAX_OFFSET = 0xffff;
101
+ const LZ4_LENGTH_MASK = 15;
102
+ const LZ4_HASH_LOG = 16;
103
+ const LZ4_HASH_MULTIPLIER = 2654435761;
104
+ const LZ4_SKIP_TRIGGER = 6;
105
+
106
+ function writeExtendedLength(output: Uint8Array, offset: number, length: number): number {
107
+ let remaining = length;
108
+ let nextOffset = offset;
109
+ while (remaining >= 255) {
110
+ output[nextOffset] = 255;
111
+ nextOffset += 1;
112
+ remaining -= 255;
113
+ }
114
+ output[nextOffset] = remaining;
115
+ return nextOffset + 1;
116
+ }
117
+
118
+ function copyBytes(
119
+ output: Uint8Array,
120
+ outputOffset: number,
121
+ input: Uint8Array,
122
+ inputOffset: number,
123
+ length: number
124
+ ): number {
125
+ output.set(input.subarray(inputOffset, inputOffset + length), outputOffset);
126
+ return outputOffset + length;
127
+ }
128
+
129
+ function emitSequence(
130
+ output: Uint8Array,
131
+ outputOffset: number,
132
+ input: Uint8Array,
133
+ anchor: number,
134
+ literalLength: number,
135
+ matchOffset: number,
136
+ matchLengthCode: number
137
+ ): number {
138
+ const tokenOffset = outputOffset;
139
+ let nextOffset = outputOffset + 1;
140
+ let token = 0;
141
+
142
+ if (literalLength >= LZ4_LENGTH_MASK) {
143
+ token = LZ4_LENGTH_MASK << 4;
144
+ nextOffset = writeExtendedLength(output, nextOffset, literalLength - LZ4_LENGTH_MASK);
145
+ } else {
146
+ token = literalLength << 4;
147
+ }
148
+ nextOffset = copyBytes(output, nextOffset, input, anchor, literalLength);
149
+ output[nextOffset] = matchOffset & 0xff;
150
+ output[nextOffset + 1] = (matchOffset >>> 8) & 0xff;
151
+ nextOffset += 2;
152
+
153
+ if (matchLengthCode >= LZ4_LENGTH_MASK) {
154
+ token |= LZ4_LENGTH_MASK;
155
+ nextOffset = writeExtendedLength(output, nextOffset, matchLengthCode - LZ4_LENGTH_MASK);
156
+ } else {
157
+ token |= matchLengthCode;
158
+ }
159
+ output[tokenOffset] = token;
160
+ return nextOffset;
161
+ }
162
+
163
+ function emitLastLiterals(
164
+ output: Uint8Array,
165
+ outputOffset: number,
166
+ input: Uint8Array,
167
+ anchor: number,
168
+ literalLength: number
169
+ ): number {
170
+ const tokenOffset = outputOffset;
171
+ let nextOffset = outputOffset + 1;
172
+ if (literalLength >= LZ4_LENGTH_MASK) {
173
+ output[tokenOffset] = LZ4_LENGTH_MASK << 4;
174
+ nextOffset = writeExtendedLength(output, nextOffset, literalLength - LZ4_LENGTH_MASK);
175
+ } else {
176
+ output[tokenOffset] = literalLength << 4;
177
+ }
178
+ return copyBytes(output, nextOffset, input, anchor, literalLength);
179
+ }
180
+
181
+ function compressRawLz4Block(input: Uint8Array, hashTable: Uint32Array): Uint8Array {
182
+ const output = new Uint8Array(input.byteLength + Math.floor(input.byteLength / 255) + 16);
183
+ const inputView = new DataView(input.buffer, input.byteOffset, input.byteLength);
184
+ const matchFindLimit = input.byteLength - LZ4_MATCH_FIND_LIMIT;
185
+ const matchExtendLimit = input.byteLength - LZ4_LAST_LITERALS;
186
+ let anchor = 0;
187
+ let inputOffset = 0;
188
+ let outputOffset = 0;
189
+ let searchMatchCount = 1 << LZ4_SKIP_TRIGGER;
190
+
191
+ hashTable.fill(0);
192
+ while (inputOffset < matchFindLimit) {
193
+ const sequence = inputView.getUint32(inputOffset, true);
194
+ const hash = Math.imul(sequence, LZ4_HASH_MULTIPLIER) >>> (32 - LZ4_HASH_LOG);
195
+ const candidate = hashTable[hash] - 1;
196
+ hashTable[hash] = inputOffset + 1;
197
+
198
+ const hasMatch = !(
199
+ candidate < 0 ||
200
+ inputOffset - candidate > LZ4_MAX_OFFSET ||
201
+ inputView.getUint32(candidate, true) !== sequence
202
+ );
203
+ if (!hasMatch) {
204
+ inputOffset += searchMatchCount >> LZ4_SKIP_TRIGGER;
205
+ searchMatchCount += 1;
206
+ } else {
207
+ searchMatchCount = 1 << LZ4_SKIP_TRIGGER;
208
+ let matchEnd = inputOffset + LZ4_MIN_MATCH;
209
+ let reference = candidate + LZ4_MIN_MATCH;
210
+ while (matchEnd < matchExtendLimit && input[matchEnd] === input[reference]) {
211
+ matchEnd += 1;
212
+ reference += 1;
213
+ }
214
+ outputOffset = emitSequence(
215
+ output,
216
+ outputOffset,
217
+ input,
218
+ anchor,
219
+ inputOffset - anchor,
220
+ inputOffset - candidate,
221
+ matchEnd - inputOffset - LZ4_MIN_MATCH
222
+ );
223
+ inputOffset = matchEnd;
224
+ anchor = inputOffset;
225
+ }
226
+ }
227
+
228
+ outputOffset = emitLastLiterals(output, outputOffset, input, anchor, input.byteLength - anchor);
229
+ return output.slice(0, outputOffset);
230
+ }
231
+
232
+ // OneKey LZ4-blocked wrapper
233
+ // --------------------------
234
+ // The archive stores an 8-byte descriptor, one compressed-size value per
235
+ // block, then the concatenated raw blocks. Blocks are independent so firmware
236
+ // can validate and decompress them with bounded memory.
237
+ function encodeLz4Blocked(data: Uint8Array): Uint8Array {
238
+ const blockSize = 1 << LZ4_BLOCK_SIZE_LOG2;
239
+ const blockCount = Math.ceil(data.byteLength / blockSize);
240
+ const hashTable = new Uint32Array(1 << LZ4_HASH_LOG);
241
+ const blocks: Uint8Array[] = [];
242
+ const header = new Uint8Array(8 + blockCount * 4);
243
+ const headerView = new DataView(header.buffer);
244
+ headerView.setUint16(0, blockCount, true);
245
+ headerView.setUint16(2, LZ4_BLOCK_SIZE_LOG2, true);
246
+
247
+ for (let index = 0; index < blockCount; index += 1) {
248
+ const block = compressRawLz4Block(
249
+ data.subarray(index * blockSize, Math.min((index + 1) * blockSize, data.byteLength)),
250
+ hashTable
251
+ );
252
+ headerView.setUint32(8 + index * 4, block.byteLength, true);
253
+ blocks.push(block);
254
+ }
255
+ return concatBytes([header, ...blocks]);
256
+ }
257
+
258
+ // OKAR integrity helpers
259
+ // ----------------------
260
+ const CRC32_TABLE = (() => {
261
+ const table = new Uint32Array(256);
262
+ for (let index = 0; index < table.length; index += 1) {
263
+ let value = index;
264
+ for (let bit = 0; bit < 8; bit += 1) {
265
+ value = value & 1 ? 0xedb88320 ^ (value >>> 1) : value >>> 1;
266
+ }
267
+ table[index] = value >>> 0;
268
+ }
269
+ return table;
270
+ })();
271
+
272
+ function crc32(data: Uint8Array): number {
273
+ let value = 0xffffffff;
274
+ for (const byte of data) {
275
+ value = CRC32_TABLE[(value ^ byte) & 0xff] ^ (value >>> 8);
276
+ }
277
+ return (value ^ 0xffffffff) >>> 0;
278
+ }
279
+
280
+ function align(value: number): number {
281
+ return (value + ARCHIVE_ALIGNMENT - 1) & ~(ARCHIVE_ALIGNMENT - 1);
282
+ }
283
+
284
+ // OKAR archive writer
285
+ // -------------------
286
+ function buildArchive(entries: Pro2HostAssetPackageEntry[]): Uint8Array {
287
+ const textEncoder = new TextEncoder();
288
+ let dataOffset = ARCHIVE_HEADER_SIZE + entries.length * ARCHIVE_ENTRY_SIZE;
289
+ const encodedEntries: EncodedArchiveEntry[] = entries.map(entry => {
290
+ const nameBytes = textEncoder.encode(entry.name);
291
+ if (!entry.name || nameBytes.byteLength > ARCHIVE_ENTRY_NAME_MAX_LENGTH) {
292
+ throw new Error('Pro2 host asset package entry names must contain 1 to 255 UTF-8 bytes.');
293
+ }
294
+ if (!(entry.data instanceof Uint8Array) || entry.data.byteLength === 0) {
295
+ throw new Error(`Pro2 host asset package entry [${entry.name}] must not be empty.`);
296
+ }
297
+ const compressed = encodeLz4Blocked(entry.data);
298
+ const offset = align(dataOffset);
299
+ dataOffset = offset + compressed.byteLength;
300
+ return { ...entry, nameBytes, compressed, offset };
301
+ });
302
+
303
+ const archive = new Uint8Array(dataOffset);
304
+ const view = new DataView(archive.buffer);
305
+ view.setUint32(0, ARCHIVE_MAGIC, true);
306
+ view.setUint32(4, ARCHIVE_VERSION, true);
307
+ view.setUint16(8, encodedEntries.length, true);
308
+
309
+ encodedEntries.forEach((entry, index) => {
310
+ const recordOffset = ARCHIVE_HEADER_SIZE + index * ARCHIVE_ENTRY_SIZE;
311
+ archive[recordOffset] = entry.nameBytes.byteLength;
312
+ archive.set(entry.nameBytes, recordOffset + 1);
313
+ view.setUint32(recordOffset + 0x100, entry.offset, true);
314
+ view.setUint32(recordOffset + 0x104, entry.data.byteLength, true);
315
+ view.setUint32(recordOffset + 0x108, entry.compressed.byteLength, true);
316
+ view.setUint32(recordOffset + 0x10c, crc32(entry.data), true);
317
+ view.setUint32(recordOffset + 0x110, crc32(entry.compressed), true);
318
+ archive[recordOffset + 0x114] = ARCHIVE_COMPRESS_LZ4_BLOCKED;
319
+ archive.set(entry.compressed, entry.offset);
320
+ });
321
+ return archive;
322
+ }
323
+
324
+ // OKPP container writer
325
+ // ---------------------
326
+ export function buildPro2HostAssetPackage(entries: Pro2HostAssetPackageEntry[]): Uint8Array {
327
+ if (entries.length === 0 || new Set(entries.map(entry => entry.name)).size !== entries.length) {
328
+ throw new Error('Pro2 host asset package entries must have unique, non-empty names.');
329
+ }
330
+
331
+ const payload = buildArchive(entries);
332
+ const header = new Uint8Array(CONTAINER_HEADER_SIZE);
333
+ const view = new DataView(header.buffer);
334
+ view.setUint32(0, CONTAINER_HEADER_MAGIC, true);
335
+ view.setUint32(4, CONTAINER_HEADER_VERSION, true);
336
+ view.setUint32(8, CONTAINER_RESOURCE_TYPE_MAGIC, true);
337
+ view.setUint32(0x0c, CONTAINER_HEADER_SIZE, true);
338
+ view.setUint32(0x10, 1, true);
339
+ view.setUint32(0x14, payload.byteLength, true);
340
+
341
+ // Host asset packages are unsigned, but firmware still requires a valid
342
+ // payload hash, header hash, and the Ed25519 algorithm discriminator. The
343
+ // zero-initialized header intentionally leaves sig_used_count and every
344
+ // signature slot empty.
345
+ header.set(sha3_512(payload), CONTAINER_HASH_SECTION_OFFSET);
346
+ header.set(sha3_512(header.subarray(0, CONTAINER_HEADER_HASH_INPUT_LENGTH)), 0x240);
347
+ view.setUint32(CONTAINER_SIGNATURE_ALGORITHM_OFFSET, CONTAINER_ED25519_SIGNATURE_ALGORITHM, true);
348
+
349
+ const packageData = concatBytes([header, payload]);
350
+ if (packageData.byteLength > HOST_ASSET_PACKAGE_MAX_SIZE) {
351
+ throw new Error('Pro2 host asset package exceeds the firmware 4 MiB limit.');
352
+ }
353
+ return packageData;
354
+ }