Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: clean? #15329

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/connect-iframe/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"private": true,
"scripts": {
"lint:js": "yarn g:eslint '**/*.{ts,tsx,js}'",
"build:iframe": "TS_NODE_PROJECT=\"tsconfig.json\" yarn webpack --config ./webpack/prod.webpack.config.ts --stats-children",
"build:iframe": "TS_NODE_PROJECT=\"tsconfig.json\" yarn webpack --config ./webpack/iframe.webpack.config.ts --stats-children",
"build:sessions": "TS_NODE_PROJECT=\"tsconfig.json\" yarn webpack --config ./webpack/sessions.webpack.config.ts --stats-children",
"build:core-module": "TS_NODE_PROJECT=\"tsconfig.json\" yarn webpack --config ./webpack/core.webpack.config.ts --stats-children",
"build": "yarn rimraf build && yarn build:iframe && yarn build:core-module",
"___NOTE__": "iframe build is one of the prerequisites of suite-web. build:lib script provides it together with other libraries",
Expand Down
9 changes: 7 additions & 2 deletions packages/connect-iframe/webpack/base.webpack.config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import path from 'path';
import { execSync } from 'child_process';
import webpack from 'webpack';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import TerserPlugin from 'terser-webpack-plugin';

import { version } from '../package.json';
import { getDistPathForProject } from './utils';

const COMMON_DATA_SRC = '../../packages/connect-common/files';
const MESSAGES_SRC = '../../packages/protobuf/messages.json';

const DIST = path.resolve(__dirname, '../build');
const project = process.env.PROJECT || 'iframe';

if (project !== 'iframe' && project !== 'suite-web') {
throw new Error(`Unsupported project: ${project}`);
}
const DIST = getDistPathForProject(project);

// Because of Expo EAS, we need to use the commit hash from expo to avoid failing git command inside EAS
// because we need to call `yarn build:libs during native build`
Expand Down
13 changes: 10 additions & 3 deletions packages/connect-iframe/webpack/core.webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ import webpack from 'webpack';
import merge from 'webpack-merge';

import baseConfig from './base.webpack.config';
import { getDistPathForProject } from './utils';

const DIST = path.resolve(__dirname, '../build');
const project = process.env.PROJECT || 'iframe';

if (project !== 'iframe' && project !== 'suite-web') {
throw new Error(`Unsupported project: ${project}`);
}
const DIST = getDistPathForProject(project);

export const config: webpack.Configuration = {
target: 'web',
Expand All @@ -15,15 +21,16 @@ export const config: webpack.Configuration = {
output: {
filename: 'js/[name].js',
path: DIST,
publicPath: './',
// TODO(karliatto)
publicPath: '/suite-web/feat/use-core-in-suite-web/web/',
// publicPath: '/',
library: {
type: 'module',
},
},
experiments: {
outputModule: true,
},
// todo: nx implicit dependencies
};

export default merge([config, baseConfig]);
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@ import HtmlWebpackPlugin from 'html-webpack-plugin';
import { config as baseConfig } from './base.webpack.config';

const DIST = path.resolve(__dirname, '../build');

const config: webpack.Configuration = {
// common instructions that are able to build correctly imports from @trezor/connect (reusing this in popup)
entry: {
iframe: path.resolve(__dirname, '../src/index.ts'),
['sessions-background-sharedworker']: {
filename: 'workers/[name].js',
import: path.resolve(
__dirname,
'../../transport/src/sessions/background-sharedworker.ts',
),
},
},
output: {
filename: 'js/[name].[contenthash].js',
Expand Down
33 changes: 33 additions & 0 deletions packages/connect-iframe/webpack/sessions.webpack.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import path from 'path';
import webpack from 'webpack';
import merge from 'webpack-merge';

import { config as baseConfig } from './base.webpack.config';
import { getSharedworkerDistPathForProject } from './utils';

const project = process.env.PROJECT || 'iframe';

if (project !== 'iframe' && project !== 'suite-web') {
throw new Error(`Unsupported project: ${project}`);
}
const DIST = getSharedworkerDistPathForProject(project);

const config: webpack.Configuration = {
// common instructions that are able to build correctly imports from @trezor/connect (reusing this in popup)
entry: {
['sessions-background-sharedworker']: {
filename: 'workers/[name].js',
import: path.resolve(
__dirname,
'../../transport/src/sessions/background-sharedworker.ts',
),
},
},
output: {
filename: 'js/[name].[contenthash].js',
path: DIST,
publicPath: './',
},
};

export default merge([config, baseConfig]);
28 changes: 28 additions & 0 deletions packages/connect-iframe/webpack/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import path from 'path';

type Project = 'iframe' | 'suite-web';

export const getDistPathForProject = (project: Project = 'iframe') => {
const basePath = path.join(__dirname, '..', '..');
switch (project) {
case 'iframe':
return path.join(basePath, 'connect-iframe', 'build');
case 'suite-web':
return path.join(basePath, 'suite-web', 'build');
default:
throw new Error('Missing project.');
}
};

// TODO: maybe sharedworker is not needed anymore.
export const getSharedworkerDistPathForProject = (project: Project = 'iframe') => {
const basePath = path.join(__dirname, '..', '..');
switch (project) {
case 'iframe':
return path.join(basePath, 'connect-iframe', 'build');
case 'suite-web':
return path.join(basePath, 'suite-web', 'build');
default:
throw new Error('Missing project.');
}
};
57 changes: 57 additions & 0 deletions packages/connect-web/src/module/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { factory } from '@trezor/connect/src/factory';
import { config } from '@trezor/connect/src/data/config';
import { TrezorConnectDynamic } from '@trezor/connect/src/impl/dynamic';
import { CoreInModule } from '@trezor/connect/src/impl/core-in-module';
import type { ConnectSettingsPublic } from '@trezor/connect/src/types';
import type { ConnectFactoryDependencies } from '@trezor/connect/src/factory';
import { ERRORS, TRANSPORT } from '@trezor/connect/src/exports';

const impl = new TrezorConnectDynamic<
'core-in-module',
ConnectSettingsPublic,
ConnectFactoryDependencies<ConnectSettingsPublic>
>({
implementations: [
{
type: 'core-in-module',
impl: new CoreInModule(),
},
],
getInitTarget: () => 'core-in-module',
handleErrorFallback: () => new Promise(resolve => resolve(false)),
});

const disableWebUSB = () => {
if (!impl.lastSettings) {
throw ERRORS.TypedError('Init_NotInitialized');
}

// @ts-ignore

Check failure on line 29 in packages/connect-web/src/module/index.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

Use "@ts-expect-error" to ensure an error is actually being suppressed
impl.handleCoreMessage({ type: TRANSPORT.DISABLE_WEBUSB });
};

const requestWebUSBDevice = async () => {
await window.navigator.usb.requestDevice({ filters: config.webusb });
// @ts-ignore

Check failure on line 35 in packages/connect-web/src/module/index.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

Use "@ts-expect-error" to ensure an error is actually being suppressed
impl.handleCoreMessage({ type: TRANSPORT.REQUEST_DEVICE });
};

const TrezorConnect = factory(
{
eventEmitter: impl.eventEmitter,
init: impl.init.bind(impl),
call: impl.call.bind(impl),
manifest: impl.manifest.bind(impl),
requestLogin: impl.requestLogin.bind(impl),
uiResponse: impl.uiResponse.bind(impl),
cancel: impl.cancel.bind(impl),
dispose: impl.dispose.bind(impl),
},
{
disableWebUSB: disableWebUSB.bind(impl),
requestWebUSBDevice: requestWebUSBDevice.bind(impl),
},
);

export default TrezorConnect;
export * from '@trezor/connect/src/exports';
3 changes: 2 additions & 1 deletion packages/connect-web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"include": [
".",
"../connect-iframe/webpack/base.webpack.config.ts",
"../connect-iframe/webpack/prod.webpack.config.ts",
"../connect-iframe/webpack/iframe.webpack.config.ts",
"../connect-iframe/webpack/utils.ts",
"../connect-iframe/package.json",
"../connect-popup/webpack/prod.webpack.config.ts",
"../connect-popup/package.json"
Expand Down
2 changes: 1 addition & 1 deletion packages/connect-web/webpack/dev.webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { WebpackPluginServe } from 'webpack-plugin-serve';
// todo: https://github.com/trezor/trezor-suite/issues/5305
import popup from '../../connect-popup/webpack/prod.webpack.config';
// todo: https://github.com/trezor/trezor-suite/issues/5305
import iframe from '../../connect-iframe/webpack/prod.webpack.config';
import iframe from '../../connect-iframe/webpack/iframe.webpack.config';
import prod from './prod.webpack.config';

const dev = {
Expand Down
79 changes: 32 additions & 47 deletions packages/connect/src/data/DataManager.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,33 @@
// origin: https://github.com/trezor/connect/blob/develop/src/js/data/DataManager.js

import { httpRequest } from '../utils/assets';
import { parseCoinsJson } from './coinInfo';

Check warning on line 3 in packages/connect/src/data/DataManager.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

`./coinInfo` import should occur after import of `@trezor/connect-common/files/firmware/t3w1/releases.json`
import { parseFirmware } from './firmwareInfo';

Check warning on line 4 in packages/connect/src/data/DataManager.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

`./firmwareInfo` import should occur after import of `@trezor/connect-common/files/firmware/t3w1/releases.json`
import { parseBridgeJSON } from './transportInfo';

Check warning on line 5 in packages/connect/src/data/DataManager.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

`./transportInfo` import should occur after import of `@trezor/connect-common/files/firmware/t3w1/releases.json`
import { ConnectSettings, DeviceModelInternal } from '../types';

Check warning on line 6 in packages/connect/src/data/DataManager.ts

View workflow job for this annotation

GitHub Actions / Linting and formatting

`../types` import should occur after import of `@trezor/connect-common/files/firmware/t3w1/releases.json`

type AssetCollection = { [key: string]: Record<string, any> };
// We need to declare those imports explicitly so webpack does not include the whole directories.
require('@trezor/connect-common/files/coins.json');
require('@trezor/connect-common/files/coins-eth.json');
require('@trezor/connect-common/files/bridge/releases.json');
require('@trezor/connect-common/files/firmware/t1b1/releases.json');
require('@trezor/connect-common/files/firmware/t2t1/releases.json');
require('@trezor/connect-common/files/firmware/t2b1/releases.json');
require('@trezor/connect-common/files/firmware/t3b1/releases.json');
require('@trezor/connect-common/files/firmware/t3t1/releases.json');
require('@trezor/connect-common/files/firmware/t3w1/releases.json');
require('@trezor/protobuf/messages.json');

import coins from '@trezor/connect-common/files/coins.json';
import coinsEth from '@trezor/connect-common/files/coins-eth.json';
import bridge from '@trezor/connect-common/files/bridge/releases.json';
import t1b1 from '@trezor/connect-common/files/firmware/t1b1/releases.json';
import t2t2 from '@trezor/connect-common/files/firmware/t2t1/releases.json';
import t2b1 from '@trezor/connect-common/files/firmware/t2b1/releases.json';
import t3b1 from '@trezor/connect-common/files/firmware/t3b1/releases.json';
import t3t1 from '@trezor/connect-common/files/firmware/t3t1/releases.json';
import t3w1 from '@trezor/connect-common/files/firmware/t3w1/releases.json';

const assets = [
{
name: 'coins',
url: './data/coins.json',
},
{
name: 'coinsEth',
url: './data/coins-eth.json',
},
{
name: 'bridge',
url: './data/bridge/releases.json',
},
{
name: 'firmware-t1b1',
url: './data/firmware/t1b1/releases.json',
},
{
name: 'firmware-t2t1',
url: './data/firmware/t2t1/releases.json',
},
{
name: 'firmware-t2b1',
url: './data/firmware/t2b1/releases.json',
},
{
name: 'firmware-t3b1',
url: './data/firmware/t3b1/releases.json',
},
{
name: 'firmware-t3t1',
url: './data/firmware/t3t1/releases.json',
},
{
name: 'firmware-t3tw1',
url: './data/firmware/t3w1/releases.json',
},
];
type AssetCollection = { [key: string]: Record<string, any> };

export class DataManager {
static assets: AssetCollection = {};
Expand All @@ -54,18 +36,21 @@
private static messages: Record<string, any>;

static async load(settings: ConnectSettings, withAssets = true) {
const ts = settings.env === 'web' ? `?r=${settings.timestamp}` : '';
this.settings = settings;

if (!withAssets) return;

const assetPromises = assets.map(async asset => {
const json = await httpRequest(`${asset.url}${ts}`, 'json');
this.assets[asset.name] = json;
});
await Promise.all(assetPromises);
this.assets['coins'] = coins;
this.assets['coinsEth'] = coinsEth;
this.assets['bridge'] = bridge;
this.assets['firmware-t1b1'] = t1b1;
this.assets['firmware-t2t1'] = t2t2;
this.assets['firmware-t2b1'] = t2b1;
this.assets['firmware-t3b1'] = t3b1;
this.assets['firmware-t3t1'] = t3t1;
this.assets['firmware-t3w1'] = t3w1;

this.messages = await httpRequest('./data/messages/messages.json', 'json');
this.messages = (await import('@trezor/protobuf/messages.json')).default;

// parse bridge JSON
parseBridgeJSON(this.assets.bridge);
Expand Down
Loading
Loading