@onekeyfe/hwk-ledger-adapter 1.1.26-alpha.100
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 +529 -0
- package/dist/index.d.ts +529 -0
- package/dist/index.js +2338 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2309 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +62 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,529 @@
|
|
|
1
|
+
import { IHardwareWallet, IConnector, TransportType, IUiHandler, DeviceInfo, Response, ChainCapability, EvmGetAddressParams, EvmAddress, ProgressCallback, EvmGetPublicKeyParams, EvmPublicKey, EvmSignTxParams, EvmSignedTx, EvmSignMsgParams, EvmSignature, EvmSignTypedDataParams, BtcGetAddressParams, BtcAddress, BtcGetPublicKeyParams, BtcPublicKey, BtcSignTxParams, BtcSignedTx, BtcSignMsgParams, BtcSignature, SolGetAddressParams, SolAddress, SolGetPublicKeyParams, SolPublicKey, SolSignTxParams, SolSignedTx, SolSignMsgParams, SolSignature, TronGetAddressParams, TronAddress, TronSignTxParams, TronSignedTx, TronSignMsgParams, TronSignature, HardwareEventMap, DeviceEventListener, ChainForFingerprint, ConnectionType, DeviceDescriptor, ConnectorDevice, ConnectorSession, ConnectorEventType, ConnectorEventMap, DeviceChangeEvent, HardwareErrorCode } from '@onekeyfe/hwk-adapter-core';
|
|
2
|
+
import { DeviceManagementKit, DeviceActionState as DeviceActionState$1, DiscoveredDevice, ExecuteDeviceActionReturnType } from '@ledgerhq/device-management-kit';
|
|
3
|
+
import { Address, Signature, SignerEth as SignerEth$1, TypedData } from '@ledgerhq/device-signer-kit-ethereum';
|
|
4
|
+
import { SignerBtc as SignerBtc$1 } from '@ledgerhq/device-signer-kit-bitcoin';
|
|
5
|
+
import { SignerSolana } from '@ledgerhq/device-signer-kit-solana';
|
|
6
|
+
import Transport from '@ledgerhq/hw-transport';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Ledger hardware wallet adapter that delegates to an IConnector.
|
|
10
|
+
*
|
|
11
|
+
* This is a thin translation layer that:
|
|
12
|
+
* - Accepts a pre-configured IConnector (transport decisions are made at connector creation time)
|
|
13
|
+
* - Translates IHardwareWallet method calls to connector.call() invocations
|
|
14
|
+
* - Maps connector results/errors to our Response<T> format with enriched error messages
|
|
15
|
+
* - Translates connector events to HardwareEventMap events
|
|
16
|
+
* - Integrates with IUiHandler for permission flows
|
|
17
|
+
*/
|
|
18
|
+
declare class LedgerAdapter implements IHardwareWallet {
|
|
19
|
+
readonly vendor: "ledger";
|
|
20
|
+
private readonly connector;
|
|
21
|
+
private readonly emitter;
|
|
22
|
+
private _uiHandler;
|
|
23
|
+
private _discoveredDevices;
|
|
24
|
+
private _sessions;
|
|
25
|
+
constructor(connector: IConnector);
|
|
26
|
+
get activeTransport(): TransportType | null;
|
|
27
|
+
getAvailableTransports(): TransportType[];
|
|
28
|
+
switchTransport(_type: TransportType): Promise<void>;
|
|
29
|
+
setUiHandler(handler: Partial<IUiHandler>): void;
|
|
30
|
+
init(_config?: unknown): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Clear cached device/session state without tearing down the adapter.
|
|
33
|
+
* Call before retrying after errors or when the device state may be stale.
|
|
34
|
+
* The next operation will re-discover and re-connect automatically.
|
|
35
|
+
*/
|
|
36
|
+
resetState(): void;
|
|
37
|
+
dispose(): Promise<void>;
|
|
38
|
+
searchDevices(): Promise<DeviceInfo[]>;
|
|
39
|
+
connectDevice(connectId: string): Promise<Response<string>>;
|
|
40
|
+
disconnectDevice(connectId: string): Promise<void>;
|
|
41
|
+
getDeviceInfo(connectId: string, deviceId: string): Promise<Response<DeviceInfo>>;
|
|
42
|
+
getSupportedChains(): ChainCapability[];
|
|
43
|
+
private callChain;
|
|
44
|
+
/**
|
|
45
|
+
* Batch version of callChain — checks permission and fingerprint once,
|
|
46
|
+
* then calls the connector for each param sequentially.
|
|
47
|
+
*/
|
|
48
|
+
private callChainBatch;
|
|
49
|
+
evmGetAddress(connectId: string, deviceId: string, params: EvmGetAddressParams): Promise<Response<EvmAddress>>;
|
|
50
|
+
evmGetAddresses(connectId: string, deviceId: string, params: EvmGetAddressParams[], onProgress?: ProgressCallback): Promise<Response<EvmAddress[]>>;
|
|
51
|
+
evmGetPublicKey(connectId: string, deviceId: string, params: EvmGetPublicKeyParams): Promise<Response<EvmPublicKey>>;
|
|
52
|
+
evmSignTransaction(connectId: string, deviceId: string, params: EvmSignTxParams): Promise<Response<EvmSignedTx>>;
|
|
53
|
+
evmSignMessage(connectId: string, deviceId: string, params: EvmSignMsgParams): Promise<Response<EvmSignature>>;
|
|
54
|
+
evmSignTypedData(connectId: string, deviceId: string, params: EvmSignTypedDataParams): Promise<Response<EvmSignature>>;
|
|
55
|
+
btcGetAddress(connectId: string, deviceId: string, params: BtcGetAddressParams): Promise<Response<BtcAddress>>;
|
|
56
|
+
btcGetAddresses(connectId: string, deviceId: string, params: BtcGetAddressParams[], onProgress?: ProgressCallback): Promise<Response<BtcAddress[]>>;
|
|
57
|
+
btcGetPublicKey(connectId: string, deviceId: string, params: BtcGetPublicKeyParams): Promise<Response<BtcPublicKey>>;
|
|
58
|
+
btcSignTransaction(connectId: string, deviceId: string, params: BtcSignTxParams): Promise<Response<BtcSignedTx>>;
|
|
59
|
+
btcSignMessage(connectId: string, deviceId: string, params: BtcSignMsgParams): Promise<Response<BtcSignature>>;
|
|
60
|
+
btcGetMasterFingerprint(connectId: string, deviceId: string): Promise<Response<{
|
|
61
|
+
masterFingerprint: string;
|
|
62
|
+
}>>;
|
|
63
|
+
solGetAddress(connectId: string, deviceId: string, params: SolGetAddressParams): Promise<Response<SolAddress>>;
|
|
64
|
+
solGetAddresses(connectId: string, deviceId: string, params: SolGetAddressParams[], onProgress?: ProgressCallback): Promise<Response<SolAddress[]>>;
|
|
65
|
+
solGetPublicKey(connectId: string, deviceId: string, params: SolGetPublicKeyParams): Promise<Response<SolPublicKey>>;
|
|
66
|
+
solSignTransaction(connectId: string, deviceId: string, params: SolSignTxParams): Promise<Response<SolSignedTx>>;
|
|
67
|
+
solSignMessage(connectId: string, deviceId: string, params: SolSignMsgParams): Promise<Response<SolSignature>>;
|
|
68
|
+
tronGetAddress(connectId: string, deviceId: string, params: TronGetAddressParams): Promise<Response<TronAddress>>;
|
|
69
|
+
tronGetAddresses(connectId: string, deviceId: string, params: TronGetAddressParams[], onProgress?: ProgressCallback): Promise<Response<TronAddress[]>>;
|
|
70
|
+
tronSignTransaction(connectId: string, deviceId: string, params: TronSignTxParams): Promise<Response<TronSignedTx>>;
|
|
71
|
+
tronSignMessage(connectId: string, deviceId: string, params: TronSignMsgParams): Promise<Response<TronSignature>>;
|
|
72
|
+
on<K extends keyof HardwareEventMap>(event: K, listener: (event: HardwareEventMap[K]) => void): void;
|
|
73
|
+
on(event: string, listener: DeviceEventListener): void;
|
|
74
|
+
off<K extends keyof HardwareEventMap>(event: K, listener: (event: HardwareEventMap[K]) => void): void;
|
|
75
|
+
off(event: string, listener: DeviceEventListener): void;
|
|
76
|
+
cancel(connectId: string): void;
|
|
77
|
+
getChainFingerprint(connectId: string, deviceId: string, chain: ChainForFingerprint): Promise<Response<string>>;
|
|
78
|
+
/**
|
|
79
|
+
* Verify that the connected device matches the expected fingerprint.
|
|
80
|
+
*
|
|
81
|
+
* - If deviceId is empty, verification is skipped (returns true).
|
|
82
|
+
* - deviceId is used here as the stored fingerprint to compare against.
|
|
83
|
+
*/
|
|
84
|
+
private _verifyDeviceFingerprint;
|
|
85
|
+
/**
|
|
86
|
+
* Derive an address at the fixed testnet path for fingerprint generation.
|
|
87
|
+
*/
|
|
88
|
+
private _deriveAddressForFingerprint;
|
|
89
|
+
/**
|
|
90
|
+
* Ensure at least one device is connected and return a valid connectId.
|
|
91
|
+
*
|
|
92
|
+
* - If a session already exists for the given connectId, reuse it.
|
|
93
|
+
* - If ANY session exists (Ledger IDs are ephemeral), reuse it.
|
|
94
|
+
* - Otherwise: search → 1 device: auto-connect, multiple: ask user, 0: throw.
|
|
95
|
+
*/
|
|
96
|
+
private static readonly MAX_DEVICE_RETRY;
|
|
97
|
+
private _deviceConnectResolve;
|
|
98
|
+
private _connectingPromise;
|
|
99
|
+
private static readonly DEVICE_CONNECT_TIMEOUT_MS;
|
|
100
|
+
/**
|
|
101
|
+
* Wait for user to connect and unlock device.
|
|
102
|
+
* Emits 'ui-request' event via the adapter's own emitter.
|
|
103
|
+
* The consumer (monorepo adapter wrapper) listens for this and shows UI.
|
|
104
|
+
* When user confirms, they call adapter.deviceConnectResponse() which resolves this promise.
|
|
105
|
+
* Times out after 60 seconds if no response is received.
|
|
106
|
+
*/
|
|
107
|
+
private _waitForDeviceConnect;
|
|
108
|
+
/**
|
|
109
|
+
* Called by consumer to respond to ui-request-device-connect.
|
|
110
|
+
* type='confirm' → retry search, type='cancel' → abort.
|
|
111
|
+
*/
|
|
112
|
+
deviceConnectResponse(type: 'confirm' | 'cancel'): void;
|
|
113
|
+
private ensureConnected;
|
|
114
|
+
private _doConnect;
|
|
115
|
+
private _connectFirstOrSelect;
|
|
116
|
+
/**
|
|
117
|
+
* Call the connector with automatic session resolution and disconnect retry.
|
|
118
|
+
*
|
|
119
|
+
* 1. Resolves a valid connectId via ensureConnected()
|
|
120
|
+
* 2. Looks up sessionId from _sessions
|
|
121
|
+
* 3. Calls connector.call()
|
|
122
|
+
* 4. On disconnect error: clears stale session, re-connects, retries once
|
|
123
|
+
*/
|
|
124
|
+
private connectorCall;
|
|
125
|
+
/** Clear stale session, reconnect, and retry the call. */
|
|
126
|
+
private _retryWithFreshConnection;
|
|
127
|
+
/**
|
|
128
|
+
* Ensure device permission before proceeding.
|
|
129
|
+
* - No connectId (searchDevices): check environment-level permission
|
|
130
|
+
* - With connectId (business methods): check device-level permission
|
|
131
|
+
* If not granted, calls onDevicePermission so the consumer can request access.
|
|
132
|
+
*/
|
|
133
|
+
private _ensureDevicePermission;
|
|
134
|
+
/**
|
|
135
|
+
* Convert a thrown error to a Response failure.
|
|
136
|
+
* Uses mapLedgerError to parse Ledger DMK error codes into HardwareErrorCode values.
|
|
137
|
+
*/
|
|
138
|
+
private errorToFailure;
|
|
139
|
+
private deviceConnectHandler;
|
|
140
|
+
private deviceDisconnectHandler;
|
|
141
|
+
private uiRequestHandler;
|
|
142
|
+
private uiEventHandler;
|
|
143
|
+
private registerEventListeners;
|
|
144
|
+
private unregisterEventListeners;
|
|
145
|
+
private handleUiEvent;
|
|
146
|
+
private connectorDeviceToDeviceInfo;
|
|
147
|
+
private extractDeviceInfoFromPayload;
|
|
148
|
+
private unknownDevice;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* A function that lazily loads and returns the transport factory
|
|
153
|
+
* for the Ledger DMK builder (e.g. webHidTransportFactory, rnBleTransportFactory).
|
|
154
|
+
*/
|
|
155
|
+
type TransportFactory = () => Promise<unknown>;
|
|
156
|
+
interface LedgerConnectorBaseOptions {
|
|
157
|
+
/**
|
|
158
|
+
* Pre-built DMK instance. If not provided, a DMK will be created
|
|
159
|
+
* lazily on first use via the transport factory.
|
|
160
|
+
*/
|
|
161
|
+
dmk?: DeviceManagementKit;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Shared base class for Ledger IConnector implementations.
|
|
165
|
+
*
|
|
166
|
+
* Encapsulates all shared logic: device discovery, connection management,
|
|
167
|
+
* method dispatch (EVM / BTC / SOL / TRON), signer lifecycle, event emission,
|
|
168
|
+
* and error handling.
|
|
169
|
+
*
|
|
170
|
+
* Chain-specific method implementations live in `./chains/` and receive
|
|
171
|
+
* a ConnectorContext that exposes shared helpers.
|
|
172
|
+
*
|
|
173
|
+
* Subclasses only need to:
|
|
174
|
+
* 1. Supply a transport factory via the constructor.
|
|
175
|
+
* 2. Optionally override `_resolveConnectId()` for transport-specific
|
|
176
|
+
* device identity resolution (e.g. BLE hex ID extraction).
|
|
177
|
+
*/
|
|
178
|
+
declare class LedgerConnectorBase implements IConnector {
|
|
179
|
+
private _deviceManager;
|
|
180
|
+
private _signerManager;
|
|
181
|
+
private _dmk;
|
|
182
|
+
private readonly _eventHandlers;
|
|
183
|
+
private readonly _providedDmk;
|
|
184
|
+
private readonly _createTransport;
|
|
185
|
+
private readonly _connectionType;
|
|
186
|
+
private _connectIdToPath;
|
|
187
|
+
private _pathToConnectId;
|
|
188
|
+
/** Register a connectId <-> path mapping from a device descriptor. */
|
|
189
|
+
private _registerDeviceId;
|
|
190
|
+
/** Get DMK path from external connectId. Falls back to connectId itself. */
|
|
191
|
+
private _getPathForConnectId;
|
|
192
|
+
/** Get external connectId from DMK path. Falls back to path itself. */
|
|
193
|
+
private _getConnectIdForPath;
|
|
194
|
+
/**
|
|
195
|
+
* Resolves a Ledger signer kit module by package name.
|
|
196
|
+
* Override via constructor to use CJS paths for Metro (React Native).
|
|
197
|
+
* Default: dynamic import with bare specifier (webpack/rspack).
|
|
198
|
+
*/
|
|
199
|
+
protected _importLedgerKit: (pkg: string) => Promise<any>;
|
|
200
|
+
/** Context object passed to per-chain handler functions. */
|
|
201
|
+
private readonly _ctx;
|
|
202
|
+
constructor(createTransport: TransportFactory, options?: {
|
|
203
|
+
connectionType?: ConnectionType;
|
|
204
|
+
dmk?: DeviceManagementKit;
|
|
205
|
+
/**
|
|
206
|
+
* Override how `@ledgerhq/device-signer-kit-*` packages are imported.
|
|
207
|
+
* Default: `(pkg) => import(pkg)` — works with webpack/rspack.
|
|
208
|
+
* For Metro (React Native): pass a resolver that uses CJS paths.
|
|
209
|
+
*/
|
|
210
|
+
importLedgerKit?: (pkg: string) => Promise<any>;
|
|
211
|
+
});
|
|
212
|
+
/**
|
|
213
|
+
* Resolve the connectId for a discovered device descriptor.
|
|
214
|
+
* Default: use the DMK path (ephemeral UUID).
|
|
215
|
+
* Override in subclasses to extract stable identifiers (e.g. BLE hex ID).
|
|
216
|
+
*/
|
|
217
|
+
protected _resolveConnectId(descriptor: DeviceDescriptor): string;
|
|
218
|
+
searchDevices(): Promise<ConnectorDevice[]>;
|
|
219
|
+
connect(deviceId?: string): Promise<ConnectorSession>;
|
|
220
|
+
disconnect(sessionId: string): Promise<void>;
|
|
221
|
+
call(sessionId: string, method: string, params: unknown): Promise<unknown>;
|
|
222
|
+
cancel(_sessionId: string): Promise<void>;
|
|
223
|
+
uiResponse(_response: {
|
|
224
|
+
type: string;
|
|
225
|
+
payload: unknown;
|
|
226
|
+
}): void;
|
|
227
|
+
on<K extends ConnectorEventType>(event: K, handler: (data: ConnectorEventMap[K]) => void): void;
|
|
228
|
+
off<K extends ConnectorEventType>(event: K, handler: (data: ConnectorEventMap[K]) => void): void;
|
|
229
|
+
reset(): void;
|
|
230
|
+
/**
|
|
231
|
+
* Lazily create or return the DMK instance.
|
|
232
|
+
* If a DMK was provided via constructor, it is used directly.
|
|
233
|
+
* Otherwise, one is created via the transport factory.
|
|
234
|
+
*/
|
|
235
|
+
protected _getOrCreateDmk(): Promise<DeviceManagementKit>;
|
|
236
|
+
private _initManagers;
|
|
237
|
+
private _getDeviceManager;
|
|
238
|
+
private _getSignerManager;
|
|
239
|
+
private _invalidateSession;
|
|
240
|
+
/**
|
|
241
|
+
* Replace an old session with a new one after app switch.
|
|
242
|
+
* Updates the connectId→path mapping so subsequent calls() use the new session,
|
|
243
|
+
* and emits device-connect so the adapter updates its _sessions Map.
|
|
244
|
+
*/
|
|
245
|
+
private _replaceSession;
|
|
246
|
+
/**
|
|
247
|
+
* Light reset: clear signer/session state but keep DMK, BLE scan, and ID mapping alive.
|
|
248
|
+
* Used by connect() retry — we want to re-discover with the same transport.
|
|
249
|
+
*/
|
|
250
|
+
private _resetSignersAndSessions;
|
|
251
|
+
private _resetAll;
|
|
252
|
+
protected _emit<K extends ConnectorEventType>(event: K, data: ConnectorEventMap[K]): void;
|
|
253
|
+
private _wrapError;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Manages device discovery, connection, and session tracking.
|
|
258
|
+
* Wraps DMK's Observable APIs into simpler imperative calls.
|
|
259
|
+
*/
|
|
260
|
+
declare class LedgerDeviceManager {
|
|
261
|
+
private readonly _dmk;
|
|
262
|
+
private readonly _discovered;
|
|
263
|
+
private readonly _sessions;
|
|
264
|
+
private readonly _sessionToDevice;
|
|
265
|
+
private _listenSub;
|
|
266
|
+
constructor(dmk: DeviceManagementKit);
|
|
267
|
+
/**
|
|
268
|
+
* One-shot enumeration: subscribe to listenToAvailableDevices,
|
|
269
|
+
* take the first emission, unsubscribe, return DeviceDescriptors.
|
|
270
|
+
*/
|
|
271
|
+
enumerate(): Promise<DeviceDescriptor[]>;
|
|
272
|
+
/**
|
|
273
|
+
* Continuous listening: tracks device connect/disconnect via diffing.
|
|
274
|
+
*/
|
|
275
|
+
listen(onChange: (event: DeviceChangeEvent) => void): void;
|
|
276
|
+
stopListening(): void;
|
|
277
|
+
/**
|
|
278
|
+
* Trigger browser device selection (WebHID requestDevice).
|
|
279
|
+
* Starts discovery for a short period, then stops.
|
|
280
|
+
*/
|
|
281
|
+
/**
|
|
282
|
+
* Start BLE discovery if not already running.
|
|
283
|
+
* Does NOT stop — DMK keeps scanning in the background so that
|
|
284
|
+
* listenToAvailableDevices() always has fresh data.
|
|
285
|
+
*/
|
|
286
|
+
private _discoverySub;
|
|
287
|
+
requestDevice(): Promise<void>;
|
|
288
|
+
/** Connect to a previously discovered device. Returns sessionId. */
|
|
289
|
+
connect(deviceId: string): Promise<string>;
|
|
290
|
+
/** Disconnect a session. */
|
|
291
|
+
disconnect(sessionId: string): Promise<void>;
|
|
292
|
+
getSessionId(deviceId: string): string | undefined;
|
|
293
|
+
getDeviceId(sessionId: string): string | undefined;
|
|
294
|
+
/** Get the underlying DMK instance (needed by SignerManager). */
|
|
295
|
+
getDmk(): DeviceManagementKit;
|
|
296
|
+
stopDiscovery(): void;
|
|
297
|
+
dispose(): void;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Re-export DMK types under local aliases for backward compatibility.
|
|
302
|
+
* These replace the previous duck-typed interfaces with the real SDK types.
|
|
303
|
+
*/
|
|
304
|
+
type DmkDiscoveredDevice = DiscoveredDevice;
|
|
305
|
+
/**
|
|
306
|
+
* DMK DeviceAction — the Observable-based return type of all DMK signer methods.
|
|
307
|
+
*
|
|
308
|
+
* This is a permissive alias for `ExecuteDeviceActionReturnType` that accepts
|
|
309
|
+
* any Error and IntermediateValue generics. Signer wrapper code only cares about
|
|
310
|
+
* the Output type and the observable/cancel shape.
|
|
311
|
+
*/
|
|
312
|
+
type DeviceAction<T> = ExecuteDeviceActionReturnType<T, any, any>;
|
|
313
|
+
/**
|
|
314
|
+
* DMK DeviceActionState — re-exported with permissive Error/IntermediateValue.
|
|
315
|
+
* The real type is a discriminated union on `status`.
|
|
316
|
+
*/
|
|
317
|
+
type DeviceActionState<T> = DeviceActionState$1<T, any, any>;
|
|
318
|
+
/** ETH address result — re-exported from DMK for backward compatibility. */
|
|
319
|
+
type SignerEvmAddress = Address;
|
|
320
|
+
/** ETH signature result — re-exported from DMK for backward compatibility. */
|
|
321
|
+
type SignerEvmSignature = Signature;
|
|
322
|
+
/** BTC address result (WalletAddress not exported from DMK top-level). */
|
|
323
|
+
interface SignerBtcAddress {
|
|
324
|
+
address: string;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Wraps Ledger's SDK signer (Observable-based DeviceActions) into
|
|
329
|
+
* a simple async interface returning plain serializable data.
|
|
330
|
+
*/
|
|
331
|
+
declare class SignerEth {
|
|
332
|
+
private readonly _sdk;
|
|
333
|
+
onInteraction?: (interaction: string) => void;
|
|
334
|
+
constructor(_sdk: SignerEth$1);
|
|
335
|
+
getAddress(derivationPath: string, options?: {
|
|
336
|
+
checkOnDevice?: boolean;
|
|
337
|
+
}): Promise<SignerEvmAddress>;
|
|
338
|
+
signTransaction(derivationPath: string, serializedTxHex: string): Promise<SignerEvmSignature>;
|
|
339
|
+
signMessage(derivationPath: string, message: string): Promise<SignerEvmSignature>;
|
|
340
|
+
signTypedData(derivationPath: string, data: TypedData): Promise<SignerEvmSignature>;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
type SignerEthBuilderFn = (args: {
|
|
344
|
+
dmk: DeviceManagementKit;
|
|
345
|
+
sessionId: string;
|
|
346
|
+
}) => {
|
|
347
|
+
build(): SignerEth$1;
|
|
348
|
+
} | Promise<{
|
|
349
|
+
build(): SignerEth$1;
|
|
350
|
+
}>;
|
|
351
|
+
/**
|
|
352
|
+
* Manages per-sessionId SignerEth instances.
|
|
353
|
+
* Creates on demand, caches for reuse, invalidates on session change.
|
|
354
|
+
*/
|
|
355
|
+
declare class SignerManager {
|
|
356
|
+
private readonly _cache;
|
|
357
|
+
private readonly _dmk;
|
|
358
|
+
private readonly _builderFn;
|
|
359
|
+
constructor(dmk: DeviceManagementKit, builderFn?: SignerEthBuilderFn);
|
|
360
|
+
getOrCreate(sessionId: string): Promise<SignerEth>;
|
|
361
|
+
invalidate(sessionId: string): void;
|
|
362
|
+
clearAll(): void;
|
|
363
|
+
private static _defaultBuilder;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
type BtcWallet = Parameters<SignerBtc$1['getWalletAddress']>[0];
|
|
367
|
+
type BtcPsbt = Parameters<SignerBtc$1['signPsbt']>[1];
|
|
368
|
+
type BtcPsbtOptions = Parameters<SignerBtc$1['signPsbt']>[2];
|
|
369
|
+
type BtcMessageOptions = Parameters<SignerBtc$1['signMessage']>[2];
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Wraps Ledger's BTC SDK signer (Observable-based DeviceActions) into
|
|
373
|
+
* a simple async interface returning plain serializable data.
|
|
374
|
+
*/
|
|
375
|
+
declare class SignerBtc {
|
|
376
|
+
private readonly _sdk;
|
|
377
|
+
onInteraction?: (interaction: string) => void;
|
|
378
|
+
constructor(_sdk: SignerBtc$1);
|
|
379
|
+
getWalletAddress(wallet: BtcWallet, addressIndex: number, options?: {
|
|
380
|
+
checkOnDevice?: boolean;
|
|
381
|
+
change?: boolean;
|
|
382
|
+
}): Promise<SignerBtcAddress>;
|
|
383
|
+
getExtendedPublicKey(derivationPath: string, options?: {
|
|
384
|
+
checkOnDevice?: boolean;
|
|
385
|
+
}): Promise<string>;
|
|
386
|
+
getMasterFingerprint(options?: {
|
|
387
|
+
skipOpenApp?: boolean;
|
|
388
|
+
}): Promise<Uint8Array>;
|
|
389
|
+
/**
|
|
390
|
+
* Sign a PSBT and return the array of partial signatures.
|
|
391
|
+
* The `wallet` param is a DefaultWallet or WalletPolicy instance.
|
|
392
|
+
* The `psbt` param can be a hex string, base64 string, or Uint8Array.
|
|
393
|
+
*/
|
|
394
|
+
signPsbt(wallet: BtcWallet, psbt: BtcPsbt, options?: BtcPsbtOptions): Promise<unknown[]>;
|
|
395
|
+
/**
|
|
396
|
+
* Sign a PSBT and return the fully extracted raw transaction as a hex string.
|
|
397
|
+
* Like signPsbt, but also finalises the PSBT and extracts the transaction.
|
|
398
|
+
*/
|
|
399
|
+
signTransaction(wallet: BtcWallet, psbt: BtcPsbt, options?: BtcPsbtOptions): Promise<string>;
|
|
400
|
+
/**
|
|
401
|
+
* Sign a message with the BTC app (BIP-137 / "Bitcoin Signed Message").
|
|
402
|
+
* Returns `{ r, s, v }` signature object.
|
|
403
|
+
*/
|
|
404
|
+
signMessage(derivationPath: string, message: string, options?: BtcMessageOptions): Promise<{
|
|
405
|
+
r: string;
|
|
406
|
+
s: string;
|
|
407
|
+
v: number;
|
|
408
|
+
}>;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
type SolTxOptions = Parameters<SignerSolana['signTransaction']>[2];
|
|
412
|
+
type SolMsgOptions = Parameters<SignerSolana['signMessage']>[2];
|
|
413
|
+
/**
|
|
414
|
+
* Wraps Ledger's Solana SDK signer (Observable-based DeviceActions) into
|
|
415
|
+
* a simple async interface returning plain serializable data.
|
|
416
|
+
*
|
|
417
|
+
* The Solana signer's `getAddress` returns a base58-encoded Ed25519 public key,
|
|
418
|
+
* which is also the Solana address.
|
|
419
|
+
*/
|
|
420
|
+
declare class SignerSol {
|
|
421
|
+
private readonly _sdk;
|
|
422
|
+
onInteraction?: (interaction: string) => void;
|
|
423
|
+
constructor(_sdk: SignerSolana);
|
|
424
|
+
/**
|
|
425
|
+
* Get the Solana address (base58-encoded Ed25519 public key) at the given derivation path.
|
|
426
|
+
*/
|
|
427
|
+
getAddress(derivationPath: string, options?: {
|
|
428
|
+
checkOnDevice?: boolean;
|
|
429
|
+
}): Promise<string>;
|
|
430
|
+
/**
|
|
431
|
+
* Sign a Solana transaction.
|
|
432
|
+
*/
|
|
433
|
+
signTransaction(derivationPath: string, transaction: Uint8Array, options?: SolTxOptions): Promise<Uint8Array>;
|
|
434
|
+
/**
|
|
435
|
+
* Sign a message with the Solana app.
|
|
436
|
+
* DMK returns { signature: string } for signMessage (unlike signTransaction which returns Uint8Array).
|
|
437
|
+
*/
|
|
438
|
+
signMessage(derivationPath: string, message: string | Uint8Array, options?: SolMsgOptions): Promise<{
|
|
439
|
+
signature: string;
|
|
440
|
+
}>;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
/**
|
|
444
|
+
* Transport adapter that bridges Ledger's legacy hw-app-* SDKs to DMK's sendApdu.
|
|
445
|
+
*
|
|
446
|
+
* Legacy SDKs (hw-app-trx, hw-app-xrp, etc.) require a Transport instance.
|
|
447
|
+
* This adapter wraps DMK's session-based sendApdu so legacy SDKs can run
|
|
448
|
+
* over DMK's transport layer without a separate USB/HID connection.
|
|
449
|
+
*
|
|
450
|
+
* Usage:
|
|
451
|
+
* const transport = new DmkTransport(dmk, sessionId);
|
|
452
|
+
* const trx = new Trx(transport);
|
|
453
|
+
* const address = await trx.getAddress("44'/195'/0'/0/0");
|
|
454
|
+
*/
|
|
455
|
+
declare class DmkTransport extends Transport {
|
|
456
|
+
private _dmk;
|
|
457
|
+
private _sessionId;
|
|
458
|
+
constructor(dmk: DeviceManagementKit, sessionId: string);
|
|
459
|
+
exchange(apdu: Buffer): Promise<Buffer>;
|
|
460
|
+
close(): Promise<void>;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Convert a DMK DeviceAction (Observable-based) into a Promise.
|
|
465
|
+
* Handles pending -> completed/error state transitions and interaction callbacks.
|
|
466
|
+
*
|
|
467
|
+
* @param timeoutMs Timeout in ms. Resets each time the Observable emits (device is alive).
|
|
468
|
+
* Pass 0 to disable. Default: 30s.
|
|
469
|
+
*/
|
|
470
|
+
declare function deviceActionToPromise<T>(action: DeviceAction<T>, onInteraction?: (interaction: string) => void, timeoutMs?: number): Promise<T>;
|
|
471
|
+
|
|
472
|
+
interface AppManagerOptions {
|
|
473
|
+
waitMs?: number;
|
|
474
|
+
maxRetries?: number;
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Orchestrates opening / closing Ledger on-device apps so that the
|
|
478
|
+
* correct signer application is running before any signing call.
|
|
479
|
+
*/
|
|
480
|
+
declare class AppManager {
|
|
481
|
+
private readonly _dmk;
|
|
482
|
+
private readonly _waitMs;
|
|
483
|
+
private readonly _maxRetries;
|
|
484
|
+
constructor(dmk: DeviceManagementKit, options?: AppManagerOptions);
|
|
485
|
+
/**
|
|
486
|
+
* Return the Ledger app name for a given chain ticker,
|
|
487
|
+
* or undefined if the chain is not supported.
|
|
488
|
+
*/
|
|
489
|
+
static getAppName(chain: string): string | undefined;
|
|
490
|
+
/**
|
|
491
|
+
* Ensure the target app is open on the device identified by `sessionId`.
|
|
492
|
+
*
|
|
493
|
+
* Flow:
|
|
494
|
+
* 1. Check the currently running app.
|
|
495
|
+
* 2. If it is already the target, return immediately.
|
|
496
|
+
* 3. If a different app is running (not dashboard), close it first.
|
|
497
|
+
* 4. Open the target app.
|
|
498
|
+
* 5. Poll until the device confirms the target app is running.
|
|
499
|
+
*/
|
|
500
|
+
/**
|
|
501
|
+
* @param onConfirmOnDevice Called after OpenAppCommand succeeds —
|
|
502
|
+
* the device is now showing a confirmation prompt to the user.
|
|
503
|
+
* NOT called if the app is already open or if the app is not installed.
|
|
504
|
+
*/
|
|
505
|
+
ensureAppOpen(sessionId: string, targetAppName: string, onConfirmOnDevice?: () => void): Promise<void>;
|
|
506
|
+
private _getCurrentApp;
|
|
507
|
+
private _openApp;
|
|
508
|
+
private _closeCurrentApp;
|
|
509
|
+
/**
|
|
510
|
+
* Poll the device until the expected app is reported as running,
|
|
511
|
+
* or throw after `_maxRetries` attempts.
|
|
512
|
+
*/
|
|
513
|
+
private _waitForApp;
|
|
514
|
+
private _isDashboard;
|
|
515
|
+
private _wait;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/** Check if an error (or any error in its chain) represents a locked Ledger device. */
|
|
519
|
+
declare function isDeviceLockedError(err: unknown): boolean;
|
|
520
|
+
/**
|
|
521
|
+
* Map a Ledger DMK error to a HardwareErrorCode and human-readable message
|
|
522
|
+
* with actionable recovery information for the caller.
|
|
523
|
+
*/
|
|
524
|
+
declare function mapLedgerError(err: unknown): {
|
|
525
|
+
code: HardwareErrorCode;
|
|
526
|
+
message: string;
|
|
527
|
+
};
|
|
528
|
+
|
|
529
|
+
export { AppManager, type DeviceActionState, type DmkDiscoveredDevice, DmkTransport, LedgerAdapter, LedgerConnectorBase, type LedgerConnectorBaseOptions, LedgerDeviceManager, SignerBtc, type SignerBtcAddress, SignerEth, type SignerEvmAddress, type SignerEvmSignature, SignerManager, SignerSol, type TransportFactory, deviceActionToPromise, isDeviceLockedError, mapLedgerError };
|