UNPKG

@zondax/ledger-js

Version:

TS / Node API for apps running on Ledger devices

120 lines (119 loc) 4.82 kB
/// <reference types="node" /> /// <reference types="node" /> /** * Class representing a byte stream for reading and writing data. */ export declare class ByteStream { private readOffset; private writeOffset; protected internalBuffer: Buffer; constructor(buffer?: Buffer); /** * Writes a single byte (Uint8) to the buffer at the current write offset, then advances the write offset. * If the write offset is at the buffer's end, the buffer is expanded. * @param value The byte to write. */ appendUint8(value: number): void; /** * Writes a two-byte unsigned integer (Uint16) to the buffer at the current write offset in little-endian format, then advances the write offset. * If the write offset is at the buffer's end, the buffer is expanded. * @param value The two-byte unsigned integer to write. */ appendUint16(value: number): void; /** * Writes a four-byte unsigned integer (Uint32) to the buffer at the current write offset in little-endian format, then advances the write offset. * If the write offset is at the buffer's end, the buffer is expanded. * @param value The four-byte unsigned integer to write. */ appendUint32(value: number): void; /** * Writes an eight-byte unsigned integer (Uint64) to the buffer at the current write offset in little-endian format, then advances the write offset. * If the write offset is at the buffer's end, the buffer is expanded. * @param value The eight-byte unsigned integer to write. */ appendUint64(value: bigint): void; /** * Reads a specified number of bytes from the current read offset, then advances the read offset. * @param length The number of bytes to read. * @returns A buffer containing the read bytes. * @throws Error if attempting to read beyond the buffer length. */ readBytes(length: number): Buffer; /** * Reads a specified number of bytes from a given offset without changing the current read offset. * @param length The number of bytes to read. * @param offset The offset from which to read the bytes. * @returns A buffer containing the read bytes. * @throws Error if attempting to read beyond the buffer length. */ readBytesAt(length: number, offset: number): Buffer; /** * Writes data to the buffer at the current write offset, then advances the write offset. * If the data exceeds the buffer length, the buffer is expanded. * @param data The data to write. */ appendBytes(data: Buffer): void; /** * Inserts data into the buffer at the specified offset without changing the current write offset. * Expands the buffer if necessary. * @param data The data to insert. * @param offset The offset at which to insert the data. */ insertBytesAt(data: Buffer, offset: number): void; /** * Writes data to the buffer at the specified offset and advances the write offset from that point. * Expands the buffer if the data exceeds the buffer length. * @param data The data to write. * @param offset The offset at which to write the data. */ writeBytesAt(data: Buffer, offset: number): void; /** * Advances the current read offset by a specified number of bytes. * @param length The number of bytes to skip. * @throws Error if attempting to skip beyond the buffer length. */ skipBytes(length: number): void; clear(): void; /** * Resets the current read and write offsets to zero. */ resetOffset(): void; /** * Returns a new buffer containing all bytes of the internal buffer. */ getCompleteBuffer(): Buffer; /** * Returns a new buffer containing the bytes from the current read offset to the end of the internal buffer. */ getAvailableBuffer(): Buffer; /** * Returns the remaining length of the buffer from the current read offset. * @returns The remaining length of the buffer. */ length(): number; /** * Returns the total capacity of the internal buffer, irrespective of the current read or write offset. * @returns The total length of the internal buffer. */ capacity(): number; /** * Returns the current read offset. * @returns The current read offset. */ getReadOffset(): number; /** * Returns the current write offset. * @returns The current write offset. */ getWriteOffset(): number; /** * Sets the read offset to a specified value. * @param offset The new read offset. */ setReadOffset(offset: number): void; /** * Sets the write offset to a specified value. * @param offset The new write offset. */ setWriteOffset(offset: number): void; }