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

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
@@ -418,6 +418,28 @@ interface LedgerConnectorBaseOptions {
418
418
  */
419
419
  dmk?: DeviceManagementKit;
420
420
  }
421
+ /**
422
+ * Shared base class for Ledger IConnector implementations.
423
+ *
424
+ * Encapsulates all shared logic: device discovery, connection management,
425
+ * method dispatch (EVM / BTC / SOL / TRON), signer lifecycle, event emission,
426
+ * and error handling.
427
+ *
428
+ * Chain-specific method implementations live in `./chains/` and receive
429
+ * a ConnectorContext that exposes shared helpers.
430
+ *
431
+ * Subclasses only need to:
432
+ * 1. Supply a transport factory via the constructor.
433
+ * 2. Optionally override `_resolveConnectId()` for transport-specific
434
+ * device identity resolution.
435
+ *
436
+ * Invariant: one instance = one transport. `connectionType` is fixed at
437
+ * construction. To switch transport (e.g. desktop BLE ↔ WebHID), the host
438
+ * must build a new connector instance and replace the old one — don't add
439
+ * multiple transports to a single instance. Lifting this constraint would
440
+ * require routing `connectionType` from `descriptor.transport` per-device
441
+ * and updating every `this.connectionType` branch in this file.
442
+ */
421
443
  declare class LedgerConnectorBase implements IConnector {
422
444
  private _deviceManager;
423
445
  private _signerManager;
package/dist/index.d.ts CHANGED
@@ -418,6 +418,28 @@ interface LedgerConnectorBaseOptions {
418
418
  */
419
419
  dmk?: DeviceManagementKit;
420
420
  }
421
+ /**
422
+ * Shared base class for Ledger IConnector implementations.
423
+ *
424
+ * Encapsulates all shared logic: device discovery, connection management,
425
+ * method dispatch (EVM / BTC / SOL / TRON), signer lifecycle, event emission,
426
+ * and error handling.
427
+ *
428
+ * Chain-specific method implementations live in `./chains/` and receive
429
+ * a ConnectorContext that exposes shared helpers.
430
+ *
431
+ * Subclasses only need to:
432
+ * 1. Supply a transport factory via the constructor.
433
+ * 2. Optionally override `_resolveConnectId()` for transport-specific
434
+ * device identity resolution.
435
+ *
436
+ * Invariant: one instance = one transport. `connectionType` is fixed at
437
+ * construction. To switch transport (e.g. desktop BLE ↔ WebHID), the host
438
+ * must build a new connector instance and replace the old one — don't add
439
+ * multiple transports to a single instance. Lifting this constraint would
440
+ * require routing `connectionType` from `descriptor.transport` per-device
441
+ * and updating every `this.connectionType` branch in this file.
442
+ */
421
443
  declare class LedgerConnectorBase implements IConnector {
422
444
  private _deviceManager;
423
445
  private _signerManager;
package/dist/index.js CHANGED
@@ -248,13 +248,7 @@ function isDeviceNotAdvertisingError(err) {
248
248
  if (!err || typeof err !== "object") return false;
249
249
  return err._tag === ERROR_TAG.DeviceNotAdvertising;
250
250
  }
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) {
251
+ function isBleBondInvalidError(err) {
258
252
  const pending = [err];
259
253
  const seen = /* @__PURE__ */ new Set();
260
254
  while (pending.length > 0) {
@@ -262,38 +256,22 @@ function findErrorInChain(err, matches) {
262
256
  if (candidate && typeof candidate === "object" && !seen.has(candidate)) {
263
257
  seen.add(candidate);
264
258
  const e = candidate;
265
- if (matches(e)) return e;
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
+ }
266
267
  pending.push(e.originalError, e.err);
267
268
  }
268
269
  }
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));
276
- }
277
- function isBleBondInvalidError(err) {
278
- return someErrorInChain(err, (e) => {
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
- ) {
283
- return true;
284
- }
285
- const reason = typeof e.reason === "string" ? e.reason : "";
286
- return reason.includes("Peer removed pairing information") || reason.includes("GATT_INSUF_AUTHENTICATION");
287
- });
288
- }
289
- function isBleNotAdvertisingTimeoutError(err) {
290
- return someErrorInChain(
291
- err,
292
- (e) => e.code === import_hwk_adapter_core.HardwareErrorCode.DeviceNotFound && e.params?.notAdvertising === true
293
- );
270
+ return false;
294
271
  }
295
272
  var PAIRING_FAILURE_TAGS = /* @__PURE__ */ new Set([
296
273
  ERROR_TAG.BlePairingTimeout,
274
+ ERROR_TAG.BleGattBondingFailed,
297
275
  ERROR_TAG.PairingRefused
298
276
  ]);
299
277
  function isBlePairingFailureError(err) {
@@ -550,8 +528,6 @@ function mapLedgerError(err, opts) {
550
528
  code = import_hwk_adapter_core.HardwareErrorCode.BleBondInvalid;
551
529
  } else if (isBlePairingFailureError(err)) {
552
530
  code = import_hwk_adapter_core.HardwareErrorCode.BlePairingTimeout;
553
- } else if (isBleGattBondingFailedError(err)) {
554
- code = import_hwk_adapter_core.HardwareErrorCode.BleConnectFailed;
555
531
  } else if (isDeviceBusyError(err)) {
556
532
  code = import_hwk_adapter_core.HardwareErrorCode.DeviceBusy;
557
533
  } else if (isUserAbortedError(err)) {
@@ -976,7 +952,6 @@ var _LedgerAdapter = class _LedgerAdapter {
976
952
  this._operations = new import_hwk_adapter_core4.OperationRegistry({
977
953
  vendor: "ledger",
978
954
  onEnded: (operation, reason) => {
979
- debugLog("[LedgerAdapter] operation ended", { operationId: operation.operationId, reason });
980
955
  this._discardPendingBinding(this._pendingOperationBindings.get(operation.operationId));
981
956
  this._pendingOperationBindings.delete(operation.operationId);
982
957
  this.emitter.emit(import_hwk_adapter_core4.SDK.OPERATION_ENDED, {
@@ -2708,8 +2683,7 @@ var _LedgerAdapter = class _LedgerAdapter {
2708
2683
  if (!result.success) {
2709
2684
  const payload = result.payload;
2710
2685
  const rethrow = Object.assign(new Error(payload.error), {
2711
- code: payload.code,
2712
- ...payload.params ? { params: payload.params } : {}
2686
+ code: payload.code
2713
2687
  });
2714
2688
  if (payload._tag !== void 0) {
2715
2689
  rethrow._tag = payload._tag;
@@ -3293,10 +3267,9 @@ var _LedgerAdapter = class _LedgerAdapter {
3293
3267
  }
3294
3268
  const { granted, reason, message } = response;
3295
3269
  if (!granted) {
3296
- throw (0, import_hwk_adapter_core4.createHwkError)({
3270
+ throw Object.assign(new Error(message ?? "Device permission denied"), {
3297
3271
  code: import_hwk_adapter_core4.HardwareErrorCode.DevicePermissionDenied,
3298
- message: message ?? "Device permission denied",
3299
- ...reason ? { params: { permissionDeniedReason: reason } } : {}
3272
+ reason
3300
3273
  });
3301
3274
  }
3302
3275
  }
@@ -3309,12 +3282,13 @@ var _LedgerAdapter = class _LedgerAdapter {
3309
3282
  const tag = err && typeof err === "object" ? err._tag : void 0;
3310
3283
  if (err && typeof err === "object" && "code" in err && typeof err.code === "number") {
3311
3284
  const e = err;
3285
+ const params = e.code === import_hwk_adapter_core4.HardwareErrorCode.DevicePermissionDenied && e.reason ? { permissionDeniedReason: e.reason } : e.params;
3312
3286
  return ledgerFailure(
3313
3287
  e.code,
3314
3288
  e.message ?? "Unknown error",
3315
3289
  e.appName,
3316
3290
  tag,
3317
- e.params,
3291
+ params,
3318
3292
  void 0,
3319
3293
  (0, import_hwk_adapter_core4.isHwkRecoveryHint)(e.recovery) ? e.recovery : void 0
3320
3294
  );
@@ -5024,6 +4998,9 @@ function withOpenAppName(event, data, appName) {
5024
4998
  }
5025
4999
  return { ...uiEvent, payload: { ...uiEvent.payload, appName } };
5026
5000
  }
5001
+ var HARDWARE_ERROR_CODE_VALUES = new Set(
5002
+ Object.values(import_hwk_adapter_core13.HardwareErrorCode).filter((value) => typeof value === "number")
5003
+ );
5027
5004
  var BLE_CONNECT_SCAN_TIMEOUT_MS = 1500;
5028
5005
  var LEDGER_RELAY_ALLOWED_ROOT_DOMAINS = ["onekeytest.com", "onekey.com"];
5029
5006
  function isAllowedLedgerRelayHost(hostname) {
@@ -5083,11 +5060,6 @@ async function _getDeviceApps(ctx, sessionId) {
5083
5060
  };
5084
5061
  return apps;
5085
5062
  }
5086
- function describeCaller(error) {
5087
- return (error.stack ?? "").split("\n").slice(2, 6).map(
5088
- (frame) => frame.trim().replace(/^at /, "").replace(/ \(.*\)$/, "")
5089
- ).join(" <- ");
5090
- }
5091
5063
  var LedgerConnectorBase = class {
5092
5064
  constructor(createTransport, options) {
5093
5065
  this._deviceManager = null;
@@ -5323,21 +5295,17 @@ var LedgerConnectorBase = class {
5323
5295
  _tag: ERROR_TAG.PeerRemovedPairing
5324
5296
  });
5325
5297
  }
5326
- if (isBleNotAdvertisingTimeoutError(err)) throwNotAdvertising();
5327
- const canonical = findCanonicalBleError(err);
5328
- if (canonical) throw canonical;
5329
5298
  const tag = err?._tag;
5330
5299
  if (isKnownConnectionTag(tag) && !isConnectionOpeningTag(tag)) {
5331
5300
  throw err;
5332
5301
  }
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 }
5302
+ const wrapped = new Error(
5303
+ "Ledger Bluetooth pairing failed. Make sure the device is unlocked and nearby, then try again."
5340
5304
  );
5305
+ wrapped._tag = ERROR_TAG.BleGattBondingFailed;
5306
+ wrapped.code = import_hwk_adapter_core13.HardwareErrorCode.BlePairingTimeout;
5307
+ wrapped.originalError = err;
5308
+ throw wrapped;
5341
5309
  }
5342
5310
  if (err && typeof err === "object" && err._tag) {
5343
5311
  const taggedError = err instanceof Error ? err : Object.assign(
@@ -5351,7 +5319,6 @@ var LedgerConnectorBase = class {
5351
5319
  }
5352
5320
  async disconnect(sessionId) {
5353
5321
  if (!this._deviceManager) return;
5354
- debugLog("[DMK] disconnect requested", { caller: describeCaller(new Error()) });
5355
5322
  const deviceId = this._deviceManager.getDeviceId(sessionId);
5356
5323
  this._signerManager?.invalidate(sessionId);
5357
5324
  this._unwatchSessionState(sessionId);
@@ -5854,7 +5821,8 @@ var LedgerConnectorBase = class {
5854
5821
  }
5855
5822
  _wrapError(err, opts) {
5856
5823
  const src = err && typeof err === "object" ? err : {};
5857
- const mapped = (0, import_hwk_adapter_core13.isHardwareErrorCode)(src.code) ? {
5824
+ const hasSerializedCode = typeof src.code === "number" && HARDWARE_ERROR_CODE_VALUES.has(src.code);
5825
+ const mapped = hasSerializedCode ? {
5858
5826
  code: src.code,
5859
5827
  message: typeof src.message === "string" ? src.message : "Unknown Ledger error",
5860
5828
  appName: typeof src.appName === "string" ? src.appName : opts?.defaultAppName