@onekeyfe/hwk-ledger-adapter 1.2.8-alpha.13 → 1.2.8-alpha.131
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 +13 -2
- package/dist/index.d.ts +13 -2
- package/dist/index.js +445 -296
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +231 -81
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -54,7 +54,7 @@ __export(index_exports, {
|
|
|
54
54
|
module.exports = __toCommonJS(index_exports);
|
|
55
55
|
|
|
56
56
|
// src/adapter/LedgerAdapter.ts
|
|
57
|
-
var
|
|
57
|
+
var import_hwk_adapter_core4 = require("@onekeyfe/hwk-adapter-core");
|
|
58
58
|
|
|
59
59
|
// src/errors.ts
|
|
60
60
|
var import_hwk_adapter_core = require("@onekeyfe/hwk-adapter-core");
|
|
@@ -219,6 +219,7 @@ var ERROR_TAG = {
|
|
|
219
219
|
// outcome when the user doesn't confirm pairing on the device, or the
|
|
220
220
|
// existing bond is invalid. Observed in production after ~30s.
|
|
221
221
|
PairingRefused: "PairingRefusedError",
|
|
222
|
+
PeerRemovedPairing: "PeerRemovedPairingError",
|
|
222
223
|
// DMK remote-network failures (manager-api HTTP / secure-channel WS).
|
|
223
224
|
WebSocketConnection: "WebSocketConnectionError",
|
|
224
225
|
HttpFetch: "FetchError",
|
|
@@ -247,6 +248,27 @@ function isDeviceNotAdvertisingError(err) {
|
|
|
247
248
|
if (!err || typeof err !== "object") return false;
|
|
248
249
|
return err._tag === ERROR_TAG.DeviceNotAdvertising;
|
|
249
250
|
}
|
|
251
|
+
function isBleBondInvalidError(err) {
|
|
252
|
+
const pending = [err];
|
|
253
|
+
const seen = /* @__PURE__ */ new Set();
|
|
254
|
+
while (pending.length > 0) {
|
|
255
|
+
const candidate = pending.pop();
|
|
256
|
+
if (candidate && typeof candidate === "object" && !seen.has(candidate)) {
|
|
257
|
+
seen.add(candidate);
|
|
258
|
+
const e = candidate;
|
|
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
|
+
}
|
|
267
|
+
pending.push(e.originalError, e.err);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return false;
|
|
271
|
+
}
|
|
250
272
|
var PAIRING_FAILURE_TAGS = /* @__PURE__ */ new Set([
|
|
251
273
|
ERROR_TAG.BlePairingTimeout,
|
|
252
274
|
ERROR_TAG.BleGattBondingFailed,
|
|
@@ -265,6 +287,7 @@ var CONNECTION_LEVEL_TAGS = /* @__PURE__ */ new Set([
|
|
|
265
287
|
ERROR_TAG.BlePairingTimeout,
|
|
266
288
|
ERROR_TAG.BleGattBondingFailed,
|
|
267
289
|
ERROR_TAG.PairingRefused,
|
|
290
|
+
ERROR_TAG.PeerRemovedPairing,
|
|
268
291
|
ERROR_TAG.DeviceNotRecognized,
|
|
269
292
|
ERROR_TAG.NoAccessibleDevice,
|
|
270
293
|
ERROR_TAG.UnknownDevice,
|
|
@@ -501,6 +524,8 @@ function mapLedgerError(err, opts) {
|
|
|
501
524
|
code = import_hwk_adapter_core.HardwareErrorCode.DeviceLocked;
|
|
502
525
|
} else if (isDeviceNotAdvertisingError(err) || isDeviceNotFoundError(err)) {
|
|
503
526
|
code = import_hwk_adapter_core.HardwareErrorCode.DeviceNotFound;
|
|
527
|
+
} else if (isBleBondInvalidError(err)) {
|
|
528
|
+
code = import_hwk_adapter_core.HardwareErrorCode.BleBondInvalid;
|
|
504
529
|
} else if (isBlePairingFailureError(err)) {
|
|
505
530
|
code = import_hwk_adapter_core.HardwareErrorCode.BlePairingTimeout;
|
|
506
531
|
} else if (isDeviceBusyError(err)) {
|
|
@@ -555,7 +580,7 @@ function ledgerQueueKey({
|
|
|
555
580
|
}
|
|
556
581
|
|
|
557
582
|
// src/adapter/methods/allNetworkGetAddress.ts
|
|
558
|
-
var
|
|
583
|
+
var import_hwk_adapter_core3 = require("@onekeyfe/hwk-adapter-core");
|
|
559
584
|
|
|
560
585
|
// src/utils/sdkEventBus.ts
|
|
561
586
|
var listeners = /* @__PURE__ */ new Set();
|
|
@@ -602,6 +627,32 @@ function debugError(...args) {
|
|
|
602
627
|
emitLog("error", ...args);
|
|
603
628
|
}
|
|
604
629
|
|
|
630
|
+
// src/utils/logSummary.ts
|
|
631
|
+
var import_hwk_adapter_core2 = require("@onekeyfe/hwk-adapter-core");
|
|
632
|
+
var import_bitcoinjs_lib = require("bitcoinjs-lib");
|
|
633
|
+
var HEX_RE = /^(0x)?[0-9a-fA-F]+$/;
|
|
634
|
+
var BASE64_RE = /^[A-Za-z0-9+/]+={0,2}$/;
|
|
635
|
+
function parsePsbt(input) {
|
|
636
|
+
if (typeof input !== "string") return import_bitcoinjs_lib.Psbt.fromBuffer(input);
|
|
637
|
+
if (HEX_RE.test(input)) return import_bitcoinjs_lib.Psbt.fromHex(input.replace(/^0x/i, ""));
|
|
638
|
+
if (BASE64_RE.test(input)) return import_bitcoinjs_lib.Psbt.fromBase64(input);
|
|
639
|
+
return void 0;
|
|
640
|
+
}
|
|
641
|
+
var describePsbtForLog = (key, value) => {
|
|
642
|
+
if (key !== "psbt") return void 0;
|
|
643
|
+
try {
|
|
644
|
+
const psbt = parsePsbt(value);
|
|
645
|
+
if (!psbt) return void 0;
|
|
646
|
+
const unsignedTx = import_bitcoinjs_lib.Transaction.fromBuffer(psbt.data.globalMap.unsignedTx.toBuffer());
|
|
647
|
+
return `inputs=${psbt.inputCount} outputs=${psbt.txOutputs.length} txid=${unsignedTx.getId()}`;
|
|
648
|
+
} catch {
|
|
649
|
+
return void 0;
|
|
650
|
+
}
|
|
651
|
+
};
|
|
652
|
+
function summarizeForLog(value, options = {}) {
|
|
653
|
+
return (0, import_hwk_adapter_core2.summarizeForLog)(value, { ...options, describePayload: describePsbtForLog });
|
|
654
|
+
}
|
|
655
|
+
|
|
605
656
|
// src/adapter/methods/allNetworkGetAddress.ts
|
|
606
657
|
var LEDGER_BTC_NETWORK_COIN_MAP = {
|
|
607
658
|
tbtc: "Testnet",
|
|
@@ -624,7 +675,7 @@ function createAllNetworkGetAddress({
|
|
|
624
675
|
connectId,
|
|
625
676
|
itemCount: params.bundle.length
|
|
626
677
|
});
|
|
627
|
-
const target = (0,
|
|
678
|
+
const target = (0, import_hwk_adapter_core3.resolveHardwareOperationTarget)(connectId, params.operationId, "ledger");
|
|
628
679
|
if (!target.success) return target;
|
|
629
680
|
const effectiveTargetId = target.payload.targetId ?? "";
|
|
630
681
|
let releaseOperationRetention;
|
|
@@ -652,7 +703,7 @@ function createAllNetworkGetAddress({
|
|
|
652
703
|
(a, b) => Number(Boolean(getItemDeviceId(params.bundle[b]))) - Number(Boolean(getItemDeviceId(params.bundle[a])))
|
|
653
704
|
);
|
|
654
705
|
try {
|
|
655
|
-
const ordered = await (0,
|
|
706
|
+
const ordered = await (0, import_hwk_adapter_core3.runAllNetworkGetAddress)({
|
|
656
707
|
connectId: effectiveTargetId,
|
|
657
708
|
deviceId: _deviceId,
|
|
658
709
|
params: { ...params, bundle: order.map((index) => params.bundle[index]) },
|
|
@@ -660,7 +711,7 @@ function createAllNetworkGetAddress({
|
|
|
660
711
|
buildUnsupportedNetworkResponse: (item) => isUnsupportedLedgerAllNetworkNetwork(item) ? buildUnsupportedNetworkResponse(item) : void 0,
|
|
661
712
|
callItem: async ({ method, chain, item }) => {
|
|
662
713
|
if (cancelScope.signal.aborted)
|
|
663
|
-
return (0,
|
|
714
|
+
return (0, import_hwk_adapter_core3.buildAllNetworkCancelledFailure)(cancelScope.signal);
|
|
664
715
|
const itemDeviceId = getItemDeviceId(item) ?? chainFingerprints.get(chain) ?? "";
|
|
665
716
|
return callAllNetworkMethod(
|
|
666
717
|
callChain,
|
|
@@ -684,8 +735,8 @@ function createAllNetworkGetAddress({
|
|
|
684
735
|
),
|
|
685
736
|
shouldAbortBundle: isTopLevelAllNetworkFailure,
|
|
686
737
|
buildTopLevelFailure: (response) => {
|
|
687
|
-
const code = response.payload?.code ??
|
|
688
|
-
return (0,
|
|
738
|
+
const code = response.payload?.code ?? import_hwk_adapter_core3.HardwareErrorCode.DeviceMismatch;
|
|
739
|
+
return (0, import_hwk_adapter_core3.failure)(
|
|
689
740
|
code,
|
|
690
741
|
response.payload?.error ?? "All-network get-address aborted",
|
|
691
742
|
response.payload?.params
|
|
@@ -698,12 +749,12 @@ function createAllNetworkGetAddress({
|
|
|
698
749
|
ordered.payload.forEach((response, position) => {
|
|
699
750
|
responses[order[position]] = response;
|
|
700
751
|
});
|
|
701
|
-
result = (0,
|
|
752
|
+
result = (0, import_hwk_adapter_core3.success)(responses);
|
|
702
753
|
}
|
|
703
754
|
debugLog("[LedgerAdapter][RES]", {
|
|
704
755
|
method: "allNetworkGetAddress",
|
|
705
756
|
success: result.success,
|
|
706
|
-
payload: result
|
|
757
|
+
payload: summarizeForLog(result, { method: "allNetworkGetAddress", role: "result" })
|
|
707
758
|
});
|
|
708
759
|
return result;
|
|
709
760
|
} finally {
|
|
@@ -718,7 +769,7 @@ function isTopLevelAllNetworkFailure(response) {
|
|
|
718
769
|
return false;
|
|
719
770
|
}
|
|
720
771
|
const code = response.payload?.code;
|
|
721
|
-
return (0,
|
|
772
|
+
return (0, import_hwk_adapter_core3.isWalletSafetyFailure)(code) || (0, import_hwk_adapter_core3.isUserRefusal)(code) || (0, import_hwk_adapter_core3.isConnectionLost)(code);
|
|
722
773
|
}
|
|
723
774
|
function getItemDeviceId(item) {
|
|
724
775
|
const { deviceId } = item;
|
|
@@ -732,7 +783,7 @@ function buildUnsupportedNetworkResponse(item) {
|
|
|
732
783
|
...item,
|
|
733
784
|
success: false,
|
|
734
785
|
payload: {
|
|
735
|
-
code:
|
|
786
|
+
code: import_hwk_adapter_core3.HardwareErrorCode.ChainNotSupported,
|
|
736
787
|
error: `Ledger allNetwork does not support ${item.network.toLowerCase() === "doge" ? "Dogecoin" : item.network}`
|
|
737
788
|
}
|
|
738
789
|
};
|
|
@@ -751,7 +802,7 @@ function normalizeLedgerAllNetworkItem(method, item) {
|
|
|
751
802
|
async function attachLedgerIdentity(getChainFingerprint, connectId, item, chain, payload, chainFingerprints, context, cancelSignal) {
|
|
752
803
|
const knownFingerprint = getItemDeviceId(item) || chainFingerprints.get(chain) || "";
|
|
753
804
|
if (!knownFingerprint && cancelSignal.aborted) {
|
|
754
|
-
const cancelled = (0,
|
|
805
|
+
const cancelled = (0, import_hwk_adapter_core3.buildAllNetworkCancelledFailure)(cancelSignal);
|
|
755
806
|
return { ...item, success: false, payload: cancelled.payload };
|
|
756
807
|
}
|
|
757
808
|
const fingerprint = knownFingerprint || await bootstrapChainFingerprint(
|
|
@@ -789,7 +840,7 @@ function buildFingerprintBootstrapFailure(item, chain) {
|
|
|
789
840
|
...item,
|
|
790
841
|
success: false,
|
|
791
842
|
payload: {
|
|
792
|
-
code:
|
|
843
|
+
code: import_hwk_adapter_core3.HardwareErrorCode.DeviceMismatch,
|
|
793
844
|
error: `Could not establish chain fingerprint for ${chain} after device call; refusing to return unverifiable result. Please retry.`
|
|
794
845
|
}
|
|
795
846
|
};
|
|
@@ -853,7 +904,7 @@ async function callAllNetworkMethod(callChain, connectId, deviceId, method, item
|
|
|
853
904
|
);
|
|
854
905
|
default:
|
|
855
906
|
throw Object.assign(new Error(`Unsupported allNetwork method: ${method}`), {
|
|
856
|
-
code:
|
|
907
|
+
code: import_hwk_adapter_core3.HardwareErrorCode.InvalidParams
|
|
857
908
|
});
|
|
858
909
|
}
|
|
859
910
|
}
|
|
@@ -874,21 +925,16 @@ function isLedgerBleDescriptor(connectionType, descriptor) {
|
|
|
874
925
|
function formatDeviceMismatchError(expected, actual) {
|
|
875
926
|
return `Wrong device: expected ${expected}, got ${actual}`;
|
|
876
927
|
}
|
|
877
|
-
var LOG_REDACTED_RESULT_KEYS = ["ufvk", "orchardFvk"];
|
|
878
|
-
function isSigningMethod(method) {
|
|
879
|
-
return /sign/i.test(method);
|
|
880
|
-
}
|
|
881
|
-
function redactResultForLog(result, method) {
|
|
882
|
-
if (isSigningMethod(method)) return "[redacted]";
|
|
883
|
-
if (!result || typeof result !== "object") return result;
|
|
884
|
-
const keys = LOG_REDACTED_RESULT_KEYS.filter((key) => key in result);
|
|
885
|
-
if (!keys.length) return result;
|
|
886
|
-
return { ...result, ...Object.fromEntries(keys.map((key) => [key, "[redacted]"])) };
|
|
887
|
-
}
|
|
888
928
|
function isLostConnectionError(err) {
|
|
889
929
|
return isDeviceDisconnectedError(err) || isDeviceNotAdvertisingError(err) || isTimeoutError(err) || isConnectionLevelError(err);
|
|
890
930
|
}
|
|
891
931
|
var BTC_HIGH_INDEX_THRESHOLD = 100;
|
|
932
|
+
var APP_MANAGEMENT_METHODS = /* @__PURE__ */ new Set([
|
|
933
|
+
"installApp",
|
|
934
|
+
"listInstalledApps",
|
|
935
|
+
"listInstalledNames",
|
|
936
|
+
"listAvailableApps"
|
|
937
|
+
]);
|
|
892
938
|
function btcAccountIndexFromPath(path) {
|
|
893
939
|
const segments = path.replace(/^m\//, "").split("/");
|
|
894
940
|
if (segments.length < 3) return null;
|
|
@@ -902,17 +948,17 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
902
948
|
// Cancel frees the DMK action and its intent-queue slot, but cannot retract a
|
|
903
949
|
// confirmation screen the device is already showing.
|
|
904
950
|
this.cancelCapability = "stops-waiting";
|
|
905
|
-
this.emitter = new
|
|
906
|
-
this._operations = new
|
|
951
|
+
this.emitter = new import_hwk_adapter_core4.TypedEventEmitter();
|
|
952
|
+
this._operations = new import_hwk_adapter_core4.OperationRegistry({
|
|
907
953
|
vendor: "ledger",
|
|
908
954
|
onEnded: (operation, reason) => {
|
|
909
955
|
this._discardPendingBinding(this._pendingOperationBindings.get(operation.operationId));
|
|
910
956
|
this._pendingOperationBindings.delete(operation.operationId);
|
|
911
|
-
this.emitter.emit(
|
|
912
|
-
type:
|
|
957
|
+
this.emitter.emit(import_hwk_adapter_core4.SDK.OPERATION_ENDED, {
|
|
958
|
+
type: import_hwk_adapter_core4.SDK.OPERATION_ENDED,
|
|
913
959
|
payload: { operationId: operation.operationId, reason }
|
|
914
960
|
});
|
|
915
|
-
if (reason === "timeout") {
|
|
961
|
+
if (reason === "timeout" && !this._bareCallMayUseConnection(operation)) {
|
|
916
962
|
void this._releaseOperationConnection(operation);
|
|
917
963
|
}
|
|
918
964
|
}
|
|
@@ -928,7 +974,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
928
974
|
// `_activeConnectionType` moves with every discovery and can go stale.
|
|
929
975
|
this._sessionTransports = /* @__PURE__ */ new Map();
|
|
930
976
|
this._selectedBleReconnectTargets = /* @__PURE__ */ new Map();
|
|
931
|
-
this._uiRegistry = new
|
|
977
|
+
this._uiRegistry = new import_hwk_adapter_core4.UiRequestRegistry();
|
|
932
978
|
// BTC App rejects account index >= 100 unless display=true. Cached per
|
|
933
979
|
// adapter instance: first 100+ path asks the user once via UI request,
|
|
934
980
|
// subsequent 100+ paths in the same session auto-promote silently.
|
|
@@ -958,7 +1004,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
958
1004
|
chain,
|
|
959
1005
|
(method, params) => this.connectorCall(connectId, method, params, void 0, void 0, void 0, context)
|
|
960
1006
|
);
|
|
961
|
-
return (0,
|
|
1007
|
+
return (0, import_hwk_adapter_core4.success)(fingerprint);
|
|
962
1008
|
} catch (error) {
|
|
963
1009
|
return this.errorToFailure(error);
|
|
964
1010
|
}
|
|
@@ -980,8 +1026,8 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
980
1026
|
const deviceInfo = this.connectorDeviceToDeviceInfo(data.device);
|
|
981
1027
|
this._discoveredDevices.set(deviceInfo.connectId, deviceInfo);
|
|
982
1028
|
this._sessions.delete(deviceInfo.connectId);
|
|
983
|
-
this.emitter.emit(
|
|
984
|
-
type:
|
|
1029
|
+
this.emitter.emit(import_hwk_adapter_core4.DEVICE.CONNECT, {
|
|
1030
|
+
type: import_hwk_adapter_core4.DEVICE.CONNECT,
|
|
985
1031
|
payload: deviceInfo
|
|
986
1032
|
});
|
|
987
1033
|
};
|
|
@@ -994,8 +1040,8 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
994
1040
|
}
|
|
995
1041
|
this._discoveredDevices.delete(data.connectId);
|
|
996
1042
|
this._sessions.delete(data.connectId);
|
|
997
|
-
this.emitter.emit(
|
|
998
|
-
type:
|
|
1043
|
+
this.emitter.emit(import_hwk_adapter_core4.DEVICE.DISCONNECT, {
|
|
1044
|
+
type: import_hwk_adapter_core4.DEVICE.DISCONNECT,
|
|
999
1045
|
payload: { connectId: data.connectId }
|
|
1000
1046
|
});
|
|
1001
1047
|
};
|
|
@@ -1003,7 +1049,8 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1003
1049
|
// need to subscribe in one place. The AppInstallProgress variant re-keys
|
|
1004
1050
|
// sessionId to connectId via `_sessions`, and drops if no mapping exists.
|
|
1005
1051
|
this.uiEventForwarder = (event) => {
|
|
1006
|
-
|
|
1052
|
+
const operationId = this._activeOperationId();
|
|
1053
|
+
if (event.type === import_hwk_adapter_core4.EConnectorInteraction.AppInstallProgress) {
|
|
1007
1054
|
let connectId;
|
|
1008
1055
|
for (const [cid, sid] of this._sessions) {
|
|
1009
1056
|
if (sid === event.payload.sessionId) {
|
|
@@ -1030,7 +1077,8 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1030
1077
|
}
|
|
1031
1078
|
this._installProgressLastEmittedValue = progress;
|
|
1032
1079
|
this.emitter.emit("ui-event", {
|
|
1033
|
-
type:
|
|
1080
|
+
type: import_hwk_adapter_core4.EConnectorInteraction.AppInstallProgress,
|
|
1081
|
+
...operationId ? { operationId } : {},
|
|
1034
1082
|
payload: {
|
|
1035
1083
|
connectId,
|
|
1036
1084
|
appName,
|
|
@@ -1039,11 +1087,11 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1039
1087
|
});
|
|
1040
1088
|
return;
|
|
1041
1089
|
}
|
|
1042
|
-
this.emitter.emit("ui-event", event);
|
|
1090
|
+
this.emitter.emit("ui-event", operationId ? { ...event, operationId } : event);
|
|
1043
1091
|
};
|
|
1044
1092
|
this.connector = connector;
|
|
1045
1093
|
this._defaultAutoInstallApp = options?.autoInstallApp ?? false;
|
|
1046
|
-
this._jobQueue = new
|
|
1094
|
+
this._jobQueue = new import_hwk_adapter_core4.DeviceJobQueue();
|
|
1047
1095
|
this.registerEventListeners();
|
|
1048
1096
|
}
|
|
1049
1097
|
_discardPendingBinding(owner, status = "cancelled") {
|
|
@@ -1055,7 +1103,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1055
1103
|
_finishBleBinding(status) {
|
|
1056
1104
|
const selectionRequestId = this._bindingSelectionRequestId;
|
|
1057
1105
|
this._bindingSelectionRequestId = void 0;
|
|
1058
|
-
if (selectionRequestId) (0,
|
|
1106
|
+
if (selectionRequestId) (0, import_hwk_adapter_core4.emitDeviceBindingStatus)(this.emitter, selectionRequestId, status);
|
|
1059
1107
|
}
|
|
1060
1108
|
_isBleConnection() {
|
|
1061
1109
|
return isLedgerBleConnectionType(this._activeConnectionType ?? this.connector.connectionType);
|
|
@@ -1098,6 +1146,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1098
1146
|
this._discoveredDevices.clear();
|
|
1099
1147
|
this._sessions.clear();
|
|
1100
1148
|
this._sessionTransports.clear();
|
|
1149
|
+
this._activeConnectionType = void 0;
|
|
1101
1150
|
this._pendingOperationBindings.clear();
|
|
1102
1151
|
this._pendingPickBindings.clear();
|
|
1103
1152
|
this._selectedBleReconnectTargets.clear();
|
|
@@ -1152,8 +1201,8 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1152
1201
|
);
|
|
1153
1202
|
if (signal) _LedgerAdapter._throwIfAborted(signal);
|
|
1154
1203
|
if (stateGeneration !== this._stateGeneration) {
|
|
1155
|
-
throw (0,
|
|
1156
|
-
code:
|
|
1204
|
+
throw (0, import_hwk_adapter_core4.createHwkError)({
|
|
1205
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.UserAborted,
|
|
1157
1206
|
message: "Ledger discovery was reset"
|
|
1158
1207
|
});
|
|
1159
1208
|
}
|
|
@@ -1185,7 +1234,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1185
1234
|
}
|
|
1186
1235
|
async searchDeviceTargets(options) {
|
|
1187
1236
|
const devices = await this.searchDevices(options);
|
|
1188
|
-
return devices.map((device) => (0,
|
|
1237
|
+
return devices.map((device) => (0, import_hwk_adapter_core4.toPhysicalDeviceSearchTarget)("ledger", device));
|
|
1189
1238
|
}
|
|
1190
1239
|
// USB single-session invariant: evict all sessions, best-effort (see connectDevice).
|
|
1191
1240
|
async _evictAllSessions(preserveOperationId) {
|
|
@@ -1205,7 +1254,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1205
1254
|
}
|
|
1206
1255
|
static _createDeviceBusyError(method) {
|
|
1207
1256
|
return Object.assign(new Error(`Ledger device is busy while calling ${method}`), {
|
|
1208
|
-
code:
|
|
1257
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.DeviceBusy
|
|
1209
1258
|
});
|
|
1210
1259
|
}
|
|
1211
1260
|
async connectDevice(searchTargetId) {
|
|
@@ -1230,13 +1279,14 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1230
1279
|
/** Binds the picked BLE device; with a recorded wallet identity it must match first. */
|
|
1231
1280
|
async bindBleDevice(params) {
|
|
1232
1281
|
const identity = params.identity?.vendor === "ledger" ? params.identity : void 0;
|
|
1282
|
+
const identities = identity ? [identity, ...params.fallbackIdentities ?? []] : [];
|
|
1233
1283
|
try {
|
|
1234
1284
|
return await this._jobQueue.enqueue(
|
|
1235
1285
|
"__ledger_bind_ble__",
|
|
1236
1286
|
async (signal) => {
|
|
1237
1287
|
if (!this.getAvailableTransports().includes("ble")) {
|
|
1238
|
-
throw (0,
|
|
1239
|
-
code:
|
|
1288
|
+
throw (0, import_hwk_adapter_core4.createHwkError)({
|
|
1289
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.TransportNotAvailable,
|
|
1240
1290
|
message: "Ledger Bluetooth transport is not available"
|
|
1241
1291
|
});
|
|
1242
1292
|
}
|
|
@@ -1261,29 +1311,32 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1261
1311
|
attempt.pendingBinding = void 0;
|
|
1262
1312
|
const sessionId = this._sessions.get(connectId);
|
|
1263
1313
|
if (!sessionId) {
|
|
1264
|
-
throw (0,
|
|
1265
|
-
code:
|
|
1314
|
+
throw (0, import_hwk_adapter_core4.createHwkError)({
|
|
1315
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.DeviceDisconnected,
|
|
1266
1316
|
message: "Selected Ledger connection ended"
|
|
1267
1317
|
});
|
|
1268
1318
|
}
|
|
1269
|
-
const
|
|
1270
|
-
|
|
1271
|
-
(
|
|
1272
|
-
|
|
1273
|
-
method,
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1319
|
+
const checked = await this._fingerprintFirstOpenableChain(
|
|
1320
|
+
identities,
|
|
1321
|
+
(chain, autoInstallApp) => this._computeChainFingerprint(
|
|
1322
|
+
chain,
|
|
1323
|
+
(method, callParams) => this._runConnectorCall(
|
|
1324
|
+
connectId,
|
|
1325
|
+
method,
|
|
1326
|
+
callParams,
|
|
1327
|
+
signal,
|
|
1328
|
+
void 0,
|
|
1329
|
+
void 0,
|
|
1330
|
+
{ autoInstallApp },
|
|
1331
|
+
{ connection: { connectId, sessionId } }
|
|
1332
|
+
)
|
|
1280
1333
|
)
|
|
1281
1334
|
);
|
|
1282
|
-
if (fingerprint !== identity.value) {
|
|
1335
|
+
if (checked.fingerprint !== checked.identity.value) {
|
|
1283
1336
|
await this._dropSession(connectId, signal);
|
|
1284
|
-
throw (0,
|
|
1285
|
-
code:
|
|
1286
|
-
message: formatDeviceMismatchError(identity.value, fingerprint)
|
|
1337
|
+
throw (0, import_hwk_adapter_core4.createHwkError)({
|
|
1338
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.DeviceMismatch,
|
|
1339
|
+
message: formatDeviceMismatchError(checked.identity.value, checked.fingerprint)
|
|
1287
1340
|
});
|
|
1288
1341
|
}
|
|
1289
1342
|
await this._saveSelectedBleBinding(
|
|
@@ -1292,18 +1345,18 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1292
1345
|
attempt,
|
|
1293
1346
|
void 0,
|
|
1294
1347
|
signal,
|
|
1295
|
-
identity
|
|
1348
|
+
checked.identity
|
|
1296
1349
|
);
|
|
1297
1350
|
}
|
|
1298
1351
|
if (!attempt.bindingSaved) {
|
|
1299
1352
|
await this._dropSession(connectId, signal);
|
|
1300
|
-
throw (0,
|
|
1301
|
-
code:
|
|
1353
|
+
throw (0, import_hwk_adapter_core4.createHwkError)({
|
|
1354
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.UnknownError,
|
|
1302
1355
|
message: "Bluetooth binding could not be saved",
|
|
1303
1356
|
origin: "host"
|
|
1304
1357
|
});
|
|
1305
1358
|
}
|
|
1306
|
-
return (0,
|
|
1359
|
+
return (0, import_hwk_adapter_core4.success)(connectId);
|
|
1307
1360
|
} catch (error) {
|
|
1308
1361
|
this._finishBleBinding(signal.aborted ? "cancelled" : "failed");
|
|
1309
1362
|
throw error;
|
|
@@ -1385,7 +1438,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1385
1438
|
connectionType,
|
|
1386
1439
|
connectionKeys: [this._sessions.get(resolvedConnectId) ?? ""]
|
|
1387
1440
|
});
|
|
1388
|
-
return (0,
|
|
1441
|
+
return (0, import_hwk_adapter_core4.success)(operation.operationId);
|
|
1389
1442
|
}
|
|
1390
1443
|
async _connectTarget(connectId, preserveOperationId, signal) {
|
|
1391
1444
|
debugLog("[LedgerAdapter][REQ]", { method: "connectDevice", connectId, params: { connectId } });
|
|
@@ -1397,7 +1450,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1397
1450
|
}
|
|
1398
1451
|
if (this._isBleConnection() && !connectId) {
|
|
1399
1452
|
throw Object.assign(new Error("Ledger BLE connectId is required."), {
|
|
1400
|
-
code:
|
|
1453
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.DeviceNotFound
|
|
1401
1454
|
});
|
|
1402
1455
|
}
|
|
1403
1456
|
await this._ensureDevicePermission(connectId, void 0, signal);
|
|
@@ -1426,7 +1479,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1426
1479
|
if (signal?.aborted || stateGeneration !== this._stateGeneration) {
|
|
1427
1480
|
await this.connector.disconnect(session.sessionId).catch(() => void 0);
|
|
1428
1481
|
throw Object.assign(new Error("Ledger connection aborted"), {
|
|
1429
|
-
code:
|
|
1482
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.UserAborted
|
|
1430
1483
|
});
|
|
1431
1484
|
}
|
|
1432
1485
|
} finally {
|
|
@@ -1439,7 +1492,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1439
1492
|
if (session.deviceInfo) {
|
|
1440
1493
|
this._discoveredDevices.set(resolvedConnectId, session.deviceInfo);
|
|
1441
1494
|
}
|
|
1442
|
-
const result = (0,
|
|
1495
|
+
const result = (0, import_hwk_adapter_core4.success)(resolvedConnectId);
|
|
1443
1496
|
debugLog("[LedgerAdapter][RES]", { method: "connectDevice", success: true, payload: result });
|
|
1444
1497
|
return result;
|
|
1445
1498
|
} catch (err) {
|
|
@@ -1456,7 +1509,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1456
1509
|
const operation = this._operations.findActive(operationId);
|
|
1457
1510
|
if (operation) this._cancelOperationWork(operation);
|
|
1458
1511
|
const endedOperation = this._operations.release(operationId);
|
|
1459
|
-
if (!endedOperation) return;
|
|
1512
|
+
if (!endedOperation || this._bareCallMayUseConnection(endedOperation)) return;
|
|
1460
1513
|
const { connectId } = endedOperation;
|
|
1461
1514
|
debugLog("[LedgerAdapter][REQ]", {
|
|
1462
1515
|
method: "releaseOperation",
|
|
@@ -1482,15 +1535,26 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1482
1535
|
*/
|
|
1483
1536
|
_cancelOperationWork(operation) {
|
|
1484
1537
|
const { operationId } = operation;
|
|
1485
|
-
const reason = (0,
|
|
1486
|
-
code:
|
|
1538
|
+
const reason = (0, import_hwk_adapter_core4.createHwkError)({
|
|
1539
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.OperationEnded,
|
|
1487
1540
|
message: "Ledger operation was released",
|
|
1488
1541
|
params: { operationId, reason: "explicit" }
|
|
1489
1542
|
});
|
|
1490
1543
|
const queueKey = ledgerQueueKey({ operationId });
|
|
1491
1544
|
const ownsRunningCall = this._jobQueue.getActiveJob()?.deviceId === queueKey;
|
|
1492
1545
|
this._jobQueue.cancelActiveAndPending(queueKey, reason);
|
|
1493
|
-
if (ownsRunningCall)
|
|
1546
|
+
if (!ownsRunningCall) return;
|
|
1547
|
+
this._uiRegistry.cancel(void 0, void 0, operationId);
|
|
1548
|
+
void this.connector.cancel(this._sessions.get(operation.connectId) ?? operation.connectId);
|
|
1549
|
+
}
|
|
1550
|
+
/**
|
|
1551
|
+
* A running call by bare connectId (or still resolving its device by identity) may be on this
|
|
1552
|
+
* connection, e.g. waiting for an unlock; its later stages need it, so it stays cached for that
|
|
1553
|
+
* caller like any bare call's session. A lost connection is torn down regardless.
|
|
1554
|
+
*/
|
|
1555
|
+
_bareCallMayUseConnection(operation) {
|
|
1556
|
+
const activeKey = this._jobQueue.getActiveJob()?.deviceId;
|
|
1557
|
+
return activeKey !== void 0 && !(0, import_hwk_adapter_core4.isHardwareOperationId)(activeKey) && (activeKey === operation.connectId || activeKey === LEDGER_DEFAULT_QUEUE_KEY || operation.connectionKeys.includes(activeKey));
|
|
1494
1558
|
}
|
|
1495
1559
|
async _releaseOperationConnection(operation) {
|
|
1496
1560
|
const sessionIds = /* @__PURE__ */ new Set();
|
|
@@ -1515,7 +1579,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1515
1579
|
async getDeviceInfo(connectIdOrOperationId, deviceId) {
|
|
1516
1580
|
let connectId;
|
|
1517
1581
|
try {
|
|
1518
|
-
connectId = (0,
|
|
1582
|
+
connectId = (0, import_hwk_adapter_core4.isHardwareOperationId)(connectIdOrOperationId) ? this._operations.resolve(connectIdOrOperationId).connectId : connectIdOrOperationId;
|
|
1519
1583
|
} catch (error) {
|
|
1520
1584
|
return this.errorToFailure(error);
|
|
1521
1585
|
}
|
|
@@ -1528,7 +1592,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1528
1592
|
await this._ensureDevicePermission(connectId, deviceId);
|
|
1529
1593
|
const cached = this._discoveredDevices.get(connectId) ?? Array.from(this._discoveredDevices.values()).find((d) => d.deviceId === deviceId);
|
|
1530
1594
|
if (cached) {
|
|
1531
|
-
const result = (0,
|
|
1595
|
+
const result = (0, import_hwk_adapter_core4.success)(cached);
|
|
1532
1596
|
debugLog("[LedgerAdapter][RES]", {
|
|
1533
1597
|
method: "getDeviceInfo",
|
|
1534
1598
|
success: true,
|
|
@@ -1536,8 +1600,8 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1536
1600
|
});
|
|
1537
1601
|
return result;
|
|
1538
1602
|
}
|
|
1539
|
-
const notFound = (0,
|
|
1540
|
-
|
|
1603
|
+
const notFound = (0, import_hwk_adapter_core4.failure)(
|
|
1604
|
+
import_hwk_adapter_core4.HardwareErrorCode.DeviceNotFound,
|
|
1541
1605
|
"Device not found in cache. Call searchDevices() or wait for a device-connected event first."
|
|
1542
1606
|
);
|
|
1543
1607
|
debugLog("[LedgerAdapter][RES]", {
|
|
@@ -1585,7 +1649,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1585
1649
|
commonParams,
|
|
1586
1650
|
installContext
|
|
1587
1651
|
);
|
|
1588
|
-
return (0,
|
|
1652
|
+
return (0, import_hwk_adapter_core4.success)(result);
|
|
1589
1653
|
} catch (err) {
|
|
1590
1654
|
return this.errorToFailure(err);
|
|
1591
1655
|
}
|
|
@@ -1614,7 +1678,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1614
1678
|
commonParams: {
|
|
1615
1679
|
autoInstallApp: typeof autoInstallApp === "boolean" ? autoInstallApp : void 0,
|
|
1616
1680
|
operationId: typeof operationId === "string" ? operationId : void 0,
|
|
1617
|
-
...(0,
|
|
1681
|
+
...(0, import_hwk_adapter_core4.pickConnectionContext)({
|
|
1618
1682
|
knownConnections,
|
|
1619
1683
|
extra,
|
|
1620
1684
|
allowDeviceSelection,
|
|
@@ -1785,7 +1849,13 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1785
1849
|
params
|
|
1786
1850
|
);
|
|
1787
1851
|
}
|
|
1788
|
-
tronSignMessage(connectId, deviceId, params) {
|
|
1852
|
+
async tronSignMessage(connectId, deviceId, params) {
|
|
1853
|
+
if (params?.messageType !== "V2") {
|
|
1854
|
+
return (0, import_hwk_adapter_core4.failure)(
|
|
1855
|
+
import_hwk_adapter_core4.HardwareErrorCode.MethodNotSupported,
|
|
1856
|
+
'Ledger signs TRON messages as TIP-191 signMessageV2 only (messageType: "V2")'
|
|
1857
|
+
);
|
|
1858
|
+
}
|
|
1789
1859
|
return this.callChainWithMergedParams(
|
|
1790
1860
|
connectId,
|
|
1791
1861
|
deviceId,
|
|
@@ -1821,7 +1891,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1821
1891
|
async installApp(connectId, appName) {
|
|
1822
1892
|
try {
|
|
1823
1893
|
await this.connectorCall(connectId, "installApp", { appName });
|
|
1824
|
-
return (0,
|
|
1894
|
+
return (0, import_hwk_adapter_core4.success)(void 0);
|
|
1825
1895
|
} catch (err) {
|
|
1826
1896
|
return this.errorToFailure(err);
|
|
1827
1897
|
}
|
|
@@ -1829,7 +1899,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1829
1899
|
async listInstalledApps(connectId) {
|
|
1830
1900
|
try {
|
|
1831
1901
|
const result = await this.connectorCall(connectId, "listInstalledApps", {});
|
|
1832
|
-
return (0,
|
|
1902
|
+
return (0, import_hwk_adapter_core4.success)(result);
|
|
1833
1903
|
} catch (err) {
|
|
1834
1904
|
return this.errorToFailure(err);
|
|
1835
1905
|
}
|
|
@@ -1838,7 +1908,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1838
1908
|
async listInstalledNames(connectId) {
|
|
1839
1909
|
try {
|
|
1840
1910
|
const result = await this.connectorCall(connectId, "listInstalledNames", {});
|
|
1841
|
-
return (0,
|
|
1911
|
+
return (0, import_hwk_adapter_core4.success)(result);
|
|
1842
1912
|
} catch (err) {
|
|
1843
1913
|
return this.errorToFailure(err);
|
|
1844
1914
|
}
|
|
@@ -1846,7 +1916,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1846
1916
|
async listAvailableApps(connectId) {
|
|
1847
1917
|
try {
|
|
1848
1918
|
const result = await this.connectorCall(connectId, "listAvailableApps", {});
|
|
1849
|
-
return (0,
|
|
1919
|
+
return (0, import_hwk_adapter_core4.success)(result);
|
|
1850
1920
|
} catch (err) {
|
|
1851
1921
|
return this.errorToFailure(err);
|
|
1852
1922
|
}
|
|
@@ -1854,7 +1924,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1854
1924
|
async getLedgerFirmwareVersion(connectId) {
|
|
1855
1925
|
try {
|
|
1856
1926
|
const result = await this.connectorCall(connectId, "getFirmwareVersion", {});
|
|
1857
|
-
return (0,
|
|
1927
|
+
return (0, import_hwk_adapter_core4.success)(result);
|
|
1858
1928
|
} catch (err) {
|
|
1859
1929
|
return this.errorToFailure(err);
|
|
1860
1930
|
}
|
|
@@ -1862,7 +1932,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1862
1932
|
async getLedgerDeviceInfo(connectId) {
|
|
1863
1933
|
try {
|
|
1864
1934
|
const result = await this.connectorCall(connectId, "getDeviceInfo", {});
|
|
1865
|
-
return (0,
|
|
1935
|
+
return (0, import_hwk_adapter_core4.success)(result);
|
|
1866
1936
|
} catch (err) {
|
|
1867
1937
|
return this.errorToFailure(err);
|
|
1868
1938
|
}
|
|
@@ -1896,8 +1966,8 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1896
1966
|
try {
|
|
1897
1967
|
if (relayUrl) {
|
|
1898
1968
|
if (!this.connector.configure) {
|
|
1899
|
-
return (0,
|
|
1900
|
-
|
|
1969
|
+
return (0, import_hwk_adapter_core4.failure)(
|
|
1970
|
+
import_hwk_adapter_core4.HardwareErrorCode.MethodNotSupported,
|
|
1901
1971
|
"This Ledger connector does not support genuine-check relay configuration"
|
|
1902
1972
|
);
|
|
1903
1973
|
}
|
|
@@ -1906,18 +1976,18 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1906
1976
|
}
|
|
1907
1977
|
const result = await this.connectorCall(connectId, "getDeviceGenuineCheck", {});
|
|
1908
1978
|
if (!result.isGenuine) {
|
|
1909
|
-
return (0,
|
|
1979
|
+
return (0, import_hwk_adapter_core4.success)({
|
|
1910
1980
|
vendor: "ledger",
|
|
1911
1981
|
verified: false
|
|
1912
1982
|
});
|
|
1913
1983
|
}
|
|
1914
1984
|
if (!result.deviceId) {
|
|
1915
|
-
return (0,
|
|
1916
|
-
|
|
1985
|
+
return (0, import_hwk_adapter_core4.failure)(
|
|
1986
|
+
import_hwk_adapter_core4.HardwareErrorCode.UnknownError,
|
|
1917
1987
|
`Genuine check completed (isGenuine=${result.isGenuine}) but no deviceId was captured`
|
|
1918
1988
|
);
|
|
1919
1989
|
}
|
|
1920
|
-
return (0,
|
|
1990
|
+
return (0, import_hwk_adapter_core4.success)({
|
|
1921
1991
|
vendor: "ledger",
|
|
1922
1992
|
verified: result.isGenuine,
|
|
1923
1993
|
deviceId: result.deviceId
|
|
@@ -1951,14 +2021,14 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1951
2021
|
return false;
|
|
1952
2022
|
}
|
|
1953
2023
|
};
|
|
1954
|
-
if ((0,
|
|
2024
|
+
if ((0, import_hwk_adapter_core4.isHardwareOperationId)(connectId) && !namedOperationIsLive(connectId)) {
|
|
1955
2025
|
debugLog("[LedgerAdapter] cancel target already ended; nothing to cancel", {
|
|
1956
2026
|
connectId
|
|
1957
2027
|
});
|
|
1958
2028
|
return;
|
|
1959
2029
|
}
|
|
1960
2030
|
const userAbortReason = Object.assign(new Error("User aborted operation"), {
|
|
1961
|
-
code:
|
|
2031
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.UserAborted,
|
|
1962
2032
|
_tag: ERROR_TAG.UserAborted
|
|
1963
2033
|
});
|
|
1964
2034
|
this._lastCancelReason = userAbortReason;
|
|
@@ -1969,9 +2039,9 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1969
2039
|
}, 2e3);
|
|
1970
2040
|
const activeJobId = this._jobQueue.getActiveJob()?.deviceId;
|
|
1971
2041
|
let operationId;
|
|
1972
|
-
if ((0,
|
|
2042
|
+
if ((0, import_hwk_adapter_core4.isHardwareOperationId)(connectId)) {
|
|
1973
2043
|
operationId = connectId;
|
|
1974
|
-
} else if (!connectId && (0,
|
|
2044
|
+
} else if (!connectId && (0, import_hwk_adapter_core4.isHardwareOperationId)(activeJobId)) {
|
|
1975
2045
|
operationId = activeJobId;
|
|
1976
2046
|
}
|
|
1977
2047
|
let resolvedConnectId = connectId;
|
|
@@ -2007,17 +2077,27 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2007
2077
|
} else {
|
|
2008
2078
|
this._jobQueue.cancelActiveAndPending(void 0, userAbortReason);
|
|
2009
2079
|
}
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2080
|
+
const connectingConnectId = this._connectingConnectId;
|
|
2081
|
+
const targetKeys = new Set(
|
|
2082
|
+
[
|
|
2083
|
+
connectId,
|
|
2084
|
+
resolvedConnectId,
|
|
2085
|
+
...pendingOperationId ? this._operations.find(pendingOperationId)?.connectionKeys ?? [] : []
|
|
2086
|
+
].filter(Boolean)
|
|
2087
|
+
);
|
|
2088
|
+
const ownsPendingConnect = !connectId || activeJobId !== void 0 && queueKeys.has(activeJobId) || connectingConnectId !== void 0 && targetKeys.has(connectingConnectId);
|
|
2089
|
+
if (activeJobId === void 0 || ownsPendingConnect) {
|
|
2090
|
+
if (resolvedConnectId) {
|
|
2091
|
+
const sessionId = this._sessions.get(resolvedConnectId) ?? resolvedConnectId;
|
|
2092
|
+
void this.connector.cancel(sessionId);
|
|
2093
|
+
} else {
|
|
2094
|
+
for (const sid of this._sessions.values()) void this.connector.cancel(sid);
|
|
2095
|
+
}
|
|
2015
2096
|
}
|
|
2016
|
-
if (this._connectingPromise) {
|
|
2097
|
+
if (this._connectingPromise && ownsPendingConnect) {
|
|
2017
2098
|
this._doConnectAbortController?.abort(userAbortReason);
|
|
2018
2099
|
}
|
|
2019
|
-
|
|
2020
|
-
if (connectingConnectId) void this.connector.cancel(connectingConnectId);
|
|
2100
|
+
if (connectingConnectId && ownsPendingConnect) void this.connector.cancel(connectingConnectId);
|
|
2021
2101
|
}
|
|
2022
2102
|
// ---------------------------------------------------------------------------
|
|
2023
2103
|
// Chain fingerprint
|
|
@@ -2031,22 +2111,40 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2031
2111
|
);
|
|
2032
2112
|
if (deviceId) {
|
|
2033
2113
|
if (fingerprint !== deviceId) {
|
|
2034
|
-
return (0,
|
|
2035
|
-
|
|
2114
|
+
return (0, import_hwk_adapter_core4.failure)(
|
|
2115
|
+
import_hwk_adapter_core4.HardwareErrorCode.DeviceMismatch,
|
|
2036
2116
|
formatDeviceMismatchError(deviceId, fingerprint)
|
|
2037
2117
|
);
|
|
2038
2118
|
}
|
|
2039
2119
|
}
|
|
2040
|
-
return (0,
|
|
2120
|
+
return (0, import_hwk_adapter_core4.success)(fingerprint);
|
|
2041
2121
|
} catch (err) {
|
|
2042
2122
|
debugError("[LedgerAdapter] getChainFingerprint error:", chain, err);
|
|
2043
2123
|
return this.errorToFailure(err);
|
|
2044
2124
|
}
|
|
2045
2125
|
}
|
|
2126
|
+
/**
|
|
2127
|
+
* Fingerprints the first recorded chain whose app the device can open. Only a missing app
|
|
2128
|
+
* moves on to the next chain; when every app is missing, the first chain offers its install.
|
|
2129
|
+
*/
|
|
2130
|
+
async _fingerprintFirstOpenableChain(identities, fingerprintOf) {
|
|
2131
|
+
const [first] = identities;
|
|
2132
|
+
if (identities.length > 1) {
|
|
2133
|
+
for (const identity of identities) {
|
|
2134
|
+
try {
|
|
2135
|
+
return { identity, fingerprint: await fingerprintOf(identity.chain, false) };
|
|
2136
|
+
} catch (error) {
|
|
2137
|
+
const isAppMissing = isAppNotInstalledError(error) || error?.code === import_hwk_adapter_core4.HardwareErrorCode.AppNotInstalled;
|
|
2138
|
+
if (!isAppMissing) throw error;
|
|
2139
|
+
}
|
|
2140
|
+
}
|
|
2141
|
+
}
|
|
2142
|
+
return { identity: first, fingerprint: await fingerprintOf(first.chain, true) };
|
|
2143
|
+
}
|
|
2046
2144
|
/** Saves a picked BLE endpoint; `identity` is set when its wallet was just checked. */
|
|
2047
2145
|
async _saveSelectedBleBinding(connectId, selectionRequestId, context, operationId, signal, identity) {
|
|
2048
2146
|
try {
|
|
2049
|
-
const outcome = await (0,
|
|
2147
|
+
const outcome = await (0, import_hwk_adapter_core4.requestSaveDeviceBinding)(
|
|
2050
2148
|
this.emitter,
|
|
2051
2149
|
this._uiRegistry,
|
|
2052
2150
|
{
|
|
@@ -2115,7 +2213,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2115
2213
|
const result = await callMethod("btcGetMasterFingerprint", {});
|
|
2116
2214
|
return result.masterFingerprint;
|
|
2117
2215
|
}
|
|
2118
|
-
const path =
|
|
2216
|
+
const path = import_hwk_adapter_core4.CHAIN_FINGERPRINT_PATHS[chain];
|
|
2119
2217
|
let address;
|
|
2120
2218
|
if (chain === "evm") {
|
|
2121
2219
|
address = (await callMethod("evmGetAddress", { path, showOnDevice: false })).address.toLowerCase();
|
|
@@ -2128,7 +2226,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2128
2226
|
} else {
|
|
2129
2227
|
throw new Error(`Unsupported chain for fingerprint: ${chain}`);
|
|
2130
2228
|
}
|
|
2131
|
-
return (0,
|
|
2229
|
+
return (0, import_hwk_adapter_core4.deriveDeviceFingerprint)(address);
|
|
2132
2230
|
}
|
|
2133
2231
|
/**
|
|
2134
2232
|
* Operation owning the running job, so a mid-call UI request can name it.
|
|
@@ -2148,11 +2246,11 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2148
2246
|
}
|
|
2149
2247
|
const operationId = this._activeOperationId();
|
|
2150
2248
|
const waitPromise = this._uiRegistry.wait(
|
|
2151
|
-
|
|
2249
|
+
import_hwk_adapter_core4.UI_REQUEST.REQUEST_DEVICE_CONNECT,
|
|
2152
2250
|
{ operationId }
|
|
2153
2251
|
);
|
|
2154
|
-
this.emitter.emit(
|
|
2155
|
-
type:
|
|
2252
|
+
this.emitter.emit(import_hwk_adapter_core4.UI_REQUEST.REQUEST_DEVICE_CONNECT, {
|
|
2253
|
+
type: import_hwk_adapter_core4.UI_REQUEST.REQUEST_DEVICE_CONNECT,
|
|
2156
2254
|
payload: {
|
|
2157
2255
|
vendor: "ledger",
|
|
2158
2256
|
reason: "device-not-found",
|
|
@@ -2164,7 +2262,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2164
2262
|
try {
|
|
2165
2263
|
if (signal) {
|
|
2166
2264
|
const onAbort = () => {
|
|
2167
|
-
this._uiRegistry.cancel(
|
|
2265
|
+
this._uiRegistry.cancel(import_hwk_adapter_core4.UI_REQUEST.REQUEST_DEVICE_CONNECT);
|
|
2168
2266
|
};
|
|
2169
2267
|
signal.addEventListener("abort", onAbort, { once: true });
|
|
2170
2268
|
try {
|
|
@@ -2176,20 +2274,20 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2176
2274
|
payload = await waitPromise;
|
|
2177
2275
|
}
|
|
2178
2276
|
} catch (err) {
|
|
2179
|
-
this.emitter.emit(
|
|
2180
|
-
type:
|
|
2181
|
-
payload: {}
|
|
2277
|
+
this.emitter.emit(import_hwk_adapter_core4.UI_REQUEST.CLOSE_UI_WINDOW, {
|
|
2278
|
+
type: import_hwk_adapter_core4.UI_REQUEST.CLOSE_UI_WINDOW,
|
|
2279
|
+
payload: { operationId }
|
|
2182
2280
|
});
|
|
2183
2281
|
throw Object.assign(new Error("User cancelled Ledger connection"), {
|
|
2184
2282
|
_tag: ERROR_TAG.UserAborted,
|
|
2185
|
-
code:
|
|
2283
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.UserAborted,
|
|
2186
2284
|
cause: err
|
|
2187
2285
|
});
|
|
2188
2286
|
}
|
|
2189
2287
|
if (!payload?.confirmed) {
|
|
2190
2288
|
throw Object.assign(new Error("User cancelled Ledger connection"), {
|
|
2191
2289
|
_tag: ERROR_TAG.UserAborted,
|
|
2192
|
-
code:
|
|
2290
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.UserAborted
|
|
2193
2291
|
});
|
|
2194
2292
|
}
|
|
2195
2293
|
await new Promise((resolve) => {
|
|
@@ -2220,11 +2318,11 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2220
2318
|
async _waitForBtcHighIndexConfirm(path, accountIndex, signal) {
|
|
2221
2319
|
const operationId = this._activeOperationId();
|
|
2222
2320
|
const waitPromise = this._uiRegistry.wait(
|
|
2223
|
-
|
|
2321
|
+
import_hwk_adapter_core4.UI_REQUEST.REQUEST_BTC_HIGH_INDEX_CONFIRM,
|
|
2224
2322
|
{ operationId }
|
|
2225
2323
|
);
|
|
2226
|
-
this.emitter.emit(
|
|
2227
|
-
type:
|
|
2324
|
+
this.emitter.emit(import_hwk_adapter_core4.UI_REQUEST.REQUEST_BTC_HIGH_INDEX_CONFIRM, {
|
|
2325
|
+
type: import_hwk_adapter_core4.UI_REQUEST.REQUEST_BTC_HIGH_INDEX_CONFIRM,
|
|
2228
2326
|
payload: {
|
|
2229
2327
|
vendor: "ledger",
|
|
2230
2328
|
path,
|
|
@@ -2234,15 +2332,15 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2234
2332
|
});
|
|
2235
2333
|
try {
|
|
2236
2334
|
const payload = await this._awaitUiAnswer(
|
|
2237
|
-
|
|
2335
|
+
import_hwk_adapter_core4.UI_REQUEST.REQUEST_BTC_HIGH_INDEX_CONFIRM,
|
|
2238
2336
|
waitPromise,
|
|
2239
2337
|
signal
|
|
2240
2338
|
);
|
|
2241
2339
|
return !!payload?.confirmed;
|
|
2242
2340
|
} catch (err) {
|
|
2243
|
-
this.emitter.emit(
|
|
2244
|
-
type:
|
|
2245
|
-
payload: {}
|
|
2341
|
+
this.emitter.emit(import_hwk_adapter_core4.UI_REQUEST.CLOSE_UI_WINDOW, {
|
|
2342
|
+
type: import_hwk_adapter_core4.UI_REQUEST.CLOSE_UI_WINDOW,
|
|
2343
|
+
payload: { operationId }
|
|
2246
2344
|
});
|
|
2247
2345
|
return false;
|
|
2248
2346
|
}
|
|
@@ -2263,24 +2361,24 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2263
2361
|
async _waitForInstallAppConfirm(appName, signal) {
|
|
2264
2362
|
const operationId = this._activeOperationId();
|
|
2265
2363
|
const waitPromise = this._uiRegistry.wait(
|
|
2266
|
-
|
|
2364
|
+
import_hwk_adapter_core4.UI_REQUEST.REQUEST_INSTALL_APP,
|
|
2267
2365
|
{ operationId }
|
|
2268
2366
|
);
|
|
2269
|
-
this.emitter.emit(
|
|
2270
|
-
type:
|
|
2367
|
+
this.emitter.emit(import_hwk_adapter_core4.UI_REQUEST.REQUEST_INSTALL_APP, {
|
|
2368
|
+
type: import_hwk_adapter_core4.UI_REQUEST.REQUEST_INSTALL_APP,
|
|
2271
2369
|
payload: { vendor: "ledger", appName, operationId }
|
|
2272
2370
|
});
|
|
2273
2371
|
try {
|
|
2274
2372
|
const payload = await this._awaitUiAnswer(
|
|
2275
|
-
|
|
2373
|
+
import_hwk_adapter_core4.UI_REQUEST.REQUEST_INSTALL_APP,
|
|
2276
2374
|
waitPromise,
|
|
2277
2375
|
signal
|
|
2278
2376
|
);
|
|
2279
2377
|
return !!payload?.confirmed;
|
|
2280
2378
|
} catch (err) {
|
|
2281
|
-
this.emitter.emit(
|
|
2282
|
-
type:
|
|
2283
|
-
payload: {}
|
|
2379
|
+
this.emitter.emit(import_hwk_adapter_core4.UI_REQUEST.CLOSE_UI_WINDOW, {
|
|
2380
|
+
type: import_hwk_adapter_core4.UI_REQUEST.CLOSE_UI_WINDOW,
|
|
2381
|
+
payload: { operationId }
|
|
2284
2382
|
});
|
|
2285
2383
|
return false;
|
|
2286
2384
|
}
|
|
@@ -2291,9 +2389,9 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2291
2389
|
async ensureConnected(connectId, signal, allowUsbEphemeralFallback = false, preserveOperationId, context) {
|
|
2292
2390
|
if (signal.aborted) _LedgerAdapter._throwIfAborted(signal);
|
|
2293
2391
|
this._assertConnectorReady("connectDevice");
|
|
2294
|
-
if (this._isBleConnection() && !connectId && !allowUsbEphemeralFallback) {
|
|
2392
|
+
if (!this.connector.availableTransports?.includes("usb") && this._isBleConnection() && !connectId && !allowUsbEphemeralFallback) {
|
|
2295
2393
|
throw Object.assign(new Error("Ledger BLE connectId is required."), {
|
|
2296
|
-
code:
|
|
2394
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.DeviceNotFound
|
|
2297
2395
|
});
|
|
2298
2396
|
}
|
|
2299
2397
|
if (connectId && this._sessions.has(connectId)) return connectId;
|
|
@@ -2303,7 +2401,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2303
2401
|
new Error(
|
|
2304
2402
|
"Ledger USB session invariant violated: more than one session is active. Please reconnect the device."
|
|
2305
2403
|
),
|
|
2306
|
-
{ code:
|
|
2404
|
+
{ code: import_hwk_adapter_core4.HardwareErrorCode.DeviceOneDeviceOnly }
|
|
2307
2405
|
);
|
|
2308
2406
|
}
|
|
2309
2407
|
return this._sessions.keys().next().value;
|
|
@@ -2325,6 +2423,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2325
2423
|
if (this._doConnectAbortController === controller) {
|
|
2326
2424
|
this._connectingPromise = null;
|
|
2327
2425
|
this._doConnectAbortController = null;
|
|
2426
|
+
if (this._sessions.size === 0) this._activeConnectionType = void 0;
|
|
2328
2427
|
}
|
|
2329
2428
|
}
|
|
2330
2429
|
})();
|
|
@@ -2344,7 +2443,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2344
2443
|
(connection) => connection.transport === "usb"
|
|
2345
2444
|
);
|
|
2346
2445
|
const usbTarget = knownUsb?.transport === "usb" ? knownUsb.connectId : usbDevices.find((device) => device.connectId === targetConnectId)?.connectId;
|
|
2347
|
-
if (usbDevices.length > 0 && (usbTarget || !targetConnectId || allowUsbEphemeralFallback)) {
|
|
2446
|
+
if (usbDevices.length > 0 && (usbTarget || !targetConnectId || allowUsbEphemeralFallback || context?.takesPluggedUsb)) {
|
|
2348
2447
|
return this._connectFirstOrSelect(
|
|
2349
2448
|
usbDevices,
|
|
2350
2449
|
usbTarget,
|
|
@@ -2355,8 +2454,8 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2355
2454
|
);
|
|
2356
2455
|
}
|
|
2357
2456
|
if (context?.supportedTransports && !context.supportedTransports.includes("ble")) {
|
|
2358
|
-
throw (0,
|
|
2359
|
-
code:
|
|
2457
|
+
throw (0, import_hwk_adapter_core4.createHwkError)({
|
|
2458
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.DeviceNotFound,
|
|
2360
2459
|
message: "This Ledger model has no Bluetooth transport; connect it over USB"
|
|
2361
2460
|
});
|
|
2362
2461
|
}
|
|
@@ -2367,19 +2466,16 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2367
2466
|
if (knownBle?.transport === "ble") {
|
|
2368
2467
|
return this._connectDeviceOrThrow(knownBle.connectId, preserveOperationId, internalSignal);
|
|
2369
2468
|
}
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
message: "Ledger connection metadata is required before starting Bluetooth binding"
|
|
2374
|
-
});
|
|
2375
|
-
}
|
|
2469
|
+
const bleContext = context ?? {};
|
|
2470
|
+
bleContext.allowBleSelection = true;
|
|
2471
|
+
bleContext.usbFallbackConnectId = usbTarget ?? targetConnectId;
|
|
2376
2472
|
const bleDevices = await this._searchDevices({ transportType: "ble" }, internalSignal);
|
|
2377
2473
|
return this._connectFirstOrSelect(
|
|
2378
2474
|
bleDevices,
|
|
2379
2475
|
void 0,
|
|
2380
2476
|
allowUsbEphemeralFallback,
|
|
2381
2477
|
preserveOperationId,
|
|
2382
|
-
|
|
2478
|
+
bleContext,
|
|
2383
2479
|
internalSignal
|
|
2384
2480
|
);
|
|
2385
2481
|
}
|
|
@@ -2389,7 +2485,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2389
2485
|
let confirms = 0;
|
|
2390
2486
|
while (!internalSignal.aborted) {
|
|
2391
2487
|
this.emitter.emit("ui-event", {
|
|
2392
|
-
type:
|
|
2488
|
+
type: import_hwk_adapter_core4.EConnectorInteraction.Searching,
|
|
2393
2489
|
payload: { sessionId: "" }
|
|
2394
2490
|
});
|
|
2395
2491
|
let devices = await this._searchDevices(void 0, internalSignal);
|
|
@@ -2397,7 +2493,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2397
2493
|
if (devices.length === 0) {
|
|
2398
2494
|
for (let i = 0; i < 3 && !internalSignal.aborted; i += 1) {
|
|
2399
2495
|
await new Promise((resolve) => {
|
|
2400
|
-
setTimeout(resolve,
|
|
2496
|
+
setTimeout(resolve, import_hwk_adapter_core4.DEVICE_CONNECT_RETRY_DELAY_MS);
|
|
2401
2497
|
});
|
|
2402
2498
|
_LedgerAdapter._throwIfAborted(internalSignal);
|
|
2403
2499
|
devices = await this._searchDevices(void 0, internalSignal);
|
|
@@ -2433,7 +2529,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2433
2529
|
new Error(
|
|
2434
2530
|
"Device not connected after multiple attempts. Please ensure your Ledger is awake, unlocked, and in range, then try again."
|
|
2435
2531
|
),
|
|
2436
|
-
{ code:
|
|
2532
|
+
{ code: import_hwk_adapter_core4.HardwareErrorCode.DeviceNotFound }
|
|
2437
2533
|
);
|
|
2438
2534
|
}
|
|
2439
2535
|
await this._waitForDeviceConnect(internalSignal);
|
|
@@ -2459,7 +2555,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2459
2555
|
}
|
|
2460
2556
|
if (!this._isBleConnection() || !allowUsbEphemeralFallback) {
|
|
2461
2557
|
const err = Object.assign(new Error(`Target Ledger unavailable: ${targetConnectId}`), {
|
|
2462
|
-
code:
|
|
2558
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.DeviceNotFound
|
|
2463
2559
|
});
|
|
2464
2560
|
if (this._isBleConnection()) {
|
|
2465
2561
|
err._tag = ERROR_TAG.DeviceNotAdvertising;
|
|
@@ -2468,9 +2564,9 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2468
2564
|
}
|
|
2469
2565
|
}
|
|
2470
2566
|
const requiresBleSelection = this._isBleConnection();
|
|
2471
|
-
if (requiresBleSelection && !allowUsbEphemeralFallback) {
|
|
2567
|
+
if (requiresBleSelection && !allowUsbEphemeralFallback && !context?.allowBleSelection) {
|
|
2472
2568
|
throw Object.assign(new Error("Ledger BLE connectId is required."), {
|
|
2473
|
-
code:
|
|
2569
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.DeviceNotFound
|
|
2474
2570
|
});
|
|
2475
2571
|
}
|
|
2476
2572
|
if (requiresBleSelection && context?.allowDeviceSelection !== false) {
|
|
@@ -2480,8 +2576,9 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2480
2576
|
const knownUsb = context?.knownConnections?.find(
|
|
2481
2577
|
(connection) => connection.transport === "usb"
|
|
2482
2578
|
);
|
|
2483
|
-
const usbConnectId = knownUsb?.transport === "usb" ? knownUsb.connectId : targetConnectId;
|
|
2484
|
-
const
|
|
2579
|
+
const usbConnectId = knownUsb?.transport === "usb" ? knownUsb.connectId : context?.usbFallbackConnectId ?? targetConnectId;
|
|
2580
|
+
const maySelectAnyUsb = allowUsbEphemeralFallback || !usbConnectId && !context?.knownConnections?.some((connection) => connection.transport !== "qr");
|
|
2581
|
+
const { device, requestId } = await (0, import_hwk_adapter_core4.requestBleDeviceSelection)({
|
|
2485
2582
|
emitter: this.emitter,
|
|
2486
2583
|
registry: this._uiRegistry,
|
|
2487
2584
|
signal,
|
|
@@ -2491,7 +2588,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2491
2588
|
const usbDevices = await this._searchDevices({ transportType: "usb" }, signal);
|
|
2492
2589
|
const candidate = usbDevices.find(
|
|
2493
2590
|
(device2) => device2.connectionType === "usb" && device2.connectId === usbConnectId
|
|
2494
|
-
) ?? (usbDevices.length === 1 && usbDevices[0].connectionType === "usb" ? usbDevices[0] : void 0);
|
|
2591
|
+
) ?? (maySelectAnyUsb && usbDevices.length === 1 && usbDevices[0].connectionType === "usb" ? usbDevices[0] : void 0);
|
|
2495
2592
|
if (candidate) return [candidate];
|
|
2496
2593
|
}
|
|
2497
2594
|
return this._searchDevices({ transportType: "ble", waitForAllTransports: true }, signal);
|
|
@@ -2536,20 +2633,20 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2536
2633
|
return connected;
|
|
2537
2634
|
}
|
|
2538
2635
|
if (devices.length > 0 && (devices.length > 1 || requiresBleSelection)) {
|
|
2539
|
-
if (context?.allowDeviceSelection === false || !this.emitter.listenerCount(
|
|
2540
|
-
throw (0,
|
|
2541
|
-
code:
|
|
2636
|
+
if (context?.allowDeviceSelection === false || !this.emitter.listenerCount(import_hwk_adapter_core4.UI_REQUEST.REQUEST_SELECT_DEVICE)) {
|
|
2637
|
+
throw (0, import_hwk_adapter_core4.createHwkError)({
|
|
2638
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.DeviceNotFound,
|
|
2542
2639
|
message: "Select a Ledger device before continuing"
|
|
2543
2640
|
});
|
|
2544
2641
|
}
|
|
2545
2642
|
const requestId = this._uiRegistry.createRequestId();
|
|
2546
2643
|
const operationId = this._activeOperationId();
|
|
2547
2644
|
const waitPromise = this._uiRegistry.wait(
|
|
2548
|
-
|
|
2645
|
+
import_hwk_adapter_core4.UI_REQUEST.REQUEST_SELECT_DEVICE,
|
|
2549
2646
|
{ requestId, operationId }
|
|
2550
2647
|
);
|
|
2551
|
-
this.emitter.emit(
|
|
2552
|
-
type:
|
|
2648
|
+
this.emitter.emit(import_hwk_adapter_core4.UI_REQUEST.REQUEST_SELECT_DEVICE, {
|
|
2649
|
+
type: import_hwk_adapter_core4.UI_REQUEST.REQUEST_SELECT_DEVICE,
|
|
2553
2650
|
payload: {
|
|
2554
2651
|
devices,
|
|
2555
2652
|
requestId,
|
|
@@ -2567,7 +2664,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2567
2664
|
const selected = devices.find((device) => device.connectId === sdkConnectId);
|
|
2568
2665
|
if (!selected) {
|
|
2569
2666
|
throw Object.assign(new Error("Selected Ledger is no longer available"), {
|
|
2570
|
-
code:
|
|
2667
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.DeviceNotFound
|
|
2571
2668
|
});
|
|
2572
2669
|
}
|
|
2573
2670
|
if (context && requiresBleSelection)
|
|
@@ -2576,7 +2673,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2576
2673
|
}
|
|
2577
2674
|
if (devices.length !== 1) {
|
|
2578
2675
|
throw Object.assign(new Error("Ledger device not found."), {
|
|
2579
|
-
code:
|
|
2676
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.DeviceNotFound
|
|
2580
2677
|
});
|
|
2581
2678
|
}
|
|
2582
2679
|
return this._connectDeviceOrThrow(devices[0].connectId, preserveOperationId, signal);
|
|
@@ -2606,7 +2703,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2606
2703
|
*/
|
|
2607
2704
|
_unwrapConnectorResult(result) {
|
|
2608
2705
|
if (result.success) return result.payload;
|
|
2609
|
-
throw (0,
|
|
2706
|
+
throw (0, import_hwk_adapter_core4.rehydrateConnectorError)(result.error);
|
|
2610
2707
|
}
|
|
2611
2708
|
/**
|
|
2612
2709
|
* `connector.call` + result unwrap, optionally raced against an abort
|
|
@@ -2681,10 +2778,10 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2681
2778
|
};
|
|
2682
2779
|
}
|
|
2683
2780
|
async connectorCall(connectId, method, params, fingerprint, permissionDeviceId, commonParams, installContext) {
|
|
2684
|
-
const positionalOperationId = (0,
|
|
2781
|
+
const positionalOperationId = (0, import_hwk_adapter_core4.isHardwareOperationId)(connectId) ? connectId : void 0;
|
|
2685
2782
|
if (positionalOperationId && commonParams?.operationId && positionalOperationId !== commonParams.operationId) {
|
|
2686
|
-
throw (0,
|
|
2687
|
-
code:
|
|
2783
|
+
throw (0, import_hwk_adapter_core4.createHwkError)({
|
|
2784
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.InvalidParams,
|
|
2688
2785
|
message: "Conflicting Ledger operation ids",
|
|
2689
2786
|
params: {
|
|
2690
2787
|
positionalOperationId,
|
|
@@ -2699,7 +2796,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2699
2796
|
debugLog("[LedgerAdapter][REQ]", {
|
|
2700
2797
|
method,
|
|
2701
2798
|
connectId: effectiveConnectId || "(empty)",
|
|
2702
|
-
params:
|
|
2799
|
+
params: summarizeForLog(params, { method })
|
|
2703
2800
|
});
|
|
2704
2801
|
const queueKey = ledgerQueueKey({ operationId, connectId: effectiveConnectId });
|
|
2705
2802
|
try {
|
|
@@ -2752,7 +2849,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2752
2849
|
debugLog("[LedgerAdapter][RES]", {
|
|
2753
2850
|
method,
|
|
2754
2851
|
success: true,
|
|
2755
|
-
payload:
|
|
2852
|
+
payload: summarizeForLog(result, { method, role: "result" })
|
|
2756
2853
|
});
|
|
2757
2854
|
return result;
|
|
2758
2855
|
} catch (err) {
|
|
@@ -2811,7 +2908,8 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2811
2908
|
/** Actual work done under the job queue — connection, fingerprint, call, and recovery. */
|
|
2812
2909
|
async _runConnectorCall(connectId, method, params, signal, fingerprint, permissionDeviceId, commonParams, installContext, operationId, lockedRetryBudget = _LedgerAdapter.MAX_BUSINESS_RETRY_BUDGET) {
|
|
2813
2910
|
_LedgerAdapter._throwIfAborted(signal);
|
|
2814
|
-
|
|
2911
|
+
const resolvesAppManagementTransport = APP_MANAGEMENT_METHODS.has(method) && this.connector.availableTransports?.includes("usb") && this.connector.availableTransports.includes("ble");
|
|
2912
|
+
if (!operationId && !resolvesAppManagementTransport) {
|
|
2815
2913
|
await this._ensureDevicePermission(
|
|
2816
2914
|
connectId,
|
|
2817
2915
|
permissionDeviceId ?? fingerprint?.deviceId,
|
|
@@ -2825,7 +2923,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2825
2923
|
if (gatedParams === null) {
|
|
2826
2924
|
throw Object.assign(new Error("User cancelled BTC high-index confirmation"), {
|
|
2827
2925
|
_tag: ERROR_TAG.UserAborted,
|
|
2828
|
-
code:
|
|
2926
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.UserAborted
|
|
2829
2927
|
});
|
|
2830
2928
|
}
|
|
2831
2929
|
effectiveParams = gatedParams;
|
|
@@ -2844,12 +2942,13 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2844
2942
|
);
|
|
2845
2943
|
const connectionAttempt = {
|
|
2846
2944
|
...commonParams,
|
|
2847
|
-
deferBindingSave: checksWallet
|
|
2945
|
+
deferBindingSave: checksWallet,
|
|
2946
|
+
...APP_MANAGEMENT_METHODS.has(method) ? { takesPluggedUsb: true } : {}
|
|
2848
2947
|
};
|
|
2849
2948
|
const bundleConnection = installContext?.connection;
|
|
2850
2949
|
if (bundleConnection && this._sessions.get(bundleConnection.connectId) !== bundleConnection.sessionId) {
|
|
2851
|
-
throw (0,
|
|
2852
|
-
code:
|
|
2950
|
+
throw (0, import_hwk_adapter_core4.createHwkError)({
|
|
2951
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.DeviceDisconnected,
|
|
2853
2952
|
message: "Ledger all-network connection ended"
|
|
2854
2953
|
});
|
|
2855
2954
|
}
|
|
@@ -2862,8 +2961,8 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2862
2961
|
);
|
|
2863
2962
|
const walletCheck = installContext?.walletCheck;
|
|
2864
2963
|
if (walletCheck && !checksWallet && (walletCheck.verified === false || walletCheck.expected && !walletCheck.verified)) {
|
|
2865
|
-
throw (0,
|
|
2866
|
-
code:
|
|
2964
|
+
throw (0, import_hwk_adapter_core4.createHwkError)({
|
|
2965
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.DeviceMismatch,
|
|
2867
2966
|
message: "Ledger wallet is not verified on this connection; refusing to derive an unverifiable address"
|
|
2868
2967
|
});
|
|
2869
2968
|
}
|
|
@@ -2874,8 +2973,8 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2874
2973
|
if (!sessionId) {
|
|
2875
2974
|
if (operationId) {
|
|
2876
2975
|
this._operations.end(operationId, "disconnect");
|
|
2877
|
-
throw (0,
|
|
2878
|
-
code:
|
|
2976
|
+
throw (0, import_hwk_adapter_core4.createHwkError)({
|
|
2977
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.OperationEnded,
|
|
2879
2978
|
message: "Ledger operation connection is no longer active",
|
|
2880
2979
|
params: { operationId, reason: "disconnect" }
|
|
2881
2980
|
});
|
|
@@ -2884,6 +2983,16 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2884
2983
|
_tag: ERROR_TAG.DeviceSessionNotFound
|
|
2885
2984
|
});
|
|
2886
2985
|
}
|
|
2986
|
+
if (!operationId && resolvesAppManagementTransport) {
|
|
2987
|
+
const transport = this._sessionTransports.get(sessionId);
|
|
2988
|
+
await this._ensureDevicePermission(
|
|
2989
|
+
resolvedConnectId,
|
|
2990
|
+
permissionDeviceId ?? fingerprint?.deviceId,
|
|
2991
|
+
signal,
|
|
2992
|
+
transport && (isLedgerBleConnectionType(transport) ? "ble" : "hid")
|
|
2993
|
+
);
|
|
2994
|
+
_LedgerAdapter._throwIfAborted(signal);
|
|
2995
|
+
}
|
|
2887
2996
|
try {
|
|
2888
2997
|
const isBleSession = this._sessionTransports.get(sessionId) === "ble";
|
|
2889
2998
|
if (!operationId && connectionAttempt.pendingBinding) {
|
|
@@ -2911,7 +3020,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2911
3020
|
await this._dropSession(resolvedConnectId, signal);
|
|
2912
3021
|
}
|
|
2913
3022
|
throw Object.assign(new Error(formatDeviceMismatchError(fp.expected, fp.actual)), {
|
|
2914
|
-
code:
|
|
3023
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.DeviceMismatch
|
|
2915
3024
|
});
|
|
2916
3025
|
}
|
|
2917
3026
|
if (walletCheck) walletCheck.verified = true;
|
|
@@ -2954,12 +3063,12 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2954
3063
|
this._sessions.delete(resolvedConnectId);
|
|
2955
3064
|
await this.connector.disconnect(sessionId).catch(() => void 0);
|
|
2956
3065
|
}
|
|
2957
|
-
const ambiguous = businessCallStarted && !(0,
|
|
3066
|
+
const ambiguous = businessCallStarted && !(0, import_hwk_adapter_core4.canReplayHardwareMethodAfterTransportFailure)(method);
|
|
2958
3067
|
const interactionParams = operationId ? { operationId, reason: "disconnect" } : void 0;
|
|
2959
|
-
throw (0,
|
|
2960
|
-
code: operationId ?
|
|
3068
|
+
throw (0, import_hwk_adapter_core4.createHwkError)({
|
|
3069
|
+
code: operationId ? import_hwk_adapter_core4.HardwareErrorCode.OperationEnded : mapLedgerError(err).code,
|
|
2961
3070
|
message: "Ledger operation connection was lost; start a new operation",
|
|
2962
|
-
params: ambiguous ? (0,
|
|
3071
|
+
params: ambiguous ? (0, import_hwk_adapter_core4.operationMayHaveCompletedParams)(method, { operationId }) : interactionParams,
|
|
2963
3072
|
recovery: ambiguous ? { scope: "unknown" } : void 0
|
|
2964
3073
|
});
|
|
2965
3074
|
}
|
|
@@ -2976,12 +3085,12 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
2976
3085
|
});
|
|
2977
3086
|
const assertSessionCurrent = (message) => {
|
|
2978
3087
|
if (this._sessions.get(resolvedConnectId) === sessionId) return;
|
|
2979
|
-
throw (0,
|
|
2980
|
-
code: operationId ?
|
|
3088
|
+
throw (0, import_hwk_adapter_core4.createHwkError)({
|
|
3089
|
+
code: operationId ? import_hwk_adapter_core4.HardwareErrorCode.OperationEnded : import_hwk_adapter_core4.HardwareErrorCode.DeviceDisconnected,
|
|
2981
3090
|
message
|
|
2982
3091
|
});
|
|
2983
3092
|
};
|
|
2984
|
-
if ((isDeviceLockedError(err) || errObj?.code ===
|
|
3093
|
+
if ((isDeviceLockedError(err) || errObj?.code === import_hwk_adapter_core4.HardwareErrorCode.DeviceLocked) && lockedRetryBudget > 0) {
|
|
2985
3094
|
await this._waitForDeviceConnect(signal);
|
|
2986
3095
|
assertSessionCurrent("Ledger connection ended while waiting for unlock");
|
|
2987
3096
|
if (walletCheck?.derived) walletCheck.verified = false;
|
|
@@ -3014,18 +3123,18 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
3014
3123
|
this._discoveredDevices.delete(resolvedConnectId);
|
|
3015
3124
|
await this.connector.disconnect(sessionId).catch(() => void 0);
|
|
3016
3125
|
}
|
|
3017
|
-
const ambiguous = !(0,
|
|
3018
|
-
throw (0,
|
|
3019
|
-
code: operationId ?
|
|
3126
|
+
const ambiguous = !(0, import_hwk_adapter_core4.canReplayHardwareMethodAfterTransportFailure)(method);
|
|
3127
|
+
throw (0, import_hwk_adapter_core4.createHwkError)({
|
|
3128
|
+
code: operationId ? import_hwk_adapter_core4.HardwareErrorCode.OperationEnded : mapLedgerError(retryErr).code,
|
|
3020
3129
|
message: `Ledger ${method} may have completed before the connection was lost`,
|
|
3021
|
-
params: ambiguous ? (0,
|
|
3130
|
+
params: ambiguous ? (0, import_hwk_adapter_core4.operationMayHaveCompletedParams)(method, { operationId, reason: "disconnect" }) : { operationId, reason: "disconnect" },
|
|
3022
3131
|
recovery: ambiguous ? { scope: "unknown" } : void 0
|
|
3023
3132
|
});
|
|
3024
3133
|
}
|
|
3025
3134
|
throw retryErr;
|
|
3026
3135
|
}
|
|
3027
3136
|
}
|
|
3028
|
-
if (!operationId && errObj?.code ===
|
|
3137
|
+
if (!operationId && errObj?.code === import_hwk_adapter_core4.HardwareErrorCode.DeviceMismatch) {
|
|
3029
3138
|
this._sessions.delete(resolvedConnectId);
|
|
3030
3139
|
this._sessionTransports.delete(sessionId);
|
|
3031
3140
|
this._discoveredDevices.delete(resolvedConnectId);
|
|
@@ -3033,7 +3142,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
3033
3142
|
throw err;
|
|
3034
3143
|
}
|
|
3035
3144
|
const autoInstallApp = commonParams?.autoInstallApp ?? this._defaultAutoInstallApp;
|
|
3036
|
-
const isAppMissing = isAppNotInstalledError(err) || err?.code ===
|
|
3145
|
+
const isAppMissing = isAppNotInstalledError(err) || err?.code === import_hwk_adapter_core4.HardwareErrorCode.AppNotInstalled;
|
|
3037
3146
|
if (autoInstallApp && isAppMissing) {
|
|
3038
3147
|
if (installContext?.deviceOutOfMemoryError) {
|
|
3039
3148
|
throw installContext.deviceOutOfMemoryError;
|
|
@@ -3041,8 +3150,8 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
3041
3150
|
const appName = err?.appName ?? mapLedgerError(err).appName;
|
|
3042
3151
|
if (appName) {
|
|
3043
3152
|
if (installContext?.installAttemptedAppNames?.has(appName)) {
|
|
3044
|
-
throw (0,
|
|
3045
|
-
code:
|
|
3153
|
+
throw (0, import_hwk_adapter_core4.createHwkError)({
|
|
3154
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.AppNotInstalled,
|
|
3046
3155
|
message: `${appName} install reported success but the app is still missing on device`,
|
|
3047
3156
|
_tag: ERROR_TAG.AppInstallVerifyFailed,
|
|
3048
3157
|
appName
|
|
@@ -3050,21 +3159,21 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
3050
3159
|
}
|
|
3051
3160
|
const confirmed = await this._waitForInstallAppConfirm(appName, signal);
|
|
3052
3161
|
if (!confirmed) {
|
|
3053
|
-
throw (0,
|
|
3054
|
-
code:
|
|
3162
|
+
throw (0, import_hwk_adapter_core4.createHwkError)({
|
|
3163
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.UserAborted,
|
|
3055
3164
|
message: `User declined to install ${appName}`,
|
|
3056
3165
|
_tag: ERROR_TAG.UserAborted,
|
|
3057
3166
|
appName
|
|
3058
3167
|
});
|
|
3059
3168
|
}
|
|
3060
3169
|
this.emitter.emit("ui-event", {
|
|
3061
|
-
type:
|
|
3170
|
+
type: import_hwk_adapter_core4.EConnectorInteraction.AppInstallProgress,
|
|
3062
3171
|
payload: { connectId: resolvedConnectId, appName, progress: 0 }
|
|
3063
3172
|
});
|
|
3064
3173
|
try {
|
|
3065
3174
|
await this._callConnector(sessionId, "installApp", { appName }, signal);
|
|
3066
3175
|
} catch (installErr) {
|
|
3067
|
-
if (mapLedgerError(installErr).code ===
|
|
3176
|
+
if (mapLedgerError(installErr).code === import_hwk_adapter_core4.HardwareErrorCode.DeviceOutOfMemory) {
|
|
3068
3177
|
if (installContext) {
|
|
3069
3178
|
installContext.deviceOutOfMemoryError = installErr;
|
|
3070
3179
|
}
|
|
@@ -3075,9 +3184,9 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
3075
3184
|
installContext.installAttemptedAppNames = installContext.installAttemptedAppNames ?? /* @__PURE__ */ new Set();
|
|
3076
3185
|
installContext.installAttemptedAppNames.add(appName);
|
|
3077
3186
|
}
|
|
3078
|
-
this.emitter.emit(
|
|
3079
|
-
type:
|
|
3080
|
-
payload: {}
|
|
3187
|
+
this.emitter.emit(import_hwk_adapter_core4.UI_REQUEST.CLOSE_UI_WINDOW, {
|
|
3188
|
+
type: import_hwk_adapter_core4.UI_REQUEST.CLOSE_UI_WINDOW,
|
|
3189
|
+
payload: { operationId }
|
|
3081
3190
|
});
|
|
3082
3191
|
return await this._runConnectorCall(
|
|
3083
3192
|
resolvedConnectId,
|
|
@@ -3116,23 +3225,23 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
3116
3225
|
* Ensure OS-level Bluetooth/USB permission; times out after 60s if the host never
|
|
3117
3226
|
* replies. Without connectId the request is environment-level, else device-level.
|
|
3118
3227
|
*/
|
|
3119
|
-
async _ensureDevicePermission(connectId, deviceId, signal) {
|
|
3228
|
+
async _ensureDevicePermission(connectId, deviceId, signal, transportOverride) {
|
|
3120
3229
|
if (signal?.aborted) {
|
|
3121
3230
|
_LedgerAdapter._throwIfAborted(signal);
|
|
3122
3231
|
}
|
|
3123
|
-
const transportType = this.activeTransport ?? "hid";
|
|
3232
|
+
const transportType = transportOverride ?? this.activeTransport ?? "hid";
|
|
3124
3233
|
const operationId = this._activeOperationId();
|
|
3125
3234
|
const waitPromise = this._uiRegistry.wait(
|
|
3126
|
-
|
|
3235
|
+
import_hwk_adapter_core4.UI_REQUEST.REQUEST_DEVICE_PERMISSION,
|
|
3127
3236
|
{ timeoutMs: 6e4, operationId }
|
|
3128
3237
|
);
|
|
3129
|
-
this.emitter.emit(
|
|
3130
|
-
type:
|
|
3238
|
+
this.emitter.emit(import_hwk_adapter_core4.UI_REQUEST.REQUEST_DEVICE_PERMISSION, {
|
|
3239
|
+
type: import_hwk_adapter_core4.UI_REQUEST.REQUEST_DEVICE_PERMISSION,
|
|
3131
3240
|
payload: { transportType, connectId, deviceId, operationId }
|
|
3132
3241
|
});
|
|
3133
3242
|
let response;
|
|
3134
3243
|
const onAbort = () => {
|
|
3135
|
-
this._uiRegistry.cancel(
|
|
3244
|
+
this._uiRegistry.cancel(import_hwk_adapter_core4.UI_REQUEST.REQUEST_DEVICE_PERMISSION);
|
|
3136
3245
|
};
|
|
3137
3246
|
try {
|
|
3138
3247
|
signal?.addEventListener("abort", onAbort, { once: true });
|
|
@@ -3141,14 +3250,14 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
3141
3250
|
if (signal?.aborted) {
|
|
3142
3251
|
_LedgerAdapter._throwIfAborted(signal);
|
|
3143
3252
|
}
|
|
3144
|
-
if (err?._tag ===
|
|
3145
|
-
this.emitter.emit(
|
|
3146
|
-
type:
|
|
3147
|
-
payload: {}
|
|
3253
|
+
if (err?._tag === import_hwk_adapter_core4.UI_REQUEST_PREEMPTED_TAG) {
|
|
3254
|
+
this.emitter.emit(import_hwk_adapter_core4.UI_REQUEST.CLOSE_UI_WINDOW, {
|
|
3255
|
+
type: import_hwk_adapter_core4.UI_REQUEST.CLOSE_UI_WINDOW,
|
|
3256
|
+
payload: { operationId }
|
|
3148
3257
|
});
|
|
3149
3258
|
throw Object.assign(new Error("Device permission request superseded"), {
|
|
3150
3259
|
_tag: ERROR_TAG.UserAborted,
|
|
3151
|
-
code:
|
|
3260
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.UserAborted,
|
|
3152
3261
|
cause: err
|
|
3153
3262
|
});
|
|
3154
3263
|
}
|
|
@@ -3159,7 +3268,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
3159
3268
|
const { granted, reason, message } = response;
|
|
3160
3269
|
if (!granted) {
|
|
3161
3270
|
throw Object.assign(new Error(message ?? "Device permission denied"), {
|
|
3162
|
-
code:
|
|
3271
|
+
code: import_hwk_adapter_core4.HardwareErrorCode.DevicePermissionDenied,
|
|
3163
3272
|
reason
|
|
3164
3273
|
});
|
|
3165
3274
|
}
|
|
@@ -3173,7 +3282,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
3173
3282
|
const tag = err && typeof err === "object" ? err._tag : void 0;
|
|
3174
3283
|
if (err && typeof err === "object" && "code" in err && typeof err.code === "number") {
|
|
3175
3284
|
const e = err;
|
|
3176
|
-
const params = e.code ===
|
|
3285
|
+
const params = e.code === import_hwk_adapter_core4.HardwareErrorCode.DevicePermissionDenied && e.reason ? { permissionDeniedReason: e.reason } : e.params;
|
|
3177
3286
|
return ledgerFailure(
|
|
3178
3287
|
e.code,
|
|
3179
3288
|
e.message ?? "Unknown error",
|
|
@@ -3181,7 +3290,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
3181
3290
|
tag,
|
|
3182
3291
|
params,
|
|
3183
3292
|
void 0,
|
|
3184
|
-
(0,
|
|
3293
|
+
(0, import_hwk_adapter_core4.isHwkRecoveryHint)(e.recovery) ? e.recovery : void 0
|
|
3185
3294
|
);
|
|
3186
3295
|
}
|
|
3187
3296
|
const mapped = mapLedgerError(err);
|
|
@@ -3234,7 +3343,7 @@ _LedgerAdapter.APP_INSTALL_PROGRESS_MIN_DELTA = 0.05;
|
|
|
3234
3343
|
var LedgerAdapter = _LedgerAdapter;
|
|
3235
3344
|
|
|
3236
3345
|
// src/connector/LedgerConnectorBase.ts
|
|
3237
|
-
var
|
|
3346
|
+
var import_hwk_adapter_core13 = require("@onekeyfe/hwk-adapter-core");
|
|
3238
3347
|
|
|
3239
3348
|
// src/device/LedgerDeviceManager.ts
|
|
3240
3349
|
var LedgerDeviceManager = class {
|
|
@@ -3507,17 +3616,20 @@ var import_device_signer_kit_ethereum = require("@ledgerhq/device-signer-kit-eth
|
|
|
3507
3616
|
var import_context_module = require("@ledgerhq/context-module");
|
|
3508
3617
|
|
|
3509
3618
|
// src/signer/SignerEth.ts
|
|
3510
|
-
var
|
|
3619
|
+
var import_hwk_adapter_core5 = require("@onekeyfe/hwk-adapter-core");
|
|
3511
3620
|
|
|
3512
3621
|
// src/signer/deviceActionToPromise.ts
|
|
3513
3622
|
var import_device_management_kit = require("@ledgerhq/device-management-kit");
|
|
3514
3623
|
var IDLE_WATCHDOG_MS = 65e3;
|
|
3515
3624
|
var INTERACTIVE_TIMEOUT_MS = 5 * 6e4;
|
|
3625
|
+
var INTERACTION_PROMPT_DELAY_MS = 500;
|
|
3516
3626
|
function deviceActionToPromise(action, onInteraction, timeoutMs = IDLE_WATCHDOG_MS, onRegisterCanceller, onIntermediate) {
|
|
3517
3627
|
return new Promise((resolve, reject) => {
|
|
3518
3628
|
let settled = false;
|
|
3519
3629
|
let lastStep;
|
|
3520
3630
|
let activeInteraction;
|
|
3631
|
+
let pendingInteraction;
|
|
3632
|
+
let pendingInteractionTimer = null;
|
|
3521
3633
|
const observedSteps = [];
|
|
3522
3634
|
let sub;
|
|
3523
3635
|
let timer = null;
|
|
@@ -3545,31 +3657,52 @@ function deviceActionToPromise(action, onInteraction, timeoutMs = IDLE_WATCHDOG_
|
|
|
3545
3657
|
}
|
|
3546
3658
|
return false;
|
|
3547
3659
|
};
|
|
3660
|
+
const cancelPendingInteraction = () => {
|
|
3661
|
+
if (pendingInteractionTimer) clearTimeout(pendingInteractionTimer);
|
|
3662
|
+
pendingInteractionTimer = null;
|
|
3663
|
+
pendingInteraction = void 0;
|
|
3664
|
+
};
|
|
3665
|
+
const showInteraction = (interactionName) => {
|
|
3666
|
+
activeInteraction = interactionName;
|
|
3667
|
+
debugLog(
|
|
3668
|
+
"[DeviceAction] requiredUserInteraction",
|
|
3669
|
+
`interaction=${interactionName}`,
|
|
3670
|
+
`step=${lastStep ?? "unknown"}`
|
|
3671
|
+
);
|
|
3672
|
+
if (interactionName === "unlock-device" && timer) {
|
|
3673
|
+
clearTimeout(timer);
|
|
3674
|
+
timer = null;
|
|
3675
|
+
}
|
|
3676
|
+
onInteraction?.(interactionName);
|
|
3677
|
+
};
|
|
3548
3678
|
const emitInteraction = (interaction) => {
|
|
3549
3679
|
if (!onInteraction) {
|
|
3550
3680
|
return;
|
|
3551
3681
|
}
|
|
3552
3682
|
if (!interaction || interaction === "none") {
|
|
3683
|
+
cancelPendingInteraction();
|
|
3553
3684
|
clearActiveInteraction();
|
|
3554
3685
|
return;
|
|
3555
3686
|
}
|
|
3556
3687
|
const interactionName = String(interaction);
|
|
3557
|
-
if (interactionName === activeInteraction) {
|
|
3688
|
+
if (interactionName === activeInteraction || interactionName === pendingInteraction) {
|
|
3558
3689
|
return;
|
|
3559
3690
|
}
|
|
3560
|
-
|
|
3561
|
-
|
|
3562
|
-
|
|
3563
|
-
|
|
3564
|
-
|
|
3565
|
-
);
|
|
3566
|
-
if (interaction === "unlock-device" && timer) {
|
|
3567
|
-
clearTimeout(timer);
|
|
3568
|
-
timer = null;
|
|
3691
|
+
cancelPendingInteraction();
|
|
3692
|
+
clearActiveInteraction();
|
|
3693
|
+
if (interactionName === "unlock-device") {
|
|
3694
|
+
showInteraction(interactionName);
|
|
3695
|
+
return;
|
|
3569
3696
|
}
|
|
3570
|
-
|
|
3697
|
+
pendingInteraction = interactionName;
|
|
3698
|
+
pendingInteractionTimer = setTimeout(() => {
|
|
3699
|
+
pendingInteractionTimer = null;
|
|
3700
|
+
pendingInteraction = void 0;
|
|
3701
|
+
if (!settled) showInteraction(interactionName);
|
|
3702
|
+
}, INTERACTION_PROMPT_DELAY_MS);
|
|
3571
3703
|
};
|
|
3572
3704
|
const completeInteraction = () => {
|
|
3705
|
+
cancelPendingInteraction();
|
|
3573
3706
|
if (!clearActiveInteraction()) {
|
|
3574
3707
|
onInteraction?.("interaction-complete");
|
|
3575
3708
|
}
|
|
@@ -3720,7 +3853,7 @@ var SignerEth = class {
|
|
|
3720
3853
|
);
|
|
3721
3854
|
}
|
|
3722
3855
|
async signTransaction(derivationPath, serializedTxHex) {
|
|
3723
|
-
const action = this._sdk.signTransaction(derivationPath, (0,
|
|
3856
|
+
const action = this._sdk.signTransaction(derivationPath, (0, import_hwk_adapter_core5.hexToBytes)(serializedTxHex));
|
|
3724
3857
|
return deviceActionToPromise(
|
|
3725
3858
|
action,
|
|
3726
3859
|
this.onInteraction,
|
|
@@ -3729,7 +3862,7 @@ var SignerEth = class {
|
|
|
3729
3862
|
);
|
|
3730
3863
|
}
|
|
3731
3864
|
async signMessage(derivationPath, message) {
|
|
3732
|
-
const action = this._sdk.signMessage(derivationPath, (0,
|
|
3865
|
+
const action = this._sdk.signMessage(derivationPath, (0, import_hwk_adapter_core5.hexToBytes)(message));
|
|
3733
3866
|
return deviceActionToPromise(
|
|
3734
3867
|
action,
|
|
3735
3868
|
this.onInteraction,
|
|
@@ -3789,23 +3922,23 @@ var SignerManager = class _SignerManager {
|
|
|
3789
3922
|
};
|
|
3790
3923
|
|
|
3791
3924
|
// src/connector/chains/evm.ts
|
|
3792
|
-
var
|
|
3925
|
+
var import_hwk_adapter_core7 = require("@onekeyfe/hwk-adapter-core");
|
|
3793
3926
|
|
|
3794
3927
|
// src/connector/chains/utils.ts
|
|
3795
|
-
var
|
|
3928
|
+
var import_hwk_adapter_core6 = require("@onekeyfe/hwk-adapter-core");
|
|
3796
3929
|
function normalizePath(path) {
|
|
3797
3930
|
return path.startsWith("m/") ? path.slice(2) : path;
|
|
3798
3931
|
}
|
|
3799
3932
|
function collapseSignerInteraction(interaction) {
|
|
3800
3933
|
switch (interaction) {
|
|
3801
3934
|
case "confirm-open-app":
|
|
3802
|
-
return
|
|
3935
|
+
return import_hwk_adapter_core6.EConnectorInteraction.ConfirmOpenApp;
|
|
3803
3936
|
case "unlock-device":
|
|
3804
|
-
return
|
|
3937
|
+
return import_hwk_adapter_core6.EConnectorInteraction.UnlockDevice;
|
|
3805
3938
|
case "interaction-complete":
|
|
3806
|
-
return
|
|
3939
|
+
return import_hwk_adapter_core6.EConnectorInteraction.InteractionComplete;
|
|
3807
3940
|
default:
|
|
3808
|
-
return
|
|
3941
|
+
return import_hwk_adapter_core6.EConnectorInteraction.ConfirmOnDevice;
|
|
3809
3942
|
}
|
|
3810
3943
|
}
|
|
3811
3944
|
function wireSignerToSession(ctx, sessionId, chain, signer) {
|
|
@@ -3852,7 +3985,7 @@ async function evmSignTransaction(ctx, sessionId, params) {
|
|
|
3852
3985
|
new Error(
|
|
3853
3986
|
"Ledger requires a pre-serialized transaction (serializedTx). Provide an RLP-encoded hex string."
|
|
3854
3987
|
),
|
|
3855
|
-
{ code:
|
|
3988
|
+
{ code: import_hwk_adapter_core7.HardwareErrorCode.InvalidParams }
|
|
3856
3989
|
);
|
|
3857
3990
|
}
|
|
3858
3991
|
const signer = await _getEthSigner(ctx, sessionId);
|
|
@@ -3861,8 +3994,8 @@ async function evmSignTransaction(ctx, sessionId, params) {
|
|
|
3861
3994
|
const result = await signer.signTransaction(path, params.serializedTx);
|
|
3862
3995
|
return {
|
|
3863
3996
|
v: `0x${result.v.toString(16)}`,
|
|
3864
|
-
r: (0,
|
|
3865
|
-
s: (0,
|
|
3997
|
+
r: (0, import_hwk_adapter_core7.padHex64)(result.r),
|
|
3998
|
+
s: (0, import_hwk_adapter_core7.padHex64)(result.s)
|
|
3866
3999
|
};
|
|
3867
4000
|
} catch (err) {
|
|
3868
4001
|
ctx.invalidateSession(sessionId);
|
|
@@ -3876,8 +4009,8 @@ async function evmSignMessage(ctx, sessionId, params) {
|
|
|
3876
4009
|
const path = normalizePath(params.path);
|
|
3877
4010
|
try {
|
|
3878
4011
|
const result = await signer.signMessage(path, params.message);
|
|
3879
|
-
const rHex = (0,
|
|
3880
|
-
const sHex = (0,
|
|
4012
|
+
const rHex = (0, import_hwk_adapter_core7.stripHex)(result.r).padStart(64, "0");
|
|
4013
|
+
const sHex = (0, import_hwk_adapter_core7.stripHex)(result.s).padStart(64, "0");
|
|
3881
4014
|
const vHex = result.v.toString(16).padStart(2, "0");
|
|
3882
4015
|
return { signature: `0x${rHex}${sHex}${vHex}` };
|
|
3883
4016
|
} catch (err) {
|
|
@@ -3893,15 +4026,15 @@ async function evmSignTypedData(ctx, sessionId, params) {
|
|
|
3893
4026
|
new Error(
|
|
3894
4027
|
'Ledger does not support hash-only EIP-712 signing. Use mode "full" with the complete typed data structure.'
|
|
3895
4028
|
),
|
|
3896
|
-
{ code:
|
|
4029
|
+
{ code: import_hwk_adapter_core7.HardwareErrorCode.MethodNotSupported }
|
|
3897
4030
|
);
|
|
3898
4031
|
}
|
|
3899
4032
|
const signer = await _getEthSigner(ctx, sessionId);
|
|
3900
4033
|
const path = normalizePath(params.path);
|
|
3901
4034
|
try {
|
|
3902
4035
|
const result = await signer.signTypedData(path, params.data);
|
|
3903
|
-
const rHex = (0,
|
|
3904
|
-
const sHex = (0,
|
|
4036
|
+
const rHex = (0, import_hwk_adapter_core7.stripHex)(result.r).padStart(64, "0");
|
|
4037
|
+
const sHex = (0, import_hwk_adapter_core7.stripHex)(result.s).padStart(64, "0");
|
|
3905
4038
|
const vHex = result.v.toString(16).padStart(2, "0");
|
|
3906
4039
|
return { signature: `0x${rHex}${sHex}${vHex}` };
|
|
3907
4040
|
} catch (err) {
|
|
@@ -3928,13 +4061,13 @@ async function _getEthSigner(ctx, sessionId) {
|
|
|
3928
4061
|
}
|
|
3929
4062
|
|
|
3930
4063
|
// src/connector/chains/btc.ts
|
|
3931
|
-
var
|
|
3932
|
-
var
|
|
4064
|
+
var import_hwk_adapter_core9 = require("@onekeyfe/hwk-adapter-core");
|
|
4065
|
+
var import_bitcoinjs_lib2 = require("bitcoinjs-lib");
|
|
3933
4066
|
|
|
3934
4067
|
// src/signer/SignerBtc.ts
|
|
3935
|
-
var
|
|
4068
|
+
var import_hwk_adapter_core8 = require("@onekeyfe/hwk-adapter-core");
|
|
3936
4069
|
function hexToUtf8(hex) {
|
|
3937
|
-
return new TextDecoder().decode((0,
|
|
4070
|
+
return new TextDecoder().decode((0, import_hwk_adapter_core8.hexToBytes)(hex));
|
|
3938
4071
|
}
|
|
3939
4072
|
var SignerBtc = class {
|
|
3940
4073
|
// eslint-disable-next-line no-useless-constructor, no-empty-function
|
|
@@ -3974,7 +4107,7 @@ var SignerBtc = class {
|
|
|
3974
4107
|
"[SignerBtc] getExtendedPublicKey result type:",
|
|
3975
4108
|
typeof result,
|
|
3976
4109
|
"value:",
|
|
3977
|
-
|
|
4110
|
+
summarizeForLog(result)
|
|
3978
4111
|
);
|
|
3979
4112
|
if (typeof result === "string") return result;
|
|
3980
4113
|
return result.extendedPublicKey;
|
|
@@ -4048,14 +4181,14 @@ function _purposeToTemplate(purpose, DDT) {
|
|
|
4048
4181
|
return DDT.TAPROOT;
|
|
4049
4182
|
default:
|
|
4050
4183
|
throw Object.assign(new Error(`Unsupported BTC purpose: m/${purpose ?? "<undefined>"}'`), {
|
|
4051
|
-
code:
|
|
4184
|
+
code: import_hwk_adapter_core9.HardwareErrorCode.InvalidParams
|
|
4052
4185
|
});
|
|
4053
4186
|
}
|
|
4054
4187
|
}
|
|
4055
4188
|
function assertBitcoinMainnet(coin) {
|
|
4056
4189
|
if (coin !== void 0 && !["btc", "bitcoin"].includes(coin.toLowerCase())) {
|
|
4057
4190
|
throw Object.assign(new Error(`Ledger BTC supports Bitcoin mainnet only, not ${coin}`), {
|
|
4058
|
-
code:
|
|
4191
|
+
code: import_hwk_adapter_core9.HardwareErrorCode.ChainNotSupported
|
|
4059
4192
|
});
|
|
4060
4193
|
}
|
|
4061
4194
|
}
|
|
@@ -4064,7 +4197,7 @@ async function btcGetAddress(ctx, sessionId, params) {
|
|
|
4064
4197
|
if (normalizePath(params.path).split("/").length !== 3) {
|
|
4065
4198
|
throw Object.assign(
|
|
4066
4199
|
new Error("Ledger btcGetAddress takes an account path (e.g. m/84'/0'/0') plus addressIndex"),
|
|
4067
|
-
{ code:
|
|
4200
|
+
{ code: import_hwk_adapter_core9.HardwareErrorCode.InvalidParams }
|
|
4068
4201
|
);
|
|
4069
4202
|
}
|
|
4070
4203
|
const btcSigner = await _createBtcSigner(ctx, sessionId);
|
|
@@ -4105,7 +4238,7 @@ async function btcGetPublicKey(ctx, sessionId, params) {
|
|
|
4105
4238
|
const xpub = await btcSigner.getExtendedPublicKey(path, {
|
|
4106
4239
|
checkOnDevice: params.showOnDevice ?? false
|
|
4107
4240
|
});
|
|
4108
|
-
debugLog("[LedgerConnector] btcGetPublicKey success, xpub:",
|
|
4241
|
+
debugLog("[LedgerConnector] btcGetPublicKey success, xpub:", summarizeForLog(xpub));
|
|
4109
4242
|
return { xpub, path: params.path };
|
|
4110
4243
|
} catch (err) {
|
|
4111
4244
|
debugError("[LedgerConnector] btcGetPublicKey error, path:", path, "err:", err);
|
|
@@ -4119,7 +4252,7 @@ async function btcSignTransaction(ctx, sessionId, params) {
|
|
|
4119
4252
|
if (!params.psbt) {
|
|
4120
4253
|
throw Object.assign(
|
|
4121
4254
|
new Error("Ledger requires PSBT format for BTC transaction signing. Provide params.psbt."),
|
|
4122
|
-
{ code:
|
|
4255
|
+
{ code: import_hwk_adapter_core9.HardwareErrorCode.InvalidParams }
|
|
4123
4256
|
);
|
|
4124
4257
|
}
|
|
4125
4258
|
const btcSigner = await _createBtcSigner(ctx, sessionId);
|
|
@@ -4133,7 +4266,7 @@ async function btcSignTransaction(ctx, sessionId, params) {
|
|
|
4133
4266
|
debugLog("[LedgerConnector] btcSignTransaction wallet:", { path, purpose, template });
|
|
4134
4267
|
const wallet = new DefaultWallet(path, template);
|
|
4135
4268
|
const signedTxHex = await btcSigner.signTransaction(wallet, params.psbt);
|
|
4136
|
-
return { serializedTx: (0,
|
|
4269
|
+
return { serializedTx: (0, import_hwk_adapter_core9.stripHex)(signedTxHex) };
|
|
4137
4270
|
} catch (err) {
|
|
4138
4271
|
ctx.invalidateSession(sessionId);
|
|
4139
4272
|
throw ctx.wrapError(err);
|
|
@@ -4144,7 +4277,7 @@ async function btcSignTransaction(ctx, sessionId, params) {
|
|
|
4144
4277
|
async function btcSignPsbt(ctx, sessionId, params) {
|
|
4145
4278
|
if (!params.psbt) {
|
|
4146
4279
|
throw Object.assign(new Error("btcSignPsbt requires params.psbt"), {
|
|
4147
|
-
code:
|
|
4280
|
+
code: import_hwk_adapter_core9.HardwareErrorCode.InvalidParams
|
|
4148
4281
|
});
|
|
4149
4282
|
}
|
|
4150
4283
|
const btcSigner = await _createBtcSigner(ctx, sessionId);
|
|
@@ -4174,8 +4307,8 @@ async function btcSignMessage(ctx, sessionId, params) {
|
|
|
4174
4307
|
try {
|
|
4175
4308
|
const result = await btcSigner.signMessage(path, params.message);
|
|
4176
4309
|
const vHex = result.v.toString(16).padStart(2, "0");
|
|
4177
|
-
const rHex = (0,
|
|
4178
|
-
const sHex = (0,
|
|
4310
|
+
const rHex = (0, import_hwk_adapter_core9.stripHex)(result.r).padStart(64, "0");
|
|
4311
|
+
const sHex = (0, import_hwk_adapter_core9.stripHex)(result.s).padStart(64, "0");
|
|
4179
4312
|
return { signature: `${vHex}${rHex}${sHex}` };
|
|
4180
4313
|
} catch (err) {
|
|
4181
4314
|
ctx.invalidateSession(sessionId);
|
|
@@ -4226,7 +4359,7 @@ function _isScriptPathInput(input) {
|
|
|
4226
4359
|
return !!(input.tapLeafScript && input.tapLeafScript.length > 0);
|
|
4227
4360
|
}
|
|
4228
4361
|
function _applySignaturesToPsbt(psbtHex, signatures) {
|
|
4229
|
-
const psbt =
|
|
4362
|
+
const psbt = import_bitcoinjs_lib2.Psbt.fromHex(psbtHex);
|
|
4230
4363
|
for (const sig of signatures) {
|
|
4231
4364
|
const input = psbt.data.inputs[sig.inputIndex];
|
|
4232
4365
|
if (!input) {
|
|
@@ -4275,7 +4408,7 @@ function _applySignaturesToPsbt(psbtHex, signatures) {
|
|
|
4275
4408
|
}
|
|
4276
4409
|
|
|
4277
4410
|
// src/connector/chains/sol.ts
|
|
4278
|
-
var
|
|
4411
|
+
var import_hwk_adapter_core10 = require("@onekeyfe/hwk-adapter-core");
|
|
4279
4412
|
var import_bs58 = __toESM(require("bs58"));
|
|
4280
4413
|
|
|
4281
4414
|
// src/signer/SignerSol.ts
|
|
@@ -4344,10 +4477,10 @@ async function solGetAddress(ctx, sessionId, params) {
|
|
|
4344
4477
|
async function solSignTransaction(ctx, sessionId, params) {
|
|
4345
4478
|
const solSigner = await _createSolSigner(ctx, sessionId);
|
|
4346
4479
|
const path = normalizePath(params.path);
|
|
4347
|
-
const txBytes = (0,
|
|
4480
|
+
const txBytes = (0, import_hwk_adapter_core10.hexToBytes)(params.serializedTx);
|
|
4348
4481
|
try {
|
|
4349
4482
|
const result = await solSigner.signTransaction(path, txBytes);
|
|
4350
|
-
return { signature: (0,
|
|
4483
|
+
return { signature: (0, import_hwk_adapter_core10.bytesToHex)(result) };
|
|
4351
4484
|
} catch (err) {
|
|
4352
4485
|
ctx.invalidateSession(sessionId);
|
|
4353
4486
|
throw ctx.wrapError(err);
|
|
@@ -4358,10 +4491,10 @@ async function solSignTransaction(ctx, sessionId, params) {
|
|
|
4358
4491
|
async function solSignMessage(ctx, sessionId, params) {
|
|
4359
4492
|
const solSigner = await _createSolSigner(ctx, sessionId);
|
|
4360
4493
|
const path = normalizePath(params.path);
|
|
4361
|
-
const messageBytes = (0,
|
|
4494
|
+
const messageBytes = (0, import_hwk_adapter_core10.hexToBytes)(params.message);
|
|
4362
4495
|
try {
|
|
4363
4496
|
if (params.messageVersion === 1) {
|
|
4364
|
-
const preparedMessage = (0,
|
|
4497
|
+
const preparedMessage = (0, import_hwk_adapter_core10.prepareSolanaOffchainMessageV1)({
|
|
4365
4498
|
message: messageBytes,
|
|
4366
4499
|
requiredSigners: params.requiredSigners
|
|
4367
4500
|
});
|
|
@@ -4387,14 +4520,14 @@ function decodeBase58Signature(signature) {
|
|
|
4387
4520
|
if (bytes.length !== 64) {
|
|
4388
4521
|
throw new Error(`Ledger Solana signature must be 64 bytes, received ${bytes.length}`);
|
|
4389
4522
|
}
|
|
4390
|
-
return (0,
|
|
4523
|
+
return (0, import_hwk_adapter_core10.bytesToHex)(bytes);
|
|
4391
4524
|
}
|
|
4392
4525
|
function decodeBase58EnvelopeSignature(envelope) {
|
|
4393
4526
|
const bytes = import_bs58.default.decode(envelope);
|
|
4394
4527
|
if (bytes.length < 65 || bytes[0] !== 1) {
|
|
4395
4528
|
throw new Error("Ledger Solana signature envelope is invalid");
|
|
4396
4529
|
}
|
|
4397
|
-
return (0,
|
|
4530
|
+
return (0, import_hwk_adapter_core10.bytesToHex)(bytes.subarray(1, 65));
|
|
4398
4531
|
}
|
|
4399
4532
|
async function _createSolSigner(ctx, sessionId) {
|
|
4400
4533
|
const dmk = await ctx.getOrCreateDmk();
|
|
@@ -4419,7 +4552,7 @@ async function _createSolSigner(ctx, sessionId) {
|
|
|
4419
4552
|
}
|
|
4420
4553
|
|
|
4421
4554
|
// src/connector/chains/zcash.ts
|
|
4422
|
-
var
|
|
4555
|
+
var import_hwk_adapter_core11 = require("@onekeyfe/hwk-adapter-core");
|
|
4423
4556
|
|
|
4424
4557
|
// src/signer/SignerZcash.ts
|
|
4425
4558
|
var SignerZcash = class {
|
|
@@ -4459,7 +4592,7 @@ async function zcashGetFullViewingKey(ctx, sessionId, params) {
|
|
|
4459
4592
|
if (result.mode === "ufvk") {
|
|
4460
4593
|
return { path: params.path, mode, ufvk: result.fullViewingKey };
|
|
4461
4594
|
}
|
|
4462
|
-
return { path: params.path, mode, orchardFvk: (0,
|
|
4595
|
+
return { path: params.path, mode, orchardFvk: (0, import_hwk_adapter_core11.bytesToHex)(result.fullViewingKey) };
|
|
4463
4596
|
});
|
|
4464
4597
|
}
|
|
4465
4598
|
async function zcashGetShieldedAddress(ctx, sessionId, params) {
|
|
@@ -4480,7 +4613,7 @@ async function _createZcashSigner(ctx, sessionId) {
|
|
|
4480
4613
|
}
|
|
4481
4614
|
|
|
4482
4615
|
// src/connector/chains/tron.ts
|
|
4483
|
-
var
|
|
4616
|
+
var import_hwk_adapter_core12 = require("@onekeyfe/hwk-adapter-core");
|
|
4484
4617
|
|
|
4485
4618
|
// src/signer/SignerTron.ts
|
|
4486
4619
|
var SignerTron = class {
|
|
@@ -4536,22 +4669,22 @@ async function tronSignTransaction(ctx, sessionId, params) {
|
|
|
4536
4669
|
if (!params.rawTxHex) {
|
|
4537
4670
|
throw Object.assign(
|
|
4538
4671
|
new Error("TRON signing requires a protobuf-encoded raw transaction hex (rawTxHex)."),
|
|
4539
|
-
{ code:
|
|
4672
|
+
{ code: import_hwk_adapter_core12.HardwareErrorCode.InvalidParams }
|
|
4540
4673
|
);
|
|
4541
4674
|
}
|
|
4542
4675
|
const tronSigner = await _createTronSigner(ctx, sessionId);
|
|
4543
4676
|
const path = normalizePath(params.path);
|
|
4544
4677
|
return runSignerCall(ctx, sessionId, async () => {
|
|
4545
|
-
const signature = await tronSigner.signTransaction(path, (0,
|
|
4546
|
-
return { signature: (0,
|
|
4678
|
+
const signature = await tronSigner.signTransaction(path, (0, import_hwk_adapter_core12.hexToBytes)(params.rawTxHex));
|
|
4679
|
+
return { signature: (0, import_hwk_adapter_core12.bytesToHex)(signature) };
|
|
4547
4680
|
});
|
|
4548
4681
|
}
|
|
4549
4682
|
async function tronSignMessage(ctx, sessionId, params) {
|
|
4550
4683
|
const tronSigner = await _createTronSigner(ctx, sessionId);
|
|
4551
4684
|
const path = normalizePath(params.path);
|
|
4552
4685
|
return runSignerCall(ctx, sessionId, async () => {
|
|
4553
|
-
const signature = await tronSigner.signPersonalMessage(path, (0,
|
|
4554
|
-
return { signature: (0,
|
|
4686
|
+
const signature = await tronSigner.signPersonalMessage(path, (0, import_hwk_adapter_core12.hexToBytes)(params.messageHex));
|
|
4687
|
+
return { signature: (0, import_hwk_adapter_core12.bytesToHex)(signature) };
|
|
4555
4688
|
});
|
|
4556
4689
|
}
|
|
4557
4690
|
async function _createTronSigner(ctx, sessionId) {
|
|
@@ -4857,8 +4990,16 @@ var METHOD_PREFIX_TO_APP_NAME = {
|
|
|
4857
4990
|
tron: "Tron",
|
|
4858
4991
|
zcash: "Zcash"
|
|
4859
4992
|
};
|
|
4993
|
+
function withOpenAppName(event, data, appName) {
|
|
4994
|
+
if (event !== "ui-event") return data;
|
|
4995
|
+
const uiEvent = data;
|
|
4996
|
+
if (uiEvent.type !== import_hwk_adapter_core13.EConnectorInteraction.ConfirmOpenApp || uiEvent.payload.appName) {
|
|
4997
|
+
return data;
|
|
4998
|
+
}
|
|
4999
|
+
return { ...uiEvent, payload: { ...uiEvent.payload, appName } };
|
|
5000
|
+
}
|
|
4860
5001
|
var HARDWARE_ERROR_CODE_VALUES = new Set(
|
|
4861
|
-
Object.values(
|
|
5002
|
+
Object.values(import_hwk_adapter_core13.HardwareErrorCode).filter((value) => typeof value === "number")
|
|
4862
5003
|
);
|
|
4863
5004
|
var BLE_CONNECT_SCAN_TIMEOUT_MS = 1500;
|
|
4864
5005
|
var LEDGER_RELAY_ALLOWED_ROOT_DOMAINS = ["onekeytest.com", "onekey.com"];
|
|
@@ -5010,7 +5151,7 @@ var LedgerConnectorBase = class {
|
|
|
5010
5151
|
async searchDevices() {
|
|
5011
5152
|
const dm = await this._getDeviceManager();
|
|
5012
5153
|
const descriptors = await this._discoverDescriptors(dm);
|
|
5013
|
-
const resolvedDescriptors = descriptors.filter((d) => !(0,
|
|
5154
|
+
const resolvedDescriptors = descriptors.filter((d) => !(0, import_hwk_adapter_core13.isKnownNonTargetHardwareVendor)(d, "ledger")).map((d) => ({
|
|
5014
5155
|
descriptor: d,
|
|
5015
5156
|
connectId: this._resolveConnectId(d)
|
|
5016
5157
|
}));
|
|
@@ -5058,7 +5199,7 @@ var LedgerConnectorBase = class {
|
|
|
5058
5199
|
`Ledger BLE connect did not return within ${HANG_CEILING_MS / 6e4}min \u2014 DMK hang fallback.`
|
|
5059
5200
|
);
|
|
5060
5201
|
err._tag = ERROR_TAG.BlePairingTimeout;
|
|
5061
|
-
err.code =
|
|
5202
|
+
err.code = import_hwk_adapter_core13.HardwareErrorCode.BlePairingTimeout;
|
|
5062
5203
|
reject(err);
|
|
5063
5204
|
}, HANG_CEILING_MS);
|
|
5064
5205
|
});
|
|
@@ -5091,7 +5232,7 @@ var LedgerConnectorBase = class {
|
|
|
5091
5232
|
"Ledger device is not currently advertising. Wake up and unlock the device, keep it nearby, then try again."
|
|
5092
5233
|
);
|
|
5093
5234
|
err._tag = ERROR_TAG.DeviceNotAdvertising;
|
|
5094
|
-
err.code =
|
|
5235
|
+
err.code = import_hwk_adapter_core13.HardwareErrorCode.DeviceNotFound;
|
|
5095
5236
|
throw err;
|
|
5096
5237
|
};
|
|
5097
5238
|
const doConnect = async (path) => {
|
|
@@ -5147,6 +5288,13 @@ var LedgerConnectorBase = class {
|
|
|
5147
5288
|
} catch (err) {
|
|
5148
5289
|
this._resetSignersAndSessions();
|
|
5149
5290
|
if (isLedgerBleConnectionType(this.connectionType)) {
|
|
5291
|
+
if (isBleBondInvalidError(err)) {
|
|
5292
|
+
throw (0, import_hwk_adapter_core13.createHwkError)({
|
|
5293
|
+
code: import_hwk_adapter_core13.HardwareErrorCode.BleBondInvalid,
|
|
5294
|
+
message: "Ledger Bluetooth pairing is no longer valid. Forget this device in system Bluetooth settings, then reconnect.",
|
|
5295
|
+
_tag: ERROR_TAG.PeerRemovedPairing
|
|
5296
|
+
});
|
|
5297
|
+
}
|
|
5150
5298
|
const tag = err?._tag;
|
|
5151
5299
|
if (isKnownConnectionTag(tag) && !isConnectionOpeningTag(tag)) {
|
|
5152
5300
|
throw err;
|
|
@@ -5155,7 +5303,7 @@ var LedgerConnectorBase = class {
|
|
|
5155
5303
|
"Ledger Bluetooth pairing failed. Make sure the device is unlocked and nearby, then try again."
|
|
5156
5304
|
);
|
|
5157
5305
|
wrapped._tag = ERROR_TAG.BleGattBondingFailed;
|
|
5158
|
-
wrapped.code =
|
|
5306
|
+
wrapped.code = import_hwk_adapter_core13.HardwareErrorCode.BlePairingTimeout;
|
|
5159
5307
|
wrapped.originalError = err;
|
|
5160
5308
|
throw wrapped;
|
|
5161
5309
|
}
|
|
@@ -5233,7 +5381,7 @@ var LedgerConnectorBase = class {
|
|
|
5233
5381
|
this._unwatchSessionState(sessionId);
|
|
5234
5382
|
this._signerManager?.invalidate(sessionId);
|
|
5235
5383
|
this._cancellers.get(sessionId)?.({
|
|
5236
|
-
code:
|
|
5384
|
+
code: import_hwk_adapter_core13.HardwareErrorCode.DeviceDisconnected,
|
|
5237
5385
|
tag: "DeviceDisconnected",
|
|
5238
5386
|
message: "Device disconnected"
|
|
5239
5387
|
});
|
|
@@ -5249,7 +5397,7 @@ var LedgerConnectorBase = class {
|
|
|
5249
5397
|
// whitelist (`toPlainError`). The SW-side adapter rehydrates and re-throws
|
|
5250
5398
|
// locally (see LedgerAdapter._unwrapConnectorResult).
|
|
5251
5399
|
async call(sessionId, method, params) {
|
|
5252
|
-
debugLog("[DMK] call:", method, JSON.stringify(params));
|
|
5400
|
+
debugLog("[DMK] call:", method, JSON.stringify(summarizeForLog(params, { method })));
|
|
5253
5401
|
try {
|
|
5254
5402
|
const payload = await this._dispatch(sessionId, method, params);
|
|
5255
5403
|
return { success: true, payload };
|
|
@@ -5257,9 +5405,9 @@ var LedgerConnectorBase = class {
|
|
|
5257
5405
|
if (isAppStuckByApdu(err)) {
|
|
5258
5406
|
return {
|
|
5259
5407
|
success: false,
|
|
5260
|
-
error: (0,
|
|
5408
|
+
error: (0, import_hwk_adapter_core13.serializeConnectorError)(
|
|
5261
5409
|
Object.assign(new Error("Ledger app is unresponsive"), {
|
|
5262
|
-
code:
|
|
5410
|
+
code: import_hwk_adapter_core13.HardwareErrorCode.DeviceAppStuck,
|
|
5263
5411
|
_tag: ERROR_TAG.DeviceAppStuck,
|
|
5264
5412
|
originalError: err
|
|
5265
5413
|
})
|
|
@@ -5269,16 +5417,16 @@ var LedgerConnectorBase = class {
|
|
|
5269
5417
|
if (isTransportStuck(err)) {
|
|
5270
5418
|
return {
|
|
5271
5419
|
success: false,
|
|
5272
|
-
error: (0,
|
|
5420
|
+
error: (0, import_hwk_adapter_core13.serializeConnectorError)(
|
|
5273
5421
|
Object.assign(new Error("Device communication interrupted, please retry"), {
|
|
5274
|
-
code:
|
|
5422
|
+
code: import_hwk_adapter_core13.HardwareErrorCode.TransportError,
|
|
5275
5423
|
_tag: ERROR_TAG.DeviceTransportStuck,
|
|
5276
5424
|
originalError: err
|
|
5277
5425
|
})
|
|
5278
5426
|
)
|
|
5279
5427
|
};
|
|
5280
5428
|
}
|
|
5281
|
-
return { success: false, error: (0,
|
|
5429
|
+
return { success: false, error: (0, import_hwk_adapter_core13.serializeConnectorError)(err) };
|
|
5282
5430
|
}
|
|
5283
5431
|
}
|
|
5284
5432
|
async _dispatch(sessionId, method, params) {
|
|
@@ -5346,7 +5494,7 @@ var LedgerConnectorBase = class {
|
|
|
5346
5494
|
try {
|
|
5347
5495
|
return await apps.install(p.appName, ({ progress }) => {
|
|
5348
5496
|
ctx.emit("ui-event", {
|
|
5349
|
-
type:
|
|
5497
|
+
type: import_hwk_adapter_core13.EConnectorInteraction.AppInstallProgress,
|
|
5350
5498
|
payload: {
|
|
5351
5499
|
sessionId,
|
|
5352
5500
|
appName: p.appName,
|
|
@@ -5658,8 +5806,8 @@ var LedgerConnectorBase = class {
|
|
|
5658
5806
|
// ---------------------------------------------------------------------------
|
|
5659
5807
|
/**
|
|
5660
5808
|
* Return a per-call ctx with the chain's Ledger app name pre-bound to
|
|
5661
|
-
* wrapError, so chain handlers don't need to repeat
|
|
5662
|
-
*
|
|
5809
|
+
* UI prompts and wrapError, so chain handlers don't need to repeat the name.
|
|
5810
|
+
* Falls through unchanged for unknown methods.
|
|
5663
5811
|
*/
|
|
5664
5812
|
_ctxForMethod(method) {
|
|
5665
5813
|
const prefix = /^(evm|btc|sol|tron|zcash)/.exec(method)?.[1];
|
|
@@ -5667,7 +5815,8 @@ var LedgerConnectorBase = class {
|
|
|
5667
5815
|
if (!defaultAppName) return this._ctx;
|
|
5668
5816
|
return {
|
|
5669
5817
|
...this._ctx,
|
|
5670
|
-
wrapError: (err, opts) => this._wrapError(err, { defaultAppName, ...opts })
|
|
5818
|
+
wrapError: (err, opts) => this._wrapError(err, { defaultAppName, ...opts }),
|
|
5819
|
+
emit: (event, data) => this._ctx.emit(event, withOpenAppName(event, data, defaultAppName))
|
|
5671
5820
|
};
|
|
5672
5821
|
}
|
|
5673
5822
|
_wrapError(err, opts) {
|
|
@@ -5724,7 +5873,7 @@ var DmkTransport = class extends import_hw_transport.default {
|
|
|
5724
5873
|
|
|
5725
5874
|
// src/app/AppManager.ts
|
|
5726
5875
|
var import_device_management_kit3 = require("@ledgerhq/device-management-kit");
|
|
5727
|
-
var
|
|
5876
|
+
var import_hwk_adapter_core14 = require("@onekeyfe/hwk-adapter-core");
|
|
5728
5877
|
var APP_NAME_MAP = {
|
|
5729
5878
|
ETH: "Ethereum",
|
|
5730
5879
|
BTC: "Bitcoin",
|
|
@@ -5842,9 +5991,9 @@ var AppManager = class {
|
|
|
5842
5991
|
);
|
|
5843
5992
|
let code;
|
|
5844
5993
|
if (errorCode === "6807" || /unknown application/i.test(message)) {
|
|
5845
|
-
code =
|
|
5994
|
+
code = import_hwk_adapter_core14.HardwareErrorCode.AppNotInstalled;
|
|
5846
5995
|
} else if (errorCode === "5501" || dmkErr._tag === "ActionRefusedError") {
|
|
5847
|
-
code =
|
|
5996
|
+
code = import_hwk_adapter_core14.HardwareErrorCode.UserRejected;
|
|
5848
5997
|
}
|
|
5849
5998
|
throw Object.assign(new Error(`Failed to open "${appName}"`), {
|
|
5850
5999
|
_tag: ERROR_TAG.OpenAppCommand,
|