chore(protocol): accept Buffer in decode function

This commit is contained in:
Szymon Lesisz
2024-07-29 16:23:06 +02:00
committed by martin
parent 2d6744abab
commit 858837bb2a
4 changed files with 6 additions and 11 deletions

View File

@@ -14,12 +14,11 @@ const readHeader = (buffer: Buffer) => {
};
export const decode: TransportProtocolDecode = bytes => {
const buffer = Buffer.from(bytes);
const { messageType, length } = readHeader(buffer);
const { messageType, length } = readHeader(bytes);
return {
messageType,
length,
payload: buffer.subarray(HEADER_SIZE),
payload: bytes.subarray(HEADER_SIZE),
};
};

View File

@@ -21,7 +21,6 @@ const readHeaderChunked = (buffer: Buffer) => {
};
// Parses first raw input that comes from Trezor and returns some information about the whole message.
// [compatibility]: accept Buffer just like decode does. But this would require changes in lower levels
export const decode: TransportProtocolDecode = bytes => {
// note: the occasionally appearing error "Attempt to access memory outside buffer bounds" comes from here in certain cases
// when usb.transferIn (read) did not receive any data but resolved with success. bytes has byteLength 0 in this case.
@@ -29,8 +28,7 @@ export const decode: TransportProtocolDecode = bytes => {
console.error('protocol-v1: decode: received empty buffer');
}
const buffer = Buffer.from(bytes);
const { magic, sharp1, sharp2, messageType, length } = readHeaderChunked(buffer);
const { magic, sharp1, sharp2, messageType, length } = readHeaderChunked(bytes);
if (
magic !== MESSAGE_MAGIC_HEADER_BYTE ||
@@ -44,6 +42,6 @@ export const decode: TransportProtocolDecode = bytes => {
return {
length,
messageType,
payload: buffer.subarray(HEADER_SIZE),
payload: bytes.subarray(HEADER_SIZE),
};
};

View File

@@ -1,4 +1,4 @@
export type TransportProtocolDecode = (bytes: ArrayBuffer) => {
export type TransportProtocolDecode = (bytes: Buffer) => {
length: number;
messageType: number | string;
payload: Buffer;

View File

@@ -61,9 +61,7 @@ export const createApi = (apiArg: 'usb' | 'udp' | AbstractApi, logger?: Log) =>
data: string;
signal: AbortSignal;
}) => {
const { messageType, payload } = protocolBridge.decode(
new Uint8Array(Buffer.from(data, 'hex')),
);
const { messageType, payload } = protocolBridge.decode(Buffer.from(data, 'hex'));
const encodedMessage = protocolV1.encode(payload, { messageType });
const chunks = createChunks(