mirror of
https://github.com/trezor/trezor-suite.git
synced 2026-03-06 23:39:38 +01:00
chore(protocol): accept Buffer in decode function
This commit is contained in:
@@ -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),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -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),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
export type TransportProtocolDecode = (bytes: ArrayBuffer) => {
|
||||
export type TransportProtocolDecode = (bytes: Buffer) => {
|
||||
length: number;
|
||||
messageType: number | string;
|
||||
payload: Buffer;
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user