-
-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(suite): separate popup logic from
@suite-common/connect-init
- Loading branch information
Showing
13 changed files
with
179 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './connectPopupThunks'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters