@onekeyfe/hwk-ledger-adapter 1.2.8-alpha.109 → 1.2.8-alpha.110

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.d.mts CHANGED
@@ -228,11 +228,6 @@ declare class LedgerAdapter implements IHardwareWallet {
228
228
  cancel(connectId?: string): void;
229
229
  /** A non-empty deviceId is an expected chain fingerprint, not a transport identifier. */
230
230
  getChainFingerprint(connectId: string, deviceId: string, chain: ChainForFingerprint): Promise<Response<string>>;
231
- /**
232
- * Fingerprints the first recorded chain whose app the device can open. Only a missing app
233
- * moves on to the next chain; when every app is missing, the first chain offers its install.
234
- */
235
- private _fingerprintFirstOpenableChain;
236
231
  /** Saves a picked BLE endpoint; `identity` is set when its wallet was just checked. */
237
232
  private _saveSelectedBleBinding;
238
233
  private _dropSession;
package/dist/index.d.ts CHANGED
@@ -228,11 +228,6 @@ declare class LedgerAdapter implements IHardwareWallet {
228
228
  cancel(connectId?: string): void;
229
229
  /** A non-empty deviceId is an expected chain fingerprint, not a transport identifier. */
230
230
  getChainFingerprint(connectId: string, deviceId: string, chain: ChainForFingerprint): Promise<Response<string>>;
231
- /**
232
- * Fingerprints the first recorded chain whose app the device can open. Only a missing app
233
- * moves on to the next chain; when every app is missing, the first chain offers its install.
234
- */
235
- private _fingerprintFirstOpenableChain;
236
231
  /** Saves a picked BLE endpoint; `identity` is set when its wallet was just checked. */
237
232
  private _saveSelectedBleBinding;
238
233
  private _dropSession;
package/dist/index.js CHANGED
@@ -219,7 +219,6 @@ var ERROR_TAG = {
219
219
  // outcome when the user doesn't confirm pairing on the device, or the
220
220
  // existing bond is invalid. Observed in production after ~30s.
221
221
  PairingRefused: "PairingRefusedError",
222
- PeerRemovedPairing: "PeerRemovedPairingError",
223
222
  // DMK remote-network failures (manager-api HTTP / secure-channel WS).
224
223
  WebSocketConnection: "WebSocketConnectionError",
225
224
  HttpFetch: "FetchError",
@@ -248,27 +247,6 @@ function isDeviceNotAdvertisingError(err) {
248
247
  if (!err || typeof err !== "object") return false;
249
248
  return err._tag === ERROR_TAG.DeviceNotAdvertising;
250
249
  }
251
- function isBleBondInvalidError(err) {
252
- const pending = [err];
253
- const seen = /* @__PURE__ */ new Set();
254
- while (pending.length > 0) {
255
- const candidate = pending.pop();
256
- if (candidate && typeof candidate === "object" && !seen.has(candidate)) {
257
- seen.add(candidate);
258
- const e = candidate;
259
- if (e._tag === ERROR_TAG.PeerRemovedPairing || e.iosErrorCode === 14 || e.attErrorCode === 5 || e.nativeErrorDomain === "CBErrorDomain" && e.nativeErrorCode === 14) {
260
- return true;
261
- }
262
- const reason = typeof e.reason === "string" ? e.reason : "";
263
- const message = typeof e.message === "string" ? e.message : "";
264
- if (/\bCBErrorDomain:14(?:\s|$)/.test(message) || reason.includes("Peer removed pairing information") || reason.includes("GATT_INSUF_AUTHENTICATION")) {
265
- return true;
266
- }
267
- pending.push(e.originalError, e.err);
268
- }
269
- }
270
- return false;
271
- }
272
250
  var PAIRING_FAILURE_TAGS = /* @__PURE__ */ new Set([
273
251
  ERROR_TAG.BlePairingTimeout,
274
252
  ERROR_TAG.BleGattBondingFailed,
@@ -287,7 +265,6 @@ var CONNECTION_LEVEL_TAGS = /* @__PURE__ */ new Set([
287
265
  ERROR_TAG.BlePairingTimeout,
288
266
  ERROR_TAG.BleGattBondingFailed,
289
267
  ERROR_TAG.PairingRefused,
290
- ERROR_TAG.PeerRemovedPairing,
291
268
  ERROR_TAG.DeviceNotRecognized,
292
269
  ERROR_TAG.NoAccessibleDevice,
293
270
  ERROR_TAG.UnknownDevice,
@@ -524,8 +501,6 @@ function mapLedgerError(err, opts) {
524
501
  code = import_hwk_adapter_core.HardwareErrorCode.DeviceLocked;
525
502
  } else if (isDeviceNotAdvertisingError(err) || isDeviceNotFoundError(err)) {
526
503
  code = import_hwk_adapter_core.HardwareErrorCode.DeviceNotFound;
527
- } else if (isBleBondInvalidError(err)) {
528
- code = import_hwk_adapter_core.HardwareErrorCode.BleBondInvalid;
529
504
  } else if (isBlePairingFailureError(err)) {
530
505
  code = import_hwk_adapter_core.HardwareErrorCode.BlePairingTimeout;
531
506
  } else if (isDeviceBusyError(err)) {
@@ -1138,7 +1113,6 @@ var _LedgerAdapter = class _LedgerAdapter {
1138
1113
  this._discoveredDevices.clear();
1139
1114
  this._sessions.clear();
1140
1115
  this._sessionTransports.clear();
1141
- this._activeConnectionType = void 0;
1142
1116
  this._pendingOperationBindings.clear();
1143
1117
  this._pendingPickBindings.clear();
1144
1118
  this._selectedBleReconnectTargets.clear();
@@ -1271,7 +1245,6 @@ var _LedgerAdapter = class _LedgerAdapter {
1271
1245
  /** Binds the picked BLE device; with a recorded wallet identity it must match first. */
1272
1246
  async bindBleDevice(params) {
1273
1247
  const identity = params.identity?.vendor === "ledger" ? params.identity : void 0;
1274
- const identities = identity ? [identity, ...params.fallbackIdentities ?? []] : [];
1275
1248
  try {
1276
1249
  return await this._jobQueue.enqueue(
1277
1250
  "__ledger_bind_ble__",
@@ -1308,27 +1281,24 @@ var _LedgerAdapter = class _LedgerAdapter {
1308
1281
  message: "Selected Ledger connection ended"
1309
1282
  });
1310
1283
  }
1311
- const checked = await this._fingerprintFirstOpenableChain(
1312
- identities,
1313
- (chain, autoInstallApp) => this._computeChainFingerprint(
1314
- chain,
1315
- (method, callParams) => this._runConnectorCall(
1316
- connectId,
1317
- method,
1318
- callParams,
1319
- signal,
1320
- void 0,
1321
- void 0,
1322
- { autoInstallApp },
1323
- { connection: { connectId, sessionId } }
1324
- )
1284
+ const fingerprint = await this._computeChainFingerprint(
1285
+ identity.chain,
1286
+ (method, callParams) => this._runConnectorCall(
1287
+ connectId,
1288
+ method,
1289
+ callParams,
1290
+ signal,
1291
+ void 0,
1292
+ void 0,
1293
+ { autoInstallApp: true },
1294
+ { connection: { connectId, sessionId } }
1325
1295
  )
1326
1296
  );
1327
- if (checked.fingerprint !== checked.identity.value) {
1297
+ if (fingerprint !== identity.value) {
1328
1298
  await this._dropSession(connectId, signal);
1329
1299
  throw (0, import_hwk_adapter_core4.createHwkError)({
1330
1300
  code: import_hwk_adapter_core4.HardwareErrorCode.DeviceMismatch,
1331
- message: formatDeviceMismatchError(checked.identity.value, checked.fingerprint)
1301
+ message: formatDeviceMismatchError(identity.value, fingerprint)
1332
1302
  });
1333
1303
  }
1334
1304
  await this._saveSelectedBleBinding(
@@ -1337,7 +1307,7 @@ var _LedgerAdapter = class _LedgerAdapter {
1337
1307
  attempt,
1338
1308
  void 0,
1339
1309
  signal,
1340
- checked.identity
1310
+ identity
1341
1311
  );
1342
1312
  }
1343
1313
  if (!attempt.bindingSaved) {
@@ -2113,24 +2083,6 @@ var _LedgerAdapter = class _LedgerAdapter {
2113
2083
  return this.errorToFailure(err);
2114
2084
  }
2115
2085
  }
2116
- /**
2117
- * Fingerprints the first recorded chain whose app the device can open. Only a missing app
2118
- * moves on to the next chain; when every app is missing, the first chain offers its install.
2119
- */
2120
- async _fingerprintFirstOpenableChain(identities, fingerprintOf) {
2121
- const [first] = identities;
2122
- if (identities.length > 1) {
2123
- for (const identity of identities) {
2124
- try {
2125
- return { identity, fingerprint: await fingerprintOf(identity.chain, false) };
2126
- } catch (error) {
2127
- const isAppMissing = isAppNotInstalledError(error) || error?.code === import_hwk_adapter_core4.HardwareErrorCode.AppNotInstalled;
2128
- if (!isAppMissing) throw error;
2129
- }
2130
- }
2131
- }
2132
- return { identity: first, fingerprint: await fingerprintOf(first.chain, true) };
2133
- }
2134
2086
  /** Saves a picked BLE endpoint; `identity` is set when its wallet was just checked. */
2135
2087
  async _saveSelectedBleBinding(connectId, selectionRequestId, context, operationId, signal, identity) {
2136
2088
  try {
@@ -2379,7 +2331,7 @@ var _LedgerAdapter = class _LedgerAdapter {
2379
2331
  async ensureConnected(connectId, signal, allowUsbEphemeralFallback = false, preserveOperationId, context) {
2380
2332
  if (signal.aborted) _LedgerAdapter._throwIfAborted(signal);
2381
2333
  this._assertConnectorReady("connectDevice");
2382
- if (!this.connector.availableTransports?.includes("usb") && this._isBleConnection() && !connectId && !allowUsbEphemeralFallback) {
2334
+ if (this._isBleConnection() && !connectId && !allowUsbEphemeralFallback) {
2383
2335
  throw Object.assign(new Error("Ledger BLE connectId is required."), {
2384
2336
  code: import_hwk_adapter_core4.HardwareErrorCode.DeviceNotFound
2385
2337
  });
@@ -2413,7 +2365,6 @@ var _LedgerAdapter = class _LedgerAdapter {
2413
2365
  if (this._doConnectAbortController === controller) {
2414
2366
  this._connectingPromise = null;
2415
2367
  this._doConnectAbortController = null;
2416
- if (this._sessions.size === 0) this._activeConnectionType = void 0;
2417
2368
  }
2418
2369
  }
2419
2370
  })();
@@ -2456,16 +2407,19 @@ var _LedgerAdapter = class _LedgerAdapter {
2456
2407
  if (knownBle?.transport === "ble") {
2457
2408
  return this._connectDeviceOrThrow(knownBle.connectId, preserveOperationId, internalSignal);
2458
2409
  }
2459
- const bleContext = context ?? {};
2460
- bleContext.allowBleSelection = true;
2461
- bleContext.usbFallbackConnectId = usbTarget ?? targetConnectId;
2410
+ if (targetConnectId && context?.knownConnections === void 0) {
2411
+ throw (0, import_hwk_adapter_core4.createHwkError)({
2412
+ code: import_hwk_adapter_core4.HardwareErrorCode.DeviceNotFound,
2413
+ message: "Ledger connection metadata is required before starting Bluetooth binding"
2414
+ });
2415
+ }
2462
2416
  const bleDevices = await this._searchDevices({ transportType: "ble" }, internalSignal);
2463
2417
  return this._connectFirstOrSelect(
2464
2418
  bleDevices,
2465
2419
  void 0,
2466
2420
  allowUsbEphemeralFallback,
2467
2421
  preserveOperationId,
2468
- bleContext,
2422
+ context,
2469
2423
  internalSignal
2470
2424
  );
2471
2425
  }
@@ -2554,7 +2508,7 @@ var _LedgerAdapter = class _LedgerAdapter {
2554
2508
  }
2555
2509
  }
2556
2510
  const requiresBleSelection = this._isBleConnection();
2557
- if (requiresBleSelection && !allowUsbEphemeralFallback && !context?.allowBleSelection) {
2511
+ if (requiresBleSelection && !allowUsbEphemeralFallback) {
2558
2512
  throw Object.assign(new Error("Ledger BLE connectId is required."), {
2559
2513
  code: import_hwk_adapter_core4.HardwareErrorCode.DeviceNotFound
2560
2514
  });
@@ -2566,8 +2520,7 @@ var _LedgerAdapter = class _LedgerAdapter {
2566
2520
  const knownUsb = context?.knownConnections?.find(
2567
2521
  (connection) => connection.transport === "usb"
2568
2522
  );
2569
- const usbConnectId = knownUsb?.transport === "usb" ? knownUsb.connectId : context?.usbFallbackConnectId ?? targetConnectId;
2570
- const maySelectAnyUsb = allowUsbEphemeralFallback || !usbConnectId && !context?.knownConnections?.some((connection) => connection.transport !== "qr");
2523
+ const usbConnectId = knownUsb?.transport === "usb" ? knownUsb.connectId : targetConnectId;
2571
2524
  const { device, requestId } = await (0, import_hwk_adapter_core4.requestBleDeviceSelection)({
2572
2525
  emitter: this.emitter,
2573
2526
  registry: this._uiRegistry,
@@ -2578,7 +2531,7 @@ var _LedgerAdapter = class _LedgerAdapter {
2578
2531
  const usbDevices = await this._searchDevices({ transportType: "usb" }, signal);
2579
2532
  const candidate = usbDevices.find(
2580
2533
  (device2) => device2.connectionType === "usb" && device2.connectId === usbConnectId
2581
- ) ?? (maySelectAnyUsb && usbDevices.length === 1 && usbDevices[0].connectionType === "usb" ? usbDevices[0] : void 0);
2534
+ ) ?? (usbDevices.length === 1 && usbDevices[0].connectionType === "usb" ? usbDevices[0] : void 0);
2582
2535
  if (candidate) return [candidate];
2583
2536
  }
2584
2537
  return this._searchDevices({ transportType: "ble", waitForAllTransports: true }, signal);
@@ -3600,14 +3553,11 @@ var import_hwk_adapter_core5 = require("@onekeyfe/hwk-adapter-core");
3600
3553
  var import_device_management_kit = require("@ledgerhq/device-management-kit");
3601
3554
  var IDLE_WATCHDOG_MS = 65e3;
3602
3555
  var INTERACTIVE_TIMEOUT_MS = 5 * 6e4;
3603
- var INTERACTION_PROMPT_DELAY_MS = 500;
3604
3556
  function deviceActionToPromise(action, onInteraction, timeoutMs = IDLE_WATCHDOG_MS, onRegisterCanceller, onIntermediate) {
3605
3557
  return new Promise((resolve, reject) => {
3606
3558
  let settled = false;
3607
3559
  let lastStep;
3608
3560
  let activeInteraction;
3609
- let pendingInteraction;
3610
- let pendingInteractionTimer = null;
3611
3561
  const observedSteps = [];
3612
3562
  let sub;
3613
3563
  let timer = null;
@@ -3635,52 +3585,31 @@ function deviceActionToPromise(action, onInteraction, timeoutMs = IDLE_WATCHDOG_
3635
3585
  }
3636
3586
  return false;
3637
3587
  };
3638
- const cancelPendingInteraction = () => {
3639
- if (pendingInteractionTimer) clearTimeout(pendingInteractionTimer);
3640
- pendingInteractionTimer = null;
3641
- pendingInteraction = void 0;
3642
- };
3643
- const showInteraction = (interactionName) => {
3644
- activeInteraction = interactionName;
3645
- debugLog(
3646
- "[DeviceAction] requiredUserInteraction",
3647
- `interaction=${interactionName}`,
3648
- `step=${lastStep ?? "unknown"}`
3649
- );
3650
- if (interactionName === "unlock-device" && timer) {
3651
- clearTimeout(timer);
3652
- timer = null;
3653
- }
3654
- onInteraction?.(interactionName);
3655
- };
3656
3588
  const emitInteraction = (interaction) => {
3657
3589
  if (!onInteraction) {
3658
3590
  return;
3659
3591
  }
3660
3592
  if (!interaction || interaction === "none") {
3661
- cancelPendingInteraction();
3662
3593
  clearActiveInteraction();
3663
3594
  return;
3664
3595
  }
3665
3596
  const interactionName = String(interaction);
3666
- if (interactionName === activeInteraction || interactionName === pendingInteraction) {
3597
+ if (interactionName === activeInteraction) {
3667
3598
  return;
3668
3599
  }
3669
- cancelPendingInteraction();
3670
- clearActiveInteraction();
3671
- if (interactionName === "unlock-device") {
3672
- showInteraction(interactionName);
3673
- return;
3600
+ activeInteraction = interactionName;
3601
+ debugLog(
3602
+ "[DeviceAction] requiredUserInteraction",
3603
+ `interaction=${interactionName}`,
3604
+ `step=${lastStep ?? "unknown"}`
3605
+ );
3606
+ if (interaction === "unlock-device" && timer) {
3607
+ clearTimeout(timer);
3608
+ timer = null;
3674
3609
  }
3675
- pendingInteraction = interactionName;
3676
- pendingInteractionTimer = setTimeout(() => {
3677
- pendingInteractionTimer = null;
3678
- pendingInteraction = void 0;
3679
- if (!settled) showInteraction(interactionName);
3680
- }, INTERACTION_PROMPT_DELAY_MS);
3610
+ onInteraction(interactionName);
3681
3611
  };
3682
3612
  const completeInteraction = () => {
3683
- cancelPendingInteraction();
3684
3613
  if (!clearActiveInteraction()) {
3685
3614
  onInteraction?.("interaction-complete");
3686
3615
  }
@@ -5258,13 +5187,6 @@ var LedgerConnectorBase = class {
5258
5187
  } catch (err) {
5259
5188
  this._resetSignersAndSessions();
5260
5189
  if (isLedgerBleConnectionType(this.connectionType)) {
5261
- if (isBleBondInvalidError(err)) {
5262
- throw (0, import_hwk_adapter_core13.createHwkError)({
5263
- code: import_hwk_adapter_core13.HardwareErrorCode.BleBondInvalid,
5264
- message: "Ledger Bluetooth pairing is no longer valid. Forget this device in system Bluetooth settings, then reconnect.",
5265
- _tag: ERROR_TAG.PeerRemovedPairing
5266
- });
5267
- }
5268
5190
  const tag = err?._tag;
5269
5191
  if (isKnownConnectionTag(tag) && !isConnectionOpeningTag(tag)) {
5270
5192
  throw err;