@onekeyfe/hwk-ledger-adapter 1.2.11-alpha.4 → 1.2.11-alpha.7

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
@@ -33,7 +33,8 @@ import {
33
33
  HardwareErrorCode,
34
34
  defaultOriginForCode,
35
35
  defaultRecoveryForCode,
36
- enrichErrorMessage
36
+ enrichErrorMessage,
37
+ isHardwareErrorCode
37
38
  } from "@onekeyfe/hwk-adapter-core";
38
39
  function ledgerFailure(code, error, appName, tag, params, origin, recovery) {
39
40
  const payload = { error, code };
@@ -225,7 +226,13 @@ function isDeviceNotAdvertisingError(err) {
225
226
  if (!err || typeof err !== "object") return false;
226
227
  return err._tag === ERROR_TAG.DeviceNotAdvertising;
227
228
  }
228
- function isBleBondInvalidError(err) {
229
+ function isBleGattBondingFailedError(err) {
230
+ if (!err || typeof err !== "object") return false;
231
+ if (err._tag === ERROR_TAG.BleGattBondingFailed) return true;
232
+ const orig = err.originalError;
233
+ return orig != null && isBleGattBondingFailedError(orig);
234
+ }
235
+ function findErrorInChain(err, matches) {
229
236
  const pending = [err];
230
237
  const seen = /* @__PURE__ */ new Set();
231
238
  while (pending.length > 0) {
@@ -233,22 +240,38 @@ function isBleBondInvalidError(err) {
233
240
  if (candidate && typeof candidate === "object" && !seen.has(candidate)) {
234
241
  seen.add(candidate);
235
242
  const e = candidate;
236
- if (e._tag === ERROR_TAG.PeerRemovedPairing || e.iosErrorCode === 14 || e.attErrorCode === 5 || e.nativeErrorDomain === "CBErrorDomain" && e.nativeErrorCode === 14) {
237
- return true;
238
- }
239
- const reason = typeof e.reason === "string" ? e.reason : "";
240
- const message = typeof e.message === "string" ? e.message : "";
241
- if (/\bCBErrorDomain:14(?:\s|$)/.test(message) || reason.includes("Peer removed pairing information") || reason.includes("GATT_INSUF_AUTHENTICATION")) {
242
- return true;
243
- }
243
+ if (matches(e)) return e;
244
244
  pending.push(e.originalError, e.err);
245
245
  }
246
246
  }
247
- return false;
247
+ return void 0;
248
+ }
249
+ function someErrorInChain(err, matches) {
250
+ return findErrorInChain(err, matches) !== void 0;
251
+ }
252
+ function findCanonicalBleError(err) {
253
+ return findErrorInChain(err, (e) => isHardwareErrorCode(e.code));
254
+ }
255
+ function isBleBondInvalidError(err) {
256
+ return someErrorInChain(err, (e) => {
257
+ if (
258
+ // Already canonical: the desktop BLE host or a connector mapped the native signal.
259
+ e.code === HardwareErrorCode.BleBondInvalid || e._tag === ERROR_TAG.PeerRemovedPairing || e.iosErrorCode === 14 || e.attErrorCode === 5 || e.nativeErrorDomain === "CBErrorDomain" && e.nativeErrorCode === 14
260
+ ) {
261
+ return true;
262
+ }
263
+ const reason = typeof e.reason === "string" ? e.reason : "";
264
+ return reason.includes("Peer removed pairing information") || reason.includes("GATT_INSUF_AUTHENTICATION");
265
+ });
266
+ }
267
+ function isBleNotAdvertisingTimeoutError(err) {
268
+ return someErrorInChain(
269
+ err,
270
+ (e) => e.code === HardwareErrorCode.DeviceNotFound && e.params?.notAdvertising === true
271
+ );
248
272
  }
249
273
  var PAIRING_FAILURE_TAGS = /* @__PURE__ */ new Set([
250
274
  ERROR_TAG.BlePairingTimeout,
251
- ERROR_TAG.BleGattBondingFailed,
252
275
  ERROR_TAG.PairingRefused
253
276
  ]);
254
277
  function isBlePairingFailureError(err) {
@@ -505,6 +528,8 @@ function mapLedgerError(err, opts) {
505
528
  code = HardwareErrorCode.BleBondInvalid;
506
529
  } else if (isBlePairingFailureError(err)) {
507
530
  code = HardwareErrorCode.BlePairingTimeout;
531
+ } else if (isBleGattBondingFailedError(err)) {
532
+ code = HardwareErrorCode.BleConnectFailed;
508
533
  } else if (isDeviceBusyError(err)) {
509
534
  code = HardwareErrorCode.DeviceBusy;
510
535
  } else if (isUserAbortedError(err)) {
@@ -939,6 +964,7 @@ var _LedgerAdapter = class _LedgerAdapter {
939
964
  this._operations = new OperationRegistry({
940
965
  vendor: "ledger",
941
966
  onEnded: (operation, reason) => {
967
+ debugLog("[LedgerAdapter] operation ended", { operationId: operation.operationId, reason });
942
968
  this._discardPendingBinding(this._pendingOperationBindings.get(operation.operationId));
943
969
  this._pendingOperationBindings.delete(operation.operationId);
944
970
  this.emitter.emit(SDK.OPERATION_ENDED, {
@@ -2670,7 +2696,8 @@ var _LedgerAdapter = class _LedgerAdapter {
2670
2696
  if (!result.success) {
2671
2697
  const payload = result.payload;
2672
2698
  const rethrow = Object.assign(new Error(payload.error), {
2673
- code: payload.code
2699
+ code: payload.code,
2700
+ ...payload.params ? { params: payload.params } : {}
2674
2701
  });
2675
2702
  if (payload._tag !== void 0) {
2676
2703
  rethrow._tag = payload._tag;
@@ -3254,9 +3281,10 @@ var _LedgerAdapter = class _LedgerAdapter {
3254
3281
  }
3255
3282
  const { granted, reason, message } = response;
3256
3283
  if (!granted) {
3257
- throw Object.assign(new Error(message ?? "Device permission denied"), {
3284
+ throw createHwkError({
3258
3285
  code: HardwareErrorCode3.DevicePermissionDenied,
3259
- reason
3286
+ message: message ?? "Device permission denied",
3287
+ ...reason ? { params: { permissionDeniedReason: reason } } : {}
3260
3288
  });
3261
3289
  }
3262
3290
  }
@@ -3269,13 +3297,12 @@ var _LedgerAdapter = class _LedgerAdapter {
3269
3297
  const tag = err && typeof err === "object" ? err._tag : void 0;
3270
3298
  if (err && typeof err === "object" && "code" in err && typeof err.code === "number") {
3271
3299
  const e = err;
3272
- const params = e.code === HardwareErrorCode3.DevicePermissionDenied && e.reason ? { permissionDeniedReason: e.reason } : e.params;
3273
3300
  return ledgerFailure(
3274
3301
  e.code,
3275
3302
  e.message ?? "Unknown error",
3276
3303
  e.appName,
3277
3304
  tag,
3278
- params,
3305
+ e.params,
3279
3306
  void 0,
3280
3307
  isHwkRecoveryHint(e.recovery) ? e.recovery : void 0
3281
3308
  );
@@ -3334,6 +3361,7 @@ import {
3334
3361
  EConnectorInteraction as EConnectorInteraction3,
3335
3362
  HardwareErrorCode as HardwareErrorCode7,
3336
3363
  createHwkError as createHwkError2,
3364
+ isHardwareErrorCode as isHardwareErrorCode2,
3337
3365
  isKnownNonTargetHardwareVendor,
3338
3366
  serializeConnectorError
3339
3367
  } from "@onekeyfe/hwk-adapter-core";
@@ -4991,9 +5019,6 @@ function withOpenAppName(event, data, appName) {
4991
5019
  }
4992
5020
  return { ...uiEvent, payload: { ...uiEvent.payload, appName } };
4993
5021
  }
4994
- var HARDWARE_ERROR_CODE_VALUES = new Set(
4995
- Object.values(HardwareErrorCode7).filter((value) => typeof value === "number")
4996
- );
4997
5022
  var BLE_CONNECT_SCAN_TIMEOUT_MS = 1500;
4998
5023
  var LEDGER_RELAY_ALLOWED_ROOT_DOMAINS = ["onekeytest.com", "onekey.com"];
4999
5024
  function isAllowedLedgerRelayHost(hostname) {
@@ -5053,6 +5078,11 @@ async function _getDeviceApps(ctx, sessionId) {
5053
5078
  };
5054
5079
  return apps;
5055
5080
  }
5081
+ function describeCaller(error) {
5082
+ return (error.stack ?? "").split("\n").slice(2, 6).map(
5083
+ (frame) => frame.trim().replace(/^at /, "").replace(/ \(.*\)$/, "")
5084
+ ).join(" <- ");
5085
+ }
5056
5086
  var LedgerConnectorBase = class {
5057
5087
  constructor(createTransport, options) {
5058
5088
  this._deviceManager = null;
@@ -5288,17 +5318,21 @@ var LedgerConnectorBase = class {
5288
5318
  _tag: ERROR_TAG.PeerRemovedPairing
5289
5319
  });
5290
5320
  }
5321
+ if (isBleNotAdvertisingTimeoutError(err)) throwNotAdvertising();
5322
+ const canonical = findCanonicalBleError(err);
5323
+ if (canonical) throw canonical;
5291
5324
  const tag = err?._tag;
5292
5325
  if (isKnownConnectionTag(tag) && !isConnectionOpeningTag(tag)) {
5293
5326
  throw err;
5294
5327
  }
5295
- const wrapped = new Error(
5296
- "Ledger Bluetooth pairing failed. Make sure the device is unlocked and nearby, then try again."
5328
+ throw Object.assign(
5329
+ createHwkError2({
5330
+ code: HardwareErrorCode7.BleConnectFailed,
5331
+ message: "Ledger Bluetooth connection failed. Make sure the device is unlocked and nearby, then try again.",
5332
+ _tag: ERROR_TAG.BleGattBondingFailed
5333
+ }),
5334
+ { originalError: err }
5297
5335
  );
5298
- wrapped._tag = ERROR_TAG.BleGattBondingFailed;
5299
- wrapped.code = HardwareErrorCode7.BlePairingTimeout;
5300
- wrapped.originalError = err;
5301
- throw wrapped;
5302
5336
  }
5303
5337
  if (err && typeof err === "object" && err._tag) {
5304
5338
  const taggedError = err instanceof Error ? err : Object.assign(
@@ -5312,6 +5346,7 @@ var LedgerConnectorBase = class {
5312
5346
  }
5313
5347
  async disconnect(sessionId) {
5314
5348
  if (!this._deviceManager) return;
5349
+ debugLog("[DMK] disconnect requested", { caller: describeCaller(new Error()) });
5315
5350
  const deviceId = this._deviceManager.getDeviceId(sessionId);
5316
5351
  this._signerManager?.invalidate(sessionId);
5317
5352
  this._unwatchSessionState(sessionId);
@@ -5814,8 +5849,7 @@ var LedgerConnectorBase = class {
5814
5849
  }
5815
5850
  _wrapError(err, opts) {
5816
5851
  const src = err && typeof err === "object" ? err : {};
5817
- const hasSerializedCode = typeof src.code === "number" && HARDWARE_ERROR_CODE_VALUES.has(src.code);
5818
- const mapped = hasSerializedCode ? {
5852
+ const mapped = isHardwareErrorCode2(src.code) ? {
5819
5853
  code: src.code,
5820
5854
  message: typeof src.message === "string" ? src.message : "Unknown Ledger error",
5821
5855
  appName: typeof src.appName === "string" ? src.appName : opts?.defaultAppName