import { type LedgerTransport } from '../types'; /** * The response returned by the Device Management Kit for a raw APDU exchange. * * Structurally identical to `ApduResponse` from `@ledgerhq/device-management-kit`. * It is redeclared here so that this package does not take a dependency on the DMK. */ export interface ApduResponseLike { readonly statusCode: Uint8Array; readonly data: Uint8Array; } /** * Arguments accepted by {@link DeviceManagementKitLike.sendApdu}. * * Structurally identical to `SendApduUseCaseArgs` from `@ledgerhq/device-management-kit`. */ export interface SendApduArgsLike { sessionId: string; apdu: Uint8Array; abortTimeout?: number; triggersDisconnection?: boolean; } /** * The subset of the Device Management Kit this adapter depends on. * * A real `DeviceManagementKit` instance satisfies this interface. Declaring the shape * structurally keeps `@ledgerhq/device-management-kit` out of this package's dependency * tree: consumers that use the DMK already have it, and consumers that do not are not * made to install it. */ export interface DeviceManagementKitLike { sendApdu(args: SendApduArgsLike): Promise; } /** * Options accepted when constructing a {@link DMKTransport}. */ export interface DMKTransportOptions { /** * Time, in milliseconds, to wait before aborting an exchange. Passed straight through * to the Device Management Kit. */ abortTimeout?: number; } /** * Error thrown when a device returns a status word that the caller did not accept. * * Mirrors `TransportStatusError` from `@ledgerhq/hw-transport`: `BaseApp` and the * downstream app SDKs recover the status word from the `statusCode` property, so the * two are interchangeable at the point where they are caught. */ export declare class DMKTransportStatusError extends Error { readonly statusCode: number; readonly statusText: string; constructor(statusCode: number); } /** * Error thrown when an app API call is attempted while another one is still in flight. * * Mirrors `TransportError(message, 'TransportLocked')` from `@ledgerhq/hw-transport` down to * `name` and `id`, not just the wording: callers that branch on those -- Ledger's own code * checks `id`, never the message text -- keep working when a DMK session is swapped in for a * hw-transport one. `TransportError` is also the name hw-transport's `deserializeError` * registers the class under, so the `id` survives a round trip across a worker boundary. */ export declare class DMKTransportLockedError extends Error { readonly id: string; constructor(message: string); } /** * Adapts a Device Management Kit session to the transport surface `BaseApp` expects. * * Ledger deprecated the `@ledgerhq/hw-transport` and `@ledgerhq/hw-app-*` packages ahead of * a September 2026 cutoff. The DMK replaces them, and for a device app that has no DMK signer * kit — which is every app this package serves — the supported path is to drive the device * with raw APDUs through `sendApdu`. That is exactly what `BaseApp` already does, so the whole * migration is this adapter: build the APDU, hand it to the DMK, and hand back the response in * the `Buffer`-with-status-word-appended form the rest of this package parses. * * @example * ```typescript * const dmk = new DeviceManagementKitBuilder().addTransport(webHidTransportFactory).build() * const sessionId = await dmk.connect({ * device, * // The DMK polls the device with GetAppAndVersion every second by default, through the * // same queue this adapter sends on. BaseApp awaits each chunk of a multi-APDU signing * // flow, so a poll can land between two chunks -- hw-transport never had background * // traffic. Disable it for the session, as Ledger Live does in its own DMK transport. * sessionRefresherOptions: { isRefresherDisabled: true }, * }) * * const app = new MyApp(new DMKTransport(dmk, sessionId)) * const version = await app.getVersion() * ``` */ export declare class DMKTransport implements LedgerTransport { private readonly dmk; private readonly sessionId; private readonly abortTimeout?; /** Name of the decorated method currently in flight, or null. See decorateAppAPIMethods. */ private appAPILock; /** * Constructs a transport bound to one Device Management Kit session. * * @param dmk - The Device Management Kit instance. * @param sessionId - The session identifier returned by `DeviceManagementKit.connect`. * @param options - Optional exchange settings. */ constructor(dmk: DeviceManagementKitLike, sessionId: string, options?: DMKTransportOptions); /** * Sends an APDU to the device and returns the raw response. * * @param cla - The instruction class. * @param ins - The instruction code. * @param p1 - The first parameter byte. * @param p2 - The second parameter byte. * @param data - The command data. Defaults to empty. * @param statusList - Status words to accept without throwing. Defaults to `[NoErrors]`. * @returns The response data with the two status word bytes appended, matching * `Transport.send` from `@ledgerhq/hw-transport`. * @throws {DMKTransportStatusError} If the device returns a status word not in `statusList`. */ send: (cla: number, ins: number, p1: number, p2: number, data?: Buffer, statusList?: number[], options?: { abortTimeoutMs?: number; }) => Promise; /** * Wraps each listed method so two app API calls cannot overlap on one device. * * This is not decoration for its own sake, and it is deliberately not a no-op. The * Device Management Kit does serialise traffic, but only per APDU: `sendApdu` enqueues * into a FIFO intent queue that runs one exchange at a time. hw-transport's lock is * coarser and stricter -- it spans an entire multi-APDU flow and *rejects* a second * caller rather than queueing it. * * That difference is observable. Given * * await Promise.all([eth.signTransaction(path, tx), eth.getAddress(path)]) * * hw-transport fails the second call immediately. A per-APDU queue instead interleaves * getAddress's exchange between two signing chunks, which the device sees as a foreign * APDU mid-flow. hw-app-eth's own source notes that the methods used *inside* the * signTransaction flow are deliberately left undecorated for exactly this reason. * * So the lock is reimplemented here rather than inherited. `hw-app-eth` calls this from * its constructor, which the EVM-adjacent SDKs (Flare, Peaq, Avalanche) invoke * unconditionally -- so without the method existing at all, merely constructing one of * those apps over a DMK session throws before a byte is sent. */ decorateAppAPIMethods: (self: Record, methods: string[], _scrambleKey: string) => void; /** * Returns `fn` wrapped so that only one decorated method may be in flight at a time. * Mirrors `Transport.decorateAppAPIMethod` from `@ledgerhq/hw-transport`, including * rejecting rather than queueing, and the wording and shape of its error. */ private lockAppAPIMethod; /** * No-op retained for compatibility with `@ledgerhq/hw-app-eth`. * * Scramble keys belong to the legacy HID/U2F framing that the Device Management Kit does * not use. The DMK addresses a device by session, so there is nothing to scramble. */ setScrambleKey: (_key: string) => void; }