Skip to content

Commit

Permalink
chore(transport): refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mroz22 committed Apr 16, 2023
1 parent 49b7c36 commit 54325ac
Show file tree
Hide file tree
Showing 61 changed files with 4,266 additions and 2,377 deletions.
2 changes: 1 addition & 1 deletion packages/connect-iframe/src/connectSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const parseConnectSettings = (
if (disableWebUsb) {
// allow all but WebUsbTransport
settings.transports = settings.transports?.filter(
(transport: string) => transport !== 'WebUsbTransport',
transport => transport !== 'WebUsbTransport',
);
}

Expand Down
5 changes: 2 additions & 3 deletions packages/connect-iframe/webpack/prod.webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ const config: webpack.Configuration = {
},
},
{
test: /sharedConnectionWorker/i,
test: (input: string) => input.includes('background-sharedworker'),
loader: 'worker-loader',
issuer: /workers\/workers-*/i, // replace import ONLY in /workers\/workers- not @trezor/transport
options: {
worker: 'SharedWorker',
filename: './workers/shared-connection-worker.[contenthash].js',
filename: './workers/sessions-background-sharedworker.[contenthash].js',
},
},
{
Expand Down
6 changes: 0 additions & 6 deletions packages/connect/src/constants/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ export const serializeError = (payload: any) => {
return payload;
};

// a slight hack
// this error string is hard-coded
// in both bridge and extension
export const WRONG_PREVIOUS_SESSION_ERROR_MESSAGE = 'wrong previous session';
export const INVALID_PIN_ERROR_MESSAGE = 'PIN invalid';
export const WEBUSB_ERROR_MESSAGE = 'NetworkError: Unable to claim interface.';
// trezord error prefix.
// user has insufficient permissions. may occur in Linux (missing udev rules), Windows and MacOS.
export const LIBUSB_ERROR_MESSAGE = 'LIBUSB_ERROR';
55 changes: 28 additions & 27 deletions packages/connect/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import EventEmitter from 'events';

import { TRANSPORT, TRANSPORT_ERROR } from '@trezor/transport';

import { DataManager } from '../data/DataManager';
import { DeviceList } from '../device/DeviceList';
import { enhancePostMessageWithAnalytics } from '../data/analyticsInfo';
Expand All @@ -12,7 +15,6 @@ import {
UI,
POPUP,
IFRAME,
TRANSPORT,
DEVICE,
createUiMessage,
createPopupMessage,
Expand Down Expand Up @@ -558,10 +560,8 @@ export const onCall = async (message: CoreMessage) => {
}
} catch (error) {
// catch wrong pin error
if (
error.message === ERRORS.INVALID_PIN_ERROR_MESSAGE &&
PIN_TRIES < MAX_PIN_TRIES
) {
// PinMatrixAck returns { code: "Failure_PinInvalid", message: "PIN invalid"}
if (error.message === 'PIN invalid' && PIN_TRIES < MAX_PIN_TRIES) {
PIN_TRIES++;
postMessage(
createUiMessage(UI.INVALID_PIN, { device: device.toMessageObject() }),
Expand Down Expand Up @@ -614,8 +614,8 @@ export const onCall = async (message: CoreMessage) => {
// thrown while acquiring device
// it's a race condition between two tabs
// workaround is to enumerate transport again and report changes to get a valid session number
if (_deviceList && error.message === ERRORS.WRONG_PREVIOUS_SESSION_ERROR_MESSAGE) {
_deviceList.enumerate();
if (_deviceList && error.message === TRANSPORT_ERROR.SESSION_WRONG_PREVIOUS) {
await _deviceList.enumerate();
}
messageResponse = createResponseMessage(method.responseID, false, { error });
}
Expand Down Expand Up @@ -644,7 +644,6 @@ export const onCall = async (message: CoreMessage) => {
method.dispose();
}

// restore default messages
if (_deviceList) {
if (response.success) {
_deviceList.removeAuthPenalty(device);
Expand Down Expand Up @@ -917,8 +916,9 @@ const initDeviceList = async (settings: ConnectSettings) => {
postMessage(createDeviceMessage(DEVICE.CHANGED, device));
});

_deviceList.on(TRANSPORT.ERROR, async error => {
_deviceList.on(TRANSPORT.ERROR, error => {
_log.warn('TRANSPORT.ERROR', error);

if (_deviceList) {
_deviceList.disconnectDevices();
_deviceList.dispose();
Expand All @@ -931,16 +931,17 @@ const initDeviceList = async (settings: ConnectSettings) => {
if (settings.transportReconnect) {
const { promise, timeout } = resolveAfter(1000, null);
_deviceListInitTimeout = timeout;
await promise;
initDeviceList(settings);
promise.then(() => {
initDeviceList(settings);
});
}
});

_deviceList.on(TRANSPORT.START, transportType =>
postMessage(createTransportMessage(TRANSPORT.START, transportType)),
);

await _deviceList.init();
_deviceList.init();
if (_deviceList) {
await _deviceList.waitForTransportFirstEvent();
}
Expand Down Expand Up @@ -984,16 +985,11 @@ export class Core extends EventEmitter {
return _callMethods;
}

getTransportInfo(): TransportInfo {
if (_deviceList) {
return _deviceList.getTransportInfo();
getTransportInfo(): TransportInfo | undefined {
if (!_deviceList) {
return undefined;
}

return {
type: '',
version: '',
outdated: true,
};
return _deviceList.getTransportInfo();
}
}

Expand All @@ -1009,7 +1005,7 @@ export const initCore = () => {

/**
* Module initialization.
* This will download the config.json, start DeviceList, init Core emitter instance.
* This will download the config.json, init Core emitter instance.
* Returns Core, an event emitter instance.
* @param {Object} settings - optional // TODO
* @returns {Promise<Core>}
Expand Down Expand Up @@ -1055,11 +1051,16 @@ const disableWebUSBTransport = async () => {
// override settings
const settings = DataManager.getSettings();

if (settings.transports?.includes('WebUsbTransport')) {
settings.transports.splice(settings.transports.indexOf('WebUsbTransport'));
}
if (!settings.transports?.includes('BridgeTransport')) {
settings.transports!.unshift('BridgeTransport');
if (settings.transports) {
const transportStr = settings.transports?.filter(
transport => typeof transport !== 'object',
);
if (transportStr.includes('WebUsbTransport')) {
settings.transports.splice(settings.transports.indexOf('WebUsbTransport'));
}
if (!transportStr.includes('BridgeTransport')) {
settings.transports!.unshift('BridgeTransport');
}
}

try {
Expand Down
4 changes: 2 additions & 2 deletions packages/connect/src/data/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// origin: https://github.com/trezor/connect/blob/develop/src/data/config.json

import { TREZOR_DESCS } from '@trezor/transport';
import { TREZOR_USB_DESCRIPTORS } from '@trezor/transport';

export const config = {
webusb: TREZOR_DESCS,
webusb: TREZOR_USB_DESCRIPTORS,
whitelist: [
{ origin: 'chrome-extension://imloifkgjagghnncjkhggdhalmcnfklk', priority: 1 },
{ origin: 'chrome-extension://niebkpllfhmpfbffbfifagfgoamhpflf', priority: 1 },
Expand Down
187 changes: 0 additions & 187 deletions packages/connect/src/device/DescriptorStream.ts

This file was deleted.

Loading

0 comments on commit 54325ac

Please sign in to comment.