Skip to content

Commit

Permalink
Add fix
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljscript committed Dec 21, 2024
1 parent 00f8e03 commit 3789e1d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
import {useTheme} from '@yoroi/theme'
import {HW} from '@yoroi/types'
import React, {useCallback, useState} from 'react'
import {ErrorBoundary} from 'react-error-boundary'
import {ActivityIndicator, ScrollView, StyleSheet, View} from 'react-native'
import {useMutation} from 'react-query'

import {LedgerTransportSwitch} from '../../../components/LedgerTransportSwitch/LedgerTransportSwitch'
import {useModal} from '../../../components/Modal/ModalContext'
import {ModalError} from '../../../components/ModalError/ModalError'
import {Text} from '../../../components/Text'
import {LedgerConnect} from '../../../legacy/HW'
import {withBLE, withUSB} from '../../../yoroi-wallets/hw/hwWallet'
import {useSelectedWallet} from '../../WalletManager/common/hooks/useSelectedWallet'
import {useWalletManager} from '../../WalletManager/context/WalletManagerProvider'
import {useStrings} from './useStrings'
import {ErrorBoundary} from 'react-error-boundary'
import {ModalError} from '../../../components/ModalError/ModalError'
import {useMutation} from 'react-query'

type TransportType = 'USB' | 'BLE'
type Step = 'select-transport' | 'connect-transport' | 'loading'
type OnConfirmOptions = {transportType: TransportType; deviceInfo: HW.DeviceInfo}

type Props = {
onConfirm: (options: {transportType: TransportType; deviceInfo: HW.DeviceInfo}) => Promise<void>
onConfirm: (options: OnConfirmOptions) => Promise<void>
onClose: () => void
onCancel: () => void
}

const modalHeight = 350
Expand All @@ -28,12 +31,12 @@ export const useConfirmHWConnectionModal = () => {
const {openModal, closeModal} = useModal()
const strings = useStrings()
const confirmHWConnection = useCallback(
({onConfirm, onClose}: {onConfirm: Props['onConfirm']; onClose: () => void}) => {
({onConfirm, onClose, onCancel}: Props) => {
openModal(
strings.signTransaction,
<ErrorBoundary
fallbackRender={({error, resetErrorBoundary}) => (
<ModalError error={error} resetErrorBoundary={resetErrorBoundary} onCancel={onClose} />
<ModalError error={error} resetErrorBoundary={resetErrorBoundary} onCancel={onCancel} />
)}
>
<ConfirmHWConnectionModal onConfirm={onConfirm} />
Expand All @@ -47,14 +50,17 @@ export const useConfirmHWConnectionModal = () => {
return {confirmHWConnection, closeModal}
}

const ConfirmHWConnectionModal = ({onConfirm}: Props) => {
const ConfirmHWConnectionModal = ({onConfirm}: Pick<Props, 'onConfirm'>) => {
const {walletManager} = useWalletManager()
const [transportType, setTransportType] = useState<TransportType>('USB')
const [step, setStep] = useState<Step>('select-transport')
const {meta} = useSelectedWallet()
const strings = useStrings()
const {styles, colors} = useStyles()
const useOnConfirm = useMutation<any, any, any>({mutationFn: onConfirm, mutationKey: ['asd'], useErrorBoundary: true})
const {mutate: handleOnConfirm} = useMutation<void, Error, OnConfirmOptions>({
mutationFn: onConfirm,
useErrorBoundary: true,
})

const onSelectTransport = (transportType: TransportType) => {
setTransportType(transportType)
Expand All @@ -65,14 +71,14 @@ const ConfirmHWConnectionModal = ({onConfirm}: Props) => {
setStep('loading')
const hwDeviceInfo = withBLE(meta, deviceId)
walletManager.updateWalletHWDeviceInfo(meta.id, hwDeviceInfo)
useOnConfirm.mutate({transportType: 'BLE', deviceInfo: hwDeviceInfo})
handleOnConfirm({transportType: 'BLE', deviceInfo: hwDeviceInfo})
}

const onConnectUSB = (deviceObj: HW.DeviceObj) => {
setStep('loading')
const hwDeviceInfo = withUSB(meta, deviceObj)
walletManager.updateWalletHWDeviceInfo(meta.id, hwDeviceInfo)
useOnConfirm.mutate({transportType: 'USB', deviceInfo: hwDeviceInfo})
handleOnConfirm({transportType: 'USB', deviceInfo: hwDeviceInfo})
}

if (step === 'select-transport') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import {useMutation} from 'react-query'
import {logger} from '../../kernel/logger/logger'
import {useWalletNavigation} from '../../kernel/navigation'
import {cip30LedgerExtensionMaker} from '../../yoroi-wallets/cardano/cip30/cip30-ledger'
import {BaseLedgerError} from '../../yoroi-wallets/hw/hw'
import {CreatedByInfoItem} from '../ReviewTx/useCases/ReviewTxScreen/ReviewTx/Overview/OverviewTab'
import {useSelectedWallet} from '../WalletManager/common/hooks/useSelectedWallet'
import {useBrowser} from './common/BrowserProvider'
import {useOpenConfirmConnectionModal} from './common/ConfirmConnectionModal'
import {useConfirmHWConnectionModal} from './common/ConfirmHWConnectionModal'
import {isUserRejectedError, userRejectedError} from './common/errors'
import {userRejectedError} from './common/errors'
import {createDappConnector} from './common/helpers'
import {usePromptRootKey} from './common/hooks'
import {useShowHWNotSupportedModal} from './common/HWNotSupportedModal'
Expand Down Expand Up @@ -253,17 +254,32 @@ export const useSignTxWithHW = () => {
const mutationFn = React.useCallback(
(options: {cbor: string; partial?: boolean}) => {
return new Promise<Transaction>((resolve, reject) => {
let shouldResolveOnClose = true
let isClosed = false
confirmHWConnection({
onConfirm: async ({transportType, deviceInfo}) => {
const cip30 = cip30LedgerExtensionMaker(wallet, meta)
const tx = await cip30.signTx(options.cbor, options.partial ?? false, deviceInfo, transportType === 'USB')
shouldResolveOnClose = false
// TODO: Fix here
return resolve(tx)
try {
const cip30 = cip30LedgerExtensionMaker(wallet, meta)
const tx = await cip30.signTx(options.cbor, options.partial ?? false, deviceInfo, transportType === 'USB')
resolve(tx)
isClosed = true
closeModal()
} catch (error) {
if (error instanceof BaseLedgerError) {
throw error
}
reject(error)
isClosed = true
closeModal()
}
},
onCancel: () => {
reject(userRejectedError())
isClosed = true
closeModal()
},
onClose: () => {
if (shouldResolveOnClose) reject(userRejectedError())
if (isClosed) return
reject(userRejectedError())
},
})
})
Expand Down
3 changes: 2 additions & 1 deletion apps/wallet-mobile/src/yoroi-wallets/cardano/hw/hw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {LocalizableError} from '../../../kernel/i18n/LocalizableError'
import {logger} from '../../../kernel/logger/logger'
import {
AdaAppClosedError,
BaseLedgerError,
GeneralConnectionError,
HARDWARE_WALLETS,
LedgerUserError,
Expand All @@ -40,7 +41,7 @@ type LedgerConnectionResponse = {
serialHex: string
}

export class DeprecatedAdaAppError extends LocalizableError {
export class DeprecatedAdaAppError extends BaseLedgerError {
constructor() {
super({
id: ledgerMessages.deprecatedAdaAppError.id,
Expand Down
12 changes: 7 additions & 5 deletions apps/wallet-mobile/src/yoroi-wallets/hw/hw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,17 @@ const getLedgerPermissions = () => {
return permissions
}

export class BluetoothDisabledError extends LocalizableError {
export class BaseLedgerError extends LocalizableError {}

export class BluetoothDisabledError extends BaseLedgerError {
constructor() {
super({
id: ledgerMessages.bluetoothDisabledError.id,
defaultMessage: ledgerMessages.bluetoothDisabledError.defaultMessage,
})
}
}
export class GeneralConnectionError extends LocalizableError {
export class GeneralConnectionError extends BaseLedgerError {
constructor() {
super({
id: ledgerMessages.connectionError.id,
Expand All @@ -56,15 +58,15 @@ export class GeneralConnectionError extends LocalizableError {
}
}
// note: uses same message as above.
export class LedgerUserError extends LocalizableError {
export class LedgerUserError extends BaseLedgerError {
constructor() {
super({
id: ledgerMessages.connectionError.id,
defaultMessage: ledgerMessages.connectionError.defaultMessage,
})
}
}
export class RejectedByUserError extends LocalizableError {
export class RejectedByUserError extends BaseLedgerError {
constructor() {
super({
id: ledgerMessages.rejectedByUserError.id,
Expand All @@ -73,7 +75,7 @@ export class RejectedByUserError extends LocalizableError {
}
}

export class AdaAppClosedError extends LocalizableError {
export class AdaAppClosedError extends BaseLedgerError {
constructor() {
super({
id: ledgerMessages.appOpened.id,
Expand Down

0 comments on commit 3789e1d

Please sign in to comment.