Skip to content

Commit

Permalink
draft(suite): test fulter accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
enjojoy committed Sep 20, 2024
1 parent 7df7a60 commit d578e82
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isDebugOnlyAccountType, Network, networksCollection } from '@suite-common/wallet-config';
import { Network, networksCollection } from '@suite-common/wallet-config';
import { selectDevice } from '@suite-common/wallet-core';
import { useEffect, useMemo, useState } from 'react';
import { useForm } from 'react-hook-form';
Expand All @@ -22,6 +22,7 @@ import {
} from 'src/types/coinmarket/coinmarketVerify';
import { useAccountAddressDictionary } from 'src/hooks/wallet/useAccounts';
import { TrezorDevice } from '@suite-common/suite-types';
import { filterRecieveAccounts } from '@suite-common/wallet-utils';

const getSelectAccountOptions = (
suiteReceiveAccounts: Account[] | undefined,
Expand Down Expand Up @@ -79,31 +80,15 @@ const getSuiteReceiveAccounts = ({
((n.isDebugOnlyNetwork && isDebug) || !n.isDebugOnlyNetwork),
);

const isSameDevice = (account: Account) => account.deviceState === device?.state;
const isSameNetwork = (account: Account) => account.symbol === receiveNetwork;
const isDebugAndIsAccountDebugOnly = (account: Account) =>
isDebugOnlyAccountType(account.accountType, account.symbol) && isDebug;
const isNotDebugOnlyAccount = (account: Account) =>
!isDebugOnlyAccountType(account.accountType, account.symbol);
// Check if the account is not empty
const isNotEmptyAccount = (account: Account) => !account.empty;
// Check if the account is marked as visible
const isVisibleAccount = (account: Account) => account.visible;
const isFirstNormalAccount = (account: Account) =>
account.accountType === 'normal' && account.index === 0;

if (receiveNetworks.length > 0) {
// Get accounts of the current symbol belonging to the current device.
return accounts.filter(
account =>
isSameDevice(account) &&
isSameNetwork(account) &&
(isDebugAndIsAccountDebugOnly(account) || isNotDebugOnlyAccount(account)) &&
(isNotEmptyAccount(account) ||
isVisibleAccount(account) ||
isFirstNormalAccount(account)),
);
}
const filteredAccounts = filterRecieveAccounts({
accounts,
deviceState: device?.state,
receiveNetwork,
isDebug,
receiveNetworks,
});

return filteredAccounts;
}

return undefined;
Expand Down Expand Up @@ -140,6 +125,8 @@ const useCoinmarketVerifyAccount = ({
}),
[accounts, currency, device, isDebug, receiveNetwork],
);

console.log('SUITE RECEIVE ACCOUNTS: ', suiteReceiveAccounts);
const selectAccountOptions = useMemo(
() => getSelectAccountOptions(suiteReceiveAccounts, device),
[device, suiteReceiveAccounts],
Expand Down
17 changes: 1 addition & 16 deletions suite-common/wallet-config/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { networks, networksCompatibility } from './networksConfig';
import { AccountType, Network, NetworkFeature, Networks, NetworkSymbol } from './types';
import { AccountType, Network, NetworkFeature, NetworkSymbol } from './types';

/**
* array from `networks` as a `Network[]` type instead of inferred type
Expand Down Expand Up @@ -39,21 +39,6 @@ export const getTestnetSymbols = () => getTestnets().map(n => n.symbol);
export const isBlockbookBasedNetwork = (symbol: NetworkSymbol) =>
networks[symbol]?.customBackends.some(backend => backend === 'blockbook');

export const isDebugOnlyAccountType = (
accountType: AccountType,
symbol?: NetworkSymbol,
): boolean => {
if (!symbol) return false;

const network = (networks as Networks)?.[symbol];

if (!network) return false;

const accountTypeInfo = network.accountTypes[accountType];

return !!accountTypeInfo?.isDebugOnlyAccountType;
};

export const getNetworkType = (symbol: NetworkSymbol) => networks[symbol]?.networkType;

// Takes into account just network features, not features for specific accountTypes.
Expand Down
58 changes: 58 additions & 0 deletions suite-common/wallet-utils/src/accountUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
TokenTransfer,
TokenInfo,
} from '@trezor/connect';
import { Network } from '@suite-common/wallet-config';
import { arrayDistinct, bufferUtils } from '@trezor/utils';
import {
networksCompatibility,
Expand All @@ -20,6 +21,7 @@ import {
AccountType,
Bip43Path,
getNetwork,
Networks,
} from '@suite-common/wallet-config';
import {
Account,
Expand Down Expand Up @@ -1153,3 +1155,59 @@ export const isTokenMatchesSearch = (token: TokenInfo, search: string) => {
token.policyId?.toLowerCase().includes(search)
);
};

export const isDebugOnlyAccountType = (
accountType: AccountType,
symbol?: NetworkSymbol,
): boolean => {
if (!symbol) return false;

const network = (networks as Networks)?.[symbol];

if (!network) return false;

const accountTypeInfo = network.accountTypes[accountType];

return !!accountTypeInfo?.isDebugOnlyAccountType;
};

type FilterRecieveAccountsProps = {
accounts: Account[];
deviceState: string | undefined;
receiveNetwork?: string;
isDebug: boolean;
receiveNetworks: Network[];
};

export const filterRecieveAccounts = ({
accounts,
deviceState,
receiveNetwork,
isDebug,
receiveNetworks,
}: FilterRecieveAccountsProps): Account[] => {
const isSameDevice = (account: Account) => account.deviceState === deviceState;
const isSameNetwork = (account: Account) => account.symbol === receiveNetwork;
const isDebugAndIsAccountDebugOnly = (account: Account) =>
isDebugOnlyAccountType(account.accountType, account.symbol) && isDebug;
const isNotDebugOnlyAccount = (account: Account) =>
!isDebugOnlyAccountType(account.accountType, account.symbol);
const isNotEmptyAccount = (account: Account) => !account.empty;
const isVisibleAccount = (account: Account) => account.visible;
const isFirstNormalAccount = (account: Account) =>
account.accountType === 'normal' && account.index === 0;

if (receiveNetworks.length > 0) {
return accounts.filter(
account =>
isSameDevice(account) &&
isSameNetwork(account) &&
(isDebugAndIsAccountDebugOnly(account) || isNotDebugOnlyAccount(account)) &&
(isNotEmptyAccount(account) ||
isVisibleAccount(account) ||
isFirstNormalAccount(account)),
);
}

return [];
};

0 comments on commit d578e82

Please sign in to comment.