Skip to content

Commit

Permalink
fix(swap): Show custom error message when ADA app on Ledger is closed (
Browse files Browse the repository at this point in the history
…#2776)

Co-authored-by: Juliano Lazzarotto <30806844+stackchain@users.noreply.github.com>
  • Loading branch information
michaeljscript and stackchain authored Oct 15, 2023
1 parent 41c9e7a commit 207e854
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {StyleSheet, View} from 'react-native'

import {Button, Icon, Text} from '../../../../components'
import {
AdaAppClosedError,
BluetoothDisabledError,
GeneralConnectionError,
LedgerUserError,
Expand Down Expand Up @@ -46,7 +47,8 @@ const getErrorMessage = (
| 'bluetoothDisabledError'
| 'ledgerUserError'
| 'ledgerGeneralConnectionError'
| 'ledgerBluetoothDisabledError',
| 'ledgerBluetoothDisabledError'
| 'ledgerAdaAppNeedsToBeOpenError',
string
>,
): string => {
Expand All @@ -70,6 +72,10 @@ const getErrorMessage = (
return strings.ledgerBluetoothDisabledError
}

if (error instanceof AdaAppClosedError) {
return strings.ledgerAdaAppNeedsToBeOpenError
}

return `${strings.error}: ${error.message}`
}

Expand Down
1 change: 1 addition & 0 deletions apps/wallet-mobile/src/features/Swap/common/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export const useStrings = () => {
ledgerBluetoothDisabledError: intl.formatMessage(ledgerMessages.bluetoothDisabledError),
ledgerGeneralConnectionError: intl.formatMessage(ledgerMessages.connectionError),
ledgerUserError: intl.formatMessage(ledgerMessages.connectionError),
ledgerAdaAppNeedsToBeOpenError: intl.formatMessage(ledgerMessages.appOpened),
}
}

Expand Down
46 changes: 29 additions & 17 deletions apps/wallet-mobile/src/yoroi-wallets/cardano/hw/hw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {BleError} from 'react-native-ble-plx'
import {ledgerMessages} from '../../../i18n/global-messages'
import LocalizableError from '../../../i18n/LocalizableError'
import {
AdaAppClosedError,
DeviceId,
DeviceObj,
GeneralConnectionError,
Expand Down Expand Up @@ -85,8 +86,15 @@ const isRejectedError = (e: Error | any): boolean => {
return false
}

const isAdaAppClosedError = (e: Error | unknown): boolean => {
return e instanceof Error && e.message.includes('0x6e01')
}

const mapLedgerError = (e: Error | any): Error | LocalizableError => {
if (isUserError(e)) {
if (isAdaAppClosedError(e)) {
Logger.info('ledgerUtils::mapLedgerError: Ada app closed', e)
return new AdaAppClosedError()
} else if (isUserError(e)) {
Logger.info('ledgerUtils::mapLedgerError: User-side error', e)
return new LedgerUserError()
} else if (isRejectedError(e)) {
Expand Down Expand Up @@ -174,25 +182,29 @@ const connectionHandler = async (
): Promise<AppAda> => {
let transport

if (useUSB) {
if (deviceObj == null) {
throw new Error('ledgerUtils::connectionHandler deviceObj is null')
}

transport = await TransportHID.open(deviceObj)
} else {
if (deviceId == null) {
throw new Error('ledgerUtils::connectionHandler deviceId is null')
try {
if (useUSB) {
if (deviceObj == null) {
throw new Error('ledgerUtils::connectionHandler deviceObj is null')
}

transport = await TransportHID.open(deviceObj)
} else {
if (deviceId == null) {
throw new Error('ledgerUtils::connectionHandler deviceId is null')
}

transport = await TransportBLE.open(deviceId)
}

transport = await TransportBLE.open(deviceId)
const appAda = new AppAda(transport)
const versionResp: GetVersionResponse = await appAda.getVersion()
Logger.debug('ledgerUtils::connectionHandler: AppAda version', versionResp)
checkDeviceVersion(versionResp)
return appAda
} catch (e) {
throw mapLedgerError(e)
}

const appAda = new AppAda(transport)
const versionResp: GetVersionResponse = await appAda.getVersion()
Logger.debug('ledgerUtils::connectionHandler: AppAda version', versionResp)
checkDeviceVersion(versionResp)
return appAda
}

export const getHWDeviceInfo = async (
Expand Down
9 changes: 9 additions & 0 deletions apps/wallet-mobile/src/yoroi-wallets/hw/hw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ export class RejectedByUserError extends LocalizableError {
}
}

export class AdaAppClosedError extends LocalizableError {
constructor() {
super({
id: ledgerMessages.appOpened.id,
defaultMessage: ledgerMessages.appOpened.defaultMessage,
})
}
}

export const HARDWARE_WALLETS = {
LEDGER_NANO: {
ENABLED: true,
Expand Down

0 comments on commit 207e854

Please sign in to comment.