@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.
- 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 +465 -56
- 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 -481
- 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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport",
|
|
3
|
-
"version": "1.2.0-alpha.
|
|
3
|
+
"version": "1.2.0-alpha.41",
|
|
4
4
|
"description": "Transport layer abstractions and utilities for OneKey hardware SDK.",
|
|
5
5
|
"author": "OneKey",
|
|
6
6
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"test": "jest",
|
|
17
17
|
"lint": "eslint .",
|
|
18
18
|
"lint:fix": "eslint . --fix",
|
|
19
|
-
"update
|
|
19
|
+
"update:protobuf": "./scripts/protobuf-build.sh"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@types/bytebuffer": "^5.0.42",
|
|
@@ -28,5 +28,5 @@
|
|
|
28
28
|
"long": "^4.0.0",
|
|
29
29
|
"protobufjs": "^6.11.2"
|
|
30
30
|
},
|
|
31
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "17fd02ad56766b4b576ea48503f92083f2101c29"
|
|
32
32
|
}
|
|
@@ -2,35 +2,29 @@
|
|
|
2
2
|
|
|
3
3
|
set -euxo pipefail
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
PARENT_PATH=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
|
|
5
|
+
PARENT_PATH=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)
|
|
8
6
|
PACKAGE_ROOT="$PARENT_PATH/.."
|
|
7
|
+
REPO_ROOT="$PARENT_PATH/../../.."
|
|
8
|
+
CORE_MESSAGES_DIR="$REPO_ROOT/packages/core/src/data/messages"
|
|
9
9
|
|
|
10
10
|
SRC="../../submodules/firmware/common/protob"
|
|
11
11
|
DIST="."
|
|
12
12
|
LANG="typescript"
|
|
13
|
-
# Absolute paths — resolved relative to this script's directory
|
|
14
|
-
REPO_ROOT="$PARENT_PATH/../../.."
|
|
15
|
-
CORE_MESSAGES_DIR="$REPO_ROOT/packages/core/src/data/messages"
|
|
16
13
|
|
|
17
|
-
if [[ $# -ne 0 && $# -ne 3 ]]
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
exit 1
|
|
14
|
+
if [[ $# -ne 0 && $# -ne 3 ]]; then
|
|
15
|
+
echo "must provide either 3 or 0 arguments. $# provided"
|
|
16
|
+
exit 1
|
|
21
17
|
fi
|
|
22
18
|
|
|
23
|
-
if [[ $# -eq 3 ]]
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
LANG=$3
|
|
19
|
+
if [[ $# -eq 3 ]]; then
|
|
20
|
+
SRC=$1
|
|
21
|
+
DIST=$2
|
|
22
|
+
LANG=$3
|
|
28
23
|
fi
|
|
29
24
|
|
|
30
|
-
if [[ "$LANG" != "typescript" && "$LANG" != "flow" ]];
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
exit 1
|
|
25
|
+
if [[ "$LANG" != "typescript" && "$LANG" != "flow" ]]; then
|
|
26
|
+
echo "either typescript or flow must be specified as the third argument"
|
|
27
|
+
exit 1
|
|
34
28
|
fi
|
|
35
29
|
|
|
36
30
|
if [[ "$SRC" = /* ]]; then
|
|
@@ -45,216 +39,100 @@ else
|
|
|
45
39
|
DIST_PATH="$PACKAGE_ROOT/$DIST"
|
|
46
40
|
fi
|
|
47
41
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
| sed 's/ management\./ /' \
|
|
68
|
-
| sed 's/^option /\/\/ option /' \
|
|
69
|
-
| grep -v ' reserved '>> "$TMP_PROTO"
|
|
70
|
-
|
|
71
|
-
npx pbjs -t json -p "$DIST_PATH" -o "$DIST_PATH/messages.json" --keep-case "$TMP_PROTO"
|
|
72
|
-
rm "$TMP_PROTO"
|
|
73
|
-
|
|
74
|
-
# Copy to core package
|
|
75
|
-
cp "$DIST_PATH/messages.json" "$CORE_MESSAGES_DIR/messages.json"
|
|
76
|
-
echo "Pro1 messages.json copied to core"
|
|
77
|
-
|
|
78
|
-
yarn --cwd "$PACKAGE_ROOT" prettier --write "$DIST_PATH/messages.json"
|
|
79
|
-
yarn --cwd "$PACKAGE_ROOT" prettier --write "$CORE_MESSAGES_DIR/messages.json"
|
|
80
|
-
# Type generation (protobuf-types.js) runs once at the end of the Protocol V2
|
|
81
|
-
# section below, after both schemas are available.
|
|
82
|
-
else
|
|
83
|
-
# Intentional asymmetry with the Protocol V2 section below: the legacy firmware
|
|
84
|
-
# submodule is optional (the committed messages.json stays in use when it is
|
|
85
|
-
# absent), while firmware-pro2 is the only source of the Protocol V2 schema,
|
|
86
|
-
# so its absence is a hard error (exit 1).
|
|
87
|
-
echo "⚠️ firmware submodule not found at $SRC_PATH"
|
|
88
|
-
echo " Skipping Pro1 protobuf build. To enable:"
|
|
89
|
-
echo " git submodule update --init submodules/firmware"
|
|
90
|
-
fi
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
# ============================================================
|
|
94
|
-
# BUILD Protocol V2 messages-protocol-v2.json
|
|
95
|
-
# ============================================================
|
|
96
|
-
# Source of truth: submodules/firmware-pro2/sys/protobuf/onekey_protocol/.
|
|
97
|
-
# Protocol V2 keeps chain/app protocols under legacy/ and system protocols under latest/.
|
|
98
|
-
# The SDK flattens them into one protobuf schema for transport runtime, but it must keep
|
|
99
|
-
# firmware message names and enum values intact. SDK-facing aliases belong in core/API code,
|
|
100
|
-
# not in the protobuf schema.
|
|
101
|
-
# ============================================================
|
|
102
|
-
cd "$PARENT_PATH"
|
|
103
|
-
|
|
42
|
+
V1_TMP_PROTO="$DIST_PATH/messages-tmp.proto"
|
|
43
|
+
V2_TMP_PROTO="$PARENT_PATH/messages-protocol-v2-tmp.proto"
|
|
44
|
+
trap 'rm -f "$V1_TMP_PROTO" "$V2_TMP_PROTO"' EXIT
|
|
45
|
+
|
|
46
|
+
echo "=== Building Protocol V1 messages ==="
|
|
47
|
+
echo 'syntax = "proto2";' > "$V1_TMP_PROTO"
|
|
48
|
+
echo 'import "google/protobuf/descriptor.proto";' >> "$V1_TMP_PROTO"
|
|
49
|
+
grep -hv -e '^import ' -e '^syntax' -e '^package' -e 'option java_' "$SRC_PATH"/messages*.proto \
|
|
50
|
+
| sed 's/ hw\.trezor\.messages\.common\./ /' \
|
|
51
|
+
| sed 's/ common\./ /' \
|
|
52
|
+
| sed 's/ ethereum_definitions\./ /' \
|
|
53
|
+
| sed 's/ management\./ /' \
|
|
54
|
+
| sed 's/^option /\/\/ option /' \
|
|
55
|
+
| grep -v ' reserved ' >> "$V1_TMP_PROTO"
|
|
56
|
+
|
|
57
|
+
npx pbjs -t json -p "$DIST_PATH" -o "$DIST_PATH/messages.json" --keep-case "$V1_TMP_PROTO"
|
|
58
|
+
cp "$DIST_PATH/messages.json" "$CORE_MESSAGES_DIR/messages.json"
|
|
59
|
+
|
|
60
|
+
echo "=== Building Protocol V2 messages ==="
|
|
104
61
|
SRC_PRO2_LEGACY="$REPO_ROOT/submodules/firmware-pro2/sys/protobuf/onekey_protocol/legacy"
|
|
105
62
|
SRC_PRO2_LATEST="$REPO_ROOT/submodules/firmware-pro2/sys/protobuf/onekey_protocol/latest"
|
|
106
63
|
|
|
107
|
-
if [ -d "$
|
|
108
|
-
echo "
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
{
|
|
112
|
-
echo 'syntax = "proto2";'
|
|
113
|
-
echo 'import "google/protobuf/descriptor.proto";'
|
|
114
|
-
echo ''
|
|
115
|
-
|
|
116
|
-
# Pro2 firmware keeps chain/app protocols under legacy/, and Protocol V2
|
|
117
|
-
# device/filesystem/firmware protocols under latest/. Build one flat
|
|
118
|
-
# schema so Protocol V2 framing can also encode legacy public-chain calls.
|
|
119
|
-
grep -hv \
|
|
120
|
-
-e '^import ' -e '^syntax' -e '^package' -e 'option java_' \
|
|
121
|
-
"$SRC_PRO2_LEGACY"/messages*.proto \
|
|
122
|
-
| sed 's/ hw\.onekey\.messages\.[a-zA-Z_]*\./ /g' \
|
|
123
|
-
| sed 's/ crypto\./ /g' \
|
|
124
|
-
| sed 's/ ethereum_definitions\./ /g' \
|
|
125
|
-
| sed 's/ management\./ /g' \
|
|
126
|
-
| sed 's/^option /\/\/ option /' \
|
|
127
|
-
| grep -v ' reserved '
|
|
128
|
-
|
|
129
|
-
echo ''
|
|
130
|
-
echo '// --- Protocol V2 system messages ---'
|
|
131
|
-
grep -hv \
|
|
132
|
-
-e '^import ' -e '^syntax' -e '^package' -e 'option java_' \
|
|
133
|
-
-e '^option ' \
|
|
134
|
-
"$SRC_PRO2_LATEST"/messages*.proto \
|
|
135
|
-
| grep -v ' reserved '
|
|
136
|
-
|
|
137
|
-
} > "$TMP_PROTO"
|
|
64
|
+
if [[ ! -d "$SRC_PRO2_LEGACY" || ! -d "$SRC_PRO2_LATEST" ]]; then
|
|
65
|
+
echo "firmware-pro2 protobuf schema is missing"
|
|
66
|
+
exit 1
|
|
67
|
+
fi
|
|
138
68
|
|
|
139
|
-
|
|
69
|
+
{
|
|
70
|
+
echo 'syntax = "proto2";'
|
|
71
|
+
echo 'import "google/protobuf/descriptor.proto";'
|
|
72
|
+
echo ''
|
|
73
|
+
grep -hv \
|
|
74
|
+
-e '^import ' -e '^syntax' -e '^package' -e 'option java_' \
|
|
75
|
+
"$SRC_PRO2_LEGACY"/messages*.proto \
|
|
76
|
+
| sed 's/ hw\.onekey\.messages\.[a-zA-Z_]*\./ /g' \
|
|
77
|
+
| sed 's/ crypto\./ /g' \
|
|
78
|
+
| sed 's/ ethereum_definitions\./ /g' \
|
|
79
|
+
| sed 's/ management\./ /g' \
|
|
80
|
+
| sed 's/^option /\/\/ option /' \
|
|
81
|
+
| grep -v ' reserved '
|
|
82
|
+
echo ''
|
|
83
|
+
echo '// --- Protocol V2 system messages ---'
|
|
84
|
+
grep -hv \
|
|
85
|
+
-e '^import ' -e '^syntax' -e '^package' -e 'option java_' -e '^option ' \
|
|
86
|
+
"$SRC_PRO2_LATEST"/messages*.proto \
|
|
87
|
+
| grep -v ' reserved '
|
|
88
|
+
} > "$V2_TMP_PROTO"
|
|
89
|
+
|
|
90
|
+
# Protocol V2 latest schema defines common request names that also exist in
|
|
91
|
+
# legacy chain messages. Keep the legacy definitions and remove the duplicate
|
|
92
|
+
# top-level latest declarations before protobufjs parses the flattened schema.
|
|
93
|
+
node - "$V2_TMP_PROTO" <<'NODE'
|
|
140
94
|
const fs = require('fs');
|
|
141
95
|
|
|
142
96
|
const protoPath = process.argv[2];
|
|
143
97
|
let proto = fs.readFileSync(protoPath, 'utf8');
|
|
144
98
|
|
|
145
|
-
|
|
146
|
-
const pattern = new RegExp(`(^|\\n)message\\s+${name}\\s*\\{`, '
|
|
147
|
-
const
|
|
148
|
-
if (
|
|
149
|
-
|
|
99
|
+
function removeLastTopLevelMessage(source, name) {
|
|
100
|
+
const pattern = new RegExp(`(^|\\n)message\\s+${name}\\s*\\{`, 'gm');
|
|
101
|
+
const matches = Array.from(source.matchAll(pattern));
|
|
102
|
+
if (matches.length < 2) return source;
|
|
103
|
+
const match = matches[matches.length - 1];
|
|
150
104
|
const start = match.index + (match[1] ? match[1].length : 0);
|
|
151
|
-
let index = start;
|
|
152
105
|
let depth = 0;
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
if (
|
|
156
|
-
depth += 1;
|
|
157
|
-
} else if (char === '}') {
|
|
106
|
+
for (let index = start; index < source.length; index += 1) {
|
|
107
|
+
if (source[index] === '{') depth += 1;
|
|
108
|
+
if (source[index] === '}') {
|
|
158
109
|
depth -= 1;
|
|
159
110
|
if (depth === 0) {
|
|
160
|
-
index
|
|
161
|
-
while (
|
|
162
|
-
return `${source.slice(0, start)}${source.slice(
|
|
111
|
+
let end = index + 1;
|
|
112
|
+
while (end < source.length && /\\s/.test(source[end])) end += 1;
|
|
113
|
+
return `${source.slice(0, start)}${source.slice(end)}`;
|
|
163
114
|
}
|
|
164
115
|
}
|
|
165
|
-
index += 1;
|
|
166
116
|
}
|
|
167
117
|
throw new Error(`Unterminated message block: ${name}`);
|
|
168
|
-
}
|
|
118
|
+
}
|
|
169
119
|
|
|
170
|
-
[
|
|
171
|
-
|
|
172
|
-
'GetFeatures',
|
|
173
|
-
'OnekeyGetFeatures',
|
|
174
|
-
'Features',
|
|
175
|
-
].forEach(name => {
|
|
176
|
-
proto = removeTopLevelMessage(proto, name);
|
|
120
|
+
['Initialize', 'GetFeatures', 'OnekeyGetFeatures', 'Features'].forEach(name => {
|
|
121
|
+
proto = removeLastTopLevelMessage(proto, name);
|
|
177
122
|
});
|
|
178
|
-
fs.writeFileSync(protoPath, proto);
|
|
179
|
-
|
|
180
|
-
const messageNames = new Set(
|
|
181
|
-
Array.from(proto.matchAll(/^\s*message\s+([A-Za-z_][A-Za-z0-9_]*)\s*\{/gm)).map(
|
|
182
|
-
match => match[1]
|
|
183
|
-
)
|
|
184
|
-
);
|
|
185
|
-
const messageTypeNames = new Set(
|
|
186
|
-
Array.from(proto.matchAll(/^\s*MessageType_([A-Za-z_][A-Za-z0-9_]*)\s*=/gm)).map(
|
|
187
|
-
match => match[1]
|
|
188
|
-
)
|
|
189
|
-
);
|
|
190
|
-
const requiredMessages = [
|
|
191
|
-
'DeviceFactoryInfoSet',
|
|
192
|
-
'DeviceFactoryInfoGet',
|
|
193
|
-
'DeviceFactoryInfo',
|
|
194
|
-
'ProtocolInfoRequest',
|
|
195
|
-
'ProtocolInfo',
|
|
196
|
-
'Ping',
|
|
197
|
-
'Success',
|
|
198
|
-
'Failure',
|
|
199
|
-
'DeviceReboot',
|
|
200
|
-
'DeviceInfoGet',
|
|
201
|
-
'DeviceInfo',
|
|
202
|
-
'DeviceFirmwareUpdateRequest',
|
|
203
|
-
'DeviceFirmwareUpdateStatusGet',
|
|
204
|
-
'DeviceFirmwareUpdateStatus',
|
|
205
|
-
'FilesystemPermissionFix',
|
|
206
|
-
'FilesystemPathInfo',
|
|
207
|
-
'FilesystemPathInfoQuery',
|
|
208
|
-
'FilesystemFile',
|
|
209
|
-
'FilesystemFileRead',
|
|
210
|
-
'FilesystemFileWrite',
|
|
211
|
-
'FilesystemFileDelete',
|
|
212
|
-
'FilesystemDir',
|
|
213
|
-
'FilesystemDirList',
|
|
214
|
-
'FilesystemDirMake',
|
|
215
|
-
'FilesystemDirRemove',
|
|
216
|
-
'FilesystemFormat',
|
|
217
|
-
];
|
|
218
|
-
const missingMessages = requiredMessages.filter(name => !messageNames.has(name));
|
|
219
|
-
const missingMessageTypes = requiredMessages.filter(name => !messageTypeNames.has(name));
|
|
220
|
-
|
|
221
|
-
if (missingMessages.length > 0 || missingMessageTypes.length > 0) {
|
|
222
|
-
throw new Error(
|
|
223
|
-
`Protocol V2 schema missing required entries: messages=[${missingMessages.join(
|
|
224
|
-
', '
|
|
225
|
-
)}], messageTypes=[${missingMessageTypes.join(
|
|
226
|
-
', '
|
|
227
|
-
)}]. Make sure submodules/firmware-pro2 is checked out on branch dev ` +
|
|
228
|
-
'(origin/dev), which contains the latest Protocol V2 Device*/Filesystem* messages.'
|
|
229
|
-
);
|
|
230
|
-
}
|
|
231
123
|
|
|
124
|
+
fs.writeFileSync(protoPath, proto);
|
|
232
125
|
NODE
|
|
233
126
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
-o "$PARENT_PATH/../messages-protocol-v2.json" \
|
|
237
|
-
--keep-case \
|
|
238
|
-
"$(basename "$TMP_PROTO")"
|
|
239
|
-
|
|
240
|
-
rm -f "$TMP_PROTO"
|
|
241
|
-
|
|
242
|
-
cp "$PARENT_PATH/../messages-protocol-v2.json" "$CORE_MESSAGES_DIR/messages-protocol-v2.json"
|
|
243
|
-
echo "Protocol V2 messages-protocol-v2.json generated from firmware-pro2 legacy + latest schema and copied to core"
|
|
127
|
+
npx pbjs -t json -p "$PARENT_PATH" -o "$PACKAGE_ROOT/messages-protocol-v2.json" --keep-case "$V2_TMP_PROTO"
|
|
128
|
+
cp "$PACKAGE_ROOT/messages-protocol-v2.json" "$CORE_MESSAGES_DIR/messages-protocol-v2.json"
|
|
244
129
|
|
|
245
|
-
|
|
246
|
-
|
|
130
|
+
echo "generating type definitions for: $LANG"
|
|
131
|
+
cd "$PARENT_PATH"
|
|
132
|
+
node ./protobuf-types.js "$LANG"
|
|
247
133
|
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
# Unlike the optional Pro1 (legacy) section above, the firmware-pro2 submodule is
|
|
254
|
-
# the only source of the Protocol V2 schema, so a missing checkout is a hard error.
|
|
255
|
-
echo "firmware-pro2 latest protobuf schema not found at $SRC_PRO2_LATEST"
|
|
256
|
-
echo "The Protocol V2 schema requires firmware-pro2 on branch dev."
|
|
257
|
-
echo "Run: git submodule update --init submodules/firmware-pro2"
|
|
258
|
-
echo "Then: git -C submodules/firmware-pro2 fetch origin dev && git -C submodules/firmware-pro2 checkout origin/dev"
|
|
259
|
-
exit 1
|
|
260
|
-
fi
|
|
134
|
+
yarn --cwd "$PACKAGE_ROOT" prettier --write "$DIST_PATH/messages.json"
|
|
135
|
+
yarn --cwd "$PACKAGE_ROOT" prettier --write "$CORE_MESSAGES_DIR/messages.json"
|
|
136
|
+
yarn --cwd "$PACKAGE_ROOT" prettier --write "$PACKAGE_ROOT/messages-protocol-v2.json"
|
|
137
|
+
yarn --cwd "$PACKAGE_ROOT" prettier --write "$CORE_MESSAGES_DIR/messages-protocol-v2.json"
|
|
138
|
+
yarn --cwd "$PACKAGE_ROOT" prettier --write "$PACKAGE_ROOT/src/types/messages.ts"
|
|
@@ -19,6 +19,10 @@ const optionalJsonFiles = ['../messages-protocol-v2.json'];
|
|
|
19
19
|
const OPTIONAL_DUPLICATE_TYPE_ALIASES = {
|
|
20
20
|
DeviceInfo: 'ProtocolV2DeviceInfo',
|
|
21
21
|
};
|
|
22
|
+
const OPTIONAL_NESTED_TYPE_ALIASES = [
|
|
23
|
+
{ parent: 'Failure', name: 'FailureType', alias: 'ProtocolV2FailureType' },
|
|
24
|
+
{ parent: 'Features', name: 'Capability', alias: 'ProtocolV2Capability' },
|
|
25
|
+
];
|
|
22
26
|
const messageTypeAliases = {};
|
|
23
27
|
const skipMessageTypeKeys = new Set(Object.values(OPTIONAL_DUPLICATE_TYPE_ALIASES));
|
|
24
28
|
|
|
@@ -53,14 +57,21 @@ const ENUM_KEYS = [
|
|
|
53
57
|
'InputScriptType',
|
|
54
58
|
'OutputScriptType',
|
|
55
59
|
'RequestType',
|
|
60
|
+
'KaspaInputScriptType',
|
|
61
|
+
'KaspaOutputScriptType',
|
|
62
|
+
'KaspaRequestType',
|
|
56
63
|
'BackupType',
|
|
57
64
|
'Capability',
|
|
58
65
|
'SafetyCheckLevel',
|
|
59
66
|
'ButtonRequestType',
|
|
60
67
|
'PinMatrixRequestType',
|
|
61
68
|
'WordRequestType',
|
|
69
|
+
'ProtocolV2Capability',
|
|
62
70
|
];
|
|
63
71
|
|
|
72
|
+
// Safety checks are accepted as both protobuf enum names and numeric legacy values.
|
|
73
|
+
const ENUM_KEYS_WITH_NUMERIC_VALUES = ['SafetyCheckLevel'];
|
|
74
|
+
|
|
64
75
|
const parseEnumTypescript = (itemName, item) => {
|
|
65
76
|
const value = [];
|
|
66
77
|
const IS_KEY = ENUM_KEYS.includes(itemName);
|
|
@@ -79,7 +90,10 @@ const parseEnumTypescript = (itemName, item) => {
|
|
|
79
90
|
value.push('}');
|
|
80
91
|
|
|
81
92
|
if (IS_KEY) {
|
|
82
|
-
|
|
93
|
+
const numericValue = ENUM_KEYS_WITH_NUMERIC_VALUES.includes(itemName)
|
|
94
|
+
? ` | Enum_${itemName}`
|
|
95
|
+
: '';
|
|
96
|
+
value.push(`export type ${itemName} = keyof typeof Enum_${itemName}${numericValue};`);
|
|
83
97
|
}
|
|
84
98
|
// empty line
|
|
85
99
|
value.push('');
|
|
@@ -317,6 +331,10 @@ optionalJsonFiles.forEach(jsonFile => {
|
|
|
317
331
|
const jsonPath = path.join(__dirname, jsonFile);
|
|
318
332
|
if (!fs.existsSync(jsonPath)) return;
|
|
319
333
|
const optionalJson = readJson(jsonPath);
|
|
334
|
+
OPTIONAL_NESTED_TYPE_ALIASES.forEach(({ parent, name, alias }) => {
|
|
335
|
+
const definition = optionalJson.nested[parent]?.nested?.[name];
|
|
336
|
+
if (definition) optionalJson.nested[alias] = definition;
|
|
337
|
+
});
|
|
320
338
|
optionalJsons.push(optionalJson);
|
|
321
339
|
Object.keys(optionalJson.nested).forEach(e => {
|
|
322
340
|
if (!json.nested[e]) return; // V2-only type, parsed after the V1 pass below
|
package/src/constants.ts
CHANGED
|
@@ -1,44 +1,58 @@
|
|
|
1
1
|
// ---- Protocol V1 (Pro1 / Touch / Mini / Classic) ----
|
|
2
2
|
|
|
3
|
-
/** Protocol V1 USB report
|
|
3
|
+
/** Protocol V1 USB report marker, ASCII `?`. */
|
|
4
4
|
export const PROTOCOL_V1_REPORT_ID = 0x3f;
|
|
5
5
|
|
|
6
|
-
/** Protocol V1 envelope
|
|
6
|
+
/** Protocol V1 envelope marker, ASCII `#`. */
|
|
7
7
|
export const PROTOCOL_V1_HEADER_BYTE = 0x23;
|
|
8
8
|
|
|
9
|
-
/** Protocol V1
|
|
9
|
+
/** Protocol V1 payload bytes per chunk after the report marker. */
|
|
10
10
|
export const PROTOCOL_V1_CHUNK_PAYLOAD_SIZE = 63;
|
|
11
11
|
|
|
12
|
-
/** Protocol V1 USB packet
|
|
12
|
+
/** Protocol V1 USB packet length: report marker plus chunk payload. */
|
|
13
13
|
export const PROTOCOL_V1_USB_PACKET_SIZE = PROTOCOL_V1_CHUNK_PAYLOAD_SIZE + 1;
|
|
14
14
|
|
|
15
|
-
/** Protocol V1 message metadata
|
|
15
|
+
/** Protocol V1 message metadata: two-byte type plus four-byte payload length. */
|
|
16
16
|
export const PROTOCOL_V1_MESSAGE_HEADER_SIZE = 2 + 4;
|
|
17
17
|
|
|
18
|
-
/** Protocol V1 envelope metadata
|
|
18
|
+
/** Protocol V1 envelope metadata: `##`, message type, and payload length. */
|
|
19
19
|
export const PROTOCOL_V1_ENVELOPE_HEADER_SIZE = 1 + 1 + PROTOCOL_V1_MESSAGE_HEADER_SIZE;
|
|
20
20
|
|
|
21
21
|
// ---- Protocol V2 (Pro2 USB / BLE transports) ----
|
|
22
22
|
|
|
23
|
-
/**
|
|
24
|
-
export const PROTOCOL_V2_FRAME_MAX_BYTES =
|
|
23
|
+
/** Firmware Proto Link runtime limit for a complete V2 frame, including header and CRC. */
|
|
24
|
+
export const PROTOCOL_V2_FRAME_MAX_BYTES = 4200;
|
|
25
25
|
|
|
26
|
-
/**
|
|
26
|
+
/** FilesystemFileWrite chunk size over WebUSB. */
|
|
27
27
|
export const PROTOCOL_V2_WEBUSB_FILE_CHUNK_SIZE = 4000;
|
|
28
28
|
|
|
29
|
-
/**
|
|
29
|
+
/** FilesystemFileWrite chunk size over BLE. */
|
|
30
30
|
export const PROTOCOL_V2_BLE_FILE_CHUNK_SIZE = 1800;
|
|
31
31
|
|
|
32
|
-
/**
|
|
32
|
+
/** BLE FilesystemFileRead chunk size, limited by the Pro2 1024-byte UART TX buffer. */
|
|
33
|
+
export const PROTOCOL_V2_BLE_FILE_READ_CHUNK_SIZE = 900;
|
|
34
|
+
|
|
35
|
+
/** Pro2 BLE/UART RX FIFO must hold a complete Proto Link frame. */
|
|
36
|
+
export const PROTOCOL_V2_BLE_FRAME_MAX_BYTES = 2048;
|
|
37
|
+
|
|
38
|
+
/** @deprecated Use the transport-specific WebUSB or BLE file chunk constant. */
|
|
33
39
|
export const PROTOCOL_V2_FILE_CHUNK_SIZE = PROTOCOL_V2_WEBUSB_FILE_CHUNK_SIZE;
|
|
34
40
|
|
|
35
41
|
/**
|
|
36
|
-
* Protocol V2
|
|
37
|
-
*
|
|
42
|
+
* Protocol V2 routing channel. USB reaches the main MCU directly, while BLE routes
|
|
43
|
+
* through the BLE coprocessor UART bridge.
|
|
38
44
|
*/
|
|
39
45
|
export const PROTOCOL_V2_CHANNEL_USB = 0;
|
|
40
46
|
export const PROTOCOL_V2_CHANNEL_BLE_UART = 1;
|
|
41
47
|
export const PROTOCOL_V2_CHANNEL_SOCKET = 2;
|
|
42
48
|
|
|
43
|
-
/** protobuf
|
|
49
|
+
/** packet_src for protobuf commands; firmware routes zero to the protobuf dispatcher. */
|
|
44
50
|
export const PROTOCOL_V2_PACKET_SRC_COMMAND = 0;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Shared upper bound for a Protocol V2 request without a method-specific timeout.
|
|
54
|
+
* Interactive and signing calls may wait on the device UI, so keep this longer than
|
|
55
|
+
* ordinary transport timeouts while still preventing a stalled link from blocking
|
|
56
|
+
* the per-device queue forever.
|
|
57
|
+
*/
|
|
58
|
+
export const PROTOCOL_V2_DEFAULT_RESPONSE_TIMEOUT_MS = 5 * 60 * 1000;
|
package/src/index.ts
CHANGED
|
@@ -10,8 +10,11 @@ import {
|
|
|
10
10
|
} from './serialization';
|
|
11
11
|
import { PROTOCOL_V2_SYS_MESSAGE_THRESHOLD, ProtocolV1, ProtocolV2 } from './protocols';
|
|
12
12
|
import * as protocolV2Codec from './protocols/v2';
|
|
13
|
+
import { ProtocolV2LinkManager } from './protocols/v2/link-manager';
|
|
14
|
+
import { ProtocolV2UsbTransportBase } from './protocols/v2/usb-transport-base';
|
|
13
15
|
import {
|
|
14
16
|
ProtocolV2FrameAssembler,
|
|
17
|
+
ProtocolV2SequenceCursor,
|
|
15
18
|
ProtocolV2Session,
|
|
16
19
|
bytesToHex,
|
|
17
20
|
concatUint8Arrays,
|
|
@@ -38,17 +41,22 @@ export type {
|
|
|
38
41
|
OneKeyDeviceInfoBase,
|
|
39
42
|
OneKeyDeviceCommType,
|
|
40
43
|
ProtocolType,
|
|
44
|
+
TransportDeviceDisconnectEvent,
|
|
41
45
|
} from './types';
|
|
42
46
|
|
|
43
|
-
export { Messages } from './types';
|
|
47
|
+
export { Messages, TRANSPORT_EVENT } from './types';
|
|
44
48
|
export * from './types/messages';
|
|
45
49
|
export * from './utils/logBlockCommand';
|
|
50
|
+
export * from './utils/transportLog';
|
|
46
51
|
|
|
47
52
|
export * from './constants';
|
|
48
53
|
export * from './protocols';
|
|
49
54
|
export * as protocolV1 from './protocols/v1';
|
|
50
55
|
export * as protocolV2 from './protocols/v2';
|
|
51
56
|
export * from './protocols/v2/session';
|
|
57
|
+
export * from './protocols/v2/link-manager';
|
|
58
|
+
export * from './protocols/v2/ble-frame-writer';
|
|
59
|
+
export * from './protocols/v2/usb-transport-base';
|
|
52
60
|
|
|
53
61
|
export default {
|
|
54
62
|
check,
|
|
@@ -58,7 +66,10 @@ export default {
|
|
|
58
66
|
ProtocolV2,
|
|
59
67
|
PROTOCOL_V2_SYS_MESSAGE_THRESHOLD,
|
|
60
68
|
ProtocolV2FrameAssembler,
|
|
69
|
+
ProtocolV2LinkManager,
|
|
70
|
+
ProtocolV2SequenceCursor,
|
|
61
71
|
ProtocolV2Session,
|
|
72
|
+
ProtocolV2UsbTransportBase,
|
|
62
73
|
bytesToHex,
|
|
63
74
|
concatUint8Arrays,
|
|
64
75
|
createMessageFromName,
|