Skip to content

Commit

Permalink
chore(suite): separate popup logic from @suite-common/connect-init
Browse files Browse the repository at this point in the history
  • Loading branch information
martykan committed Dec 16, 2024
1 parent 0b11679 commit 9786cea
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 102 deletions.
1 change: 1 addition & 0 deletions packages/suite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@suite-common/analytics": "workspace:*",
"@suite-common/assets": "workspace:*",
"@suite-common/connect-init": "workspace:*",
"@suite-common/connect-popup": "workspace:*",
"@suite-common/device-authenticity": "workspace:*",
"@suite-common/fiat-services": "workspace:*",
"@suite-common/firmware": "workspace:*",
Expand Down
3 changes: 2 additions & 1 deletion packages/suite/src/actions/suite/initAction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { initMessageSystemThunk } from '@suite-common/message-system';
import * as trezorConnectActions from '@suite-common/connect-init';
import * as trezorConnectPopupActions from '@suite-common/connect-popup';
import {
initBlockchainThunk,
initDevices,
Expand Down Expand Up @@ -115,7 +116,7 @@ export const init = () => async (dispatch: Dispatch, getState: GetState) => {

// 14. init connect popup handler
if (isDesktop()) {
dispatch(trezorConnectActions.connectPopupInitThunk());
dispatch(trezorConnectPopupActions.connectPopupInitThunk());
}

// 15. backend connected, suite is ready to use
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { UI } from '@trezor/connect';
import { isDeviceAcquired } from '@suite-common/suite-utils';
import { DiscoveryStatus } from '@suite-common/wallet-constants';
import { createMiddlewareWithExtraDeps } from '@suite-common/redux-utils';
import { connectPopupCallThunk } from '@suite-common/connect-init';
import { connectPopupCallThunk } from '@suite-common/connect-popup';

import { SUITE, ROUTER, MODAL } from 'src/actions/suite/constants';
import * as walletSettingsActions from 'src/actions/settings/walletSettingsActions';
Expand Down
3 changes: 3 additions & 0 deletions packages/suite/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
{
"path": "../../suite-common/connect-init"
},
{
"path": "../../suite-common/connect-popup"
},
{
"path": "../../suite-common/device-authenticity"
},
Expand Down
1 change: 0 additions & 1 deletion suite-common/connect-init/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"@suite-common/wallet-core": "workspace:*",
"@trezor/connect": "workspace:*",
"@trezor/env-utils": "workspace:^",
"@trezor/suite-desktop-api": "workspace:*",
"@trezor/urls": "workspace:*",
"@trezor/utils": "workspace:*"
},
Expand Down
98 changes: 2 additions & 96 deletions suite-common/connect-init/src/connectInitThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ import TrezorConnect, {
BLOCKCHAIN_EVENT,
DEVICE,
DEVICE_EVENT,
ERRORS,
TRANSPORT_EVENT,
UI_EVENT,
} from '@trezor/connect';
import { DATA_URL } from '@trezor/urls';
import { createDeferred, getSynchronize } from '@trezor/utils';
import { deviceConnectThunks, selectDevice } from '@suite-common/wallet-core';
import { getSynchronize } from '@trezor/utils';
import { deviceConnectThunks } from '@suite-common/wallet-core';
import { isDesktop, isNative } from '@trezor/env-utils';
import { desktopApi } from '@trezor/suite-desktop-api';
import { serializeError } from '@trezor/connect/src/constants/errors';

import { cardanoConnectPatch } from './cardanoConnectPatch';

Expand Down Expand Up @@ -167,94 +164,3 @@ export const connectInitThunk = createThunk(
}
},
);

export const connectPopupCallThunk = createThunk(
`${CONNECT_INIT_MODULE}/callThunk`,
async (
{
id,
method,
payload,
processName,
origin,
}: {
id: number;
method: string;
payload: any;
processName?: string;
origin?: string;
},
{ dispatch, getState, extra },
) => {
try {
const device = selectDevice(getState());

if (!device) {
console.error('Device not found');

// TODO: wait for device selection and continue
throw ERRORS.TypedError('Device_NotFound');
}

// @ts-expect-error: method is dynamic
const methodInfo = await TrezorConnect[method]({
...payload,
__info: true,
});
if (!methodInfo.success) {
throw methodInfo;
}

const confirmation = createDeferred();
dispatch(extra.actions.lockDevice(true));
dispatch(
extra.actions.openModal({
type: 'connect-popup',
onCancel: () => confirmation.reject(ERRORS.TypedError('Method_Cancel')),
onConfirm: () => confirmation.resolve(),
method: methodInfo.payload.info,
processName,
origin,
}),
);
await confirmation.promise;
dispatch(extra.actions.lockDevice(false));

// @ts-expect-error: method is dynamic
const response = await TrezorConnect[method]({
device: {
path: device.path,
instance: device.instance,
state: device.state,
},
...payload,
});

dispatch(extra.actions.onModalCancel());

desktopApi.connectPopupResponse({
...response,
id,
});
} catch (error) {
console.error('connectPopupCallThunk', error);
desktopApi.connectPopupResponse({
success: false,
payload: serializeError(error),
id,
});
}
},
);

export const connectPopupInitThunk = createThunk(
`${CONNECT_INIT_MODULE}/initPopupThunk`,
async (_, { dispatch }) => {
if (desktopApi.available && (await desktopApi.connectPopupEnabled())) {
desktopApi.on('connect-popup/call', params => {
dispatch(connectPopupCallThunk(params));
});
desktopApi.connectPopupReady();
}
},
);
3 changes: 0 additions & 3 deletions suite-common/connect-init/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
{ "path": "../wallet-core" },
{ "path": "../../packages/connect" },
{ "path": "../../packages/env-utils" },
{
"path": "../../packages/suite-desktop-api"
},
{ "path": "../../packages/urls" },
{ "path": "../../packages/utils" }
]
Expand Down
27 changes: 27 additions & 0 deletions suite-common/connect-popup/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@suite-common/connect-popup",
"version": "1.0.0",
"private": true,
"license": "See LICENSE.md in repo root",
"sideEffects": false,
"main": "src/index",
"scripts": {
"type-check": "yarn g:tsc --build tsconfig.json"
},
"dependencies": {
"@reduxjs/toolkit": "1.9.5",
"@suite-common/redux-utils": "workspace:*",
"@suite-common/suite-types": "workspace:*",
"@suite-common/test-utils": "workspace:*",
"@suite-common/wallet-core": "workspace:*",
"@trezor/connect": "workspace:*",
"@trezor/env-utils": "workspace:^",
"@trezor/suite-desktop-api": "workspace:*",
"@trezor/urls": "workspace:*",
"@trezor/utils": "workspace:*"
},
"devDependencies": {
"redux-mock-store": "^1.5.4",
"redux-thunk": "^2.4.2"
}
}
7 changes: 7 additions & 0 deletions suite-common/connect-popup/redux.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { AsyncThunkAction } from '@reduxjs/toolkit';

declare module 'redux' {
export interface Dispatch {
<TThunk extends AsyncThunkAction<any, any, any>>(thunk: TThunk): ReturnType<TThunk>;
}
}
99 changes: 99 additions & 0 deletions suite-common/connect-popup/src/connectPopupThunks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { createThunk } from '@suite-common/redux-utils';
import TrezorConnect, { ERRORS } from '@trezor/connect';
import { createDeferred } from '@trezor/utils';
import { selectDevice } from '@suite-common/wallet-core';
import { desktopApi } from '@trezor/suite-desktop-api';
import { serializeError } from '@trezor/connect/src/constants/errors';

const CONNECT_POPUP_MODULE = '@common/connect-popup';

export const connectPopupCallThunk = createThunk(
`${CONNECT_POPUP_MODULE}/callThunk`,
async (
{
id,
method,
payload,
processName,
origin,
}: {
id: number;
method: string;
payload: any;
processName?: string;
origin?: string;
},
{ dispatch, getState, extra },
) => {
try {
const device = selectDevice(getState());

if (!device) {
console.error('Device not found');

// TODO: wait for device selection and continue
throw ERRORS.TypedError('Device_NotFound');
}

// @ts-expect-error: method is dynamic
const methodInfo = await TrezorConnect[method]({
...payload,
__info: true,
});
if (!methodInfo.success) {
throw methodInfo;
}

const confirmation = createDeferred();
dispatch(extra.actions.lockDevice(true));
dispatch(
extra.actions.openModal({
type: 'connect-popup',
onCancel: () => confirmation.reject(ERRORS.TypedError('Method_Cancel')),
onConfirm: () => confirmation.resolve(),
method: methodInfo.payload.info,
processName,
origin,
}),
);
await confirmation.promise;
dispatch(extra.actions.lockDevice(false));

// @ts-expect-error: method is dynamic
const response = await TrezorConnect[method]({
device: {
path: device.path,
instance: device.instance,
state: device.state,
},
...payload,
});

dispatch(extra.actions.onModalCancel());

desktopApi.connectPopupResponse({
...response,
id,
});
} catch (error) {
console.error('connectPopupCallThunk', error);
desktopApi.connectPopupResponse({
success: false,
payload: serializeError(error),
id,
});
}
},
);

export const connectPopupInitThunk = createThunk(
`${CONNECT_POPUP_MODULE}/initPopupThunk`,
async (_, { dispatch }) => {
if (desktopApi.available && (await desktopApi.connectPopupEnabled())) {
desktopApi.on('connect-popup/call', params => {
dispatch(connectPopupCallThunk(params));
});
desktopApi.connectPopupReady();
}
},
);
1 change: 1 addition & 0 deletions suite-common/connect-popup/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './connectPopupThunks';
17 changes: 17 additions & 0 deletions suite-common/connect-popup/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": { "outDir": "libDev" },
"references": [
{ "path": "../redux-utils" },
{ "path": "../suite-types" },
{ "path": "../test-utils" },
{ "path": "../wallet-core" },
{ "path": "../../packages/connect" },
{ "path": "../../packages/env-utils" },
{
"path": "../../packages/suite-desktop-api"
},
{ "path": "../../packages/urls" },
{ "path": "../../packages/utils" }
]
}
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9414,6 +9414,24 @@ __metadata:
"@suite-common/connect-init@workspace:*, @suite-common/connect-init@workspace:suite-common/connect-init":
version: 0.0.0-use.local
resolution: "@suite-common/connect-init@workspace:suite-common/connect-init"
dependencies:
"@reduxjs/toolkit": "npm:1.9.5"
"@suite-common/redux-utils": "workspace:*"
"@suite-common/suite-types": "workspace:*"
"@suite-common/test-utils": "workspace:*"
"@suite-common/wallet-core": "workspace:*"
"@trezor/connect": "workspace:*"
"@trezor/env-utils": "workspace:^"
"@trezor/urls": "workspace:*"
"@trezor/utils": "workspace:*"
redux-mock-store: "npm:^1.5.4"
redux-thunk: "npm:^2.4.2"
languageName: unknown
linkType: soft

"@suite-common/connect-popup@workspace:*, @suite-common/connect-popup@workspace:suite-common/connect-popup":
version: 0.0.0-use.local
resolution: "@suite-common/connect-popup@workspace:suite-common/connect-popup"
dependencies:
"@reduxjs/toolkit": "npm:1.9.5"
"@suite-common/redux-utils": "workspace:*"
Expand Down Expand Up @@ -12541,6 +12559,7 @@ __metadata:
"@suite-common/analytics": "workspace:*"
"@suite-common/assets": "workspace:*"
"@suite-common/connect-init": "workspace:*"
"@suite-common/connect-popup": "workspace:*"
"@suite-common/device-authenticity": "workspace:*"
"@suite-common/fiat-services": "workspace:*"
"@suite-common/firmware": "workspace:*"
Expand Down

0 comments on commit 9786cea

Please sign in to comment.