mirror of
https://github.com/trezor/trezor-suite.git
synced 2026-03-06 23:39:38 +01:00
fix(protocol): don't throw on malformed push notification
This commit is contained in:
@@ -1,9 +1,4 @@
|
||||
import {
|
||||
type DecodedTrezorPushNotification,
|
||||
TrezorPushNotificationMode,
|
||||
TrezorPushNotificationType,
|
||||
tpn,
|
||||
} from '@trezor/protocol';
|
||||
import { TrezorPushNotificationMode, TrezorPushNotificationType, tpn } from '@trezor/protocol';
|
||||
import { resolveAfter } from '@trezor/utils';
|
||||
|
||||
import { DEVICE } from '../../events/device';
|
||||
@@ -48,7 +43,12 @@ const setupDeviceMode = async (
|
||||
};
|
||||
|
||||
export const trezorPushNotificationHandler = async ({ device, message }: TpnWorkflowContext) => {
|
||||
const decoded: DecodedTrezorPushNotification = tpn.decode(message);
|
||||
const decodedResult = tpn.decode(message);
|
||||
|
||||
if (!decodedResult.success) return;
|
||||
|
||||
const decoded = decodedResult.payload;
|
||||
|
||||
device.lifecycle.emit(DEVICE.TREZOR_PUSH_NOTIFICATION, decoded);
|
||||
|
||||
const { type, mode } = decoded;
|
||||
|
||||
@@ -30,23 +30,20 @@ export interface DecodedTrezorPushNotification {
|
||||
mode: TrezorPushNotificationMode;
|
||||
}
|
||||
|
||||
export const decode = (message: number[]): DecodedTrezorPushNotification => {
|
||||
export const decode = (message: number[]) => {
|
||||
const [version, type, mode] = message;
|
||||
if (!version || version !== TPN_VERSION) {
|
||||
throw new Error(PROTOCOL_MISSMATCH_VERSION);
|
||||
return { success: false, error: PROTOCOL_MISSMATCH_VERSION } as const;
|
||||
}
|
||||
|
||||
if (
|
||||
message.length !== MESSAGE_LENGTH ||
|
||||
message.length < MESSAGE_LENGTH ||
|
||||
!Object.values(Version).includes(version) ||
|
||||
!Object.values(TrezorPushNotificationType).includes(type) ||
|
||||
!Object.values(TrezorPushNotificationMode).includes(mode)
|
||||
) {
|
||||
throw new Error(PROTOCOL_MALFORMED);
|
||||
return { success: false, error: PROTOCOL_MALFORMED } as const;
|
||||
}
|
||||
|
||||
return {
|
||||
type,
|
||||
mode,
|
||||
};
|
||||
return { success: true, payload: { type, mode } } as const;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user