Skip to content

Commit

Permalink
fix(connect-popup): systemInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
mroz22 committed Apr 20, 2023
1 parent c3fb23f commit 41b6bb4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/connect-popup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@trezor/connect-analytics": "workspace:*",
"@trezor/connect-ui": "workspace:*",
"@trezor/crypto-utils": "workspace:*",
"@trezor/env-utils": "workspace:*",
"@trezor/urls": "workspace:*",
"eth-phishing-detect": "^1.2.0"
},
Expand Down
40 changes: 39 additions & 1 deletion packages/connect-popup/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
PopupEvent,
PopupInit,
PopupHandshake,
SystemInfo,
} from '@trezor/connect';
import { config } from '@trezor/connect/lib/data/config';

import { reactEventBus } from '@trezor/connect-ui/src/utils/eventBus';
import { analytics, EventType } from '@trezor/connect-analytics';
Expand All @@ -24,6 +26,35 @@ import {
postMessage,
} from './view/common';
import { isPhishingDomain } from './utils/isPhishingDomain';
import { getBrowserVersion, getBrowserName, getDeviceType, getOsFamily } from '@trezor/env-utils';

// FIXME!!!
// this is copy paste hotfix from connect-iframe package
// why: systemInfo is sent in POPUP.INIT message from connect-web/src/popup. And this was added in 9.0.8.
// In case implementator has earlier version, systemInfo does not come. One of results is that popup displays
// outdated browser.
const getSystemInfo = (supportedBrowsers: { [key: string]: { version: number } }): SystemInfo => {
const browserName = getBrowserName();
const browserVersion = getBrowserVersion();
const supportedBrowser = browserName ? supportedBrowsers[browserName.toLowerCase()] : undefined;
const outdatedBrowser = supportedBrowser
? supportedBrowser.version > parseInt(browserVersion, 10)
: false;
const mobile = getDeviceType() === 'mobile';
const supportedMobile = mobile ? typeof navigator.usb !== 'undefined' : true;
const supported = !!(supportedBrowser && !outdatedBrowser && supportedMobile);

return {
os: {
family: getOsFamily(),
mobile,
},
browser: {
supported,
outdated: outdatedBrowser,
},
};
};

let handshakeTimeout: ReturnType<typeof setTimeout>;

Expand All @@ -43,6 +74,8 @@ const escapeHtml = (payload: any) => {
const handleMessage = (event: MessageEvent<PopupEvent | UiEvent>) => {
const { data } = event;

console.log('popup handle message', event.data);

if (!data) return;

// This is message from the window.opener
Expand Down Expand Up @@ -181,6 +214,9 @@ const handleMessage = (event: MessageEvent<PopupEvent | UiEvent>) => {
// handle POPUP.INIT message from window.opener
const init = async (payload: PopupInit['payload']) => {
if (!payload) return;

payload.systemInfo = getSystemInfo(config.supportedBrowsers);

try {
initMessageChannel(payload, handleMessage);
// reset loading hash
Expand Down Expand Up @@ -209,7 +245,9 @@ const handshake = (handshake: PopupHandshake) => {
clearTimeout(handshakeTimeout);

// use trusted settings from iframe
setState({ settings: payload.settings });
setState({
settings: payload.settings,
});

reactEventBus.dispatch(handshake);

Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7738,6 +7738,7 @@ __metadata:
"@trezor/connect-analytics": "workspace:*"
"@trezor/connect-ui": "workspace:*"
"@trezor/crypto-utils": "workspace:*"
"@trezor/env-utils": "workspace:*"
"@trezor/node-utils": "workspace:*"
"@trezor/trezor-user-env-link": "workspace:*"
"@trezor/urls": "workspace:*"
Expand Down

0 comments on commit 41b6bb4

Please sign in to comment.