@onekeyfe/hd-transport 1.2.0-alpha.7 → 1.2.0-alpha.71

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 (70) hide show
  1. package/__tests__/messages.test.js +220 -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 +492 -53
  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 +810 -198
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +836 -228
  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 +23 -6
  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/serialization/protobuf/encode.d.ts.map +1 -1
  37. package/dist/types/messages.d.ts +386 -110
  38. package/dist/types/messages.d.ts.map +1 -1
  39. package/dist/types/transport.d.ts +20 -3
  40. package/dist/types/transport.d.ts.map +1 -1
  41. package/dist/utils/transportLog.d.ts +9 -0
  42. package/dist/utils/transportLog.d.ts.map +1 -0
  43. package/messages-protocol-v2.json +1138 -441
  44. package/package.json +3 -3
  45. package/scripts/protobuf-build.sh +86 -208
  46. package/scripts/protobuf-patches/index.js +1 -0
  47. package/scripts/protobuf-types.js +19 -1
  48. package/src/constants.ts +28 -14
  49. package/src/index.ts +13 -1
  50. package/src/protocols/index.ts +39 -40
  51. package/src/protocols/v2/ble-frame-writer.ts +77 -0
  52. package/src/protocols/v2/constants.ts +1 -0
  53. package/src/protocols/v2/crc8.ts +1 -1
  54. package/src/protocols/v2/decode.ts +71 -37
  55. package/src/protocols/v2/encode.ts +2 -28
  56. package/src/protocols/v2/errors.ts +25 -0
  57. package/src/protocols/v2/frame-assembler.ts +6 -4
  58. package/src/protocols/v2/index.ts +2 -1
  59. package/src/protocols/v2/link-manager.ts +209 -0
  60. package/src/protocols/v2/sequence-cursor.ts +10 -0
  61. package/src/protocols/v2/session.ts +173 -201
  62. package/src/protocols/v2/usb-transport-base.ts +213 -0
  63. package/src/serialization/protobuf/decode.ts +4 -1
  64. package/src/serialization/protobuf/encode.ts +5 -1
  65. package/src/types/messages.ts +471 -142
  66. package/src/types/transport.ts +23 -3
  67. package/src/utils/transportLog.ts +102 -0
  68. package/dist/protocols/v2/debug.d.ts +0 -13
  69. package/dist/protocols/v2/debug.d.ts.map +0 -1
  70. package/src/protocols/v2/debug.ts +0 -26
@@ -1,5 +1,15 @@
1
1
  import type EventEmitter from 'events';
2
2
 
3
+ export const TRANSPORT_EVENT = {
4
+ DEVICE_DISCONNECT: 'transport-device-disconnect',
5
+ } as const;
6
+
7
+ export type TransportDeviceDisconnectEvent = {
8
+ id: string;
9
+ connectId: string;
10
+ name: string | null;
11
+ };
12
+
3
13
  export type ProtocolType = 'V1' | 'V2';
4
14
 
5
15
  export type OneKeyDeviceCommType =
@@ -43,15 +53,25 @@ export type AcquireInput = {
43
53
  uuid?: string;
44
54
  forceCleanRunPromise?: boolean;
45
55
  expectedProtocol?: ProtocolType;
56
+ protocolHint?: ProtocolType;
46
57
  };
47
58
 
48
59
  export type MessageFromOneKey = { type: string; message: Record<string, any> };
49
60
 
61
+ export type TransportWriteMetrics = {
62
+ elapsedMs: number;
63
+ frameBytes: number;
64
+ };
65
+
50
66
  export type TransportCallOptions = {
51
67
  timeoutMs?: number;
52
68
  expectedTypes?: string[];
53
69
  intermediateTypes?: string[];
54
70
  onIntermediateResponse?: (response: MessageFromOneKey) => void;
71
+ /** Called after the complete request frame has been submitted to the transport. */
72
+ onWriteCompleted?: (metrics: TransportWriteMetrics) => void;
73
+ /** Prefer acknowledged BLE characteristic writes for this call when supported. */
74
+ writeWithResponse?: boolean;
55
75
  };
56
76
 
57
77
  type ITransportInitFn = (
@@ -92,7 +112,7 @@ export type Transport = {
92
112
 
93
113
  // resolves when the transport can be used; rejects when it cannot
94
114
  init: ITransportInitFn;
95
- stop(): void;
115
+ stop(): void | Promise<void>;
96
116
 
97
117
  configured: boolean;
98
118
  version: string;
@@ -105,8 +125,8 @@ export type Transport = {
105
125
  export type LowLevelDevice = OneKeyDeviceInfoBase & { id: string; name: string };
106
126
  export type LowlevelTransportSharedPlugin = {
107
127
  enumerate: () => Promise<LowLevelDevice[]>;
108
- send: (uuid: string, data: string) => Promise<void>;
109
- receive: () => Promise<string>;
128
+ send: (uuid: string, data: string, options?: { withoutResponse?: boolean }) => Promise<void>;
129
+ receive: (uuid?: string) => Promise<string>;
110
130
  connect: (uuid: string) => Promise<void>;
111
131
  disconnect: (uuid: string) => Promise<void>;
112
132
 
@@ -0,0 +1,102 @@
1
+ import type { ProtocolType } from '../types';
2
+
3
+ const REDACTED = '[REDACTED]';
4
+
5
+ const SENSITIVE_LOG_KEYS = new Set([
6
+ 'accesstoken',
7
+ 'address',
8
+ 'apikey',
9
+ 'connectid',
10
+ 'devicestate',
11
+ 'deviceid',
12
+ 'entropy',
13
+ 'expectedpassphrasestate',
14
+ 'mnemonic',
15
+ 'passphrase',
16
+ 'passphrasestate',
17
+ 'password',
18
+ 'pin',
19
+ 'privatekey',
20
+ 'publickey',
21
+ 'seed',
22
+ 'serialnumber',
23
+ 'session',
24
+ 'sessionid',
25
+ 'signature',
26
+ 'token',
27
+ 'uuid',
28
+ 'walletsessionid',
29
+ 'xprv',
30
+ 'xpub',
31
+ ]);
32
+
33
+ const normalizeLogKey = (key: string) => key.replace(/[_-]/g, '').toLowerCase();
34
+
35
+ const isSensitiveLogKey = (key: string) => {
36
+ const normalizedKey = normalizeLogKey(key);
37
+ return (
38
+ SENSITIVE_LOG_KEYS.has(normalizedKey) ||
39
+ normalizedKey.endsWith('address') ||
40
+ normalizedKey.endsWith('privatekey') ||
41
+ normalizedKey.endsWith('publickey') ||
42
+ normalizedKey.endsWith('signature')
43
+ );
44
+ };
45
+
46
+ const isSigningMessage = (messageName?: string) =>
47
+ Boolean(messageName && /(?:sign|txack|transaction)/i.test(messageName));
48
+
49
+ const redactLogValue = (value: unknown, seen: WeakSet<object>): unknown => {
50
+ if (ArrayBuffer.isView(value)) {
51
+ return `[BINARY:${value.byteLength}]`;
52
+ }
53
+ if (value instanceof ArrayBuffer) {
54
+ return `[BINARY:${value.byteLength}]`;
55
+ }
56
+ if (Array.isArray(value)) {
57
+ return value.map(item => redactLogValue(item, seen));
58
+ }
59
+ if (!value || typeof value !== 'object') {
60
+ return value;
61
+ }
62
+ if (seen.has(value)) {
63
+ return '[CIRCULAR]';
64
+ }
65
+
66
+ seen.add(value);
67
+ const redacted = Object.fromEntries(
68
+ Object.entries(value as Record<string, unknown>).map(([key, item]) => [
69
+ key,
70
+ isSensitiveLogKey(key) && item !== null && item !== undefined
71
+ ? REDACTED
72
+ : redactLogValue(item, seen),
73
+ ])
74
+ );
75
+ seen.delete(value);
76
+ return redacted;
77
+ };
78
+
79
+ export function getSafeTransportLogPayload(value: unknown, messageName?: string): unknown {
80
+ if (isSigningMessage(messageName)) {
81
+ return REDACTED;
82
+ }
83
+ return redactLogValue(value, new WeakSet());
84
+ }
85
+
86
+ export function createTransportCallLog(
87
+ name: string,
88
+ protocol: ProtocolType,
89
+ data: Record<string, unknown>
90
+ ) {
91
+ return {
92
+ name,
93
+ protocol,
94
+ request: getSafeTransportLogPayload(data, name),
95
+ };
96
+ }
97
+
98
+ const HIGH_VOLUME_CALLS = new Set(['FileWrite', 'FilesystemFileWrite', 'EmmcFileWrite']);
99
+
100
+ export function shouldSuppressHighVolumeCallLog(name: string) {
101
+ return HIGH_VOLUME_CALLS.has(name);
102
+ }
@@ -1,13 +0,0 @@
1
- export type ProtocolV2DebugLogger = {
2
- debug?: (...args: unknown[]) => void;
3
- };
4
- export type ProtocolV2FrameDebugOptions = {
5
- logger?: ProtocolV2DebugLogger;
6
- logPrefix?: string;
7
- context?: string;
8
- messageName?: string;
9
- messageTypeId?: number;
10
- pbPayloadLength?: number;
11
- };
12
- export declare function logProtocolV2Debug(options: ProtocolV2FrameDebugOptions | undefined, stage: string, details: Record<string, unknown>): void;
13
- //# sourceMappingURL=debug.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"debug.d.ts","sourceRoot":"","sources":["../../../src/protocols/v2/debug.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,qBAAqB,GAAG;IAClC,KAAK,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,CAAC;CACtC,CAAC;AAEF,MAAM,MAAM,2BAA2B,GAAG;IACxC,MAAM,CAAC,EAAE,qBAAqB,CAAC;IAC/B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,2BAA2B,GAAG,SAAS,EAChD,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,QASjC"}
@@ -1,26 +0,0 @@
1
- export type ProtocolV2DebugLogger = {
2
- debug?: (...args: unknown[]) => void;
3
- };
4
-
5
- export type ProtocolV2FrameDebugOptions = {
6
- logger?: ProtocolV2DebugLogger;
7
- logPrefix?: string;
8
- context?: string;
9
- messageName?: string;
10
- messageTypeId?: number;
11
- pbPayloadLength?: number;
12
- };
13
-
14
- export function logProtocolV2Debug(
15
- options: ProtocolV2FrameDebugOptions | undefined,
16
- stage: string,
17
- details: Record<string, unknown>
18
- ) {
19
- options?.logger?.debug?.(`[${options.logPrefix ?? 'ProtocolV2'}] ${stage}`, {
20
- ...(options.context ? { context: options.context } : {}),
21
- ...(options.messageName ? { messageName: options.messageName } : {}),
22
- ...(options.messageTypeId !== undefined ? { messageTypeId: options.messageTypeId } : {}),
23
- ...(options.pbPayloadLength !== undefined ? { pbPayloadLength: options.pbPayloadLength } : {}),
24
- ...details,
25
- });
26
- }