Skip to content

Commit

Permalink
chore(suite): update network symbol naming
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive authored and tomasklim committed Dec 1, 2024
1 parent 10e5bc4 commit a4ed158
Show file tree
Hide file tree
Showing 24 changed files with 185 additions and 125 deletions.
18 changes: 9 additions & 9 deletions packages/coinjoin/tests/tools/discovery.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
/* eslint-disable no-console */

import { BITCOIN_ONLY_NETWORKS } from '@suite-common/suite-constants';
import { BITCOIN_ONLY_SYMBOLS } from '@suite-common/suite-constants';
import { isArrayMember } from '@trezor/utils';

import { CoinjoinBackend } from '../../src/backend/CoinjoinBackend';
import type { CoinjoinBackendSettings } from '../../src/types';

const { getCoinjoinConfig } = require('../../../suite/src/services/coinjoin/config');

const supportedNetworks = BITCOIN_ONLY_NETWORKS;
type SupportedNetwork = (typeof supportedNetworks)[number];
const supportedSymbols = BITCOIN_ONLY_SYMBOLS;
type SupportedNetwork = (typeof supportedSymbols)[number];

const isSupportedNetwork = (network: string): network is SupportedNetwork =>
isArrayMember(network, supportedNetworks);
const isSupportedSymbol = (symbol: string): symbol is SupportedNetwork =>
isArrayMember(symbol, supportedSymbols);

export const getAccountInfoParams = (network: string, descriptor: string) => {
if (!network) throw new Error('network arg missing');
if (!isSupportedNetwork(network)) throw new Error('unsupported network');
export const getAccountInfoParams = (symbol: string, descriptor: string) => {
if (!symbol) throw new Error('symbol arg missing');
if (!isSupportedSymbol(symbol)) throw new Error('unsupported symbol');
if (!descriptor) throw new Error('descriptor arg missing');

const config: CoinjoinBackendSettings = getCoinjoinConfig(network);
const config: CoinjoinBackendSettings = getCoinjoinConfig(symbol);

return {
descriptor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ export const createCoinjoinAccount = [
account: { accountType: 'coinjoin' },
},
result: {
actions: [
COINJOIN.CLIENT_ENABLE,
COINJOIN.CLIENT_DISABLE,
notificationsActions.addToast.type,
],
actions: [],
},
},
{
Expand Down Expand Up @@ -156,11 +152,7 @@ export const startCoinjoinSession = [
symbol: 'ltc', // only btc is supported in tests
},
result: {
actions: [
COINJOIN.CLIENT_ENABLE,
COINJOIN.CLIENT_DISABLE,
notificationsActions.addToast.type,
],
actions: [],
},
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ export const onCoinjoinRoundChanged = [
connect: undefined,
state: {
devices: [{ ...DEVICE, features: { busy: false } }],
accounts: [{ key: 'account-A', deviceState: '1stTestnetAddress@device_id:0' }],
accounts: [
{ key: 'account-A', deviceState: '1stTestnetAddress@device_id:0', symbol: 'btc' },
],
selectedAccount: { key: 'account-A' },
coinjoin: {
accounts: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventEmitter } from 'events';

import { NetworkSymbol } from '@suite-common/wallet-config';
import type { CoinjoinSymbol } from 'src/services/coinjoin';

export const mockCoinjoinService = () => {
const allowed = ['btc', 'test'];
Expand Down Expand Up @@ -63,15 +63,15 @@ export const mockCoinjoinService = () => {
};

return {
// for test purposes enable only btc network
// for test purposes enable only btc symbol
CoinjoinService: {
getInstance: jest.fn((symbol: string) => clients[symbol]),
getInstances: jest.fn(() => Object.values(clients)),
createInstance: jest.fn(({ network }: { network: NetworkSymbol }) => {
if (!allowed.includes(network)) throw new Error('Client not supported');
if (clients[network]) return clients[network];
const instance = getMockedInstance(network);
clients[network] = instance;
createInstance: jest.fn(({ symbol }: { symbol: CoinjoinSymbol }) => {
if (!allowed.includes(symbol)) throw new Error('Client not supported');
if (clients[symbol]) return clients[symbol];
const instance = getMockedInstance(symbol);
clients[symbol] = instance;

return instance;
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('coinjoinAccountActions', () => {
const store = initStore(f.state as Wallet);

if (f.client) {
await CoinjoinService.createInstance({ network: f.client as any });
await CoinjoinService.createInstance({ symbol: f.client as any });
}

await store.dispatch(coinjoinClientActions.stopCoinjoinSession(f.param));
Expand All @@ -128,7 +128,7 @@ describe('coinjoinAccountActions', () => {
const store = initStore(f.state as Wallet);

if (f.client) {
await CoinjoinService.createInstance({ network: f.client as any });
await CoinjoinService.createInstance({ symbol: f.client as any });
}

await store.dispatch(coinjoinAccountActions.restoreCoinjoinSession(f.param));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ describe('coinjoinClientActions', () => {
const cli2 = await store.dispatch(initCoinjoinService('btc'));
expect(cli1).toEqual(cli2);
expect(spy.mock.calls[0][0]).toMatchObject({
network: 'btc',
symbol: 'btc',
prison: [
{
accountKey: 'account-A',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { notificationsActions } from '@suite-common/toast-notifications';
import { DiscoveryStatus } from '@suite-common/wallet-constants';
import * as discoveryActions from '@suite-common/wallet-core';
import TrezorConnect, { ERRORS } from '@trezor/connect';
import { BITCOIN_ONLY_NETWORKS } from '@suite-common/suite-constants';
import { BITCOIN_ONLY_SYMBOLS } from '@suite-common/suite-constants';

import { configureStore, filterThunkActionTypes } from 'src/support/tests/configureStore';
import walletSettingsReducer from 'src/reducers/wallet/settingsReducer';
Expand Down Expand Up @@ -183,7 +183,7 @@ const getInitialState = (device = SUITE_DEVICE) => ({
accounts: accountsReducer(undefined, { type: 'foo' } as any),
settings: walletSettingsReducer(undefined, {
type: walletSettingsActions.changeNetworks.type,
payload: BITCOIN_ONLY_NETWORKS,
payload: BITCOIN_ONLY_SYMBOLS,
}),
},
});
Expand Down
22 changes: 16 additions & 6 deletions packages/suite/src/actions/wallet/coinjoinAccountActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import {
} from '@suite-common/wallet-utils';

import { CoinjoinService, COORDINATOR_FEE_RATE_MULTIPLIER } from 'src/services/coinjoin';
import { getAccountProgressHandle, getRegisterAccountParams } from 'src/utils/wallet/coinjoinUtils';
import type { CoinjoinSymbol } from 'src/services/coinjoin';
import {
getAccountProgressHandle,
getRegisterAccountParams,
isCoinjoinSupportedSymbol,
} from 'src/utils/wallet/coinjoinUtils';
import { Dispatch, GetState } from 'src/types/suite';
import {
CoinjoinAccount,
Expand Down Expand Up @@ -260,6 +265,7 @@ const getAccountCache = ({ addresses, path }: Extract<Account, { backendType: 'c

export const updateClientAccount =
(account: Account) => (dispatch: Dispatch, getState: GetState) => {
if (!isCoinjoinSupportedSymbol(account.symbol)) return;
const client = coinjoinClientActions.getCoinjoinClient(account.symbol);
if (!client) return;

Expand Down Expand Up @@ -540,12 +546,12 @@ export const fetchAndUpdateAccount =
};

export const clearCoinjoinInstances =
(networkSymbol: NetworkSymbol) => (dispatch: Dispatch, getState: GetState) => {
const cjAccount = selectCoinjoinAccounts(getState()).find(a => a.symbol === networkSymbol);
(symbol: CoinjoinSymbol) => (dispatch: Dispatch, getState: GetState) => {
const cjAccount = selectCoinjoinAccounts(getState()).find(a => a.symbol === symbol);
// clear CoinjoinClientInstance if there are no related accounts left
if (!cjAccount) {
dispatch(coinjoinClientActions.clientDisable(networkSymbol));
CoinjoinService.removeInstance(networkSymbol);
dispatch(coinjoinClientActions.clientDisable(symbol));
CoinjoinService.removeInstance(symbol);
}
};

Expand All @@ -560,6 +566,10 @@ export const createCoinjoinAccount =
throw new Error('createCoinjoinAccount: invalid account type');
}

if (!isCoinjoinSupportedSymbol(network.symbol)) {
return;
}

// initialize @trezor/coinjoin client
const api = await dispatch(coinjoinClientActions.initCoinjoinService(network.symbol));
if (!api) {
Expand Down Expand Up @@ -768,7 +778,7 @@ export const restoreCoinjoinSession =
const device = selectDevice(getState());
const account = selectAccountByKey(getState(), accountKey);

if (!account) {
if (!account || !isCoinjoinSupportedSymbol(account.symbol)) {
return;
}

Expand Down
26 changes: 19 additions & 7 deletions packages/suite/src/actions/wallet/coinjoinClientActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import {
prepareCoinjoinTransaction,
getSessionDeadline,
getEstimatedTimePerRound,
isCoinjoinSupportedSymbol,
} from 'src/utils/wallet/coinjoinUtils';
import type { CoinjoinSymbol } from 'src/services/coinjoin';
import { CoinjoinService, getCoinjoinConfig } from 'src/services/coinjoin';
import { Dispatch, GetState } from 'src/types/suite';
import { CoinjoinAccount, EndRoundState, CoinjoinDebugSettings } from 'src/types/wallet/coinjoin';
Expand Down Expand Up @@ -187,15 +189,22 @@ export type CoinjoinClientAction =
| ReturnType<typeof coinjoinSessionPause>;

// return only active instances
export const getCoinjoinClient = (symbol: Account['symbol']) =>
export const getCoinjoinClient = (symbol: CoinjoinSymbol) =>
CoinjoinService.getInstance(symbol)?.client;

export const unregisterByAccountKey =
(accountKey: string) => (_dispatch: Dispatch, getState: GetState) => {
const { accounts } = getState().wallet;
const realAccount = accounts.find(a => a.key === accountKey);
const client = realAccount && getCoinjoinClient(realAccount.symbol);
client?.unregisterAccount(accountKey);

const client =
realAccount && isCoinjoinSupportedSymbol(realAccount.symbol)
? getCoinjoinClient(realAccount.symbol)
: undefined;

if (client) {
client.unregisterAccount(accountKey);
}
};

export const endCoinjoinSession = (accountKey: string) => (dispatch: Dispatch) => {
Expand Down Expand Up @@ -274,7 +283,7 @@ export const pauseCoinjoinSession =
(accountKey: string) => (dispatch: Dispatch, getState: GetState) => {
const account = selectAccountByKey(getState(), accountKey);

if (!account) {
if (!account || !isCoinjoinSupportedSymbol(account.symbol)) {
return;
}
// get @trezor/coinjoin client if available
Expand All @@ -293,7 +302,7 @@ export const stopCoinjoinSession =
const state = getState();
const account = selectAccountByKey(state, accountKey);

if (!account) {
if (!account || !isCoinjoinSupportedSymbol(account.symbol)) {
return;
}

Expand Down Expand Up @@ -556,7 +565,7 @@ interface ClientEmitExceptionOptions {
export const clientEmitException =
(reason: string, options: ClientEmitExceptionOptions = {}) =>
() => {
(options.symbol
(options.symbol && isCoinjoinSupportedSymbol(options.symbol)
? [CoinjoinService.getInstance(options.symbol)]
: CoinjoinService.getInstances()
).forEach(instance => {
Expand Down Expand Up @@ -726,6 +735,9 @@ export const initCoinjoinService =
(symbol: Account['symbol']) => async (dispatch: Dispatch, getState: GetState) => {
const state = getState();
const { clients, debug, accounts } = state.wallet.coinjoin;

if (!isCoinjoinSupportedSymbol(symbol)) return;

const knownClient = clients[symbol];
if (knownClient?.status === 'loading') return;

Expand Down Expand Up @@ -790,7 +802,7 @@ export const initCoinjoinService =
try {
const config = getCoinjoinConfig(symbol, environment);
const service = await CoinjoinService.createInstance({
network: symbol,
symbol,
prison,
settings: { ...config, ...debug?.coinjoinConfigOverride?.[symbol] },
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { ScanProgressInfo } from '@trezor/coinjoin';
import { CoinjoinService } from 'src/services/coinjoin';
import { selectSelectedAccount } from 'src/reducers/wallet/selectedAccountReducer';
import { useSelector } from 'src/hooks/suite/useSelector';
import { getAccountProgressHandle } from 'src/utils/wallet/coinjoinUtils';
import {
getAccountProgressHandle,
isCoinjoinSupportedSymbol,
} from 'src/utils/wallet/coinjoinUtils';
import { TranslationKey } from 'src/components/suite/Translation';

const INIT_THRESHOLD = 0.05;
Expand Down Expand Up @@ -74,15 +77,20 @@ export const useCoinjoinAccountLoadingProgress = () => {
progress: 0,
});

const { symbol: network, backendType } = selectedAccount || {};
const { symbol, backendType } = selectedAccount || {};
const progressHandle = selectedAccount && getAccountProgressHandle(selectedAccount);

useEffect(() => {
if (!network || !progressHandle || backendType !== 'coinjoin') {
if (
!symbol ||
!progressHandle ||
backendType !== 'coinjoin' ||
!isCoinjoinSupportedSymbol(symbol)
) {
return;
}

const api = CoinjoinService.getInstance(network);
const api = CoinjoinService.getInstance(symbol);

if (!api) {
return;
Expand All @@ -93,7 +101,7 @@ export const useCoinjoinAccountLoadingProgress = () => {
return () => {
api.backend.off(`progress-info/${progressHandle}`, dispatchProgressInfo);
};
}, [network, backendType, progressHandle]);
}, [symbol, backendType, progressHandle]);

return progressInfo;
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { accountsActions } from '@suite-common/wallet-core';
import { SelectedAccountLoaded } from '@suite-common/wallet-types';
import { AnonymitySet } from '@trezor/blockchain-link';
import { DEVICE, StaticSessionId } from '@trezor/connect';
import { NetworkSymbol } from '@suite-common/wallet-config';

import { SuiteState } from 'src/reducers/suite/suiteReducer';
import { ROUTER, SUITE } from 'src/actions/suite/constants';
Expand Down Expand Up @@ -144,7 +143,7 @@ export const fixtures = [
{
description: 'stopping coinjoin session when remembered device disconnects',
state: DEFAULT_STATE,
client: 'btc' as NetworkSymbol,
client: 'btc' as const,
action: {
type: DEVICE.DISCONNECT,
payload: {
Expand Down Expand Up @@ -172,7 +171,7 @@ export const fixtures = [
{
description: 'restore all interrupted coinjoin sessions when Tor is enabled',
state: STATE_WITH_INTERRUPTED_SESSION,
client: 'btc' as NetworkSymbol,
client: 'btc' as const,
connect: {
success: true,
},
Expand Down Expand Up @@ -205,7 +204,7 @@ export const fixtures = [
{
description: 'restore all interrupted coinjoin sessions when user leaves send form',
state: STATE_WITH_INTERRUPTED_SESSION,
client: 'btc' as NetworkSymbol,
client: 'btc' as const,
connect: [
{
success: true,
Expand Down Expand Up @@ -270,7 +269,7 @@ export const fixtures = [
{
description: 'restore related coinjoin session when an account syncs',
state: STATE_WITH_INTERRUPTED_SESSION,
client: 'btc' as NetworkSymbol,
client: 'btc' as const,
connect: [
{
success: true,
Expand All @@ -294,7 +293,7 @@ export const fixtures = [
{
description: 'restore all interrupted coinjoin sessions when Suite goes online',
state: STATE_WITH_INTERRUPTED_SESSION,
client: 'btc' as NetworkSymbol,
client: 'btc' as const,
connect: [
{
success: true,
Expand All @@ -312,7 +311,7 @@ export const fixtures = [
...STATE_WITH_INTERRUPTED_SESSION,
router: { route: { name: 'wallet-send' } } as RouterState,
},
client: 'btc' as NetworkSymbol,
client: 'btc' as const,
connect: [
{
success: true,
Expand Down
Loading

0 comments on commit a4ed158

Please sign in to comment.