@onekeyfe/hwk-ledger-adapter 1.1.27-alpha.7 → 1.1.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +200 -115
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +200 -115
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -491,6 +491,53 @@ function mapLedgerError(err, opts) {
|
|
|
491
491
|
|
|
492
492
|
// src/adapter/methods/allNetworkGetAddress.ts
|
|
493
493
|
var import_hwk_adapter_core2 = require("@onekeyfe/hwk-adapter-core");
|
|
494
|
+
|
|
495
|
+
// src/utils/sdkEventBus.ts
|
|
496
|
+
var listeners = /* @__PURE__ */ new Set();
|
|
497
|
+
function onSdkEvent(listener) {
|
|
498
|
+
listeners.add(listener);
|
|
499
|
+
return () => {
|
|
500
|
+
listeners.delete(listener);
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
function offSdkEvent(listener) {
|
|
504
|
+
listeners.delete(listener);
|
|
505
|
+
}
|
|
506
|
+
function emitSdkEvent(event) {
|
|
507
|
+
if (listeners.size === 0) return;
|
|
508
|
+
for (const listener of listeners) {
|
|
509
|
+
try {
|
|
510
|
+
listener(event);
|
|
511
|
+
} catch {
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
function emitLog(level, ...args) {
|
|
516
|
+
if (listeners.size === 0) return;
|
|
517
|
+
const message = args.map((a) => typeof a === "string" ? a : safeStringify(a)).join(" ");
|
|
518
|
+
emitSdkEvent({ type: "log", level, message });
|
|
519
|
+
}
|
|
520
|
+
function safeStringify(value) {
|
|
521
|
+
if (value instanceof Error) {
|
|
522
|
+
return value.stack ? `${value.message}
|
|
523
|
+
${value.stack}` : value.message;
|
|
524
|
+
}
|
|
525
|
+
try {
|
|
526
|
+
return JSON.stringify(value);
|
|
527
|
+
} catch {
|
|
528
|
+
return String(value);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// src/utils/debugLog.ts
|
|
533
|
+
function debugLog(...args) {
|
|
534
|
+
emitLog("debug", ...args);
|
|
535
|
+
}
|
|
536
|
+
function debugError(...args) {
|
|
537
|
+
emitLog("error", ...args);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// src/adapter/methods/allNetworkGetAddress.ts
|
|
494
541
|
var LEDGER_BTC_NETWORK_COIN_MAP = {
|
|
495
542
|
tbtc: "Testnet",
|
|
496
543
|
bch: "Bcash",
|
|
@@ -503,6 +550,7 @@ function createAllNetworkGetAddress({
|
|
|
503
550
|
getChainFingerprint
|
|
504
551
|
}) {
|
|
505
552
|
return async function allNetworkGetAddress(connectId, _deviceId, params) {
|
|
553
|
+
debugLog("[LedgerAdapter][REQ]", { method: "allNetworkGetAddress", connectId, params });
|
|
506
554
|
const installContext = {};
|
|
507
555
|
const commonParams = {
|
|
508
556
|
autoInstallApp: params.autoInstallApp
|
|
@@ -530,23 +578,37 @@ function createAllNetworkGetAddress({
|
|
|
530
578
|
chainFingerprints
|
|
531
579
|
);
|
|
532
580
|
if (isTopLevelAllNetworkFailure(response)) {
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
581
|
+
const code = response.payload?.code ?? import_hwk_adapter_core2.HardwareErrorCode.DeviceMismatch;
|
|
582
|
+
const result2 = (0, import_hwk_adapter_core2.failure)(
|
|
583
|
+
code,
|
|
584
|
+
response.payload?.error ?? "All-network get-address aborted",
|
|
536
585
|
response.payload?.params
|
|
537
586
|
);
|
|
587
|
+
debugLog("[LedgerAdapter][RES]", {
|
|
588
|
+
method: "allNetworkGetAddress",
|
|
589
|
+
success: false,
|
|
590
|
+
payload: result2
|
|
591
|
+
});
|
|
592
|
+
return result2;
|
|
538
593
|
}
|
|
539
594
|
responses.push(response);
|
|
540
595
|
}
|
|
541
596
|
}
|
|
542
|
-
|
|
597
|
+
const result = (0, import_hwk_adapter_core2.success)(responses);
|
|
598
|
+
debugLog("[LedgerAdapter][RES]", {
|
|
599
|
+
method: "allNetworkGetAddress",
|
|
600
|
+
success: true,
|
|
601
|
+
payload: result
|
|
602
|
+
});
|
|
603
|
+
return result;
|
|
543
604
|
};
|
|
544
605
|
}
|
|
545
606
|
function isTopLevelAllNetworkFailure(response) {
|
|
546
607
|
if (response.success) {
|
|
547
608
|
return false;
|
|
548
609
|
}
|
|
549
|
-
|
|
610
|
+
const code = response.payload?.code;
|
|
611
|
+
return code === import_hwk_adapter_core2.HardwareErrorCode.DeviceMismatch || code === import_hwk_adapter_core2.HardwareErrorCode.UserAborted || code === import_hwk_adapter_core2.HardwareErrorCode.UserRejected;
|
|
550
612
|
}
|
|
551
613
|
function getItemDeviceId(item) {
|
|
552
614
|
const { deviceId } = item;
|
|
@@ -721,51 +783,6 @@ function isLedgerBleDescriptor(connectionType, descriptor) {
|
|
|
721
783
|
return isLedgerBleConnectionType(connectionType) || isLedgerDmkBleTransport(descriptor.transport);
|
|
722
784
|
}
|
|
723
785
|
|
|
724
|
-
// src/utils/sdkEventBus.ts
|
|
725
|
-
var listeners = /* @__PURE__ */ new Set();
|
|
726
|
-
function onSdkEvent(listener) {
|
|
727
|
-
listeners.add(listener);
|
|
728
|
-
return () => {
|
|
729
|
-
listeners.delete(listener);
|
|
730
|
-
};
|
|
731
|
-
}
|
|
732
|
-
function offSdkEvent(listener) {
|
|
733
|
-
listeners.delete(listener);
|
|
734
|
-
}
|
|
735
|
-
function emitSdkEvent(event) {
|
|
736
|
-
if (listeners.size === 0) return;
|
|
737
|
-
for (const listener of listeners) {
|
|
738
|
-
try {
|
|
739
|
-
listener(event);
|
|
740
|
-
} catch {
|
|
741
|
-
}
|
|
742
|
-
}
|
|
743
|
-
}
|
|
744
|
-
function emitLog(level, ...args) {
|
|
745
|
-
if (listeners.size === 0) return;
|
|
746
|
-
const message = args.map((a) => typeof a === "string" ? a : safeStringify(a)).join(" ");
|
|
747
|
-
emitSdkEvent({ type: "log", level, message });
|
|
748
|
-
}
|
|
749
|
-
function safeStringify(value) {
|
|
750
|
-
if (value instanceof Error) {
|
|
751
|
-
return value.stack ? `${value.message}
|
|
752
|
-
${value.stack}` : value.message;
|
|
753
|
-
}
|
|
754
|
-
try {
|
|
755
|
-
return JSON.stringify(value);
|
|
756
|
-
} catch {
|
|
757
|
-
return String(value);
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
|
-
|
|
761
|
-
// src/utils/debugLog.ts
|
|
762
|
-
function debugLog(...args) {
|
|
763
|
-
emitLog("debug", ...args);
|
|
764
|
-
}
|
|
765
|
-
function debugError(...args) {
|
|
766
|
-
emitLog("error", ...args);
|
|
767
|
-
}
|
|
768
|
-
|
|
769
786
|
// src/adapter/LedgerAdapter.ts
|
|
770
787
|
function formatDeviceMismatchError(expected, actual) {
|
|
771
788
|
return `Wrong device: expected ${expected}, got ${actual}`;
|
|
@@ -924,30 +941,42 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
924
941
|
// Device management
|
|
925
942
|
// ---------------------------------------------------------------------------
|
|
926
943
|
async searchDevices(options) {
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
const devices = await this.connector.searchDevices();
|
|
936
|
-
this._discoveredDevices.clear();
|
|
937
|
-
for (const d of devices) {
|
|
938
|
-
if (d.connectId) {
|
|
939
|
-
this._discoveredDevices.set(d.connectId, this.connectorDeviceToDeviceInfo(d));
|
|
944
|
+
debugLog("[LedgerAdapter][REQ]", { method: "searchDevices", params: options });
|
|
945
|
+
try {
|
|
946
|
+
if (options?.resetSession) {
|
|
947
|
+
this._doConnectAbortController?.abort();
|
|
948
|
+
this._sessions.clear();
|
|
949
|
+
this._connectingPromise = null;
|
|
950
|
+
this._doConnectAbortController = null;
|
|
951
|
+
this._btcHighIndexConfirmedThisSession = false;
|
|
940
952
|
}
|
|
941
|
-
}
|
|
942
|
-
if (this._discoveredDevices.size === 0) {
|
|
943
953
|
await this._ensureDevicePermission();
|
|
954
|
+
const devices = await this.connector.searchDevices();
|
|
955
|
+
this._discoveredDevices.clear();
|
|
956
|
+
for (const d of devices) {
|
|
957
|
+
if (d.connectId) {
|
|
958
|
+
this._discoveredDevices.set(d.connectId, this.connectorDeviceToDeviceInfo(d));
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
if (this._discoveredDevices.size === 0) {
|
|
962
|
+
await this._ensureDevicePermission();
|
|
963
|
+
}
|
|
964
|
+
const result = Array.from(this._discoveredDevices.values());
|
|
965
|
+
debugLog("[LedgerAdapter][RES]", {
|
|
966
|
+
method: "searchDevices",
|
|
967
|
+
success: true,
|
|
968
|
+
payload: result
|
|
969
|
+
});
|
|
970
|
+
return result;
|
|
971
|
+
} catch (err) {
|
|
972
|
+
const e = err;
|
|
973
|
+
debugLog("[LedgerAdapter][RES]", {
|
|
974
|
+
method: "searchDevices",
|
|
975
|
+
success: false,
|
|
976
|
+
error: { message: e?.message, _tag: e?._tag, code: e?.code ?? e?.errorCode }
|
|
977
|
+
});
|
|
978
|
+
throw err;
|
|
944
979
|
}
|
|
945
|
-
debugLog(
|
|
946
|
-
`[LedgerAdapter] searchDevices() return count=${this._discoveredDevices.size} ids=[${[
|
|
947
|
-
...this._discoveredDevices.keys()
|
|
948
|
-
].join(",")}]`
|
|
949
|
-
);
|
|
950
|
-
return Array.from(this._discoveredDevices.values());
|
|
951
980
|
}
|
|
952
981
|
// USB single-session invariant: evict all sessions, best-effort (see connectDevice).
|
|
953
982
|
async _evictAllSessions() {
|
|
@@ -967,6 +996,7 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
967
996
|
});
|
|
968
997
|
}
|
|
969
998
|
async connectDevice(connectId) {
|
|
999
|
+
debugLog("[LedgerAdapter][REQ]", { method: "connectDevice", connectId, params: { connectId } });
|
|
970
1000
|
try {
|
|
971
1001
|
if (isLedgerBleConnectionType(this.connector.connectionType) && !connectId) {
|
|
972
1002
|
throw Object.assign(new Error("Ledger BLE connectId is required."), {
|
|
@@ -982,28 +1012,79 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
982
1012
|
if (session.deviceInfo) {
|
|
983
1013
|
this._discoveredDevices.set(connectId, session.deviceInfo);
|
|
984
1014
|
}
|
|
985
|
-
|
|
1015
|
+
const result = (0, import_hwk_adapter_core3.success)(connectId);
|
|
1016
|
+
debugLog("[LedgerAdapter][RES]", { method: "connectDevice", success: true, payload: result });
|
|
1017
|
+
return result;
|
|
986
1018
|
} catch (err) {
|
|
987
|
-
|
|
1019
|
+
const failureResult = this.errorToFailure(err);
|
|
1020
|
+
debugLog("[LedgerAdapter][RES]", {
|
|
1021
|
+
method: "connectDevice",
|
|
1022
|
+
success: false,
|
|
1023
|
+
payload: failureResult
|
|
1024
|
+
});
|
|
1025
|
+
return failureResult;
|
|
988
1026
|
}
|
|
989
1027
|
}
|
|
990
1028
|
async disconnectDevice(connectId) {
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
1029
|
+
debugLog("[LedgerAdapter][REQ]", {
|
|
1030
|
+
method: "disconnectDevice",
|
|
1031
|
+
connectId,
|
|
1032
|
+
params: { connectId }
|
|
1033
|
+
});
|
|
1034
|
+
try {
|
|
1035
|
+
const sessionId = this._sessions.get(connectId);
|
|
1036
|
+
if (sessionId) {
|
|
1037
|
+
await this.connector.disconnect(sessionId);
|
|
1038
|
+
this._sessions.delete(connectId);
|
|
1039
|
+
}
|
|
1040
|
+
debugLog("[LedgerAdapter][RES]", { method: "disconnectDevice", success: true });
|
|
1041
|
+
} catch (err) {
|
|
1042
|
+
const e = err;
|
|
1043
|
+
debugLog("[LedgerAdapter][RES]", {
|
|
1044
|
+
method: "disconnectDevice",
|
|
1045
|
+
success: false,
|
|
1046
|
+
error: { message: e?.message, _tag: e?._tag, code: e?.code ?? e?.errorCode }
|
|
1047
|
+
});
|
|
1048
|
+
throw err;
|
|
995
1049
|
}
|
|
996
1050
|
}
|
|
997
1051
|
async getDeviceInfo(connectId, deviceId) {
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1052
|
+
debugLog("[LedgerAdapter][REQ]", {
|
|
1053
|
+
method: "getDeviceInfo",
|
|
1054
|
+
connectId,
|
|
1055
|
+
params: { connectId, deviceId }
|
|
1056
|
+
});
|
|
1057
|
+
try {
|
|
1058
|
+
await this._ensureDevicePermission(connectId, deviceId);
|
|
1059
|
+
const cached = this._discoveredDevices.get(connectId) ?? Array.from(this._discoveredDevices.values()).find((d) => d.deviceId === deviceId);
|
|
1060
|
+
if (cached) {
|
|
1061
|
+
const result = (0, import_hwk_adapter_core3.success)(cached);
|
|
1062
|
+
debugLog("[LedgerAdapter][RES]", {
|
|
1063
|
+
method: "getDeviceInfo",
|
|
1064
|
+
success: true,
|
|
1065
|
+
payload: result
|
|
1066
|
+
});
|
|
1067
|
+
return result;
|
|
1068
|
+
}
|
|
1069
|
+
const notFound = (0, import_hwk_adapter_core3.failure)(
|
|
1070
|
+
import_hwk_adapter_core3.HardwareErrorCode.DeviceNotFound,
|
|
1071
|
+
"Device not found in cache. Call searchDevices() or wait for a device-connected event first."
|
|
1072
|
+
);
|
|
1073
|
+
debugLog("[LedgerAdapter][RES]", {
|
|
1074
|
+
method: "getDeviceInfo",
|
|
1075
|
+
success: false,
|
|
1076
|
+
payload: notFound
|
|
1077
|
+
});
|
|
1078
|
+
return notFound;
|
|
1079
|
+
} catch (err) {
|
|
1080
|
+
const e = err;
|
|
1081
|
+
debugLog("[LedgerAdapter][RES]", {
|
|
1082
|
+
method: "getDeviceInfo",
|
|
1083
|
+
success: false,
|
|
1084
|
+
error: { message: e?.message, _tag: e?._tag, code: e?.code ?? e?.errorCode }
|
|
1085
|
+
});
|
|
1086
|
+
throw err;
|
|
1002
1087
|
}
|
|
1003
|
-
return (0, import_hwk_adapter_core3.failure)(
|
|
1004
|
-
import_hwk_adapter_core3.HardwareErrorCode.DeviceNotFound,
|
|
1005
|
-
"Device not found in cache. Call searchDevices() or wait for a device-connected event first."
|
|
1006
|
-
);
|
|
1007
1088
|
}
|
|
1008
1089
|
getSupportedChains() {
|
|
1009
1090
|
return ["evm", "btc", "sol", "tron"];
|
|
@@ -1671,26 +1752,42 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1671
1752
|
return this._unwrapConnectorResult(result);
|
|
1672
1753
|
}
|
|
1673
1754
|
async connectorCall(connectId, method, params, fingerprint, permissionDeviceId, commonParams, installContext) {
|
|
1674
|
-
debugLog("[LedgerAdapter]
|
|
1755
|
+
debugLog("[LedgerAdapter][REQ]", { method, connectId: connectId || "(empty)", params });
|
|
1675
1756
|
const queueKey = connectId || "__ledger_default__";
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1757
|
+
try {
|
|
1758
|
+
const result = await this._jobQueue.enqueue(
|
|
1759
|
+
queueKey,
|
|
1760
|
+
async (signal) => this._runConnectorCall(
|
|
1761
|
+
connectId,
|
|
1762
|
+
method,
|
|
1763
|
+
params,
|
|
1764
|
+
signal,
|
|
1765
|
+
fingerprint,
|
|
1766
|
+
permissionDeviceId,
|
|
1767
|
+
commonParams,
|
|
1768
|
+
installContext
|
|
1769
|
+
),
|
|
1770
|
+
{
|
|
1771
|
+
label: method,
|
|
1772
|
+
rejectIfBusy: true,
|
|
1773
|
+
busyError: _LedgerAdapter._createDeviceBusyError(method)
|
|
1774
|
+
}
|
|
1775
|
+
);
|
|
1776
|
+
debugLog("[LedgerAdapter][RES]", { method, success: true, payload: result });
|
|
1777
|
+
return result;
|
|
1778
|
+
} catch (err) {
|
|
1779
|
+
const e = err;
|
|
1780
|
+
debugLog("[LedgerAdapter][RES]", {
|
|
1680
1781
|
method,
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
)
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
rejectIfBusy: true,
|
|
1691
|
-
busyError: _LedgerAdapter._createDeviceBusyError(method)
|
|
1692
|
-
}
|
|
1693
|
-
);
|
|
1782
|
+
success: false,
|
|
1783
|
+
error: {
|
|
1784
|
+
message: e?.message,
|
|
1785
|
+
_tag: e?._tag,
|
|
1786
|
+
code: e?.code ?? e?.errorCode
|
|
1787
|
+
}
|
|
1788
|
+
});
|
|
1789
|
+
throw err;
|
|
1790
|
+
}
|
|
1694
1791
|
}
|
|
1695
1792
|
/**
|
|
1696
1793
|
* Race a promise against an abort signal. On abort, rejects with
|
|
@@ -1794,14 +1891,6 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1794
1891
|
}
|
|
1795
1892
|
const appName = err?.appName ?? mapLedgerError(err).appName;
|
|
1796
1893
|
if (appName) {
|
|
1797
|
-
if (installContext?.declinedAppNames?.has(appName)) {
|
|
1798
|
-
throw (0, import_hwk_adapter_core3.createHwkError)({
|
|
1799
|
-
code: import_hwk_adapter_core3.HardwareErrorCode.UserAborted,
|
|
1800
|
-
message: `User declined to install ${appName}`,
|
|
1801
|
-
_tag: ERROR_TAG.UserAborted,
|
|
1802
|
-
appName
|
|
1803
|
-
});
|
|
1804
|
-
}
|
|
1805
1894
|
if (installContext?.installAttemptedAppNames?.has(appName)) {
|
|
1806
1895
|
throw (0, import_hwk_adapter_core3.createHwkError)({
|
|
1807
1896
|
code: import_hwk_adapter_core3.HardwareErrorCode.AppNotInstalled,
|
|
@@ -1812,10 +1901,6 @@ var _LedgerAdapter = class _LedgerAdapter {
|
|
|
1812
1901
|
}
|
|
1813
1902
|
const confirmed = await this._waitForInstallAppConfirm(appName);
|
|
1814
1903
|
if (!confirmed) {
|
|
1815
|
-
if (installContext) {
|
|
1816
|
-
installContext.declinedAppNames = installContext.declinedAppNames ?? /* @__PURE__ */ new Set();
|
|
1817
|
-
installContext.declinedAppNames.add(appName);
|
|
1818
|
-
}
|
|
1819
1904
|
throw (0, import_hwk_adapter_core3.createHwkError)({
|
|
1820
1905
|
code: import_hwk_adapter_core3.HardwareErrorCode.UserAborted,
|
|
1821
1906
|
message: `User declined to install ${appName}`,
|