-
-
Notifications
You must be signed in to change notification settings - Fork 266
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
190 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
suite-common/wallet-utils/src/__tests__/filterReceiveAccounts.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { testMocks } from '@suite-common/test-utils'; | ||
import { Network, networksCollection } from '@suite-common/wallet-config'; | ||
import { Account } from '@suite-common/wallet-types'; | ||
|
||
import { isDebugOnlyAccountType, filterReceiveAccounts } from '../filterReceiveAccounts'; | ||
|
||
const { getSuiteDevice, getWalletAccount } = testMocks; | ||
|
||
const accountsList: Account[] = [ | ||
getWalletAccount({ symbol: 'eth', accountType: 'legacy' }), | ||
getWalletAccount({ symbol: 'eth', accountType: 'normal' }), | ||
getWalletAccount({ symbol: 'eth', accountType: 'ledger' }), | ||
getWalletAccount({ symbol: 'btc', accountType: 'coinjoin' }), | ||
getWalletAccount({ symbol: 'btc', accountType: 'taproot' }), | ||
getWalletAccount({ symbol: 'btc', accountType: 'legacy' }), | ||
getWalletAccount({ symbol: 'btc', accountType: 'segwit' }), | ||
getWalletAccount({ symbol: 'btc', accountType: 'ledger' }), | ||
getWalletAccount({ symbol: 'pol', accountType: 'legacy' }), | ||
getWalletAccount({ symbol: 'pol', accountType: 'normal' }), | ||
getWalletAccount({ symbol: 'pol', accountType: 'ledger' }), | ||
getWalletAccount({ symbol: 'sol', accountType: 'normal', empty: true, visible: false }), | ||
getWalletAccount({ symbol: 'sol', accountType: 'ledger' }), | ||
getWalletAccount({ | ||
symbol: 'sol', | ||
accountType: 'ledger', | ||
empty: true, | ||
visible: false, | ||
}), | ||
]; | ||
|
||
type RunFilterReceiveAccountsTestParams = { | ||
isDebug?: boolean; | ||
receiveNetwork?: string; | ||
deviceState?: `${string}@${string}:${number}`; | ||
accounts?: Account[]; | ||
}; | ||
|
||
const runFilterReceiveAccouns = ({ | ||
isDebug = true, | ||
receiveNetwork = 'eth', | ||
deviceState = '1stTestnetAddress@device_id:0', | ||
accounts = accountsList, | ||
}: RunFilterReceiveAccountsTestParams) => { | ||
const device = getSuiteDevice({ | ||
unavailableCapabilities: { | ||
dash: 'no-support', | ||
}, | ||
state: deviceState, | ||
}); | ||
const unavailableCapabilities = device?.unavailableCapabilities ?? {}; | ||
|
||
const receiveNetworks = networksCollection.filter( | ||
(n: Network) => | ||
n.symbol === receiveNetwork && | ||
!unavailableCapabilities[n.symbol] && | ||
((n.isDebugOnlyNetwork && isDebug) || !n.isDebugOnlyNetwork), | ||
); | ||
|
||
return filterReceiveAccounts({ | ||
accounts, | ||
deviceState: device.state, | ||
receiveNetwork, | ||
isDebug, | ||
receiveNetworks, | ||
}); | ||
}; | ||
|
||
describe('filter receive accounts', () => { | ||
it('checks if account is debug only type', () => { | ||
expect(isDebugOnlyAccountType('legacy', 'btc')).toBe(false); | ||
expect(isDebugOnlyAccountType('segwit', 'btc')).toBe(false); | ||
expect(isDebugOnlyAccountType('coinjoin', 'btc')).toBe(false); | ||
expect(isDebugOnlyAccountType('taproot', 'btc')).toBe(false); | ||
expect(isDebugOnlyAccountType('ledger', 'btc')).toBe(false); | ||
expect(isDebugOnlyAccountType('legacy', 'eth')).toBe(true); | ||
expect(isDebugOnlyAccountType('ledger', 'eth')).toBe(true); | ||
expect(isDebugOnlyAccountType('normal', 'regtest')).toBe(false); | ||
}); | ||
|
||
it('returns no results when given an empty accounts array', () => { | ||
expect(runFilterReceiveAccouns({ accounts: [] })).toEqual([]); | ||
}); | ||
|
||
it('returns no results when given a non-existing network in acccounts list', () => { | ||
expect(runFilterReceiveAccouns({ receiveNetwork: 'bnb' })).toEqual([]); | ||
}); | ||
|
||
it('returns all accounts when debug mode is on', () => { | ||
const filteredAccounts = [ | ||
getWalletAccount({ symbol: 'eth', accountType: 'legacy' }), | ||
getWalletAccount({ symbol: 'eth', accountType: 'normal' }), | ||
getWalletAccount({ symbol: 'eth', accountType: 'ledger' }), | ||
]; | ||
expect(runFilterReceiveAccouns({})).toEqual(filteredAccounts); | ||
}); | ||
|
||
it('returns only non-debug accounts when debug mode is off', () => { | ||
const filteredAccounts = [getWalletAccount({ symbol: 'eth', accountType: 'normal' })]; | ||
|
||
expect(runFilterReceiveAccouns({ isDebug: false })).toEqual(filteredAccounts); | ||
}); | ||
|
||
it('returns no results when device is not the same', () => { | ||
expect(runFilterReceiveAccouns({ deviceState: '2ndTestnetAddress@device_id:0' })).toEqual( | ||
[], | ||
); | ||
}); | ||
|
||
it('excludes coinjoin accounts for BTC network (also tests isAnotherNetwork and isCoinjoinAccount methods)', () => { | ||
const filteredAccounts = [ | ||
getWalletAccount({ symbol: 'btc', accountType: 'taproot' }), | ||
getWalletAccount({ symbol: 'btc', accountType: 'legacy' }), | ||
getWalletAccount({ symbol: 'btc', accountType: 'segwit' }), | ||
getWalletAccount({ symbol: 'btc', accountType: 'ledger' }), | ||
]; | ||
|
||
expect(runFilterReceiveAccouns({ receiveNetwork: 'btc' })).toEqual(filteredAccounts); | ||
}); | ||
|
||
it('returns account when when its either first normal account (no matter is empty or not visible) or it is not empty and visible', () => { | ||
const filteredAccounts = [ | ||
getWalletAccount({ symbol: 'sol', accountType: 'normal', empty: true, visible: false }), | ||
getWalletAccount({ symbol: 'sol', accountType: 'ledger' }), | ||
]; | ||
|
||
expect(runFilterReceiveAccouns({ receiveNetwork: 'sol' })).toEqual(filteredAccounts); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { AccountType, getNetwork, Network, NetworkSymbol } from '@suite-common/wallet-config'; | ||
import { Account } from '@suite-common/wallet-types'; | ||
|
||
export const isDebugOnlyAccountType = ( | ||
accountType: AccountType, | ||
symbol?: NetworkSymbol, | ||
): boolean => { | ||
if (!symbol) return false; | ||
|
||
const network = getNetwork(symbol); | ||
|
||
const accountTypeInfo = network.accountTypes[accountType]; | ||
|
||
return !!accountTypeInfo?.isDebugOnlyAccountType; | ||
}; | ||
|
||
type FilterReceiveAccountsProps = { | ||
accounts: Account[]; | ||
deviceState: string | undefined; | ||
receiveNetwork?: string; | ||
isDebug: boolean; | ||
receiveNetworks: Network[]; | ||
}; | ||
|
||
export const filterReceiveAccounts = ({ | ||
accounts, | ||
deviceState, | ||
receiveNetwork, | ||
isDebug, | ||
}: FilterReceiveAccountsProps): Account[] => { | ||
const isSameDevice = (account: Account) => account.deviceState === deviceState; | ||
const isSameNetwork = (account: Account) => account.symbol === receiveNetwork; | ||
const shouldDisplayDebugOnly = (account: Account) => | ||
isDebug || !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; | ||
const isCoinjoinAccount = (account: Account) => account.accountType === 'coinjoin'; | ||
|
||
return accounts.filter( | ||
account => | ||
isSameDevice(account) && | ||
isSameNetwork(account) && | ||
!isCoinjoinAccount(account) && | ||
shouldDisplayDebugOnly(account) && | ||
(isNotEmptyAccount(account) || | ||
isVisibleAccount(account) || | ||
isFirstNormalAccount(account)), | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters