Skip to content

Commit

Permalink
refactor(suite): general refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
marekrjpolak committed Dec 12, 2024
1 parent 72a265f commit 7a851c7
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 28 deletions.
6 changes: 3 additions & 3 deletions packages/suite/src/middlewares/wallet/storageMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import {
sendFormActions,
} from '@suite-common/wallet-core';
import { firmwareActions } from '@suite-common/firmware';
import { contactsActions } from '@suite-common/contacts';
import { isDeviceRemembered } from '@suite-common/suite-utils';
import { messageSystemActions } from '@suite-common/message-system';
import { findAccountDevice } from '@suite-common/wallet-utils';
import { analyticsActions } from '@suite-common/analytics';
import { tokenDefinitionsActions } from '@suite-common/token-definitions/src/tokenDefinitionsActions';
import { TokenManagementAction } from '@suite-common/token-definitions';
import { CONTACTS } from '@suite-common/contacts';

import { db } from 'src/storage';
import { WALLET_SETTINGS } from 'src/actions/settings/constants';
Expand Down Expand Up @@ -349,10 +349,10 @@ const storageMiddleware = (api: MiddlewareAPI<Dispatch, AppState>) => {
break;
}

case CONTACTS.ADD:
case contactsActions.addContact.type:
api.dispatch(storageActions.saveContact(action.payload));
break;
case CONTACTS.REMOVE:
case contactsActions.removeContact.type:
api.dispatch(storageActions.removeContact(action.payload));
break;

Expand Down
3 changes: 2 additions & 1 deletion packages/suite/src/types/suite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import type { ThunkDispatch, ThunkAction as TAction } from 'redux-thunk';
import type { Store as ReduxStore } from 'redux';

import { deviceActions, discoveryActions, transactionsActions } from '@suite-common/wallet-core';
import { contactsActions } from '@suite-common/contacts';
import { firmwareActions } from '@suite-common/firmware';
import { analyticsActions } from '@suite-common/analytics';
import type { UiEvent, TransportEvent, BlockchainEvent } from '@trezor/connect';
import { notificationsActions } from '@suite-common/toast-notifications';
import { messageSystemActions } from '@suite-common/message-system';
import { deviceAuthenticityActions } from '@suite-common/device-authenticity';
import type { Route } from '@suite-common/suite-types';
import { ContactsAction } from '@suite-common/contacts';

import type { RouterAction } from 'src/actions/suite/routerActions';
import type { AppState } from 'src/reducers/store';
Expand Down Expand Up @@ -53,6 +53,7 @@ export type MessageSystemAction = ReturnType<
(typeof messageSystemActions)[keyof typeof messageSystemActions]
>;
type AnalyticsAction = ReturnType<(typeof analyticsActions)[keyof typeof analyticsActions]>;
type ContactsAction = ReturnType<(typeof contactsActions)[keyof typeof contactsActions]>;
type FirmwareAction = ReturnType<(typeof firmwareActions)[keyof typeof firmwareActions]>;
type DeviceAction = ReturnType<(typeof deviceActions)[keyof typeof deviceActions]>;
type DiscoveryAction = ReturnType<(typeof discoveryActions)[keyof typeof discoveryActions]>;
Expand Down
8 changes: 6 additions & 2 deletions packages/suite/src/views/contacts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { Banner, Button, Row } from '@trezor/components';
import { selectDevice } from '@suite-common/wallet-core';
import { TrezorDevice } from '@suite-common/suite-types';
import { spacings } from '@trezor/theme';
import { Contact, findContact, selectContactsForDevice } from '@suite-common/contacts';
import {
Contact,
findContactBySignedMessage,
selectContactsForDevice,
} from '@suite-common/contacts';

import { useSelector } from 'src/hooks/suite';

Expand All @@ -25,7 +29,7 @@ const FindContactButton = ({ contacts }: { contacts: Contact[] }) => {
return;
}

const contact = await findContact(contacts, address, signature);
const contact = await findContactBySignedMessage(contacts, address, signature);
if (contact) alert(`Address "${address}" was signed by your contact "${contact.label}"`);
else alert('Recipient not in your contacts');
};
Expand Down
3 changes: 1 addition & 2 deletions packages/suite/src/views/contacts/useAddContact.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import TrezorConnect from '@trezor/connect';
import { selectDevice } from '@suite-common/wallet-core';
import { contactsActions } from '@suite-common/contacts';
import { getDeviceState } from '@suite-common/contacts/src/redux/contactsReducer';
import { contactsActions, getDeviceState } from '@suite-common/contacts';

import { useDispatch, useSelector } from '../../hooks/suite';

Expand Down
3 changes: 1 addition & 2 deletions suite-common/contacts/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export const ADD = '@contacts/add';
export const REMOVE = '@contacts/remove';
export const CONTACTS_MODULE_PREFIX = '@common/contacts';
9 changes: 4 additions & 5 deletions suite-common/contacts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * as contactsActions from './redux/contactsActions';
export type { ContactsAction } from './redux/contactsActions';
export { prepareContactsReducer } from './redux/contactsReducer';
export * from './redux/contactsActions';
export * from './redux/contactsReducer';
export * from './redux/contactsSelectors';
export * as CONTACTS from './constants';
export * from './constants';
export * from './types';
export * from './utils/findContact';
export * from './utils';
13 changes: 8 additions & 5 deletions suite-common/contacts/src/redux/contactsActions.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { createAction } from '@reduxjs/toolkit';

import { Contact } from '../types';
import * as CONTACTS from '../constants';
import { CONTACTS_MODULE_PREFIX } from '../constants';

export const addContact = createAction(CONTACTS.ADD, (payload: Contact) => ({ payload }));
export const addContact = createAction(`${CONTACTS_MODULE_PREFIX}/add`, (payload: Contact) => ({
payload,
}));

export const removeContact = createAction(CONTACTS.REMOVE, (payload: Contact) => ({ payload }));
export const removeContact = createAction(
`${CONTACTS_MODULE_PREFIX}/remove`,
(payload: Contact) => ({ payload }),
);

export const contactsActions = {
addContact,
removeContact,
};

export type ContactsAction = ReturnType<typeof addContact> | ReturnType<typeof removeContact>;
4 changes: 0 additions & 4 deletions suite-common/contacts/src/redux/contactsReducer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { TrezorDevice } from '@suite-common/suite-types';
import { createReducerWithExtraDeps } from '@suite-common/redux-utils';

import { Contact } from '../types';
Expand All @@ -15,9 +14,6 @@ const initialState: ContactsState = [];
const isEqual = (c1: Contact, c2: Contact) =>
c1.address === c2.address && c1.deviceState === c2.deviceState;

export const getDeviceState = (device: TrezorDevice) =>
device.state?.staticSessionId?.split('@')?.[0];

export const prepareContactsReducer = createReducerWithExtraDeps(initialState, (builder, extra) => {
builder
.addCase(contactsActions.addContact, (state, { payload: contact }) => {
Expand Down
3 changes: 2 additions & 1 deletion suite-common/contacts/src/redux/contactsSelectors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TrezorDevice } from '@suite-common/suite-types';

import { ContactsRootState, getDeviceState } from './contactsReducer';
import { ContactsRootState } from './contactsReducer';
import { getDeviceState } from '../utils';

export const selectContacts = (state: ContactsRootState) => state.contacts;

Expand Down
6 changes: 5 additions & 1 deletion suite-common/contacts/src/utils/findContact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import TrezorConnect from '@trezor/connect';

import { Contact } from '../types';

export const findContact = async (contacts: Contact[], address: string, signature: string) => {
export const findContactBySignedMessage = async (
contacts: Contact[],
address: string,
signature: string,
) => {
for (const contact of contacts) {
try {
// I thought this would work better but it still doesn't...
Expand Down
6 changes: 6 additions & 0 deletions suite-common/contacts/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { TrezorDevice } from '@suite-common/suite-types';

export * from './findContact';

export const getDeviceState = (device: TrezorDevice) =>
device.state?.staticSessionId?.split('@')?.[0];
4 changes: 2 additions & 2 deletions suite-common/wallet-core/src/send/sendFormBitcoinThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
PrecomposedTransaction,
} from '@suite-common/wallet-types';
import { createThunk } from '@suite-common/redux-utils';
import { findContact, selectContactsForDevice } from '@suite-common/contacts';
import { findContactBySignedMessage, selectContactsForDevice } from '@suite-common/contacts';

import { selectTransactions } from '../transactions/transactionsReducer';
import { selectDevice } from '../device/deviceReducer';
Expand Down Expand Up @@ -353,7 +353,7 @@ export const signBitcoinSendFormTransactionThunk = createThunk<
const contactSignature = formState.contactSignatures?.[output.address];
if (contactSignature) {
//console.log('contactSignature', contactSignature);
const contact = await findContact(
const contact = await findContactBySignedMessage(
contacts,
output.address,
contactSignature,
Expand Down

0 comments on commit 7a851c7

Please sign in to comment.