Skip to content

Commit

Permalink
feat(suite): minimal walletconnect implementation for evm
Browse files Browse the repository at this point in the history
  • Loading branch information
martykan committed Dec 18, 2024
1 parent 7d33220 commit 915e87a
Show file tree
Hide file tree
Showing 13 changed files with 1,973 additions and 38 deletions.
46 changes: 23 additions & 23 deletions packages/suite-desktop-connect-popup/src/connectPopupThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@ import { serializeError } from '@trezor/connect/src/constants/errors';

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

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

Expand Down Expand Up @@ -71,17 +69,18 @@ export const connectPopupCallThunk = createThunk(

dispatch(extra.actions.onModalCancel());

desktopApi.connectPopupResponse({
return {
...response,
id,
});
};
} catch (error) {
console.error('connectPopupCallThunk', error);
desktopApi.connectPopupResponse({

return {
success: false,
payload: serializeError(error),
id,
});
};
}
},
);
Expand All @@ -90,8 +89,9 @@ 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.on('connect-popup/call', async params => {
const response = await dispatch(connectPopupCallThunk(params)).unwrap();
desktopApi.connectPopupResponse(response);
});
desktopApi.connectPopupReady();
}
Expand Down
26 changes: 26 additions & 0 deletions packages/suite-walletconnect/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@trezor/suite-walletconnect",
"version": "1.0.0",
"private": true,
"license": "See LICENSE.md in repo root",
"sideEffects": false,
"main": "src/index",
"scripts": {
"depcheck": "yarn g:depcheck",
"type-check": "yarn g:tsc --build",
"test:unit": "yarn g:jest -c ../../jest.config.base.js",
"test-unit:watch": "yarn g:jest -c ../../jest.config.base.js -o --watch"
},
"dependencies": {
"@reduxjs/toolkit": "1.9.5",
"@reown/walletkit": "^1.1.1",
"@suite-common/redux-utils": "workspace:*",
"@suite-common/suite-types": "workspace:*",
"@trezor/connect": "workspace:*",
"@walletconnect/core": "^2.17.2",
"@walletconnect/utils": "^2.17.2"
},
"devDependencies": {
"redux-thunk": "^2.4.2"
}
}
2 changes: 2 additions & 0 deletions packages/suite-walletconnect/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './walletConnectThunks';
export * from './walletConnectMiddleware';
27 changes: 27 additions & 0 deletions packages/suite-walletconnect/src/walletConnectMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { accountsActions } from '@suite-common/wallet-core';
import { createMiddlewareWithExtraDeps } from '@suite-common/redux-utils';

import * as walletConnectActions from './walletConnectThunks';

export const prepareWalletConnectMiddleware = createMiddlewareWithExtraDeps(
async (action, { dispatch, next }) => {
await next(action);

if (accountsActions.updateSelectedAccount.match(action) && action.payload.account) {
dispatch(
walletConnectActions.switchSelectedAccountThunk({
account: action.payload.account,
}),
);
}

if (
accountsActions.createAccount.match(action) ||
accountsActions.removeAccount.match(action)
) {
dispatch(walletConnectActions.updateAccountsThunk());
}

return action;
},
);
Loading

0 comments on commit 915e87a

Please sign in to comment.