Skip to content

Commit

Permalink
Merge pull request #2124 from oasisprotocol/lw/harden-against-wrong-a…
Browse files Browse the repository at this point in the history
…cc-type

Harden code against wrong imported account type
  • Loading branch information
lukaw3d authored Feb 21, 2025
2 parents 08f5418 + 3c5229d commit 693900f
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 50 deletions.
1 change: 1 addition & 0 deletions .changelog/2124.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Harden code against wrong imported account type
2 changes: 0 additions & 2 deletions src/app/pages/CreateWalletPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { ImportAccountsSelectionModal } from 'app/pages/OpenWalletPage/Features/
import { selectShowAccountsSelectionModal } from 'app/state/importaccounts/selectors'
import { createWalletActions } from './slice'
import { selectCheckbox, selectMnemonic } from './slice/selectors'
import { WalletType } from 'app/state/wallet/types'

export interface CreateWalletProps {}

Expand Down Expand Up @@ -95,7 +94,6 @@ export function CreateWalletPage(props: CreateWalletProps) {
abort={() => {
dispatch(importAccountsActions.clear())
}}
type={WalletType.Mnemonic}
/>
)}
<Grid gap="small" pad="small" columns={size === 'small' ? ['auto'] : ['2fr', '2fr']}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ export function FromBleLedger() {
abort={() => {
dispatch(importAccountsActions.clear())
}}
type={WalletType.BleLedger}
/>
)}
</Box>
Expand Down
2 changes: 0 additions & 2 deletions src/app/pages/OpenWalletPage/Features/FromMnemonic/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { MnemonicValidation } from 'app/components/MnemonicValidation'
import { importAccountsActions } from 'app/state/importaccounts'
import { ImportAccountsSelectionModal } from 'app/pages/OpenWalletPage/Features/ImportAccountsSelectionModal'
import { selectShowAccountsSelectionModal } from 'app/state/importaccounts/selectors'
import { WalletType } from 'app/state/wallet/types'

export function FromMnemonic() {
const dispatch = useDispatch()
Expand All @@ -20,7 +19,6 @@ export function FromMnemonic() {
abort={() => {
dispatch(importAccountsActions.clear())
}}
type={WalletType.Mnemonic}
/>
)}
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export function FromUsbLedger() {
abort={() => {
dispatch(importAccountsActions.clear())
}}
type={WalletType.UsbLedger}
/>
)}
</Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const renderComponent = (store: any, abortFunction = () => {}) =>
render(
<Provider store={store}>
<ThemeProvider>
<ImportAccountsSelectionModal abort={abortFunction} type={WalletType.UsbLedger} />
<ImportAccountsSelectionModal abort={abortFunction} />
</ThemeProvider>
</Provider>,
)
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('<ImportAccountsSelectionModal />', () => {
pathDisplay: `m/44'/474'/0'`,
publicKey: '00',
selected: false,
type: WalletType.Mnemonic,
type: WalletType.UsbLedger,
},
]),
)
Expand All @@ -82,7 +82,7 @@ describe('<ImportAccountsSelectionModal />', () => {
pathDisplay: `m/44'/474'/0'`,
publicKey: '00',
selected: false,
type: WalletType.Mnemonic,
type: WalletType.UsbLedger,
},
{
address: 'oasis1qqv25adrld8jjquzxzg769689lgf9jxvwgjs8tha',
Expand All @@ -91,7 +91,7 @@ describe('<ImportAccountsSelectionModal />', () => {
pathDisplay: `m/44'/474'/1'`,
publicKey: '00',
selected: false,
type: WalletType.Mnemonic,
type: WalletType.UsbLedger,
},
]),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { ResponsiveContext } from 'grommet/es6/contexts/ResponsiveContext'
import { Spinner } from 'grommet/es6/components/Spinner'
import { Text } from 'grommet/es6/components/Text'
import { numberOfAccountPages } from 'app/state/importaccounts/saga'
import { WalletType, LedgerWalletType } from 'app/state/wallet/types'
import { WalletType } from 'app/state/wallet/types'
import { ChoosePasswordFields } from 'app/components/Persist/ChoosePasswordFields'
import { FormValue as ChoosePasswordFieldsFormValue } from 'app/components/Persist/ChoosePasswordInputFields'
import { preventSavingInputsToUserData } from 'app/lib/preventSavingInputsToUserData'
Expand All @@ -47,7 +47,6 @@ function ImportAccountsSelector({ accounts }: ImportAccountsSelectorSelectorProp

interface ImportAccountsSelectionModalProps {
abort: () => void
type: WalletType.Mnemonic | LedgerWalletType
}

interface FormValue extends ChoosePasswordFieldsFormValue {}
Expand All @@ -61,10 +60,11 @@ export function ImportAccountsSelectionModal(props: ImportAccountsSelectionModal
const error = importAccounts.error
const selectedAccounts = useSelector(selectSelectedAccounts)
const dispatch = useDispatch()
const type = accounts.length > 0 ? accounts[0].type : undefined

const openAccounts = ({ value }: { value: FormValue }) => {
dispatch(
props.type === WalletType.UsbLedger
type === WalletType.UsbLedger
? walletActions.openWalletsFromLedger({ choosePassword: value.password2 })
: walletActions.openWalletFromMnemonic({ choosePassword: value.password2 }),
)
Expand All @@ -83,8 +83,8 @@ export function ImportAccountsSelectionModal(props: ImportAccountsSelectionModal

const onNext = () => {
dispatch(importAccountsActions.setPage(pageNum + 1))
if (props.type === WalletType.UsbLedger || props.type === WalletType.BleLedger) {
dispatch(importAccountsActions.enumerateMoreAccountsFromLedger(props.type))
if (type === WalletType.UsbLedger || type === WalletType.BleLedger) {
dispatch(importAccountsActions.enumerateMoreAccountsFromLedger(type))
}
}

Expand Down
20 changes: 1 addition & 19 deletions src/app/pages/OpenWalletPage/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import React from 'react'
import { render, screen, waitFor } from '@testing-library/react'
import { render } from '@testing-library/react'
import { MemoryRouter } from 'react-router-dom'
import { useSelector } from 'react-redux'
import { when } from 'jest-when'
import { selectShowAccountsSelectionModal } from 'app/state/importaccounts/selectors'
import { SelectOpenMethod } from '..'

jest.mock('app/lib/ledger', () => ({
Expand Down Expand Up @@ -39,18 +35,4 @@ describe('<SelectOpenMethod />', () => {

expect(container).toMatchSnapshot()
})

it('should redirect user to ledger page', async () => {
when(useSelector as any)
.calledWith(selectShowAccountsSelectionModal)
.mockReturnValue(true)

renderComponent()

await waitFor(() => {
expect(screen.queryByText('openWallet.method.ledger')).toBeInTheDocument()

expect(mockNavigate).toHaveBeenCalledWith('/open-wallet/ledger/usb')
})
})
})
16 changes: 1 addition & 15 deletions src/app/pages/OpenWalletPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,13 @@
*/
import { Anchor } from 'grommet/es6/components/Anchor'
import { Box } from 'grommet/es6/components/Box'
import React, { useEffect } from 'react'
import { useSelector } from 'react-redux'
import { useNavigate } from 'react-router-dom'
import React from 'react'
import { Trans, useTranslation } from 'react-i18next'
import { ButtonLink } from 'app/components/ButtonLink'
import { Header } from 'app/components/Header'
import { selectShowAccountsSelectionModal } from 'app/state/importaccounts/selectors'
import { runtimeIs } from 'config'

export function SelectOpenMethod() {
const { t } = useTranslation()
const navigate = useNavigate()
const showAccountsSelectionModal = useSelector(selectShowAccountsSelectionModal)

useEffect(() => {
const isExtension = runtimeIs === 'extension'

if (isExtension && showAccountsSelectionModal) {
navigate('/open-wallet/ledger/usb')
}
}, [navigate, showAccountsSelectionModal])

return (
<Box
Expand Down
2 changes: 1 addition & 1 deletion src/app/state/wallet/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function* openWalletsFromLedger({ payload }: PayloadAction<OpenSelectedAc
yield* call(addWallet, {
address: account.address,
publicKey: account.publicKey,
type: WalletType.UsbLedger,
type: account.type,
balance: account.balance!,
path: account.path,
pathDisplay: account.pathDisplay,
Expand Down

0 comments on commit 693900f

Please sign in to comment.