@onekeyfe/hwk-ledger-adapter 1.2.3-alpha.3 → 1.2.3-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.js CHANGED
@@ -491,6 +491,15 @@ function mapLedgerError(err, opts) {
491
491
  };
492
492
  }
493
493
 
494
+ // src/utils/queueKey.ts
495
+ var LEDGER_DEFAULT_QUEUE_KEY = "__ledger_default__";
496
+ function ledgerQueueKey({
497
+ operationId,
498
+ connectId
499
+ }) {
500
+ return (operationId ?? connectId) || LEDGER_DEFAULT_QUEUE_KEY;
501
+ }
502
+
494
503
  // src/adapter/methods/allNetworkGetAddress.ts
495
504
  var import_hwk_adapter_core2 = require("@onekeyfe/hwk-adapter-core");
496
505
 
@@ -551,7 +560,8 @@ function createAllNetworkGetAddress({
551
560
  callChain,
552
561
  getChainFingerprint,
553
562
  retainOperation,
554
- errorToFailure
563
+ errorToFailure,
564
+ createCancelScope
555
565
  }) {
556
566
  return async function allNetworkGetAddress(connectId, _deviceId, params) {
557
567
  debugLog("[LedgerAdapter][REQ]", {
@@ -568,6 +578,9 @@ function createAllNetworkGetAddress({
568
578
  } catch (error) {
569
579
  return errorToFailure(error);
570
580
  }
581
+ const cancelScope = createCancelScope(
582
+ ledgerQueueKey({ operationId: target.payload.operationId, connectId: effectiveTargetId })
583
+ );
571
584
  const installContext = {};
572
585
  const commonParams = {
573
586
  autoInstallApp: params.autoInstallApp,
@@ -585,6 +598,7 @@ function createAllNetworkGetAddress({
585
598
  normalizeItem: normalizeLedgerAllNetworkItem,
586
599
  buildUnsupportedNetworkResponse: (item) => isUnsupportedLedgerAllNetworkNetwork(item) ? buildUnsupportedNetworkResponse(item) : void 0,
587
600
  callItem: async ({ method, chain, item }) => {
601
+ if (cancelScope.signal.aborted) return buildCancelledFailure(cancelScope.signal);
588
602
  const itemDeviceId = getItemDeviceId(item) ?? chainFingerprints.get(chain) ?? "";
589
603
  return callAllNetworkMethod(
590
604
  callChain,
@@ -603,7 +617,8 @@ function createAllNetworkGetAddress({
603
617
  chain,
604
618
  payload,
605
619
  chainFingerprints,
606
- installContext
620
+ installContext,
621
+ cancelScope.signal
607
622
  ),
608
623
  shouldAbortBundle: isTopLevelAllNetworkFailure,
609
624
  buildTopLevelFailure: (response) => {
@@ -622,10 +637,17 @@ function createAllNetworkGetAddress({
622
637
  });
623
638
  return result;
624
639
  } finally {
640
+ cancelScope.release();
625
641
  releaseOperationRetention?.();
626
642
  }
627
643
  };
628
644
  }
645
+ function buildCancelledFailure(signal) {
646
+ const reason = signal.reason;
647
+ const code = typeof reason?.code === "number" ? reason.code : import_hwk_adapter_core2.HardwareErrorCode.UserAborted;
648
+ const message = typeof reason?.message === "string" ? reason.message : "";
649
+ return (0, import_hwk_adapter_core2.failure)(code, message || "All-network get-address cancelled");
650
+ }
629
651
  function isTopLevelAllNetworkFailure(response) {
630
652
  if (response.success) {
631
653
  return false;
@@ -661,8 +683,13 @@ function normalizeLedgerAllNetworkItem(method, item) {
661
683
  const coin = LEDGER_BTC_NETWORK_COIN_MAP[item.network];
662
684
  return coin ? { ...item, coin } : item;
663
685
  }
664
- async function attachLedgerIdentity(getChainFingerprint, connectId, item, chain, payload, chainFingerprints, context) {
665
- const fingerprint = getItemDeviceId(item) || chainFingerprints.get(chain) || await bootstrapChainFingerprint(
686
+ async function attachLedgerIdentity(getChainFingerprint, connectId, item, chain, payload, chainFingerprints, context, cancelSignal) {
687
+ const knownFingerprint = getItemDeviceId(item) || chainFingerprints.get(chain) || "";
688
+ if (!knownFingerprint && cancelSignal.aborted) {
689
+ const cancelled = buildCancelledFailure(cancelSignal);
690
+ return { ...item, success: false, payload: cancelled.payload };
691
+ }
692
+ const fingerprint = knownFingerprint || await bootstrapChainFingerprint(
666
693
  getChainFingerprint,
667
694
  context.connection?.connectId ?? connectId,
668
695
  chain,
@@ -851,7 +878,8 @@ var _LedgerAdapter = class _LedgerAdapter {
851
878
  }
852
879
  },
853
880
  retainOperation: (operationId) => this._operations.retain(operationId),
854
- errorToFailure: (error) => this.errorToFailure(error)
881
+ errorToFailure: (error) => this.errorToFailure(error),
882
+ createCancelScope: (queueKey) => this._jobQueue.createCancelScope(queueKey)
855
883
  });
856
884
  // ---------------------------------------------------------------------------
857
885
  // Private helpers
@@ -1870,14 +1898,23 @@ var _LedgerAdapter = class _LedgerAdapter {
1870
1898
  this._uiRegistry.cancel();
1871
1899
  this._finishBleBinding("cancelled");
1872
1900
  const interactionForPhysicalId = !operationId && connectId ? this._operations.findActiveByConnectionKey(connectId) : void 0;
1873
- const queueKey = operationId ?? (activeJobId === connectId ? connectId : interactionForPhysicalId?.operationId) ?? connectId;
1874
1901
  const pendingOperationId = operationId ?? interactionForPhysicalId?.operationId;
1875
1902
  if (!connectId) this._pendingOperationBindings.clear();
1876
1903
  else if (pendingOperationId) {
1877
1904
  this._pendingOperationBindings.delete(pendingOperationId);
1878
1905
  }
1879
- if (queueKey) {
1880
- this._jobQueue.cancelActiveAndPending(queueKey, userAbortReason);
1906
+ const queueKeys = /* @__PURE__ */ new Set();
1907
+ if (connectId) queueKeys.add(ledgerQueueKey({ connectId }));
1908
+ if (pendingOperationId) queueKeys.add(ledgerQueueKey({ operationId: pendingOperationId }));
1909
+ if (queueKeys.size) {
1910
+ let cancelledAnyJob = false;
1911
+ for (const key of queueKeys) {
1912
+ cancelledAnyJob = this._jobQueue.cancelActiveAndPending(key, userAbortReason) || cancelledAnyJob;
1913
+ }
1914
+ debugLog("[LedgerAdapter] cancel routed to queue keys", {
1915
+ queueKeys: [...queueKeys],
1916
+ cancelledAnyJob
1917
+ });
1881
1918
  } else {
1882
1919
  this._jobQueue.cancelActiveAndPending(void 0, userAbortReason);
1883
1920
  }
@@ -2545,7 +2582,7 @@ var _LedgerAdapter = class _LedgerAdapter {
2545
2582
  connectId: effectiveConnectId || "(empty)",
2546
2583
  params
2547
2584
  });
2548
- const queueKey = (operationId ?? effectiveConnectId) || "__ledger_default__";
2585
+ const queueKey = ledgerQueueKey({ operationId, connectId: effectiveConnectId });
2549
2586
  try {
2550
2587
  const result = await this._jobQueue.enqueue(
2551
2588
  queueKey,