DocumentationDevice interactionIntegration WalkthroughsHow to ...Build a custom command

Build a Custom Command

You can build your own command simply by extending the Command class and implementing the getApdu and parseResponse methods.

Then you can use the sendCommand method to send it to a connected device.

This is strongly recommended over direct usage of sendApdu.

Check the existing commands for a variety of 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