@onekeyfe/hwk-adapter-core 1.2.8-alpha.11 → 1.2.8-alpha.111
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/dist/{chunk-BAGEFFFY.js → chunk-AYS7A7Z3.js} +45 -13
- package/dist/chunk-AYS7A7Z3.js.map +1 -0
- package/dist/chunk-DB4JOPZO.mjs +74 -0
- package/dist/chunk-DB4JOPZO.mjs.map +1 -0
- package/dist/{chunk-U63HTLGJ.mjs → chunk-G5EU7VRW.mjs} +44 -12
- package/dist/chunk-G5EU7VRW.mjs.map +1 -0
- package/dist/{chunk-YA4BWFTZ.mjs → chunk-KTRQZ5AJ.mjs} +155 -55
- package/dist/chunk-KTRQZ5AJ.mjs.map +1 -0
- package/dist/{chunk-SZZF623R.js → chunk-T5HIQPNG.js} +155 -55
- package/dist/chunk-T5HIQPNG.js.map +1 -0
- package/dist/chunk-YLCHSNXO.js +74 -0
- package/dist/chunk-YLCHSNXO.js.map +1 -0
- package/dist/errors-BgJ4kzRx.d.mts +276 -0
- package/dist/errors-BgJ4kzRx.d.ts +276 -0
- package/dist/errors.d.mts +1 -207
- package/dist/errors.d.ts +1 -207
- package/dist/errors.js +8 -2
- package/dist/errors.js.map +1 -1
- package/dist/errors.mjs +9 -3
- package/dist/index.d.mts +651 -104
- package/dist/index.d.ts +651 -104
- package/dist/index.js +1105 -87
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1097 -79
- package/dist/index.mjs.map +1 -1
- package/dist/{ui-events-Cb7haAeE.d.ts → ui-events-C61GTilK.d.mts} +67 -3
- package/dist/{ui-events-Cb7haAeE.d.mts → ui-events-C61GTilK.d.ts} +67 -3
- package/dist/ui-events.d.mts +1 -1
- package/dist/ui-events.d.ts +1 -1
- package/dist/ui-events.js +3 -2
- package/dist/ui-events.js.map +1 -1
- package/dist/ui-events.mjs +2 -1
- package/package.json +2 -2
- package/dist/chunk-BAGEFFFY.js.map +0 -1
- package/dist/chunk-SZZF623R.js.map +0 -1
- package/dist/chunk-U63HTLGJ.mjs.map +0 -1
- package/dist/chunk-YA4BWFTZ.mjs.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
|
-
HardwareErrorCode,
|
|
3
|
-
ORPHAN_ELIGIBLE_ERROR_CODES,
|
|
4
|
-
createHwkError,
|
|
5
2
|
enrichErrorMessage
|
|
6
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-DB4JOPZO.mjs";
|
|
7
4
|
import {
|
|
8
5
|
UI_EVENT,
|
|
9
6
|
UI_REQUEST,
|
|
@@ -13,16 +10,49 @@ import {
|
|
|
13
10
|
UI_REQUEST_TIMEOUT_TAG,
|
|
14
11
|
UI_RESPONSE,
|
|
15
12
|
UiRequestRegistry
|
|
16
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-G5EU7VRW.mjs";
|
|
14
|
+
import {
|
|
15
|
+
HardwareErrorCode,
|
|
16
|
+
ORPHAN_ELIGIBLE_ERROR_CODES,
|
|
17
|
+
createHwkError,
|
|
18
|
+
defaultOriginForCode,
|
|
19
|
+
defaultRecoveryForCode,
|
|
20
|
+
isHwkRecoveryHint,
|
|
21
|
+
operationMayHaveCompletedParams
|
|
22
|
+
} from "./chunk-KTRQZ5AJ.mjs";
|
|
17
23
|
|
|
18
24
|
// src/types/response.ts
|
|
19
25
|
function success(payload) {
|
|
20
26
|
return { success: true, payload };
|
|
21
27
|
}
|
|
22
|
-
function failure(code, error, params) {
|
|
28
|
+
function failure(code, error, params, origin, recovery) {
|
|
23
29
|
return {
|
|
24
30
|
success: false,
|
|
25
|
-
payload: {
|
|
31
|
+
payload: {
|
|
32
|
+
error,
|
|
33
|
+
code,
|
|
34
|
+
...origin !== void 0 ? { origin } : {},
|
|
35
|
+
recovery: recovery ?? defaultRecoveryForCode(code),
|
|
36
|
+
...params ? { params } : {}
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/types/device.ts
|
|
42
|
+
function resolveSearchTargetReusePolicy(device) {
|
|
43
|
+
return device.capabilities?.persistentDeviceIdentity ? "reconnectable" : "current-discovery";
|
|
44
|
+
}
|
|
45
|
+
function toPhysicalDeviceSearchTarget(vendor, device) {
|
|
46
|
+
return {
|
|
47
|
+
searchTargetId: device.connectId,
|
|
48
|
+
searchTargetReusePolicy: resolveSearchTargetReusePolicy(device),
|
|
49
|
+
vendor,
|
|
50
|
+
connectionType: device.connectionType,
|
|
51
|
+
kind: "physical",
|
|
52
|
+
label: device.label,
|
|
53
|
+
model: device.model,
|
|
54
|
+
modelName: device.modelName,
|
|
55
|
+
serialNumber: device.serialNumber
|
|
26
56
|
};
|
|
27
57
|
}
|
|
28
58
|
|
|
@@ -35,8 +65,20 @@ var CHAIN_FINGERPRINT_PATHS = {
|
|
|
35
65
|
// Cointype 1 (testnet) is rejected by some Ledger BTC App configurations.
|
|
36
66
|
btc: "m/44'/0'/0'",
|
|
37
67
|
sol: "m/44'/501'/0'",
|
|
38
|
-
tron: "m/44'/195'/0'/0/0"
|
|
68
|
+
tron: "m/44'/195'/0'/0/0",
|
|
69
|
+
// Shielded UA (single Orchard receiver) derived from the transparent path.
|
|
70
|
+
zcash: "m/44'/133'/0'/0/0"
|
|
39
71
|
};
|
|
72
|
+
function parseBip32MasterFingerprint(value) {
|
|
73
|
+
if (typeof value !== "string") return void 0;
|
|
74
|
+
const normalized = value.trim().toLowerCase();
|
|
75
|
+
return /^[0-9a-f]{8}$/.test(normalized) ? normalized : void 0;
|
|
76
|
+
}
|
|
77
|
+
var MISSING_BIP32_MASTER_FINGERPRINT = "00000000";
|
|
78
|
+
function parseWalletMasterFingerprint(value) {
|
|
79
|
+
const fingerprint = parseBip32MasterFingerprint(value);
|
|
80
|
+
return fingerprint === MISSING_BIP32_MASTER_FINGERPRINT ? void 0 : fingerprint;
|
|
81
|
+
}
|
|
40
82
|
function deriveDeviceFingerprint(value) {
|
|
41
83
|
return bytesToHex(sha256(utf8ToBytes(value))).slice(0, 16);
|
|
42
84
|
}
|
|
@@ -66,7 +108,8 @@ var SDK = {
|
|
|
66
108
|
DEVICE_STUCK: "device-stuck",
|
|
67
109
|
DEVICE_UNRESPONSIVE: "device-unresponsive",
|
|
68
110
|
DEVICE_RECOVERED: "device-recovered",
|
|
69
|
-
DEVICE_INTERACTION: "device-interaction"
|
|
111
|
+
DEVICE_INTERACTION: "device-interaction",
|
|
112
|
+
OPERATION_ENDED: "operation-ended"
|
|
70
113
|
};
|
|
71
114
|
|
|
72
115
|
// src/utils/DeviceJobQueue.ts
|
|
@@ -75,6 +118,7 @@ var DeviceJobQueue = class {
|
|
|
75
118
|
this._tail = Promise.resolve();
|
|
76
119
|
this._active = null;
|
|
77
120
|
this._jobs = /* @__PURE__ */ new Map();
|
|
121
|
+
this._cancelScopes = /* @__PURE__ */ new Map();
|
|
78
122
|
/** Incremented on clear() so queued-but-not-yet-running jobs detect invalidation. */
|
|
79
123
|
this._generation = 0;
|
|
80
124
|
this._generationCancelReasons = /* @__PURE__ */ new Map();
|
|
@@ -97,12 +141,13 @@ var DeviceJobQueue = class {
|
|
|
97
141
|
abortController: ac,
|
|
98
142
|
startedAt: Date.now()
|
|
99
143
|
};
|
|
100
|
-
this._jobs.set(jobToken,
|
|
144
|
+
this._jobs.set(jobToken, activeJob);
|
|
101
145
|
const next = prev.catch(() => {
|
|
102
146
|
}).then(async () => {
|
|
103
147
|
if (this._generation !== gen) {
|
|
104
148
|
throw this._generationCancelReasons.get(gen) ?? new Error("Job cancelled: queue was cleared");
|
|
105
149
|
}
|
|
150
|
+
if (ac.signal.aborted) throw ac.signal.reason;
|
|
106
151
|
this._active = activeJob;
|
|
107
152
|
try {
|
|
108
153
|
return await job(ac.signal);
|
|
@@ -118,32 +163,52 @@ var DeviceJobQueue = class {
|
|
|
118
163
|
});
|
|
119
164
|
return next;
|
|
120
165
|
}
|
|
121
|
-
/**
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
166
|
+
/**
|
|
167
|
+
* Holds a cancel across the gaps between a bundle's per-item jobs, when the queue is empty.
|
|
168
|
+
* Callers must `release()` when the bundle ends.
|
|
169
|
+
*/
|
|
170
|
+
createCancelScope(deviceId) {
|
|
171
|
+
const scopeToken = {};
|
|
172
|
+
const abortController = new AbortController();
|
|
173
|
+
this._cancelScopes.set(scopeToken, { deviceId, abortController });
|
|
174
|
+
return {
|
|
175
|
+
signal: abortController.signal,
|
|
176
|
+
release: () => {
|
|
177
|
+
this._cancelScopes.delete(scopeToken);
|
|
178
|
+
}
|
|
179
|
+
};
|
|
127
180
|
}
|
|
128
|
-
/**
|
|
129
|
-
|
|
181
|
+
/**
|
|
182
|
+
* Cancels the running job only; `reason` becomes signal.reason. Adapters use
|
|
183
|
+
* `cancelActiveAndPending`; this stays for a connector layer that stops only what is on the wire.
|
|
184
|
+
*/
|
|
185
|
+
cancelActive(deviceId, reason) {
|
|
130
186
|
if (!this._active) return false;
|
|
131
187
|
if (deviceId && this._active.deviceId !== deviceId) return false;
|
|
132
|
-
this._active.abortController.abort(reason ?? new Error("
|
|
188
|
+
this._active.abortController.abort(reason ?? new Error("Cancelled"));
|
|
133
189
|
return true;
|
|
134
190
|
}
|
|
135
|
-
/**
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
140
|
-
/** Cancel the active job and invalidate queued jobs that have not started. */
|
|
191
|
+
/**
|
|
192
|
+
* Cancels the active job and unstarted queued jobs; returns whether anything was reached. Only
|
|
193
|
+
* `undefined` means everything: an empty key matches nothing instead of tearing the queue down.
|
|
194
|
+
*/
|
|
141
195
|
cancelActiveAndPending(deviceId, reason) {
|
|
142
|
-
|
|
143
|
-
|
|
196
|
+
const cancelReason = reason ?? new Error("Cancelled by cancelActiveAndPending");
|
|
197
|
+
if (deviceId !== void 0) {
|
|
198
|
+
let cancelled = false;
|
|
199
|
+
const targets = [this._jobs, this._cancelScopes];
|
|
200
|
+
for (const map of targets) {
|
|
201
|
+
for (const target of map.values()) {
|
|
202
|
+
if (target.deviceId !== deviceId) continue;
|
|
203
|
+
target.abortController.abort(cancelReason);
|
|
204
|
+
cancelled = true;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return cancelled;
|
|
144
208
|
}
|
|
145
|
-
this.
|
|
146
|
-
|
|
209
|
+
const reached = this._active !== null || this._jobs.size > 0 || this._cancelScopes.size > 0;
|
|
210
|
+
this.clear(cancelReason);
|
|
211
|
+
return reached;
|
|
147
212
|
}
|
|
148
213
|
/** Get info about the currently active job, or null if idle. */
|
|
149
214
|
getActiveJob(deviceId) {
|
|
@@ -155,10 +220,6 @@ var DeviceJobQueue = class {
|
|
|
155
220
|
startedAt: this._active.startedAt
|
|
156
221
|
};
|
|
157
222
|
}
|
|
158
|
-
/** True if any job is currently running. */
|
|
159
|
-
isBusy() {
|
|
160
|
-
return this._jobs.size > 0;
|
|
161
|
-
}
|
|
162
223
|
clear(reason) {
|
|
163
224
|
const cancelledGeneration = this._generation;
|
|
164
225
|
this._generation++;
|
|
@@ -172,9 +233,234 @@ var DeviceJobQueue = class {
|
|
|
172
233
|
if (this._active) {
|
|
173
234
|
this._active.abortController.abort(cancelReason);
|
|
174
235
|
}
|
|
236
|
+
for (const scope of this._cancelScopes.values()) {
|
|
237
|
+
scope.abortController.abort(cancelReason);
|
|
238
|
+
}
|
|
175
239
|
}
|
|
176
240
|
};
|
|
177
241
|
|
|
242
|
+
// src/utils/hardwareRuntimeId.ts
|
|
243
|
+
import { bytesToHex as bytesToHex2, randomBytes } from "@noble/hashes/utils";
|
|
244
|
+
var HARDWARE_RUNTIME_ID_PREFIX = "hwk:runtime";
|
|
245
|
+
var RANDOM_ID_BYTES = 16;
|
|
246
|
+
var RANDOM_ID_PATTERN = /^[0-9a-f]{32}$/;
|
|
247
|
+
var KINDS = /* @__PURE__ */ new Set([
|
|
248
|
+
"operation",
|
|
249
|
+
"search-target",
|
|
250
|
+
"link"
|
|
251
|
+
]);
|
|
252
|
+
var VENDORS = /* @__PURE__ */ new Set(["trezor", "ledger", "keystone"]);
|
|
253
|
+
function create(kind, vendor) {
|
|
254
|
+
return `${HARDWARE_RUNTIME_ID_PREFIX}:${kind}:${vendor}:${bytesToHex2(
|
|
255
|
+
randomBytes(RANDOM_ID_BYTES)
|
|
256
|
+
)}`;
|
|
257
|
+
}
|
|
258
|
+
function createHardwareOperationId(vendor) {
|
|
259
|
+
return create("operation", vendor);
|
|
260
|
+
}
|
|
261
|
+
function createHardwareSearchTargetId(vendor) {
|
|
262
|
+
return create("search-target", vendor);
|
|
263
|
+
}
|
|
264
|
+
function createHardwareLinkId(vendor) {
|
|
265
|
+
return create("link", vendor);
|
|
266
|
+
}
|
|
267
|
+
function hasHardwareRuntimeIdPrefix(value) {
|
|
268
|
+
return typeof value === "string" && value.startsWith(`${HARDWARE_RUNTIME_ID_PREFIX}:`);
|
|
269
|
+
}
|
|
270
|
+
function parseHardwareRuntimeId(value) {
|
|
271
|
+
if (!hasHardwareRuntimeIdPrefix(value)) return void 0;
|
|
272
|
+
const parts = value.split(":");
|
|
273
|
+
if (parts.length !== 5) return void 0;
|
|
274
|
+
const [, , kind, vendor, nonce] = parts;
|
|
275
|
+
if (!KINDS.has(kind) || !VENDORS.has(vendor) || !RANDOM_ID_PATTERN.test(nonce)) {
|
|
276
|
+
return void 0;
|
|
277
|
+
}
|
|
278
|
+
return { kind, vendor };
|
|
279
|
+
}
|
|
280
|
+
function isHardwareOperationId(value) {
|
|
281
|
+
return parseHardwareRuntimeId(value)?.kind === "operation";
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// src/utils/OperationRegistry.ts
|
|
285
|
+
var OPERATION_DEFAULT_TTL_MS = 6e5;
|
|
286
|
+
var uniqueKeys = (keys) => Array.from(new Set(keys.filter(Boolean)));
|
|
287
|
+
var OperationRegistry = class {
|
|
288
|
+
constructor(options) {
|
|
289
|
+
this._active = /* @__PURE__ */ new Map();
|
|
290
|
+
this._ended = /* @__PURE__ */ new Map();
|
|
291
|
+
this._vendor = options.vendor;
|
|
292
|
+
this._ttlMs = options.ttlMs ?? OPERATION_DEFAULT_TTL_MS;
|
|
293
|
+
this._onEnded = options.onEnded;
|
|
294
|
+
}
|
|
295
|
+
create(params) {
|
|
296
|
+
const operationId = createHardwareOperationId(this._vendor);
|
|
297
|
+
const operation = {
|
|
298
|
+
operationId,
|
|
299
|
+
connectId: params.connectId,
|
|
300
|
+
device: params.device,
|
|
301
|
+
connectionType: params.connectionType,
|
|
302
|
+
connectionKeys: uniqueKeys([
|
|
303
|
+
params.searchTargetId,
|
|
304
|
+
params.connectId,
|
|
305
|
+
...params.connectionKeys ?? []
|
|
306
|
+
])
|
|
307
|
+
};
|
|
308
|
+
const timer = this._createTimer(operationId);
|
|
309
|
+
this._active.set(operationId, { ...operation, timer, retainCount: 0 });
|
|
310
|
+
return operation;
|
|
311
|
+
}
|
|
312
|
+
resolve(operationId) {
|
|
313
|
+
const active = this._active.get(operationId);
|
|
314
|
+
if (!active) {
|
|
315
|
+
const ended = this._ended.get(operationId);
|
|
316
|
+
if (ended) {
|
|
317
|
+
throw createHwkError({
|
|
318
|
+
code: 10113 /* OperationEnded */,
|
|
319
|
+
message: `Hardware operation has ended (${ended.reason})`,
|
|
320
|
+
params: { operationId, reason: ended.reason }
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
throw createHwkError({
|
|
324
|
+
code: 10112 /* OperationNotFound */,
|
|
325
|
+
message: "Hardware operation was not found",
|
|
326
|
+
params: { operationId }
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
this._touch(active);
|
|
330
|
+
return active;
|
|
331
|
+
}
|
|
332
|
+
/** Keep an operation alive while one device job is actively using it. */
|
|
333
|
+
retain(operationId) {
|
|
334
|
+
const active = this.resolve(operationId);
|
|
335
|
+
active.retainCount += 1;
|
|
336
|
+
this._touch(active);
|
|
337
|
+
let released = false;
|
|
338
|
+
return () => {
|
|
339
|
+
if (released) return;
|
|
340
|
+
released = true;
|
|
341
|
+
const current = this._active.get(operationId);
|
|
342
|
+
if (!current) return;
|
|
343
|
+
current.retainCount = Math.max(0, current.retainCount - 1);
|
|
344
|
+
this._touch(current);
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
/** Return a live operation without refreshing its TTL; ended and unknown ids give undefined. */
|
|
348
|
+
findActive(operationId) {
|
|
349
|
+
return this._active.get(operationId);
|
|
350
|
+
}
|
|
351
|
+
/** Return active or tombstoned binding data without refreshing its TTL. */
|
|
352
|
+
find(operationId) {
|
|
353
|
+
return this._active.get(operationId) ?? this._ended.get(operationId)?.operation;
|
|
354
|
+
}
|
|
355
|
+
findActiveByConnectionKey(connectionKey) {
|
|
356
|
+
if (!connectionKey) return void 0;
|
|
357
|
+
return [...this._active.values()].find(
|
|
358
|
+
(operation) => operation.connectionKeys.includes(connectionKey)
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
/** Map a job queue key (an operation id or a connection key) to its live operation id. */
|
|
362
|
+
findOperationIdByJobKey(jobKey) {
|
|
363
|
+
if (!jobKey) return void 0;
|
|
364
|
+
if (isHardwareOperationId(jobKey)) return jobKey;
|
|
365
|
+
return this.findActiveByConnectionKey(jobKey)?.operationId;
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* End an operation on the caller's explicit release. Throws for an unknown id; returns undefined
|
|
369
|
+
* when it had already ended.
|
|
370
|
+
*/
|
|
371
|
+
release(operationId) {
|
|
372
|
+
if (!this.find(operationId)) {
|
|
373
|
+
this.resolve(operationId);
|
|
374
|
+
return void 0;
|
|
375
|
+
}
|
|
376
|
+
return this.end(operationId, "explicit");
|
|
377
|
+
}
|
|
378
|
+
end(operationId, reason) {
|
|
379
|
+
const active = this._active.get(operationId);
|
|
380
|
+
if (!active) return void 0;
|
|
381
|
+
if (active.timer) clearTimeout(active.timer);
|
|
382
|
+
this._active.delete(operationId);
|
|
383
|
+
this._rememberEnded(active, reason);
|
|
384
|
+
this._onEnded?.(active, reason);
|
|
385
|
+
return active;
|
|
386
|
+
}
|
|
387
|
+
endByConnectionKey(connectionKey, reason, exceptOperationId) {
|
|
388
|
+
if (!connectionKey) return;
|
|
389
|
+
for (const operation of [...this._active.values()]) {
|
|
390
|
+
if (operation.operationId === exceptOperationId) continue;
|
|
391
|
+
if (operation.connectionKeys.includes(connectionKey)) {
|
|
392
|
+
this.end(operation.operationId, reason);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
endAll(reason, exceptOperationId) {
|
|
397
|
+
for (const operationId of [...this._active.keys()]) {
|
|
398
|
+
if (operationId === exceptOperationId) continue;
|
|
399
|
+
this.end(operationId, reason);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
_touch(active) {
|
|
403
|
+
if (active.timer) clearTimeout(active.timer);
|
|
404
|
+
active.timer = active.retainCount === 0 ? this._createTimer(active.operationId) : void 0;
|
|
405
|
+
}
|
|
406
|
+
_createTimer(operationId) {
|
|
407
|
+
const timer = setTimeout(() => {
|
|
408
|
+
this.end(operationId, "timeout");
|
|
409
|
+
}, this._ttlMs);
|
|
410
|
+
timer.unref?.();
|
|
411
|
+
return timer;
|
|
412
|
+
}
|
|
413
|
+
_rememberEnded(operation, reason) {
|
|
414
|
+
this._ended.set(operation.operationId, { reason, operation });
|
|
415
|
+
if (this._ended.size <= 100) return;
|
|
416
|
+
const oldest = this._ended.keys().next().value;
|
|
417
|
+
if (oldest) this._ended.delete(oldest);
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
// src/utils/hardwareOperationTarget.ts
|
|
422
|
+
function resolveHardwareOperationTarget(positionalTargetId, commonOperationId, expectedVendor) {
|
|
423
|
+
const targetId = positionalTargetId ?? void 0;
|
|
424
|
+
const commonId = commonOperationId || void 0;
|
|
425
|
+
const parsedTarget = parseHardwareRuntimeId(targetId);
|
|
426
|
+
const parsedCommon = parseHardwareRuntimeId(commonId);
|
|
427
|
+
const positionalOperationId = parsedTarget?.kind === "operation" ? targetId : void 0;
|
|
428
|
+
if (hasHardwareRuntimeIdPrefix(targetId) && !parsedTarget) {
|
|
429
|
+
return failure(10002 /* InvalidParams */, "Invalid hardware operation target id");
|
|
430
|
+
}
|
|
431
|
+
if (parsedTarget?.kind === "link") {
|
|
432
|
+
return failure(
|
|
433
|
+
10002 /* InvalidParams */,
|
|
434
|
+
"Hardware transport link id cannot be used as an operation target"
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
if (commonId && parsedCommon?.kind !== "operation") {
|
|
438
|
+
return failure(10002 /* InvalidParams */, "Invalid hardware operation id");
|
|
439
|
+
}
|
|
440
|
+
if (expectedVendor && [parsedTarget, parsedCommon].some((parsed) => parsed && parsed.vendor !== expectedVendor)) {
|
|
441
|
+
return failure(
|
|
442
|
+
10002 /* InvalidParams */,
|
|
443
|
+
`Hardware operation does not belong to ${expectedVendor}`
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
if (positionalOperationId && commonId && positionalOperationId !== commonId) {
|
|
447
|
+
return failure(10002 /* InvalidParams */, "Conflicting hardware operation ids", {
|
|
448
|
+
positionalOperationId,
|
|
449
|
+
commonOperationId: commonId
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
const operationId = commonId ?? positionalOperationId;
|
|
453
|
+
return success({ operationId, targetId: operationId ?? targetId });
|
|
454
|
+
}
|
|
455
|
+
function pickConnectionContext(params) {
|
|
456
|
+
return {
|
|
457
|
+
knownConnections: params.knownConnections,
|
|
458
|
+
extra: params.extra,
|
|
459
|
+
allowDeviceSelection: typeof params.allowDeviceSelection === "boolean" ? params.allowDeviceSelection : void 0,
|
|
460
|
+
supportedTransports: params.supportedTransports
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
|
|
178
464
|
// src/types/connector.ts
|
|
179
465
|
var EConnectorInteraction = /* @__PURE__ */ ((EConnectorInteraction2) => {
|
|
180
466
|
EConnectorInteraction2["Searching"] = "searching";
|
|
@@ -190,7 +476,7 @@ function createBridgedConnector(vendor, connectionType, bridge) {
|
|
|
190
476
|
return {
|
|
191
477
|
connectionType,
|
|
192
478
|
searchDevices: (options) => bridge.searchDevices({ vendor, options }),
|
|
193
|
-
connect: (deviceId) => bridge.connect({ vendor, deviceId }),
|
|
479
|
+
connect: (deviceId, options) => bridge.connect({ vendor, deviceId, ...options ? { options } : {} }),
|
|
194
480
|
disconnect: (sessionId) => bridge.disconnect({ vendor, sessionId }),
|
|
195
481
|
call: (sessionId, method, callParams) => bridge.call({ vendor, sessionId, method, callParams }),
|
|
196
482
|
cancel: (sessionId) => bridge.cancel({ vendor, sessionId }),
|
|
@@ -279,6 +565,15 @@ function rehydrateConnectorError(error) {
|
|
|
279
565
|
...params ?? {}
|
|
280
566
|
});
|
|
281
567
|
}
|
|
568
|
+
function withChildConnectionType(event, data, connectionType) {
|
|
569
|
+
if (event !== "device-connect" && event !== "features") return data;
|
|
570
|
+
const { device } = data;
|
|
571
|
+
if (device.connectionType) return data;
|
|
572
|
+
return {
|
|
573
|
+
...data,
|
|
574
|
+
device: { ...device, connectionType }
|
|
575
|
+
};
|
|
576
|
+
}
|
|
282
577
|
function isDeviceUnavailableError(error) {
|
|
283
578
|
const typed = error;
|
|
284
579
|
if (typed.code === 10100 /* DeviceNotFound */ || typed.code === 10101 /* DeviceDisconnected */) {
|
|
@@ -327,14 +622,21 @@ function createCombinedConnector(connectors) {
|
|
|
327
622
|
};
|
|
328
623
|
for (const child of connectors) {
|
|
329
624
|
child.on("device-trezor-thp-credentials-changed", (data) => {
|
|
330
|
-
|
|
625
|
+
const removed = new Set((data.removed ?? []).map(credentialKey));
|
|
626
|
+
const kept = sharedCredentials.filter((cred) => !removed.has(credentialKey(cred)));
|
|
627
|
+
const dropped = kept.length !== sharedCredentials.length;
|
|
628
|
+
sharedCredentials.splice(0, sharedCredentials.length, ...kept);
|
|
629
|
+
if (mergeShared(data.credentials) || dropped) void broadcastCredentials();
|
|
331
630
|
});
|
|
332
631
|
}
|
|
632
|
+
const onTransport = (child, transportType) => !transportType || child.connectionType === transportType;
|
|
333
633
|
const searchDevices = async (options = {}) => {
|
|
634
|
+
const selectedConnectors = connectors.map((child, index) => ({ child, index })).filter(({ child }) => onTransport(child, options.transportType));
|
|
635
|
+
if (!selectedConnectors.length) return [];
|
|
334
636
|
const perConnector = [];
|
|
335
|
-
await new Promise((resolve) => {
|
|
637
|
+
await new Promise((resolve, reject) => {
|
|
336
638
|
let finished = false;
|
|
337
|
-
let remaining =
|
|
639
|
+
let remaining = selectedConnectors.length;
|
|
338
640
|
let settleTimer;
|
|
339
641
|
const finish = () => {
|
|
340
642
|
if (finished) return;
|
|
@@ -342,19 +644,20 @@ function createCombinedConnector(connectors) {
|
|
|
342
644
|
if (settleTimer) clearTimeout(settleTimer);
|
|
343
645
|
resolve();
|
|
344
646
|
};
|
|
345
|
-
|
|
346
|
-
void child.searchDevices().then(
|
|
647
|
+
selectedConnectors.forEach(({ child, index }) => {
|
|
648
|
+
void child.searchDevices(options.purpose ? { purpose: options.purpose } : void 0).then(
|
|
347
649
|
(devices) => devices.map((device) => ({
|
|
348
650
|
...device,
|
|
349
651
|
connectionType: device.connectionType ?? child.connectionType
|
|
350
652
|
}))
|
|
351
|
-
).catch(
|
|
352
|
-
()
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
653
|
+
).catch((error) => {
|
|
654
|
+
if (options.transportType) {
|
|
655
|
+
finished = true;
|
|
656
|
+
if (settleTimer) clearTimeout(settleTimer);
|
|
657
|
+
reject(error);
|
|
658
|
+
}
|
|
659
|
+
return [];
|
|
660
|
+
}).then((devices) => {
|
|
358
661
|
remaining -= 1;
|
|
359
662
|
if (!finished) {
|
|
360
663
|
perConnector.push({ index, devices });
|
|
@@ -371,7 +674,9 @@ function createCombinedConnector(connectors) {
|
|
|
371
674
|
});
|
|
372
675
|
});
|
|
373
676
|
perConnector.sort((a, b) => a.index - b.index);
|
|
374
|
-
deviceOwner
|
|
677
|
+
for (const [id, owner] of deviceOwner) {
|
|
678
|
+
if (onTransport(owner, options.transportType)) deviceOwner.delete(id);
|
|
679
|
+
}
|
|
375
680
|
const merged = [];
|
|
376
681
|
perConnector.forEach(({ index, devices }) => {
|
|
377
682
|
for (const device of devices) {
|
|
@@ -381,11 +686,12 @@ function createCombinedConnector(connectors) {
|
|
|
381
686
|
});
|
|
382
687
|
return merged.sort((a, b) => rank(a) - rank(b));
|
|
383
688
|
};
|
|
384
|
-
const resolveOwner = async (deviceId) => {
|
|
385
|
-
|
|
386
|
-
|
|
689
|
+
const resolveOwner = async (deviceId, transportType) => {
|
|
690
|
+
const cachedOwner = deviceId ? deviceOwner.get(deviceId) : void 0;
|
|
691
|
+
if (cachedOwner && onTransport(cachedOwner, transportType)) {
|
|
692
|
+
return { owner: cachedOwner, deviceId };
|
|
387
693
|
}
|
|
388
|
-
const devices = await searchDevices({ waitForAll: Boolean(deviceId) });
|
|
694
|
+
const devices = await searchDevices({ waitForAll: Boolean(deviceId), transportType });
|
|
389
695
|
if (deviceId) {
|
|
390
696
|
const owner = deviceOwner.get(deviceId);
|
|
391
697
|
if (!owner)
|
|
@@ -396,13 +702,14 @@ function createCombinedConnector(connectors) {
|
|
|
396
702
|
if (!first) throw combinedDeviceNotFoundError("Combined connector: no devices found");
|
|
397
703
|
return { owner: deviceOwner.get(first.connectId), deviceId: first.connectId };
|
|
398
704
|
};
|
|
399
|
-
const connectByExplicitId = async (deviceId) => {
|
|
705
|
+
const connectByExplicitId = async (deviceId, transportType) => {
|
|
400
706
|
const cachedOwner = deviceOwner.get(deviceId);
|
|
401
|
-
if (cachedOwner) {
|
|
707
|
+
if (cachedOwner && onTransport(cachedOwner, transportType)) {
|
|
402
708
|
return { session: await cachedOwner.connect(deviceId), owner: cachedOwner };
|
|
403
709
|
}
|
|
404
710
|
let lastError;
|
|
405
711
|
for (const child of connectors) {
|
|
712
|
+
if (!onTransport(child, transportType)) continue;
|
|
406
713
|
try {
|
|
407
714
|
const session = await child.connect(deviceId);
|
|
408
715
|
deviceOwner.set(deviceId, child);
|
|
@@ -424,14 +731,15 @@ function createCombinedConnector(connectors) {
|
|
|
424
731
|
// Nominal value only — the per-device `connectionType` is authoritative
|
|
425
732
|
// for a fused connector.
|
|
426
733
|
connectionType: connectors[0].connectionType,
|
|
427
|
-
|
|
428
|
-
|
|
734
|
+
availableTransports: [...new Set(connectors.map((child) => child.connectionType))],
|
|
735
|
+
searchDevices: (options) => searchDevices(options),
|
|
736
|
+
connect: async (deviceId, options) => {
|
|
429
737
|
let owner;
|
|
430
738
|
let session;
|
|
431
739
|
if (deviceId) {
|
|
432
|
-
({ session, owner } = await connectByExplicitId(deviceId));
|
|
740
|
+
({ session, owner } = await connectByExplicitId(deviceId, options?.transportType));
|
|
433
741
|
} else {
|
|
434
|
-
const resolved = await resolveOwner();
|
|
742
|
+
const resolved = await resolveOwner(void 0, options?.transportType);
|
|
435
743
|
owner = resolved.owner;
|
|
436
744
|
session = await owner.connect(resolved.deviceId);
|
|
437
745
|
}
|
|
@@ -453,15 +761,18 @@ function createCombinedConnector(connectors) {
|
|
|
453
761
|
},
|
|
454
762
|
cancel: async (sessionId) => {
|
|
455
763
|
const owner = sessionOwner.get(sessionId);
|
|
456
|
-
if (
|
|
457
|
-
|
|
764
|
+
if (owner) {
|
|
765
|
+
await owner.cancel(sessionId);
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
await Promise.all(connectors.map((child) => child.cancel(sessionId)));
|
|
458
769
|
},
|
|
459
770
|
uiResponse: (response) => {
|
|
460
771
|
for (const child of connectors) child.uiResponse(response);
|
|
461
772
|
},
|
|
462
773
|
on: (event, handler) => {
|
|
463
774
|
const attached = connectors.map((child) => {
|
|
464
|
-
const forward = (data) => handler(data);
|
|
775
|
+
const forward = (data) => handler(withChildConnectionType(event, data, child.connectionType));
|
|
465
776
|
child.on(event, forward);
|
|
466
777
|
return { child, handler: forward };
|
|
467
778
|
});
|
|
@@ -537,6 +848,144 @@ var TypedEventEmitter = class {
|
|
|
537
848
|
}
|
|
538
849
|
};
|
|
539
850
|
|
|
851
|
+
// src/utils/requestSaveDeviceBinding.ts
|
|
852
|
+
function emitDeviceBindingStatus(emitter, selectionRequestId, status) {
|
|
853
|
+
emitter.emit(UI_REQUEST.DEVICE_BINDING_STATUS, {
|
|
854
|
+
type: UI_REQUEST.DEVICE_BINDING_STATUS,
|
|
855
|
+
payload: { selectionRequestId, status }
|
|
856
|
+
});
|
|
857
|
+
}
|
|
858
|
+
async function requestSaveDeviceBinding(emitter, registry, binding, signal) {
|
|
859
|
+
const type = UI_REQUEST.REQUEST_SAVE_DEVICE_BINDING;
|
|
860
|
+
const emitStatus = (status) => emitDeviceBindingStatus(emitter, binding.selectionRequestId, status);
|
|
861
|
+
const declined = (reason) => {
|
|
862
|
+
emitStatus("failed");
|
|
863
|
+
return { saved: false, reason };
|
|
864
|
+
};
|
|
865
|
+
if (!emitter.listenerCount(type)) {
|
|
866
|
+
return declined("skipped");
|
|
867
|
+
}
|
|
868
|
+
if (signal?.aborted) throw signal.reason;
|
|
869
|
+
const requestId = registry.createRequestId();
|
|
870
|
+
const pending = registry.wait(type, { requestId, operationId: binding.operationId });
|
|
871
|
+
void pending.catch(() => void 0);
|
|
872
|
+
const cancel = () => registry.cancel(type, requestId);
|
|
873
|
+
signal?.addEventListener("abort", cancel, { once: true });
|
|
874
|
+
try {
|
|
875
|
+
emitter.emit(type, { type, payload: { ...binding, requestId } });
|
|
876
|
+
const response = await pending;
|
|
877
|
+
if (signal?.aborted) throw signal.reason;
|
|
878
|
+
if (response?.saved !== true) {
|
|
879
|
+
return declined(response?.reason ?? "skipped");
|
|
880
|
+
}
|
|
881
|
+
emitStatus("saved");
|
|
882
|
+
return { saved: true };
|
|
883
|
+
} catch (error) {
|
|
884
|
+
if (signal?.aborted) {
|
|
885
|
+
emitStatus("cancelled");
|
|
886
|
+
throw error;
|
|
887
|
+
}
|
|
888
|
+
return declined("skipped");
|
|
889
|
+
} finally {
|
|
890
|
+
signal?.removeEventListener("abort", cancel);
|
|
891
|
+
registry.cancel(type, requestId);
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
// src/utils/requestBleDeviceSelection.ts
|
|
896
|
+
async function requestBleDeviceSelection({
|
|
897
|
+
emitter,
|
|
898
|
+
registry,
|
|
899
|
+
request,
|
|
900
|
+
scan,
|
|
901
|
+
signal,
|
|
902
|
+
allowUsbFallback = false,
|
|
903
|
+
pollIntervalMs = 1500
|
|
904
|
+
}) {
|
|
905
|
+
const type = UI_REQUEST.REQUEST_SELECT_DEVICE;
|
|
906
|
+
if (signal.aborted) throw signal.reason;
|
|
907
|
+
if (!emitter.listenerCount(type)) {
|
|
908
|
+
throw createHwkError({
|
|
909
|
+
code: 10100 /* DeviceNotFound */,
|
|
910
|
+
message: "Select a Bluetooth device before continuing"
|
|
911
|
+
});
|
|
912
|
+
}
|
|
913
|
+
const requestId = registry.createRequestId();
|
|
914
|
+
const { operationId } = request;
|
|
915
|
+
let devices = allowUsbFallback ? request.devices.filter((device) => device.connectionType === "ble") : request.devices;
|
|
916
|
+
const publishedDevices = /* @__PURE__ */ new Map();
|
|
917
|
+
let stopped = false;
|
|
918
|
+
let timer;
|
|
919
|
+
let wake;
|
|
920
|
+
const stop = () => {
|
|
921
|
+
stopped = true;
|
|
922
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
923
|
+
wake?.();
|
|
924
|
+
};
|
|
925
|
+
const waitForNextScan = () => new Promise((resolve) => {
|
|
926
|
+
wake = resolve;
|
|
927
|
+
timer = setTimeout(resolve, pollIntervalMs);
|
|
928
|
+
if (stopped) resolve();
|
|
929
|
+
});
|
|
930
|
+
const cancel = () => registry.cancel(type, requestId);
|
|
931
|
+
const reply = registry.wait(type, { requestId, operationId }).finally(stop);
|
|
932
|
+
void reply.catch(() => void 0);
|
|
933
|
+
const publish = () => {
|
|
934
|
+
for (const device of devices) publishedDevices.set(device.connectId, device);
|
|
935
|
+
emitter.emit(type, {
|
|
936
|
+
type,
|
|
937
|
+
payload: { ...request, devices, requestId, scanning: true }
|
|
938
|
+
});
|
|
939
|
+
};
|
|
940
|
+
signal.addEventListener("abort", cancel, { once: true });
|
|
941
|
+
const rescan = () => scan().catch((error) => {
|
|
942
|
+
if (stopped || signal.aborted || devices.length) return void 0;
|
|
943
|
+
throw error;
|
|
944
|
+
});
|
|
945
|
+
let polling;
|
|
946
|
+
try {
|
|
947
|
+
publish();
|
|
948
|
+
polling = (async () => {
|
|
949
|
+
await Promise.resolve();
|
|
950
|
+
while (!stopped) {
|
|
951
|
+
const snapshot = await rescan();
|
|
952
|
+
if (stopped || signal.aborted) return;
|
|
953
|
+
const usbFallback = allowUsbFallback ? snapshot?.find((device2) => device2.connectionType === "usb") : void 0;
|
|
954
|
+
if (usbFallback) return usbFallback;
|
|
955
|
+
if (snapshot) {
|
|
956
|
+
devices = snapshot;
|
|
957
|
+
publish();
|
|
958
|
+
}
|
|
959
|
+
await waitForNextScan();
|
|
960
|
+
}
|
|
961
|
+
})();
|
|
962
|
+
const userSelection = reply.then((selected) => publishedDevices.get(selected.sdkConnectId));
|
|
963
|
+
const device = await Promise.race([
|
|
964
|
+
userSelection,
|
|
965
|
+
polling.then((usbFallback) => usbFallback ?? userSelection)
|
|
966
|
+
]);
|
|
967
|
+
stop();
|
|
968
|
+
await polling;
|
|
969
|
+
if (signal.aborted) throw signal.reason;
|
|
970
|
+
if (!device) {
|
|
971
|
+
throw createHwkError({
|
|
972
|
+
code: 10100 /* DeviceNotFound */,
|
|
973
|
+
message: "Selected Bluetooth device is no longer available"
|
|
974
|
+
});
|
|
975
|
+
}
|
|
976
|
+
emitDeviceBindingStatus(emitter, requestId, "verifying");
|
|
977
|
+
return { device, requestId };
|
|
978
|
+
} catch (error) {
|
|
979
|
+
emitDeviceBindingStatus(emitter, requestId, signal.aborted ? "cancelled" : "failed");
|
|
980
|
+
throw error;
|
|
981
|
+
} finally {
|
|
982
|
+
stop();
|
|
983
|
+
signal.removeEventListener("abort", cancel);
|
|
984
|
+
registry.cancel(type, requestId);
|
|
985
|
+
await polling?.catch(() => void 0);
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
|
|
540
989
|
// src/utils/semver.ts
|
|
541
990
|
function compareSemver(a, b) {
|
|
542
991
|
const pa = a.split(".").map(Number);
|
|
@@ -564,14 +1013,480 @@ function padHex64(hex) {
|
|
|
564
1013
|
function hexToBytes(hex) {
|
|
565
1014
|
return nobleHexToBytes(stripHex(hex));
|
|
566
1015
|
}
|
|
567
|
-
function
|
|
1016
|
+
function bytesToHex3(bytes) {
|
|
568
1017
|
return nobleBytesToHex(bytes);
|
|
569
1018
|
}
|
|
570
1019
|
|
|
1020
|
+
// src/utils/boundedCbor.ts
|
|
1021
|
+
var MAX_CBOR_DEPTH = 64;
|
|
1022
|
+
var DEFAULT_MAX_CBOR_BYTES = 8 * 1024 * 1024;
|
|
1023
|
+
var ARGUMENT_BYTES = [1, 2, 4, 8];
|
|
1024
|
+
function malformed(reason) {
|
|
1025
|
+
return createHwkError({
|
|
1026
|
+
code: 10002 /* InvalidParams */,
|
|
1027
|
+
message: `Malformed CBOR payload: ${reason}`
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
function assertBoundedCbor(input, maxBytes = DEFAULT_MAX_CBOR_BYTES) {
|
|
1031
|
+
if (input.length === 0) throw malformed("empty");
|
|
1032
|
+
if (input.length > maxBytes) throw malformed(`larger than ${maxBytes} bytes`);
|
|
1033
|
+
let offset = 0;
|
|
1034
|
+
const readArgument = (info) => {
|
|
1035
|
+
if (info < 24) return info;
|
|
1036
|
+
const size = ARGUMENT_BYTES[info - 24];
|
|
1037
|
+
if (offset + size > input.length) throw malformed("truncated length");
|
|
1038
|
+
let value = 0;
|
|
1039
|
+
for (let i = 0; i < size; i += 1) value = value * 256 + input[offset + i];
|
|
1040
|
+
offset += size;
|
|
1041
|
+
return value;
|
|
1042
|
+
};
|
|
1043
|
+
const walkItem = (depth) => {
|
|
1044
|
+
if (depth > MAX_CBOR_DEPTH) throw malformed("nested too deeply");
|
|
1045
|
+
if (offset >= input.length) throw malformed("truncated item");
|
|
1046
|
+
const initial = input[offset];
|
|
1047
|
+
offset += 1;
|
|
1048
|
+
const major = Math.floor(initial / 32);
|
|
1049
|
+
const info = initial % 32;
|
|
1050
|
+
if (info >= 28) throw malformed("indefinite or reserved length");
|
|
1051
|
+
const argument = readArgument(info);
|
|
1052
|
+
const remaining = input.length - offset;
|
|
1053
|
+
if (major === 2 || major === 3) {
|
|
1054
|
+
if (argument > remaining) throw malformed("string longer than the payload");
|
|
1055
|
+
offset += argument;
|
|
1056
|
+
} else if (major === 4 || major === 5) {
|
|
1057
|
+
const items = major === 5 ? argument * 2 : argument;
|
|
1058
|
+
if (items > remaining) throw malformed("container longer than the payload");
|
|
1059
|
+
for (let i = 0; i < items; i += 1) walkItem(depth + 1);
|
|
1060
|
+
} else if (major === 6) {
|
|
1061
|
+
walkItem(depth + 1);
|
|
1062
|
+
}
|
|
1063
|
+
};
|
|
1064
|
+
walkItem(0);
|
|
1065
|
+
if (offset !== input.length) throw malformed("trailing bytes");
|
|
1066
|
+
}
|
|
1067
|
+
function boundedCborFromHex(hex, maxBytes) {
|
|
1068
|
+
if (!/^(?:[0-9a-f]{2})+$/i.test(hex)) throw malformed("not an even-length hex string");
|
|
1069
|
+
const bytes = Buffer.from(hex, "hex");
|
|
1070
|
+
assertBoundedCbor(bytes, maxBytes);
|
|
1071
|
+
return bytes;
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
// src/utils/solanaOffchainMessage.ts
|
|
1075
|
+
var SOLANA_OFFCHAIN_SIGNING_DOMAIN = Uint8Array.of(
|
|
1076
|
+
255,
|
|
1077
|
+
...Array.from("solana offchain", (character) => character.charCodeAt(0))
|
|
1078
|
+
);
|
|
1079
|
+
var SOLANA_PUBLIC_KEY_LENGTH = 32;
|
|
1080
|
+
var MAX_SOLANA_OFFCHAIN_SIGNERS = 255;
|
|
1081
|
+
function compareBytes(a, b) {
|
|
1082
|
+
for (let index = 0; index < a.length; index += 1) {
|
|
1083
|
+
const difference = (a[index] ?? 0) - (b[index] ?? 0);
|
|
1084
|
+
if (difference !== 0) return difference;
|
|
1085
|
+
}
|
|
1086
|
+
return a.length - b.length;
|
|
1087
|
+
}
|
|
1088
|
+
function bytesEqual(a, b) {
|
|
1089
|
+
return a.length === b.length && a.every((value, index) => value === b[index]);
|
|
1090
|
+
}
|
|
1091
|
+
function decodeUtf8Strict(bytes) {
|
|
1092
|
+
return decodeURIComponent(
|
|
1093
|
+
Array.from(bytes, (value) => `%${value.toString(16).padStart(2, "0")}`).join("")
|
|
1094
|
+
);
|
|
1095
|
+
}
|
|
1096
|
+
function prepareSolanaOffchainMessageV1({
|
|
1097
|
+
message,
|
|
1098
|
+
requiredSigners
|
|
1099
|
+
}) {
|
|
1100
|
+
if (message.length === 0) {
|
|
1101
|
+
throw new Error("Solana off-chain message cannot be empty");
|
|
1102
|
+
}
|
|
1103
|
+
if (requiredSigners.length === 0 || requiredSigners.length > MAX_SOLANA_OFFCHAIN_SIGNERS) {
|
|
1104
|
+
throw new Error("Solana off-chain message requires between 1 and 255 signers");
|
|
1105
|
+
}
|
|
1106
|
+
let messageText;
|
|
1107
|
+
try {
|
|
1108
|
+
messageText = decodeUtf8Strict(message);
|
|
1109
|
+
} catch {
|
|
1110
|
+
throw new Error("Solana off-chain message must contain valid UTF-8");
|
|
1111
|
+
}
|
|
1112
|
+
const signerBytes = requiredSigners.map((signer, index) => {
|
|
1113
|
+
let bytes;
|
|
1114
|
+
try {
|
|
1115
|
+
bytes = hexToBytes(signer);
|
|
1116
|
+
} catch {
|
|
1117
|
+
throw new Error(`Solana off-chain signer ${index} must be a hex public key`);
|
|
1118
|
+
}
|
|
1119
|
+
if (bytes.length !== SOLANA_PUBLIC_KEY_LENGTH) {
|
|
1120
|
+
throw new Error(`Solana off-chain signer ${index} must be 32 bytes`);
|
|
1121
|
+
}
|
|
1122
|
+
return bytes;
|
|
1123
|
+
});
|
|
1124
|
+
signerBytes.sort(compareBytes);
|
|
1125
|
+
if (signerBytes.some((bytes, index) => index > 0 && bytesEqual(signerBytes[index - 1], bytes))) {
|
|
1126
|
+
throw new Error("Solana off-chain signers must be unique");
|
|
1127
|
+
}
|
|
1128
|
+
const signersOffset = SOLANA_OFFCHAIN_SIGNING_DOMAIN.length + 2;
|
|
1129
|
+
const messageOffset = signersOffset + signerBytes.length * SOLANA_PUBLIC_KEY_LENGTH;
|
|
1130
|
+
const serializedMessage = new Uint8Array(messageOffset + message.length);
|
|
1131
|
+
serializedMessage.set(SOLANA_OFFCHAIN_SIGNING_DOMAIN);
|
|
1132
|
+
serializedMessage.set([1, signerBytes.length], SOLANA_OFFCHAIN_SIGNING_DOMAIN.length);
|
|
1133
|
+
signerBytes.forEach(
|
|
1134
|
+
(signer, index) => serializedMessage.set(signer, signersOffset + index * SOLANA_PUBLIC_KEY_LENGTH)
|
|
1135
|
+
);
|
|
1136
|
+
serializedMessage.set(message, messageOffset);
|
|
1137
|
+
return {
|
|
1138
|
+
messageText,
|
|
1139
|
+
requiredSigners: signerBytes.map(bytesToHex3),
|
|
1140
|
+
serializedMessage
|
|
1141
|
+
};
|
|
1142
|
+
}
|
|
1143
|
+
|
|
1144
|
+
// src/utils/logSummary.ts
|
|
1145
|
+
import { sha256 as sha2562 } from "@noble/hashes/sha256";
|
|
1146
|
+
import { bytesToHex as bytesToHex4, utf8ToBytes as utf8ToBytes2 } from "@noble/hashes/utils";
|
|
1147
|
+
var MAX_DEPTH = 6;
|
|
1148
|
+
var MAX_ARRAY_ITEMS = 20;
|
|
1149
|
+
var MAX_PLAIN_STRING = 64;
|
|
1150
|
+
var MAX_ERROR_STRING = 500;
|
|
1151
|
+
var normalizeKey = (key) => key.replace(/[_-]/g, "").toLowerCase();
|
|
1152
|
+
var keySet = (keys) => new Set(keys.map(normalizeKey));
|
|
1153
|
+
var KEEP_KEYS = keySet([
|
|
1154
|
+
"path",
|
|
1155
|
+
"paths",
|
|
1156
|
+
"derivationPath",
|
|
1157
|
+
"fullPath",
|
|
1158
|
+
"basePath",
|
|
1159
|
+
"method",
|
|
1160
|
+
"methodName",
|
|
1161
|
+
"chainId",
|
|
1162
|
+
"coin",
|
|
1163
|
+
"network",
|
|
1164
|
+
"networkName",
|
|
1165
|
+
"scriptType",
|
|
1166
|
+
"template",
|
|
1167
|
+
"appName",
|
|
1168
|
+
"_tag",
|
|
1169
|
+
"code",
|
|
1170
|
+
"errorCode",
|
|
1171
|
+
"statusCode"
|
|
1172
|
+
]);
|
|
1173
|
+
var ERROR_TEXT_KEYS = keySet(["message", "error", "errorMessage", "originalMessage"]);
|
|
1174
|
+
var ERROR_CONTEXT_KEYS = keySet(["error", "err", "originalError", "cause"]);
|
|
1175
|
+
var SECRET_KEYS = keySet([
|
|
1176
|
+
"ufvk",
|
|
1177
|
+
"orchardFvk",
|
|
1178
|
+
"fvk",
|
|
1179
|
+
"viewingKey",
|
|
1180
|
+
"passphraseState",
|
|
1181
|
+
"passphrase",
|
|
1182
|
+
"password",
|
|
1183
|
+
"pin",
|
|
1184
|
+
"mnemonic",
|
|
1185
|
+
"mnemonics",
|
|
1186
|
+
"word",
|
|
1187
|
+
"words",
|
|
1188
|
+
"seed",
|
|
1189
|
+
"entropy",
|
|
1190
|
+
"privateKey",
|
|
1191
|
+
"xprv",
|
|
1192
|
+
"mac",
|
|
1193
|
+
// THP pairing and channel material.
|
|
1194
|
+
"credential",
|
|
1195
|
+
"credentials",
|
|
1196
|
+
"hostStaticKey",
|
|
1197
|
+
"trezorStaticPublicKey",
|
|
1198
|
+
// Wallet sessions and host connection records.
|
|
1199
|
+
"session",
|
|
1200
|
+
"sessionId",
|
|
1201
|
+
"knownConnections",
|
|
1202
|
+
"extra"
|
|
1203
|
+
]);
|
|
1204
|
+
var KEY_MATERIAL_KEYS = keySet([
|
|
1205
|
+
"xpub",
|
|
1206
|
+
"ypub",
|
|
1207
|
+
"zpub",
|
|
1208
|
+
"tpub",
|
|
1209
|
+
"extendedPublicKey",
|
|
1210
|
+
"publicKey",
|
|
1211
|
+
"pubkey",
|
|
1212
|
+
"pub",
|
|
1213
|
+
"chainCode",
|
|
1214
|
+
"tapInternalKey"
|
|
1215
|
+
]);
|
|
1216
|
+
var SIGNATURE_KEYS = keySet(["signature", "signatures", "sig", "r", "s", "v", "partialSig"]);
|
|
1217
|
+
var SIGNED_TX_KEYS = keySet(["signedTx", "signedTransaction", "signedTxHex"]);
|
|
1218
|
+
var SIGNED_PSBT_KEYS = keySet(["signedPsbt"]);
|
|
1219
|
+
var PAYLOAD_KEYS = keySet([
|
|
1220
|
+
"serializedTx",
|
|
1221
|
+
"rawTx",
|
|
1222
|
+
"rawTxHex",
|
|
1223
|
+
"txHex",
|
|
1224
|
+
"tx",
|
|
1225
|
+
"transaction",
|
|
1226
|
+
"psbt",
|
|
1227
|
+
"message",
|
|
1228
|
+
"messageHex",
|
|
1229
|
+
"data",
|
|
1230
|
+
"dataJson",
|
|
1231
|
+
"typedData",
|
|
1232
|
+
"hex",
|
|
1233
|
+
"bytes",
|
|
1234
|
+
"apdu",
|
|
1235
|
+
"domainSeparatorHash",
|
|
1236
|
+
"messageHash",
|
|
1237
|
+
"encodedNetwork",
|
|
1238
|
+
"encodedToken",
|
|
1239
|
+
"refBlockBytes",
|
|
1240
|
+
"refBlockHash",
|
|
1241
|
+
"opReturnData",
|
|
1242
|
+
"scriptSig",
|
|
1243
|
+
"witness",
|
|
1244
|
+
"extraData",
|
|
1245
|
+
// Structured transaction contents (Trezor-style and Tron params).
|
|
1246
|
+
"contract",
|
|
1247
|
+
"inputs",
|
|
1248
|
+
"outputs",
|
|
1249
|
+
"refTxs",
|
|
1250
|
+
"paymentRequest",
|
|
1251
|
+
"paymentRequests",
|
|
1252
|
+
"additionalInfo",
|
|
1253
|
+
"accessList",
|
|
1254
|
+
"memos"
|
|
1255
|
+
]);
|
|
1256
|
+
var ADDRESS_KEYS = keySet([
|
|
1257
|
+
"to",
|
|
1258
|
+
"from",
|
|
1259
|
+
"recipient",
|
|
1260
|
+
"verifyingContract",
|
|
1261
|
+
"requiredSigners",
|
|
1262
|
+
"tokenMint",
|
|
1263
|
+
"tokenAccount",
|
|
1264
|
+
"tokenProgram"
|
|
1265
|
+
]);
|
|
1266
|
+
var TX_IN_RESULT_KEYS = keySet(["serializedTx", "rawTx", "rawTxHex", "txHex", "tx"]);
|
|
1267
|
+
var AMOUNT_KEYS = keySet(["value", "amount", "amounts", "fee", "fees", "tokenAmount"]);
|
|
1268
|
+
var ERROR_FIELDS = ["_tag", "code", "errorCode", "statusCode", "originalError"];
|
|
1269
|
+
var XPUB_RE = /^[a-zA-Z]pub[1-9A-HJ-NP-Za-km-z]{90,}$/;
|
|
1270
|
+
var HEX_RE = /^(0x)?[0-9a-fA-F]*$/;
|
|
1271
|
+
var EVM_TX_TYPES = {
|
|
1272
|
+
1: "eip2930",
|
|
1273
|
+
2: "eip1559",
|
|
1274
|
+
3: "eip4844",
|
|
1275
|
+
4: "eip7702"
|
|
1276
|
+
};
|
|
1277
|
+
function sha256Short(input) {
|
|
1278
|
+
return bytesToHex4(sha2562(typeof input === "string" ? utf8ToBytes2(input) : input)).slice(0, 16);
|
|
1279
|
+
}
|
|
1280
|
+
function toBytes(value) {
|
|
1281
|
+
if (value instanceof Uint8Array) return value;
|
|
1282
|
+
if (value instanceof ArrayBuffer) return new Uint8Array(value);
|
|
1283
|
+
if (ArrayBuffer.isView(value)) {
|
|
1284
|
+
return new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
1285
|
+
}
|
|
1286
|
+
if (value && typeof value === "object" && value.type === "Buffer" && Array.isArray(value.data)) {
|
|
1287
|
+
return Uint8Array.from(value.data);
|
|
1288
|
+
}
|
|
1289
|
+
return void 0;
|
|
1290
|
+
}
|
|
1291
|
+
function isPlainHex(value) {
|
|
1292
|
+
return value.length > 0 && HEX_RE.test(value) && value !== "0x";
|
|
1293
|
+
}
|
|
1294
|
+
function stripHexPrefix(value) {
|
|
1295
|
+
return value.startsWith("0x") || value.startsWith("0X") ? value.slice(2) : value;
|
|
1296
|
+
}
|
|
1297
|
+
function maskAddress(value) {
|
|
1298
|
+
if (value.length <= 12) return `<addr len=${value.length}>`;
|
|
1299
|
+
return `${value.slice(0, 6)}\u2026${value.slice(-4)}`;
|
|
1300
|
+
}
|
|
1301
|
+
function summarizeKeyMaterial(value) {
|
|
1302
|
+
const bytes = toBytes(value);
|
|
1303
|
+
if (bytes) return `\u2026 len=${bytes.length}`;
|
|
1304
|
+
const text = String(value);
|
|
1305
|
+
return XPUB_RE.test(text) ? `${text.slice(0, 4)}\u2026 len=${text.length}` : `\u2026 len=${text.length}`;
|
|
1306
|
+
}
|
|
1307
|
+
function lengthOf(value) {
|
|
1308
|
+
const bytes = toBytes(value);
|
|
1309
|
+
if (bytes) return bytes.length;
|
|
1310
|
+
if (typeof value === "string") return value.length;
|
|
1311
|
+
const json = safeJson(value);
|
|
1312
|
+
return json === void 0 ? 0 : json.length;
|
|
1313
|
+
}
|
|
1314
|
+
function safeJson(value) {
|
|
1315
|
+
try {
|
|
1316
|
+
return JSON.stringify(value, (_k, v) => typeof v === "bigint" ? `${v}n` : v);
|
|
1317
|
+
} catch {
|
|
1318
|
+
return void 0;
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
function evmTxType(hex) {
|
|
1322
|
+
const body = stripHexPrefix(hex);
|
|
1323
|
+
if (body.length < 2) return void 0;
|
|
1324
|
+
const first = parseInt(body.slice(0, 2), 16);
|
|
1325
|
+
if (first >= 192) return "legacy";
|
|
1326
|
+
return EVM_TX_TYPES[first];
|
|
1327
|
+
}
|
|
1328
|
+
function describe(key, value, ctx) {
|
|
1329
|
+
if (!ctx.describePayload) return void 0;
|
|
1330
|
+
try {
|
|
1331
|
+
const hint = ctx.describePayload(key, value, { method: ctx.method, role: ctx.role });
|
|
1332
|
+
return typeof hint === "string" && hint.length > 0 ? hint : void 0;
|
|
1333
|
+
} catch {
|
|
1334
|
+
return void 0;
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
function jsonSummary(json, parsed) {
|
|
1338
|
+
const primaryType = parsed && typeof parsed === "object" ? parsed.primaryType : void 0;
|
|
1339
|
+
const hint = typeof primaryType === "string" && primaryType.length <= MAX_PLAIN_STRING ? ` primaryType=${primaryType}` : "";
|
|
1340
|
+
return `<json len=${json.length} sha256=${sha256Short(json)}${hint}>`;
|
|
1341
|
+
}
|
|
1342
|
+
function summarizePayload(key, value, ctx) {
|
|
1343
|
+
if (value === null || value === void 0 || typeof value === "boolean") return value;
|
|
1344
|
+
if (typeof value === "number") return value;
|
|
1345
|
+
const bytes = toBytes(value);
|
|
1346
|
+
if (bytes) {
|
|
1347
|
+
const hint = describe(key, bytes, ctx);
|
|
1348
|
+
return `<hex len=${bytes.length} sha256=${sha256Short(bytes)}${hint ? ` ${hint}` : ""}>`;
|
|
1349
|
+
}
|
|
1350
|
+
if (typeof value === "string") {
|
|
1351
|
+
const trimmed = value.trim();
|
|
1352
|
+
if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
|
|
1353
|
+
try {
|
|
1354
|
+
return jsonSummary(value, JSON.parse(value));
|
|
1355
|
+
} catch {
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
const hints = [];
|
|
1359
|
+
if (key === "serializedtx" && ctx.method.startsWith("evm") && isPlainHex(value)) {
|
|
1360
|
+
const type = evmTxType(value);
|
|
1361
|
+
if (type) hints.push(`type=${type}`);
|
|
1362
|
+
}
|
|
1363
|
+
const extra = describe(key, value, ctx);
|
|
1364
|
+
if (extra) hints.push(extra);
|
|
1365
|
+
const kind = isPlainHex(value) ? "hex" : "str";
|
|
1366
|
+
const suffix = hints.length ? ` ${hints.join(" ")}` : "";
|
|
1367
|
+
return `<${kind} len=${value.length} sha256=${sha256Short(value)}${suffix}>`;
|
|
1368
|
+
}
|
|
1369
|
+
const json = safeJson(value);
|
|
1370
|
+
if (json === void 0) return "<object unhashable>";
|
|
1371
|
+
return jsonSummary(json, value);
|
|
1372
|
+
}
|
|
1373
|
+
function summarizeString(value, key, ctx) {
|
|
1374
|
+
if (key && KEEP_KEYS.has(key)) return value;
|
|
1375
|
+
if (key && ctx.inError && ERROR_TEXT_KEYS.has(key)) {
|
|
1376
|
+
return value.length > MAX_ERROR_STRING ? `${value.slice(0, MAX_ERROR_STRING)}\u2026` : value;
|
|
1377
|
+
}
|
|
1378
|
+
if (XPUB_RE.test(value)) return summarizeKeyMaterial(value);
|
|
1379
|
+
if (value.length <= MAX_PLAIN_STRING) return value;
|
|
1380
|
+
const kind = isPlainHex(value) ? "hex" : "str";
|
|
1381
|
+
return `<${kind} len=${value.length} sha256=${sha256Short(value)}>`;
|
|
1382
|
+
}
|
|
1383
|
+
function summarizeError(err, depth, ctx) {
|
|
1384
|
+
const e = err;
|
|
1385
|
+
const out = {
|
|
1386
|
+
name: err.name,
|
|
1387
|
+
message: summarizeString(err.message, "message", ctx)
|
|
1388
|
+
};
|
|
1389
|
+
for (const field of ERROR_FIELDS) {
|
|
1390
|
+
if (e[field] !== void 0) {
|
|
1391
|
+
out[field] = summarizeValue(e[field], normalizeKey(field), depth + 1, ctx);
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
return out;
|
|
1395
|
+
}
|
|
1396
|
+
function summarizeByKey(rawKey, value, depth, ctx) {
|
|
1397
|
+
const k = normalizeKey(rawKey);
|
|
1398
|
+
if (value === void 0 || value === null) return value;
|
|
1399
|
+
if (SECRET_KEYS.has(k)) return `<redacted len=${lengthOf(value)}>`;
|
|
1400
|
+
if (KEY_MATERIAL_KEYS.has(k)) {
|
|
1401
|
+
if (typeof value === "string" || toBytes(value)) return summarizeKeyMaterial(value);
|
|
1402
|
+
}
|
|
1403
|
+
if (SIGNATURE_KEYS.has(k)) {
|
|
1404
|
+
if (Array.isArray(value)) return `<signatures count=${value.length}>`;
|
|
1405
|
+
return `<signature len=${lengthOf(value)}>`;
|
|
1406
|
+
}
|
|
1407
|
+
if (SIGNED_PSBT_KEYS.has(k)) return `<psbt len=${lengthOf(value)}>`;
|
|
1408
|
+
if (SIGNED_TX_KEYS.has(k) || ctx.role === "result" && TX_IN_RESULT_KEYS.has(k)) {
|
|
1409
|
+
return `<signedTx len=${lengthOf(value)}>`;
|
|
1410
|
+
}
|
|
1411
|
+
if (AMOUNT_KEYS.has(k)) return "<amount>";
|
|
1412
|
+
if (ctx.inError && ERROR_TEXT_KEYS.has(k) && typeof value === "string") {
|
|
1413
|
+
return summarizeString(value, k, ctx);
|
|
1414
|
+
}
|
|
1415
|
+
if (PAYLOAD_KEYS.has(k)) return summarizePayload(k, value, ctx);
|
|
1416
|
+
if (ADDRESS_KEYS.has(k) || k.endsWith("address") || k.endsWith("addresses")) {
|
|
1417
|
+
if (typeof value === "string") return maskAddress(value);
|
|
1418
|
+
if (Array.isArray(value)) {
|
|
1419
|
+
return capArray(value, (item) => typeof item === "string" ? maskAddress(item) : "<addr>");
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
const inError = ERROR_CONTEXT_KEYS.has(k);
|
|
1423
|
+
const childCtx = inError === ctx.inError ? ctx : { ...ctx, inError };
|
|
1424
|
+
return summarizeValue(value, k, depth, childCtx);
|
|
1425
|
+
}
|
|
1426
|
+
function capArray(items, map) {
|
|
1427
|
+
const out = items.slice(0, MAX_ARRAY_ITEMS).map(map);
|
|
1428
|
+
if (items.length > MAX_ARRAY_ITEMS) out.push(`\u2026+${items.length - MAX_ARRAY_ITEMS} more`);
|
|
1429
|
+
return out;
|
|
1430
|
+
}
|
|
1431
|
+
function summarizeValue(value, key, depth, ctx) {
|
|
1432
|
+
switch (typeof value) {
|
|
1433
|
+
case "undefined":
|
|
1434
|
+
case "boolean":
|
|
1435
|
+
return value;
|
|
1436
|
+
case "number":
|
|
1437
|
+
return Number.isFinite(value) ? value : String(value);
|
|
1438
|
+
case "bigint":
|
|
1439
|
+
return `${value}n`;
|
|
1440
|
+
case "string":
|
|
1441
|
+
return summarizeString(value, key, ctx);
|
|
1442
|
+
case "function":
|
|
1443
|
+
return "<function>";
|
|
1444
|
+
case "symbol":
|
|
1445
|
+
return "<symbol>";
|
|
1446
|
+
default:
|
|
1447
|
+
break;
|
|
1448
|
+
}
|
|
1449
|
+
if (value === null) return null;
|
|
1450
|
+
const bytes = toBytes(value);
|
|
1451
|
+
if (bytes) return `<hex len=${bytes.length} sha256=${sha256Short(bytes)}>`;
|
|
1452
|
+
const obj = value;
|
|
1453
|
+
if (ctx.ancestors.has(obj)) return "<circular>";
|
|
1454
|
+
if (depth >= MAX_DEPTH) return "<depth>";
|
|
1455
|
+
ctx.ancestors.add(obj);
|
|
1456
|
+
try {
|
|
1457
|
+
if (value instanceof Error) return summarizeError(value, depth, { ...ctx, inError: true });
|
|
1458
|
+
if (Array.isArray(value)) {
|
|
1459
|
+
return capArray(value, (item) => summarizeValue(item, key, depth + 1, ctx));
|
|
1460
|
+
}
|
|
1461
|
+
const out = {};
|
|
1462
|
+
for (const [childKey, child] of Object.entries(value)) {
|
|
1463
|
+
out[childKey] = summarizeByKey(childKey, child, depth + 1, ctx);
|
|
1464
|
+
}
|
|
1465
|
+
return out;
|
|
1466
|
+
} finally {
|
|
1467
|
+
ctx.ancestors.delete(obj);
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
function summarizeForLog(value, options = {}) {
|
|
1471
|
+
try {
|
|
1472
|
+
return summarizeValue(value, void 0, 0, {
|
|
1473
|
+
method: options.method ?? "",
|
|
1474
|
+
role: options.role ?? "params",
|
|
1475
|
+
describePayload: options.describePayload,
|
|
1476
|
+
inError: false,
|
|
1477
|
+
ancestors: /* @__PURE__ */ new Set()
|
|
1478
|
+
});
|
|
1479
|
+
} catch {
|
|
1480
|
+
return "<unloggable>";
|
|
1481
|
+
}
|
|
1482
|
+
}
|
|
1483
|
+
|
|
571
1484
|
// src/utils/deviceIdentity.ts
|
|
572
1485
|
var ONEKEY_BLE_SERVICE_UUID = "00000001-0000-1000-8000-00805f9b34fb";
|
|
573
1486
|
var TREZOR_BLE_SERVICE_UUID = "8c000001-a59b-4d58-a9ad-073df69fa1b1";
|
|
574
1487
|
var LEDGER_USB_VENDOR_ID = 11415;
|
|
1488
|
+
var KEYSTONE_USB_VENDOR_ID = 4617;
|
|
1489
|
+
var KEYSTONE_USB_PRODUCT_ID = 12289;
|
|
575
1490
|
var lower = (value) => value?.trim().toLowerCase() ?? "";
|
|
576
1491
|
var hasServiceUuid = (input, uuid) => {
|
|
577
1492
|
const expected = uuid.toLowerCase();
|
|
@@ -594,6 +1509,11 @@ function detectHardwareVendorFromDescriptor(input) {
|
|
|
594
1509
|
if (typeof input.vendor === "number" && input.vendor === LEDGER_USB_VENDOR_ID || input.vendorId === LEDGER_USB_VENDOR_ID || manufacturerName.includes("ledger")) {
|
|
595
1510
|
return "ledger";
|
|
596
1511
|
}
|
|
1512
|
+
const usbVendorId = typeof input.vendor === "number" ? input.vendor : input.vendorId;
|
|
1513
|
+
const usbProductId = typeof input.product === "number" ? input.product : input.productId;
|
|
1514
|
+
if (usbVendorId === KEYSTONE_USB_VENDOR_ID && usbProductId === KEYSTONE_USB_PRODUCT_ID || manufacturerName.includes("keystone")) {
|
|
1515
|
+
return "keystone";
|
|
1516
|
+
}
|
|
597
1517
|
return void 0;
|
|
598
1518
|
}
|
|
599
1519
|
function isKnownNonTargetHardwareVendor(input, targetVendor) {
|
|
@@ -617,28 +1537,62 @@ async function batchCall(params, callFn, onProgress) {
|
|
|
617
1537
|
|
|
618
1538
|
// src/utils/methodCatalog.ts
|
|
619
1539
|
var HARDWARE_METHOD_CATALOG = {
|
|
620
|
-
evmGetAddress: { chain: "evm", allNetwork: true },
|
|
621
|
-
evmSignTransaction: {
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
1540
|
+
evmGetAddress: { chain: "evm", allNetwork: true, replayAfterTransportFailure: "safe" },
|
|
1541
|
+
evmSignTransaction: {
|
|
1542
|
+
chain: "evm",
|
|
1543
|
+
allNetwork: false,
|
|
1544
|
+
replayAfterTransportFailure: "unsafe"
|
|
1545
|
+
},
|
|
1546
|
+
evmSignMessage: { chain: "evm", allNetwork: false, replayAfterTransportFailure: "unsafe" },
|
|
1547
|
+
evmSignTypedData: { chain: "evm", allNetwork: false, replayAfterTransportFailure: "unsafe" },
|
|
1548
|
+
btcGetAddress: { chain: "btc", allNetwork: true, replayAfterTransportFailure: "safe" },
|
|
1549
|
+
btcGetPublicKey: { chain: "btc", allNetwork: true, replayAfterTransportFailure: "safe" },
|
|
1550
|
+
btcSignTransaction: {
|
|
1551
|
+
chain: "btc",
|
|
1552
|
+
allNetwork: false,
|
|
1553
|
+
replayAfterTransportFailure: "unsafe"
|
|
1554
|
+
},
|
|
1555
|
+
btcSignPsbt: { chain: "btc", allNetwork: false, replayAfterTransportFailure: "unsafe" },
|
|
1556
|
+
btcSignMessage: { chain: "btc", allNetwork: false, replayAfterTransportFailure: "unsafe" },
|
|
1557
|
+
btcGetMasterFingerprint: {
|
|
1558
|
+
chain: "btc",
|
|
1559
|
+
allNetwork: false,
|
|
1560
|
+
replayAfterTransportFailure: "safe"
|
|
1561
|
+
},
|
|
1562
|
+
solGetAddress: { chain: "sol", allNetwork: true, replayAfterTransportFailure: "safe" },
|
|
1563
|
+
solSignTransaction: {
|
|
1564
|
+
chain: "sol",
|
|
1565
|
+
allNetwork: false,
|
|
1566
|
+
replayAfterTransportFailure: "unsafe"
|
|
1567
|
+
},
|
|
1568
|
+
solSignMessage: { chain: "sol", allNetwork: false, replayAfterTransportFailure: "unsafe" },
|
|
1569
|
+
tronGetAddress: { chain: "tron", allNetwork: true, replayAfterTransportFailure: "safe" },
|
|
1570
|
+
tronSignTransaction: {
|
|
1571
|
+
chain: "tron",
|
|
1572
|
+
allNetwork: false,
|
|
1573
|
+
replayAfterTransportFailure: "unsafe"
|
|
1574
|
+
},
|
|
1575
|
+
tronSignMessage: { chain: "tron", allNetwork: false, replayAfterTransportFailure: "unsafe" },
|
|
1576
|
+
zcashGetFullViewingKey: {
|
|
1577
|
+
chain: "zcash",
|
|
1578
|
+
allNetwork: false,
|
|
1579
|
+
replayAfterTransportFailure: "safe"
|
|
1580
|
+
},
|
|
1581
|
+
zcashGetShieldedAddress: {
|
|
1582
|
+
chain: "zcash",
|
|
1583
|
+
allNetwork: false,
|
|
1584
|
+
replayAfterTransportFailure: "safe"
|
|
1585
|
+
}
|
|
636
1586
|
};
|
|
637
1587
|
var ALL_NETWORK_METHOD_NAMES = Object.entries(HARDWARE_METHOD_CATALOG).filter(([, metadata]) => metadata.allNetwork).map(([method]) => method);
|
|
638
1588
|
var ALL_NETWORK_METHOD_SET = new Set(ALL_NETWORK_METHOD_NAMES);
|
|
639
1589
|
function getHardwareMethodMetadata(method) {
|
|
640
1590
|
return HARDWARE_METHOD_CATALOG[method];
|
|
641
1591
|
}
|
|
1592
|
+
var SAFE_NON_CHAIN_METHODS = /* @__PURE__ */ new Set(["getFeatures", "authenticateDevice"]);
|
|
1593
|
+
function canReplayHardwareMethodAfterTransportFailure(method) {
|
|
1594
|
+
return getHardwareMethodMetadata(method)?.replayAfterTransportFailure === "safe" || SAFE_NON_CHAIN_METHODS.has(method);
|
|
1595
|
+
}
|
|
642
1596
|
function isAllNetworkMethodName(method) {
|
|
643
1597
|
return ALL_NETWORK_METHOD_SET.has(method);
|
|
644
1598
|
}
|
|
@@ -698,6 +1652,38 @@ async function runAllNetworkGetAddress({
|
|
|
698
1652
|
}
|
|
699
1653
|
return success(responses);
|
|
700
1654
|
}
|
|
1655
|
+
function isUserRefusal(code) {
|
|
1656
|
+
return code === 10005 /* UserAborted */ || code === 10001 /* UserRejected */ || code === 10401 /* PinCancelled */;
|
|
1657
|
+
}
|
|
1658
|
+
function isWalletSafetyFailure(code) {
|
|
1659
|
+
const codes = [
|
|
1660
|
+
10106 /* DeviceMismatch */,
|
|
1661
|
+
10114 /* DeviceSearchMismatch */,
|
|
1662
|
+
10103 /* DeviceLocked */,
|
|
1663
|
+
10400 /* PinInvalid */,
|
|
1664
|
+
10404 /* PinMismatch */,
|
|
1665
|
+
10402 /* PassphraseRejected */,
|
|
1666
|
+
10403 /* PassphraseStateMismatch */,
|
|
1667
|
+
10405 /* PassphraseAlwaysOnDevice */
|
|
1668
|
+
];
|
|
1669
|
+
return code !== void 0 && codes.includes(code);
|
|
1670
|
+
}
|
|
1671
|
+
function isConnectionLost(code) {
|
|
1672
|
+
const codes = [
|
|
1673
|
+
10101 /* DeviceDisconnected */,
|
|
1674
|
+
10003 /* OperationTimeout */,
|
|
1675
|
+
10300 /* TransportError */,
|
|
1676
|
+
10113 /* OperationEnded */,
|
|
1677
|
+
10112 /* OperationNotFound */
|
|
1678
|
+
];
|
|
1679
|
+
return code !== void 0 && codes.includes(code);
|
|
1680
|
+
}
|
|
1681
|
+
function buildAllNetworkCancelledFailure(signal) {
|
|
1682
|
+
const reason = signal.reason;
|
|
1683
|
+
const code = typeof reason?.code === "number" ? reason.code : 10005 /* UserAborted */;
|
|
1684
|
+
const message = typeof reason?.message === "string" ? reason.message : "";
|
|
1685
|
+
return failure(code, message || "All-network get-address cancelled");
|
|
1686
|
+
}
|
|
701
1687
|
function buildUnsupportedMethodResponse(item) {
|
|
702
1688
|
return {
|
|
703
1689
|
...item,
|
|
@@ -731,8 +1717,12 @@ export {
|
|
|
731
1717
|
DeviceJobQueue,
|
|
732
1718
|
EConnectorInteraction,
|
|
733
1719
|
HARDWARE_METHOD_CATALOG,
|
|
1720
|
+
HARDWARE_RUNTIME_ID_PREFIX,
|
|
734
1721
|
HardwareErrorCode,
|
|
1722
|
+
MISSING_BIP32_MASTER_FINGERPRINT,
|
|
1723
|
+
OPERATION_DEFAULT_TTL_MS,
|
|
735
1724
|
ORPHAN_ELIGIBLE_ERROR_CODES,
|
|
1725
|
+
OperationRegistry,
|
|
736
1726
|
SDK,
|
|
737
1727
|
TypedEventEmitter,
|
|
738
1728
|
UI_EVENT,
|
|
@@ -743,28 +1733,56 @@ export {
|
|
|
743
1733
|
UI_REQUEST_TIMEOUT_TAG,
|
|
744
1734
|
UI_RESPONSE,
|
|
745
1735
|
UiRequestRegistry,
|
|
1736
|
+
assertBoundedCbor,
|
|
746
1737
|
batchCall,
|
|
1738
|
+
boundedCborFromHex,
|
|
1739
|
+
buildAllNetworkCancelledFailure,
|
|
747
1740
|
buildUnsupportedMethodResponse,
|
|
748
|
-
|
|
1741
|
+
bytesToHex3 as bytesToHex,
|
|
1742
|
+
canReplayHardwareMethodAfterTransportFailure,
|
|
749
1743
|
compareSemver,
|
|
750
1744
|
createBridgedConnector,
|
|
751
1745
|
createCombinedConnector,
|
|
1746
|
+
createHardwareLinkId,
|
|
1747
|
+
createHardwareOperationId,
|
|
1748
|
+
createHardwareSearchTargetId,
|
|
752
1749
|
createHwkError,
|
|
1750
|
+
defaultOriginForCode,
|
|
1751
|
+
defaultRecoveryForCode,
|
|
753
1752
|
deriveDeviceFingerprint,
|
|
754
1753
|
detectHardwareVendorFromDescriptor,
|
|
1754
|
+
emitDeviceBindingStatus,
|
|
755
1755
|
enrichErrorMessage,
|
|
756
1756
|
ensure0x,
|
|
757
1757
|
failure,
|
|
758
1758
|
getAllNetworkMethodChain,
|
|
759
1759
|
getHardwareMethodMetadata,
|
|
1760
|
+
hasHardwareRuntimeIdPrefix,
|
|
760
1761
|
hexToBytes,
|
|
761
1762
|
isAllNetworkMethodName,
|
|
1763
|
+
isConnectionLost,
|
|
1764
|
+
isHardwareOperationId,
|
|
1765
|
+
isHwkRecoveryHint,
|
|
762
1766
|
isKnownNonTargetHardwareVendor,
|
|
1767
|
+
isUserRefusal,
|
|
1768
|
+
isWalletSafetyFailure,
|
|
1769
|
+
operationMayHaveCompletedParams,
|
|
763
1770
|
padHex64,
|
|
1771
|
+
parseBip32MasterFingerprint,
|
|
1772
|
+
parseHardwareRuntimeId,
|
|
1773
|
+
parseWalletMasterFingerprint,
|
|
1774
|
+
pickConnectionContext,
|
|
1775
|
+
prepareSolanaOffchainMessageV1,
|
|
764
1776
|
rehydrateConnectorError,
|
|
1777
|
+
requestBleDeviceSelection,
|
|
1778
|
+
requestSaveDeviceBinding,
|
|
1779
|
+
resolveHardwareOperationTarget,
|
|
1780
|
+
resolveSearchTargetReusePolicy,
|
|
765
1781
|
runAllNetworkGetAddress,
|
|
766
1782
|
serializeConnectorError,
|
|
767
1783
|
stripHex,
|
|
768
|
-
success
|
|
1784
|
+
success,
|
|
1785
|
+
summarizeForLog,
|
|
1786
|
+
toPhysicalDeviceSearchTarget
|
|
769
1787
|
};
|
|
770
1788
|
//# sourceMappingURL=index.mjs.map
|