@onekeyfe/hwk-ledger-adapter 1.1.26-alpha.9 → 1.1.26-patch.1

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.mjs CHANGED
@@ -3,9 +3,11 @@ import {
3
3
  CHAIN_FINGERPRINT_PATHS,
4
4
  DEVICE,
5
5
  DeviceJobQueue,
6
+ EConnectorInteraction,
6
7
  HardwareErrorCode as HardwareErrorCode2,
7
8
  TypedEventEmitter,
8
9
  UI_REQUEST,
10
+ UI_REQUEST_PREEMPTED_TAG,
9
11
  UiRequestRegistry,
10
12
  deriveDeviceFingerprint,
11
13
  failure,
@@ -14,24 +16,32 @@ import {
14
16
 
15
17
  // src/errors.ts
16
18
  import { HardwareErrorCode, enrichErrorMessage } from "@onekeyfe/hwk-adapter-core";
17
- function ledgerFailure(code, error, appName) {
19
+ var MULTIPLE_USB_LEDGER_DEVICES_ERROR_MESSAGE = "Multiple Ledger USB devices are connected. Please connect only one Ledger device and try again.";
20
+ function createMultipleUsbLedgerDevicesError() {
21
+ return Object.assign(new Error(MULTIPLE_USB_LEDGER_DEVICES_ERROR_MESSAGE), {
22
+ code: HardwareErrorCode.DeviceOneDeviceOnly
23
+ });
24
+ }
25
+ function ledgerFailure(code, error, appName, tag, params) {
18
26
  const payload = { error, code };
19
27
  if (appName !== void 0) payload.appName = appName;
28
+ if (tag !== void 0) payload._tag = tag;
29
+ if (params !== void 0) payload.params = params;
20
30
  return { success: false, payload };
21
31
  }
22
32
  var LOCKED_ERROR_CODES = /* @__PURE__ */ new Set(["5515", "21781", "6982", "27010", "5303", "21251"]);
23
33
  var USER_REJECTED_CODES = /* @__PURE__ */ new Set(["6985", "27013"]);
24
34
  var WRONG_APP_CODES = /* @__PURE__ */ new Set(["6e00", "28160", "6d00", "27904", "6a83", "27267"]);
25
35
  var APP_NOT_INSTALLED_CODES = /* @__PURE__ */ new Set(["6807", "26631"]);
26
- var STEP_BLIND_SIGN_FALLBACK = "signer.eth.steps.blindSignTransactionFallback";
36
+ var STEP_BLIND_SIGN_TRANSACTION_FALLBACK = "signer.eth.steps.blindSignTransactionFallback";
27
37
  function getEthAppErrorCode(err) {
28
38
  if (!err || typeof err !== "object") return null;
29
39
  const e = err;
30
- if (e._tag === "EthAppCommandError" && typeof e.errorCode === "string") {
40
+ if (e._tag === ERROR_TAG.EthAppCommand && typeof e.errorCode === "string") {
31
41
  return e.errorCode.toLowerCase();
32
42
  }
33
43
  const orig = e.originalError;
34
- if (orig?._tag === "EthAppCommandError" && typeof orig.errorCode === "string") {
44
+ if (orig?._tag === ERROR_TAG.EthAppCommand && typeof orig.errorCode === "string") {
35
45
  return orig.errorCode.toLowerCase();
36
46
  }
37
47
  return null;
@@ -88,13 +98,8 @@ function mapBtcAppError(hex) {
88
98
  return null;
89
99
  }
90
100
  }
91
- function mapEthAppError(ethCode, lastStep) {
101
+ function mapEthAppErrorCode(ethCode) {
92
102
  switch (ethCode) {
93
- case "6a80":
94
- if (lastStep === STEP_BLIND_SIGN_FALLBACK) {
95
- return HardwareErrorCode.EvmBlindSigningRequired;
96
- }
97
- return null;
98
103
  case "6984":
99
104
  return HardwareErrorCode.EvmClearSignPluginMissing;
100
105
  case "6a84":
@@ -107,16 +112,133 @@ function mapEthAppError(ethCode, lastStep) {
107
112
  return null;
108
113
  }
109
114
  }
115
+ function classifyEthAppError(err) {
116
+ const ethCode = getEthAppErrorCode(err);
117
+ if (!ethCode) return null;
118
+ if (ethCode === "6a80") {
119
+ return hasBlindSignFallbackStep(err) ? HardwareErrorCode.EvmBlindSigningRequired : null;
120
+ }
121
+ return mapEthAppErrorCode(ethCode);
122
+ }
123
+ var ERROR_TAG = {
124
+ // SDK-mint
125
+ DeviceNotAdvertising: "DeviceNotAdvertisingError",
126
+ // BLE scan miss
127
+ DeviceNotInDiscoveryCache: "DeviceNotInDiscoveryCacheError",
128
+ // dm.connect() before enumerate
129
+ BlePairingTimeout: "BlePairingTimeoutError",
130
+ // SMP 30s timeout
131
+ BleGattBondingFailed: "BleGattBondingFailedError",
132
+ // other GATT failure
133
+ UserAborted: "UserAborted",
134
+ DeviceAppStuck: "DeviceAppStuck",
135
+ // chain app wedged (APDU 0x6901)
136
+ DeviceTransportStuck: "DeviceTransportStuck",
137
+ // DMK transport queue wedged
138
+ // DMK-reuse (DMK throws same string; we synthesize too)
139
+ DeviceLocked: "DeviceLockedError",
140
+ DeviceNotRecognized: "DeviceNotRecognizedError",
141
+ DeviceSessionNotFound: "DeviceSessionNotFound",
142
+ OpenAppCommand: "OpenAppCommandError",
143
+ // DMK-only (read only)
144
+ EthAppCommand: "EthAppCommandError",
145
+ UserRefusedOnDevice: "UserRefusedOnDevice",
146
+ WrongAppOpened: "WrongAppOpenedError",
147
+ InvalidStatusWord: "InvalidStatusWordError",
148
+ AlreadySendingApdu: "AlreadySendingApduError",
149
+ UnknownDeviceExchange: "UnknownDeviceExchangeError",
150
+ NoAccessibleDevice: "NoAccessibleDeviceError",
151
+ UnknownDevice: "UnknownDeviceError",
152
+ DeviceSessionRefresher: "DeviceSessionRefresherError",
153
+ DeviceNotInitialized: "DeviceNotInitializedError",
154
+ OpeningConnection: "OpeningConnectionError",
155
+ DeviceDisconnectedBeforeSendingApdu: "DeviceDisconnectedBeforeSendingApdu",
156
+ DeviceDisconnectedWhileSending: "DeviceDisconnectedWhileSendingError",
157
+ Disconnect: "DisconnectError",
158
+ ReconnectionFailed: "ReconnectionFailedError",
159
+ WebHIDDisconnect: "WebHIDDisconnectError",
160
+ // ble-plx surfaces this when GATT notification setup fails — typical
161
+ // outcome when the user doesn't confirm pairing on the device, or the
162
+ // existing bond is invalid. Observed in production after ~30s.
163
+ PairingRefused: "PairingRefusedError"
164
+ };
110
165
  function isDeviceLockedError(err) {
111
166
  if (!err || typeof err !== "object") return false;
112
167
  const e = err;
113
168
  if (e.errorCode != null && LOCKED_ERROR_CODES.has(String(e.errorCode))) return true;
114
169
  if (e.statusCode != null && LOCKED_ERROR_CODES.has(String(e.statusCode))) return true;
115
- if (e._tag === "DeviceLockedError") return true;
170
+ if (e._tag === ERROR_TAG.DeviceLocked) return true;
116
171
  if (e.originalError != null && isDeviceLockedError(e.originalError)) return true;
117
172
  if (e.error != null && e._tag && isDeviceLockedError(e.error)) return true;
118
173
  return false;
119
174
  }
175
+ function isDeviceNotAdvertisingError(err) {
176
+ if (!err || typeof err !== "object") return false;
177
+ return err._tag === ERROR_TAG.DeviceNotAdvertising;
178
+ }
179
+ var PAIRING_FAILURE_TAGS = /* @__PURE__ */ new Set([
180
+ ERROR_TAG.BlePairingTimeout,
181
+ ERROR_TAG.BleGattBondingFailed,
182
+ ERROR_TAG.PairingRefused
183
+ ]);
184
+ function isBlePairingFailureError(err) {
185
+ if (!err || typeof err !== "object") return false;
186
+ const tag = err._tag;
187
+ if (tag && PAIRING_FAILURE_TAGS.has(tag)) return true;
188
+ const orig = err.originalError;
189
+ if (orig != null && isBlePairingFailureError(orig)) return true;
190
+ return false;
191
+ }
192
+ var CONNECTION_LEVEL_TAGS = /* @__PURE__ */ new Set([
193
+ ERROR_TAG.DeviceLocked,
194
+ ERROR_TAG.DeviceNotAdvertising,
195
+ ERROR_TAG.BlePairingTimeout,
196
+ ERROR_TAG.BleGattBondingFailed,
197
+ ERROR_TAG.PairingRefused,
198
+ ERROR_TAG.DeviceNotRecognized,
199
+ ERROR_TAG.NoAccessibleDevice,
200
+ ERROR_TAG.UnknownDevice,
201
+ ERROR_TAG.DeviceSessionNotFound,
202
+ ERROR_TAG.DeviceSessionRefresher,
203
+ ERROR_TAG.DeviceNotInitialized,
204
+ ERROR_TAG.OpeningConnection,
205
+ ERROR_TAG.DeviceDisconnectedBeforeSendingApdu,
206
+ ERROR_TAG.DeviceDisconnectedWhileSending,
207
+ ERROR_TAG.Disconnect,
208
+ ERROR_TAG.ReconnectionFailed,
209
+ ERROR_TAG.WebHIDDisconnect
210
+ ]);
211
+ var DEVICE_NOT_FOUND_TAGS = /* @__PURE__ */ new Set([
212
+ ERROR_TAG.NoAccessibleDevice,
213
+ ERROR_TAG.UnknownDevice,
214
+ ERROR_TAG.DeviceNotInitialized,
215
+ // SDK-internal: dm.connect() called before _discovered was populated.
216
+ // Map to DeviceNotFound so non-BLE-direct paths get a sensible error code.
217
+ ERROR_TAG.DeviceNotInDiscoveryCache
218
+ ]);
219
+ var DEVICE_BUSY_TAGS = /* @__PURE__ */ new Set([ERROR_TAG.OpeningConnection]);
220
+ var DEVICE_DISCONNECTED_TAGS = /* @__PURE__ */ new Set([
221
+ ERROR_TAG.DeviceNotRecognized,
222
+ ERROR_TAG.DeviceSessionNotFound,
223
+ ERROR_TAG.DeviceSessionRefresher,
224
+ ERROR_TAG.DeviceDisconnectedBeforeSendingApdu,
225
+ ERROR_TAG.DeviceDisconnectedWhileSending,
226
+ ERROR_TAG.Disconnect,
227
+ ERROR_TAG.ReconnectionFailed,
228
+ ERROR_TAG.WebHIDDisconnect
229
+ ]);
230
+ function isConnectionLevelError(err) {
231
+ if (!err || typeof err !== "object") return false;
232
+ const tag = err._tag;
233
+ if (tag && CONNECTION_LEVEL_TAGS.has(tag)) return true;
234
+ const e = err;
235
+ if (e.originalError != null && isConnectionLevelError(e.originalError)) return true;
236
+ if (e.error != null && e._tag && isConnectionLevelError(e.error)) return true;
237
+ return false;
238
+ }
239
+ function isKnownConnectionTag(tag) {
240
+ return typeof tag === "string" && CONNECTION_LEVEL_TAGS.has(tag);
241
+ }
120
242
  function hasStatusCode(err, codeSet) {
121
243
  if (!err || typeof err !== "object") return false;
122
244
  const e = err;
@@ -126,10 +248,39 @@ function hasStatusCode(err, codeSet) {
126
248
  if (e.error != null && e._tag && hasStatusCode(e.error, codeSet)) return true;
127
249
  return false;
128
250
  }
251
+ function hasBlindSignFallbackStep(err) {
252
+ if (!err || typeof err !== "object") return false;
253
+ const e = err;
254
+ if (e._lastStep === STEP_BLIND_SIGN_TRANSACTION_FALLBACK) return true;
255
+ if (Array.isArray(e._deviceActionSteps) && e._deviceActionSteps.includes(STEP_BLIND_SIGN_TRANSACTION_FALLBACK)) {
256
+ return true;
257
+ }
258
+ if (e.originalError != null && hasBlindSignFallbackStep(e.originalError)) return true;
259
+ if (e.error != null && e._tag && hasBlindSignFallbackStep(e.error)) return true;
260
+ return false;
261
+ }
262
+ function isDeviceNotFoundError(err) {
263
+ if (!err || typeof err !== "object") return false;
264
+ const tag = err._tag;
265
+ if (tag && DEVICE_NOT_FOUND_TAGS.has(tag)) return true;
266
+ const e = err;
267
+ if (e.originalError != null && isDeviceNotFoundError(e.originalError)) return true;
268
+ if (e.error != null && e._tag && isDeviceNotFoundError(e.error)) return true;
269
+ return false;
270
+ }
271
+ function isDeviceBusyError(err) {
272
+ if (!err || typeof err !== "object") return false;
273
+ const tag = err._tag;
274
+ if (tag && DEVICE_BUSY_TAGS.has(tag)) return true;
275
+ const e = err;
276
+ if (e.originalError != null && isDeviceBusyError(e.originalError)) return true;
277
+ if (e.error != null && e._tag && isDeviceBusyError(e.error)) return true;
278
+ return false;
279
+ }
129
280
  function isUserRejectedError(err) {
130
281
  if (!err || typeof err !== "object") return false;
131
282
  const e = err;
132
- if (e._tag === "UserRefusedOnDevice") return true;
283
+ if (e._tag === ERROR_TAG.UserRefusedOnDevice) return true;
133
284
  if (typeof e.message === "string" && /denied|rejected|refused/i.test(e.message)) return true;
134
285
  if (hasStatusCode(err, USER_REJECTED_CODES)) return true;
135
286
  return false;
@@ -137,7 +288,7 @@ function isUserRejectedError(err) {
137
288
  function isWrongAppError(err) {
138
289
  if (!err || typeof err !== "object") return false;
139
290
  const e = err;
140
- if (e._tag === "WrongAppOpenedError" || e._tag === "InvalidStatusWordError") {
291
+ if (e._tag === ERROR_TAG.WrongAppOpened || e._tag === ERROR_TAG.InvalidStatusWord) {
141
292
  if (hasStatusCode(err, WRONG_APP_CODES)) return true;
142
293
  }
143
294
  if (typeof e.message === "string") {
@@ -151,7 +302,6 @@ function isWrongAppError(err) {
151
302
  function isAppNotInstalledError(err) {
152
303
  if (!err || typeof err !== "object") return false;
153
304
  const e = err;
154
- if (e._tag === "OpenAppCommandError") return true;
155
305
  if (typeof e.message === "string" && /unknown application/i.test(e.message)) return true;
156
306
  if (hasStatusCode(err, APP_NOT_INSTALLED_CODES)) return true;
157
307
  return false;
@@ -159,7 +309,8 @@ function isAppNotInstalledError(err) {
159
309
  function isDeviceDisconnectedError(err) {
160
310
  if (!err || typeof err !== "object") return false;
161
311
  const e = err;
162
- if (e._tag === "DeviceNotRecognizedError" || e._tag === "DeviceSessionNotFound") return true;
312
+ const tag = e._tag;
313
+ if (typeof tag === "string" && DEVICE_DISCONNECTED_TAGS.has(tag)) return true;
163
314
  if (typeof e.message === "string") {
164
315
  const msg = e.message.toLowerCase();
165
316
  if (msg.includes("disconnected") || msg.includes("not found") || msg.includes("no device") || msg.includes("unplugged"))
@@ -179,6 +330,21 @@ function isTimeoutError(err) {
179
330
  if (e.code === HardwareErrorCode.OperationTimeout) return true;
180
331
  return false;
181
332
  }
333
+ function isAppStuckByApdu(err) {
334
+ if (!err || typeof err !== "object") return false;
335
+ const tag = err._tag;
336
+ if (tag === ERROR_TAG.DeviceAppStuck) return true;
337
+ return extractApduHex(err) === "6901";
338
+ }
339
+ function isTransportStuck(err) {
340
+ if (!err || typeof err !== "object") return false;
341
+ const tag = err._tag;
342
+ return tag === ERROR_TAG.DeviceTransportStuck || // already wrapped form
343
+ tag === ERROR_TAG.UnknownDeviceExchange || tag === ERROR_TAG.AlreadySendingApdu;
344
+ }
345
+ function isStuckAppStateError(err) {
346
+ return isAppStuckByApdu(err) || isTransportStuck(err);
347
+ }
182
348
  function mapLedgerError(err, opts) {
183
349
  let originalMessage = "Unknown Ledger error";
184
350
  if (err instanceof Error) {
@@ -190,20 +356,24 @@ function mapLedgerError(err, opts) {
190
356
  let code;
191
357
  if (isDeviceLockedError(err)) {
192
358
  code = HardwareErrorCode.DeviceLocked;
359
+ } else if (isDeviceNotAdvertisingError(err) || isDeviceNotFoundError(err)) {
360
+ code = HardwareErrorCode.DeviceNotFound;
361
+ } else if (isDeviceBusyError(err)) {
362
+ code = HardwareErrorCode.DeviceBusy;
363
+ } else if (isBlePairingFailureError(err)) {
364
+ code = HardwareErrorCode.BlePairingTimeout;
193
365
  } else if (isUserRejectedError(err)) {
194
366
  code = HardwareErrorCode.UserRejected;
195
367
  } else if (isWrongAppError(err)) {
196
368
  code = HardwareErrorCode.WrongApp;
197
369
  } else if (isAppNotInstalledError(err)) {
198
- code = HardwareErrorCode.AppNotOpen;
370
+ code = HardwareErrorCode.AppNotInstalled;
199
371
  } else if (isDeviceDisconnectedError(err)) {
200
372
  code = HardwareErrorCode.DeviceDisconnected;
201
373
  } else if (isTimeoutError(err)) {
202
374
  code = HardwareErrorCode.OperationTimeout;
203
375
  } else {
204
- const ethCode = getEthAppErrorCode(err);
205
- const lastStep = err && typeof err === "object" ? err._lastStep : void 0;
206
- const ethMapped = ethCode ? mapEthAppError(ethCode, lastStep) : null;
376
+ const ethMapped = classifyEthAppError(err);
207
377
  const apduHex = ethMapped ? null : extractApduHex(err);
208
378
  const chainMapped = apduHex ? mapSolanaAppError(apduHex) ?? mapTronAppError(apduHex) ?? mapBtcAppError(apduHex) : null;
209
379
  code = ethMapped ?? chainMapped ?? HardwareErrorCode.UnknownError;
@@ -213,38 +383,100 @@ function mapLedgerError(err, opts) {
213
383
  return { code, message: enrichErrorMessage(code, originalMessage), appName };
214
384
  }
215
385
 
216
- // src/utils/debugLog.ts
217
- var enabled = false;
218
- function setDebugEnabled(value) {
219
- enabled = value;
386
+ // src/utils/ledgerDmkTransport.ts
387
+ function isLedgerDmkBleTransport(transport) {
388
+ const normalized = transport?.toUpperCase();
389
+ return normalized === "BLE" || normalized?.endsWith("_BLE") === true;
220
390
  }
221
- function isDebugEnabled() {
222
- return enabled;
391
+ function isLedgerBleConnectionType(connectionType) {
392
+ return connectionType === "ble";
223
393
  }
224
- function debugLog(...args) {
225
- if (enabled) {
226
- console.debug(...args);
394
+ function isLedgerBleDescriptor(connectionType, descriptor) {
395
+ return isLedgerBleConnectionType(connectionType) || isLedgerDmkBleTransport(descriptor.transport);
396
+ }
397
+
398
+ // src/utils/sdkEventBus.ts
399
+ var listeners = /* @__PURE__ */ new Set();
400
+ function onSdkEvent(listener) {
401
+ listeners.add(listener);
402
+ return () => {
403
+ listeners.delete(listener);
404
+ };
405
+ }
406
+ function offSdkEvent(listener) {
407
+ listeners.delete(listener);
408
+ }
409
+ function emitSdkEvent(event) {
410
+ if (listeners.size === 0) return;
411
+ for (const listener of listeners) {
412
+ try {
413
+ listener(event);
414
+ } catch {
415
+ }
227
416
  }
228
417
  }
229
- function debugError(...args) {
230
- if (enabled) {
231
- console.error(...args);
418
+ function emitLog(level, ...args) {
419
+ if (listeners.size === 0) return;
420
+ const message = args.map((a) => typeof a === "string" ? a : safeStringify(a)).join(" ");
421
+ emitSdkEvent({ type: "log", level, message });
422
+ }
423
+ function safeStringify(value) {
424
+ if (value instanceof Error) {
425
+ return value.stack ? `${value.message}
426
+ ${value.stack}` : value.message;
427
+ }
428
+ try {
429
+ return JSON.stringify(value);
430
+ } catch {
431
+ return String(value);
232
432
  }
233
433
  }
234
434
 
435
+ // src/utils/debugLog.ts
436
+ function debugLog(...args) {
437
+ emitLog("debug", ...args);
438
+ }
439
+ function debugError(...args) {
440
+ emitLog("error", ...args);
441
+ }
442
+
235
443
  // src/adapter/LedgerAdapter.ts
236
444
  function formatDeviceMismatchError(expected, actual) {
237
445
  return `Wrong device: expected ${expected}, got ${actual}`;
238
446
  }
447
+ var BTC_HIGH_INDEX_THRESHOLD = 100;
448
+ function btcAccountIndexFromPath(path) {
449
+ const segments = path.replace(/^m\//, "").split("/");
450
+ if (segments.length < 3) return null;
451
+ const accountSeg = segments[2].replace(/['h]$/i, "");
452
+ const accountIndex = parseInt(accountSeg, 10);
453
+ return Number.isFinite(accountIndex) ? accountIndex : null;
454
+ }
239
455
  var _LedgerAdapter = class _LedgerAdapter {
240
- constructor(connector, options) {
456
+ constructor(connector) {
241
457
  this.vendor = "ledger";
242
458
  this.emitter = new TypedEventEmitter();
243
- // Device cache: tracks discovered devices from connector events
244
459
  this._discoveredDevices = /* @__PURE__ */ new Map();
245
- // Session tracking: maps connectId -> sessionId
246
460
  this._sessions = /* @__PURE__ */ new Map();
247
461
  this._uiRegistry = new UiRequestRegistry();
462
+ // BTC App rejects account index >= 100 unless display=true. Cached per
463
+ // adapter instance: first 100+ path asks the user once via UI request,
464
+ // subsequent 100+ paths in the same session auto-promote silently.
465
+ // The Ledger device itself still requires a per-call confirmation — that's
466
+ // the Ledger app's safety boundary, not ours to bypass.
467
+ this._btcHighIndexConfirmedThisSession = false;
468
+ // Shared across concurrent callers — only `cancel()` aborts.
469
+ this._doConnectAbortController = null;
470
+ // ---------------------------------------------------------------------------
471
+ // Private helpers
472
+ // ---------------------------------------------------------------------------
473
+ /**
474
+ * Ensure at least one device is connected and return a valid connectId.
475
+ *
476
+ * - If a session already exists for the given connectId, reuse it.
477
+ * - If ANY session exists (Ledger IDs are ephemeral), reuse it.
478
+ * - Otherwise: search → exactly 1 USB device auto-connects; multiple or none throws.
479
+ */
248
480
  // Mutex for ensureConnected — prevents concurrent calls from establishing duplicate connections
249
481
  this._connectingPromise = null;
250
482
  // ---------------------------------------------------------------------------
@@ -274,40 +506,23 @@ var _LedgerAdapter = class _LedgerAdapter {
274
506
  this.emitter.emit("ui-event", event);
275
507
  };
276
508
  this.connector = connector;
277
- this._handleSelectDevice = options?.handleSelectDevice ?? false;
278
- this._jobQueue = new DeviceJobQueue({
279
- emit: (event, data) => this.emitter.emit(event, data),
280
- uiRegistry: this._uiRegistry
281
- });
509
+ this._jobQueue = new DeviceJobQueue();
282
510
  this.registerEventListeners();
283
511
  }
284
- /**
285
- * Classify a method's interruptibility.
286
- * - Signing / typed data / transaction → 'confirm' (user may decide via preemption UI)
287
- * - Read-only queries (getAddress / getPublicKey / getMasterFingerprint) → 'safe'
288
- * (auto-cancels any pending read for the same device)
289
- */
290
- static _getInterruptibility(method) {
291
- if (method.toLowerCase().includes("sign")) return "confirm";
292
- return "safe";
293
- }
294
- // ---------------------------------------------------------------------------
295
512
  // Transport
296
- // ---------------------------------------------------------------------------
297
- // Transport is decided at connector creation time. These methods
298
- // satisfy the IHardwareWallet interface with sensible defaults.
299
513
  get activeTransport() {
300
- return this.connector.connectionType === "ble" ? "ble" : "hid";
514
+ return isLedgerBleConnectionType(this.connector.connectionType) ? "ble" : "hid";
301
515
  }
302
516
  getAvailableTransports() {
303
517
  return this.activeTransport ? [this.activeTransport] : [];
304
518
  }
305
- async switchTransport(_type) {
519
+ // Connector is bound at construction; switching requires a new adapter.
520
+ switchTransport(_type) {
521
+ return Promise.resolve();
306
522
  }
307
- // ---------------------------------------------------------------------------
308
523
  // Lifecycle
309
- // ---------------------------------------------------------------------------
310
- async init(_config) {
524
+ init(_config) {
525
+ return Promise.resolve();
311
526
  }
312
527
  /**
313
528
  * Clear cached device/session state without tearing down the adapter.
@@ -320,6 +535,7 @@ var _LedgerAdapter = class _LedgerAdapter {
320
535
  this._connectingPromise = null;
321
536
  this._uiRegistry.reset();
322
537
  this._jobQueue.clear();
538
+ this._btcHighIndexConfirmedThisSession = false;
323
539
  }
324
540
  async dispose() {
325
541
  this._uiRegistry.reset();
@@ -328,6 +544,7 @@ var _LedgerAdapter = class _LedgerAdapter {
328
544
  this.connector.reset();
329
545
  this._discoveredDevices.clear();
330
546
  this._sessions.clear();
547
+ this._btcHighIndexConfirmedThisSession = false;
331
548
  this.emitter.removeAllListeners();
332
549
  }
333
550
  uiResponse(response) {
@@ -336,10 +553,16 @@ var _LedgerAdapter = class _LedgerAdapter {
336
553
  // ---------------------------------------------------------------------------
337
554
  // Device management
338
555
  // ---------------------------------------------------------------------------
339
- async searchDevices() {
556
+ async searchDevices(options) {
557
+ if (options?.resetSession) {
558
+ this._doConnectAbortController?.abort();
559
+ this._sessions.clear();
560
+ this._connectingPromise = null;
561
+ this._doConnectAbortController = null;
562
+ this._btcHighIndexConfirmedThisSession = false;
563
+ }
340
564
  await this._ensureDevicePermission();
341
565
  const devices = await this.connector.searchDevices();
342
- debugLog("[DMK] adapter.searchDevices raw:", JSON.stringify(devices));
343
566
  this._discoveredDevices.clear();
344
567
  for (const d of devices) {
345
568
  if (d.connectId) {
@@ -349,11 +572,41 @@ var _LedgerAdapter = class _LedgerAdapter {
349
572
  if (this._discoveredDevices.size === 0) {
350
573
  await this._ensureDevicePermission();
351
574
  }
575
+ debugLog(
576
+ `[LedgerAdapter] searchDevices() return count=${this._discoveredDevices.size} ids=[${[
577
+ ...this._discoveredDevices.keys()
578
+ ].join(",")}]`
579
+ );
352
580
  return Array.from(this._discoveredDevices.values());
353
581
  }
582
+ // USB single-session invariant: evict all sessions, best-effort (see connectDevice).
583
+ async _evictAllSessions() {
584
+ if (this._sessions.size === 0) return;
585
+ const stale = [...this._sessions.values()];
586
+ this._sessions.clear();
587
+ for (const sid of stale) {
588
+ try {
589
+ await this.connector.disconnect(sid);
590
+ } catch {
591
+ }
592
+ }
593
+ }
594
+ static _createDeviceBusyError(method) {
595
+ return Object.assign(new Error(`Ledger device is busy while calling ${method}`), {
596
+ code: HardwareErrorCode2.DeviceBusy
597
+ });
598
+ }
354
599
  async connectDevice(connectId) {
355
- await this._ensureDevicePermission(connectId);
356
600
  try {
601
+ if (isLedgerBleConnectionType(this.connector.connectionType) && !connectId) {
602
+ throw Object.assign(new Error("Ledger BLE connectId is required."), {
603
+ code: HardwareErrorCode2.DeviceNotFound
604
+ });
605
+ }
606
+ if (!isLedgerBleConnectionType(this.connector.connectionType)) {
607
+ await this._evictAllSessions();
608
+ }
609
+ await this._ensureDevicePermission(connectId);
357
610
  const session = await this.connector.connect(connectId);
358
611
  this._sessions.set(connectId, session.sessionId);
359
612
  if (session.deviceInfo) {
@@ -389,7 +642,6 @@ var _LedgerAdapter = class _LedgerAdapter {
389
642
  // Chain call helper
390
643
  // ---------------------------------------------------------------------------
391
644
  async callChain(connectId, deviceId, chain, method, params, skipFingerprint = false) {
392
- await this._ensureDevicePermission(connectId, deviceId);
393
645
  try {
394
646
  const result = await this.connectorCall(connectId, method, params, {
395
647
  chain,
@@ -401,45 +653,12 @@ var _LedgerAdapter = class _LedgerAdapter {
401
653
  return this.errorToFailure(err);
402
654
  }
403
655
  }
404
- /**
405
- * Batch version of callChain — checks permission once,
406
- * fingerprint is verified on the first call inside connectorCall.
407
- */
408
- async callChainBatch(connectId, deviceId, chain, method, params, onProgress, skipFingerprint = false) {
409
- await this._ensureDevicePermission(connectId, deviceId);
410
- const results = [];
411
- for (let i = 0; i < params.length; i++) {
412
- try {
413
- const result = await this.connectorCall(connectId, method, params[i], {
414
- chain,
415
- deviceId,
416
- // Only verify fingerprint on the first call in the batch
417
- skipFingerprint: skipFingerprint || i > 0
418
- });
419
- results.push(result);
420
- onProgress?.({ index: i, total: params.length });
421
- } catch (err) {
422
- return this.errorToFailure(err);
423
- }
424
- }
425
- return success(results);
426
- }
427
656
  // ---------------------------------------------------------------------------
428
657
  // EVM chain methods
429
658
  // ---------------------------------------------------------------------------
430
659
  evmGetAddress(connectId, deviceId, params) {
431
660
  return this.callChain(connectId, deviceId, "evm", "evmGetAddress", params);
432
661
  }
433
- evmGetAddresses(connectId, deviceId, params, onProgress) {
434
- return this.callChainBatch(
435
- connectId,
436
- deviceId,
437
- "evm",
438
- "evmGetAddress",
439
- params,
440
- onProgress
441
- );
442
- }
443
662
  evmSignTransaction(connectId, deviceId, params) {
444
663
  return this.callChain(connectId, deviceId, "evm", "evmSignTransaction", params);
445
664
  }
@@ -455,16 +674,6 @@ var _LedgerAdapter = class _LedgerAdapter {
455
674
  btcGetAddress(connectId, deviceId, params) {
456
675
  return this.callChain(connectId, deviceId, "btc", "btcGetAddress", params);
457
676
  }
458
- btcGetAddresses(connectId, deviceId, params, onProgress) {
459
- return this.callChainBatch(
460
- connectId,
461
- deviceId,
462
- "btc",
463
- "btcGetAddress",
464
- params,
465
- onProgress
466
- );
467
- }
468
677
  btcGetPublicKey(connectId, deviceId, params) {
469
678
  return this.callChain(connectId, deviceId, "btc", "btcGetPublicKey", params);
470
679
  }
@@ -492,16 +701,6 @@ var _LedgerAdapter = class _LedgerAdapter {
492
701
  solGetAddress(connectId, deviceId, params) {
493
702
  return this.callChain(connectId, deviceId, "sol", "solGetAddress", params);
494
703
  }
495
- solGetAddresses(connectId, deviceId, params, onProgress) {
496
- return this.callChainBatch(
497
- connectId,
498
- deviceId,
499
- "sol",
500
- "solGetAddress",
501
- params,
502
- onProgress
503
- );
504
- }
505
704
  solSignTransaction(connectId, deviceId, params) {
506
705
  return this.callChain(connectId, deviceId, "sol", "solSignTransaction", params);
507
706
  }
@@ -512,38 +711,13 @@ var _LedgerAdapter = class _LedgerAdapter {
512
711
  // TRON chain methods
513
712
  // ---------------------------------------------------------------------------
514
713
  tronGetAddress(connectId, deviceId, params) {
515
- return this.callChain(connectId, deviceId, "tron", "tronGetAddress", params, true);
516
- }
517
- tronGetAddresses(connectId, deviceId, params, onProgress) {
518
- return this.callChainBatch(
519
- connectId,
520
- deviceId,
521
- "tron",
522
- "tronGetAddress",
523
- params,
524
- onProgress,
525
- true
526
- );
714
+ return this.callChain(connectId, deviceId, "tron", "tronGetAddress", params);
527
715
  }
528
716
  tronSignTransaction(connectId, deviceId, params) {
529
- return this.callChain(
530
- connectId,
531
- deviceId,
532
- "tron",
533
- "tronSignTransaction",
534
- params,
535
- true
536
- );
717
+ return this.callChain(connectId, deviceId, "tron", "tronSignTransaction", params);
537
718
  }
538
719
  tronSignMessage(connectId, deviceId, params) {
539
- return this.callChain(
540
- connectId,
541
- deviceId,
542
- "tron",
543
- "tronSignMessage",
544
- params,
545
- true
546
- );
720
+ return this.callChain(connectId, deviceId, "tron", "tronSignMessage", params);
547
721
  }
548
722
  on(event, listener) {
549
723
  this.emitter.on(event, listener);
@@ -552,30 +726,41 @@ var _LedgerAdapter = class _LedgerAdapter {
552
726
  this.emitter.off(event, listener);
553
727
  }
554
728
  cancel(connectId) {
555
- const sessionId = this._sessions.get(connectId) ?? connectId;
556
- this._jobQueue.forceCancelActive(connectId || "__ledger_default__");
557
- void this.connector.cancel(sessionId);
729
+ const userAbortReason = Object.assign(new Error("User aborted operation"), {
730
+ code: HardwareErrorCode2.UserAborted,
731
+ _tag: ERROR_TAG.UserAborted
732
+ });
733
+ this._lastCancelReason = userAbortReason;
734
+ setTimeout(() => {
735
+ if (this._lastCancelReason === userAbortReason) {
736
+ this._lastCancelReason = void 0;
737
+ }
738
+ }, 2e3);
739
+ this._uiRegistry.cancel();
740
+ if (connectId) {
741
+ this._jobQueue.cancelActiveAndPending(connectId, userAbortReason);
742
+ } else {
743
+ this._jobQueue.cancelActiveAndPending(void 0, userAbortReason);
744
+ }
745
+ if (connectId) {
746
+ const sessionId = this._sessions.get(connectId) ?? connectId;
747
+ void this.connector.cancel(sessionId);
748
+ } else {
749
+ for (const sid of this._sessions.values()) void this.connector.cancel(sid);
750
+ }
751
+ if (this._connectingPromise) {
752
+ this._doConnectAbortController?.abort(userAbortReason);
753
+ }
558
754
  }
559
755
  // ---------------------------------------------------------------------------
560
756
  // Chain fingerprint
561
757
  // ---------------------------------------------------------------------------
562
758
  async getChainFingerprint(connectId, deviceId, chain) {
563
- debugLog(
564
- "[LedgerAdapter] getChainFingerprint called, chain:",
565
- chain,
566
- "connectId:",
567
- connectId || "(empty)",
568
- "sessions:",
569
- this._sessions.size
570
- );
571
- await this._ensureDevicePermission(connectId, deviceId);
572
- debugLog("[LedgerAdapter] getChainFingerprint permission ok, computing fingerprint");
573
759
  try {
574
760
  const fingerprint = await this._computeChainFingerprint(
575
761
  chain,
576
- (method, params) => this.connectorCall(connectId, method, params)
762
+ (method, params) => this.connectorCall(connectId, method, params, void 0, deviceId)
577
763
  );
578
- debugLog("[LedgerAdapter] getChainFingerprint result:", fingerprint?.substring(0, 20));
579
764
  return success(fingerprint);
580
765
  } catch (err) {
581
766
  debugError("[LedgerAdapter] getChainFingerprint error:", chain, err);
@@ -646,102 +831,252 @@ var _LedgerAdapter = class _LedgerAdapter {
646
831
  if (signal?.aborted) {
647
832
  _LedgerAdapter._throwIfAborted(signal);
648
833
  }
834
+ const waitPromise = this._uiRegistry.wait(
835
+ UI_REQUEST.REQUEST_DEVICE_CONNECT
836
+ );
649
837
  this.emitter.emit(UI_REQUEST.REQUEST_DEVICE_CONNECT, {
650
838
  type: UI_REQUEST.REQUEST_DEVICE_CONNECT,
651
839
  payload: {
840
+ vendor: "ledger",
841
+ reason: "device-not-found",
652
842
  message: "Please connect and unlock your Ledger device"
653
843
  }
654
844
  });
655
- const waitPromise = this._uiRegistry.wait(
656
- UI_REQUEST.REQUEST_DEVICE_CONNECT
657
- );
658
845
  let payload;
659
- if (signal) {
660
- const onAbort = () => {
661
- this._uiRegistry.cancel(UI_REQUEST.REQUEST_DEVICE_CONNECT);
662
- };
663
- signal.addEventListener("abort", onAbort, { once: true });
664
- try {
846
+ try {
847
+ if (signal) {
848
+ const onAbort = () => {
849
+ this._uiRegistry.cancel(UI_REQUEST.REQUEST_DEVICE_CONNECT);
850
+ };
851
+ signal.addEventListener("abort", onAbort, { once: true });
852
+ try {
853
+ payload = await waitPromise;
854
+ } finally {
855
+ signal.removeEventListener("abort", onAbort);
856
+ }
857
+ } else {
665
858
  payload = await waitPromise;
666
- } finally {
667
- signal.removeEventListener("abort", onAbort);
668
859
  }
669
- } else {
670
- payload = await waitPromise;
860
+ } catch (err) {
861
+ this.emitter.emit(UI_REQUEST.CLOSE_UI_WINDOW, {
862
+ type: UI_REQUEST.CLOSE_UI_WINDOW,
863
+ payload: {}
864
+ });
865
+ throw Object.assign(new Error("User cancelled Ledger connection"), {
866
+ _tag: ERROR_TAG.UserAborted,
867
+ code: HardwareErrorCode2.UserAborted,
868
+ cause: err
869
+ });
671
870
  }
672
871
  if (!payload?.confirmed) {
673
872
  throw Object.assign(new Error("User cancelled Ledger connection"), {
674
- _tag: "DeviceNotRecognizedError"
873
+ _tag: ERROR_TAG.UserAborted,
874
+ code: HardwareErrorCode2.UserAborted
675
875
  });
676
876
  }
877
+ await new Promise((resolve) => {
878
+ setTimeout(resolve, 800);
879
+ });
677
880
  }
678
- async ensureConnected(connectId) {
679
- if (connectId && this._sessions.has(connectId)) {
680
- return connectId;
681
- }
682
- if (this._sessions.size > 0) {
683
- const firstKey = this._sessions.keys().next().value;
684
- return firstKey;
881
+ /**
882
+ * Decide whether a BTC pubkey call needs `showOnDevice=true` because of
883
+ * the BTC App's account-index policy, asking the user once per session.
884
+ *
885
+ * Returns the params to pass through (with `showOnDevice` possibly
886
+ * promoted), or `null` if the user declined.
887
+ */
888
+ async _gateBtcHighIndex(params) {
889
+ const accountIndex = btcAccountIndexFromPath(params.path);
890
+ if (params.showOnDevice) return params;
891
+ if (accountIndex === null || accountIndex < BTC_HIGH_INDEX_THRESHOLD) {
892
+ return params;
685
893
  }
686
- if (this._connectingPromise) {
687
- return this._connectingPromise;
894
+ if (this._btcHighIndexConfirmedThisSession) {
895
+ return { ...params, showOnDevice: true };
688
896
  }
689
- this._connectingPromise = this._doConnect();
897
+ const confirmed = await this._waitForBtcHighIndexConfirm(params.path, accountIndex);
898
+ if (!confirmed) return null;
899
+ this._btcHighIndexConfirmedThisSession = true;
900
+ return { ...params, showOnDevice: true };
901
+ }
902
+ async _waitForBtcHighIndexConfirm(path, accountIndex) {
903
+ const waitPromise = this._uiRegistry.wait(
904
+ UI_REQUEST.REQUEST_BTC_HIGH_INDEX_CONFIRM
905
+ );
906
+ this.emitter.emit(UI_REQUEST.REQUEST_BTC_HIGH_INDEX_CONFIRM, {
907
+ type: UI_REQUEST.REQUEST_BTC_HIGH_INDEX_CONFIRM,
908
+ payload: {
909
+ vendor: "ledger",
910
+ path,
911
+ accountIndex
912
+ }
913
+ });
690
914
  try {
691
- return await this._connectingPromise;
692
- } finally {
693
- this._connectingPromise = null;
915
+ const payload = await waitPromise;
916
+ return !!payload?.confirmed;
917
+ } catch (err) {
918
+ this.emitter.emit(UI_REQUEST.CLOSE_UI_WINDOW, {
919
+ type: UI_REQUEST.CLOSE_UI_WINDOW,
920
+ payload: {}
921
+ });
922
+ return false;
694
923
  }
695
924
  }
696
- async _doConnect() {
697
- for (let attempt = 0; attempt < _LedgerAdapter.MAX_DEVICE_RETRY; attempt++) {
698
- const devices = await this.searchDevices();
699
- if (devices.length > 0) {
700
- return this._connectFirstOrSelect(devices);
701
- }
702
- if (attempt < _LedgerAdapter.MAX_DEVICE_RETRY - 1) {
703
- await this._waitForDeviceConnect();
925
+ // Layer 1 entry. Caller signal only races the outer awaiter; the shared
926
+ // `_doConnect` runs under its own internal controller so caller A's cancel
927
+ // doesn't kill caller B's await.
928
+ async ensureConnected(connectId, signal) {
929
+ if (signal.aborted) _LedgerAdapter._throwIfAborted(signal);
930
+ if (isLedgerBleConnectionType(this.connector.connectionType) && !connectId) {
931
+ throw Object.assign(new Error("Ledger BLE connectId is required."), {
932
+ code: HardwareErrorCode2.DeviceNotFound
933
+ });
934
+ }
935
+ if (connectId && this._sessions.has(connectId)) return connectId;
936
+ if (!connectId && this._sessions.size > 0) {
937
+ if (!isLedgerBleConnectionType(this.connector.connectionType) && this._sessions.size > 1) {
938
+ throw Object.assign(
939
+ new Error(
940
+ "Ledger USB session invariant violated: more than one session is active. Please reconnect the device."
941
+ ),
942
+ { code: HardwareErrorCode2.DeviceOneDeviceOnly }
943
+ );
704
944
  }
945
+ return this._sessions.keys().next().value;
705
946
  }
706
- throw Object.assign(
707
- new Error(
708
- "No Ledger device found after multiple attempts. Please connect and unlock your device."
709
- ),
710
- { _tag: "DeviceNotRecognizedError" }
711
- );
947
+ if (!this._connectingPromise) {
948
+ this._doConnectAbortController = new AbortController();
949
+ const innerSignal = this._doConnectAbortController.signal;
950
+ this._connectingPromise = (async () => {
951
+ try {
952
+ return await this._doConnect(innerSignal, connectId);
953
+ } finally {
954
+ this._connectingPromise = null;
955
+ this._doConnectAbortController = null;
956
+ }
957
+ })();
958
+ }
959
+ return this._abortable(signal, this._connectingPromise);
712
960
  }
713
- async _connectFirstOrSelect(devices) {
714
- const chosenConnectId = devices.length === 1 ? devices[0].connectId : await this._chooseDeviceFromList(devices);
715
- const result = await this.connectDevice(chosenConnectId);
716
- if (!result.success) {
717
- throw Object.assign(new Error(result.payload.error), {
718
- _tag: "DeviceNotRecognizedError"
961
+ // Layer 1 main loop — the ONLY place in SDK that emits unlock dialog.
962
+ // Bounded by MAX_DOCONNECT_CONFIRMS after N Confirms with no progress,
963
+ // throw DeviceNotFound so the user is kicked out of the loop.
964
+ async _doConnect(internalSignal, targetConnectId) {
965
+ if (isLedgerBleConnectionType(this.connector.connectionType) && targetConnectId) {
966
+ try {
967
+ return await this._connectDeviceOrThrow(targetConnectId);
968
+ } catch (err) {
969
+ if (!isDeviceLockedError(err) && !isDeviceNotAdvertisingError(err) && !isDeviceDisconnectedError(err)) {
970
+ throw err;
971
+ }
972
+ this._discoveredDevices.delete(targetConnectId);
973
+ if (isDeviceDisconnectedError(err)) {
974
+ try {
975
+ this.connector.reset?.();
976
+ } catch {
977
+ }
978
+ }
979
+ }
980
+ }
981
+ let confirms = 0;
982
+ while (!internalSignal.aborted) {
983
+ this.emitter.emit("ui-event", {
984
+ type: EConnectorInteraction.Searching,
985
+ payload: { sessionId: "" }
719
986
  });
987
+ let devices = await this.searchDevices();
988
+ if (devices.length === 0) {
989
+ for (let i = 0; i < 3 && !internalSignal.aborted; i += 1) {
990
+ await new Promise((resolve) => {
991
+ setTimeout(resolve, 1e3);
992
+ });
993
+ devices = await this.searchDevices();
994
+ if (devices.length > 0) break;
995
+ }
996
+ }
997
+ if (devices.length > 0) {
998
+ try {
999
+ return await this._connectFirstOrSelect(devices, targetConnectId);
1000
+ } catch (err) {
1001
+ if (!isDeviceLockedError(err) && !isDeviceNotAdvertisingError(err) && !isDeviceDisconnectedError(err)) {
1002
+ throw err;
1003
+ }
1004
+ this._discoveredDevices.clear();
1005
+ if (isDeviceDisconnectedError(err)) {
1006
+ try {
1007
+ this.connector.reset?.();
1008
+ } catch {
1009
+ }
1010
+ }
1011
+ }
1012
+ }
1013
+ if (confirms >= _LedgerAdapter.MAX_DOCONNECT_CONFIRMS) {
1014
+ throw Object.assign(
1015
+ new Error(
1016
+ "Device not connected after multiple attempts. Please ensure your Ledger is awake, unlocked, and in range, then try again."
1017
+ ),
1018
+ { code: HardwareErrorCode2.DeviceNotFound }
1019
+ );
1020
+ }
1021
+ await this._waitForDeviceConnect(internalSignal);
1022
+ confirms += 1;
720
1023
  }
721
- return chosenConnectId;
1024
+ _LedgerAdapter._throwIfAborted(internalSignal);
1025
+ throw new Error("_doConnect aborted");
722
1026
  }
723
- async _chooseDeviceFromList(devices) {
724
- if (!this._handleSelectDevice) {
1027
+ async _connectFirstOrSelect(devices, targetConnectId) {
1028
+ if (!isLedgerBleConnectionType(this.connector.connectionType) && devices.length > 1) {
725
1029
  debugLog(
726
- `[DMK] Multiple Ledger devices found (${devices.length}); handleSelectDevice=false, picking first (${devices[0].connectId}).`
1030
+ `[DMK] Multiple Ledger USB devices found (${devices.length}); refusing to auto-select by ephemeral connectId.`
727
1031
  );
728
- return devices[0].connectId;
1032
+ throw createMultipleUsbLedgerDevicesError();
729
1033
  }
730
- this.emitter.emit(UI_REQUEST.REQUEST_SELECT_DEVICE, {
731
- type: UI_REQUEST.REQUEST_SELECT_DEVICE,
732
- payload: { devices }
733
- });
734
- const response = await this._uiRegistry.wait(
735
- UI_REQUEST.REQUEST_SELECT_DEVICE
736
- );
737
- const chosen = devices.find((d) => d.connectId === response?.sdkConnectId);
738
- if (!chosen) {
739
- throw Object.assign(
740
- new Error(`Selected sdkConnectId '${response?.sdkConnectId}' not in discovered list`),
741
- { _tag: "DeviceNotRecognizedError" }
1034
+ if (targetConnectId) {
1035
+ const target = devices.find(
1036
+ (d) => d.connectId === targetConnectId || d.deviceId === targetConnectId
742
1037
  );
1038
+ if (target) {
1039
+ return this._connectDeviceOrThrow(target.connectId);
1040
+ }
1041
+ if (!isLedgerBleConnectionType(this.connector.connectionType) && devices.length === 1) {
1042
+ debugLog(
1043
+ `[LedgerAdapter] target ${targetConnectId} not in fresh enumeration; accepting sole USB device ${devices[0].connectId} (assumed ephemeral path change)`
1044
+ );
1045
+ return this._connectDeviceOrThrow(devices[0].connectId);
1046
+ }
1047
+ const err = Object.assign(new Error(`Target Ledger unavailable: ${targetConnectId}`), {
1048
+ code: HardwareErrorCode2.DeviceNotFound
1049
+ });
1050
+ if (isLedgerBleConnectionType(this.connector.connectionType)) {
1051
+ err._tag = ERROR_TAG.DeviceNotAdvertising;
1052
+ }
1053
+ throw err;
743
1054
  }
744
- return chosen.connectId;
1055
+ if (isLedgerBleConnectionType(this.connector.connectionType)) {
1056
+ throw Object.assign(new Error("Ledger BLE connectId is required."), {
1057
+ code: HardwareErrorCode2.DeviceNotFound
1058
+ });
1059
+ }
1060
+ if (devices.length !== 1) {
1061
+ throw Object.assign(new Error("Ledger device not found."), {
1062
+ code: HardwareErrorCode2.DeviceNotFound
1063
+ });
1064
+ }
1065
+ return this._connectDeviceOrThrow(devices[0].connectId);
1066
+ }
1067
+ async _connectDeviceOrThrow(chosenConnectId) {
1068
+ const result = await this.connectDevice(chosenConnectId);
1069
+ if (!result.success) {
1070
+ const payload = result.payload;
1071
+ const rethrow = Object.assign(new Error(payload.error), {
1072
+ code: payload.code
1073
+ });
1074
+ if (payload._tag !== void 0) {
1075
+ rethrow._tag = payload._tag;
1076
+ }
1077
+ throw rethrow;
1078
+ }
1079
+ return chosenConnectId;
745
1080
  }
746
1081
  /**
747
1082
  * Call the connector with automatic session resolution and disconnect retry.
@@ -751,30 +1086,31 @@ var _LedgerAdapter = class _LedgerAdapter {
751
1086
  * 3. Calls connector.call()
752
1087
  * 4. On disconnect error: clears stale session, re-connects, retries once
753
1088
  */
754
- async connectorCall(connectId, method, params, fingerprint) {
1089
+ async connectorCall(connectId, method, params, fingerprint, permissionDeviceId) {
755
1090
  debugLog("[LedgerAdapter] connectorCall:", method, "connectId:", connectId || "(empty)");
756
1091
  const queueKey = connectId || "__ledger_default__";
757
- const interruptibility = _LedgerAdapter._getInterruptibility(method);
758
1092
  return this._jobQueue.enqueue(
759
1093
  queueKey,
760
- async (signal) => this._runConnectorCall(connectId, method, params, signal, fingerprint),
761
- { interruptibility, label: method }
1094
+ async (signal) => this._runConnectorCall(connectId, method, params, signal, fingerprint, permissionDeviceId),
1095
+ {
1096
+ label: method,
1097
+ rejectIfBusy: true,
1098
+ busyError: _LedgerAdapter._createDeviceBusyError(method)
1099
+ }
762
1100
  );
763
1101
  }
764
1102
  /**
765
- * Race a promise against an abort signal. If the signal fires, rejects with the
766
- * signal's reason (or a generic Error). The underlying connector.call() cannot
767
- * actually be cancelled on Ledger DMK, but the caller gets the abort immediately.
1103
+ * Race a promise against an abort signal. On abort, rejects with
1104
+ * signal.reason instance _lastCancelReason → generic Error('Aborted').
768
1105
  */
769
- static _abortable(signal, promise) {
1106
+ _abortable(signal, promise) {
1107
+ const getAbortReason = () => signal.reason ?? this._lastCancelReason ?? new Error("Aborted");
770
1108
  if (signal.aborted) {
771
- return Promise.reject(
772
- signal.reason ?? new Error("Aborted")
773
- );
1109
+ return Promise.reject(getAbortReason());
774
1110
  }
775
1111
  return new Promise((resolve, reject) => {
776
1112
  const onAbort = () => {
777
- reject(signal.reason ?? new Error("Aborted"));
1113
+ reject(getAbortReason());
778
1114
  };
779
1115
  signal.addEventListener("abort", onAbort, { once: true });
780
1116
  promise.then(
@@ -796,31 +1132,195 @@ var _LedgerAdapter = class _LedgerAdapter {
796
1132
  }
797
1133
  }
798
1134
  /** Actual work done under the job queue — connection, fingerprint, call, and recovery. */
799
- async _runConnectorCall(connectId, method, params, signal, fingerprint) {
1135
+ async _runConnectorCall(connectId, method, params, signal, fingerprint, permissionDeviceId) {
800
1136
  _LedgerAdapter._throwIfAborted(signal);
801
- const resolvedConnectId = await _LedgerAdapter._abortable(
802
- signal,
803
- this.ensureConnected(connectId)
1137
+ await this._ensureDevicePermission(
1138
+ connectId,
1139
+ permissionDeviceId ?? fingerprint?.deviceId,
1140
+ signal
804
1141
  );
805
1142
  _LedgerAdapter._throwIfAborted(signal);
1143
+ let effectiveParams = params;
1144
+ if (method === "btcGetPublicKey") {
1145
+ const gatedParams = await this._gateBtcHighIndex(params);
1146
+ if (gatedParams === null) {
1147
+ throw Object.assign(new Error("User cancelled BTC high-index confirmation"), {
1148
+ _tag: ERROR_TAG.UserAborted,
1149
+ code: HardwareErrorCode2.UserAborted
1150
+ });
1151
+ }
1152
+ effectiveParams = gatedParams;
1153
+ }
1154
+ const resolvedConnectId = await this.ensureConnected(connectId, signal);
806
1155
  const sessionId = this._sessions.get(resolvedConnectId);
807
- debugLog(
808
- "[LedgerAdapter] connectorCall resolved:",
809
- method,
810
- "resolvedConnectId:",
811
- resolvedConnectId,
812
- "sessionId:",
813
- sessionId
814
- );
815
1156
  if (!sessionId) {
816
1157
  throw Object.assign(new Error("Auto-connect succeeded but no session found"), {
817
- _tag: "DeviceSessionNotFound"
1158
+ _tag: ERROR_TAG.DeviceSessionNotFound
818
1159
  });
819
1160
  }
1161
+ try {
1162
+ if (fingerprint && !fingerprint.skipFingerprint && fingerprint.deviceId) {
1163
+ const fp = await this._abortable(
1164
+ signal,
1165
+ this._verifyDeviceFingerprintWithSession(
1166
+ sessionId,
1167
+ fingerprint.deviceId,
1168
+ fingerprint.chain
1169
+ )
1170
+ );
1171
+ if (!fp.success) {
1172
+ throw Object.assign(new Error(formatDeviceMismatchError(fp.expected, fp.actual)), {
1173
+ code: HardwareErrorCode2.DeviceMismatch
1174
+ });
1175
+ }
1176
+ }
1177
+ return await this._abortable(signal, this.connector.call(sessionId, method, effectiveParams));
1178
+ } catch (err) {
1179
+ if (signal.aborted) throw err;
1180
+ const errObj = err;
1181
+ debugLog("[LedgerAdapter] connectorCall error:", method, {
1182
+ message: errObj?.message,
1183
+ _tag: errObj?._tag,
1184
+ errorCode: errObj?.errorCode,
1185
+ statusCode: errObj?.statusCode,
1186
+ isDisconnected: isDeviceDisconnectedError(err),
1187
+ isLocked: isDeviceLockedError(err),
1188
+ isNotAdvertising: isDeviceNotAdvertisingError(err),
1189
+ isStuckApp: isStuckAppStateError(err)
1190
+ });
1191
+ if (isStuckAppStateError(err)) {
1192
+ try {
1193
+ this._sessions.delete(resolvedConnectId);
1194
+ this._discoveredDevices.delete(resolvedConnectId);
1195
+ this.connector.reset?.();
1196
+ } catch {
1197
+ }
1198
+ debugLog(
1199
+ "[LedgerAdapter] stuck-app retry: method=",
1200
+ method,
1201
+ "delayMs=",
1202
+ _LedgerAdapter.STUCK_APP_RETRY_DELAY_MS,
1203
+ "_tag=",
1204
+ err?._tag
1205
+ );
1206
+ try {
1207
+ const retryResult = await this._retryAfterStuckApp(
1208
+ resolvedConnectId,
1209
+ method,
1210
+ effectiveParams,
1211
+ signal,
1212
+ err,
1213
+ fingerprint
1214
+ );
1215
+ debugLog("[LedgerAdapter] stuck-app retry succeeded: method=", method);
1216
+ return retryResult;
1217
+ } catch (retryErr) {
1218
+ if (signal.aborted) throw retryErr;
1219
+ if (isStuckAppStateError(retryErr)) {
1220
+ debugLog("[LedgerAdapter] stuck-app retry exhausted (2nd 6901): method=", method);
1221
+ throw err;
1222
+ }
1223
+ debugLog(
1224
+ "[LedgerAdapter] stuck-app retry threw non-stuck error: method=",
1225
+ method,
1226
+ "retryErrTag=",
1227
+ retryErr?._tag
1228
+ );
1229
+ throw retryErr;
1230
+ }
1231
+ }
1232
+ if (isDeviceLockedError(err) || isDeviceNotAdvertisingError(err) || isDeviceDisconnectedError(err)) {
1233
+ let lastErr = err;
1234
+ for (let attempt = 0; attempt < _LedgerAdapter.MAX_BUSINESS_RETRY_BUDGET; attempt += 1) {
1235
+ if (signal.aborted) throw lastErr;
1236
+ try {
1237
+ this._sessions.delete(resolvedConnectId);
1238
+ this._discoveredDevices.delete(resolvedConnectId);
1239
+ if (isDeviceDisconnectedError(lastErr)) {
1240
+ try {
1241
+ this.connector.reset?.();
1242
+ } catch {
1243
+ }
1244
+ }
1245
+ const reConnectId = await this.ensureConnected(resolvedConnectId, signal);
1246
+ const reSessionId = this._sessions.get(reConnectId);
1247
+ if (!reSessionId) throw lastErr;
1248
+ if (fingerprint && !fingerprint.skipFingerprint && fingerprint.deviceId) {
1249
+ const fp = await this._abortable(
1250
+ signal,
1251
+ this._verifyDeviceFingerprintWithSession(
1252
+ reSessionId,
1253
+ fingerprint.deviceId,
1254
+ fingerprint.chain
1255
+ )
1256
+ );
1257
+ if (!fp.success) {
1258
+ throw Object.assign(new Error(formatDeviceMismatchError(fp.expected, fp.actual)), {
1259
+ code: HardwareErrorCode2.DeviceMismatch
1260
+ });
1261
+ }
1262
+ }
1263
+ return await this._abortable(
1264
+ signal,
1265
+ this.connector.call(reSessionId, method, effectiveParams)
1266
+ );
1267
+ } catch (retryErr) {
1268
+ if (signal.aborted) throw retryErr;
1269
+ lastErr = retryErr;
1270
+ const canRetry = attempt < _LedgerAdapter.MAX_BUSINESS_RETRY_BUDGET - 1 && (isDeviceLockedError(retryErr) || isDeviceNotAdvertisingError(retryErr) || isDeviceDisconnectedError(retryErr));
1271
+ if (!canRetry) {
1272
+ throw retryErr;
1273
+ }
1274
+ }
1275
+ }
1276
+ throw lastErr;
1277
+ }
1278
+ if (isTimeoutError(err)) {
1279
+ debugLog("[LedgerAdapter] timeout, retrying with fresh connection...");
1280
+ this._discoveredDevices.delete(resolvedConnectId);
1281
+ return this._retryWithFreshConnection(
1282
+ resolvedConnectId,
1283
+ method,
1284
+ effectiveParams,
1285
+ signal,
1286
+ err,
1287
+ fingerprint
1288
+ );
1289
+ }
1290
+ if (isConnectionLevelError(err)) {
1291
+ debugLog("[LedgerAdapter] connection-level fail-closed reset");
1292
+ this._sessions.delete(resolvedConnectId);
1293
+ this._discoveredDevices.delete(resolvedConnectId);
1294
+ this.connector.reset();
1295
+ const codeNum = err?.code;
1296
+ throw Object.assign(err, {
1297
+ code: codeNum ?? HardwareErrorCode2.DeviceDisconnected
1298
+ });
1299
+ }
1300
+ throw err;
1301
+ }
1302
+ }
1303
+ /**
1304
+ * Stuck-app recovery: pause for the device's UI transition, then retry once.
1305
+ *
1306
+ * Caller has already cleared the session + reset connector. We wait so Stax
1307
+ * finishes its post-CloseApp animation, then go through ensureConnected +
1308
+ * fingerprint check + call exactly once. Caller decides what to do on a
1309
+ * second stuck-app hit.
1310
+ */
1311
+ async _retryAfterStuckApp(resolvedConnectId, method, params, signal, originalErr, fingerprint) {
1312
+ await this._sleepAbortable(_LedgerAdapter.STUCK_APP_RETRY_DELAY_MS, signal);
1313
+ const retryConnectId = await this.ensureConnected(resolvedConnectId, signal);
1314
+ const retrySessionId = this._sessions.get(retryConnectId);
1315
+ if (!retrySessionId) throw originalErr;
820
1316
  if (fingerprint && !fingerprint.skipFingerprint && fingerprint.deviceId) {
821
- const fp = await _LedgerAdapter._abortable(
1317
+ const fp = await this._abortable(
822
1318
  signal,
823
- this._verifyDeviceFingerprintWithSession(sessionId, fingerprint.deviceId, fingerprint.chain)
1319
+ this._verifyDeviceFingerprintWithSession(
1320
+ retrySessionId,
1321
+ fingerprint.deviceId,
1322
+ fingerprint.chain
1323
+ )
824
1324
  );
825
1325
  if (!fp.success) {
826
1326
  throw Object.assign(new Error(formatDeviceMismatchError(fp.expected, fp.actual)), {
@@ -828,48 +1328,97 @@ var _LedgerAdapter = class _LedgerAdapter {
828
1328
  });
829
1329
  }
830
1330
  }
831
- try {
832
- return await _LedgerAdapter._abortable(signal, this.connector.call(sessionId, method, params));
833
- } catch (err) {
834
- if (signal.aborted) throw err;
835
- const errObj = err;
836
- debugLog("[LedgerAdapter] connectorCall error:", method, {
837
- message: errObj?.message,
838
- _tag: errObj?._tag,
839
- errorCode: errObj?.errorCode,
840
- statusCode: errObj?.statusCode,
841
- isDisconnected: isDeviceDisconnectedError(err),
842
- isLocked: isDeviceLockedError(err)
843
- });
844
- if (isDeviceDisconnectedError(err)) {
845
- debugLog("[LedgerAdapter] disconnected, retrying with fresh connection...");
846
- this._discoveredDevices.clear();
847
- return this._retryWithFreshConnection(resolvedConnectId, method, params, signal, err);
848
- }
849
- if (isDeviceLockedError(err)) {
850
- await this._waitForDeviceConnect(signal);
851
- _LedgerAdapter._throwIfAborted(signal);
852
- return _LedgerAdapter._abortable(signal, this.connector.call(sessionId, method, params));
853
- }
854
- if (isTimeoutError(err)) {
855
- debugLog("[LedgerAdapter] timeout, retrying with fresh connection...");
856
- this._discoveredDevices.delete(resolvedConnectId);
857
- return this._retryWithFreshConnection(resolvedConnectId, method, params, signal, err);
1331
+ return this._abortable(signal, this.connector.call(retrySessionId, method, params));
1332
+ }
1333
+ _sleepAbortable(ms, signal) {
1334
+ return new Promise((resolve, reject) => {
1335
+ if (signal.aborted) {
1336
+ reject(signal.reason ?? new Error("Aborted"));
1337
+ return;
858
1338
  }
859
- throw err;
860
- }
1339
+ const timer = setTimeout(() => {
1340
+ signal.removeEventListener("abort", onAbort);
1341
+ resolve();
1342
+ }, ms);
1343
+ const onAbort = () => {
1344
+ clearTimeout(timer);
1345
+ reject(signal.reason ?? new Error("Aborted"));
1346
+ };
1347
+ signal.addEventListener("abort", onAbort, { once: true });
1348
+ });
861
1349
  }
862
- /** Clear stale session, reconnect, and retry the call. */
863
- async _retryWithFreshConnection(resolvedConnectId, method, params, signal, originalErr) {
864
- this._sessions.delete(resolvedConnectId);
865
- _LedgerAdapter._throwIfAborted(signal);
866
- const retryConnectId = await this.ensureConnected();
867
- _LedgerAdapter._throwIfAborted(signal);
1350
+ /**
1351
+ * Clear stale session, reconnect, and retry the call.
1352
+ *
1353
+ * Timeout recovery starts with a full connector reset. After an APDU
1354
+ * timeout, DMK/transport state may still emit malformed responses; retrying
1355
+ * on the same DMK can poison the next chain switch.
1356
+ */
1357
+ async _retryWithFreshConnection(targetConnectId, method, params, signal, originalErr, fingerprint) {
1358
+ this.connector.reset();
1359
+ this._sessions.clear();
1360
+ this._discoveredDevices.clear();
1361
+ this._connectingPromise = null;
1362
+ const retryConnectId = await this.ensureConnected(targetConnectId, signal);
868
1363
  const retrySessionId = this._sessions.get(retryConnectId);
869
1364
  if (!retrySessionId) {
870
1365
  throw originalErr;
871
1366
  }
872
- return _LedgerAdapter._abortable(signal, this.connector.call(retrySessionId, method, params));
1367
+ try {
1368
+ if (fingerprint && !fingerprint.skipFingerprint && fingerprint.deviceId) {
1369
+ const fp = await this._abortable(
1370
+ signal,
1371
+ this._verifyDeviceFingerprintWithSession(
1372
+ retrySessionId,
1373
+ fingerprint.deviceId,
1374
+ fingerprint.chain
1375
+ )
1376
+ );
1377
+ if (!fp.success) {
1378
+ throw Object.assign(new Error(formatDeviceMismatchError(fp.expected, fp.actual)), {
1379
+ code: HardwareErrorCode2.DeviceMismatch
1380
+ });
1381
+ }
1382
+ }
1383
+ return await this._abortable(signal, this.connector.call(retrySessionId, method, params));
1384
+ } catch (retryErr) {
1385
+ if (signal.aborted) throw retryErr;
1386
+ this.connector.reset();
1387
+ this._sessions.clear();
1388
+ this._discoveredDevices.clear();
1389
+ this._connectingPromise = null;
1390
+ if (!isDeviceDisconnectedError(retryErr) && !isTimeoutError(retryErr)) {
1391
+ throw retryErr;
1392
+ }
1393
+ debugLog(
1394
+ "[LedgerAdapter] fresh-session retry still failed; resetting connector and rebuilding DMK"
1395
+ );
1396
+ this.connector.reset();
1397
+ this._sessions.clear();
1398
+ this._discoveredDevices.clear();
1399
+ this._connectingPromise = null;
1400
+ const finalConnectId = await this.ensureConnected(targetConnectId, signal);
1401
+ const finalSessionId = this._sessions.get(finalConnectId);
1402
+ if (!finalSessionId) {
1403
+ throw originalErr;
1404
+ }
1405
+ if (fingerprint && !fingerprint.skipFingerprint && fingerprint.deviceId) {
1406
+ const fp = await this._abortable(
1407
+ signal,
1408
+ this._verifyDeviceFingerprintWithSession(
1409
+ finalSessionId,
1410
+ fingerprint.deviceId,
1411
+ fingerprint.chain
1412
+ )
1413
+ );
1414
+ if (!fp.success) {
1415
+ throw Object.assign(new Error(formatDeviceMismatchError(fp.expected, fp.actual)), {
1416
+ code: HardwareErrorCode2.DeviceMismatch
1417
+ });
1418
+ }
1419
+ }
1420
+ return this._abortable(signal, this.connector.call(finalSessionId, method, params));
1421
+ }
873
1422
  }
874
1423
  /**
875
1424
  * Ensure OS-level device permission (Bluetooth / USB) before proceeding.
@@ -883,7 +1432,10 @@ var _LedgerAdapter = class _LedgerAdapter {
883
1432
  * - No connectId (searchDevices): environment-level permission
884
1433
  * - With connectId (business methods): device-level permission
885
1434
  */
886
- async _ensureDevicePermission(connectId, deviceId) {
1435
+ async _ensureDevicePermission(connectId, deviceId, signal) {
1436
+ if (signal?.aborted) {
1437
+ _LedgerAdapter._throwIfAborted(signal);
1438
+ }
887
1439
  const transportType = this.activeTransport ?? "hid";
888
1440
  const waitPromise = this._uiRegistry.wait(
889
1441
  UI_REQUEST.REQUEST_DEVICE_PERMISSION,
@@ -893,10 +1445,37 @@ var _LedgerAdapter = class _LedgerAdapter {
893
1445
  type: UI_REQUEST.REQUEST_DEVICE_PERMISSION,
894
1446
  payload: { transportType, connectId, deviceId }
895
1447
  });
896
- const { granted } = await waitPromise;
1448
+ let response;
1449
+ const onAbort = () => {
1450
+ this._uiRegistry.cancel(UI_REQUEST.REQUEST_DEVICE_PERMISSION);
1451
+ };
1452
+ try {
1453
+ signal?.addEventListener("abort", onAbort, { once: true });
1454
+ response = await waitPromise;
1455
+ } catch (err) {
1456
+ if (signal?.aborted) {
1457
+ _LedgerAdapter._throwIfAborted(signal);
1458
+ }
1459
+ if (err?._tag === UI_REQUEST_PREEMPTED_TAG) {
1460
+ this.emitter.emit(UI_REQUEST.CLOSE_UI_WINDOW, {
1461
+ type: UI_REQUEST.CLOSE_UI_WINDOW,
1462
+ payload: {}
1463
+ });
1464
+ throw Object.assign(new Error("Device permission request superseded"), {
1465
+ _tag: ERROR_TAG.UserAborted,
1466
+ code: HardwareErrorCode2.UserAborted,
1467
+ cause: err
1468
+ });
1469
+ }
1470
+ throw err;
1471
+ } finally {
1472
+ signal?.removeEventListener("abort", onAbort);
1473
+ }
1474
+ const { granted, reason, message } = response;
897
1475
  if (!granted) {
898
- throw Object.assign(new Error("Device permission denied"), {
899
- code: HardwareErrorCode2.DevicePermissionDenied
1476
+ throw Object.assign(new Error(message ?? "Device permission denied"), {
1477
+ code: HardwareErrorCode2.DevicePermissionDenied,
1478
+ reason
900
1479
  });
901
1480
  }
902
1481
  }
@@ -906,12 +1485,14 @@ var _LedgerAdapter = class _LedgerAdapter {
906
1485
  */
907
1486
  errorToFailure(err) {
908
1487
  debugError("[LedgerAdapter] error:", err);
1488
+ const tag = err && typeof err === "object" ? err._tag : void 0;
909
1489
  if (err && typeof err === "object" && "code" in err && typeof err.code === "number") {
910
1490
  const e = err;
911
- return ledgerFailure(e.code, e.message ?? "Unknown error", e.appName);
1491
+ const params = e.code === HardwareErrorCode2.DevicePermissionDenied && e.reason ? { permissionDeniedReason: e.reason } : void 0;
1492
+ return ledgerFailure(e.code, e.message ?? "Unknown error", e.appName, tag, params);
912
1493
  }
913
1494
  const mapped = mapLedgerError(err);
914
- return ledgerFailure(mapped.code, mapped.message, mapped.appName);
1495
+ return ledgerFailure(mapped.code, mapped.message, mapped.appName, tag);
915
1496
  }
916
1497
  registerEventListeners() {
917
1498
  this.connector.on("device-connect", this.deviceConnectHandler);
@@ -927,32 +1508,38 @@ var _LedgerAdapter = class _LedgerAdapter {
927
1508
  // Device info mapping
928
1509
  // ---------------------------------------------------------------------------
929
1510
  connectorDeviceToDeviceInfo(device) {
930
- const isBle = device.connectId && /^[0-9A-Fa-f]{4}$/.test(device.connectId);
931
1511
  return {
932
1512
  vendor: "ledger",
933
1513
  model: device.model ?? "unknown",
1514
+ modelName: device.modelName,
934
1515
  firmwareVersion: "",
935
1516
  deviceId: device.deviceId,
936
1517
  connectId: device.connectId,
937
1518
  label: device.name,
938
- connectionType: isBle ? "ble" : "usb",
1519
+ connectionType: this.connector.connectionType,
1520
+ rssi: device.rssi,
1521
+ isConnectable: device.isConnectable,
1522
+ serialNumber: device.serialNumber,
939
1523
  capabilities: device.capabilities
940
1524
  };
941
1525
  }
942
1526
  };
943
- // ---------------------------------------------------------------------------
944
- // Private helpers
945
- // ---------------------------------------------------------------------------
946
- /**
947
- * Ensure at least one device is connected and return a valid connectId.
948
- *
949
- * - If a session already exists for the given connectId, reuse it.
950
- * - If ANY session exists (Ledger IDs are ephemeral), reuse it.
951
- * - Otherwise: search 1 device: auto-connect, multiple: ask user, 0: throw.
952
- */
953
- _LedgerAdapter.MAX_DEVICE_RETRY = 3;
1527
+ // Layer 2 retry budget after connection-class error. Each round delegates
1528
+ // to Layer 1 (which owns the unlock prompt). Layer 2 never emits UI itself.
1529
+ _LedgerAdapter.MAX_BUSINESS_RETRY_BUDGET = 3;
1530
+ // Stuck-app (APDU 0x6901) retry pause. Ledger Stax often returns 6901 when
1531
+ // OpenAppCommand lands mid-transition right after CloseApp; a short wait
1532
+ // lets the device finish its UI animation before we retry once.
1533
+ _LedgerAdapter.STUCK_APP_RETRY_DELAY_MS = 500;
1534
+ // Layer 1 confirm budget. After N failed Confirm cycles (user keeps clicking
1535
+ // Confirm but device never shows up / never unlocks), throw DeviceNotFound
1536
+ // instead of looping forever.
1537
+ _LedgerAdapter.MAX_DOCONNECT_CONFIRMS = 3;
954
1538
  var LedgerAdapter = _LedgerAdapter;
955
1539
 
1540
+ // src/connector/LedgerConnectorBase.ts
1541
+ import { HardwareErrorCode as HardwareErrorCode7 } from "@onekeyfe/hwk-adapter-core";
1542
+
956
1543
  // src/device/LedgerDeviceManager.ts
957
1544
  var LedgerDeviceManager = class {
958
1545
  constructor(dmk) {
@@ -989,7 +1576,9 @@ var LedgerDeviceManager = class {
989
1576
  if (resolved) return;
990
1577
  resolved = true;
991
1578
  this._discovered.clear();
992
- debugLog("[DMK] enumerate raw devices:", devices.length);
1579
+ debugLog(
1580
+ `[DMK] enumerate count=${devices.length} ids=[${devices.map((d) => d.id).join(",")}]`
1581
+ );
993
1582
  for (const d of devices) {
994
1583
  this._discovered.set(d.id, d);
995
1584
  }
@@ -999,8 +1588,10 @@ var LedgerDeviceManager = class {
999
1588
  devices.map((d) => ({
1000
1589
  path: d.id,
1001
1590
  type: d.deviceModel.model,
1591
+ modelName: d.deviceModel.name,
1002
1592
  name: d.name,
1003
- transport: d.transport
1593
+ transport: d.transport,
1594
+ rssi: d.rssi
1004
1595
  }))
1005
1596
  );
1006
1597
  } else {
@@ -1021,8 +1612,10 @@ var LedgerDeviceManager = class {
1021
1612
  devices.map((d) => ({
1022
1613
  path: d.id,
1023
1614
  type: d.deviceModel.model,
1615
+ modelName: d.deviceModel.name,
1024
1616
  name: d.name,
1025
- transport: d.transport
1617
+ transport: d.transport,
1618
+ rssi: d.rssi
1026
1619
  }))
1027
1620
  );
1028
1621
  }
@@ -1046,8 +1639,10 @@ var LedgerDeviceManager = class {
1046
1639
  descriptor: {
1047
1640
  path: d.id,
1048
1641
  type: d.deviceModel.model,
1642
+ modelName: d.deviceModel.name,
1049
1643
  name: d.name,
1050
- transport: d.transport
1644
+ transport: d.transport,
1645
+ rssi: d.rssi
1051
1646
  }
1052
1647
  });
1053
1648
  }
@@ -1066,6 +1661,54 @@ var LedgerDeviceManager = class {
1066
1661
  this._listenSub?.unsubscribe();
1067
1662
  this._listenSub = null;
1068
1663
  }
1664
+ /**
1665
+ * Resolve to the latest snapshot of advertising devices observed within
1666
+ * `timeoutMs`. Rewrites `_discovered` so it tracks the live list.
1667
+ *
1668
+ * `listenToAvailableDevices` is a BehaviorSubject — it emits the cached
1669
+ * list on subscribe and *only* re-emits when the list changes. A stable
1670
+ * list of currently-advertising devices therefore produces only the
1671
+ * subscription frame; treating that frame as "fossil to be discarded"
1672
+ * caused devices to flicker in/out of the UI as searches alternated
1673
+ * between "saw a change frame" and "saw only the cached frame".
1674
+ *
1675
+ * The window still gives a chance for the active scan to update the list
1676
+ * (e.g. drop a peripheral that just stopped broadcasting), but if nothing
1677
+ * changes we trust the snapshot — DMK silence on a stable list means
1678
+ * "everything's still there".
1679
+ */
1680
+ getLiveDevices(timeoutMs = 1500) {
1681
+ debugLog(`[DMK] getLiveDevices() called, timeoutMs=${timeoutMs}`);
1682
+ void this.requestDevice();
1683
+ return new Promise((resolve) => {
1684
+ let done = false;
1685
+ let lastSeen = [];
1686
+ let sub = null;
1687
+ const finish = (devices) => {
1688
+ if (done) return;
1689
+ done = true;
1690
+ sub?.unsubscribe();
1691
+ clearTimeout(timer);
1692
+ this._discovered.clear();
1693
+ for (const d of devices) this._discovered.set(d.id, d);
1694
+ debugLog(
1695
+ `[DMK] getLiveDevices() resolved count=${devices.length} ids=[${devices.map((d) => d.id).join(",")}]`
1696
+ );
1697
+ resolve(devices);
1698
+ };
1699
+ const timer = setTimeout(() => finish(lastSeen), timeoutMs);
1700
+ sub = this._dmk.listenToAvailableDevices({}).subscribe({
1701
+ next: (devices) => {
1702
+ lastSeen = devices;
1703
+ },
1704
+ error: (err) => {
1705
+ debugError("[DMK] getLiveDevices() error:", err);
1706
+ finish(lastSeen);
1707
+ },
1708
+ complete: () => finish(lastSeen)
1709
+ });
1710
+ });
1711
+ }
1069
1712
  requestDevice() {
1070
1713
  if (this._discoverySub) {
1071
1714
  return Promise.resolve();
@@ -1083,11 +1726,33 @@ var LedgerDeviceManager = class {
1083
1726
  });
1084
1727
  return Promise.resolve();
1085
1728
  }
1729
+ /** Lookup a previously-discovered device's display name. */
1730
+ getDeviceName(deviceId) {
1731
+ return this._discovered.get(deviceId)?.name;
1732
+ }
1733
+ /** Lookup minimal model/signal info from a previously-discovered device. */
1734
+ getDiscoveredDeviceInfo(deviceId) {
1735
+ const d = this._discovered.get(deviceId);
1736
+ if (!d) return void 0;
1737
+ return {
1738
+ model: d.deviceModel?.model,
1739
+ modelName: d.deviceModel?.name,
1740
+ name: d.name,
1741
+ rssi: d.rssi
1742
+ };
1743
+ }
1744
+ hasDiscoveredDevice(deviceId) {
1745
+ return this._discovered.has(deviceId);
1746
+ }
1086
1747
  /** Connect to a previously discovered device. Returns sessionId. */
1087
1748
  async connect(deviceId) {
1088
1749
  const device = this._discovered.get(deviceId);
1089
1750
  if (!device) {
1090
- throw new Error(`Device "${deviceId}" not found. Call enumerate() or listen() first.`);
1751
+ const err = new Error(
1752
+ `Device "${deviceId}" not found in discovery cache. Call enumerate() or listen() first.`
1753
+ );
1754
+ err._tag = ERROR_TAG.DeviceNotInDiscoveryCache;
1755
+ throw err;
1091
1756
  }
1092
1757
  const sessionId = await this._dmk.connect({ device });
1093
1758
  this._sessions.set(deviceId, sessionId);
@@ -1126,6 +1791,19 @@ var LedgerDeviceManager = class {
1126
1791
  this._sessionToDevice.clear();
1127
1792
  this._dmk.close();
1128
1793
  }
1794
+ /**
1795
+ * Tear down RxJS subscriptions and local state without closing the DMK.
1796
+ * For light-reset paths that want to drop session state but keep the
1797
+ * underlying transport alive for retry. Call this instead of orphaning
1798
+ * the manager — bare null assignment leaks _listenSub / _discoverySub.
1799
+ */
1800
+ disposeKeepingDmk() {
1801
+ this.stopListening();
1802
+ this.stopDiscovery();
1803
+ this._discovered.clear();
1804
+ this._sessions.clear();
1805
+ this._sessionToDevice.clear();
1806
+ }
1129
1807
  };
1130
1808
 
1131
1809
  // src/signer/SignerManager.ts
@@ -1137,11 +1815,12 @@ import { hexToBytes } from "@onekeyfe/hwk-adapter-core";
1137
1815
 
1138
1816
  // src/signer/deviceActionToPromise.ts
1139
1817
  import { DeviceActionStatus } from "@ledgerhq/device-management-kit";
1140
- var DEFAULT_TIMEOUT_MS = 3e4;
1141
- function deviceActionToPromise(action, onInteraction, timeoutMs = DEFAULT_TIMEOUT_MS, onRegisterCanceller) {
1818
+ var IDLE_WATCHDOG_MS = 65e3;
1819
+ function deviceActionToPromise(action, onInteraction, timeoutMs = IDLE_WATCHDOG_MS, onRegisterCanceller) {
1142
1820
  return new Promise((resolve, reject) => {
1143
1821
  let settled = false;
1144
1822
  let lastStep;
1823
+ const observedSteps = [];
1145
1824
  let sub;
1146
1825
  let timer = null;
1147
1826
  const cancelAction = () => {
@@ -1154,44 +1833,59 @@ function deviceActionToPromise(action, onInteraction, timeoutMs = DEFAULT_TIMEOU
1154
1833
  } catch {
1155
1834
  }
1156
1835
  };
1157
- const resetTimer = () => {
1836
+ const armIdleWatchdog = () => {
1158
1837
  if (timer) clearTimeout(timer);
1159
1838
  if (timeoutMs > 0) {
1160
1839
  timer = setTimeout(() => {
1161
1840
  if (!settled) {
1162
1841
  settled = true;
1163
1842
  cancelAction();
1164
- reject(new Error("Device action timed out \u2014 device may be locked or disconnected"));
1843
+ reject(
1844
+ new Error(
1845
+ "Ledger transport unresponsive: no DMK state change within the watchdog window. The device may have lost connection."
1846
+ )
1847
+ );
1165
1848
  }
1166
1849
  }, timeoutMs);
1167
1850
  }
1168
1851
  };
1169
- resetTimer();
1852
+ armIdleWatchdog();
1170
1853
  if (onRegisterCanceller) {
1171
- onRegisterCanceller(() => {
1172
- if (settled) return;
1854
+ onRegisterCanceller((reason) => {
1855
+ if (settled) {
1856
+ return;
1857
+ }
1173
1858
  settled = true;
1174
1859
  if (timer) clearTimeout(timer);
1175
1860
  cancelAction();
1176
- reject(new Error("Device action cancelled"));
1861
+ const err = new Error(reason?.message ?? "Device action cancelled");
1862
+ if (reason?.code !== void 0) {
1863
+ err.code = reason.code;
1864
+ }
1865
+ if (reason?.tag) {
1866
+ Object.defineProperty(err, "_tag", {
1867
+ value: reason.tag,
1868
+ configurable: true,
1869
+ enumerable: false,
1870
+ writable: true
1871
+ });
1872
+ }
1873
+ reject(err);
1177
1874
  });
1178
1875
  }
1179
- debugLog("[DMK-Observable] subscribing to action.observable...");
1180
1876
  sub = action.observable.subscribe({
1181
1877
  next: (state) => {
1182
- if (settled) return;
1183
- resetTimer();
1878
+ if (settled) {
1879
+ return;
1880
+ }
1881
+ armIdleWatchdog();
1184
1882
  const step = state?.intermediateValue?.step;
1185
- if (step) lastStep = step;
1186
- debugLog(
1187
- "[DMK-Observable] state:",
1188
- JSON.stringify({
1189
- status: state.status,
1190
- intermediateValue: state.status === DeviceActionStatus.Pending ? state.intermediateValue : void 0,
1191
- hasOutput: state.status === DeviceActionStatus.Completed,
1192
- hasError: state.status === DeviceActionStatus.Error
1193
- })
1194
- );
1883
+ if (step) {
1884
+ lastStep = step;
1885
+ if (observedSteps[observedSteps.length - 1] !== step) {
1886
+ observedSteps.push(step);
1887
+ }
1888
+ }
1195
1889
  if (state.status === DeviceActionStatus.Completed) {
1196
1890
  settled = true;
1197
1891
  if (timer) clearTimeout(timer);
@@ -1203,10 +1897,14 @@ function deviceActionToPromise(action, onInteraction, timeoutMs = DEFAULT_TIMEOU
1203
1897
  if (timer) clearTimeout(timer);
1204
1898
  onInteraction?.("interaction-complete");
1205
1899
  sub?.unsubscribe();
1206
- rejectWithLastStep(state.error, lastStep, reject);
1900
+ rejectWithStepContext(state.error, lastStep, observedSteps, reject);
1207
1901
  } else if (state.status === DeviceActionStatus.Pending && onInteraction) {
1208
1902
  const interaction = state.intermediateValue?.requiredUserInteraction;
1209
1903
  if (interaction && interaction !== "none") {
1904
+ if (interaction === "unlock-device" && timer) {
1905
+ clearTimeout(timer);
1906
+ timer = null;
1907
+ }
1210
1908
  onInteraction(String(interaction));
1211
1909
  }
1212
1910
  }
@@ -1216,7 +1914,7 @@ function deviceActionToPromise(action, onInteraction, timeoutMs = DEFAULT_TIMEOU
1216
1914
  settled = true;
1217
1915
  if (timer) clearTimeout(timer);
1218
1916
  sub?.unsubscribe();
1219
- rejectWithLastStep(err, lastStep, reject);
1917
+ rejectWithStepContext(err, lastStep, observedSteps, reject);
1220
1918
  }
1221
1919
  },
1222
1920
  complete: () => {
@@ -1229,15 +1927,25 @@ function deviceActionToPromise(action, onInteraction, timeoutMs = DEFAULT_TIMEOU
1229
1927
  });
1230
1928
  });
1231
1929
  }
1232
- function rejectWithLastStep(err, lastStep, reject) {
1233
- if (err && typeof err === "object" && lastStep) {
1930
+ function rejectWithStepContext(err, lastStep, observedSteps, reject) {
1931
+ if (err && typeof err === "object" && (lastStep || observedSteps.length > 0)) {
1234
1932
  try {
1235
- Object.defineProperty(err, "_lastStep", {
1236
- value: lastStep,
1237
- configurable: true,
1238
- enumerable: false,
1239
- writable: true
1240
- });
1933
+ if (lastStep) {
1934
+ Object.defineProperty(err, "_lastStep", {
1935
+ value: lastStep,
1936
+ configurable: true,
1937
+ enumerable: false,
1938
+ writable: true
1939
+ });
1940
+ }
1941
+ if (observedSteps.length > 0) {
1942
+ Object.defineProperty(err, "_deviceActionSteps", {
1943
+ value: [...observedSteps],
1944
+ configurable: true,
1945
+ enumerable: false,
1946
+ writable: true
1947
+ });
1948
+ }
1241
1949
  } catch {
1242
1950
  }
1243
1951
  }
@@ -1303,7 +2011,8 @@ var SignerManager = class _SignerManager {
1303
2011
  async getOrCreate(sessionId) {
1304
2012
  debugLog("[DMK] SignerManager.getOrCreate:", { sessionId });
1305
2013
  const builder = await this._builderFn({ dmk: this._dmk, sessionId });
1306
- return new SignerEth(builder.build());
2014
+ const builderWithContext = builder.withContextModule ? builder.withContextModule(_SignerManager._createContextModule()) : builder;
2015
+ return new SignerEth(builderWithContext.build());
1307
2016
  }
1308
2017
  // eslint-disable-next-line @typescript-eslint/no-unused-vars, class-methods-use-this
1309
2018
  invalidate(_sessionId) {
@@ -1312,10 +2021,24 @@ var SignerManager = class _SignerManager {
1312
2021
  clearAll() {
1313
2022
  }
1314
2023
  static _defaultBuilder() {
1315
- return (args) => {
1316
- const contextModule = new ContextModuleBuilder({}).removeDefaultLoaders().build();
1317
- return new SignerEthBuilder(args).withContextModule(contextModule);
2024
+ return (args) => new SignerEthBuilder(args);
2025
+ }
2026
+ static _createContextModule() {
2027
+ const contextModule = new ContextModuleBuilder({}).removeDefaultLoaders().build();
2028
+ return _SignerManager.wrapBlindSigningReportNonBlocking(contextModule);
2029
+ }
2030
+ static wrapBlindSigningReportNonBlocking(contextModule) {
2031
+ const report = contextModule.report.bind(contextModule);
2032
+ contextModule.report = async (params) => {
2033
+ try {
2034
+ void report(params).catch((err) => {
2035
+ debugLog("[DMK] ContextModule.report failed:", err);
2036
+ });
2037
+ } catch (err) {
2038
+ debugLog("[DMK] ContextModule.report failed:", err);
2039
+ }
1318
2040
  };
2041
+ return contextModule;
1319
2042
  }
1320
2043
  };
1321
2044
 
@@ -1323,20 +2046,20 @@ var SignerManager = class _SignerManager {
1323
2046
  import { HardwareErrorCode as HardwareErrorCode3, padHex64, stripHex } from "@onekeyfe/hwk-adapter-core";
1324
2047
 
1325
2048
  // src/connector/chains/utils.ts
1326
- import { EConnectorInteraction } from "@onekeyfe/hwk-adapter-core";
2049
+ import { EConnectorInteraction as EConnectorInteraction2 } from "@onekeyfe/hwk-adapter-core";
1327
2050
  function normalizePath(path) {
1328
2051
  return path.startsWith("m/") ? path.slice(2) : path;
1329
2052
  }
1330
2053
  function collapseSignerInteraction(interaction) {
1331
2054
  switch (interaction) {
1332
2055
  case "confirm-open-app":
1333
- return EConnectorInteraction.ConfirmOpenApp;
2056
+ return EConnectorInteraction2.ConfirmOpenApp;
1334
2057
  case "unlock-device":
1335
- return EConnectorInteraction.UnlockDevice;
2058
+ return EConnectorInteraction2.UnlockDevice;
1336
2059
  case "interaction-complete":
1337
- return EConnectorInteraction.InteractionComplete;
2060
+ return EConnectorInteraction2.InteractionComplete;
1338
2061
  default:
1339
- return EConnectorInteraction.ConfirmOnDevice;
2062
+ return EConnectorInteraction2.ConfirmOnDevice;
1340
2063
  }
1341
2064
  }
1342
2065
 
@@ -1425,6 +2148,7 @@ async function _getEthSigner(ctx, sessionId) {
1425
2148
  const signerManager = await ctx.getSignerManager();
1426
2149
  const signer = await signerManager.getOrCreate(sessionId);
1427
2150
  signer.onInteraction = (interaction) => {
2151
+ debugLog("[LedgerConnector] evm.onInteraction:", interaction);
1428
2152
  ctx.emit("ui-event", {
1429
2153
  type: collapseSignerInteraction(interaction),
1430
2154
  payload: { sessionId }
@@ -1700,6 +2424,7 @@ async function _createBtcSigner(ctx, sessionId) {
1700
2424
  const sdkSigner = new SignerBtcBuilder({ dmk, sessionId }).build();
1701
2425
  const signer = new SignerBtc(sdkSigner);
1702
2426
  signer.onInteraction = (interaction) => {
2427
+ debugLog("[LedgerConnector] btc.onInteraction:", interaction);
1703
2428
  ctx.emit("ui-event", {
1704
2429
  type: collapseSignerInteraction(interaction),
1705
2430
  payload: { sessionId }
@@ -1870,6 +2595,7 @@ async function _createSolSigner(ctx, sessionId) {
1870
2595
  const sdkSigner = new SignerSolanaBuilder({ dmk, sessionId }).withContextModule(contextModule).build();
1871
2596
  const signer = new SignerSol(sdkSigner);
1872
2597
  signer.onInteraction = (interaction) => {
2598
+ debugLog("[LedgerConnector] sol.onInteraction:", interaction);
1873
2599
  ctx.emit("ui-event", {
1874
2600
  type: collapseSignerInteraction(interaction),
1875
2601
  payload: { sessionId }
@@ -1882,11 +2608,11 @@ async function _createSolSigner(ctx, sessionId) {
1882
2608
  }
1883
2609
 
1884
2610
  // src/connector/chains/tron.ts
1885
- import { HardwareErrorCode as HardwareErrorCode5 } from "@onekeyfe/hwk-adapter-core";
2611
+ import { HardwareErrorCode as HardwareErrorCode6 } from "@onekeyfe/hwk-adapter-core";
1886
2612
  import Trx from "@ledgerhq/hw-app-trx";
1887
2613
 
1888
2614
  // src/connector/chains/legacyChainCall.ts
1889
- import { EConnectorInteraction as EConnectorInteraction2 } from "@onekeyfe/hwk-adapter-core";
2615
+ import { EConnectorInteraction as EConnectorInteraction3 } from "@onekeyfe/hwk-adapter-core";
1890
2616
 
1891
2617
  // src/app/AppManager.ts
1892
2618
  import {
@@ -1895,6 +2621,7 @@ import {
1895
2621
  OpenAppCommand,
1896
2622
  isSuccessCommandResult
1897
2623
  } from "@ledgerhq/device-management-kit";
2624
+ import { HardwareErrorCode as HardwareErrorCode5 } from "@onekeyfe/hwk-adapter-core";
1898
2625
  var APP_NAME_MAP = {
1899
2626
  ETH: "Ethereum",
1900
2627
  BTC: "Bitcoin",
@@ -1966,7 +2693,31 @@ var AppManager = class {
1966
2693
  debugLog("[AppManager] currentApp:", result.data.name);
1967
2694
  return result.data.name;
1968
2695
  }
1969
- throw new Error("Failed to get current app from device");
2696
+ const errResult = result;
2697
+ const dmkErr = errResult.error ?? {};
2698
+ const original = dmkErr.originalError;
2699
+ debugLog(
2700
+ "[AppManager] _getCurrentApp failed sessionId=",
2701
+ sessionId,
2702
+ "tag=",
2703
+ dmkErr._tag,
2704
+ "errorCode=",
2705
+ dmkErr.errorCode,
2706
+ "message=",
2707
+ dmkErr.message,
2708
+ "originalErrorMessage=",
2709
+ original?.message ?? String(original ?? "")
2710
+ );
2711
+ throw Object.assign(
2712
+ new Error(
2713
+ dmkErr.message ?? "Failed to get current app from device"
2714
+ ),
2715
+ {
2716
+ _tag: dmkErr._tag,
2717
+ errorCode: dmkErr.errorCode,
2718
+ originalError: original
2719
+ }
2720
+ );
1970
2721
  }
1971
2722
  async _openApp(sessionId, appName) {
1972
2723
  const result = await this._dmk.sendCommand({
@@ -1975,9 +2726,11 @@ var AppManager = class {
1975
2726
  });
1976
2727
  if (!isSuccessCommandResult(result)) {
1977
2728
  const { statusCode } = result;
2729
+ const hasStatusCode2 = statusCode != null && statusCode !== "";
1978
2730
  debugLog("[AppManager] openApp failed:", appName, "statusCode:", statusCode);
1979
2731
  throw Object.assign(new Error(`Failed to open "${appName}"`), {
1980
- _tag: "OpenAppCommandError",
2732
+ _tag: ERROR_TAG.OpenAppCommand,
2733
+ code: hasStatusCode2 ? void 0 : HardwareErrorCode5.AppNotInstalled,
1981
2734
  errorCode: String(statusCode ?? ""),
1982
2735
  statusCode,
1983
2736
  appName
@@ -2033,14 +2786,14 @@ async function withLegacyChainCall(ctx, sessionId, options, action) {
2033
2786
  const onAppOpenPrompt = () => {
2034
2787
  openAppPromptShown = true;
2035
2788
  ctx.emit("ui-event", {
2036
- type: EConnectorInteraction2.ConfirmOpenApp,
2789
+ type: EConnectorInteraction3.ConfirmOpenApp,
2037
2790
  payload: { sessionId }
2038
2791
  });
2039
2792
  };
2040
2793
  const closeOpenAppUiIfShown = () => {
2041
2794
  if (openAppPromptShown) {
2042
2795
  ctx.emit("ui-event", {
2043
- type: EConnectorInteraction2.InteractionComplete,
2796
+ type: EConnectorInteraction3.InteractionComplete,
2044
2797
  payload: { sessionId }
2045
2798
  });
2046
2799
  openAppPromptShown = false;
@@ -2061,7 +2814,7 @@ async function withLegacyChainCall(ctx, sessionId, options, action) {
2061
2814
  let confirmEmitted = false;
2062
2815
  if (needsConfirmation) {
2063
2816
  ctx.emit("ui-event", {
2064
- type: EConnectorInteraction2.ConfirmOnDevice,
2817
+ type: EConnectorInteraction3.ConfirmOnDevice,
2065
2818
  payload: { sessionId }
2066
2819
  });
2067
2820
  confirmEmitted = true;
@@ -2071,7 +2824,7 @@ async function withLegacyChainCall(ctx, sessionId, options, action) {
2071
2824
  } finally {
2072
2825
  if (confirmEmitted || openAppPromptShown) {
2073
2826
  ctx.emit("ui-event", {
2074
- type: EConnectorInteraction2.InteractionComplete,
2827
+ type: EConnectorInteraction3.InteractionComplete,
2075
2828
  payload: { sessionId }
2076
2829
  });
2077
2830
  openAppPromptShown = false;
@@ -2159,7 +2912,7 @@ async function tronSignTransaction(ctx, sessionId, params) {
2159
2912
  if (!params.rawTxHex) {
2160
2913
  throw Object.assign(
2161
2914
  new Error("TRON signing requires a protobuf-encoded raw transaction hex (rawTxHex)."),
2162
- { code: HardwareErrorCode5.InvalidParams }
2915
+ { code: HardwareErrorCode6.InvalidParams }
2163
2916
  );
2164
2917
  }
2165
2918
  const path = normalizePath(params.path);
@@ -2203,6 +2956,10 @@ var METHOD_PREFIX_TO_APP_NAME = {
2203
2956
  sol: "Solana",
2204
2957
  tron: "Tron"
2205
2958
  };
2959
+ var HARDWARE_ERROR_CODE_VALUES = new Set(
2960
+ Object.values(HardwareErrorCode7).filter((value) => typeof value === "number")
2961
+ );
2962
+ var BLE_CONNECT_SCAN_TIMEOUT_MS = 1500;
2206
2963
  async function defaultLedgerKitImporter(pkg) {
2207
2964
  switch (pkg) {
2208
2965
  case "@ledgerhq/device-management-kit":
@@ -2226,16 +2983,6 @@ var LedgerConnectorBase = class {
2226
2983
  this._dmk = null;
2227
2984
  this._eventHandlers = /* @__PURE__ */ new Map();
2228
2985
  // ---------------------------------------------------------------------------
2229
- // ConnectId <-> DMK path mapping
2230
- //
2231
- // DMK uses internal paths (BLE MAC, USB UUID) that may change across sessions.
2232
- // _resolveConnectId() maps these to stable external IDs (BLE: "A58F", USB: same).
2233
- // This bidirectional map is the SINGLE SOURCE OF TRUTH for all connectId usage.
2234
- // ---------------------------------------------------------------------------
2235
- this._connectIdToPath = /* @__PURE__ */ new Map();
2236
- // "A58F" -> "D5:75:7D:4B:51:E8"
2237
- this._pathToConnectId = /* @__PURE__ */ new Map();
2238
- // ---------------------------------------------------------------------------
2239
2986
  // Per-session DeviceAction cancellers
2240
2987
  //
2241
2988
  // Each chain handler registers its active DeviceAction's canceller via
@@ -2244,10 +2991,23 @@ var LedgerConnectorBase = class {
2244
2991
  // unsubscribes the observable and releases DMK's IntentQueue slot.
2245
2992
  // ---------------------------------------------------------------------------
2246
2993
  this._cancellers = /* @__PURE__ */ new Map();
2994
+ // ---------------------------------------------------------------------------
2995
+ // Per-session DMK state subscriptions
2996
+ //
2997
+ // DMK only emits `device-disconnect` on the connector when `disconnect()`
2998
+ // is called explicitly. To pick up autonomous disconnects (USB unplug,
2999
+ // BLE drop, device sleep, DMK transport reset) we subscribe to
3000
+ // `_dmk.getDeviceSessionState({sessionId})` per active session and emit
3001
+ // `device-disconnect` ourselves the moment DMK reports the device went
3002
+ // to NOT_CONNECTED. The subscription is torn down on disconnect /
3003
+ // reset / observable completion to avoid leaks.
3004
+ // ---------------------------------------------------------------------------
3005
+ this._sessionStateSubs = /* @__PURE__ */ new Map();
2247
3006
  this._createTransport = createTransport;
2248
3007
  this.connectionType = options?.connectionType ?? "usb";
2249
3008
  this._providedDmk = options?.dmk;
2250
3009
  this._importLedgerKit = options?.importLedgerKit ?? defaultLedgerKitImporter;
3010
+ this._requirePreFlightScan = options?.requirePreFlightScan ?? false;
2251
3011
  if (this._providedDmk) {
2252
3012
  this._initManagers(this._providedDmk);
2253
3013
  }
@@ -2265,38 +3025,23 @@ var LedgerConnectorBase = class {
2265
3025
  importLedgerKit: this._importLedgerKit
2266
3026
  };
2267
3027
  }
2268
- // "D5:75:7D:4B:51:E8" -> "A58F"
2269
- /** Register a connectId <-> path mapping from a device descriptor. */
2270
- _registerDeviceId(descriptor) {
2271
- const connectId = this._resolveConnectId(descriptor);
2272
- this._connectIdToPath.set(connectId, descriptor.path);
2273
- this._pathToConnectId.set(descriptor.path, connectId);
2274
- return connectId;
2275
- }
2276
- /** Get DMK path from external connectId. Falls back to connectId itself. */
2277
- _getPathForConnectId(connectId) {
2278
- return this._connectIdToPath.get(connectId) ?? connectId;
2279
- }
2280
- /** Get external connectId from DMK path. Falls back to path itself. */
2281
- _getConnectIdForPath(path) {
2282
- return this._pathToConnectId.get(path) ?? path;
2283
- }
2284
3028
  // ---------------------------------------------------------------------------
2285
3029
  // Protected — hooks for subclasses
2286
3030
  // ---------------------------------------------------------------------------
2287
3031
  /**
2288
3032
  * Resolve the connectId for a discovered device descriptor.
2289
3033
  * Default: use the DMK path (ephemeral UUID).
2290
- * Override in subclasses to extract stable identifiers (e.g. BLE hex ID).
3034
+ * Override in subclasses only when the public connectId differs from the transport path.
2291
3035
  */
2292
3036
  _resolveConnectId(descriptor) {
2293
3037
  return descriptor.path;
2294
3038
  }
2295
- // ---------------------------------------------------------------------------
2296
- // IConnector -- Device discovery
2297
- // ---------------------------------------------------------------------------
2298
- async searchDevices() {
2299
- const dm = await this._getDeviceManager();
3039
+ /**
3040
+ * Authoritative discovery for this transport. Default = enumerate snapshot
3041
+ * (USB/HID). BLE overrides to drive from a live scan window since DMK's
3042
+ * enumerate() cache survives peripherals going offline.
3043
+ */
3044
+ async _discoverDescriptors(dm) {
2300
3045
  let descriptors = await dm.enumerate();
2301
3046
  if (descriptors.length === 0) {
2302
3047
  try {
@@ -2305,92 +3050,284 @@ var LedgerConnectorBase = class {
2305
3050
  }
2306
3051
  descriptors = await dm.enumerate();
2307
3052
  }
2308
- const result = descriptors.map((d) => {
2309
- const connectId = this._registerDeviceId(d);
2310
- return {
2311
- connectId,
2312
- deviceId: d.path,
2313
- name: d.name || d.type || "Ledger",
2314
- model: d.type
2315
- };
2316
- });
3053
+ return descriptors;
3054
+ }
3055
+ _assertSingleUsbDescriptor(descriptors) {
3056
+ if (isLedgerBleConnectionType(this.connectionType) || descriptors.length <= 1) {
3057
+ return;
3058
+ }
3059
+ debugLog(
3060
+ `[DMK] Multiple Ledger USB devices found (${descriptors.length}); refusing to choose by ephemeral path. paths=[${descriptors.map((d) => d.path).join(",")}]`
3061
+ );
3062
+ throw createMultipleUsbLedgerDevicesError();
3063
+ }
3064
+ // ---------------------------------------------------------------------------
3065
+ // IConnector -- Device discovery
3066
+ // ---------------------------------------------------------------------------
3067
+ async searchDevices() {
3068
+ const dm = await this._getDeviceManager();
3069
+ const descriptors = await this._discoverDescriptors(dm);
3070
+ this._assertSingleUsbDescriptor(descriptors);
3071
+ const resolvedDescriptors = descriptors.map((d) => ({
3072
+ descriptor: d,
3073
+ connectId: this._resolveConnectId(d)
3074
+ }));
3075
+ const result = resolvedDescriptors.map(({ descriptor: d, connectId }) => ({
3076
+ connectId,
3077
+ deviceId: d.path,
3078
+ name: d.name || d.type || "Ledger",
3079
+ model: d.type,
3080
+ modelName: d.modelName,
3081
+ rssi: d.rssi,
3082
+ isConnectable: d.isConnectable,
3083
+ serialNumber: d.serialNumber
3084
+ }));
3085
+ debugLog(
3086
+ `[DMK] connector.searchDevices() return count=${result.length} ids=[${result.map((r) => r.connectId).join(",")}] paths=[${result.map((r) => r.deviceId).join(",")}]`
3087
+ );
2317
3088
  return result;
2318
3089
  }
2319
3090
  // ---------------------------------------------------------------------------
2320
3091
  // IConnector -- Connection
2321
3092
  // ---------------------------------------------------------------------------
2322
3093
  async connect(deviceId) {
2323
- const dm = await this._getDeviceManager();
2324
- let discovered = await this.searchDevices();
2325
- const dmkPath = deviceId ? this._getPathForConnectId(deviceId) : void 0;
2326
- let targetPath = dmkPath;
3094
+ const callerSuppliedConnectId = Boolean(deviceId);
3095
+ let targetPath = deviceId;
3096
+ if (callerSuppliedConnectId && !isLedgerBleConnectionType(this.connectionType)) {
3097
+ const dm = await this._getDeviceManager();
3098
+ this._assertSingleUsbDescriptor(await this._discoverDescriptors(dm));
3099
+ }
2327
3100
  if (!targetPath) {
2328
- const descriptors = await dm.enumerate();
2329
- if (descriptors.length === 0) {
3101
+ const discovered = await this.searchDevices();
3102
+ if (discovered.length === 0) {
2330
3103
  throw new Error(
2331
- `No Ledger device found. Make sure the device is connected${this.connectionType === "ble" ? " nearby with Bluetooth enabled" : " via USB"} and unlocked.`
3104
+ `No Ledger device found. Make sure the device is connected${isLedgerBleConnectionType(this.connectionType) ? " nearby with Bluetooth enabled" : " via USB"} and unlocked.`
2332
3105
  );
2333
3106
  }
2334
- targetPath = descriptors[0].path;
2335
- }
2336
- const externalConnectId = this._getConnectIdForPath(targetPath);
3107
+ targetPath = discovered[0].deviceId;
3108
+ }
3109
+ const externalConnectId = targetPath;
3110
+ const HANG_CEILING_MS = 5 * 6e4;
3111
+ const dmConnectWithObserve = async (dm, path) => {
3112
+ if (!isLedgerBleConnectionType(this.connectionType)) return dm.connect(path);
3113
+ let timeoutId;
3114
+ let timedOut = false;
3115
+ const connectPromise = dm.connect(path);
3116
+ const hangPromise = new Promise((_, reject) => {
3117
+ timeoutId = setTimeout(() => {
3118
+ timedOut = true;
3119
+ const err = new Error(
3120
+ `Ledger BLE connect did not return within ${HANG_CEILING_MS / 6e4}min \u2014 DMK hang fallback.`
3121
+ );
3122
+ err._tag = ERROR_TAG.BlePairingTimeout;
3123
+ err.code = HardwareErrorCode7.BlePairingTimeout;
3124
+ reject(err);
3125
+ }, HANG_CEILING_MS);
3126
+ });
3127
+ try {
3128
+ return await Promise.race([connectPromise, hangPromise]);
3129
+ } catch (err) {
3130
+ const e = err;
3131
+ debugLog("[DMK] dm.connect rejected \u2014 observed:", {
3132
+ _tag: e?._tag,
3133
+ message: e?.message,
3134
+ errorCode: e?.errorCode,
3135
+ statusCode: e?.statusCode,
3136
+ originalTag: e?.originalError?._tag,
3137
+ originalMessage: e?.originalError?.message
3138
+ });
3139
+ if (timedOut) {
3140
+ void connectPromise.then(
3141
+ (sessionId) => dm.disconnect(sessionId).catch(() => void 0),
3142
+ () => void 0
3143
+ );
3144
+ }
3145
+ throw err;
3146
+ } finally {
3147
+ if (timeoutId) clearTimeout(timeoutId);
3148
+ }
3149
+ };
3150
+ const isBleDirectConnect = isLedgerBleConnectionType(this.connectionType) && callerSuppliedConnectId;
3151
+ const throwNotAdvertising = () => {
3152
+ const err = new Error(
3153
+ "Ledger device is not currently advertising. Wake up and unlock the device, keep it nearby, then try again."
3154
+ );
3155
+ err._tag = ERROR_TAG.DeviceNotAdvertising;
3156
+ err.code = HardwareErrorCode7.DeviceNotFound;
3157
+ throw err;
3158
+ };
2337
3159
  const doConnect = async (path) => {
2338
- const sessionId = await dm.connect(path);
3160
+ const dm = await this._getDeviceManager();
3161
+ if (isBleDirectConnect && !dm.hasDiscoveredDevice(path)) {
3162
+ const live = await dm.getLiveDevices(BLE_CONNECT_SCAN_TIMEOUT_MS);
3163
+ if (this._requirePreFlightScan && !live.some((d) => d.id === path)) {
3164
+ throwNotAdvertising();
3165
+ }
3166
+ }
3167
+ let sessionId;
3168
+ try {
3169
+ sessionId = await dmConnectWithObserve(dm, path);
3170
+ } catch (err) {
3171
+ if (isBleDirectConnect && err?._tag === ERROR_TAG.DeviceNotInDiscoveryCache) {
3172
+ throwNotAdvertising();
3173
+ }
3174
+ throw err;
3175
+ }
3176
+ try {
3177
+ this._watchSessionState(sessionId, externalConnectId);
3178
+ } catch (subErr) {
3179
+ debugLog("[DMK] state subscription failed during connect; disconnecting session:", subErr);
3180
+ try {
3181
+ await dm.disconnect(sessionId);
3182
+ } catch {
3183
+ }
3184
+ throw subErr;
3185
+ }
3186
+ const info = dm.getDiscoveredDeviceInfo(path);
2339
3187
  const session = {
2340
3188
  sessionId,
2341
3189
  deviceInfo: {
2342
3190
  vendor: "ledger",
2343
- model: "unknown",
3191
+ model: info?.model ?? "unknown",
3192
+ modelName: info?.modelName,
2344
3193
  firmwareVersion: "unknown",
2345
3194
  deviceId: path,
2346
3195
  connectId: externalConnectId,
2347
3196
  connectionType: this.connectionType,
3197
+ rssi: info?.rssi,
2348
3198
  capabilities: { persistentDeviceIdentity: false }
2349
3199
  }
2350
3200
  };
2351
- const realName = discovered.find((d) => d.connectId === externalConnectId)?.name ?? "Ledger";
3201
+ const name = dm.getDeviceName(path) ?? "Ledger";
2352
3202
  this._emit("device-connect", {
2353
- device: {
2354
- connectId: externalConnectId,
2355
- deviceId: path,
2356
- name: realName
2357
- }
3203
+ device: { connectId: externalConnectId, deviceId: path, name }
2358
3204
  });
2359
3205
  return session;
2360
3206
  };
2361
3207
  try {
2362
3208
  return await doConnect(targetPath);
2363
- } catch {
3209
+ } catch (err) {
2364
3210
  this._resetSignersAndSessions();
2365
- const dm2 = await this._getDeviceManager();
2366
- discovered = await this.searchDevices();
2367
- const retryPath = this._getPathForConnectId(externalConnectId);
2368
- if (!retryPath || retryPath === externalConnectId) {
2369
- const descriptors = await dm2.enumerate();
2370
- if (descriptors.length === 0) {
2371
- throw new Error(
2372
- `No Ledger device found after retry. Make sure the device is connected${this.connectionType === "ble" ? " nearby with Bluetooth enabled" : " via USB"} and unlocked.`
2373
- );
3211
+ if (isLedgerBleConnectionType(this.connectionType)) {
3212
+ const tag = err?._tag;
3213
+ if (isKnownConnectionTag(tag)) {
3214
+ throw err;
2374
3215
  }
2375
- return doConnect(descriptors[0].path);
3216
+ const wrapped = new Error(
3217
+ "Ledger Bluetooth pairing failed. Make sure the device is unlocked and nearby, then try again."
3218
+ );
3219
+ wrapped._tag = ERROR_TAG.BleGattBondingFailed;
3220
+ wrapped.code = HardwareErrorCode7.BlePairingTimeout;
3221
+ wrapped.originalError = err;
3222
+ throw wrapped;
3223
+ }
3224
+ if (err && typeof err === "object" && err._tag) {
3225
+ const taggedError = err instanceof Error ? err : Object.assign(
3226
+ new Error(err.message ?? "Ledger device error"),
3227
+ err
3228
+ );
3229
+ throw taggedError;
2376
3230
  }
2377
- return doConnect(retryPath);
3231
+ throw new Error("Ledger device appears unplugged. Plug in the device and try again.");
2378
3232
  }
2379
3233
  }
2380
3234
  async disconnect(sessionId) {
2381
3235
  if (!this._deviceManager) return;
2382
3236
  const deviceId = this._deviceManager.getDeviceId(sessionId);
2383
3237
  this._signerManager?.invalidate(sessionId);
3238
+ this._unwatchSessionState(sessionId);
2384
3239
  await this._deviceManager.disconnect(sessionId);
2385
3240
  if (deviceId) {
2386
3241
  this._emit("device-disconnect", { connectId: deviceId });
2387
3242
  }
2388
3243
  }
3244
+ /**
3245
+ * Subscribe to DMK's per-session state observable so that any autonomous
3246
+ * disconnect (USB unplug, BLE drop, device sleep, transport reset) is
3247
+ * surfaced as a `device-disconnect` event — without this, the upstream
3248
+ * `LedgerAdapter._sessions` map would hold a dead session entry until
3249
+ * the next call hit `DeviceSessionNotFound`.
3250
+ *
3251
+ * Subscribe failure is fatal — see the inline note at the subscribe call.
3252
+ */
3253
+ _watchSessionState(sessionId, externalConnectId) {
3254
+ const dmk = this._dmk;
3255
+ if (!dmk) return;
3256
+ const previous = this._sessionStateSubs.get(sessionId);
3257
+ if (previous) {
3258
+ try {
3259
+ previous.unsubscribe();
3260
+ } catch {
3261
+ }
3262
+ }
3263
+ const sub = dmk.getDeviceSessionState({ sessionId }).subscribe({
3264
+ next: (state) => {
3265
+ if (state?.deviceStatus === "NOT CONNECTED") {
3266
+ this._handleAutonomousDisconnect(sessionId, externalConnectId);
3267
+ }
3268
+ },
3269
+ error: () => {
3270
+ this._handleAutonomousDisconnect(sessionId, externalConnectId);
3271
+ },
3272
+ complete: () => {
3273
+ this._handleAutonomousDisconnect(sessionId, externalConnectId);
3274
+ }
3275
+ });
3276
+ this._sessionStateSubs.set(sessionId, sub);
3277
+ }
3278
+ _unwatchSessionState(sessionId) {
3279
+ const sub = this._sessionStateSubs.get(sessionId);
3280
+ if (!sub) return;
3281
+ try {
3282
+ sub.unsubscribe();
3283
+ } catch {
3284
+ }
3285
+ this._sessionStateSubs.delete(sessionId);
3286
+ }
3287
+ _handleAutonomousDisconnect(sessionId, externalConnectId) {
3288
+ if (!this._sessionStateSubs.has(sessionId)) return;
3289
+ debugLog(
3290
+ "[DMK] autonomous disconnect detected \u2014 sessionId:",
3291
+ sessionId,
3292
+ "connectId:",
3293
+ externalConnectId
3294
+ );
3295
+ this._unwatchSessionState(sessionId);
3296
+ this._signerManager?.invalidate(sessionId);
3297
+ this._cancellers.get(sessionId)?.({
3298
+ code: HardwareErrorCode7.DeviceDisconnected,
3299
+ tag: "DeviceDisconnected",
3300
+ message: "Device disconnected"
3301
+ });
3302
+ this._cancellers.delete(sessionId);
3303
+ this._emit("device-disconnect", { connectId: externalConnectId });
3304
+ }
2389
3305
  // ---------------------------------------------------------------------------
2390
3306
  // IConnector -- Method dispatch
2391
3307
  // ---------------------------------------------------------------------------
2392
3308
  async call(sessionId, method, params) {
2393
3309
  debugLog("[DMK] call:", method, JSON.stringify(params));
3310
+ try {
3311
+ return await this._dispatch(sessionId, method, params);
3312
+ } catch (err) {
3313
+ if (isAppStuckByApdu(err)) {
3314
+ throw Object.assign(new Error("Ledger app is unresponsive"), {
3315
+ code: HardwareErrorCode7.DeviceAppStuck,
3316
+ _tag: ERROR_TAG.DeviceAppStuck,
3317
+ originalError: err
3318
+ });
3319
+ }
3320
+ if (isTransportStuck(err)) {
3321
+ throw Object.assign(new Error("Device communication interrupted, please retry"), {
3322
+ code: HardwareErrorCode7.TransportError,
3323
+ _tag: ERROR_TAG.DeviceTransportStuck,
3324
+ originalError: err
3325
+ });
3326
+ }
3327
+ throw err;
3328
+ }
3329
+ }
3330
+ async _dispatch(sessionId, method, params) {
2394
3331
  const ctx = this._ctxForMethod(method);
2395
3332
  switch (method) {
2396
3333
  // EVM
@@ -2530,14 +3467,13 @@ var LedgerConnectorBase = class {
2530
3467
  }
2531
3468
  /**
2532
3469
  * Replace an old session with a new one after app switch.
2533
- * Updates the connectId→path mapping so subsequent calls() use the new session,
2534
- * and emits device-connect so the adapter updates its _sessions Map.
3470
+ * Emits device-connect so the adapter updates its _sessions Map.
2535
3471
  */
2536
3472
  _replaceSession(oldSessionId, _newSessionId) {
2537
3473
  const dm = this._deviceManager;
2538
3474
  if (!dm) return;
2539
3475
  const oldDeviceId = dm.getDeviceId(oldSessionId);
2540
- const connectId = oldDeviceId ? this._pathToConnectId.get(oldDeviceId) : void 0;
3476
+ const connectId = oldDeviceId;
2541
3477
  this._signerManager?.invalidate(oldSessionId);
2542
3478
  if (connectId) {
2543
3479
  this._emit("device-connect", {
@@ -2550,13 +3486,18 @@ var LedgerConnectorBase = class {
2550
3486
  }
2551
3487
  }
2552
3488
  /**
2553
- * Light reset: clear signer/session state but keep DMK, BLE scan, and ID mapping alive.
3489
+ * Light reset: clear signer/session state but keep DMK alive.
2554
3490
  * Used by connect() retry — we want to re-discover with the same transport.
3491
+ *
3492
+ * Note: drops the device manager but tears down its RxJS subs first via
3493
+ * disposeKeepingDmk(). Bare null-assignment would leak _listenSub /
3494
+ * _discoverySub and double-scan after the next _initManagers().
2555
3495
  */
2556
3496
  _resetSignersAndSessions() {
2557
3497
  debugLog("[DMK] _resetSignersAndSessions called");
2558
3498
  this._signerManager?.clearAll();
2559
3499
  this._signerManager = null;
3500
+ this._deviceManager?.disposeKeepingDmk();
2560
3501
  this._deviceManager = null;
2561
3502
  }
2562
3503
  _resetAll() {
@@ -2568,13 +3509,18 @@ var LedgerConnectorBase = class {
2568
3509
  }
2569
3510
  }
2570
3511
  this._cancellers.clear();
3512
+ for (const sub of this._sessionStateSubs.values()) {
3513
+ try {
3514
+ sub.unsubscribe();
3515
+ } catch {
3516
+ }
3517
+ }
3518
+ this._sessionStateSubs.clear();
2571
3519
  this._signerManager?.clearAll();
2572
3520
  this._deviceManager?.dispose();
2573
3521
  this._deviceManager = null;
2574
3522
  this._signerManager = null;
2575
3523
  this._dmk = null;
2576
- this._connectIdToPath.clear();
2577
- this._pathToConnectId.clear();
2578
3524
  }
2579
3525
  // ---------------------------------------------------------------------------
2580
3526
  // Private -- Events
@@ -2608,15 +3554,21 @@ var LedgerConnectorBase = class {
2608
3554
  };
2609
3555
  }
2610
3556
  _wrapError(err, opts) {
2611
- const mapped = mapLedgerError(err, opts);
2612
- const error = new Error(mapped.message);
2613
3557
  const src = err && typeof err === "object" ? err : {};
3558
+ const hasSerializedCode = typeof src.code === "number" && HARDWARE_ERROR_CODE_VALUES.has(src.code);
3559
+ const mapped = hasSerializedCode ? {
3560
+ code: src.code,
3561
+ message: typeof src.message === "string" ? src.message : "Unknown Ledger error",
3562
+ appName: typeof src.appName === "string" ? src.appName : opts?.defaultAppName
3563
+ } : mapLedgerError(err, opts);
3564
+ const error = new Error(mapped.message);
2614
3565
  Object.assign(error, {
2615
3566
  code: mapped.code,
2616
3567
  appName: mapped.appName,
2617
3568
  _tag: src._tag,
2618
3569
  errorCode: src.errorCode,
2619
- _lastStep: src._lastStep
3570
+ _lastStep: src._lastStep,
3571
+ _deviceActionSteps: src._deviceActionSteps
2620
3572
  });
2621
3573
  Object.defineProperty(error, "originalError", {
2622
3574
  value: err,
@@ -2627,13 +3579,6 @@ var LedgerConnectorBase = class {
2627
3579
  return error;
2628
3580
  }
2629
3581
  };
2630
-
2631
- // src/utils/bleIdentity.ts
2632
- function extractBleHexId(name) {
2633
- if (!name) return void 0;
2634
- const match = name.match(/\b([0-9A-Fa-f]{4})$/);
2635
- return match ? match[1].toUpperCase() : void 0;
2636
- }
2637
3582
  export {
2638
3583
  AppManager,
2639
3584
  DmkTransport,
@@ -2644,12 +3589,15 @@ export {
2644
3589
  SignerEth,
2645
3590
  SignerManager,
2646
3591
  SignerSol,
3592
+ debugLog,
2647
3593
  deviceActionToPromise,
2648
- extractBleHexId,
2649
- isDebugEnabled,
2650
3594
  isDeviceLockedError,
3595
+ isLedgerBleConnectionType,
3596
+ isLedgerBleDescriptor,
3597
+ isLedgerDmkBleTransport,
2651
3598
  ledgerFailure,
2652
3599
  mapLedgerError,
2653
- setDebugEnabled
3600
+ offSdkEvent,
3601
+ onSdkEvent
2654
3602
  };
2655
3603
  //# sourceMappingURL=index.mjs.map