@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.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 someErrorInChain(err, matches) {
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,31 +240,38 @@ function someErrorInChain(err, matches) {
233
240
  if (candidate && typeof candidate === "object" && !seen.has(candidate)) {
234
241
  seen.add(candidate);
235
242
  const e = candidate;
236
- if (matches(e)) return true;
243
+ if (matches(e)) return e;
237
244
  pending.push(e.originalError, e.err);
238
245
  }
239
246
  }
240
- 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));
241
254
  }
242
255
  function isBleBondInvalidError(err) {
243
256
  return someErrorInChain(err, (e) => {
244
- if (e._tag === ERROR_TAG.PeerRemovedPairing || e.iosErrorCode === 14 || e.attErrorCode === 5 || e.nativeErrorDomain === "CBErrorDomain" && e.nativeErrorCode === 14) {
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
+ ) {
245
261
  return true;
246
262
  }
247
263
  const reason = typeof e.reason === "string" ? e.reason : "";
248
- const message = typeof e.message === "string" ? e.message : "";
249
- 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");
264
+ return reason.includes("Peer removed pairing information") || reason.includes("GATT_INSUF_AUTHENTICATION");
250
265
  });
251
266
  }
252
267
  function isBleNotAdvertisingTimeoutError(err) {
253
268
  return someErrorInChain(
254
269
  err,
255
- (e) => typeof e.message === "string" && /connect timed out after \d+ms: device not advertising$/.test(e.message)
270
+ (e) => e.code === HardwareErrorCode.DeviceNotFound && e.params?.notAdvertising === true
256
271
  );
257
272
  }
258
273
  var PAIRING_FAILURE_TAGS = /* @__PURE__ */ new Set([
259
274
  ERROR_TAG.BlePairingTimeout,
260
- ERROR_TAG.BleGattBondingFailed,
261
275
  ERROR_TAG.PairingRefused
262
276
  ]);
263
277
  function isBlePairingFailureError(err) {
@@ -514,6 +528,8 @@ function mapLedgerError(err, opts) {
514
528
  code = HardwareErrorCode.BleBondInvalid;
515
529
  } else if (isBlePairingFailureError(err)) {
516
530
  code = HardwareErrorCode.BlePairingTimeout;
531
+ } else if (isBleGattBondingFailedError(err)) {
532
+ code = HardwareErrorCode.BleConnectFailed;
517
533
  } else if (isDeviceBusyError(err)) {
518
534
  code = HardwareErrorCode.DeviceBusy;
519
535
  } else if (isUserAbortedError(err)) {
@@ -2680,7 +2696,8 @@ var _LedgerAdapter = class _LedgerAdapter {
2680
2696
  if (!result.success) {
2681
2697
  const payload = result.payload;
2682
2698
  const rethrow = Object.assign(new Error(payload.error), {
2683
- code: payload.code
2699
+ code: payload.code,
2700
+ ...payload.params ? { params: payload.params } : {}
2684
2701
  });
2685
2702
  if (payload._tag !== void 0) {
2686
2703
  rethrow._tag = payload._tag;
@@ -3264,9 +3281,10 @@ var _LedgerAdapter = class _LedgerAdapter {
3264
3281
  }
3265
3282
  const { granted, reason, message } = response;
3266
3283
  if (!granted) {
3267
- throw Object.assign(new Error(message ?? "Device permission denied"), {
3284
+ throw createHwkError({
3268
3285
  code: HardwareErrorCode3.DevicePermissionDenied,
3269
- reason
3286
+ message: message ?? "Device permission denied",
3287
+ ...reason ? { params: { permissionDeniedReason: reason } } : {}
3270
3288
  });
3271
3289
  }
3272
3290
  }
@@ -3279,13 +3297,12 @@ var _LedgerAdapter = class _LedgerAdapter {
3279
3297
  const tag = err && typeof err === "object" ? err._tag : void 0;
3280
3298
  if (err && typeof err === "object" && "code" in err && typeof err.code === "number") {
3281
3299
  const e = err;
3282
- const params = e.code === HardwareErrorCode3.DevicePermissionDenied && e.reason ? { permissionDeniedReason: e.reason } : e.params;
3283
3300
  return ledgerFailure(
3284
3301
  e.code,
3285
3302
  e.message ?? "Unknown error",
3286
3303
  e.appName,
3287
3304
  tag,
3288
- params,
3305
+ e.params,
3289
3306
  void 0,
3290
3307
  isHwkRecoveryHint(e.recovery) ? e.recovery : void 0
3291
3308
  );
@@ -3344,6 +3361,7 @@ import {
3344
3361
  EConnectorInteraction as EConnectorInteraction3,
3345
3362
  HardwareErrorCode as HardwareErrorCode7,
3346
3363
  createHwkError as createHwkError2,
3364
+ isHardwareErrorCode as isHardwareErrorCode2,
3347
3365
  isKnownNonTargetHardwareVendor,
3348
3366
  serializeConnectorError
3349
3367
  } from "@onekeyfe/hwk-adapter-core";
@@ -5001,9 +5019,6 @@ function withOpenAppName(event, data, appName) {
5001
5019
  }
5002
5020
  return { ...uiEvent, payload: { ...uiEvent.payload, appName } };
5003
5021
  }
5004
- var HARDWARE_ERROR_CODE_VALUES = new Set(
5005
- Object.values(HardwareErrorCode7).filter((value) => typeof value === "number")
5006
- );
5007
5022
  var BLE_CONNECT_SCAN_TIMEOUT_MS = 1500;
5008
5023
  var LEDGER_RELAY_ALLOWED_ROOT_DOMAINS = ["onekeytest.com", "onekey.com"];
5009
5024
  function isAllowedLedgerRelayHost(hostname) {
@@ -5304,17 +5319,20 @@ var LedgerConnectorBase = class {
5304
5319
  });
5305
5320
  }
5306
5321
  if (isBleNotAdvertisingTimeoutError(err)) throwNotAdvertising();
5322
+ const canonical = findCanonicalBleError(err);
5323
+ if (canonical) throw canonical;
5307
5324
  const tag = err?._tag;
5308
5325
  if (isKnownConnectionTag(tag) && !isConnectionOpeningTag(tag)) {
5309
5326
  throw err;
5310
5327
  }
5311
- const wrapped = new Error(
5312
- "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 }
5313
5335
  );
5314
- wrapped._tag = ERROR_TAG.BleGattBondingFailed;
5315
- wrapped.code = HardwareErrorCode7.BlePairingTimeout;
5316
- wrapped.originalError = err;
5317
- throw wrapped;
5318
5336
  }
5319
5337
  if (err && typeof err === "object" && err._tag) {
5320
5338
  const taggedError = err instanceof Error ? err : Object.assign(
@@ -5831,8 +5849,7 @@ var LedgerConnectorBase = class {
5831
5849
  }
5832
5850
  _wrapError(err, opts) {
5833
5851
  const src = err && typeof err === "object" ? err : {};
5834
- const hasSerializedCode = typeof src.code === "number" && HARDWARE_ERROR_CODE_VALUES.has(src.code);
5835
- const mapped = hasSerializedCode ? {
5852
+ const mapped = isHardwareErrorCode2(src.code) ? {
5836
5853
  code: src.code,
5837
5854
  message: typeof src.message === "string" ? src.message : "Unknown Ledger error",
5838
5855
  appName: typeof src.appName === "string" ? src.appName : opts?.defaultAppName