Skip to content

Commit

Permalink
feat(suite-native): retry FW authenticity checks if stale
Browse files Browse the repository at this point in the history
  • Loading branch information
Lemonexe committed Mar 6, 2025
1 parent 22bb3b2 commit 091cb5e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 1 deletion.
3 changes: 3 additions & 0 deletions suite-native/app/src/hooks/useGlobalHooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
useDetectDeviceError,
useHandleDeviceConnection,
useReportDeviceCompromised,
useRetryFwAuthenticityChecks,
} from '@suite-native/device';
import { useRenderDeviceCompromisedBanner } from '@suite-native/module-authenticity-checks';
import { useConnectPopupNavigation } from '@suite-native/module-connect-popup';
Expand All @@ -21,4 +22,6 @@ export const useGlobalHooks = () => {
useDetectDeviceError();
useReportDeviceCompromised();
useRenderDeviceCompromisedBanner();

useRetryFwAuthenticityChecks();
};
1 change: 1 addition & 0 deletions suite-native/device/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@trezor/connect": "workspace:*",
"@trezor/device-utils": "workspace:*",
"@trezor/styles": "workspace:*",
"@trezor/type-utils": "workspace:*",
"@trezor/utils": "workspace:*",
"lottie-react-native": "^7.1.0",
"react": "18.2.0",
Expand Down
35 changes: 35 additions & 0 deletions suite-native/device/src/hooks/useRetryFwAuthenticityChecks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useEffect } from 'react';
import { useSelector } from 'react-redux';

import TrezorConnect, { FIRMWARE } from '@trezor/connect';
import { TimerId } from '@trezor/type-utils';
import { isArrayMember } from '@trezor/utils';

import { selectFirmwareRevisionCheckErrorIfEnabled } from '../selectors';

const REFRESH_INTERVAL = 10_000; // [ms]

export const useRetryFwAuthenticityChecks = () => {
const firmwareRevisionCheckError = useSelector(selectFirmwareRevisionCheckErrorIfEnabled);

const isRetriableError =
firmwareRevisionCheckError !== null &&
isArrayMember(firmwareRevisionCheckError, FIRMWARE.REVISION_CHECK_RETRIABLE_ERRORS);

useEffect(() => {
let timeoutHandle: TimerId;
const recheckFwRevision = () => {
if (isRetriableError) {
// it'd be useless to await the result; what interests us is the Device state that updates, and gets propagated into redux
TrezorConnect.runAuthenticityChecks().then();
timeoutHandle = setTimeout(recheckFwRevision, REFRESH_INTERVAL);
}
};

if (isRetriableError) {
timeoutHandle = setTimeout(recheckFwRevision, REFRESH_INTERVAL);
}

return () => clearTimeout(timeoutHandle);
}, [isRetriableError]);
};
1 change: 1 addition & 0 deletions suite-native/device/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './hooks/useHandleDeviceConnection';
export * from './hooks/useDetectDeviceError';
export * from './hooks/useReportDeviceConnectToAnalytics';
export * from './hooks/useReportDeviceCompromised';
export * from './hooks/useRetryFwAuthenticityChecks';
export * from './components/ConnectDeviceAnimation';
export * from './components/ConfirmOnTrezorImage';
export * from './components/ConnectorImage';
Expand Down
3 changes: 2 additions & 1 deletion suite-native/device/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
{ "path": "../../packages/connect" },
{ "path": "../../packages/device-utils" },
{ "path": "../../packages/styles" },
{ "path": "../../packages/utils" }
{ "path": "../../packages/utils" },
{ "path": "../../packages/type-utils" }
],
"include": [".", "**/*.json"]
}
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10287,6 +10287,7 @@ __metadata:
"@trezor/connect": "workspace:*"
"@trezor/device-utils": "workspace:*"
"@trezor/styles": "workspace:*"
"@trezor/type-utils": "workspace:*"
"@trezor/utils": "workspace:*"
lottie-react-native: "npm:^7.1.0"
react: "npm:18.2.0"
Expand Down

0 comments on commit 091cb5e

Please sign in to comment.