@zondax/ledger-js
Version:
TS / Node API for apps running on Ledger devices
104 lines (103 loc) • 4.87 kB
TypeScript
/// <reference types="node" />
/// <reference types="node" />
/******************************************************************************
* (c) 2018 - 2022 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
import type Transport from '@ledgerhq/hw-transport';
import { LedgerError } from './consts';
import { ResponsePayload } from './payload';
import { type ConstructorParams, type INSGeneric, type P1_VALUESGeneric, type ResponseAppInfo, type ResponseDeviceInfo, type ResponseVersion } from './types';
/**
* Base class for interacting with a Ledger device.
*/
export default class BaseApp {
readonly transport: Transport;
readonly CLA: number;
readonly INS: INSGeneric;
readonly P1_VALUES: P1_VALUESGeneric;
readonly REQUIRED_PATH_LENGTHS?: number[];
readonly CHUNK_SIZE: number;
readonly CUSTOM_APP_ERROR_DESCRIPTION?: Readonly<Record<LedgerError, string>>;
/**
* Constructs a new BaseApp instance.
* @param transport - The transport mechanism to communicate with the device.
* @param params - The constructor parameters.
*/
constructor(transport: Transport, params: ConstructorParams);
/**
* Serializes a derivation path into a buffer.
* @param path - The derivation path in string format.
* @returns A buffer representing the serialized path.
*/
serializePath(path: string): Buffer;
/**
* Prepares chunks of data to be sent to the device.
* @param path - The derivation path.
* @param message - The message to be sent.
* @returns An array of buffers that are ready to be sent.
*/
protected prepareChunks(path: string, message: Buffer): Buffer[];
/**
* Splits a buffer into chunks of `this.CHUNK_SIZE` size.
* @param message - The message to be chunked.
* @returns An array of buffers, each representing a chunk of the original message.
*/
protected messageToChunks(message: Buffer): Buffer[];
/**
* Sends a chunk of data to the device and handles the response.
* Determines the payload type based on the chunk index and sends the chunk to the device.
* Processes the response from the device.
*
* @param ins - The instruction byte.
* @param p2 - P2 parameter byte.
* @param chunkIdx - The current chunk index.
* @param chunkNum - The total number of chunks.
* @param chunk - The chunk data as a buffer.
* @returns A promise that resolves to the processed response from the device.
* @throws {ResponseError} If the response from the device indicates an error.
*/
protected sendGenericChunk(ins: number, p2: number, chunkIdx: number, chunkNum: number, chunk: Buffer): Promise<ResponsePayload>;
/**
* Sends a chunk of data to the device and handles the response.
* This method determines the payload type based on the chunk index and sends the chunk to the device.
* It then processes the response from the device.
*
* @param ins - The instruction byte.
* @param chunkIdx - The current chunk index.
* @param chunkNum - The total number of chunks.
* @param chunk - The chunk data as a buffer.
* @returns A promise that resolves to the processed response from the device.
* @throws {ResponseError} If the response from the device indicates an error.
*/
protected signSendChunk(ins: number, chunkIdx: number, chunkNum: number, chunk: Buffer): Promise<ResponsePayload>;
/**
* Retrieves the version information from the device.
* @returns A promise that resolves to the version information.
* @throws {ResponseError} If the response from the device indicates an error.
*/
getVersion(): Promise<ResponseVersion>;
/**
* Retrieves application information from the device.
* @returns A promise that resolves to the application information.
* @throws {ResponseError} If the response from the device indicates an error.
*/
appInfo(): Promise<ResponseAppInfo>;
/**
* Retrieves device information from the device.
* @returns A promise that resolves to the device information.
* @throws {ResponseError} If the response from the device indicates an error.
*/
deviceInfo(): Promise<ResponseDeviceInfo>;
}