@onekeyfe/hwk-ledger-adapter 1.2.6-alpha.0 → 1.2.6-alpha.2

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/index.js CHANGED
@@ -39,7 +39,6 @@ __export(index_exports, {
39
39
  SignerEth: () => SignerEth,
40
40
  SignerManager: () => SignerManager,
41
41
  SignerSol: () => SignerSol,
42
- SignerZcash: () => SignerZcash,
43
42
  debugLog: () => debugLog,
44
43
  deviceActionToPromise: () => deviceActionToPromise,
45
44
  isDeviceLockedError: () => isDeviceLockedError,
@@ -58,14 +57,17 @@ var import_hwk_adapter_core3 = require("@onekeyfe/hwk-adapter-core");
58
57
 
59
58
  // src/errors.ts
60
59
  var import_hwk_adapter_core = require("@onekeyfe/hwk-adapter-core");
61
- function ledgerFailure(code, error, appName, tag, params, origin, recovery) {
60
+ var MULTIPLE_USB_LEDGER_DEVICES_ERROR_MESSAGE = "Multiple Ledger USB devices are connected. Please connect only one Ledger device and try again.";
61
+ function createMultipleUsbLedgerDevicesError() {
62
+ return Object.assign(new Error(MULTIPLE_USB_LEDGER_DEVICES_ERROR_MESSAGE), {
63
+ code: import_hwk_adapter_core.HardwareErrorCode.DeviceOneDeviceOnly
64
+ });
65
+ }
66
+ function ledgerFailure(code, error, appName, tag, params) {
62
67
  const payload = { error, code };
63
68
  if (appName !== void 0) payload.appName = appName;
64
69
  if (tag !== void 0) payload._tag = tag;
65
70
  if (params !== void 0) payload.params = params;
66
- const resolvedOrigin = origin ?? (0, import_hwk_adapter_core.defaultOriginForCode)(code);
67
- if (resolvedOrigin !== void 0) payload.origin = resolvedOrigin;
68
- payload.recovery = recovery ?? (0, import_hwk_adapter_core.defaultRecoveryForCode)(code);
69
71
  return { success: false, payload };
70
72
  }
71
73
  var LOCKED_ERROR_CODES = /* @__PURE__ */ new Set(["5515", "21781", "6982", "27010", "5303", "21251"]);
@@ -206,10 +208,7 @@ var ERROR_TAG = {
206
208
  UnknownDevice: "UnknownDeviceError",
207
209
  DeviceSessionRefresher: "DeviceSessionRefresherError",
208
210
  DeviceNotInitialized: "DeviceNotInitializedError",
209
- // DMK class OpeningConnectionError carries `_tag` "ConnectionOpeningError"; both
210
- // spellings are kept so a DMK release that aligns them still classifies.
211
- OpeningConnection: "ConnectionOpeningError",
212
- OpeningConnectionLegacy: "OpeningConnectionError",
211
+ OpeningConnection: "OpeningConnectionError",
213
212
  DeviceDisconnectedBeforeSendingApdu: "DeviceDisconnectedBeforeSendingApdu",
214
213
  DeviceDisconnectedWhileSending: "DeviceDisconnectedWhileSendingError",
215
214
  Disconnect: "DisconnectError",
@@ -222,16 +221,7 @@ var ERROR_TAG = {
222
221
  // DMK remote-network failures (manager-api HTTP / secure-channel WS).
223
222
  WebSocketConnection: "WebSocketConnectionError",
224
223
  HttpFetch: "FetchError",
225
- NetworkDA: "NetworkDAError",
226
- InvalidFirmwareMetadataResponse: "InvalidGetFirmwareMetadataResponseError",
227
- ApplicationsMetadataTask: "GetApplicationsMetadataTaskError",
228
- // DMK OS device actions. SecureChannelError is what mapInstallDAErrors() leaves
229
- // after splitting out device answers, i.e. the relay itself broke.
230
- SecureChannel: "SecureChannelError",
231
- RefusedByUserDA: "RefusedByUserDAError",
232
- AppAlreadyInstalledDA: "AppAlreadyInstalledDAError",
233
- OutOfMemoryDA: "OutOfMemoryDAError",
234
- DeviceNotOnboarded: "DeviceNotOnboardedError"
224
+ InvalidFirmwareMetadataResponse: "InvalidGetFirmwareMetadataResponseError"
235
225
  };
236
226
  function isDeviceLockedError(err) {
237
227
  if (!err || typeof err !== "object") return false;
@@ -261,6 +251,7 @@ function isBlePairingFailureError(err) {
261
251
  return false;
262
252
  }
263
253
  var CONNECTION_LEVEL_TAGS = /* @__PURE__ */ new Set([
254
+ ERROR_TAG.DeviceLocked,
264
255
  ERROR_TAG.DeviceNotAdvertising,
265
256
  ERROR_TAG.BlePairingTimeout,
266
257
  ERROR_TAG.BleGattBondingFailed,
@@ -272,7 +263,6 @@ var CONNECTION_LEVEL_TAGS = /* @__PURE__ */ new Set([
272
263
  ERROR_TAG.DeviceSessionRefresher,
273
264
  ERROR_TAG.DeviceNotInitialized,
274
265
  ERROR_TAG.OpeningConnection,
275
- ERROR_TAG.OpeningConnectionLegacy,
276
266
  ERROR_TAG.DeviceDisconnectedBeforeSendingApdu,
277
267
  ERROR_TAG.DeviceDisconnectedWhileSending,
278
268
  ERROR_TAG.Disconnect,
@@ -287,10 +277,7 @@ var DEVICE_NOT_FOUND_TAGS = /* @__PURE__ */ new Set([
287
277
  // Map to DeviceNotFound so non-BLE-direct paths get a sensible error code.
288
278
  ERROR_TAG.DeviceNotInDiscoveryCache
289
279
  ]);
290
- var DEVICE_BUSY_TAGS = /* @__PURE__ */ new Set([
291
- ERROR_TAG.OpeningConnection,
292
- ERROR_TAG.OpeningConnectionLegacy
293
- ]);
280
+ var DEVICE_BUSY_TAGS = /* @__PURE__ */ new Set([ERROR_TAG.OpeningConnection]);
294
281
  var DEVICE_DISCONNECTED_TAGS = /* @__PURE__ */ new Set([
295
282
  ERROR_TAG.DeviceNotRecognized,
296
283
  ERROR_TAG.DeviceSessionNotFound,
@@ -302,18 +289,17 @@ var DEVICE_DISCONNECTED_TAGS = /* @__PURE__ */ new Set([
302
289
  ERROR_TAG.WebHIDDisconnect
303
290
  ]);
304
291
  function isConnectionLevelError(err) {
305
- return hasErrorTag(err, CONNECTION_LEVEL_TAGS);
292
+ if (!err || typeof err !== "object") return false;
293
+ const tag = err._tag;
294
+ if (tag && CONNECTION_LEVEL_TAGS.has(tag)) return true;
295
+ const e = err;
296
+ if (e.originalError != null && isConnectionLevelError(e.originalError)) return true;
297
+ if (e.error != null && e._tag && isConnectionLevelError(e.error)) return true;
298
+ return false;
306
299
  }
307
300
  function isKnownConnectionTag(tag) {
308
301
  return typeof tag === "string" && CONNECTION_LEVEL_TAGS.has(tag);
309
302
  }
310
- var CONNECTION_OPENING_TAGS = /* @__PURE__ */ new Set([
311
- ERROR_TAG.OpeningConnection,
312
- ERROR_TAG.OpeningConnectionLegacy
313
- ]);
314
- function isConnectionOpeningTag(tag) {
315
- return typeof tag === "string" && CONNECTION_OPENING_TAGS.has(tag);
316
- }
317
303
  function hasStatusCode(err, codeSet) {
318
304
  if (!err || typeof err !== "object") return false;
319
305
  const e = err;
@@ -349,28 +335,28 @@ function hasInvalidArgumentCode(err) {
349
335
  if (e.error != null && e._tag && hasInvalidArgumentCode(e.error)) return true;
350
336
  return false;
351
337
  }
352
- function hasErrorTag(err, tags) {
338
+ function isDeviceNotFoundError(err) {
353
339
  if (!err || typeof err !== "object") return false;
340
+ const tag = err._tag;
341
+ if (tag && DEVICE_NOT_FOUND_TAGS.has(tag)) return true;
354
342
  const e = err;
355
- if (typeof e._tag === "string" && tags.has(e._tag)) return true;
356
- if (e.originalError != null && hasErrorTag(e.originalError, tags)) return true;
357
- if (e.error != null && e._tag && hasErrorTag(e.error, tags)) return true;
343
+ if (e.originalError != null && isDeviceNotFoundError(e.originalError)) return true;
344
+ if (e.error != null && e._tag && isDeviceNotFoundError(e.error)) return true;
358
345
  return false;
359
346
  }
360
- function isDeviceNotFoundError(err) {
361
- return hasErrorTag(err, DEVICE_NOT_FOUND_TAGS);
362
- }
363
347
  function isDeviceBusyError(err) {
364
- return hasErrorTag(err, DEVICE_BUSY_TAGS);
348
+ if (!err || typeof err !== "object") return false;
349
+ const tag = err._tag;
350
+ if (tag && DEVICE_BUSY_TAGS.has(tag)) return true;
351
+ const e = err;
352
+ if (e.originalError != null && isDeviceBusyError(e.originalError)) return true;
353
+ if (e.error != null && e._tag && isDeviceBusyError(e.error)) return true;
354
+ return false;
365
355
  }
366
- var USER_REJECTED_TAGS = /* @__PURE__ */ new Set([
367
- ERROR_TAG.UserRefusedOnDevice,
368
- ERROR_TAG.RefusedByUserDA
369
- ]);
370
356
  function isUserRejectedError(err) {
371
357
  if (!err || typeof err !== "object") return false;
372
358
  const e = err;
373
- if (hasErrorTag(err, USER_REJECTED_TAGS)) return true;
359
+ if (e._tag === ERROR_TAG.UserRefusedOnDevice) return true;
374
360
  if (typeof e.message === "string" && /denied|rejected|refused/i.test(e.message)) return true;
375
361
  if (hasStatusCode(err, USER_REJECTED_CODES)) return true;
376
362
  return false;
@@ -401,33 +387,15 @@ function isAppNotInstalledError(err) {
401
387
  if (hasStatusCode(err, APP_NOT_INSTALLED_CODES)) return true;
402
388
  return false;
403
389
  }
404
- var OUT_OF_MEMORY_TAGS = /* @__PURE__ */ new Set([ERROR_TAG.OutOfMemoryDA]);
405
390
  function isOutOfMemoryError(err) {
406
- return hasErrorTag(err, OUT_OF_MEMORY_TAGS);
407
- }
408
- var APP_ALREADY_INSTALLED_TAGS = /* @__PURE__ */ new Set([ERROR_TAG.AppAlreadyInstalledDA]);
409
- function isAppAlreadyInstalledError(err) {
410
- return hasErrorTag(err, APP_ALREADY_INSTALLED_TAGS);
411
- }
412
- var DEVICE_NOT_ONBOARDED_TAGS = /* @__PURE__ */ new Set([ERROR_TAG.DeviceNotOnboarded]);
413
- function isDeviceNotOnboardedError(err) {
414
- return hasErrorTag(err, DEVICE_NOT_ONBOARDED_TAGS);
415
- }
416
- var FIRMWARE_METADATA_TAGS = /* @__PURE__ */ new Set([
417
- ERROR_TAG.InvalidFirmwareMetadataResponse,
418
- ERROR_TAG.ApplicationsMetadataTask
419
- ]);
420
- function isFirmwareMetadataError(err) {
421
- return hasErrorTag(err, FIRMWARE_METADATA_TAGS);
422
- }
423
- var SECURE_CHANNEL_TAGS = /* @__PURE__ */ new Set([ERROR_TAG.SecureChannel]);
424
- function isSecureChannelError(err) {
425
- return hasErrorTag(err, SECURE_CHANNEL_TAGS);
391
+ if (!err || typeof err !== "object") return false;
392
+ const e = err;
393
+ return e._tag === "OutOfMemoryDAError";
426
394
  }
427
395
  var NETWORK_ERROR_TAGS = /* @__PURE__ */ new Set([
428
396
  ERROR_TAG.WebSocketConnection,
429
397
  ERROR_TAG.HttpFetch,
430
- ERROR_TAG.NetworkDA
398
+ ERROR_TAG.InvalidFirmwareMetadataResponse
431
399
  ]);
432
400
  function isNetworkError(err) {
433
401
  if (!err || typeof err !== "object") return false;
@@ -477,34 +445,23 @@ function isTransportStuck(err) {
477
445
  function isStuckAppStateError(err) {
478
446
  return isAppStuckByApdu(err) || isTransportStuck(err);
479
447
  }
480
- var DMK_PLACEHOLDER_MESSAGE = "Unknown error.";
481
- function nestedErrorMessage(nested) {
482
- if (!nested || typeof nested !== "object") return void 0;
483
- const { message } = nested;
484
- if (typeof message !== "string") return void 0;
485
- const trimmed = message.trim();
486
- if (!trimmed || trimmed === DMK_PLACEHOLDER_MESSAGE) return void 0;
487
- return message;
488
- }
489
448
  function mapLedgerError(err, opts) {
490
449
  let originalMessage = "Unknown Ledger error";
491
450
  if (err instanceof Error) {
492
451
  originalMessage = err.message;
493
452
  } else if (err && typeof err === "object") {
494
453
  const e = err;
495
- originalMessage = String(
496
- e.message ?? nestedErrorMessage(e.originalError) ?? e._tag ?? e.type ?? JSON.stringify(err)
497
- );
454
+ originalMessage = String(e.message ?? e._tag ?? e.type ?? JSON.stringify(err));
498
455
  }
499
456
  let code;
500
457
  if (isDeviceLockedError(err)) {
501
458
  code = import_hwk_adapter_core.HardwareErrorCode.DeviceLocked;
502
459
  } else if (isDeviceNotAdvertisingError(err) || isDeviceNotFoundError(err)) {
503
460
  code = import_hwk_adapter_core.HardwareErrorCode.DeviceNotFound;
504
- } else if (isBlePairingFailureError(err)) {
505
- code = import_hwk_adapter_core.HardwareErrorCode.BlePairingTimeout;
506
461
  } else if (isDeviceBusyError(err)) {
507
462
  code = import_hwk_adapter_core.HardwareErrorCode.DeviceBusy;
463
+ } else if (isBlePairingFailureError(err)) {
464
+ code = import_hwk_adapter_core.HardwareErrorCode.BlePairingTimeout;
508
465
  } else if (isUserAbortedError(err)) {
509
466
  code = import_hwk_adapter_core.HardwareErrorCode.UserAborted;
510
467
  } else if (isUserRejectedError(err)) {
@@ -513,18 +470,10 @@ function mapLedgerError(err, opts) {
513
470
  code = import_hwk_adapter_core.HardwareErrorCode.WrongApp;
514
471
  } else if (isAppNotInstalledError(err)) {
515
472
  code = import_hwk_adapter_core.HardwareErrorCode.AppNotInstalled;
516
- } else if (isAppAlreadyInstalledError(err)) {
517
- code = import_hwk_adapter_core.HardwareErrorCode.AppAlreadyInstalled;
518
473
  } else if (isOutOfMemoryError(err)) {
519
474
  code = import_hwk_adapter_core.HardwareErrorCode.DeviceOutOfMemory;
520
- } else if (isDeviceNotOnboardedError(err)) {
521
- code = import_hwk_adapter_core.HardwareErrorCode.DeviceNotInitialized;
522
- } else if (isFirmwareMetadataError(err)) {
523
- code = import_hwk_adapter_core.HardwareErrorCode.LedgerFirmwareMetadataError;
524
475
  } else if (isNetworkError(err)) {
525
476
  code = import_hwk_adapter_core.HardwareErrorCode.NetworkError;
526
- } else if (isSecureChannelError(err)) {
527
- code = import_hwk_adapter_core.HardwareErrorCode.LedgerSecureChannelError;
528
477
  } else if (isDeviceDisconnectedError(err)) {
529
478
  code = import_hwk_adapter_core.HardwareErrorCode.DeviceDisconnected;
530
479
  } else if (isTimeoutError(err)) {
@@ -537,21 +486,7 @@ function mapLedgerError(err, opts) {
537
486
  }
538
487
  const errAppName = err && typeof err === "object" ? err.appName : void 0;
539
488
  const appName = errAppName ?? opts?.defaultAppName;
540
- return {
541
- code,
542
- message: (0, import_hwk_adapter_core.enrichErrorMessage)(code, originalMessage),
543
- origin: (0, import_hwk_adapter_core.defaultOriginForCode)(code),
544
- appName
545
- };
546
- }
547
-
548
- // src/utils/queueKey.ts
549
- var LEDGER_DEFAULT_QUEUE_KEY = "__ledger_default__";
550
- function ledgerQueueKey({
551
- operationId,
552
- connectId
553
- }) {
554
- return (operationId ?? connectId) || LEDGER_DEFAULT_QUEUE_KEY;
489
+ return { code, message: (0, import_hwk_adapter_core.enrichErrorMessage)(code, originalMessage), appName };
555
490
  }
556
491
 
557
492
  // src/adapter/methods/allNetworkGetAddress.ts
@@ -612,103 +547,65 @@ var LEDGER_BTC_NETWORK_COIN_MAP = {
612
547
  var LEDGER_UNSUPPORTED_ALLNETWORK_NETWORKS = /* @__PURE__ */ new Set(["doge", "dogecoin"]);
613
548
  function createAllNetworkGetAddress({
614
549
  callChain,
615
- getChainFingerprint,
616
- retainOperation,
617
- errorToFailure,
618
- createCancelScope
550
+ getChainFingerprint
619
551
  }) {
620
552
  return async function allNetworkGetAddress(connectId, _deviceId, params) {
621
- debugLog("[LedgerAdapter][REQ]", {
622
- method: "allNetworkGetAddress",
623
- connectId,
624
- itemCount: params.bundle.length
625
- });
626
- const target = (0, import_hwk_adapter_core2.resolveHardwareOperationTarget)(connectId, params.operationId, "ledger");
627
- if (!target.success) return target;
628
- const effectiveTargetId = target.payload.targetId ?? "";
629
- let releaseOperationRetention;
630
- try {
631
- releaseOperationRetention = target.payload.operationId ? retainOperation(target.payload.operationId) : void 0;
632
- } catch (error) {
633
- return errorToFailure(error);
634
- }
635
- const cancelScope = createCancelScope(
636
- ledgerQueueKey({ operationId: target.payload.operationId, connectId: effectiveTargetId })
637
- );
553
+ debugLog("[LedgerAdapter][REQ]", { method: "allNetworkGetAddress", connectId, params });
638
554
  const installContext = {};
639
555
  const commonParams = {
640
- autoInstallApp: params.autoInstallApp,
641
- operationId: target.payload.operationId,
642
- knownConnections: params.knownConnections,
643
- extra: params.extra,
644
- allowDeviceSelection: params.allowDeviceSelection,
645
- supportedTransports: params.supportedTransports
556
+ autoInstallApp: params.autoInstallApp
646
557
  };
647
558
  const chainFingerprints = /* @__PURE__ */ new Map();
648
- try {
649
- const result = await (0, import_hwk_adapter_core2.runAllNetworkGetAddress)({
650
- connectId: effectiveTargetId,
651
- deviceId: _deviceId,
652
- params,
653
- normalizeItem: normalizeLedgerAllNetworkItem,
654
- buildUnsupportedNetworkResponse: (item) => isUnsupportedLedgerAllNetworkNetwork(item) ? buildUnsupportedNetworkResponse(item) : void 0,
655
- callItem: async ({ method, chain, item }) => {
656
- if (cancelScope.signal.aborted) return buildCancelledFailure(cancelScope.signal);
657
- const itemDeviceId = getItemDeviceId(item) ?? chainFingerprints.get(chain) ?? "";
658
- return callAllNetworkMethod(
659
- callChain,
660
- effectiveTargetId,
661
- itemDeviceId,
662
- method,
663
- item,
664
- commonParams,
665
- installContext
666
- );
667
- },
668
- attachIdentity: async ({ item, chain, payload }) => attachLedgerIdentity(
669
- getChainFingerprint,
670
- effectiveTargetId,
559
+ const result = await (0, import_hwk_adapter_core2.runAllNetworkGetAddress)({
560
+ connectId,
561
+ deviceId: _deviceId,
562
+ params,
563
+ normalizeItem: normalizeLedgerAllNetworkItem,
564
+ buildUnsupportedNetworkResponse: (item) => isUnsupportedLedgerAllNetworkNetwork(item) ? buildUnsupportedNetworkResponse(item) : void 0,
565
+ callItem: async ({ method, chain, item }) => {
566
+ const itemDeviceId = getItemDeviceId(item) ?? chainFingerprints.get(chain) ?? "";
567
+ return callAllNetworkMethod(
568
+ callChain,
569
+ connectId,
570
+ itemDeviceId,
571
+ method,
671
572
  item,
672
- chain,
673
- payload,
674
- chainFingerprints,
675
- installContext,
676
- cancelScope.signal
677
- ),
678
- shouldAbortBundle: isTopLevelAllNetworkFailure,
679
- buildTopLevelFailure: (response) => {
680
- const code = response.payload?.code ?? import_hwk_adapter_core2.HardwareErrorCode.DeviceMismatch;
681
- return (0, import_hwk_adapter_core2.failure)(
682
- code,
683
- response.payload?.error ?? "All-network get-address aborted",
684
- response.payload?.params
685
- );
686
- }
687
- });
688
- debugLog("[LedgerAdapter][RES]", {
689
- method: "allNetworkGetAddress",
690
- success: result.success,
691
- payload: result
692
- });
693
- return result;
694
- } finally {
695
- cancelScope.release();
696
- releaseOperationRetention?.();
697
- }
573
+ commonParams,
574
+ installContext
575
+ );
576
+ },
577
+ attachIdentity: async ({ item, chain, payload }) => attachLedgerIdentity(
578
+ getChainFingerprint,
579
+ connectId,
580
+ item,
581
+ chain,
582
+ payload,
583
+ chainFingerprints
584
+ ),
585
+ shouldAbortBundle: isTopLevelAllNetworkFailure,
586
+ buildTopLevelFailure: (response) => {
587
+ const code = response.payload?.code ?? import_hwk_adapter_core2.HardwareErrorCode.DeviceMismatch;
588
+ return (0, import_hwk_adapter_core2.failure)(
589
+ code,
590
+ response.payload?.error ?? "All-network get-address aborted",
591
+ response.payload?.params
592
+ );
593
+ }
594
+ });
595
+ debugLog("[LedgerAdapter][RES]", {
596
+ method: "allNetworkGetAddress",
597
+ success: result.success,
598
+ payload: result
599
+ });
600
+ return result;
698
601
  };
699
602
  }
700
- function buildCancelledFailure(signal) {
701
- const reason = signal.reason;
702
- const code = typeof reason?.code === "number" ? reason.code : import_hwk_adapter_core2.HardwareErrorCode.UserAborted;
703
- const message = typeof reason?.message === "string" ? reason.message : "";
704
- return (0, import_hwk_adapter_core2.failure)(code, message || "All-network get-address cancelled");
705
- }
706
603
  function isTopLevelAllNetworkFailure(response) {
707
604
  if (response.success) {
708
605
  return false;
709
606
  }
710
607
  const code = response.payload?.code;
711
- return code === import_hwk_adapter_core2.HardwareErrorCode.DeviceMismatch || (0, import_hwk_adapter_core2.isUserRefusal)(code) || (0, import_hwk_adapter_core2.isConnectionLost)(code);
608
+ return code === import_hwk_adapter_core2.HardwareErrorCode.DeviceMismatch || code === import_hwk_adapter_core2.HardwareErrorCode.UserAborted || code === import_hwk_adapter_core2.HardwareErrorCode.UserRejected;
712
609
  }
713
610
  function getItemDeviceId(item) {
714
611
  const { deviceId } = item;
@@ -738,18 +635,8 @@ function normalizeLedgerAllNetworkItem(method, item) {
738
635
  const coin = LEDGER_BTC_NETWORK_COIN_MAP[item.network];
739
636
  return coin ? { ...item, coin } : item;
740
637
  }
741
- async function attachLedgerIdentity(getChainFingerprint, connectId, item, chain, payload, chainFingerprints, context, cancelSignal) {
742
- const knownFingerprint = getItemDeviceId(item) || chainFingerprints.get(chain) || "";
743
- if (!knownFingerprint && cancelSignal.aborted) {
744
- const cancelled = buildCancelledFailure(cancelSignal);
745
- return { ...item, success: false, payload: cancelled.payload };
746
- }
747
- const fingerprint = knownFingerprint || await bootstrapChainFingerprint(
748
- getChainFingerprint,
749
- context.connection?.connectId ?? connectId,
750
- chain,
751
- context
752
- );
638
+ async function attachLedgerIdentity(getChainFingerprint, connectId, item, chain, payload, chainFingerprints) {
639
+ const fingerprint = getItemDeviceId(item) || chainFingerprints.get(chain) || await bootstrapChainFingerprint(getChainFingerprint, connectId, chain);
753
640
  if (!fingerprint) {
754
641
  return buildFingerprintBootstrapFailure(item, chain);
755
642
  }
@@ -770,8 +657,8 @@ async function attachLedgerIdentity(getChainFingerprint, connectId, item, chain,
770
657
  }
771
658
  };
772
659
  }
773
- async function bootstrapChainFingerprint(getChainFingerprint, connectId, chain, context) {
774
- const response = await getChainFingerprint(connectId, chain, context);
660
+ async function bootstrapChainFingerprint(getChainFingerprint, connectId, chain) {
661
+ const response = await getChainFingerprint(connectId, chain);
775
662
  return response.success ? response.payload : "";
776
663
  }
777
664
  function buildFingerprintBootstrapFailure(item, chain) {
@@ -864,16 +751,6 @@ function isLedgerBleDescriptor(connectionType, descriptor) {
864
751
  function formatDeviceMismatchError(expected, actual) {
865
752
  return `Wrong device: expected ${expected}, got ${actual}`;
866
753
  }
867
- var LOG_REDACTED_RESULT_KEYS = ["ufvk", "orchardFvk"];
868
- function redactResultForLog(result) {
869
- if (!result || typeof result !== "object") return result;
870
- const keys = LOG_REDACTED_RESULT_KEYS.filter((key) => key in result);
871
- if (!keys.length) return result;
872
- return { ...result, ...Object.fromEntries(keys.map((key) => [key, "[redacted]"])) };
873
- }
874
- function isLostConnectionError(err) {
875
- return isDeviceDisconnectedError(err) || isDeviceNotAdvertisingError(err) || isTimeoutError(err) || isConnectionLevelError(err);
876
- }
877
754
  var BTC_HIGH_INDEX_THRESHOLD = 100;
878
755
  function btcAccountIndexFromPath(path) {
879
756
  const segments = path.replace(/^m\//, "").split("/");
@@ -885,35 +762,9 @@ function btcAccountIndexFromPath(path) {
885
762
  var _LedgerAdapter = class _LedgerAdapter {
886
763
  constructor(connector, options) {
887
764
  this.vendor = "ledger";
888
- // Cancel frees the DMK action and its intent-queue slot, but cannot retract a
889
- // confirmation screen the device is already showing.
890
- this.cancelCapability = "stops-waiting";
891
765
  this.emitter = new import_hwk_adapter_core3.TypedEventEmitter();
892
- this._operations = new import_hwk_adapter_core3.OperationRegistry({
893
- vendor: "ledger",
894
- onEnded: (operation, reason) => {
895
- this._discardPendingBinding(this._pendingOperationBindings.get(operation.operationId));
896
- this._pendingOperationBindings.delete(operation.operationId);
897
- this.emitter.emit(import_hwk_adapter_core3.SDK.OPERATION_ENDED, {
898
- type: import_hwk_adapter_core3.SDK.OPERATION_ENDED,
899
- payload: { operationId: operation.operationId, reason }
900
- });
901
- if (reason === "timeout") {
902
- void this._releaseOperationConnection(operation);
903
- }
904
- }
905
- });
906
766
  this._discoveredDevices = /* @__PURE__ */ new Map();
907
767
  this._sessions = /* @__PURE__ */ new Map();
908
- // An operation's BLE pick is saved by its first call that gets past the wallet check.
909
- this._pendingOperationBindings = /* @__PURE__ */ new Map();
910
- // A one-shot call's unchecked BLE pick, by connectId. It outlives the call only
911
- // through an unlock retry; any other failure drops it with its session.
912
- this._pendingPickBindings = /* @__PURE__ */ new Map();
913
- // Transport each session was opened on, by sessionId. The adapter-wide
914
- // `_activeConnectionType` moves with every discovery and can go stale.
915
- this._sessionTransports = /* @__PURE__ */ new Map();
916
- this._selectedBleReconnectTargets = /* @__PURE__ */ new Map();
917
768
  this._uiRegistry = new import_hwk_adapter_core3.UiRequestRegistry();
918
769
  // BTC App rejects account index >= 100 unless display=true. Cached per
919
770
  // adapter instance: first 100+ path asks the user once via UI request,
@@ -927,31 +778,10 @@ var _LedgerAdapter = class _LedgerAdapter {
927
778
  this._deviceAuthenticityQueueTail = Promise.resolve();
928
779
  // Shared across concurrent callers — only `cancel()` aborts.
929
780
  this._doConnectAbortController = null;
930
- this._unsettledConnectorOperations = /* @__PURE__ */ new Map();
931
- this._connectorIdleWaiters = /* @__PURE__ */ new Set();
932
- this._resetPromise = null;
933
- this._stateGeneration = 0;
934
- this._connectorTeardownTail = Promise.resolve();
935
- this._pendingConnectorTeardowns = 0;
936
- this._activeOperationJobs = /* @__PURE__ */ new Set();
937
- this._pendingOperationDisconnects = /* @__PURE__ */ new Set();
938
781
  this._installProgressLastEmittedValue = -Infinity;
939
782
  this.allNetworkGetAddress = createAllNetworkGetAddress({
940
783
  callChain: this.callChain.bind(this),
941
- getChainFingerprint: async (connectId, chain, context) => {
942
- try {
943
- const fingerprint = await this._computeChainFingerprint(
944
- chain,
945
- (method, params) => this.connectorCall(connectId, method, params, void 0, void 0, void 0, context)
946
- );
947
- return (0, import_hwk_adapter_core3.success)(fingerprint);
948
- } catch (error) {
949
- return this.errorToFailure(error);
950
- }
951
- },
952
- retainOperation: (operationId) => this._operations.retain(operationId),
953
- errorToFailure: (error) => this.errorToFailure(error),
954
- createCancelScope: (queueKey) => this._jobQueue.createCancelScope(queueKey)
784
+ getChainFingerprint: (connectId, chain) => this.getChainFingerprint(connectId, "", chain)
955
785
  });
956
786
  // ---------------------------------------------------------------------------
957
787
  // Private helpers
@@ -961,7 +791,7 @@ var _LedgerAdapter = class _LedgerAdapter {
961
791
  *
962
792
  * - If a session already exists for the given connectId, reuse it.
963
793
  * - If ANY session exists (Ledger IDs are ephemeral), reuse it.
964
- * - Otherwise: search → one USB device auto-connects; multiple asks the host to choose.
794
+ * - Otherwise: search → exactly 1 USB device auto-connects; multiple or none throws.
965
795
  */
966
796
  // Mutex for ensureConnected — prevents concurrent calls from establishing duplicate connections
967
797
  this._connectingPromise = null;
@@ -978,12 +808,6 @@ var _LedgerAdapter = class _LedgerAdapter {
978
808
  });
979
809
  };
980
810
  this.deviceDisconnectHandler = (data) => {
981
- const activeOperation = this._operations.findActiveByConnectionKey(data.connectId);
982
- if (activeOperation && this._activeOperationJobs.has(activeOperation.operationId)) {
983
- this._pendingOperationDisconnects.add(activeOperation.operationId);
984
- } else {
985
- this._operations.endByConnectionKey(data.connectId, "disconnect");
986
- }
987
811
  this._discoveredDevices.delete(data.connectId);
988
812
  this._sessions.delete(data.connectId);
989
813
  this.emitter.emit(import_hwk_adapter_core3.DEVICE.DISCONNECT, {
@@ -992,8 +816,10 @@ var _LedgerAdapter = class _LedgerAdapter {
992
816
  });
993
817
  };
994
818
  // Forward connector `ui-event` to the public hw.emitter so consumers only
995
- // need to subscribe in one place. The AppInstallProgress variant re-keys
996
- // sessionId to connectId via `_sessions`, and drops if no mapping exists.
819
+ // need to subscribe in one place. For the AppInstallProgress variant we
820
+ // re-key sessionId → connectId via the live _sessions map; if no mapping
821
+ // exists (race during teardown) we drop. All other variants pass through
822
+ // unchanged.
997
823
  this.uiEventForwarder = (event) => {
998
824
  if (event.type === import_hwk_adapter_core3.EConnectorInteraction.AppInstallProgress) {
999
825
  let connectId;
@@ -1038,34 +864,11 @@ var _LedgerAdapter = class _LedgerAdapter {
1038
864
  this._jobQueue = new import_hwk_adapter_core3.DeviceJobQueue();
1039
865
  this.registerEventListeners();
1040
866
  }
1041
- _discardPendingBinding(owner, status = "cancelled") {
1042
- const pending = owner?.pendingBinding;
1043
- if (!owner || !pending) return;
1044
- owner.pendingBinding = void 0;
1045
- if (pending.requestId === this._bindingSelectionRequestId) this._finishBleBinding(status);
1046
- }
1047
- _finishBleBinding(status) {
1048
- const selectionRequestId = this._bindingSelectionRequestId;
1049
- this._bindingSelectionRequestId = void 0;
1050
- if (selectionRequestId)
1051
- this.emitter.emit(import_hwk_adapter_core3.UI_REQUEST.DEVICE_BINDING_STATUS, {
1052
- type: import_hwk_adapter_core3.UI_REQUEST.DEVICE_BINDING_STATUS,
1053
- payload: { selectionRequestId, status }
1054
- });
1055
- }
1056
- _isBleConnection() {
1057
- return isLedgerBleConnectionType(this._activeConnectionType ?? this.connector.connectionType);
1058
- }
1059
867
  // Transport
1060
868
  get activeTransport() {
1061
- return this._isBleConnection() ? "ble" : "hid";
869
+ return isLedgerBleConnectionType(this.connector.connectionType) ? "ble" : "hid";
1062
870
  }
1063
871
  getAvailableTransports() {
1064
- if (this.connector.availableTransports) {
1065
- return this.connector.availableTransports.map(
1066
- (transport) => transport === "ble" ? "ble" : "hid"
1067
- );
1068
- }
1069
872
  return this.activeTransport ? [this.activeTransport] : [];
1070
873
  }
1071
874
  // Connector is bound at construction; switching requires a new adapter.
@@ -1082,42 +885,21 @@ var _LedgerAdapter = class _LedgerAdapter {
1082
885
  * The next operation will re-discover and re-connect automatically.
1083
886
  */
1084
887
  resetState() {
1085
- void this._resetStateAndDisconnectSessions();
1086
- }
1087
- _resetStateAndDisconnectSessions() {
1088
- if (this._resetPromise) return this._resetPromise;
1089
- const sessionIds = new Set(this._sessions.values());
1090
- this._stateGeneration += 1;
1091
- this._finishBleBinding("cancelled");
1092
- this._operations.endAll("runtime-reset");
1093
- this._doConnectAbortController?.abort();
1094
888
  this._discoveredDevices.clear();
1095
889
  this._sessions.clear();
1096
- this._sessionTransports.clear();
1097
- this._pendingOperationBindings.clear();
1098
- this._pendingPickBindings.clear();
1099
- this._selectedBleReconnectTargets.clear();
1100
890
  this._connectingPromise = null;
1101
- this._doConnectAbortController = null;
1102
891
  this._uiRegistry.reset();
1103
892
  this._jobQueue.clear();
1104
893
  this._btcHighIndexConfirmedThisSession = false;
1105
- const resetPromise = this._runConnectorTeardown(async () => {
1106
- for (const sessionId of sessionIds) {
1107
- await this.connector.disconnect(sessionId).catch(() => void 0);
1108
- }
1109
- });
1110
- this._resetPromise = resetPromise;
1111
- return resetPromise.finally(() => {
1112
- if (this._resetPromise === resetPromise) {
1113
- this._resetPromise = null;
1114
- }
1115
- });
1116
894
  }
1117
895
  async dispose() {
1118
- await this._resetStateAndDisconnectSessions();
896
+ this._uiRegistry.reset();
897
+ this._jobQueue.clear();
1119
898
  this.unregisterEventListeners();
1120
899
  this.connector.reset();
900
+ this._discoveredDevices.clear();
901
+ this._sessions.clear();
902
+ this._btcHighIndexConfirmedThisSession = false;
1121
903
  this.emitter.removeAllListeners();
1122
904
  }
1123
905
  uiResponse(response) {
@@ -1127,32 +909,17 @@ var _LedgerAdapter = class _LedgerAdapter {
1127
909
  // Device management
1128
910
  // ---------------------------------------------------------------------------
1129
911
  async searchDevices(options) {
1130
- return this._searchDevices(options);
1131
- }
1132
- async _searchDevices(options, signal) {
1133
912
  debugLog("[LedgerAdapter][REQ]", { method: "searchDevices", params: options });
1134
913
  try {
1135
914
  if (options?.resetSession) {
1136
- await this._resetStateAndDisconnectSessions();
1137
- } else {
1138
- await this._connectorTeardownTail;
1139
- }
1140
- await this._ensureDevicePermission(void 0, void 0, signal);
1141
- if (signal) _LedgerAdapter._throwIfAborted(signal);
1142
- const stateGeneration = this._stateGeneration;
1143
- const devices = await this.connector.searchDevices(
1144
- options?.transportType ? {
1145
- transportType: options.transportType,
1146
- waitForAll: options.waitForAllTransports
1147
- } : void 0
1148
- );
1149
- if (signal) _LedgerAdapter._throwIfAborted(signal);
1150
- if (stateGeneration !== this._stateGeneration) {
1151
- throw (0, import_hwk_adapter_core3.createHwkError)({
1152
- code: import_hwk_adapter_core3.HardwareErrorCode.UserAborted,
1153
- message: "Ledger discovery was reset"
1154
- });
915
+ this._doConnectAbortController?.abort();
916
+ this._sessions.clear();
917
+ this._connectingPromise = null;
918
+ this._doConnectAbortController = null;
919
+ this._btcHighIndexConfirmedThisSession = false;
1155
920
  }
921
+ await this._ensureDevicePermission();
922
+ const devices = await this.connector.searchDevices();
1156
923
  this._discoveredDevices.clear();
1157
924
  for (const d of devices) {
1158
925
  if (d.connectId) {
@@ -1160,7 +927,7 @@ var _LedgerAdapter = class _LedgerAdapter {
1160
927
  }
1161
928
  }
1162
929
  if (this._discoveredDevices.size === 0) {
1163
- await this._ensureDevicePermission(void 0, void 0, signal);
930
+ await this._ensureDevicePermission();
1164
931
  }
1165
932
  const result = Array.from(this._discoveredDevices.values());
1166
933
  debugLog("[LedgerAdapter][RES]", {
@@ -1179,280 +946,41 @@ var _LedgerAdapter = class _LedgerAdapter {
1179
946
  throw err;
1180
947
  }
1181
948
  }
1182
- async searchDeviceTargets(options) {
1183
- const devices = await this.searchDevices(options);
1184
- return devices.map((device) => ({
1185
- searchTargetId: device.connectId,
1186
- searchTargetReusePolicy: (0, import_hwk_adapter_core3.resolveSearchTargetReusePolicy)(device),
1187
- vendor: "ledger",
1188
- connectionType: device.connectionType,
1189
- kind: "physical",
1190
- label: device.label,
1191
- model: device.model,
1192
- modelName: device.modelName,
1193
- serialNumber: device.serialNumber
1194
- }));
1195
- }
1196
- async listConnectionTargets(options) {
1197
- const targets = await this.searchDeviceTargets(options);
1198
- return targets.map(({ searchTargetId, ...target }) => ({
1199
- ...target,
1200
- targetId: searchTargetId
1201
- }));
1202
- }
1203
949
  // USB single-session invariant: evict all sessions, best-effort (see connectDevice).
1204
- async _evictAllSessions(preserveOperationId) {
1205
- this._operations.endAll("explicit", preserveOperationId);
950
+ async _evictAllSessions() {
1206
951
  if (this._sessions.size === 0) return;
1207
952
  const stale = [...this._sessions.values()];
1208
953
  this._sessions.clear();
1209
- for (const sid of stale) this._sessionTransports.delete(sid);
1210
- await this._runConnectorTeardown(async () => {
1211
- for (const sid of stale) {
1212
- try {
1213
- await this.connector.disconnect(sid);
1214
- } catch {
1215
- }
954
+ for (const sid of stale) {
955
+ try {
956
+ await this.connector.disconnect(sid);
957
+ } catch {
1216
958
  }
1217
- });
959
+ }
1218
960
  }
1219
961
  static _createDeviceBusyError(method) {
1220
962
  return Object.assign(new Error(`Ledger device is busy while calling ${method}`), {
1221
963
  code: import_hwk_adapter_core3.HardwareErrorCode.DeviceBusy
1222
964
  });
1223
965
  }
1224
- async connectDevice(searchTargetId) {
1225
- try {
1226
- return await this._jobQueue.enqueue(
1227
- searchTargetId || "__ledger_connect__",
1228
- async (signal) => {
1229
- const connected = await this._connectTarget(searchTargetId, void 0, signal);
1230
- if (!connected.success) return connected;
1231
- return this._createOperation(searchTargetId, connected.payload);
1232
- },
1233
- {
1234
- label: "connectDevice",
1235
- rejectIfBusy: true,
1236
- busyError: _LedgerAdapter._createDeviceBusyError("connectDevice")
1237
- }
1238
- );
1239
- } catch (error) {
1240
- return this.errorToFailure(error);
1241
- }
1242
- }
1243
- /** Binds the picked BLE device; with a recorded wallet identity it must match first. */
1244
- async bindBleDevice(params) {
1245
- const identity = params.identity?.vendor === "ledger" ? params.identity : void 0;
1246
- try {
1247
- return await this._jobQueue.enqueue(
1248
- "__ledger_bind_ble__",
1249
- async (signal) => {
1250
- if (!this.getAvailableTransports().includes("ble")) {
1251
- throw (0, import_hwk_adapter_core3.createHwkError)({
1252
- code: import_hwk_adapter_core3.HardwareErrorCode.TransportNotAvailable,
1253
- message: "Ledger Bluetooth transport is not available"
1254
- });
1255
- }
1256
- this._activeConnectionType = "ble";
1257
- await this._ensureDevicePermission(void 0, void 0, signal);
1258
- const attempt = {
1259
- extra: params.extra,
1260
- bindingReason: "manual-rebind",
1261
- deferBindingSave: Boolean(identity)
1262
- };
1263
- try {
1264
- const connectId = await this._connectFirstOrSelect(
1265
- [],
1266
- void 0,
1267
- true,
1268
- void 0,
1269
- attempt,
1270
- signal
1271
- );
1272
- const pending = attempt.pendingBinding;
1273
- if (identity && pending) {
1274
- attempt.pendingBinding = void 0;
1275
- const sessionId = this._sessions.get(connectId);
1276
- if (!sessionId) {
1277
- throw (0, import_hwk_adapter_core3.createHwkError)({
1278
- code: import_hwk_adapter_core3.HardwareErrorCode.DeviceDisconnected,
1279
- message: "Selected Ledger connection ended"
1280
- });
1281
- }
1282
- const fingerprint = await this._computeChainFingerprint(
1283
- identity.chain,
1284
- (method, callParams) => this._runConnectorCall(
1285
- connectId,
1286
- method,
1287
- callParams,
1288
- signal,
1289
- void 0,
1290
- void 0,
1291
- { autoInstallApp: true },
1292
- { connection: { connectId, sessionId } }
1293
- )
1294
- );
1295
- if (fingerprint !== identity.value) {
1296
- await this._dropSession(connectId, signal);
1297
- throw (0, import_hwk_adapter_core3.createHwkError)({
1298
- code: import_hwk_adapter_core3.HardwareErrorCode.DeviceMismatch,
1299
- message: formatDeviceMismatchError(identity.value, fingerprint)
1300
- });
1301
- }
1302
- await this._saveSelectedBleBinding(
1303
- connectId,
1304
- pending.requestId,
1305
- attempt,
1306
- void 0,
1307
- signal,
1308
- identity
1309
- );
1310
- }
1311
- if (!attempt.bindingSaved) {
1312
- await this._dropSession(connectId, signal);
1313
- throw (0, import_hwk_adapter_core3.createHwkError)({
1314
- code: import_hwk_adapter_core3.HardwareErrorCode.UnknownError,
1315
- message: "Bluetooth binding could not be saved",
1316
- origin: "host"
1317
- });
1318
- }
1319
- return (0, import_hwk_adapter_core3.success)(connectId);
1320
- } catch (error) {
1321
- this._finishBleBinding(signal.aborted ? "cancelled" : "failed");
1322
- throw error;
1323
- }
1324
- },
1325
- {
1326
- label: "bindBleDevice",
1327
- rejectIfBusy: true,
1328
- busyError: _LedgerAdapter._createDeviceBusyError("bindBleDevice")
1329
- }
1330
- );
1331
- } catch (error) {
1332
- return this.errorToFailure(error);
1333
- }
1334
- }
1335
- async acquireOperation(connectId, context) {
1336
- try {
1337
- return await this._jobQueue.enqueue(
1338
- connectId || "__ledger_acquire__",
1339
- async (signal) => {
1340
- const transport = this._isBleConnection() ? "ble" : "usb";
1341
- const hint = context.knownConnections?.find(
1342
- (connection) => connection.transport === transport
1343
- );
1344
- const target = hint && hint.transport !== "qr" ? hint.connectId : connectId;
1345
- await this._ensureDevicePermission(target, void 0, signal);
1346
- const attempt = {
1347
- ...context,
1348
- extra: context.extra ? { ...context.extra } : void 0,
1349
- // The operation's calls decide whether the pick is the right wallet.
1350
- deferBindingSave: true
1351
- };
1352
- try {
1353
- const resolvedConnectId = await this.ensureConnected(
1354
- target,
1355
- signal,
1356
- true,
1357
- void 0,
1358
- attempt
1359
- );
1360
- _LedgerAdapter._throwIfAborted(signal);
1361
- const result = this._createOperation(connectId, resolvedConnectId);
1362
- if (result.success && attempt.pendingBinding) {
1363
- this._pendingOperationBindings.set(result.payload, attempt);
1364
- }
1365
- return result;
1366
- } catch (error) {
1367
- this._finishBleBinding(signal.aborted ? "cancelled" : "failed");
1368
- throw error;
1369
- }
1370
- },
1371
- {
1372
- label: "acquireOperation",
1373
- rejectIfBusy: true,
1374
- busyError: _LedgerAdapter._createDeviceBusyError("acquireOperation")
1375
- }
1376
- );
1377
- } catch (error) {
1378
- return this.errorToFailure(error);
1379
- }
1380
- }
1381
- _createOperation(searchTargetId, resolvedConnectId) {
1382
- this._operations.endByConnectionKey(resolvedConnectId, "explicit");
1383
- const sessionId = this._sessions.get(resolvedConnectId);
1384
- if (sessionId) this._operations.endByConnectionKey(sessionId, "explicit");
1385
- const connectionType = sessionId && this._sessionTransports.get(sessionId) || (this._isBleConnection() ? "ble" : "usb");
1386
- const device = this._discoveredDevices.get(resolvedConnectId) ?? {
1387
- vendor: "ledger",
1388
- model: "unknown",
1389
- firmwareVersion: "",
1390
- deviceId: "",
1391
- connectId: resolvedConnectId,
1392
- connectionType
1393
- };
1394
- const operation = this._operations.create({
1395
- searchTargetId,
1396
- connectId: resolvedConnectId,
1397
- device,
1398
- connectionType,
1399
- connectionKeys: [this._sessions.get(resolvedConnectId) ?? ""]
1400
- });
1401
- return (0, import_hwk_adapter_core3.success)(operation.operationId);
1402
- }
1403
- async _connectTarget(connectId, preserveOperationId, signal) {
966
+ async connectDevice(connectId) {
1404
967
  debugLog("[LedgerAdapter][REQ]", { method: "connectDevice", connectId, params: { connectId } });
1405
968
  try {
1406
- this._assertConnectorReady("connectDevice");
1407
- const discoveredType = this._discoveredDevices.get(connectId)?.connectionType;
1408
- if (discoveredType === "usb" || discoveredType === "ble") {
1409
- this._activeConnectionType = discoveredType;
1410
- }
1411
- if (this._isBleConnection() && !connectId) {
969
+ if (isLedgerBleConnectionType(this.connector.connectionType) && !connectId) {
1412
970
  throw Object.assign(new Error("Ledger BLE connectId is required."), {
1413
971
  code: import_hwk_adapter_core3.HardwareErrorCode.DeviceNotFound
1414
972
  });
1415
973
  }
1416
- await this._ensureDevicePermission(connectId, void 0, signal);
1417
- if (signal) _LedgerAdapter._throwIfAborted(signal);
1418
- const isMultiTransport = (this.connector.availableTransports?.length ?? 0) > 1;
1419
- if (!isMultiTransport && this._isBleConnection()) {
1420
- const previousSessionId = this._sessions.get(connectId);
1421
- this._operations.endByConnectionKey(connectId, "explicit", preserveOperationId);
1422
- if (previousSessionId) {
1423
- this._operations.endByConnectionKey(previousSessionId, "explicit", preserveOperationId);
1424
- this._sessions.delete(connectId);
1425
- await this.connector.disconnect(previousSessionId).catch(() => void 0);
1426
- }
1427
- } else {
1428
- await this._evictAllSessions(preserveOperationId);
1429
- }
1430
- if (signal) _LedgerAdapter._throwIfAborted(signal);
1431
- const stateGeneration = this._stateGeneration;
1432
- const releaseOperation = this._retainConnectorOperation(`connect:${connectId}`);
1433
- let session;
1434
- this._connectingConnectId = connectId;
1435
- try {
1436
- session = this.connector.availableTransports?.length ? await this.connector.connect(connectId, {
1437
- transportType: this._activeConnectionType ?? "usb"
1438
- }) : await this.connector.connect(connectId);
1439
- if (signal?.aborted || stateGeneration !== this._stateGeneration) {
1440
- await this.connector.disconnect(session.sessionId).catch(() => void 0);
1441
- throw Object.assign(new Error("Ledger connection aborted"), {
1442
- code: import_hwk_adapter_core3.HardwareErrorCode.UserAborted
1443
- });
1444
- }
1445
- } finally {
1446
- if (this._connectingConnectId === connectId) this._connectingConnectId = void 0;
1447
- releaseOperation();
974
+ if (!isLedgerBleConnectionType(this.connector.connectionType)) {
975
+ await this._evictAllSessions();
1448
976
  }
1449
- const resolvedConnectId = session.deviceInfo?.connectId || connectId;
1450
- this._sessions.set(resolvedConnectId, session.sessionId);
1451
- this._sessionTransports.set(session.sessionId, this._isBleConnection() ? "ble" : "usb");
977
+ await this._ensureDevicePermission(connectId);
978
+ const session = await this.connector.connect(connectId);
979
+ this._sessions.set(connectId, session.sessionId);
1452
980
  if (session.deviceInfo) {
1453
- this._discoveredDevices.set(resolvedConnectId, session.deviceInfo);
981
+ this._discoveredDevices.set(connectId, session.deviceInfo);
1454
982
  }
1455
- const result = (0, import_hwk_adapter_core3.success)(resolvedConnectId);
983
+ const result = (0, import_hwk_adapter_core3.success)(connectId);
1456
984
  debugLog("[LedgerAdapter][RES]", { method: "connectDevice", success: true, payload: result });
1457
985
  return result;
1458
986
  } catch (err) {
@@ -1465,60 +993,30 @@ var _LedgerAdapter = class _LedgerAdapter {
1465
993
  return failureResult;
1466
994
  }
1467
995
  }
1468
- async releaseOperation(operationId) {
1469
- const operation = this._operations.find(operationId);
1470
- if (!operation) {
1471
- this._operations.resolve(operationId);
1472
- return;
1473
- }
1474
- const endedOperation = this._operations.end(operationId, "explicit");
1475
- if (!endedOperation) return;
1476
- const { connectId } = operation;
996
+ async disconnectDevice(connectId) {
1477
997
  debugLog("[LedgerAdapter][REQ]", {
1478
- method: "releaseOperation",
998
+ method: "disconnectDevice",
1479
999
  connectId,
1480
1000
  params: { connectId }
1481
1001
  });
1482
1002
  try {
1483
- await this._releaseOperationConnection(endedOperation);
1484
- debugLog("[LedgerAdapter][RES]", { method: "releaseOperation", success: true });
1003
+ const sessionId = this._sessions.get(connectId);
1004
+ if (sessionId) {
1005
+ await this.connector.disconnect(sessionId);
1006
+ this._sessions.delete(connectId);
1007
+ }
1008
+ debugLog("[LedgerAdapter][RES]", { method: "disconnectDevice", success: true });
1485
1009
  } catch (err) {
1486
1010
  const e = err;
1487
1011
  debugLog("[LedgerAdapter][RES]", {
1488
- method: "releaseOperation",
1012
+ method: "disconnectDevice",
1489
1013
  success: false,
1490
1014
  error: { message: e?.message, _tag: e?._tag, code: e?.code ?? e?.errorCode }
1491
1015
  });
1492
1016
  throw err;
1493
1017
  }
1494
1018
  }
1495
- async _releaseOperationConnection(operation) {
1496
- const sessionIds = /* @__PURE__ */ new Set();
1497
- for (const [connectId, sessionId] of this._sessions) {
1498
- if (connectId === operation.connectId || operation.connectionKeys.includes(connectId) || operation.connectionKeys.includes(sessionId)) {
1499
- this._sessions.delete(connectId);
1500
- sessionIds.add(sessionId);
1501
- }
1502
- }
1503
- for (const sessionId of sessionIds) {
1504
- await this._runConnectorTeardown(
1505
- () => this.connector.disconnect(sessionId).catch(() => void 0)
1506
- );
1507
- }
1508
- }
1509
- async _releaseLostOperationConnection(operationId) {
1510
- const endedOperation = this._operations.end(operationId, "disconnect");
1511
- if (!endedOperation) return;
1512
- this._discoveredDevices.delete(endedOperation.connectId);
1513
- await this._releaseOperationConnection(endedOperation);
1514
- }
1515
- async getDeviceInfo(connectIdOrOperationId, deviceId) {
1516
- let connectId;
1517
- try {
1518
- connectId = (0, import_hwk_adapter_core3.isHardwareOperationId)(connectIdOrOperationId) ? this._operations.resolve(connectIdOrOperationId).connectId : connectIdOrOperationId;
1519
- } catch (error) {
1520
- return this.errorToFailure(error);
1521
- }
1019
+ async getDeviceInfo(connectId, deviceId) {
1522
1020
  debugLog("[LedgerAdapter][REQ]", {
1523
1021
  method: "getDeviceInfo",
1524
1022
  connectId,
@@ -1553,9 +1051,12 @@ var _LedgerAdapter = class _LedgerAdapter {
1553
1051
  success: false,
1554
1052
  error: { message: e?.message, _tag: e?._tag, code: e?.code ?? e?.errorCode }
1555
1053
  });
1556
- return this.errorToFailure(err);
1054
+ throw err;
1557
1055
  }
1558
1056
  }
1057
+ getSupportedChains() {
1058
+ return ["evm", "btc", "sol", "tron"];
1059
+ }
1559
1060
  // ---------------------------------------------------------------------------
1560
1061
  // Chain call helper
1561
1062
  // ---------------------------------------------------------------------------
@@ -1590,23 +1091,13 @@ var _LedgerAdapter = class _LedgerAdapter {
1590
1091
  if (params && typeof params === "object") {
1591
1092
  const {
1592
1093
  autoInstallApp,
1593
- operationId,
1594
1094
  passphraseState: _passphraseState,
1595
1095
  useEmptyPassphrase: _useEmptyPassphrase,
1596
- knownConnections,
1597
- extra,
1598
- allowDeviceSelection,
1599
- supportedTransports,
1600
1096
  ...rest
1601
1097
  } = params;
1602
1098
  return {
1603
1099
  commonParams: {
1604
- autoInstallApp: typeof autoInstallApp === "boolean" ? autoInstallApp : void 0,
1605
- operationId: typeof operationId === "string" ? operationId : void 0,
1606
- knownConnections,
1607
- extra,
1608
- allowDeviceSelection: typeof allowDeviceSelection === "boolean" ? allowDeviceSelection : void 0,
1609
- supportedTransports
1100
+ autoInstallApp: typeof autoInstallApp === "boolean" ? autoInstallApp : void 0
1610
1101
  },
1611
1102
  rest
1612
1103
  };
@@ -1782,28 +1273,9 @@ var _LedgerAdapter = class _LedgerAdapter {
1782
1273
  );
1783
1274
  }
1784
1275
  // ---------------------------------------------------------------------------
1785
- // Zcash chain methods (viewing key + shielded address reads; Zcash app >= 3.8.0)
1786
- // ---------------------------------------------------------------------------
1787
- zcashGetFullViewingKey(connectId, deviceId, params) {
1788
- return this.callChainWithMergedParams(
1789
- connectId,
1790
- deviceId,
1791
- "zcash",
1792
- "zcashGetFullViewingKey",
1793
- params
1794
- );
1795
- }
1796
- zcashGetShieldedAddress(connectId, deviceId, params) {
1797
- return this.callChainWithMergedParams(
1798
- connectId,
1799
- deviceId,
1800
- "zcash",
1801
- "zcashGetShieldedAddress",
1802
- params
1803
- );
1804
- }
1805
- // ---------------------------------------------------------------------------
1806
- // App management: OS-level app install/list, no fingerprint or chain dispatch.
1276
+ // App management — OS-level Ledger app install / list. Bypasses fingerprint
1277
+ // and chain-handler dispatch; installApp progress is forwarded to the adapter
1278
+ // emitter via 'ui-event' AppInstallProgress events.
1807
1279
  // ---------------------------------------------------------------------------
1808
1280
  async installApp(connectId, appName) {
1809
1281
  try {
@@ -1889,7 +1361,7 @@ var _LedgerAdapter = class _LedgerAdapter {
1889
1361
  );
1890
1362
  }
1891
1363
  await this.connector.configure({ ledgerGenuineCheckWebSocketUrl: relayUrl });
1892
- await this._resetStateAndDisconnectSessions();
1364
+ this.resetState();
1893
1365
  }
1894
1366
  const result = await this.connectorCall(connectId, "getDeviceGenuineCheck", {});
1895
1367
  if (!result.isGenuine) {
@@ -1915,10 +1387,10 @@ var _LedgerAdapter = class _LedgerAdapter {
1915
1387
  if (relayUrl) {
1916
1388
  try {
1917
1389
  await this.connector.configure?.({ ledgerGenuineCheckWebSocketUrl: void 0 });
1918
- await this._resetStateAndDisconnectSessions();
1390
+ this.resetState();
1919
1391
  } catch {
1920
1392
  this.connector.reset();
1921
- await this._resetStateAndDisconnectSessions();
1393
+ this.resetState();
1922
1394
  }
1923
1395
  }
1924
1396
  }
@@ -1930,20 +1402,6 @@ var _LedgerAdapter = class _LedgerAdapter {
1930
1402
  this.emitter.off(event, listener);
1931
1403
  }
1932
1404
  cancel(connectId) {
1933
- const namedOperationIsLive = (id) => {
1934
- try {
1935
- this._operations.resolve(id);
1936
- return true;
1937
- } catch {
1938
- return false;
1939
- }
1940
- };
1941
- if ((0, import_hwk_adapter_core3.isHardwareOperationId)(connectId) && !namedOperationIsLive(connectId)) {
1942
- debugLog("[LedgerAdapter] cancel target already ended; nothing to cancel", {
1943
- connectId
1944
- });
1945
- return;
1946
- }
1947
1405
  const userAbortReason = Object.assign(new Error("User aborted operation"), {
1948
1406
  code: import_hwk_adapter_core3.HardwareErrorCode.UserAborted,
1949
1407
  _tag: ERROR_TAG.UserAborted
@@ -1954,48 +1412,14 @@ var _LedgerAdapter = class _LedgerAdapter {
1954
1412
  this._lastCancelReason = void 0;
1955
1413
  }
1956
1414
  }, 2e3);
1957
- const activeJobId = this._jobQueue.getActiveJob()?.deviceId;
1958
- let operationId;
1959
- if ((0, import_hwk_adapter_core3.isHardwareOperationId)(connectId)) {
1960
- operationId = connectId;
1961
- } else if (!connectId && (0, import_hwk_adapter_core3.isHardwareOperationId)(activeJobId)) {
1962
- operationId = activeJobId;
1963
- }
1964
- let resolvedConnectId = connectId;
1965
- if (operationId) {
1966
- try {
1967
- resolvedConnectId = this._operations.resolve(operationId).connectId;
1968
- } catch {
1969
- resolvedConnectId = void 0;
1970
- }
1971
- }
1972
- const interactionForPhysicalId = !operationId && connectId ? this._operations.findActiveByConnectionKey(connectId) : void 0;
1973
- const pendingOperationId = operationId ?? interactionForPhysicalId?.operationId;
1974
- if (!connectId) {
1975
- this._uiRegistry.cancel();
1976
- } else if (pendingOperationId) {
1977
- this._uiRegistry.cancel(void 0, void 0, pendingOperationId);
1978
- }
1979
- this._finishBleBinding("cancelled");
1980
- if (!connectId) this._pendingOperationBindings.clear();
1981
- else if (pendingOperationId) this._pendingOperationBindings.delete(pendingOperationId);
1982
- const queueKeys = /* @__PURE__ */ new Set();
1983
- if (connectId) queueKeys.add(ledgerQueueKey({ connectId }));
1984
- if (pendingOperationId) queueKeys.add(ledgerQueueKey({ operationId: pendingOperationId }));
1985
- if (queueKeys.size) {
1986
- let cancelledAnyJob = false;
1987
- for (const key of queueKeys) {
1988
- cancelledAnyJob = this._jobQueue.cancelActiveAndPending(key, userAbortReason) || cancelledAnyJob;
1989
- }
1990
- debugLog("[LedgerAdapter] cancel routed to queue keys", {
1991
- queueKeys: [...queueKeys],
1992
- cancelledAnyJob
1993
- });
1415
+ this._uiRegistry.cancel();
1416
+ if (connectId) {
1417
+ this._jobQueue.cancelActiveAndPending(connectId, userAbortReason);
1994
1418
  } else {
1995
1419
  this._jobQueue.cancelActiveAndPending(void 0, userAbortReason);
1996
1420
  }
1997
- if (resolvedConnectId) {
1998
- const sessionId = this._sessions.get(resolvedConnectId) ?? resolvedConnectId;
1421
+ if (connectId) {
1422
+ const sessionId = this._sessions.get(connectId) ?? connectId;
1999
1423
  void this.connector.cancel(sessionId);
2000
1424
  } else {
2001
1425
  for (const sid of this._sessions.values()) void this.connector.cancel(sid);
@@ -2003,86 +1427,45 @@ var _LedgerAdapter = class _LedgerAdapter {
2003
1427
  if (this._connectingPromise) {
2004
1428
  this._doConnectAbortController?.abort(userAbortReason);
2005
1429
  }
2006
- const connectingConnectId = this._connectingConnectId;
2007
- if (connectingConnectId) void this.connector.cancel(connectingConnectId);
2008
1430
  }
2009
1431
  // ---------------------------------------------------------------------------
2010
1432
  // Chain fingerprint
2011
1433
  // ---------------------------------------------------------------------------
2012
- /** A non-empty deviceId is an expected chain fingerprint, not a transport identifier. */
2013
1434
  async getChainFingerprint(connectId, deviceId, chain) {
2014
1435
  try {
2015
1436
  const fingerprint = await this._computeChainFingerprint(
2016
1437
  chain,
2017
1438
  (method, params) => this.connectorCall(connectId, method, params, void 0, deviceId)
2018
1439
  );
2019
- if (deviceId) {
2020
- if (fingerprint !== deviceId) {
2021
- return (0, import_hwk_adapter_core3.failure)(
2022
- import_hwk_adapter_core3.HardwareErrorCode.DeviceMismatch,
2023
- formatDeviceMismatchError(deviceId, fingerprint)
2024
- );
2025
- }
2026
- }
2027
1440
  return (0, import_hwk_adapter_core3.success)(fingerprint);
2028
1441
  } catch (err) {
2029
1442
  debugError("[LedgerAdapter] getChainFingerprint error:", chain, err);
2030
1443
  return this.errorToFailure(err);
2031
1444
  }
2032
1445
  }
2033
- /** Saves a picked BLE endpoint; `identity` is set when its wallet was just checked. */
2034
- async _saveSelectedBleBinding(connectId, selectionRequestId, context, operationId, signal, identity) {
1446
+ /**
1447
+ * Verify fingerprint using an existing sessionId directly.
1448
+ * Safe to call inside connectorCall without causing queue deadlock.
1449
+ */
1450
+ async _verifyDeviceFingerprintWithSession(sessionId, deviceId, chain) {
1451
+ if (!deviceId) return { success: true };
2035
1452
  try {
2036
- const outcome = await (0, import_hwk_adapter_core3.requestSaveDeviceBinding)(
2037
- this.emitter,
2038
- this._uiRegistry,
2039
- {
2040
- selectionRequestId,
2041
- connection: { transport: "ble", connectId },
2042
- ...identity ? { identity } : {},
2043
- extra: context?.extra,
2044
- operationId
2045
- },
2046
- signal
1453
+ const fingerprint = await this._computeChainFingerprint(
1454
+ chain,
1455
+ (method, params) => this._callConnector(sessionId, method, params)
2047
1456
  );
2048
- if (context) context.bindingSaved = outcome.saved;
2049
- if (!outcome.saved) {
2050
- debugLog("[LedgerAdapter] BLE binding not persisted by host", {
2051
- connectId,
2052
- reason: outcome.reason
2053
- });
1457
+ if (fingerprint === deviceId) {
1458
+ return { success: true };
2054
1459
  }
2055
- } catch (error) {
2056
- await this._dropSession(connectId, signal);
2057
- throw error;
2058
- } finally {
2059
- if (this._bindingSelectionRequestId === selectionRequestId) {
2060
- this._bindingSelectionRequestId = void 0;
1460
+ return { success: false, expected: deviceId, actual: fingerprint };
1461
+ } catch (err) {
1462
+ const mapped = mapLedgerError(err);
1463
+ if (mapped.code === import_hwk_adapter_core3.HardwareErrorCode.WrongApp || mapped.code === import_hwk_adapter_core3.HardwareErrorCode.DeviceLocked) {
1464
+ return { success: true };
2061
1465
  }
1466
+ throw err;
2062
1467
  }
2063
1468
  }
2064
- async _dropSession(connectId, signal) {
2065
- const sessionId = this._sessions.get(connectId);
2066
- this._sessions.delete(connectId);
2067
- if (!sessionId) return;
2068
- this._sessionTransports.delete(sessionId);
2069
- const teardown = this._runConnectorTeardown(() => this.connector.disconnect(sessionId)).catch(
2070
- () => void 0
2071
- );
2072
- if (!signal.aborted) await teardown;
2073
- }
2074
- /** Verify on the acquired session without re-entering the job queue. */
2075
- async _verifyDeviceFingerprintWithSession(sessionId, deviceId, chain) {
2076
- if (!deviceId) return { success: true };
2077
- const fingerprint = await this._computeChainFingerprint(
2078
- chain,
2079
- (method, params) => this._callConnector(sessionId, method, params)
2080
- );
2081
- if (fingerprint === deviceId) {
2082
- return { success: true };
2083
- }
2084
- return { success: false, expected: deviceId, actual: fingerprint };
2085
- }
2086
1469
  /**
2087
1470
  * Compute the chain fingerprint via a caller-supplied call strategy.
2088
1471
  *
@@ -2110,23 +1493,11 @@ var _LedgerAdapter = class _LedgerAdapter {
2110
1493
  address = (await callMethod("solGetAddress", { path, showOnDevice: false })).address;
2111
1494
  } else if (chain === "tron") {
2112
1495
  address = (await callMethod("tronGetAddress", { path, showOnDevice: false })).address;
2113
- } else if (chain === "zcash") {
2114
- address = (await callMethod("zcashGetShieldedAddress", { path, showOnDevice: false })).address;
2115
1496
  } else {
2116
1497
  throw new Error(`Unsupported chain for fingerprint: ${chain}`);
2117
1498
  }
2118
1499
  return (0, import_hwk_adapter_core3.deriveDeviceFingerprint)(address);
2119
1500
  }
2120
- /**
2121
- * Operation owning the running job, so a mid-call UI request can name it.
2122
- * Undefined at cold start when no operation owns the job.
2123
- */
2124
- _activeOperationId() {
2125
- const activeJobId = this._jobQueue.getActiveJob()?.deviceId;
2126
- if (!activeJobId) return void 0;
2127
- if ((0, import_hwk_adapter_core3.isHardwareOperationId)(activeJobId)) return activeJobId;
2128
- return this._operations.findActiveByConnectionKey(activeJobId)?.operationId;
2129
- }
2130
1501
  // Ledger WebUSB won't expose a locked device, so we can't auto-detect unlock.
2131
1502
  // The user must press Confirm after unlocking, which triggers a search retry.
2132
1503
  // If `signal` is provided, an abort cancels the pending UI request so the
@@ -2136,18 +1507,15 @@ var _LedgerAdapter = class _LedgerAdapter {
2136
1507
  if (signal?.aborted) {
2137
1508
  _LedgerAdapter._throwIfAborted(signal);
2138
1509
  }
2139
- const operationId = this._activeOperationId();
2140
1510
  const waitPromise = this._uiRegistry.wait(
2141
- import_hwk_adapter_core3.UI_REQUEST.REQUEST_DEVICE_CONNECT,
2142
- { operationId }
1511
+ import_hwk_adapter_core3.UI_REQUEST.REQUEST_DEVICE_CONNECT
2143
1512
  );
2144
1513
  this.emitter.emit(import_hwk_adapter_core3.UI_REQUEST.REQUEST_DEVICE_CONNECT, {
2145
1514
  type: import_hwk_adapter_core3.UI_REQUEST.REQUEST_DEVICE_CONNECT,
2146
1515
  payload: {
2147
1516
  vendor: "ledger",
2148
1517
  reason: "device-not-found",
2149
- message: "Please connect and unlock your Ledger device",
2150
- operationId
1518
+ message: "Please connect and unlock your Ledger device"
2151
1519
  }
2152
1520
  });
2153
1521
  let payload;
@@ -2208,18 +1576,15 @@ var _LedgerAdapter = class _LedgerAdapter {
2208
1576
  return { ...params, showOnDevice: true };
2209
1577
  }
2210
1578
  async _waitForBtcHighIndexConfirm(path, accountIndex) {
2211
- const operationId = this._activeOperationId();
2212
1579
  const waitPromise = this._uiRegistry.wait(
2213
- import_hwk_adapter_core3.UI_REQUEST.REQUEST_BTC_HIGH_INDEX_CONFIRM,
2214
- { operationId }
1580
+ import_hwk_adapter_core3.UI_REQUEST.REQUEST_BTC_HIGH_INDEX_CONFIRM
2215
1581
  );
2216
1582
  this.emitter.emit(import_hwk_adapter_core3.UI_REQUEST.REQUEST_BTC_HIGH_INDEX_CONFIRM, {
2217
1583
  type: import_hwk_adapter_core3.UI_REQUEST.REQUEST_BTC_HIGH_INDEX_CONFIRM,
2218
1584
  payload: {
2219
1585
  vendor: "ledger",
2220
1586
  path,
2221
- accountIndex,
2222
- operationId
1587
+ accountIndex
2223
1588
  }
2224
1589
  });
2225
1590
  try {
@@ -2236,14 +1601,12 @@ var _LedgerAdapter = class _LedgerAdapter {
2236
1601
  // Ask the user whether to install a missing app (autoInstallApp flow).
2237
1602
  // Same register-then-emit ordering as the BTC high-index gate.
2238
1603
  async _waitForInstallAppConfirm(appName) {
2239
- const operationId = this._activeOperationId();
2240
1604
  const waitPromise = this._uiRegistry.wait(
2241
- import_hwk_adapter_core3.UI_REQUEST.REQUEST_INSTALL_APP,
2242
- { operationId }
1605
+ import_hwk_adapter_core3.UI_REQUEST.REQUEST_INSTALL_APP
2243
1606
  );
2244
1607
  this.emitter.emit(import_hwk_adapter_core3.UI_REQUEST.REQUEST_INSTALL_APP, {
2245
1608
  type: import_hwk_adapter_core3.UI_REQUEST.REQUEST_INSTALL_APP,
2246
- payload: { vendor: "ledger", appName, operationId }
1609
+ payload: { vendor: "ledger", appName }
2247
1610
  });
2248
1611
  try {
2249
1612
  const payload = await waitPromise;
@@ -2259,17 +1622,16 @@ var _LedgerAdapter = class _LedgerAdapter {
2259
1622
  // Layer 1 entry. Caller signal only races the outer awaiter; the shared
2260
1623
  // `_doConnect` runs under its own internal controller so caller A's cancel
2261
1624
  // doesn't kill caller B's await.
2262
- async ensureConnected(connectId, signal, allowUsbEphemeralFallback = false, preserveOperationId, context) {
1625
+ async ensureConnected(connectId, signal, allowUsbEphemeralFallback = false) {
2263
1626
  if (signal.aborted) _LedgerAdapter._throwIfAborted(signal);
2264
- this._assertConnectorReady("connectDevice");
2265
- if (this._isBleConnection() && !connectId && !allowUsbEphemeralFallback) {
1627
+ if (isLedgerBleConnectionType(this.connector.connectionType) && !connectId) {
2266
1628
  throw Object.assign(new Error("Ledger BLE connectId is required."), {
2267
1629
  code: import_hwk_adapter_core3.HardwareErrorCode.DeviceNotFound
2268
1630
  });
2269
1631
  }
2270
1632
  if (connectId && this._sessions.has(connectId)) return connectId;
2271
1633
  if (!connectId && this._sessions.size > 0) {
2272
- if (!this._isBleConnection() && this._sessions.size > 1) {
1634
+ if (!isLedgerBleConnectionType(this.connector.connectionType) && this._sessions.size > 1) {
2273
1635
  throw Object.assign(
2274
1636
  new Error(
2275
1637
  "Ledger USB session invariant violated: more than one session is active. Please reconnect the device."
@@ -2279,24 +1641,15 @@ var _LedgerAdapter = class _LedgerAdapter {
2279
1641
  }
2280
1642
  return this._sessions.keys().next().value;
2281
1643
  }
2282
- if (!this._connectingPromise || this._doConnectAbortController?.signal.aborted) {
2283
- const controller = new AbortController();
2284
- this._doConnectAbortController = controller;
2285
- const innerSignal = controller.signal;
1644
+ if (!this._connectingPromise) {
1645
+ this._doConnectAbortController = new AbortController();
1646
+ const innerSignal = this._doConnectAbortController.signal;
2286
1647
  this._connectingPromise = (async () => {
2287
1648
  try {
2288
- return await this._doConnect(
2289
- innerSignal,
2290
- connectId,
2291
- allowUsbEphemeralFallback,
2292
- preserveOperationId,
2293
- context
2294
- );
1649
+ return await this._doConnect(innerSignal, connectId, allowUsbEphemeralFallback);
2295
1650
  } finally {
2296
- if (this._doConnectAbortController === controller) {
2297
- this._connectingPromise = null;
2298
- this._doConnectAbortController = null;
2299
- }
1651
+ this._connectingPromise = null;
1652
+ this._doConnectAbortController = null;
2300
1653
  }
2301
1654
  })();
2302
1655
  }
@@ -2305,57 +1658,22 @@ var _LedgerAdapter = class _LedgerAdapter {
2305
1658
  // Layer 1 main loop — the ONLY place in SDK that emits unlock dialog.
2306
1659
  // Bounded by MAX_DOCONNECT_CONFIRMS — after N Confirms with no progress,
2307
1660
  // throw DeviceNotFound so the user is kicked out of the loop.
2308
- async _doConnect(internalSignal, targetConnectId, allowUsbEphemeralFallback = false, preserveOperationId, context) {
2309
- _LedgerAdapter._throwIfAborted(internalSignal);
2310
- if (this.connector.availableTransports?.includes("usb") && this.connector.availableTransports.includes("ble")) {
2311
- this._activeConnectionType = "usb";
2312
- const usbDevices = await this._searchDevices({ transportType: "usb" }, internalSignal);
2313
- _LedgerAdapter._throwIfAborted(internalSignal);
2314
- if (usbDevices.length > 0) {
2315
- const knownUsb = context?.knownConnections?.find(
2316
- (connection) => connection.transport === "usb"
2317
- );
2318
- const usbTarget = knownUsb?.transport === "usb" ? knownUsb.connectId : usbDevices.find((device) => device.connectId === targetConnectId)?.connectId;
2319
- return this._connectFirstOrSelect(
2320
- usbDevices,
2321
- usbTarget,
2322
- allowUsbEphemeralFallback,
2323
- preserveOperationId,
2324
- context,
2325
- internalSignal
2326
- );
2327
- }
2328
- if (context?.supportedTransports && !context.supportedTransports.includes("ble")) {
2329
- throw (0, import_hwk_adapter_core3.createHwkError)({
2330
- code: import_hwk_adapter_core3.HardwareErrorCode.DeviceNotFound,
2331
- message: "This Ledger model has no Bluetooth transport; connect it over USB"
2332
- });
2333
- }
2334
- this._activeConnectionType = "ble";
2335
- const knownBle = context?.knownConnections?.find(
2336
- (connection) => connection.transport === "ble"
2337
- );
2338
- if (knownBle?.transport === "ble") {
2339
- return this._connectDeviceOrThrow(knownBle.connectId, preserveOperationId, internalSignal);
2340
- }
2341
- if (targetConnectId && context?.knownConnections === void 0) {
2342
- throw (0, import_hwk_adapter_core3.createHwkError)({
2343
- code: import_hwk_adapter_core3.HardwareErrorCode.DeviceNotFound,
2344
- message: "Ledger connection metadata is required before starting Bluetooth binding"
2345
- });
1661
+ async _doConnect(internalSignal, targetConnectId, allowUsbEphemeralFallback = false) {
1662
+ if (isLedgerBleConnectionType(this.connector.connectionType) && targetConnectId) {
1663
+ try {
1664
+ return await this._connectDeviceOrThrow(targetConnectId);
1665
+ } catch (err) {
1666
+ if (!isDeviceLockedError(err) && !isDeviceNotAdvertisingError(err) && !isDeviceDisconnectedError(err)) {
1667
+ throw err;
1668
+ }
1669
+ this._discoveredDevices.delete(targetConnectId);
1670
+ if (isDeviceDisconnectedError(err)) {
1671
+ try {
1672
+ this.connector.reset?.();
1673
+ } catch {
1674
+ }
1675
+ }
2346
1676
  }
2347
- const bleDevices = await this._searchDevices({ transportType: "ble" }, internalSignal);
2348
- return this._connectFirstOrSelect(
2349
- bleDevices,
2350
- void 0,
2351
- allowUsbEphemeralFallback,
2352
- preserveOperationId,
2353
- context,
2354
- internalSignal
2355
- );
2356
- }
2357
- if (this._isBleConnection() && targetConnectId) {
2358
- return this._connectDeviceOrThrow(targetConnectId, preserveOperationId, internalSignal);
2359
1677
  }
2360
1678
  let confirms = 0;
2361
1679
  while (!internalSignal.aborted) {
@@ -2363,28 +1681,22 @@ var _LedgerAdapter = class _LedgerAdapter {
2363
1681
  type: import_hwk_adapter_core3.EConnectorInteraction.Searching,
2364
1682
  payload: { sessionId: "" }
2365
1683
  });
2366
- let devices = await this._searchDevices(void 0, internalSignal);
2367
- _LedgerAdapter._throwIfAborted(internalSignal);
1684
+ let devices = await this.searchDevices();
2368
1685
  if (devices.length === 0) {
2369
1686
  for (let i = 0; i < 3 && !internalSignal.aborted; i += 1) {
2370
1687
  await new Promise((resolve) => {
2371
1688
  setTimeout(resolve, import_hwk_adapter_core3.DEVICE_CONNECT_RETRY_DELAY_MS);
2372
1689
  });
2373
- _LedgerAdapter._throwIfAborted(internalSignal);
2374
- devices = await this._searchDevices(void 0, internalSignal);
2375
- _LedgerAdapter._throwIfAborted(internalSignal);
1690
+ devices = await this.searchDevices();
2376
1691
  if (devices.length > 0) break;
2377
1692
  }
2378
1693
  }
2379
- if (devices.length > 0 || this._isBleConnection() && allowUsbEphemeralFallback) {
1694
+ if (devices.length > 0) {
2380
1695
  try {
2381
1696
  return await this._connectFirstOrSelect(
2382
1697
  devices,
2383
1698
  targetConnectId,
2384
- allowUsbEphemeralFallback,
2385
- preserveOperationId,
2386
- context,
2387
- internalSignal
1699
+ allowUsbEphemeralFallback
2388
1700
  );
2389
1701
  } catch (err) {
2390
1702
  if (!isDeviceLockedError(err) && !isDeviceNotAdvertisingError(err) && !isDeviceDisconnectedError(err)) {
@@ -2413,147 +1725,45 @@ var _LedgerAdapter = class _LedgerAdapter {
2413
1725
  _LedgerAdapter._throwIfAborted(internalSignal);
2414
1726
  throw new Error("_doConnect aborted");
2415
1727
  }
2416
- async _connectFirstOrSelect(devices, targetConnectId, allowUsbEphemeralFallback, preserveOperationId, context, signal) {
2417
- _LedgerAdapter._throwIfAborted(signal);
1728
+ async _connectFirstOrSelect(devices, targetConnectId, allowUsbEphemeralFallback = false) {
2418
1729
  if (targetConnectId) {
2419
1730
  const target = devices.find(
2420
1731
  (d) => d.connectId === targetConnectId || d.deviceId === targetConnectId
2421
1732
  );
2422
1733
  if (target) {
2423
- return this._connectDeviceOrThrow(target.connectId, preserveOperationId, signal);
1734
+ return this._connectDeviceOrThrow(target.connectId);
2424
1735
  }
2425
- if (!this._isBleConnection() && devices.length === 1 && allowUsbEphemeralFallback) {
1736
+ if (!isLedgerBleConnectionType(this.connector.connectionType) && devices.length === 1 && allowUsbEphemeralFallback) {
2426
1737
  debugLog(
2427
1738
  `[LedgerAdapter] target ${targetConnectId} not in fresh enumeration; accepting sole USB device ${devices[0].connectId} for fingerprint-verified recovery`
2428
1739
  );
2429
- return this._connectDeviceOrThrow(devices[0].connectId, preserveOperationId, signal);
1740
+ return this._connectDeviceOrThrow(devices[0].connectId);
2430
1741
  }
2431
- if (!this._isBleConnection() || !allowUsbEphemeralFallback) {
2432
- const err = Object.assign(new Error(`Target Ledger unavailable: ${targetConnectId}`), {
2433
- code: import_hwk_adapter_core3.HardwareErrorCode.DeviceNotFound
2434
- });
2435
- if (this._isBleConnection()) {
2436
- err._tag = ERROR_TAG.DeviceNotAdvertising;
2437
- }
2438
- throw err;
1742
+ const err = Object.assign(new Error(`Target Ledger unavailable: ${targetConnectId}`), {
1743
+ code: import_hwk_adapter_core3.HardwareErrorCode.DeviceNotFound
1744
+ });
1745
+ if (isLedgerBleConnectionType(this.connector.connectionType)) {
1746
+ err._tag = ERROR_TAG.DeviceNotAdvertising;
2439
1747
  }
1748
+ throw err;
2440
1749
  }
2441
- const requiresBleSelection = this._isBleConnection();
2442
- if (requiresBleSelection && !allowUsbEphemeralFallback) {
1750
+ if (isLedgerBleConnectionType(this.connector.connectionType)) {
2443
1751
  throw Object.assign(new Error("Ledger BLE connectId is required."), {
2444
1752
  code: import_hwk_adapter_core3.HardwareErrorCode.DeviceNotFound
2445
1753
  });
2446
1754
  }
2447
- if (requiresBleSelection && context?.allowDeviceSelection !== false) {
2448
- const bindingSessionId = context?.bindingSessionId ?? this._uiRegistry.createRequestId();
2449
- if (context) context.bindingSessionId = bindingSessionId;
2450
- const allowUsbFallback = context?.bindingReason !== "manual-rebind" && Boolean(this.connector.availableTransports?.includes("usb"));
2451
- const knownUsb = context?.knownConnections?.find(
2452
- (connection) => connection.transport === "usb"
2453
- );
2454
- const usbConnectId = knownUsb?.transport === "usb" ? knownUsb.connectId : targetConnectId;
2455
- const { device, requestId } = await (0, import_hwk_adapter_core3.requestBleDeviceSelection)({
2456
- emitter: this.emitter,
2457
- registry: this._uiRegistry,
2458
- signal,
2459
- allowUsbFallback,
2460
- scan: async () => {
2461
- if (allowUsbFallback) {
2462
- const usbDevices = await this._searchDevices({ transportType: "usb" }, signal);
2463
- const candidate = usbDevices.find(
2464
- (device2) => device2.connectionType === "usb" && device2.connectId === usbConnectId
2465
- ) ?? (usbDevices.length === 1 && usbDevices[0].connectionType === "usb" ? usbDevices[0] : void 0);
2466
- if (candidate) return [candidate];
2467
- }
2468
- return this._searchDevices({ transportType: "ble", waitForAllTransports: true }, signal);
2469
- },
2470
- request: {
2471
- devices,
2472
- bindingSessionId,
2473
- context: {
2474
- kind: "bind-connection",
2475
- transport: "ble",
2476
- reason: context?.bindingReason ?? (targetConnectId ? "known-connection-unavailable" : "missing-binding")
2477
- },
2478
- extra: context?.extra,
2479
- operationId: preserveOperationId
2480
- }
2481
- });
2482
- if (device.connectionType === "usb") {
2483
- this._activeConnectionType = "usb";
2484
- if (context) context.selectedConnection = void 0;
2485
- this._bindingSelectionRequestId = requestId;
2486
- this._finishBleBinding("cancelled");
2487
- return this._connectDeviceOrThrow(device.connectId, preserveOperationId, signal);
2488
- }
2489
- this._bindingSelectionRequestId = requestId;
2490
- const connected = await this._connectDeviceOrThrow(
2491
- device.connectId,
2492
- preserveOperationId,
2493
- signal
2494
- );
2495
- if (context) context.selectedConnection = { connectId: connected, requestId };
2496
- if (context?.deferBindingSave) {
2497
- context.pendingBinding = { connectId: connected, requestId };
2498
- } else {
2499
- await this._saveSelectedBleBinding(
2500
- connected,
2501
- requestId,
2502
- context,
2503
- preserveOperationId,
2504
- signal
2505
- );
2506
- }
2507
- return connected;
2508
- }
2509
- if (devices.length > 0 && (devices.length > 1 || requiresBleSelection)) {
2510
- if (context?.allowDeviceSelection === false || !this.emitter.listenerCount(import_hwk_adapter_core3.UI_REQUEST.REQUEST_SELECT_DEVICE)) {
2511
- throw (0, import_hwk_adapter_core3.createHwkError)({
2512
- code: import_hwk_adapter_core3.HardwareErrorCode.DeviceNotFound,
2513
- message: "Select a Ledger device before continuing"
2514
- });
2515
- }
2516
- const requestId = this._uiRegistry.createRequestId();
2517
- const operationId = this._activeOperationId();
2518
- const waitPromise = this._uiRegistry.wait(
2519
- import_hwk_adapter_core3.UI_REQUEST.REQUEST_SELECT_DEVICE,
2520
- { requestId, operationId }
2521
- );
2522
- this.emitter.emit(import_hwk_adapter_core3.UI_REQUEST.REQUEST_SELECT_DEVICE, {
2523
- type: import_hwk_adapter_core3.UI_REQUEST.REQUEST_SELECT_DEVICE,
2524
- payload: {
2525
- devices,
2526
- requestId,
2527
- operationId,
2528
- context: requiresBleSelection ? {
2529
- kind: "bind-connection",
2530
- transport: "ble",
2531
- reason: targetConnectId ? "known-connection-unavailable" : "missing-binding"
2532
- } : { kind: "select-device", transport: "usb", reason: "multiple-candidates" },
2533
- extra: context?.extra
2534
- }
2535
- });
2536
- const { sdkConnectId } = await this._abortable(signal, waitPromise);
2537
- _LedgerAdapter._throwIfAborted(signal);
2538
- const selected = devices.find((device) => device.connectId === sdkConnectId);
2539
- if (!selected) {
2540
- throw Object.assign(new Error("Selected Ledger is no longer available"), {
2541
- code: import_hwk_adapter_core3.HardwareErrorCode.DeviceNotFound
2542
- });
2543
- }
2544
- if (context && requiresBleSelection)
2545
- context.selectedConnection = { connectId: selected.connectId, requestId };
2546
- return this._connectDeviceOrThrow(selected.connectId, preserveOperationId, signal);
1755
+ if (devices.length > 1) {
1756
+ throw createMultipleUsbLedgerDevicesError();
2547
1757
  }
2548
1758
  if (devices.length !== 1) {
2549
1759
  throw Object.assign(new Error("Ledger device not found."), {
2550
1760
  code: import_hwk_adapter_core3.HardwareErrorCode.DeviceNotFound
2551
1761
  });
2552
1762
  }
2553
- return this._connectDeviceOrThrow(devices[0].connectId, preserveOperationId, signal);
1763
+ return this._connectDeviceOrThrow(devices[0].connectId);
2554
1764
  }
2555
- async _connectDeviceOrThrow(chosenConnectId, preserveOperationId, signal) {
2556
- const result = await this._connectTarget(chosenConnectId, preserveOperationId, signal);
1765
+ async _connectDeviceOrThrow(chosenConnectId) {
1766
+ const result = await this.connectDevice(chosenConnectId);
2557
1767
  if (!result.success) {
2558
1768
  const payload = result.payload;
2559
1769
  const rethrow = Object.assign(new Error(payload.error), {
@@ -2564,7 +1774,7 @@ var _LedgerAdapter = class _LedgerAdapter {
2564
1774
  }
2565
1775
  throw rethrow;
2566
1776
  }
2567
- return result.payload;
1777
+ return chosenConnectId;
2568
1778
  }
2569
1779
  /**
2570
1780
  * Call the connector with automatic session resolution and disconnect retry.
@@ -2594,139 +1804,33 @@ var _LedgerAdapter = class _LedgerAdapter {
2594
1804
  * lives in one place.
2595
1805
  */
2596
1806
  async _callConnector(sessionId, method, params, signal) {
2597
- this._assertConnectorReady(method);
2598
- if (signal?.aborted) throw this._abortReason(signal);
2599
- const releaseOperation = this._retainConnectorOperation(`call:${sessionId}`);
2600
- let promise;
2601
- try {
2602
- promise = this.connector.call(sessionId, method, params).finally(releaseOperation);
2603
- } catch (error) {
2604
- releaseOperation();
2605
- throw error;
2606
- }
1807
+ const promise = this.connector.call(sessionId, method, params);
2607
1808
  const result = signal ? await this._abortable(signal, promise) : await promise;
2608
1809
  return this._unwrapConnectorResult(result);
2609
1810
  }
2610
- _assertConnectorReady(method) {
2611
- if (this._resetPromise || this._pendingConnectorTeardowns > 0 || this._unsettledConnectorOperations.size > 0) {
2612
- throw _LedgerAdapter._createDeviceBusyError(method);
2613
- }
2614
- }
2615
- _runConnectorTeardown(task) {
2616
- const previous = this._connectorTeardownTail;
2617
- let releaseTail = () => void 0;
2618
- this._connectorTeardownTail = new Promise((resolve) => {
2619
- releaseTail = resolve;
2620
- });
2621
- this._pendingConnectorTeardowns += 1;
2622
- return (async () => {
2623
- try {
2624
- await previous;
2625
- await this._waitForConnectorOperationsToDrain();
2626
- await task();
2627
- } finally {
2628
- this._pendingConnectorTeardowns -= 1;
2629
- releaseTail();
2630
- }
2631
- })();
2632
- }
2633
- _waitForConnectorOperationsToDrain() {
2634
- if (this._unsettledConnectorOperations.size === 0) {
2635
- return Promise.resolve();
2636
- }
2637
- return new Promise((resolve) => {
2638
- this._connectorIdleWaiters.add(resolve);
2639
- });
2640
- }
2641
- _retainConnectorOperation(key) {
2642
- this._unsettledConnectorOperations.set(
2643
- key,
2644
- (this._unsettledConnectorOperations.get(key) ?? 0) + 1
2645
- );
2646
- let released = false;
2647
- return () => {
2648
- if (released) return;
2649
- released = true;
2650
- const remaining = (this._unsettledConnectorOperations.get(key) ?? 1) - 1;
2651
- if (remaining > 0) {
2652
- this._unsettledConnectorOperations.set(key, remaining);
2653
- } else {
2654
- this._unsettledConnectorOperations.delete(key);
2655
- }
2656
- if (this._unsettledConnectorOperations.size === 0) {
2657
- for (const resolve of this._connectorIdleWaiters) resolve();
2658
- this._connectorIdleWaiters.clear();
2659
- }
2660
- };
2661
- }
2662
1811
  async connectorCall(connectId, method, params, fingerprint, permissionDeviceId, commonParams, installContext) {
2663
- const positionalOperationId = (0, import_hwk_adapter_core3.isHardwareOperationId)(connectId) ? connectId : void 0;
2664
- if (positionalOperationId && commonParams?.operationId && positionalOperationId !== commonParams.operationId) {
2665
- throw (0, import_hwk_adapter_core3.createHwkError)({
2666
- code: import_hwk_adapter_core3.HardwareErrorCode.InvalidParams,
2667
- message: "Conflicting Ledger operation ids",
2668
- params: {
2669
- positionalOperationId,
2670
- commonOperationId: commonParams.operationId
2671
- }
2672
- });
2673
- }
2674
- const operationId = commonParams?.operationId ?? positionalOperationId;
2675
- const operation = operationId ? this._operations.resolve(operationId) : void 0;
2676
- const releaseOperationRetention = operationId ? this._operations.retain(operationId) : void 0;
2677
- const effectiveConnectId = operation?.connectId ?? connectId;
2678
- debugLog("[LedgerAdapter][REQ]", {
2679
- method,
2680
- connectId: effectiveConnectId || "(empty)",
2681
- params
2682
- });
2683
- const queueKey = ledgerQueueKey({ operationId, connectId: effectiveConnectId });
1812
+ debugLog("[LedgerAdapter][REQ]", { method, connectId: connectId || "(empty)", params });
1813
+ const queueKey = connectId || "__ledger_default__";
2684
1814
  try {
2685
1815
  const result = await this._jobQueue.enqueue(
2686
1816
  queueKey,
2687
- async (signal) => {
2688
- if (operationId) this._activeOperationJobs.add(operationId);
2689
- try {
2690
- return await this._runConnectorCall(
2691
- effectiveConnectId,
2692
- method,
2693
- params,
2694
- signal,
2695
- fingerprint,
2696
- permissionDeviceId,
2697
- commonParams,
2698
- installContext ?? {},
2699
- operationId
2700
- );
2701
- } catch (error) {
2702
- const status = signal.aborted ? "cancelled" : "failed";
2703
- for (const [pickedConnectId, owner] of this._pendingPickBindings) {
2704
- this._pendingPickBindings.delete(pickedConnectId);
2705
- this._discardPendingBinding(owner, status);
2706
- await this._dropSession(pickedConnectId, signal);
2707
- }
2708
- this._finishBleBinding(status);
2709
- throw error;
2710
- } finally {
2711
- if (operationId) {
2712
- this._activeOperationJobs.delete(operationId);
2713
- if (this._pendingOperationDisconnects.delete(operationId)) {
2714
- await this._releaseLostOperationConnection(operationId);
2715
- }
2716
- }
2717
- }
2718
- },
1817
+ async (signal) => this._runConnectorCall(
1818
+ connectId,
1819
+ method,
1820
+ params,
1821
+ signal,
1822
+ fingerprint,
1823
+ permissionDeviceId,
1824
+ commonParams,
1825
+ installContext
1826
+ ),
2719
1827
  {
2720
1828
  label: method,
2721
1829
  rejectIfBusy: true,
2722
1830
  busyError: _LedgerAdapter._createDeviceBusyError(method)
2723
1831
  }
2724
1832
  );
2725
- debugLog("[LedgerAdapter][RES]", {
2726
- method,
2727
- success: true,
2728
- payload: redactResultForLog(result)
2729
- });
1833
+ debugLog("[LedgerAdapter][RES]", { method, success: true, payload: result });
2730
1834
  return result;
2731
1835
  } catch (err) {
2732
1836
  const e = err;
@@ -2740,22 +1844,15 @@ var _LedgerAdapter = class _LedgerAdapter {
2740
1844
  }
2741
1845
  });
2742
1846
  throw err;
2743
- } finally {
2744
- releaseOperationRetention?.();
2745
1847
  }
2746
1848
  }
2747
- /** Hermes/RN polyfills don't always populate signal.reason; fall back. */
2748
- _abortReason(signal) {
2749
- return signal.reason ?? this._lastCancelReason ?? new Error("Aborted");
2750
- }
2751
1849
  /**
2752
1850
  * Race a promise against an abort signal. On abort, rejects with
2753
1851
  * signal.reason → instance _lastCancelReason → generic Error('Aborted').
2754
1852
  */
2755
1853
  _abortable(signal, promise) {
2756
- const getAbortReason = () => this._abortReason(signal);
1854
+ const getAbortReason = () => signal.reason ?? this._lastCancelReason ?? new Error("Aborted");
2757
1855
  if (signal.aborted) {
2758
- void promise.catch(() => void 0);
2759
1856
  return Promise.reject(getAbortReason());
2760
1857
  }
2761
1858
  return new Promise((resolve, reject) => {
@@ -2782,15 +1879,13 @@ var _LedgerAdapter = class _LedgerAdapter {
2782
1879
  }
2783
1880
  }
2784
1881
  /** Actual work done under the job queue — connection, fingerprint, call, and recovery. */
2785
- async _runConnectorCall(connectId, method, params, signal, fingerprint, permissionDeviceId, commonParams, installContext, operationId, lockedRetryBudget = _LedgerAdapter.MAX_BUSINESS_RETRY_BUDGET) {
1882
+ async _runConnectorCall(connectId, method, params, signal, fingerprint, permissionDeviceId, commonParams, installContext) {
2786
1883
  _LedgerAdapter._throwIfAborted(signal);
2787
- if (!operationId) {
2788
- await this._ensureDevicePermission(
2789
- connectId,
2790
- permissionDeviceId ?? fingerprint?.deviceId,
2791
- signal
2792
- );
2793
- }
1884
+ await this._ensureDevicePermission(
1885
+ connectId,
1886
+ permissionDeviceId ?? fingerprint?.deviceId,
1887
+ signal
1888
+ );
2794
1889
  _LedgerAdapter._throwIfAborted(signal);
2795
1890
  let effectiveParams = params;
2796
1891
  if (method === "btcGetPublicKey") {
@@ -2804,60 +1899,19 @@ var _LedgerAdapter = class _LedgerAdapter {
2804
1899
  effectiveParams = gatedParams;
2805
1900
  }
2806
1901
  const allowUsbEphemeralFallback = !!fingerprint?.deviceId && !fingerprint.skipFingerprint;
2807
- let businessCallStarted = false;
2808
- const selectedBleTarget = connectId ? this._selectedBleReconnectTargets.get(connectId) : void 0;
2809
- const knownTransport = this._isBleConnection() ? "ble" : "usb";
2810
- const hintedConnectId = commonParams?.knownConnections?.find(
2811
- (connection) => connection.transport === knownTransport
2812
- );
2813
- const inputConnectId = hintedConnectId && hintedConnectId.transport !== "qr" ? hintedConnectId.connectId : connectId;
2814
- const preferredConnectId = fingerprint && !fingerprint.skipFingerprint && selectedBleTarget?.chain === fingerprint.chain && selectedBleTarget.fingerprint === fingerprint.deviceId ? selectedBleTarget.connectId : inputConnectId;
2815
- const checksWallet = Boolean(
2816
- fingerprint && !fingerprint.skipFingerprint && fingerprint.deviceId
2817
- );
2818
- const connectionAttempt = {
2819
- ...commonParams,
2820
- deferBindingSave: checksWallet
2821
- };
2822
- const bundleConnection = installContext?.connection;
2823
- if (bundleConnection && this._sessions.get(bundleConnection.connectId) !== bundleConnection.sessionId) {
2824
- throw (0, import_hwk_adapter_core3.createHwkError)({
2825
- code: import_hwk_adapter_core3.HardwareErrorCode.DeviceDisconnected,
2826
- message: "Ledger all-network connection ended"
2827
- });
2828
- }
2829
- const resolvedConnectId = operationId ? this._operations.resolve(operationId).connectId : bundleConnection?.connectId ?? await this.ensureConnected(
2830
- preferredConnectId,
1902
+ const resolvedConnectId = await this.ensureConnected(
1903
+ connectId,
2831
1904
  signal,
2832
- allowUsbEphemeralFallback,
2833
- void 0,
2834
- connectionAttempt
1905
+ allowUsbEphemeralFallback
2835
1906
  );
2836
1907
  const sessionId = this._sessions.get(resolvedConnectId);
2837
- if (sessionId && installContext && !installContext.connection) {
2838
- installContext.connection = { connectId: resolvedConnectId, sessionId };
2839
- }
2840
1908
  if (!sessionId) {
2841
- if (operationId) {
2842
- this._operations.end(operationId, "disconnect");
2843
- throw (0, import_hwk_adapter_core3.createHwkError)({
2844
- code: import_hwk_adapter_core3.HardwareErrorCode.OperationEnded,
2845
- message: "Ledger operation connection is no longer active",
2846
- params: { operationId, reason: "disconnect" }
2847
- });
2848
- }
2849
1909
  throw Object.assign(new Error("Auto-connect succeeded but no session found"), {
2850
1910
  _tag: ERROR_TAG.DeviceSessionNotFound
2851
1911
  });
2852
1912
  }
2853
1913
  try {
2854
- const isBleSession = this._sessionTransports.get(sessionId) === "ble";
2855
- if (!operationId && connectionAttempt.pendingBinding) {
2856
- this._pendingPickBindings.set(resolvedConnectId, connectionAttempt);
2857
- }
2858
- const bindingOwner = operationId ? this._pendingOperationBindings.get(operationId) : this._pendingPickBindings.get(resolvedConnectId);
2859
- const pendingBinding = bindingOwner?.pendingBinding?.connectId === resolvedConnectId ? bindingOwner.pendingBinding : void 0;
2860
- if (fingerprint && checksWallet) {
1914
+ if (fingerprint && !fingerprint.skipFingerprint && fingerprint.deviceId) {
2861
1915
  const fp = await this._abortable(
2862
1916
  signal,
2863
1917
  this._verifyDeviceFingerprintWithSession(
@@ -2867,65 +1921,14 @@ var _LedgerAdapter = class _LedgerAdapter {
2867
1921
  )
2868
1922
  );
2869
1923
  if (!fp.success) {
2870
- if (pendingBinding) {
2871
- this._discardPendingBinding(bindingOwner, "failed");
2872
- this._pendingPickBindings.delete(resolvedConnectId);
2873
- if (operationId) {
2874
- this._pendingOperationBindings.delete(operationId);
2875
- this._operations.end(operationId, "explicit");
2876
- await this._dropSession(resolvedConnectId, signal);
2877
- }
2878
- }
2879
1924
  throw Object.assign(new Error(formatDeviceMismatchError(fp.expected, fp.actual)), {
2880
1925
  code: import_hwk_adapter_core3.HardwareErrorCode.DeviceMismatch
2881
1926
  });
2882
1927
  }
2883
1928
  }
2884
- if (pendingBinding && bindingOwner) {
2885
- bindingOwner.pendingBinding = void 0;
2886
- if (operationId) this._pendingOperationBindings.delete(operationId);
2887
- else this._pendingPickBindings.delete(resolvedConnectId);
2888
- await this._saveSelectedBleBinding(
2889
- pendingBinding.connectId,
2890
- pendingBinding.requestId,
2891
- bindingOwner,
2892
- operationId,
2893
- signal,
2894
- fingerprint && checksWallet ? {
2895
- vendor: "ledger",
2896
- type: "chainFingerprint",
2897
- chain: fingerprint.chain,
2898
- value: fingerprint.deviceId
2899
- } : void 0
2900
- );
2901
- }
2902
- if (fingerprint && !fingerprint.skipFingerprint && fingerprint.deviceId && !operationId && isBleSession && connectionAttempt.selectedConnection?.connectId === resolvedConnectId) {
2903
- this._selectedBleReconnectTargets.set(connectId, {
2904
- connectId: resolvedConnectId,
2905
- chain: fingerprint.chain,
2906
- fingerprint: fingerprint.deviceId
2907
- });
2908
- }
2909
- businessCallStarted = true;
2910
1929
  return await this._callConnector(sessionId, method, effectiveParams, signal);
2911
1930
  } catch (err) {
2912
1931
  if (signal.aborted) throw err;
2913
- if (isLostConnectionError(err)) {
2914
- this._discoveredDevices.delete(resolvedConnectId);
2915
- if (operationId) await this._releaseLostOperationConnection(operationId);
2916
- else {
2917
- this._sessions.delete(resolvedConnectId);
2918
- await this.connector.disconnect(sessionId).catch(() => void 0);
2919
- }
2920
- const ambiguous = businessCallStarted && !(0, import_hwk_adapter_core3.canReplayHardwareMethodAfterTransportFailure)(method);
2921
- const interactionParams = operationId ? { operationId, reason: "disconnect" } : void 0;
2922
- throw (0, import_hwk_adapter_core3.createHwkError)({
2923
- code: operationId ? import_hwk_adapter_core3.HardwareErrorCode.OperationEnded : mapLedgerError(err).code,
2924
- message: "Ledger operation connection was lost; start a new operation",
2925
- params: ambiguous ? (0, import_hwk_adapter_core3.operationMayHaveCompletedParams)(method, { operationId }) : interactionParams,
2926
- recovery: ambiguous ? { scope: "unknown" } : void 0
2927
- });
2928
- }
2929
1932
  const errObj = err;
2930
1933
  debugLog("[LedgerAdapter] connectorCall error:", method, {
2931
1934
  message: errObj?.message,
@@ -2937,61 +1940,6 @@ var _LedgerAdapter = class _LedgerAdapter {
2937
1940
  isNotAdvertising: isDeviceNotAdvertisingError(err),
2938
1941
  isStuckApp: isStuckAppStateError(err)
2939
1942
  });
2940
- const assertSessionCurrent = (message) => {
2941
- if (this._sessions.get(resolvedConnectId) === sessionId) return;
2942
- throw (0, import_hwk_adapter_core3.createHwkError)({
2943
- code: operationId ? import_hwk_adapter_core3.HardwareErrorCode.OperationEnded : import_hwk_adapter_core3.HardwareErrorCode.DeviceDisconnected,
2944
- message
2945
- });
2946
- };
2947
- if ((isDeviceLockedError(err) || errObj?.code === import_hwk_adapter_core3.HardwareErrorCode.DeviceLocked) && lockedRetryBudget > 0) {
2948
- await this._waitForDeviceConnect(signal);
2949
- assertSessionCurrent("Ledger connection ended while waiting for unlock");
2950
- return this._runConnectorCall(
2951
- resolvedConnectId,
2952
- method,
2953
- effectiveParams,
2954
- signal,
2955
- fingerprint,
2956
- permissionDeviceId,
2957
- commonParams,
2958
- installContext,
2959
- operationId,
2960
- lockedRetryBudget - 1
2961
- );
2962
- }
2963
- if (businessCallStarted && isStuckAppStateError(err)) {
2964
- await this._sleepAbortable(_LedgerAdapter.STUCK_APP_RETRY_DELAY_MS, signal);
2965
- assertSessionCurrent("Ledger connection ended during the app transition");
2966
- try {
2967
- return await this._callConnector(sessionId, method, effectiveParams, signal);
2968
- } catch (retryErr) {
2969
- if (isStuckAppStateError(retryErr)) throw err;
2970
- if (isLostConnectionError(retryErr)) {
2971
- if (operationId) await this._releaseLostOperationConnection(operationId);
2972
- else {
2973
- this._sessions.delete(resolvedConnectId);
2974
- this._discoveredDevices.delete(resolvedConnectId);
2975
- await this.connector.disconnect(sessionId).catch(() => void 0);
2976
- }
2977
- const ambiguous = !(0, import_hwk_adapter_core3.canReplayHardwareMethodAfterTransportFailure)(method);
2978
- throw (0, import_hwk_adapter_core3.createHwkError)({
2979
- code: operationId ? import_hwk_adapter_core3.HardwareErrorCode.OperationEnded : mapLedgerError(retryErr).code,
2980
- message: `Ledger ${method} may have completed before the connection was lost`,
2981
- params: ambiguous ? (0, import_hwk_adapter_core3.operationMayHaveCompletedParams)(method, { operationId, reason: "disconnect" }) : { operationId, reason: "disconnect" },
2982
- recovery: ambiguous ? { scope: "unknown" } : void 0
2983
- });
2984
- }
2985
- throw retryErr;
2986
- }
2987
- }
2988
- if (!operationId && errObj?.code === import_hwk_adapter_core3.HardwareErrorCode.DeviceMismatch) {
2989
- this._sessions.delete(resolvedConnectId);
2990
- this._sessionTransports.delete(sessionId);
2991
- this._discoveredDevices.delete(resolvedConnectId);
2992
- await this.connector.disconnect(sessionId).catch(() => void 0);
2993
- throw err;
2994
- }
2995
1943
  const autoInstallApp = commonParams?.autoInstallApp ?? this._defaultAutoInstallApp;
2996
1944
  const isAppMissing = isAppNotInstalledError(err) || err?.code === import_hwk_adapter_core3.HardwareErrorCode.AppNotInstalled;
2997
1945
  if (autoInstallApp && isAppMissing) {
@@ -3047,14 +1995,157 @@ var _LedgerAdapter = class _LedgerAdapter {
3047
1995
  fingerprint,
3048
1996
  permissionDeviceId,
3049
1997
  commonParams,
3050
- installContext,
3051
- operationId
1998
+ installContext
3052
1999
  );
3053
2000
  }
3054
2001
  }
2002
+ if (isStuckAppStateError(err)) {
2003
+ try {
2004
+ this._sessions.delete(resolvedConnectId);
2005
+ this._discoveredDevices.delete(resolvedConnectId);
2006
+ this.connector.reset?.();
2007
+ } catch {
2008
+ }
2009
+ debugLog(
2010
+ "[LedgerAdapter] stuck-app retry: method=",
2011
+ method,
2012
+ "delayMs=",
2013
+ _LedgerAdapter.STUCK_APP_RETRY_DELAY_MS,
2014
+ "_tag=",
2015
+ err?._tag
2016
+ );
2017
+ try {
2018
+ const retryResult = await this._retryAfterStuckApp(
2019
+ resolvedConnectId,
2020
+ method,
2021
+ effectiveParams,
2022
+ signal,
2023
+ err,
2024
+ fingerprint
2025
+ );
2026
+ debugLog("[LedgerAdapter] stuck-app retry succeeded: method=", method);
2027
+ return retryResult;
2028
+ } catch (retryErr) {
2029
+ if (signal.aborted) throw retryErr;
2030
+ if (isStuckAppStateError(retryErr)) {
2031
+ debugLog("[LedgerAdapter] stuck-app retry exhausted (2nd 6901): method=", method);
2032
+ throw err;
2033
+ }
2034
+ debugLog(
2035
+ "[LedgerAdapter] stuck-app retry threw non-stuck error: method=",
2036
+ method,
2037
+ "retryErrTag=",
2038
+ retryErr?._tag
2039
+ );
2040
+ throw retryErr;
2041
+ }
2042
+ }
2043
+ if (isDeviceLockedError(err) || isDeviceNotAdvertisingError(err) || isDeviceDisconnectedError(err)) {
2044
+ let lastErr = err;
2045
+ for (let attempt = 0; attempt < _LedgerAdapter.MAX_BUSINESS_RETRY_BUDGET; attempt += 1) {
2046
+ if (signal.aborted) throw lastErr;
2047
+ try {
2048
+ this._sessions.delete(resolvedConnectId);
2049
+ this._discoveredDevices.delete(resolvedConnectId);
2050
+ if (isDeviceDisconnectedError(lastErr)) {
2051
+ try {
2052
+ this.connector.reset?.();
2053
+ } catch {
2054
+ }
2055
+ }
2056
+ const reConnectId = await this.ensureConnected(
2057
+ resolvedConnectId,
2058
+ signal,
2059
+ allowUsbEphemeralFallback
2060
+ );
2061
+ const reSessionId = this._sessions.get(reConnectId);
2062
+ if (!reSessionId) throw lastErr;
2063
+ if (fingerprint && !fingerprint.skipFingerprint && fingerprint.deviceId) {
2064
+ const fp = await this._abortable(
2065
+ signal,
2066
+ this._verifyDeviceFingerprintWithSession(
2067
+ reSessionId,
2068
+ fingerprint.deviceId,
2069
+ fingerprint.chain
2070
+ )
2071
+ );
2072
+ if (!fp.success) {
2073
+ throw Object.assign(new Error(formatDeviceMismatchError(fp.expected, fp.actual)), {
2074
+ code: import_hwk_adapter_core3.HardwareErrorCode.DeviceMismatch
2075
+ });
2076
+ }
2077
+ }
2078
+ return await this._callConnector(reSessionId, method, effectiveParams, signal);
2079
+ } catch (retryErr) {
2080
+ if (signal.aborted) throw retryErr;
2081
+ lastErr = retryErr;
2082
+ const canRetry = attempt < _LedgerAdapter.MAX_BUSINESS_RETRY_BUDGET - 1 && (isDeviceLockedError(retryErr) || isDeviceNotAdvertisingError(retryErr) || isDeviceDisconnectedError(retryErr));
2083
+ if (!canRetry) {
2084
+ throw retryErr;
2085
+ }
2086
+ }
2087
+ }
2088
+ throw lastErr;
2089
+ }
2090
+ if (isTimeoutError(err)) {
2091
+ debugLog("[LedgerAdapter] timeout, retrying with fresh connection...");
2092
+ this._discoveredDevices.delete(resolvedConnectId);
2093
+ return this._retryWithFreshConnection(
2094
+ resolvedConnectId,
2095
+ method,
2096
+ effectiveParams,
2097
+ signal,
2098
+ err,
2099
+ fingerprint
2100
+ );
2101
+ }
2102
+ if (isConnectionLevelError(err)) {
2103
+ debugLog("[LedgerAdapter] connection-level fail-closed reset");
2104
+ this._sessions.delete(resolvedConnectId);
2105
+ this._discoveredDevices.delete(resolvedConnectId);
2106
+ this.connector.reset();
2107
+ const codeNum = err?.code;
2108
+ throw Object.assign(err, {
2109
+ code: codeNum ?? import_hwk_adapter_core3.HardwareErrorCode.DeviceDisconnected
2110
+ });
2111
+ }
3055
2112
  throw err;
3056
2113
  }
3057
2114
  }
2115
+ /**
2116
+ * Stuck-app recovery: pause for the device's UI transition, then retry once.
2117
+ *
2118
+ * Caller has already cleared the session + reset connector. We wait so Stax
2119
+ * finishes its post-CloseApp animation, then go through ensureConnected +
2120
+ * fingerprint check + call exactly once. Caller decides what to do on a
2121
+ * second stuck-app hit.
2122
+ */
2123
+ async _retryAfterStuckApp(resolvedConnectId, method, params, signal, originalErr, fingerprint) {
2124
+ await this._sleepAbortable(_LedgerAdapter.STUCK_APP_RETRY_DELAY_MS, signal);
2125
+ const retryConnectId = await this.ensureConnected(
2126
+ resolvedConnectId,
2127
+ signal,
2128
+ !!fingerprint?.deviceId && !fingerprint.skipFingerprint
2129
+ );
2130
+ const retrySessionId = this._sessions.get(retryConnectId);
2131
+ if (!retrySessionId) throw originalErr;
2132
+ if (fingerprint && !fingerprint.skipFingerprint && fingerprint.deviceId) {
2133
+ const fp = await this._abortable(
2134
+ signal,
2135
+ this._verifyDeviceFingerprintWithSession(
2136
+ retrySessionId,
2137
+ fingerprint.deviceId,
2138
+ fingerprint.chain
2139
+ )
2140
+ );
2141
+ if (!fp.success) {
2142
+ throw Object.assign(new Error(formatDeviceMismatchError(fp.expected, fp.actual)), {
2143
+ code: import_hwk_adapter_core3.HardwareErrorCode.DeviceMismatch
2144
+ });
2145
+ }
2146
+ }
2147
+ return this._callConnector(retrySessionId, method, params, signal);
2148
+ }
3058
2149
  _sleepAbortable(ms, signal) {
3059
2150
  return new Promise((resolve, reject) => {
3060
2151
  if (signal.aborted) {
@@ -3073,22 +2164,111 @@ var _LedgerAdapter = class _LedgerAdapter {
3073
2164
  });
3074
2165
  }
3075
2166
  /**
3076
- * Ensure OS-level Bluetooth/USB permission; times out after 60s if the host never
3077
- * replies. Without connectId the request is environment-level, else device-level.
2167
+ * Clear stale session, reconnect, and retry the call.
2168
+ *
2169
+ * Timeout recovery starts with a full connector reset. After an APDU
2170
+ * timeout, DMK/transport state may still emit malformed responses; retrying
2171
+ * on the same DMK can poison the next chain switch.
2172
+ */
2173
+ async _retryWithFreshConnection(targetConnectId, method, params, signal, originalErr, fingerprint) {
2174
+ this.connector.reset();
2175
+ this._sessions.clear();
2176
+ this._discoveredDevices.clear();
2177
+ this._connectingPromise = null;
2178
+ const allowUsbEphemeralFallback = !!fingerprint?.deviceId && !fingerprint.skipFingerprint;
2179
+ const retryConnectId = await this.ensureConnected(
2180
+ targetConnectId,
2181
+ signal,
2182
+ allowUsbEphemeralFallback
2183
+ );
2184
+ const retrySessionId = this._sessions.get(retryConnectId);
2185
+ if (!retrySessionId) {
2186
+ throw originalErr;
2187
+ }
2188
+ try {
2189
+ if (fingerprint && !fingerprint.skipFingerprint && fingerprint.deviceId) {
2190
+ const fp = await this._abortable(
2191
+ signal,
2192
+ this._verifyDeviceFingerprintWithSession(
2193
+ retrySessionId,
2194
+ fingerprint.deviceId,
2195
+ fingerprint.chain
2196
+ )
2197
+ );
2198
+ if (!fp.success) {
2199
+ throw Object.assign(new Error(formatDeviceMismatchError(fp.expected, fp.actual)), {
2200
+ code: import_hwk_adapter_core3.HardwareErrorCode.DeviceMismatch
2201
+ });
2202
+ }
2203
+ }
2204
+ return await this._callConnector(retrySessionId, method, params, signal);
2205
+ } catch (retryErr) {
2206
+ if (signal.aborted) throw retryErr;
2207
+ this.connector.reset();
2208
+ this._sessions.clear();
2209
+ this._discoveredDevices.clear();
2210
+ this._connectingPromise = null;
2211
+ if (!isDeviceDisconnectedError(retryErr) && !isTimeoutError(retryErr)) {
2212
+ throw retryErr;
2213
+ }
2214
+ debugLog(
2215
+ "[LedgerAdapter] fresh-session retry still failed; resetting connector and rebuilding DMK"
2216
+ );
2217
+ this.connector.reset();
2218
+ this._sessions.clear();
2219
+ this._discoveredDevices.clear();
2220
+ this._connectingPromise = null;
2221
+ const finalConnectId = await this.ensureConnected(
2222
+ targetConnectId,
2223
+ signal,
2224
+ allowUsbEphemeralFallback
2225
+ );
2226
+ const finalSessionId = this._sessions.get(finalConnectId);
2227
+ if (!finalSessionId) {
2228
+ throw originalErr;
2229
+ }
2230
+ if (fingerprint && !fingerprint.skipFingerprint && fingerprint.deviceId) {
2231
+ const fp = await this._abortable(
2232
+ signal,
2233
+ this._verifyDeviceFingerprintWithSession(
2234
+ finalSessionId,
2235
+ fingerprint.deviceId,
2236
+ fingerprint.chain
2237
+ )
2238
+ );
2239
+ if (!fp.success) {
2240
+ throw Object.assign(new Error(formatDeviceMismatchError(fp.expected, fp.actual)), {
2241
+ code: import_hwk_adapter_core3.HardwareErrorCode.DeviceMismatch
2242
+ });
2243
+ }
2244
+ }
2245
+ return this._callConnector(finalSessionId, method, params, signal);
2246
+ }
2247
+ }
2248
+ /**
2249
+ * Ensure OS-level device permission (Bluetooth / USB) before proceeding.
2250
+ *
2251
+ * Emits `REQUEST_DEVICE_PERMISSION` and awaits the consumer's
2252
+ * `RECEIVE_DEVICE_PERMISSION` reply (60s budget covers "probe → system
2253
+ * prompt → user tap" plus a generous margin). If the consumer never wires
2254
+ * a handler or never replies, the wait times out and the operation fails
2255
+ * fast so scanners/callers don't hang silently.
2256
+ *
2257
+ * - No connectId (searchDevices): environment-level permission
2258
+ * - With connectId (business methods): device-level permission
3078
2259
  */
3079
2260
  async _ensureDevicePermission(connectId, deviceId, signal) {
3080
2261
  if (signal?.aborted) {
3081
2262
  _LedgerAdapter._throwIfAborted(signal);
3082
2263
  }
3083
2264
  const transportType = this.activeTransport ?? "hid";
3084
- const operationId = this._activeOperationId();
3085
2265
  const waitPromise = this._uiRegistry.wait(
3086
2266
  import_hwk_adapter_core3.UI_REQUEST.REQUEST_DEVICE_PERMISSION,
3087
- { timeoutMs: 6e4, operationId }
2267
+ { timeoutMs: 6e4 }
3088
2268
  );
3089
2269
  this.emitter.emit(import_hwk_adapter_core3.UI_REQUEST.REQUEST_DEVICE_PERMISSION, {
3090
2270
  type: import_hwk_adapter_core3.UI_REQUEST.REQUEST_DEVICE_PERMISSION,
3091
- payload: { transportType, connectId, deviceId, operationId }
2271
+ payload: { transportType, connectId, deviceId }
3092
2272
  });
3093
2273
  let response;
3094
2274
  const onAbort = () => {
@@ -3133,16 +2313,8 @@ var _LedgerAdapter = class _LedgerAdapter {
3133
2313
  const tag = err && typeof err === "object" ? err._tag : void 0;
3134
2314
  if (err && typeof err === "object" && "code" in err && typeof err.code === "number") {
3135
2315
  const e = err;
3136
- const params = e.code === import_hwk_adapter_core3.HardwareErrorCode.DevicePermissionDenied && e.reason ? { permissionDeniedReason: e.reason } : e.params;
3137
- return ledgerFailure(
3138
- e.code,
3139
- e.message ?? "Unknown error",
3140
- e.appName,
3141
- tag,
3142
- params,
3143
- void 0,
3144
- (0, import_hwk_adapter_core3.isHwkRecoveryHint)(e.recovery) ? e.recovery : void 0
3145
- );
2316
+ const params = e.code === import_hwk_adapter_core3.HardwareErrorCode.DevicePermissionDenied && e.reason ? { permissionDeniedReason: e.reason } : e.params;
2317
+ return ledgerFailure(e.code, e.message ?? "Unknown error", e.appName, tag, params);
3146
2318
  }
3147
2319
  const mapped = mapLedgerError(err);
3148
2320
  return ledgerFailure(mapped.code, mapped.message, mapped.appName, tag);
@@ -3169,7 +2341,7 @@ var _LedgerAdapter = class _LedgerAdapter {
3169
2341
  deviceId: device.deviceId,
3170
2342
  connectId: device.connectId,
3171
2343
  label: device.name,
3172
- connectionType: device.connectionType ?? this.connector.connectionType,
2344
+ connectionType: this.connector.connectionType,
3173
2345
  rssi: device.rssi,
3174
2346
  isConnectable: device.isConnectable,
3175
2347
  serialNumber: device.serialNumber,
@@ -3194,7 +2366,7 @@ _LedgerAdapter.APP_INSTALL_PROGRESS_MIN_DELTA = 0.05;
3194
2366
  var LedgerAdapter = _LedgerAdapter;
3195
2367
 
3196
2368
  // src/connector/LedgerConnectorBase.ts
3197
- var import_hwk_adapter_core12 = require("@onekeyfe/hwk-adapter-core");
2369
+ var import_hwk_adapter_core13 = require("@onekeyfe/hwk-adapter-core");
3198
2370
 
3199
2371
  // src/device/LedgerDeviceManager.ts
3200
2372
  var LedgerDeviceManager = class {
@@ -3730,7 +2902,7 @@ var SignerManager = class _SignerManager {
3730
2902
  return (args) => new import_device_signer_kit_ethereum.SignerEthBuilder(args);
3731
2903
  }
3732
2904
  static _createContextModule() {
3733
- const contextModule = new import_context_module.ContextModuleBuilder({}).setChain(import_context_module.ContextModuleChainID.Ethereum).removeDefaultLoaders().build();
2905
+ const contextModule = new import_context_module.ContextModuleBuilder({}).removeDefaultLoaders().build();
3734
2906
  return _SignerManager.wrapBlindSigningReportNonBlocking(contextModule);
3735
2907
  }
3736
2908
  static wrapBlindSigningReportNonBlocking(contextModule) {
@@ -3768,27 +2940,6 @@ function collapseSignerInteraction(interaction) {
3768
2940
  return import_hwk_adapter_core5.EConnectorInteraction.ConfirmOnDevice;
3769
2941
  }
3770
2942
  }
3771
- function wireSignerToSession(ctx, sessionId, chain, signer) {
3772
- signer.onInteraction = (interaction) => {
3773
- debugLog(`[LedgerConnector] ${chain}.onInteraction:`, interaction);
3774
- ctx.emit("ui-event", {
3775
- type: collapseSignerInteraction(interaction),
3776
- payload: { sessionId }
3777
- });
3778
- };
3779
- signer.onRegisterCanceller = (cancel) => ctx.registerCanceller(sessionId, cancel);
3780
- return signer;
3781
- }
3782
- async function runSignerCall(ctx, sessionId, call) {
3783
- try {
3784
- return await call();
3785
- } catch (err) {
3786
- ctx.invalidateSession(sessionId);
3787
- throw ctx.wrapError(err);
3788
- } finally {
3789
- ctx.clearCanceller(sessionId);
3790
- }
3791
- }
3792
2943
 
3793
2944
  // src/connector/chains/evm.ts
3794
2945
  async function evmGetAddress(ctx, sessionId, params) {
@@ -4222,7 +3373,6 @@ function _applySignaturesToPsbt(psbtHex, signatures) {
4222
3373
 
4223
3374
  // src/connector/chains/sol.ts
4224
3375
  var import_hwk_adapter_core9 = require("@onekeyfe/hwk-adapter-core");
4225
- var import_bs58 = __toESM(require("bs58"));
4226
3376
 
4227
3377
  // src/signer/SignerSol.ts
4228
3378
  var SignerSol = class {
@@ -4306,21 +3456,8 @@ async function solSignMessage(ctx, sessionId, params) {
4306
3456
  const path = normalizePath(params.path);
4307
3457
  const messageBytes = (0, import_hwk_adapter_core9.hexToBytes)(params.message);
4308
3458
  try {
4309
- if (params.messageVersion === 1) {
4310
- const preparedMessage = (0, import_hwk_adapter_core9.prepareSolanaOffchainMessageV1)({
4311
- message: messageBytes,
4312
- requiredSigners: params.requiredSigners
4313
- });
4314
- const { SignMessageVersion } = await ctx.importLedgerKit(
4315
- "@ledgerhq/device-signer-kit-solana"
4316
- );
4317
- const result2 = await solSigner.signMessage(path, preparedMessage.serializedMessage, {
4318
- version: SignMessageVersion.Raw
4319
- });
4320
- return { signature: decodeBase58Signature(result2.signature) };
4321
- }
4322
3459
  const result = await solSigner.signMessage(path, messageBytes);
4323
- return { signature: decodeBase58EnvelopeSignature(result.signature) };
3460
+ return { signature: result.signature };
4324
3461
  } catch (err) {
4325
3462
  ctx.invalidateSession(sessionId);
4326
3463
  throw ctx.wrapError(err);
@@ -4328,27 +3465,11 @@ async function solSignMessage(ctx, sessionId, params) {
4328
3465
  ctx.clearCanceller(sessionId);
4329
3466
  }
4330
3467
  }
4331
- function decodeBase58Signature(signature) {
4332
- const bytes = import_bs58.default.decode(signature);
4333
- if (bytes.length !== 64) {
4334
- throw new Error(`Ledger Solana signature must be 64 bytes, received ${bytes.length}`);
4335
- }
4336
- return (0, import_hwk_adapter_core9.bytesToHex)(bytes);
4337
- }
4338
- function decodeBase58EnvelopeSignature(envelope) {
4339
- const bytes = import_bs58.default.decode(envelope);
4340
- if (bytes.length < 65 || bytes[0] !== 1) {
4341
- throw new Error("Ledger Solana signature envelope is invalid");
4342
- }
4343
- return (0, import_hwk_adapter_core9.bytesToHex)(bytes.subarray(1, 65));
4344
- }
4345
3468
  async function _createSolSigner(ctx, sessionId) {
4346
3469
  const dmk = await ctx.getOrCreateDmk();
4347
- const { ContextModuleBuilder: ContextModuleBuilder2, ContextModuleChainID: ContextModuleChainID2 } = await ctx.importLedgerKit(
4348
- "@ledgerhq/context-module"
4349
- );
3470
+ const { ContextModuleBuilder: ContextModuleBuilder2 } = await ctx.importLedgerKit("@ledgerhq/context-module");
4350
3471
  const { SignerSolanaBuilder } = await ctx.importLedgerKit("@ledgerhq/device-signer-kit-solana");
4351
- const contextModule = new ContextModuleBuilder2({}).setChain(ContextModuleChainID2.Solana).removeDefaultLoaders().build();
3472
+ const contextModule = new ContextModuleBuilder2({}).removeDefaultLoaders().build();
4352
3473
  const sdkSigner = new SignerSolanaBuilder({ dmk, sessionId }).withContextModule(contextModule).build();
4353
3474
  const signer = new SignerSol(sdkSigner);
4354
3475
  signer.onInteraction = (interaction) => {
@@ -4364,151 +3485,360 @@ async function _createSolSigner(ctx, sessionId) {
4364
3485
  return signer;
4365
3486
  }
4366
3487
 
4367
- // src/connector/chains/zcash.ts
4368
- var import_hwk_adapter_core10 = require("@onekeyfe/hwk-adapter-core");
3488
+ // src/connector/chains/tron.ts
3489
+ var import_hwk_adapter_core12 = require("@onekeyfe/hwk-adapter-core");
3490
+ var import_hw_app_trx = __toESM(require("@ledgerhq/hw-app-trx"));
4369
3491
 
4370
- // src/signer/SignerZcash.ts
4371
- var SignerZcash = class {
4372
- // eslint-disable-next-line no-useless-constructor, no-empty-function
4373
- constructor(_sdk) {
4374
- this._sdk = _sdk;
3492
+ // src/connector/chains/legacyChainCall.ts
3493
+ var import_hwk_adapter_core11 = require("@onekeyfe/hwk-adapter-core");
3494
+
3495
+ // src/app/AppManager.ts
3496
+ var import_device_management_kit2 = require("@ledgerhq/device-management-kit");
3497
+ var import_hwk_adapter_core10 = require("@onekeyfe/hwk-adapter-core");
3498
+ var APP_NAME_MAP = {
3499
+ ETH: "Ethereum",
3500
+ BTC: "Bitcoin",
3501
+ SOL: "Solana",
3502
+ TRX: "Tron",
3503
+ XRP: "XRP",
3504
+ ADA: "Cardano",
3505
+ DOT: "Polkadot",
3506
+ ATOM: "Cosmos"
3507
+ };
3508
+ var DASHBOARD_APP_NAME = "BOLOS";
3509
+ var AppManager = class {
3510
+ constructor(dmk, options) {
3511
+ this._dmk = dmk;
3512
+ this._waitMs = options?.waitMs ?? 1e3;
3513
+ this._maxRetries = options?.maxRetries ?? 10;
4375
3514
  }
4376
- /** GET_VK: UFVK string (default) or raw 96-byte Orchard FVK. */
4377
- async getFullViewingKey(derivationPath, options) {
4378
- const action = this._sdk.getFullViewingKey(derivationPath, options);
4379
- return deviceActionToPromise(
4380
- action,
4381
- this.onInteraction,
4382
- void 0,
4383
- this.onRegisterCanceller
3515
+ /**
3516
+ * Return the Ledger app name for a given chain ticker,
3517
+ * or undefined if the chain is not supported.
3518
+ */
3519
+ static getAppName(chain) {
3520
+ return APP_NAME_MAP[chain];
3521
+ }
3522
+ /**
3523
+ * Ensure the target app is open on the device identified by `sessionId`.
3524
+ *
3525
+ * Flow:
3526
+ * 1. Check the currently running app.
3527
+ * 2. If it is already the target, return immediately.
3528
+ * 3. If a different app is running (not dashboard), close it first.
3529
+ * 4. Open the target app.
3530
+ * 5. Poll until the device confirms the target app is running.
3531
+ */
3532
+ /**
3533
+ * @param onConfirmOnDevice Called BEFORE OpenAppCommand is issued — the
3534
+ * device is about to display "Open <app>" on screen and wait for the
3535
+ * user's button press. UI consumers should show their "open app" prompt
3536
+ * in response. NOT called when the target app is already open (no user
3537
+ * interaction needed in that case).
3538
+ *
3539
+ * Important: OpenAppCommand is blocking. It does not resolve until the user
3540
+ * has physically confirmed on the device, so anything that runs AFTER
3541
+ * `await this._openApp(...)` lands AFTER the prompt is already gone.
3542
+ * Hence the callback must fire BEFORE that await.
3543
+ */
3544
+ async ensureAppOpen(sessionId, targetAppName, onConfirmOnDevice) {
3545
+ const currentApp = await this._getCurrentApp(sessionId);
3546
+ if (currentApp === targetAppName) {
3547
+ return;
3548
+ }
3549
+ if (!this._isDashboard(currentApp)) {
3550
+ await this._closeCurrentApp(sessionId);
3551
+ await this._waitForApp(sessionId, DASHBOARD_APP_NAME);
3552
+ }
3553
+ onConfirmOnDevice?.();
3554
+ await this._openApp(sessionId, targetAppName);
3555
+ await this._waitForApp(sessionId, targetAppName);
3556
+ }
3557
+ // ---------------------------------------------------------------------------
3558
+ // Private helpers
3559
+ // ---------------------------------------------------------------------------
3560
+ async _getCurrentApp(sessionId) {
3561
+ const result = await this._dmk.sendCommand({
3562
+ sessionId,
3563
+ command: new import_device_management_kit2.GetAppAndVersionCommand()
3564
+ });
3565
+ if ((0, import_device_management_kit2.isSuccessCommandResult)(result)) {
3566
+ debugLog("[AppManager] currentApp:", result.data.name);
3567
+ return result.data.name;
3568
+ }
3569
+ const errResult = result;
3570
+ const dmkErr = errResult.error ?? {};
3571
+ const original = dmkErr.originalError;
3572
+ debugLog(
3573
+ "[AppManager] _getCurrentApp failed sessionId=",
3574
+ sessionId,
3575
+ "tag=",
3576
+ dmkErr._tag,
3577
+ "errorCode=",
3578
+ dmkErr.errorCode,
3579
+ "message=",
3580
+ dmkErr.message,
3581
+ "originalErrorMessage=",
3582
+ original?.message ?? String(original ?? "")
3583
+ );
3584
+ throw Object.assign(
3585
+ new Error(
3586
+ dmkErr.message ?? "Failed to get current app from device"
3587
+ ),
3588
+ {
3589
+ _tag: dmkErr._tag,
3590
+ errorCode: dmkErr.errorCode,
3591
+ originalError: original
3592
+ }
4384
3593
  );
4385
3594
  }
4386
- /** GET_SHIELDED_ADDRESS: single-Orchard-receiver UA for a 5-level transparent path. */
4387
- async getShieldedAddress(derivationPath, options) {
4388
- const action = this._sdk.getShieldedAddress(derivationPath, options);
4389
- return deviceActionToPromise(
4390
- action,
4391
- this.onInteraction,
4392
- void 0,
4393
- this.onRegisterCanceller
3595
+ async _openApp(sessionId, appName) {
3596
+ const result = await this._dmk.sendCommand({
3597
+ sessionId,
3598
+ command: new import_device_management_kit2.OpenAppCommand({ appName })
3599
+ });
3600
+ if (!(0, import_device_management_kit2.isSuccessCommandResult)(result)) {
3601
+ const dmkErr = result.error;
3602
+ const errorCode = "errorCode" in dmkErr && dmkErr.errorCode != null ? String(dmkErr.errorCode) : "";
3603
+ const message = "message" in dmkErr && typeof dmkErr.message === "string" ? dmkErr.message : "";
3604
+ debugLog(
3605
+ "[AppManager] openApp failed:",
3606
+ appName,
3607
+ "errorCode:",
3608
+ errorCode,
3609
+ "tag:",
3610
+ dmkErr._tag
3611
+ );
3612
+ let code;
3613
+ if (errorCode === "6807" || /unknown application/i.test(message)) {
3614
+ code = import_hwk_adapter_core10.HardwareErrorCode.AppNotInstalled;
3615
+ } else if (errorCode === "5501" || dmkErr._tag === "ActionRefusedError") {
3616
+ code = import_hwk_adapter_core10.HardwareErrorCode.UserRejected;
3617
+ }
3618
+ throw Object.assign(new Error(`Failed to open "${appName}"`), {
3619
+ _tag: ERROR_TAG.OpenAppCommand,
3620
+ code,
3621
+ errorCode,
3622
+ statusCode: errorCode,
3623
+ appName,
3624
+ originalError: dmkErr
3625
+ });
3626
+ }
3627
+ }
3628
+ async _closeCurrentApp(sessionId) {
3629
+ debugLog("[AppManager] closeCurrentApp");
3630
+ await this._dmk.sendCommand({
3631
+ sessionId,
3632
+ command: new import_device_management_kit2.CloseAppCommand()
3633
+ });
3634
+ }
3635
+ /**
3636
+ * Poll the device until the expected app is reported as running,
3637
+ * or throw after `_maxRetries` attempts.
3638
+ */
3639
+ async _waitForApp(sessionId, expectedAppName) {
3640
+ let lastSeen = "";
3641
+ for (let i = 0; i < this._maxRetries; i++) {
3642
+ await this._wait();
3643
+ const current = await this._getCurrentApp(sessionId);
3644
+ lastSeen = current;
3645
+ if (current === expectedAppName) {
3646
+ return;
3647
+ }
3648
+ }
3649
+ debugLog(
3650
+ "[AppManager] waitForApp exhausted: expected=",
3651
+ expectedAppName,
3652
+ "lastSeen=",
3653
+ lastSeen
3654
+ );
3655
+ throw new Error(
3656
+ `Ledger: failed to open "${expectedAppName}" after ${this._maxRetries} retries (last seen: ${lastSeen})`
4394
3657
  );
4395
3658
  }
3659
+ _isDashboard(appName) {
3660
+ return appName === DASHBOARD_APP_NAME;
3661
+ }
3662
+ _wait() {
3663
+ return new Promise((resolve) => setTimeout(resolve, this._waitMs));
3664
+ }
4396
3665
  };
4397
3666
 
4398
- // src/connector/chains/zcash.ts
4399
- async function zcashGetFullViewingKey(ctx, sessionId, params) {
4400
- const signer = await _createZcashSigner(ctx, sessionId);
4401
- const path = normalizePath(params.path);
4402
- const mode = params.mode ?? "ufvk";
4403
- return runSignerCall(ctx, sessionId, async () => {
4404
- const result = await signer.getFullViewingKey(path, { mode });
4405
- if (result.mode === "ufvk") {
4406
- return { path: params.path, mode, ufvk: result.fullViewingKey };
4407
- }
4408
- return { path: params.path, mode, orchardFvk: (0, import_hwk_adapter_core10.bytesToHex)(result.fullViewingKey) };
4409
- });
3667
+ // src/connector/chains/legacyChainCall.ts
3668
+ function isLegacyWrongAppError(err, _appName) {
3669
+ return isWrongAppError(err);
4410
3670
  }
4411
- async function zcashGetShieldedAddress(ctx, sessionId, params) {
4412
- const signer = await _createZcashSigner(ctx, sessionId);
4413
- const path = normalizePath(params.path);
4414
- return runSignerCall(ctx, sessionId, async () => {
4415
- const result = await signer.getShieldedAddress(path, {
4416
- checkOnDevice: params.showOnDevice ?? false
3671
+ async function withLegacyChainCall(ctx, sessionId, options, action) {
3672
+ const { appName, needsConfirmation } = options;
3673
+ let openAppPromptShown = false;
3674
+ const onAppOpenPrompt = () => {
3675
+ openAppPromptShown = true;
3676
+ ctx.emit("ui-event", {
3677
+ type: import_hwk_adapter_core11.EConnectorInteraction.ConfirmOpenApp,
3678
+ payload: { sessionId }
4417
3679
  });
4418
- return { address: result.address, path: params.path };
4419
- });
4420
- }
4421
- async function _createZcashSigner(ctx, sessionId) {
4422
- const dmk = await ctx.getOrCreateDmk();
4423
- const { SignerZcashBuilder } = await ctx.importLedgerKit("@ledgerhq/device-signer-kit-zcash");
4424
- const sdkSigner = new SignerZcashBuilder({ dmk, sessionId }).build();
4425
- return wireSignerToSession(ctx, sessionId, "zcash", new SignerZcash(sdkSigner));
4426
- }
4427
-
4428
- // src/connector/chains/tron.ts
4429
- var import_hwk_adapter_core11 = require("@onekeyfe/hwk-adapter-core");
4430
-
4431
- // src/signer/SignerTron.ts
4432
- var SignerTron = class {
4433
- // eslint-disable-next-line no-useless-constructor, no-empty-function
4434
- constructor(_sdk) {
4435
- this._sdk = _sdk;
4436
- }
4437
- /** Base58 Tron address plus the uncompressed public key at `derivationPath`. */
4438
- async getAddress(derivationPath, options) {
4439
- const action = this._sdk.getAddress(derivationPath, options);
4440
- const result = await deviceActionToPromise(
4441
- action,
4442
- this.onInteraction,
4443
- void 0,
4444
- this.onRegisterCanceller
3680
+ };
3681
+ const closeOpenAppUiIfShown = () => {
3682
+ if (openAppPromptShown) {
3683
+ ctx.emit("ui-event", {
3684
+ type: import_hwk_adapter_core11.EConnectorInteraction.InteractionComplete,
3685
+ payload: { sessionId }
3686
+ });
3687
+ openAppPromptShown = false;
3688
+ }
3689
+ };
3690
+ try {
3691
+ await _ensureAppOpen(ctx, sessionId, appName, onAppOpenPrompt);
3692
+ } catch (err) {
3693
+ debugLog(
3694
+ "[LegacyChainCall] pre-flight ensureAppOpen failed:",
3695
+ appName,
3696
+ err?.message
4445
3697
  );
4446
- return { address: result.address, publicKey: result.publicKey };
3698
+ closeOpenAppUiIfShown();
3699
+ throw ctx.wrapError(err, { defaultAppName: appName });
3700
+ }
3701
+ const runOnce = async () => {
3702
+ let confirmEmitted = false;
3703
+ if (needsConfirmation) {
3704
+ ctx.emit("ui-event", {
3705
+ type: import_hwk_adapter_core11.EConnectorInteraction.ConfirmOnDevice,
3706
+ payload: { sessionId }
3707
+ });
3708
+ confirmEmitted = true;
3709
+ }
3710
+ try {
3711
+ return await action(sessionId);
3712
+ } finally {
3713
+ if (confirmEmitted || openAppPromptShown) {
3714
+ ctx.emit("ui-event", {
3715
+ type: import_hwk_adapter_core11.EConnectorInteraction.InteractionComplete,
3716
+ payload: { sessionId }
3717
+ });
3718
+ openAppPromptShown = false;
3719
+ }
3720
+ }
3721
+ };
3722
+ try {
3723
+ return await runOnce();
3724
+ } catch (err) {
3725
+ if (!isLegacyWrongAppError(err, appName)) {
3726
+ debugLog("[LegacyChainCall] non-wrong-app failure:", appName, err?.message);
3727
+ ctx.invalidateSession(sessionId);
3728
+ throw ctx.wrapError(err, { defaultAppName: appName });
3729
+ }
3730
+ debugLog("[LegacyChainCall] wrong-app detected, retrying:", appName);
3731
+ try {
3732
+ await _ensureAppOpen(ctx, sessionId, appName, onAppOpenPrompt);
3733
+ } catch (switchErr) {
3734
+ debugLog(
3735
+ "[LegacyChainCall] retry ensureAppOpen failed:",
3736
+ appName,
3737
+ switchErr?.message
3738
+ );
3739
+ closeOpenAppUiIfShown();
3740
+ throw ctx.wrapError(switchErr, { defaultAppName: appName });
3741
+ }
3742
+ ctx.clearAllSigners();
3743
+ const result = await runOnce();
3744
+ debugLog("[LegacyChainCall] retry succeeded:", appName);
3745
+ return result;
3746
+ }
3747
+ }
3748
+ async function _ensureAppOpen(ctx, sessionId, appName, onPrompt) {
3749
+ const dmk = await ctx.getOrCreateDmk();
3750
+ const appManager = new AppManager(dmk);
3751
+ await appManager.ensureAppOpen(sessionId, appName, onPrompt);
3752
+ }
3753
+
3754
+ // src/transport/DmkTransport.ts
3755
+ var import_hw_transport = __toESM(require("@ledgerhq/hw-transport"));
3756
+ var DmkTransport = class extends import_hw_transport.default {
3757
+ constructor(dmk, sessionId) {
3758
+ super();
3759
+ this._dmk = dmk;
3760
+ this._sessionId = sessionId;
4447
3761
  }
4448
- /** Sign a protobuf-encoded raw transaction. */
4449
- async signTransaction(derivationPath, transaction, options) {
4450
- const action = this._sdk.signTransaction(derivationPath, transaction, options);
4451
- return deviceActionToPromise(
4452
- action,
4453
- this.onInteraction,
4454
- void 0,
4455
- this.onRegisterCanceller
4456
- );
3762
+ async exchange(apdu) {
3763
+ const response = await this._dmk.sendApdu({
3764
+ sessionId: this._sessionId,
3765
+ apdu: new Uint8Array(apdu)
3766
+ });
3767
+ const { data, statusCode } = response;
3768
+ const result = Buffer.alloc(data.length + 2);
3769
+ if (data.length > 0) {
3770
+ result.set(data, 0);
3771
+ }
3772
+ result.set(statusCode, data.length);
3773
+ return result;
4457
3774
  }
4458
- /** Sign a personal message (TIP-191). */
4459
- async signPersonalMessage(derivationPath, message, options) {
4460
- const action = this._sdk.signPersonalMessage(derivationPath, message, options);
4461
- return deviceActionToPromise(
4462
- action,
4463
- this.onInteraction,
4464
- void 0,
4465
- this.onRegisterCanceller
4466
- );
3775
+ async close() {
4467
3776
  }
4468
3777
  };
4469
3778
 
4470
3779
  // src/connector/chains/tron.ts
4471
3780
  async function tronGetAddress(ctx, sessionId, params) {
4472
- const tronSigner = await _createTronSigner(ctx, sessionId);
4473
3781
  const path = normalizePath(params.path);
4474
- return runSignerCall(ctx, sessionId, async () => {
4475
- const result = await tronSigner.getAddress(path, {
4476
- checkOnDevice: params.showOnDevice ?? false
4477
- });
4478
- return { address: result.address, publicKey: result.publicKey, path: params.path };
4479
- });
3782
+ const showOnDevice = params.showOnDevice ?? false;
3783
+ return withLegacyChainCall(
3784
+ ctx,
3785
+ sessionId,
3786
+ {
3787
+ appName: "Tron",
3788
+ // Only show "confirm on device" UI when the device is actually going
3789
+ // to display the address for the user to verify.
3790
+ needsConfirmation: showOnDevice
3791
+ },
3792
+ async (sid) => {
3793
+ const trx = await _createTrx(ctx, sid);
3794
+ const result = await trx.getAddress(path, showOnDevice);
3795
+ return { address: result.address, publicKey: result.publicKey, path: params.path };
3796
+ }
3797
+ );
4480
3798
  }
4481
3799
  async function tronSignTransaction(ctx, sessionId, params) {
4482
3800
  if (!params.rawTxHex) {
4483
3801
  throw Object.assign(
4484
3802
  new Error("TRON signing requires a protobuf-encoded raw transaction hex (rawTxHex)."),
4485
- { code: import_hwk_adapter_core11.HardwareErrorCode.InvalidParams }
3803
+ { code: import_hwk_adapter_core12.HardwareErrorCode.InvalidParams }
4486
3804
  );
4487
3805
  }
4488
- const tronSigner = await _createTronSigner(ctx, sessionId);
4489
3806
  const path = normalizePath(params.path);
4490
- return runSignerCall(ctx, sessionId, async () => {
4491
- const signature = await tronSigner.signTransaction(path, (0, import_hwk_adapter_core11.hexToBytes)(params.rawTxHex));
4492
- return { signature: (0, import_hwk_adapter_core11.bytesToHex)(signature) };
4493
- });
3807
+ return withLegacyChainCall(
3808
+ ctx,
3809
+ sessionId,
3810
+ { appName: "Tron", needsConfirmation: true },
3811
+ async (sid) => {
3812
+ const trx = await _createTrx(ctx, sid);
3813
+ const signature = await trx.signTransaction(
3814
+ path,
3815
+ params.rawTxHex,
3816
+ params.tokenSignatures ?? []
3817
+ );
3818
+ return { signature };
3819
+ }
3820
+ );
4494
3821
  }
4495
3822
  async function tronSignMessage(ctx, sessionId, params) {
4496
- const tronSigner = await _createTronSigner(ctx, sessionId);
4497
3823
  const path = normalizePath(params.path);
4498
- return runSignerCall(ctx, sessionId, async () => {
4499
- const signature = await tronSigner.signPersonalMessage(path, (0, import_hwk_adapter_core11.hexToBytes)(params.messageHex));
4500
- return { signature: (0, import_hwk_adapter_core11.bytesToHex)(signature) };
4501
- });
3824
+ return withLegacyChainCall(
3825
+ ctx,
3826
+ sessionId,
3827
+ { appName: "Tron", needsConfirmation: true },
3828
+ async (sid) => {
3829
+ const trx = await _createTrx(ctx, sid);
3830
+ const signature = await trx.signPersonalMessage(path, params.messageHex);
3831
+ return { signature };
3832
+ }
3833
+ );
4502
3834
  }
4503
- async function _createTronSigner(ctx, sessionId) {
3835
+ async function _createTrx(ctx, sessionId) {
4504
3836
  const dmk = await ctx.getOrCreateDmk();
4505
- const { SignerTrxBuilder } = await ctx.importLedgerKit("@ledgerhq/device-signer-kit-tron");
4506
- const sdkSigner = new SignerTrxBuilder({ dmk, sessionId }).build();
4507
- return wireSignerToSession(ctx, sessionId, "tron", new SignerTron(sdkSigner));
3837
+ return new import_hw_app_trx.default(new DmkTransport(dmk, sessionId));
4508
3838
  }
4509
3839
 
4510
3840
  // src/device-apps/customActions.ts
4511
- var import_device_management_kit2 = require("@ledgerhq/device-management-kit");
3841
+ var import_device_management_kit3 = require("@ledgerhq/device-management-kit");
4512
3842
  var import_rxjs = require("rxjs");
4513
3843
  var GetOsVersionDeviceAction = class {
4514
3844
  // eslint-disable-next-line no-useless-constructor, no-empty-function
@@ -4522,7 +3852,7 @@ var GetOsVersionDeviceAction = class {
4522
3852
  (async () => {
4523
3853
  try {
4524
3854
  subject.next({
4525
- status: import_device_management_kit2.DeviceActionStatus.Pending,
3855
+ status: import_device_management_kit3.DeviceActionStatus.Pending,
4526
3856
  intermediateValue: { requiredUserInteraction: "none" }
4527
3857
  });
4528
3858
  const result = await internalApi.sendCommand(new this._deps.GetOsVersionCommand());
@@ -4531,11 +3861,11 @@ var GetOsVersionDeviceAction = class {
4531
3861
  const errObj = result?.error;
4532
3862
  throw new Error(errObj?.message ?? "GetOsVersionCommand failed");
4533
3863
  }
4534
- subject.next({ status: import_device_management_kit2.DeviceActionStatus.Completed, output: result.data });
3864
+ subject.next({ status: import_device_management_kit3.DeviceActionStatus.Completed, output: result.data });
4535
3865
  subject.complete();
4536
3866
  } catch (err) {
4537
3867
  if (cancelled) return;
4538
- subject.next({ status: import_device_management_kit2.DeviceActionStatus.Error, error: err });
3868
+ subject.next({ status: import_device_management_kit3.DeviceActionStatus.Error, error: err });
4539
3869
  subject.complete();
4540
3870
  }
4541
3871
  })();
@@ -4543,7 +3873,7 @@ var GetOsVersionDeviceAction = class {
4543
3873
  observable: subject.asObservable(),
4544
3874
  cancel: () => {
4545
3875
  cancelled = true;
4546
- subject.next({ status: import_device_management_kit2.DeviceActionStatus.Stopped });
3876
+ subject.next({ status: import_device_management_kit3.DeviceActionStatus.Stopped });
4547
3877
  subject.complete();
4548
3878
  }
4549
3879
  };
@@ -4561,7 +3891,7 @@ var ListAvailableAppsDeviceAction = class {
4561
3891
  (async () => {
4562
3892
  try {
4563
3893
  subject.next({
4564
- status: import_device_management_kit2.DeviceActionStatus.Pending,
3894
+ status: import_device_management_kit3.DeviceActionStatus.Pending,
4565
3895
  intermediateValue: { requiredUserInteraction: "none" }
4566
3896
  });
4567
3897
  const osVersionResult = await internalApi.sendCommand(new this._GetOsVersionCommand());
@@ -4578,11 +3908,11 @@ var ListAvailableAppsDeviceAction = class {
4578
3908
  throw new Error(httpErr?.message ?? "Manager API getAppList failed");
4579
3909
  }
4580
3910
  const apps = either.extract();
4581
- subject.next({ status: import_device_management_kit2.DeviceActionStatus.Completed, output: apps });
3911
+ subject.next({ status: import_device_management_kit3.DeviceActionStatus.Completed, output: apps });
4582
3912
  subject.complete();
4583
3913
  } catch (err) {
4584
3914
  if (cancelled) return;
4585
- subject.next({ status: import_device_management_kit2.DeviceActionStatus.Error, error: err });
3915
+ subject.next({ status: import_device_management_kit3.DeviceActionStatus.Error, error: err });
4586
3916
  subject.complete();
4587
3917
  }
4588
3918
  })();
@@ -4590,7 +3920,7 @@ var ListAvailableAppsDeviceAction = class {
4590
3920
  observable: subject.asObservable(),
4591
3921
  cancel: () => {
4592
3922
  cancelled = true;
4593
- subject.next({ status: import_device_management_kit2.DeviceActionStatus.Stopped });
3923
+ subject.next({ status: import_device_management_kit3.DeviceActionStatus.Stopped });
4594
3924
  subject.complete();
4595
3925
  }
4596
3926
  };
@@ -4683,7 +4013,7 @@ var DeviceApps = class {
4683
4013
  seTargetId: v.seTargetId,
4684
4014
  mcuTargetId: v.mcuTargetId,
4685
4015
  seVersion: v.seVersion,
4686
- seFlagsHex: bytesToHex4(v.seFlags),
4016
+ seFlagsHex: bytesToHex2(v.seFlags),
4687
4017
  mcuSephVersion: v.mcuSephVersion,
4688
4018
  mcuBootloaderVersion: v.mcuBootloaderVersion,
4689
4019
  hwVersion: v.hwVersion
@@ -4757,7 +4087,7 @@ var DeviceApps = class {
4757
4087
  }
4758
4088
  }
4759
4089
  };
4760
- function bytesToHex4(bytes) {
4090
+ function bytesToHex2(bytes) {
4761
4091
  if (!bytes) return "";
4762
4092
  return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
4763
4093
  }
@@ -4800,11 +4130,10 @@ var METHOD_PREFIX_TO_APP_NAME = {
4800
4130
  evm: "Ethereum",
4801
4131
  btc: "Bitcoin",
4802
4132
  sol: "Solana",
4803
- tron: "Tron",
4804
- zcash: "Zcash"
4133
+ tron: "Tron"
4805
4134
  };
4806
4135
  var HARDWARE_ERROR_CODE_VALUES = new Set(
4807
- Object.values(import_hwk_adapter_core12.HardwareErrorCode).filter((value) => typeof value === "number")
4136
+ Object.values(import_hwk_adapter_core13.HardwareErrorCode).filter((value) => typeof value === "number")
4808
4137
  );
4809
4138
  var BLE_CONNECT_SCAN_TIMEOUT_MS = 1500;
4810
4139
  var LEDGER_RELAY_ALLOWED_ROOT_DOMAINS = ["onekeytest.com", "onekey.com"];
@@ -4841,10 +4170,6 @@ async function defaultLedgerKitImporter(pkg) {
4841
4170
  return import("@ledgerhq/device-signer-kit-bitcoin");
4842
4171
  case "@ledgerhq/device-signer-kit-solana":
4843
4172
  return import("@ledgerhq/device-signer-kit-solana");
4844
- case "@ledgerhq/device-signer-kit-tron":
4845
- return import("@ledgerhq/device-signer-kit-tron");
4846
- case "@ledgerhq/device-signer-kit-zcash":
4847
- return import("@ledgerhq/device-signer-kit-zcash");
4848
4173
  case "@ledgerhq/context-module":
4849
4174
  return import("@ledgerhq/context-module");
4850
4175
  default:
@@ -4904,7 +4229,6 @@ var LedgerConnectorBase = class {
4904
4229
  this._ctx = {
4905
4230
  emit: (event, data) => this._emit(event, data),
4906
4231
  invalidateSession: (sid) => this._invalidateSession(sid),
4907
- teardownSecureChannelSession: (sid) => this._teardownSecureChannelSession(sid),
4908
4232
  wrapError: (err, opts) => this._wrapError(err, opts),
4909
4233
  getOrCreateDmk: () => this._getOrCreateDmk(),
4910
4234
  getDeviceManager: () => this._getDeviceManager(),
@@ -4956,7 +4280,7 @@ var LedgerConnectorBase = class {
4956
4280
  async searchDevices() {
4957
4281
  const dm = await this._getDeviceManager();
4958
4282
  const descriptors = await this._discoverDescriptors(dm);
4959
- const resolvedDescriptors = descriptors.filter((d) => !(0, import_hwk_adapter_core12.isKnownNonTargetHardwareVendor)(d, "ledger")).map((d) => ({
4283
+ const resolvedDescriptors = descriptors.filter((d) => !(0, import_hwk_adapter_core13.isKnownNonTargetHardwareVendor)(d, "ledger")).map((d) => ({
4960
4284
  descriptor: d,
4961
4285
  connectId: this._resolveConnectId(d)
4962
4286
  }));
@@ -5004,7 +4328,7 @@ var LedgerConnectorBase = class {
5004
4328
  `Ledger BLE connect did not return within ${HANG_CEILING_MS / 6e4}min \u2014 DMK hang fallback.`
5005
4329
  );
5006
4330
  err._tag = ERROR_TAG.BlePairingTimeout;
5007
- err.code = import_hwk_adapter_core12.HardwareErrorCode.BlePairingTimeout;
4331
+ err.code = import_hwk_adapter_core13.HardwareErrorCode.BlePairingTimeout;
5008
4332
  reject(err);
5009
4333
  }, HANG_CEILING_MS);
5010
4334
  });
@@ -5037,7 +4361,7 @@ var LedgerConnectorBase = class {
5037
4361
  "Ledger device is not currently advertising. Wake up and unlock the device, keep it nearby, then try again."
5038
4362
  );
5039
4363
  err._tag = ERROR_TAG.DeviceNotAdvertising;
5040
- err.code = import_hwk_adapter_core12.HardwareErrorCode.DeviceNotFound;
4364
+ err.code = import_hwk_adapter_core13.HardwareErrorCode.DeviceNotFound;
5041
4365
  throw err;
5042
4366
  };
5043
4367
  const doConnect = async (path) => {
@@ -5094,14 +4418,14 @@ var LedgerConnectorBase = class {
5094
4418
  this._resetSignersAndSessions();
5095
4419
  if (isLedgerBleConnectionType(this.connectionType)) {
5096
4420
  const tag = err?._tag;
5097
- if (isKnownConnectionTag(tag) && !isConnectionOpeningTag(tag)) {
4421
+ if (isKnownConnectionTag(tag)) {
5098
4422
  throw err;
5099
4423
  }
5100
4424
  const wrapped = new Error(
5101
4425
  "Ledger Bluetooth pairing failed. Make sure the device is unlocked and nearby, then try again."
5102
4426
  );
5103
4427
  wrapped._tag = ERROR_TAG.BleGattBondingFailed;
5104
- wrapped.code = import_hwk_adapter_core12.HardwareErrorCode.BlePairingTimeout;
4428
+ wrapped.code = import_hwk_adapter_core13.HardwareErrorCode.BlePairingTimeout;
5105
4429
  wrapped.originalError = err;
5106
4430
  throw wrapped;
5107
4431
  }
@@ -5179,7 +4503,7 @@ var LedgerConnectorBase = class {
5179
4503
  this._unwatchSessionState(sessionId);
5180
4504
  this._signerManager?.invalidate(sessionId);
5181
4505
  this._cancellers.get(sessionId)?.({
5182
- code: import_hwk_adapter_core12.HardwareErrorCode.DeviceDisconnected,
4506
+ code: import_hwk_adapter_core13.HardwareErrorCode.DeviceDisconnected,
5183
4507
  tag: "DeviceDisconnected",
5184
4508
  message: "Device disconnected"
5185
4509
  });
@@ -5203,9 +4527,9 @@ var LedgerConnectorBase = class {
5203
4527
  if (isAppStuckByApdu(err)) {
5204
4528
  return {
5205
4529
  success: false,
5206
- error: (0, import_hwk_adapter_core12.serializeConnectorError)(
4530
+ error: (0, import_hwk_adapter_core13.serializeConnectorError)(
5207
4531
  Object.assign(new Error("Ledger app is unresponsive"), {
5208
- code: import_hwk_adapter_core12.HardwareErrorCode.DeviceAppStuck,
4532
+ code: import_hwk_adapter_core13.HardwareErrorCode.DeviceAppStuck,
5209
4533
  _tag: ERROR_TAG.DeviceAppStuck,
5210
4534
  originalError: err
5211
4535
  })
@@ -5215,16 +4539,16 @@ var LedgerConnectorBase = class {
5215
4539
  if (isTransportStuck(err)) {
5216
4540
  return {
5217
4541
  success: false,
5218
- error: (0, import_hwk_adapter_core12.serializeConnectorError)(
4542
+ error: (0, import_hwk_adapter_core13.serializeConnectorError)(
5219
4543
  Object.assign(new Error("Device communication interrupted, please retry"), {
5220
- code: import_hwk_adapter_core12.HardwareErrorCode.TransportError,
4544
+ code: import_hwk_adapter_core13.HardwareErrorCode.TransportError,
5221
4545
  _tag: ERROR_TAG.DeviceTransportStuck,
5222
4546
  originalError: err
5223
4547
  })
5224
4548
  )
5225
4549
  };
5226
4550
  }
5227
- return { success: false, error: (0, import_hwk_adapter_core12.serializeConnectorError)(err) };
4551
+ return { success: false, error: (0, import_hwk_adapter_core13.serializeConnectorError)(err) };
5228
4552
  }
5229
4553
  }
5230
4554
  async _dispatch(sessionId, method, params) {
@@ -5263,11 +4587,6 @@ var LedgerConnectorBase = class {
5263
4587
  return solSignTransaction(ctx, sessionId, params);
5264
4588
  case "solSignMessage":
5265
4589
  return solSignMessage(ctx, sessionId, params);
5266
- // ZCASH
5267
- case "zcashGetFullViewingKey":
5268
- return zcashGetFullViewingKey(ctx, sessionId, params);
5269
- case "zcashGetShieldedAddress":
5270
- return zcashGetShieldedAddress(ctx, sessionId, params);
5271
4590
  // TRON
5272
4591
  case "tronGetAddress":
5273
4592
  return tronGetAddress(ctx, sessionId, params);
@@ -5292,7 +4611,7 @@ var LedgerConnectorBase = class {
5292
4611
  try {
5293
4612
  return await apps.install(p.appName, ({ progress }) => {
5294
4613
  ctx.emit("ui-event", {
5295
- type: import_hwk_adapter_core12.EConnectorInteraction.AppInstallProgress,
4614
+ type: import_hwk_adapter_core13.EConnectorInteraction.AppInstallProgress,
5296
4615
  payload: {
5297
4616
  sessionId,
5298
4617
  appName: p.appName,
@@ -5301,7 +4620,7 @@ var LedgerConnectorBase = class {
5301
4620
  });
5302
4621
  });
5303
4622
  } catch (err) {
5304
- ctx.teardownSecureChannelSession(sessionId);
4623
+ ctx.invalidateSession(sessionId);
5305
4624
  throw ctx.wrapError(err);
5306
4625
  } finally {
5307
4626
  ctx.clearCanceller(sessionId);
@@ -5391,7 +4710,7 @@ var LedgerConnectorBase = class {
5391
4710
  );
5392
4711
  return { isGenuine: output.isGenuine, deviceId };
5393
4712
  } catch (err) {
5394
- ctx.teardownSecureChannelSession(sessionId);
4713
+ ctx.invalidateSession(sessionId);
5395
4714
  throw ctx.wrapError(err);
5396
4715
  } finally {
5397
4716
  ctx.clearCanceller(sessionId);
@@ -5510,26 +4829,9 @@ var LedgerConnectorBase = class {
5510
4829
  }
5511
4830
  return this._deviceAppsManager;
5512
4831
  }
5513
- // DeviceAppsManager is a per-call factory with no cached session state, so
5514
- // there is nothing to invalidate for it here.
5515
4832
  _invalidateSession(sessionId) {
5516
4833
  this._signerManager?.invalidate(sessionId);
5517
4834
  }
5518
- /**
5519
- * Fire the canceller so DMK closes the secure-channel WebSocket. The DMK device
5520
- * session is kept: unlock/retry recovery reuses it.
5521
- */
5522
- _teardownSecureChannelSession(sessionId) {
5523
- const cancel = this._cancellers.get(sessionId);
5524
- this._cancellers.delete(sessionId);
5525
- if (cancel) {
5526
- try {
5527
- cancel();
5528
- } catch {
5529
- }
5530
- }
5531
- this._invalidateSession(sessionId);
5532
- }
5533
4835
  /**
5534
4836
  * Replace an old session with a new one after app switch.
5535
4837
  * Emits device-connect so the adapter updates its _sessions Map.
@@ -5614,7 +4916,7 @@ var LedgerConnectorBase = class {
5614
4916
  * at every catch site. Falls through unchanged for unknown methods.
5615
4917
  */
5616
4918
  _ctxForMethod(method) {
5617
- const prefix = /^(evm|btc|sol|tron|zcash)/.exec(method)?.[1];
4919
+ const prefix = /^(evm|btc|sol|tron)/.exec(method)?.[1];
5618
4920
  const defaultAppName = prefix ? METHOD_PREFIX_TO_APP_NAME[prefix] : void 0;
5619
4921
  if (!defaultAppName) return this._ctx;
5620
4922
  return {
@@ -5648,204 +4950,6 @@ var LedgerConnectorBase = class {
5648
4950
  return error;
5649
4951
  }
5650
4952
  };
5651
-
5652
- // src/transport/DmkTransport.ts
5653
- var import_hw_transport = __toESM(require("@ledgerhq/hw-transport"));
5654
- var DmkTransport = class extends import_hw_transport.default {
5655
- constructor(dmk, sessionId) {
5656
- super();
5657
- this._dmk = dmk;
5658
- this._sessionId = sessionId;
5659
- }
5660
- async exchange(apdu) {
5661
- const response = await this._dmk.sendApdu({
5662
- sessionId: this._sessionId,
5663
- apdu: new Uint8Array(apdu)
5664
- });
5665
- const { data, statusCode } = response;
5666
- const result = Buffer.alloc(data.length + 2);
5667
- if (data.length > 0) {
5668
- result.set(data, 0);
5669
- }
5670
- result.set(statusCode, data.length);
5671
- return result;
5672
- }
5673
- async close() {
5674
- }
5675
- };
5676
-
5677
- // src/app/AppManager.ts
5678
- var import_device_management_kit3 = require("@ledgerhq/device-management-kit");
5679
- var import_hwk_adapter_core13 = require("@onekeyfe/hwk-adapter-core");
5680
- var APP_NAME_MAP = {
5681
- ETH: "Ethereum",
5682
- BTC: "Bitcoin",
5683
- SOL: "Solana",
5684
- TRX: "Tron",
5685
- ZEC: "Zcash",
5686
- XRP: "XRP",
5687
- ADA: "Cardano",
5688
- DOT: "Polkadot",
5689
- ATOM: "Cosmos"
5690
- };
5691
- var DASHBOARD_APP_NAME = "BOLOS";
5692
- var AppManager = class {
5693
- constructor(dmk, options) {
5694
- this._dmk = dmk;
5695
- this._waitMs = options?.waitMs ?? 1e3;
5696
- this._maxRetries = options?.maxRetries ?? 10;
5697
- }
5698
- /**
5699
- * Return the Ledger app name for a given chain ticker,
5700
- * or undefined if the chain is not supported.
5701
- */
5702
- static getAppName(chain) {
5703
- return APP_NAME_MAP[chain];
5704
- }
5705
- /**
5706
- * Ensure the target app is open on the device identified by `sessionId`.
5707
- *
5708
- * Flow:
5709
- * 1. Check the currently running app.
5710
- * 2. If it is already the target, return immediately.
5711
- * 3. If a different app is running (not dashboard), close it first.
5712
- * 4. Open the target app.
5713
- * 5. Poll until the device confirms the target app is running.
5714
- */
5715
- /**
5716
- * @param onConfirmOnDevice Called BEFORE OpenAppCommand is issued — the
5717
- * device is about to display "Open <app>" on screen and wait for the
5718
- * user's button press. UI consumers should show their "open app" prompt
5719
- * in response. NOT called when the target app is already open (no user
5720
- * interaction needed in that case).
5721
- *
5722
- * Important: OpenAppCommand is blocking. It does not resolve until the user
5723
- * has physically confirmed on the device, so anything that runs AFTER
5724
- * `await this._openApp(...)` lands AFTER the prompt is already gone.
5725
- * Hence the callback must fire BEFORE that await.
5726
- */
5727
- async ensureAppOpen(sessionId, targetAppName, onConfirmOnDevice) {
5728
- const currentApp = await this._getCurrentApp(sessionId);
5729
- if (currentApp === targetAppName) {
5730
- return;
5731
- }
5732
- if (!this._isDashboard(currentApp)) {
5733
- await this._closeCurrentApp(sessionId);
5734
- await this._waitForApp(sessionId, DASHBOARD_APP_NAME);
5735
- }
5736
- onConfirmOnDevice?.();
5737
- await this._openApp(sessionId, targetAppName);
5738
- await this._waitForApp(sessionId, targetAppName);
5739
- }
5740
- // ---------------------------------------------------------------------------
5741
- // Private helpers
5742
- // ---------------------------------------------------------------------------
5743
- async _getCurrentApp(sessionId) {
5744
- const result = await this._dmk.sendCommand({
5745
- sessionId,
5746
- command: new import_device_management_kit3.GetAppAndVersionCommand()
5747
- });
5748
- if ((0, import_device_management_kit3.isSuccessCommandResult)(result)) {
5749
- debugLog("[AppManager] currentApp:", result.data.name);
5750
- return result.data.name;
5751
- }
5752
- const errResult = result;
5753
- const dmkErr = errResult.error ?? {};
5754
- const original = dmkErr.originalError;
5755
- debugLog(
5756
- "[AppManager] _getCurrentApp failed sessionId=",
5757
- sessionId,
5758
- "tag=",
5759
- dmkErr._tag,
5760
- "errorCode=",
5761
- dmkErr.errorCode,
5762
- "message=",
5763
- dmkErr.message,
5764
- "originalErrorMessage=",
5765
- original?.message ?? String(original ?? "")
5766
- );
5767
- throw Object.assign(
5768
- new Error(
5769
- dmkErr.message ?? "Failed to get current app from device"
5770
- ),
5771
- {
5772
- _tag: dmkErr._tag,
5773
- errorCode: dmkErr.errorCode,
5774
- originalError: original
5775
- }
5776
- );
5777
- }
5778
- async _openApp(sessionId, appName) {
5779
- const result = await this._dmk.sendCommand({
5780
- sessionId,
5781
- command: new import_device_management_kit3.OpenAppCommand({ appName })
5782
- });
5783
- if (!(0, import_device_management_kit3.isSuccessCommandResult)(result)) {
5784
- const dmkErr = result.error;
5785
- const errorCode = "errorCode" in dmkErr && dmkErr.errorCode != null ? String(dmkErr.errorCode) : "";
5786
- const message = "message" in dmkErr && typeof dmkErr.message === "string" ? dmkErr.message : "";
5787
- debugLog(
5788
- "[AppManager] openApp failed:",
5789
- appName,
5790
- "errorCode:",
5791
- errorCode,
5792
- "tag:",
5793
- dmkErr._tag
5794
- );
5795
- let code;
5796
- if (errorCode === "6807" || /unknown application/i.test(message)) {
5797
- code = import_hwk_adapter_core13.HardwareErrorCode.AppNotInstalled;
5798
- } else if (errorCode === "5501" || dmkErr._tag === "ActionRefusedError") {
5799
- code = import_hwk_adapter_core13.HardwareErrorCode.UserRejected;
5800
- }
5801
- throw Object.assign(new Error(`Failed to open "${appName}"`), {
5802
- _tag: ERROR_TAG.OpenAppCommand,
5803
- code,
5804
- errorCode,
5805
- statusCode: errorCode,
5806
- appName,
5807
- originalError: dmkErr
5808
- });
5809
- }
5810
- }
5811
- async _closeCurrentApp(sessionId) {
5812
- debugLog("[AppManager] closeCurrentApp");
5813
- await this._dmk.sendCommand({
5814
- sessionId,
5815
- command: new import_device_management_kit3.CloseAppCommand()
5816
- });
5817
- }
5818
- /**
5819
- * Poll the device until the expected app is reported as running,
5820
- * or throw after `_maxRetries` attempts.
5821
- */
5822
- async _waitForApp(sessionId, expectedAppName) {
5823
- let lastSeen = "";
5824
- for (let i = 0; i < this._maxRetries; i++) {
5825
- await this._wait();
5826
- const current = await this._getCurrentApp(sessionId);
5827
- lastSeen = current;
5828
- if (current === expectedAppName) {
5829
- return;
5830
- }
5831
- }
5832
- debugLog(
5833
- "[AppManager] waitForApp exhausted: expected=",
5834
- expectedAppName,
5835
- "lastSeen=",
5836
- lastSeen
5837
- );
5838
- throw new Error(
5839
- `Ledger: failed to open "${expectedAppName}" after ${this._maxRetries} retries (last seen: ${lastSeen})`
5840
- );
5841
- }
5842
- _isDashboard(appName) {
5843
- return appName === DASHBOARD_APP_NAME;
5844
- }
5845
- _wait() {
5846
- return new Promise((resolve) => setTimeout(resolve, this._waitMs));
5847
- }
5848
- };
5849
4953
  // Annotate the CommonJS export names for ESM import in node:
5850
4954
  0 && (module.exports = {
5851
4955
  AppManager,
@@ -5857,7 +4961,6 @@ var AppManager = class {
5857
4961
  SignerEth,
5858
4962
  SignerManager,
5859
4963
  SignerSol,
5860
- SignerZcash,
5861
4964
  debugLog,
5862
4965
  deviceActionToPromise,
5863
4966
  isDeviceLockedError,