diff --git a/packages/suite/src/hooks/wallet/coinmarket/form/useCoinmarketVerifyAccount.tsx b/packages/suite/src/hooks/wallet/coinmarket/form/useCoinmarketVerifyAccount.tsx index 19bbbc982280..fbb8c8ccaa4a 100644 --- a/packages/suite/src/hooks/wallet/coinmarket/form/useCoinmarketVerifyAccount.tsx +++ b/packages/suite/src/hooks/wallet/coinmarket/form/useCoinmarketVerifyAccount.tsx @@ -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'; @@ -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, @@ -78,32 +79,40 @@ const getSuiteReceiveAccounts = ({ !unavailableCapabilities[n.symbol] && ((n.isDebugOnlyNetwork && isDebug) || !n.isDebugOnlyNetwork), ); + const filteredAccounts = filterRecieveAccounts({ + accounts, + deviceState: device?.state, + receiveNetwork, + isDebug, + receiveNetworks, + }); - 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)), - ); - } + console.log('FILTERED ACCOUNTS: ', filteredAccounts); + // 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)), + // ); + // } } return undefined; diff --git a/suite-common/wallet-config/src/utils.ts b/suite-common/wallet-config/src/utils.ts index b07200caeccf..fd968d347346 100644 --- a/suite-common/wallet-config/src/utils.ts +++ b/suite-common/wallet-config/src/utils.ts @@ -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 @@ -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. diff --git a/suite-common/wallet-utils/src/accountUtils.ts b/suite-common/wallet-utils/src/accountUtils.ts index 66ecb3eec946..bca5cd5be1cf 100644 --- a/suite-common/wallet-utils/src/accountUtils.ts +++ b/suite-common/wallet-utils/src/accountUtils.ts @@ -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, @@ -20,6 +21,7 @@ import { AccountType, Bip43Path, getNetwork, + Networks, } from '@suite-common/wallet-config'; import { Account, @@ -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 []; +};