Skip to content

Commit

Permalink
WIP system info
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonlesisz committed Mar 15, 2023
1 parent 4809236 commit f28d327
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 31 deletions.
15 changes: 10 additions & 5 deletions packages/connect-iframe/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import { getOrigin } from '@trezor/connect/src/utils/urlUtils';
import { suggestBridgeInstaller } from '@trezor/connect/src/data/transportInfo';
import { suggestUdevInstaller } from '@trezor/connect/src/data/udevInfo';
import { storage } from '@trezor/connect-common';
import { browserUtils } from '@trezor/connect-browser-utils';

let _core: Core | undefined;

Expand Down Expand Up @@ -151,9 +150,8 @@ const postMessage = (message: CoreMessage) => {

if (message.event === TRANSPORT_EVENT) {
// add preferred bridge installer
const platform = browserUtils.getSuggestedPlatform();
message.payload.bridge = suggestBridgeInstaller(platform);
message.payload.udev = suggestUdevInstaller(platform);
message.payload.bridge = suggestBridgeInstaller(DataManager.systemInfo?.platform);
message.payload.udev = suggestUdevInstaller(DataManager.systemInfo?.platform);
}

if (usingPopup && targetUiEvent(message)) {
Expand Down Expand Up @@ -228,10 +226,17 @@ const init = async (payload: IFrameInit['payload'], origin: string) => {
_core = await initCore(parsedSettings);
_core.on(CORE_EVENT, postMessage);

const systemInfo: any = { os: 'linux' }; // TODO: call dom-utils

DataManager.systemInfo = systemInfo;

// initialize transport and wait for the first transport event (start or error)
await initTransport(parsedSettings);
postMessage(
createIFrameMessage(IFRAME.LOADED, { useBroadcastChannel: !!_popupMessagePort }),
createIFrameMessage(IFRAME.LOADED, {
useBroadcastChannel: !!_popupMessagePort,
systemInfo,
}),
);
} catch (error) {
postMessage(createIFrameMessage(IFRAME.ERROR, { error }));
Expand Down
10 changes: 4 additions & 6 deletions packages/connect-popup/src/view/browser.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// origin: https://github.com/trezor/connect/blob/develop/src/js/popup/view/browser.js

import { POPUP, createUiResponse } from '@trezor/connect';
import { config } from '@trezor/connect/src/data/config';
import { browserUtils } from '@trezor/connect-browser-utils';
import { storage } from '@trezor/connect-common';
import { container, showView, postMessage } from './common';
import { container, showView, postMessage, getState } from './common';

const validateBrowser = () => {
const state = browserUtils.getBrowserState(config.supportedBrowsers);
if (!state.supported) {
const { systemInfo } = getState();
if (!systemInfo?.supported) {
const permitted = storage.load().browser;
return !permitted ? state : null;
return !permitted ? systemInfo : null;
}
};

Expand Down
14 changes: 11 additions & 3 deletions packages/connect-popup/src/view/common.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// origin: https://github.com/trezor/connect/blob/develop/src/js/popup/view/common.js

import { POPUP, ERRORS, PopupInit, CoreMessage, ConnectSettings } from '@trezor/connect';
import {
POPUP,
ERRORS,
PopupInit,
CoreMessage,
ConnectSettings,
SystemInfo,
} from '@trezor/connect';
import React from 'react';
import { createRoot } from 'react-dom/client';

Expand All @@ -16,6 +23,7 @@ type State = {
settings?: ConnectSettings;
iframe?: Window;
broadcast?: BroadcastChannel;
systemInfo?: SystemInfo;
};

let state: State = {};
Expand Down Expand Up @@ -91,7 +99,7 @@ export const initMessageChannel = (
) => {
// settings received from window.opener (POPUP.INIT) are not considered as safe (they could be injected/modified)
// settings will be set later on, after POPUP.HANDSHAKE event from iframe
const { settings } = payload;
const { settings, systemInfo } = payload;
// npm version < 8.1.20 doesn't have it in POPUP.INIT message
const useBroadcastChannel =
typeof payload.useBroadcastChannel === 'boolean' ? payload.useBroadcastChannel : true;
Expand All @@ -113,7 +121,7 @@ export const initMessageChannel = (
channel.port1.onmessage = handler;
}

setState({ iframe, broadcast });
setState({ iframe, broadcast, systemInfo });
};

// this method can be used from anywhere
Expand Down
8 changes: 5 additions & 3 deletions packages/connect-popup/src/view/selectDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import TrezorConnect, {
} from '@trezor/connect';
import { SUITE_BRIDGE_URL, SUITE_UDEV_URL, TREZOR_SUPPORT_URL } from '@trezor/urls';
import { container, getState, showView, postMessage } from './common';
import { browserUtils } from '@trezor/connect-browser-utils';

const initWebUsbButton = (webusb: boolean, showLoader: boolean) => {
if (!webusb) return;
Expand Down Expand Up @@ -123,12 +122,15 @@ export const selectDevice = (payload: UiRequestSelectDevice['payload']) => {

// handle unreadable device
if (device.type === 'unreadable') {
const os = browserUtils.getOS();
const { systemInfo } = getState();
// default explanation: contact support

let explanationContent = `Please <a href="${TREZOR_SUPPORT_URL}" target="_blank" rel="noreferrer noopener" onclick="window.closeWindow();">contact support.</a>`;
// linux + LIBUSB_ERROR handling
if (os === 'linux' && device.error.indexOf(ERRORS.LIBUSB_ERROR_MESSAGE) >= 0) {
if (
systemInfo?.os === 'linux' &&
device.error.indexOf(ERRORS.LIBUSB_ERROR_MESSAGE) >= 0
) {
explanationContent = `Please install <a href="${SUITE_UDEV_URL}" target="_blank" rel="noreferrer noopener" onclick="window.closeWindow();">Udev rules</a> to use Trezor device.`;
}
// webusb error handling (top priority)
Expand Down
25 changes: 14 additions & 11 deletions packages/connect-web/src/popup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

import EventEmitter from 'events';
import { createDeferred, Deferred } from '@trezor/utils';
import { POPUP, IFRAME, UI, ConnectSettings, CoreMessage } from '@trezor/connect/lib/index';
import {
POPUP,
IFRAME,
UI,
ConnectSettings,
CoreMessage,
IFrameInit,
} from '@trezor/connect/lib/index';
import { getOrigin } from '@trezor/connect/lib/utils/urlUtils';
import { showPopupRequest } from './showPopupRequest';

Expand All @@ -26,7 +33,7 @@ export class PopupManager extends EventEmitter {

closeInterval = 0;

iframeHandshake: Deferred<boolean>;
iframeHandshake: Deferred<IFrameInit['payload']>;

popupPromise: Deferred<void> | undefined;

Expand Down Expand Up @@ -211,12 +218,12 @@ export class PopupManager extends EventEmitter {
if (this.popupPromise) {
this.popupPromise.resolve();
}
this.iframeHandshake.promise.then(useBroadcastChannel => {
this.iframeHandshake.promise.then(payload => {
port.postMessage({
type: POPUP.INIT,
payload: {
...payload,
settings: this.settings,
useBroadcastChannel,
},
});
});
Expand Down Expand Up @@ -251,11 +258,7 @@ export class PopupManager extends EventEmitter {
if (getOrigin(message.origin) !== this.origin || !data || typeof data !== 'object') return;

if (data.type === IFRAME.LOADED) {
const useBroadcastChannel =
data.payload && typeof data.payload.useBroadcastChannel === 'boolean'
? data.payload.useBroadcastChannel
: false;
this.iframeHandshake.resolve(useBroadcastChannel);
this.iframeHandshake.resolve(data.payload);
} else if (data.type === POPUP.BOOTSTRAP) {
// popup is opened properly, now wait for POPUP.LOADED message
if (this.openTimeout) clearTimeout(this.openTimeout);
Expand All @@ -269,13 +272,13 @@ export class PopupManager extends EventEmitter {
this.popupPromise.resolve();
}
// popup is successfully loaded
this.iframeHandshake.promise.then(useBroadcastChannel => {
this.iframeHandshake.promise.then(payload => {
this._window.postMessage(
{
type: POPUP.INIT,
payload: {
...payload,
settings: this.settings,
useBroadcastChannel,
},
},
this.origin,
Expand Down
12 changes: 11 additions & 1 deletion packages/connect/src/data/DataManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { parseFirmware } from './firmwareInfo';
import { parseBridgeJSON } from './transportInfo';
import { config } from './config';

import type { ConnectSettings } from '../types';
import type { ConnectSettings, SystemInfo } from '../types';

type AssetCollection = { [key: string]: JSON };

Expand Down Expand Up @@ -130,4 +130,14 @@ export class DataManager {
}
return this.settings;
}

private static _systemInfo?: SystemInfo;

static set systemInfo(info: SystemInfo | undefined) {
this._systemInfo = info;
}

static get systemInfo() {
return this._systemInfo;
}
}
3 changes: 2 additions & 1 deletion packages/connect/src/events/iframe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UI_EVENT } from './ui-request';
import type { ConnectSettings } from '../types';
import type { ConnectSettings, SystemInfo } from '../types';
import type { MessageFactoryFn } from '../types/utils';

export const IFRAME = {
Expand Down Expand Up @@ -27,6 +27,7 @@ export interface IFrameLoaded {
type: typeof IFRAME.LOADED;
payload: {
useBroadcastChannel: boolean;
systemInfo: SystemInfo;
};
}

Expand Down
3 changes: 2 additions & 1 deletion packages/connect/src/events/popup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UI_EVENT } from './ui-request';
import type { TransportInfo } from './transport';
import type { ConnectSettings } from '../types';
import type { ConnectSettings, SystemInfo } from '../types';
import type { MessageFactoryFn } from '../types/utils';

export const POPUP = {
Expand Down Expand Up @@ -33,6 +33,7 @@ export interface PopupInit {
payload: {
settings: ConnectSettings; // settings from window.opener (sent by @trezor/connect-web)
useBroadcastChannel: boolean;
systemInfo: SystemInfo;
};
}

Expand Down
10 changes: 10 additions & 0 deletions packages/connect/src/types/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,13 @@ export interface ConnectSettings {
timestamp: number;
proxy?: Proxy;
}

// partial UAParser/IResult
export interface SystemInfo {
supported: boolean;
platform: string;
os: {
name?: string;
version?: string;
};
}

0 comments on commit f28d327

Please sign in to comment.