@onekeyfe/hwk-ledger-adapter 1.2.10-alpha.3 → 1.2.11-alpha.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.js CHANGED
@@ -248,7 +248,13 @@ function isDeviceNotAdvertisingError(err) {
248
248
  if (!err || typeof err !== "object") return false;
249
249
  return err._tag === ERROR_TAG.DeviceNotAdvertising;
250
250
  }
251
- function someErrorInChain(err, matches) {
251
+ function isBleGattBondingFailedError(err) {
252
+ if (!err || typeof err !== "object") return false;
253
+ if (err._tag === ERROR_TAG.BleGattBondingFailed) return true;
254
+ const orig = err.originalError;
255
+ return orig != null && isBleGattBondingFailedError(orig);
256
+ }
257
+ function findErrorInChain(err, matches) {
252
258
  const pending = [err];
253
259
  const seen = /* @__PURE__ */ new Set();
254
260
  while (pending.length > 0) {
@@ -256,31 +262,38 @@ function someErrorInChain(err, matches) {
256
262
  if (candidate && typeof candidate === "object" && !seen.has(candidate)) {
257
263
  seen.add(candidate);
258
264
  const e = candidate;
259
- if (matches(e)) return true;
265
+ if (matches(e)) return e;
260
266
  pending.push(e.originalError, e.err);
261
267
  }
262
268
  }
263
- return false;
269
+ return void 0;
270
+ }
271
+ function someErrorInChain(err, matches) {
272
+ return findErrorInChain(err, matches) !== void 0;
273
+ }
274
+ function findCanonicalBleError(err) {
275
+ return findErrorInChain(err, (e) => (0, import_hwk_adapter_core.isHardwareErrorCode)(e.code));
264
276
  }
265
277
  function isBleBondInvalidError(err) {
266
278
  return someErrorInChain(err, (e) => {
267
- if (e._tag === ERROR_TAG.PeerRemovedPairing || e.iosErrorCode === 14 || e.attErrorCode === 5 || e.nativeErrorDomain === "CBErrorDomain" && e.nativeErrorCode === 14) {
279
+ if (
280
+ // Already canonical: the desktop BLE host or a connector mapped the native signal.
281
+ e.code === import_hwk_adapter_core.HardwareErrorCode.BleBondInvalid || e._tag === ERROR_TAG.PeerRemovedPairing || e.iosErrorCode === 14 || e.attErrorCode === 5 || e.nativeErrorDomain === "CBErrorDomain" && e.nativeErrorCode === 14
282
+ ) {
268
283
  return true;
269
284
  }
270
285
  const reason = typeof e.reason === "string" ? e.reason : "";
271
- const message = typeof e.message === "string" ? e.message : "";
272
- return /\bCBErrorDomain:14(?:\s|$)/.test(message) || /'\$onekey-3p-ble-subscribe': Error: status: 3$/.test(message) || reason.includes("Peer removed pairing information") || reason.includes("GATT_INSUF_AUTHENTICATION");
286
+ return reason.includes("Peer removed pairing information") || reason.includes("GATT_INSUF_AUTHENTICATION");
273
287
  });
274
288
  }
275
289
  function isBleNotAdvertisingTimeoutError(err) {
276
290
  return someErrorInChain(
277
291
  err,
278
- (e) => typeof e.message === "string" && /connect timed out after \d+ms: device not advertising$/.test(e.message)
292
+ (e) => e.code === import_hwk_adapter_core.HardwareErrorCode.DeviceNotFound && e.params?.notAdvertising === true
279
293
  );
280
294
  }
281
295
  var PAIRING_FAILURE_TAGS = /* @__PURE__ */ new Set([
282
296
  ERROR_TAG.BlePairingTimeout,
283
- ERROR_TAG.BleGattBondingFailed,
284
297
  ERROR_TAG.PairingRefused
285
298
  ]);
286
299
  function isBlePairingFailureError(err) {
@@ -537,6 +550,8 @@ function mapLedgerError(err, opts) {
537
550
  code = import_hwk_adapter_core.HardwareErrorCode.BleBondInvalid;
538
551
  } else if (isBlePairingFailureError(err)) {
539
552
  code = import_hwk_adapter_core.HardwareErrorCode.BlePairingTimeout;
553
+ } else if (isBleGattBondingFailedError(err)) {
554
+ code = import_hwk_adapter_core.HardwareErrorCode.BleConnectFailed;
540
555
  } else if (isDeviceBusyError(err)) {
541
556
  code = import_hwk_adapter_core.HardwareErrorCode.DeviceBusy;
542
557
  } else if (isUserAbortedError(err)) {
@@ -2693,7 +2708,8 @@ var _LedgerAdapter = class _LedgerAdapter {
2693
2708
  if (!result.success) {
2694
2709
  const payload = result.payload;
2695
2710
  const rethrow = Object.assign(new Error(payload.error), {
2696
- code: payload.code
2711
+ code: payload.code,
2712
+ ...payload.params ? { params: payload.params } : {}
2697
2713
  });
2698
2714
  if (payload._tag !== void 0) {
2699
2715
  rethrow._tag = payload._tag;
@@ -3277,9 +3293,10 @@ var _LedgerAdapter = class _LedgerAdapter {
3277
3293
  }
3278
3294
  const { granted, reason, message } = response;
3279
3295
  if (!granted) {
3280
- throw Object.assign(new Error(message ?? "Device permission denied"), {
3296
+ throw (0, import_hwk_adapter_core4.createHwkError)({
3281
3297
  code: import_hwk_adapter_core4.HardwareErrorCode.DevicePermissionDenied,
3282
- reason
3298
+ message: message ?? "Device permission denied",
3299
+ ...reason ? { params: { permissionDeniedReason: reason } } : {}
3283
3300
  });
3284
3301
  }
3285
3302
  }
@@ -3292,13 +3309,12 @@ var _LedgerAdapter = class _LedgerAdapter {
3292
3309
  const tag = err && typeof err === "object" ? err._tag : void 0;
3293
3310
  if (err && typeof err === "object" && "code" in err && typeof err.code === "number") {
3294
3311
  const e = err;
3295
- const params = e.code === import_hwk_adapter_core4.HardwareErrorCode.DevicePermissionDenied && e.reason ? { permissionDeniedReason: e.reason } : e.params;
3296
3312
  return ledgerFailure(
3297
3313
  e.code,
3298
3314
  e.message ?? "Unknown error",
3299
3315
  e.appName,
3300
3316
  tag,
3301
- params,
3317
+ e.params,
3302
3318
  void 0,
3303
3319
  (0, import_hwk_adapter_core4.isHwkRecoveryHint)(e.recovery) ? e.recovery : void 0
3304
3320
  );
@@ -5008,9 +5024,6 @@ function withOpenAppName(event, data, appName) {
5008
5024
  }
5009
5025
  return { ...uiEvent, payload: { ...uiEvent.payload, appName } };
5010
5026
  }
5011
- var HARDWARE_ERROR_CODE_VALUES = new Set(
5012
- Object.values(import_hwk_adapter_core13.HardwareErrorCode).filter((value) => typeof value === "number")
5013
- );
5014
5027
  var BLE_CONNECT_SCAN_TIMEOUT_MS = 1500;
5015
5028
  var LEDGER_RELAY_ALLOWED_ROOT_DOMAINS = ["onekeytest.com", "onekey.com"];
5016
5029
  function isAllowedLedgerRelayHost(hostname) {
@@ -5311,17 +5324,20 @@ var LedgerConnectorBase = class {
5311
5324
  });
5312
5325
  }
5313
5326
  if (isBleNotAdvertisingTimeoutError(err)) throwNotAdvertising();
5327
+ const canonical = findCanonicalBleError(err);
5328
+ if (canonical) throw canonical;
5314
5329
  const tag = err?._tag;
5315
5330
  if (isKnownConnectionTag(tag) && !isConnectionOpeningTag(tag)) {
5316
5331
  throw err;
5317
5332
  }
5318
- const wrapped = new Error(
5319
- "Ledger Bluetooth pairing failed. Make sure the device is unlocked and nearby, then try again."
5333
+ throw Object.assign(
5334
+ (0, import_hwk_adapter_core13.createHwkError)({
5335
+ code: import_hwk_adapter_core13.HardwareErrorCode.BleConnectFailed,
5336
+ message: "Ledger Bluetooth connection failed. Make sure the device is unlocked and nearby, then try again.",
5337
+ _tag: ERROR_TAG.BleGattBondingFailed
5338
+ }),
5339
+ { originalError: err }
5320
5340
  );
5321
- wrapped._tag = ERROR_TAG.BleGattBondingFailed;
5322
- wrapped.code = import_hwk_adapter_core13.HardwareErrorCode.BlePairingTimeout;
5323
- wrapped.originalError = err;
5324
- throw wrapped;
5325
5341
  }
5326
5342
  if (err && typeof err === "object" && err._tag) {
5327
5343
  const taggedError = err instanceof Error ? err : Object.assign(
@@ -5838,8 +5854,7 @@ var LedgerConnectorBase = class {
5838
5854
  }
5839
5855
  _wrapError(err, opts) {
5840
5856
  const src = err && typeof err === "object" ? err : {};
5841
- const hasSerializedCode = typeof src.code === "number" && HARDWARE_ERROR_CODE_VALUES.has(src.code);
5842
- const mapped = hasSerializedCode ? {
5857
+ const mapped = (0, import_hwk_adapter_core13.isHardwareErrorCode)(src.code) ? {
5843
5858
  code: src.code,
5844
5859
  message: typeof src.message === "string" ? src.message : "Unknown Ledger error",
5845
5860
  appName: typeof src.appName === "string" ? src.appName : opts?.defaultAppName