@onekeyfe/hd-transport 1.2.0-alpha.6 → 1.2.0-alpha.63
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/__tests__/messages.test.js +176 -0
- package/__tests__/protocol-v2-ble-frame-writer.test.js +119 -0
- package/__tests__/protocol-v2-link-manager.test.js +437 -0
- package/__tests__/protocol-v2-usb-transport-base.test.js +330 -0
- package/__tests__/protocol-v2.test.js +462 -53
- package/__tests__/transport-log.test.js +79 -0
- package/dist/constants.d.ts +4 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/index.d.ts +784 -198
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +820 -227
- package/dist/protocols/index.d.ts +14 -5
- package/dist/protocols/index.d.ts.map +1 -1
- package/dist/protocols/v2/ble-frame-writer.d.ts +15 -0
- package/dist/protocols/v2/ble-frame-writer.d.ts.map +1 -0
- package/dist/protocols/v2/constants.d.ts +1 -0
- package/dist/protocols/v2/constants.d.ts.map +1 -1
- package/dist/protocols/v2/decode.d.ts +13 -2
- package/dist/protocols/v2/decode.d.ts.map +1 -1
- package/dist/protocols/v2/encode.d.ts +2 -3
- package/dist/protocols/v2/encode.d.ts.map +1 -1
- package/dist/protocols/v2/errors.d.ts +8 -0
- package/dist/protocols/v2/errors.d.ts.map +1 -0
- package/dist/protocols/v2/frame-assembler.d.ts.map +1 -1
- package/dist/protocols/v2/index.d.ts +2 -1
- package/dist/protocols/v2/index.d.ts.map +1 -1
- package/dist/protocols/v2/link-manager.d.ts +39 -0
- package/dist/protocols/v2/link-manager.d.ts.map +1 -0
- package/dist/protocols/v2/sequence-cursor.d.ts +5 -0
- package/dist/protocols/v2/sequence-cursor.d.ts.map +1 -0
- package/dist/protocols/v2/session.d.ts +19 -5
- package/dist/protocols/v2/session.d.ts.map +1 -1
- package/dist/protocols/v2/usb-transport-base.d.ts +32 -0
- package/dist/protocols/v2/usb-transport-base.d.ts.map +1 -0
- package/dist/serialization/protobuf/decode.d.ts.map +1 -1
- package/dist/types/messages.d.ts +376 -110
- package/dist/types/messages.d.ts.map +1 -1
- package/dist/types/transport.d.ts +14 -3
- package/dist/types/transport.d.ts.map +1 -1
- package/dist/utils/transportLog.d.ts +9 -0
- package/dist/utils/transportLog.d.ts.map +1 -0
- package/messages-protocol-v2.json +1155 -483
- package/package.json +3 -3
- package/scripts/protobuf-build.sh +86 -208
- package/scripts/protobuf-patches/index.js +1 -0
- package/scripts/protobuf-types.js +19 -1
- package/src/constants.ts +28 -14
- package/src/index.ts +12 -1
- package/src/protocols/index.ts +39 -40
- package/src/protocols/v2/ble-frame-writer.ts +77 -0
- package/src/protocols/v2/constants.ts +1 -0
- package/src/protocols/v2/crc8.ts +1 -1
- package/src/protocols/v2/decode.ts +71 -37
- package/src/protocols/v2/encode.ts +2 -28
- package/src/protocols/v2/errors.ts +25 -0
- package/src/protocols/v2/frame-assembler.ts +6 -4
- package/src/protocols/v2/index.ts +2 -1
- package/src/protocols/v2/link-manager.ts +209 -0
- package/src/protocols/v2/sequence-cursor.ts +10 -0
- package/src/protocols/v2/session.ts +157 -200
- package/src/protocols/v2/usb-transport-base.ts +213 -0
- package/src/serialization/protobuf/decode.ts +4 -1
- package/src/types/messages.ts +458 -142
- package/src/types/transport.ts +14 -3
- package/src/utils/transportLog.ts +102 -0
- package/dist/protocols/v2/debug.d.ts +0 -13
- package/dist/protocols/v2/debug.d.ts.map +0 -1
- package/src/protocols/v2/debug.ts +0 -26
package/src/types/transport.ts
CHANGED
|
@@ -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,6 +53,7 @@ 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> };
|
|
@@ -92,7 +103,7 @@ export type Transport = {
|
|
|
92
103
|
|
|
93
104
|
// resolves when the transport can be used; rejects when it cannot
|
|
94
105
|
init: ITransportInitFn;
|
|
95
|
-
stop(): void
|
|
106
|
+
stop(): void | Promise<void>;
|
|
96
107
|
|
|
97
108
|
configured: boolean;
|
|
98
109
|
version: string;
|
|
@@ -105,8 +116,8 @@ export type Transport = {
|
|
|
105
116
|
export type LowLevelDevice = OneKeyDeviceInfoBase & { id: string; name: string };
|
|
106
117
|
export type LowlevelTransportSharedPlugin = {
|
|
107
118
|
enumerate: () => Promise<LowLevelDevice[]>;
|
|
108
|
-
send: (uuid: string, data: string) => Promise<void>;
|
|
109
|
-
receive: () => Promise<string>;
|
|
119
|
+
send: (uuid: string, data: string, options?: { withoutResponse?: boolean }) => Promise<void>;
|
|
120
|
+
receive: (uuid?: string) => Promise<string>;
|
|
110
121
|
connect: (uuid: string) => Promise<void>;
|
|
111
122
|
disconnect: (uuid: string) => Promise<void>;
|
|
112
123
|
|
|
@@ -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
|
-
}
|