DocumentationDevice interactionIntegration walkthroughsLedger Device Management KitBuild a custom command

Build a Custom Command

Method

To build your own command:

  1. Extend the Command class and implement the getApdu and parseResponse methods
  2. Use the the sendCommand method to send it to a connected device
ℹ️

This is strongly recommended over direct usage of sendApdu. Note : Explain why.

Examples

export class MyCustomCommand
  implements Command<MyCustomResponse, MyCustomCommandArgs>
{
   args: GetAddressCommandArgs;
 
  constructor(args: GetAddressCommandArgs) {
    this.args = args;
  }
 
  getApdu(): Apdu {
 
    // Main args for the APDU
    const getEthAddressArgs: ApduBuilderArgs = {
      cla: 0xe0, // Command CLA
      ins: 0x02, // Command INS
      p1: 0x00, //Parameter P1
      p2: 0x00, //Parameter P1
    };
 
    //Add the data attache to the APDU with the builder
    const builder = new ApduBuilder(getEthAddressArgs);
    builder.add32BitUIntToData(0);
    ... // Add more data to the APDU
    return builder.build();
  }
 
  parseResponse(
    response: ApduResponse,
  ): CommandResult<GetAddressCommandResponse> {
    //Create Apdu Parser
    const parser = new ApduParser(response);
 
    // FIRST: check status word
    if (!CommandUtils.isSuccessResponse(response)) {
      return CommandResultFactory({
        error: GlobalCommandErrorHandler.handle(response),
      });
    }
 
    // Extract fields from the response
    const customAttributes1 = parser.extract8BitUInt();
    if (customAttributes1 === undefined) {
      return CommandResultFactory({
        error: new InvalidStatusWordError("Public key length is missing"),
      });
    }
    ... // Extract more fields from the response
 
    return CommandResultFactory({
      MyCustomResponse: {
        customAttributes1,
        customAttributes2,
        ...
      },
    });
  }
}

Usage of ApduBuilder

Usage of ApduParser

Ledger
Copyright © Ledger SAS. All rights reserved. Ledger, Ledger Nano S, Ledger Vault, Ledger OS are registered trademarks of Ledger SAS