Device Management Kit
The device management kit is the entry point for all the other libraries related to it. As we wanted to make the project modular.
Main Features
- Discovering and connecting to Ledger devices via USB, through WebHID.
- Discovering and connecting to Ledger devices via BLE, through WebBLE.
- Observing the state of a connected device.
- Sending custom APDU commands to Ledger devices.
- Sending a set of pre-defined commands to Ledger devices.
- Get OS version
- Get app and version
- Open app
- Close app
- Get battery status
- Execute a flow of commands with DeviceAction.
[!NOTE]
At the moment we do not provide the possibility to distinguish two devices of the same model, via WebHID and to avoid connection to the same device twice.
Communicate with a Ledger device
The DMK is offering several ways to communicate with a Ledger device.
Send APDU
This method is not recommended for most of the use cases. We recommend using the Command or DeviceAction instead.
You can send APDU commands to the device using the sendApdu
method of the Device Management Kit instance (here dmk
) instance.
Parameters:
sessionId
: string - The session ID, identifier of the connection with a device.apdu
: UInt8Array - bytes array of data to be send to the device.
await dmk.sendApdu({ sessionId, apdu });
Commands
Commands are pre-defined actions that you can send to the device.
You can use the sendCommand
method of the dmk
instance to send a command to the device.
Parameters:
sessionId
: string - The session ID, which an identifier of the connection with a device.command
: Command - The command to be sent to the device.
import { OpenAppCommand } from "@ledgerhq/device-management-kit";
const command = new OpenAppCommand("Bitcoin"); // Open the Bitcoin app
await dmk.sendCommand({ sessionId, command });
Device Actions
Device actions are a set of commands that are executed in a sequence.
You can use the executeDeviceAction
method of the dmk
instance to execute a device action.
It is returning an observable that will emit different states of the action execution.
A device action is cancellable, you can cancel it by calling the cancel
function returned by the executeDeviceAction
method.
Parameters:
sessionId
: string - The session ID, which an identifier of the connection with a device.deviceAction
: DeviceAction - The DeviceAction to be sent to the device.
const openAppDeviceAction = new OpenAppDeviceAction({ appName: "Bitcoin" });
const { observable, cancel } = await dmk.executeDeviceAction({
sessionId,
openAppDeviceAction,
});
State Management
For each connected device, we are managing and providing a device state.
The different states are:
connected
: The device is connected.locked
: The device is locked. User needs to unlock it to perform operations.busy
: The device is busy, so not reachable.disconnected
: The device is disconnected.