@onekeyfe/hd-transport 1.2.0-alpha.4 → 1.2.0-alpha.41

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 (68) hide show
  1. package/__tests__/messages.test.js +176 -0
  2. package/__tests__/protocol-v2-ble-frame-writer.test.js +119 -0
  3. package/__tests__/protocol-v2-link-manager.test.js +437 -0
  4. package/__tests__/protocol-v2-usb-transport-base.test.js +330 -0
  5. package/__tests__/protocol-v2.test.js +465 -56
  6. package/__tests__/transport-log.test.js +79 -0
  7. package/dist/constants.d.ts +4 -1
  8. package/dist/constants.d.ts.map +1 -1
  9. package/dist/index.d.ts +784 -198
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +820 -227
  12. package/dist/protocols/index.d.ts +14 -5
  13. package/dist/protocols/index.d.ts.map +1 -1
  14. package/dist/protocols/v2/ble-frame-writer.d.ts +15 -0
  15. package/dist/protocols/v2/ble-frame-writer.d.ts.map +1 -0
  16. package/dist/protocols/v2/constants.d.ts +1 -0
  17. package/dist/protocols/v2/constants.d.ts.map +1 -1
  18. package/dist/protocols/v2/decode.d.ts +13 -2
  19. package/dist/protocols/v2/decode.d.ts.map +1 -1
  20. package/dist/protocols/v2/encode.d.ts +2 -3
  21. package/dist/protocols/v2/encode.d.ts.map +1 -1
  22. package/dist/protocols/v2/errors.d.ts +8 -0
  23. package/dist/protocols/v2/errors.d.ts.map +1 -0
  24. package/dist/protocols/v2/frame-assembler.d.ts.map +1 -1
  25. package/dist/protocols/v2/index.d.ts +2 -1
  26. package/dist/protocols/v2/index.d.ts.map +1 -1
  27. package/dist/protocols/v2/link-manager.d.ts +39 -0
  28. package/dist/protocols/v2/link-manager.d.ts.map +1 -0
  29. package/dist/protocols/v2/sequence-cursor.d.ts +5 -0
  30. package/dist/protocols/v2/sequence-cursor.d.ts.map +1 -0
  31. package/dist/protocols/v2/session.d.ts +19 -5
  32. package/dist/protocols/v2/session.d.ts.map +1 -1
  33. package/dist/protocols/v2/usb-transport-base.d.ts +32 -0
  34. package/dist/protocols/v2/usb-transport-base.d.ts.map +1 -0
  35. package/dist/serialization/protobuf/decode.d.ts.map +1 -1
  36. package/dist/types/messages.d.ts +376 -110
  37. package/dist/types/messages.d.ts.map +1 -1
  38. package/dist/types/transport.d.ts +14 -3
  39. package/dist/types/transport.d.ts.map +1 -1
  40. package/dist/utils/transportLog.d.ts +9 -0
  41. package/dist/utils/transportLog.d.ts.map +1 -0
  42. package/messages-protocol-v2.json +1155 -481
  43. package/package.json +3 -3
  44. package/scripts/protobuf-build.sh +86 -208
  45. package/scripts/protobuf-patches/index.js +1 -0
  46. package/scripts/protobuf-types.js +19 -1
  47. package/src/constants.ts +28 -14
  48. package/src/index.ts +12 -1
  49. package/src/protocols/index.ts +39 -40
  50. package/src/protocols/v2/ble-frame-writer.ts +77 -0
  51. package/src/protocols/v2/constants.ts +1 -0
  52. package/src/protocols/v2/crc8.ts +1 -1
  53. package/src/protocols/v2/decode.ts +71 -37
  54. package/src/protocols/v2/encode.ts +2 -28
  55. package/src/protocols/v2/errors.ts +25 -0
  56. package/src/protocols/v2/frame-assembler.ts +6 -4
  57. package/src/protocols/v2/index.ts +2 -1
  58. package/src/protocols/v2/link-manager.ts +209 -0
  59. package/src/protocols/v2/sequence-cursor.ts +10 -0
  60. package/src/protocols/v2/session.ts +157 -200
  61. package/src/protocols/v2/usb-transport-base.ts +213 -0
  62. package/src/serialization/protobuf/decode.ts +4 -1
  63. package/src/types/messages.ts +458 -142
  64. package/src/types/transport.ts +14 -3
  65. package/src/utils/transportLog.ts +102 -0
  66. package/dist/protocols/v2/debug.d.ts +0 -13
  67. package/dist/protocols/v2/debug.d.ts.map +0 -1
  68. package/src/protocols/v2/debug.ts +0 -26
@@ -3,6 +3,11 @@ const {
3
3
  createMessageFromType,
4
4
  parseConfigure,
5
5
  } = require('../src/serialization/protobuf/messages');
6
+ const v1Messages = require('../../core/src/data/messages/messages.json');
7
+ const v2Messages = require('../messages-protocol-v2.json');
8
+ const coreV2Messages = require('../../core/src/data/messages/messages-protocol-v2.json');
9
+ const generatedTypes = require('../src/types/messages');
10
+
6
11
  // const json = require('./data/messages.json');
7
12
 
8
13
  const json = {
@@ -58,6 +63,177 @@ const json = {
58
63
  };
59
64
 
60
65
  describe('messages', () => {
66
+ test('V1 GetPassphraseState matches the current firmware schema', () => {
67
+ const { fields } = v1Messages.nested.GetPassphraseState;
68
+
69
+ expect(fields.passphrase_state).toMatchObject({ id: 1, type: 'string' });
70
+ expect(fields).not.toHaveProperty('_only_main_pin');
71
+ expect(fields).not.toHaveProperty('allow_create_attach_pin');
72
+ });
73
+
74
+ test('Protocol V2 firmware targets match the current firmware-pro2 enum', () => {
75
+ expect(v2Messages.nested.DeviceFirmwareTargetType.values).toEqual({
76
+ FW_MGMT_TARGET_INVALID: 0,
77
+ FW_MGMT_TARGET_CRATE: 1,
78
+ FW_MGMT_TARGET_ROMLOADER: 2,
79
+ FW_MGMT_TARGET_BOOTLOADER: 3,
80
+ FW_MGMT_TARGET_APPLICATION_P1: 4,
81
+ FW_MGMT_TARGET_APPLICATION_P2: 5,
82
+ FW_MGMT_TARGET_COPROCESSOR: 6,
83
+ FW_MGMT_TARGET_SE01: 7,
84
+ FW_MGMT_TARGET_SE02: 8,
85
+ FW_MGMT_TARGET_SE03: 9,
86
+ FW_MGMT_TARGET_SE04: 10,
87
+ });
88
+ });
89
+
90
+ test('Protocol V2 conflicting enums keep their own wire values', () => {
91
+ expect(generatedTypes.ProtocolV2FailureType).toMatchObject({
92
+ Failure_DataError: 4,
93
+ Failure_ProcessError: 5,
94
+ });
95
+ expect(generatedTypes.Enum_ProtocolV2Capability).toMatchObject({
96
+ Capability_AttachToPin: 18,
97
+ });
98
+ });
99
+
100
+ test('Protocol V2 device status and session messages match firmware-pro2', () => {
101
+ expect(v2Messages.nested.MessageType.values).toMatchObject({
102
+ MessageType_DeviceStatusGet: 60602,
103
+ MessageType_DeviceStatus: 60603,
104
+ MessageType_DeviceSessionGet: 61200,
105
+ MessageType_DeviceSession: 61201,
106
+ MessageType_DeviceSessionAskPin: 61202,
107
+ MessageType_DeviceSessionAskPassphrase: 61203,
108
+ });
109
+ expect(v2Messages.nested.DeviceStatusGet).toEqual({ fields: {} });
110
+ expect(v2Messages.nested.ProtocolInfoRequest.fields.eventless_wallet_session).toMatchObject({
111
+ id: 1,
112
+ type: 'bool',
113
+ options: { default: false },
114
+ });
115
+ expect(v2Messages.nested.ProtocolInfo.fields).toMatchObject({
116
+ version: { id: 1, type: 'uint32', rule: 'required' },
117
+ build_fingerprint: { id: 2, type: 'string', rule: 'required' },
118
+ supported_messages: { id: 3, type: 'uint32', rule: 'repeated' },
119
+ });
120
+ expect(v2Messages.nested.DeviceSessionGet.fields.session_id).toMatchObject({
121
+ id: 1,
122
+ type: 'bytes',
123
+ });
124
+ expect(v2Messages.nested.DeviceSessionPinType.values).toEqual({
125
+ Any: 1,
126
+ Main: 2,
127
+ AttachToPin: 3,
128
+ });
129
+ expect(v2Messages.nested.DeviceSessionErrorCode.values).toEqual({
130
+ DeviceSessionError_None: 0,
131
+ DeviceSessionError_UserCancelled: 1,
132
+ DeviceSessionError_InvalidSession: 2,
133
+ DeviceSessionError_AttachPinUnavailable: 3,
134
+ DeviceSessionError_PassphraseDisabled: 4,
135
+ DeviceSessionError_Busy: 5,
136
+ });
137
+ expect(v2Messages.nested).not.toHaveProperty('DeviceWalletSelect');
138
+ expect(v2Messages.nested).not.toHaveProperty('DeviceWalletType');
139
+ expect(v2Messages.nested).not.toHaveProperty('DeviceHiddenWalletSelect');
140
+ expect(v2Messages.nested.DeviceSession.fields).toMatchObject({
141
+ session_id: { id: 1, type: 'bytes' },
142
+ btc_test_address: { id: 2, type: 'string' },
143
+ });
144
+ expect(v2Messages.nested.DeviceSessionAskPin.fields.type).toMatchObject({
145
+ id: 1,
146
+ type: 'DeviceSessionPinType',
147
+ });
148
+ expect(v2Messages.nested.DeviceSessionAskPassphrase).toEqual({
149
+ fields: {
150
+ passphrase: {
151
+ type: 'string',
152
+ id: 1,
153
+ },
154
+ on_device: {
155
+ rule: 'required',
156
+ type: 'bool',
157
+ id: 2,
158
+ },
159
+ },
160
+ });
161
+ expect(v2Messages.nested.DeviceSessionAskPin_FailureSubCodes.values).toEqual({
162
+ UserCancel: 1,
163
+ });
164
+ expect(v2Messages.nested.MessageType.values).not.toHaveProperty(
165
+ 'MessageType_DeviceSessionPinResult'
166
+ );
167
+ expect(v2Messages.nested.MessageType.values).not.toHaveProperty(
168
+ 'MessageType_DeviceSessionOpen'
169
+ );
170
+ });
171
+
172
+ test('Protocol V2 passphrase selection explicitly selects host or device input on wire', () => {
173
+ const messages = parseConfigure(v2Messages);
174
+ const { Message } = createMessageFromName(messages, 'DeviceSessionAskPassphrase');
175
+
176
+ const onHost = Message.encode(
177
+ Message.create({ passphrase: 'host hidden wallet', on_device: false })
178
+ ).finish();
179
+ const onDevice = Message.encode(Message.create({ on_device: true })).finish();
180
+
181
+ expect(Buffer.from(onHost).toString('hex')).toBe(
182
+ '0a12686f73742068696464656e2077616c6c65741000'
183
+ );
184
+ expect(Buffer.from(onDevice).toString('hex')).toBe('1001');
185
+ expect(Message.decode(onHost)).toMatchObject({
186
+ passphrase: 'host hidden wallet',
187
+ on_device: false,
188
+ });
189
+ expect(Message.decode(onDevice)).toMatchObject({ on_device: true });
190
+ });
191
+
192
+ test('Protocol V2 onboarding status matches the current firmware-pro2 schema', () => {
193
+ expect(v2Messages.nested.MessageType.values).toMatchObject({
194
+ MessageType_OnboardingStatusGet: 61600,
195
+ MessageType_OnboardingStatus: 61601,
196
+ });
197
+ expect(v2Messages.nested.OnboardingStep.values).toMatchObject({
198
+ ONBOARDING_STEP_UNKNOWN: 0,
199
+ ONBOARDING_STEP_CHECKING: 1,
200
+ ONBOARDING_STEP_PERSONALIZATION: 2,
201
+ ONBOARDING_STEP_PIN: 3,
202
+ ONBOARDING_STEP_SETUP: 4,
203
+ ONBOARDING_STEP_DONE: 5,
204
+ });
205
+ expect(v2Messages.nested.OnboardingPhase.values).toBeDefined();
206
+ expect(v2Messages.nested.OnboardingSetupKind.values).toBeDefined();
207
+ expect(v2Messages.nested.OnboardingSetupMethod.values).toBeDefined();
208
+ expect(v2Messages.nested.OnboardingSetupStatus.fields).toMatchObject({
209
+ kind: { id: 1, type: 'OnboardingSetupKind' },
210
+ method: { id: 2, type: 'OnboardingSetupMethod' },
211
+ });
212
+ expect(v2Messages.nested.OnboardingStatus.fields).toMatchObject({
213
+ step: { id: 1, type: 'OnboardingStep' },
214
+ phase: { id: 2, type: 'OnboardingPhase' },
215
+ setup: { id: 3, type: 'OnboardingSetupStatus' },
216
+ pin_set: { id: 4, type: 'bool' },
217
+ wallet_initialized: { id: 5, type: 'bool' },
218
+ });
219
+ expect(v2Messages.nested).not.toHaveProperty('DevOnboardingStatus');
220
+ });
221
+
222
+ test('Protocol V2 does not restore retired unlock or passphrase ids', () => {
223
+ expect(v2Messages.nested.MessageType.values).not.toHaveProperty('MessageType_UnLockDevice');
224
+ expect(v2Messages.nested.MessageType.values).not.toHaveProperty(
225
+ 'MessageType_UnLockDeviceResponse'
226
+ );
227
+ expect(v2Messages.nested.MessageType.values).not.toHaveProperty(
228
+ 'MessageType_GetPassphraseState'
229
+ );
230
+ expect(v2Messages.nested.MessageType.values).not.toHaveProperty('MessageType_PassphraseState');
231
+ });
232
+
233
+ test('Protocol V2 transport and core schemas stay identical', () => {
234
+ expect(coreV2Messages).toEqual(v2Messages);
235
+ });
236
+
61
237
  test('createMessageFromName (common case)', () => {
62
238
  const messages = parseConfigure(json);
63
239
  const name = 'Initialize';
@@ -0,0 +1,119 @@
1
+ /* eslint-disable @typescript-eslint/no-var-requires */
2
+ const { writeProtocolV2BleFrame } = require('../src');
3
+
4
+ describe('Protocol V2 BLE frame writer', () => {
5
+ test('splits one complete Protocol V2 frame without changing packet order', async () => {
6
+ const packets = [];
7
+
8
+ await writeProtocolV2BleFrame({
9
+ frame: new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
10
+ packetCapacity: 4,
11
+ writePacket: packet => {
12
+ packets.push(Array.from(packet));
13
+ return Promise.resolve();
14
+ },
15
+ });
16
+
17
+ expect(packets).toEqual([
18
+ [0, 1, 2, 3],
19
+ [4, 5, 6, 7],
20
+ [8, 9],
21
+ ]);
22
+ });
23
+
24
+ test('stops before the next packet when the connection generation changes', async () => {
25
+ const packets = [];
26
+ let active = true;
27
+
28
+ await expect(
29
+ writeProtocolV2BleFrame({
30
+ frame: new Uint8Array(9),
31
+ packetCapacity: 4,
32
+ assertActive: () => {
33
+ if (!active) throw new Error('stale BLE generation');
34
+ },
35
+ writePacket: packet => {
36
+ packets.push(packet);
37
+ active = false;
38
+ return Promise.resolve();
39
+ },
40
+ })
41
+ ).rejects.toThrow('stale BLE generation');
42
+
43
+ expect(packets).toHaveLength(1);
44
+ });
45
+
46
+ test('does not write when the call is already aborted', async () => {
47
+ const controller = new AbortController();
48
+ const writePacket = jest.fn();
49
+ controller.abort();
50
+
51
+ await expect(
52
+ writeProtocolV2BleFrame({
53
+ frame: new Uint8Array(4),
54
+ packetCapacity: 4,
55
+ signal: controller.signal,
56
+ abortMessage: 'Protocol V2 BLE write aborted for Ping',
57
+ writePacket,
58
+ })
59
+ ).rejects.toThrow('Protocol V2 BLE write aborted for Ping');
60
+
61
+ expect(writePacket).not.toHaveBeenCalled();
62
+ });
63
+
64
+ test('surfaces an abort that occurs while the final packet write is completing', async () => {
65
+ const controller = new AbortController();
66
+ const writePacket = jest.fn().mockImplementation(() => {
67
+ controller.abort();
68
+ return Promise.resolve();
69
+ });
70
+
71
+ await expect(
72
+ writeProtocolV2BleFrame({
73
+ frame: new Uint8Array(4),
74
+ packetCapacity: 4,
75
+ signal: controller.signal,
76
+ abortMessage: 'Protocol V2 BLE write aborted for Ping',
77
+ writePacket,
78
+ })
79
+ ).rejects.toThrow('Protocol V2 BLE write aborted for Ping');
80
+
81
+ expect(writePacket).toHaveBeenCalledTimes(1);
82
+ });
83
+
84
+ test('applies bounded burst pauses and one final flush delay', async () => {
85
+ const waits = [];
86
+ const writePacket = jest.fn().mockResolvedValue(undefined);
87
+
88
+ await writeProtocolV2BleFrame({
89
+ frame: new Uint8Array(10),
90
+ packetCapacity: 3,
91
+ initialDelayMs: 3,
92
+ burstSize: 2,
93
+ burstPauseMs: 7,
94
+ flushDelayMs: 11,
95
+ wait: ms => {
96
+ waits.push(ms);
97
+ return Promise.resolve();
98
+ },
99
+ writePacket,
100
+ });
101
+
102
+ expect(writePacket).toHaveBeenCalledTimes(4);
103
+ expect(waits).toEqual([3, 7, 11]);
104
+ });
105
+
106
+ test('rejects an invalid packet capacity before writing', async () => {
107
+ const writePacket = jest.fn();
108
+
109
+ await expect(
110
+ writeProtocolV2BleFrame({
111
+ frame: new Uint8Array(4),
112
+ packetCapacity: 0,
113
+ writePacket,
114
+ })
115
+ ).rejects.toThrow('Protocol V2 BLE packet capacity must be a positive integer');
116
+
117
+ expect(writePacket).not.toHaveBeenCalled();
118
+ });
119
+ });