Skip to content

Commit

Permalink
Show alert when attempting to sign with Ledger device disconnected (#826
Browse files Browse the repository at this point in the history
)
  • Loading branch information
brunobar79 authored Aug 2, 2023
1 parent 7b13e07 commit ec1d9cb
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/core/languages/_english.json
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,9 @@
"waiting_for_trezor_connect": "Waiting for Trezor Connect",
"continue_on_trezor_connect": "Please follow the instructions on the Trezor Connect window",
"trezor_success": "Success!",
"you_can_close_this_window": "You can close this window"
"you_can_close_this_window": "You can close this window",
"ledger_locked_error": "Please make sure your ledger is unlocked and open the Ethereum app",
"check_ledger_disconnected": "Make sure your device is connected, unlocked and the ethereum app is open"
},
"choose_wallet_group": {
"title": "Choose wallet group",
Expand Down
27 changes: 24 additions & 3 deletions src/entries/popup/handlers/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getProvider } from '@wagmi/core';
import { ethers } from 'ethers';
import { Address } from 'wagmi';

import { i18n } from '~/core/languages';
import { ChainId } from '~/core/types/chains';

import { walletAction } from './walletAction';
Expand Down Expand Up @@ -84,9 +85,12 @@ export async function signTransactionFromLedger(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
if (e?.name === 'TransportStatusError' || e?.name === 'LockedDeviceError') {
alert(
'Please make sure your ledger is unlocked and open the Ethereum app',
);
alert(i18n.t('hw.ledger_locked_error'));
} else if (
e?.name === 'TransportOpenUserCancelled' &&
e?.message === 'Access denied to use Ledger device'
) {
alert(i18n.t('hw.check_ledger_disconnected'));
} else if (e?.message) {
alert(e.message);
}
Expand Down Expand Up @@ -175,3 +179,20 @@ export async function signMessageByTypeFromLedger(
throw new Error(`Message type ${messageType} not supported`);
}
}

export const showLedgerDisconnectedAlertIfNeeded = (e: Error) => {
if (
e.name === 'TransportOpenUserCancelled' &&
e.message === 'Access denied to use Ledger device'
) {
alert(i18n.t('hw.check_ledger_disconnected'));
}
};

export const isLedgerConnectionError = (e: Error) => {
return [
'TransportOpenUserCancelled',
'TransportStatusError',
'LockedDeviceError',
].includes(e?.name);
};
5 changes: 3 additions & 2 deletions src/entries/popup/pages/messages/RequestAccounts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ export const RequestAccounts = ({
dappURL: appHost,
dappName: appName,
});
} catch (e) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
logger.info('error connecting to dapp');
logger.error(e as RainbowError);
logger.error(new RainbowError(e.name), { message: e.message });
} finally {
setLoading(false);
}
Expand Down
4 changes: 4 additions & 0 deletions src/entries/popup/pages/messages/SendTransaction/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { TransactionStatus, TransactionType } from '~/core/types/transactions';
import { addNewTransaction } from '~/core/utils/transactions';
import { Row, Rows } from '~/design-system';
import { triggerAlert } from '~/design-system/components/Alert/util';
import { showLedgerDisconnectedAlertIfNeeded } from '~/entries/popup/handlers/ledger';
import { useSendAsset } from '~/entries/popup/hooks/send/useSendAsset';
import { useAppMetadata } from '~/entries/popup/hooks/useAppMetadata';
import { useAppSession } from '~/entries/popup/hooks/useAppSession';
Expand Down Expand Up @@ -119,6 +120,9 @@ export function SendTransaction({
dappName: appName,
});
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
showLedgerDisconnectedAlertIfNeeded(e);
} finally {
setWaitingForDevice(false);
setLoading(false);
Expand Down
7 changes: 5 additions & 2 deletions src/entries/popup/pages/messages/SignMessage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { RPCMethod } from '~/core/types/rpcMethods';
import { getSigningRequestDisplayDetails } from '~/core/utils/signMessages';
import { Box } from '~/design-system';
import { triggerAlert } from '~/design-system/components/Alert/util';
import { showLedgerDisconnectedAlertIfNeeded } from '~/entries/popup/handlers/ledger';
import { useAppMetadata } from '~/entries/popup/hooks/useAppMetadata';
import { useAppSession } from '~/entries/popup/hooks/useAppSession';
import { useWallets } from '~/entries/popup/hooks/useWallets';
Expand Down Expand Up @@ -90,9 +91,11 @@ export function SignMessage({
});
}
approveRequest(result);
} catch (e) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
showLedgerDisconnectedAlertIfNeeded(e);
logger.info('error in sign message');
logger.error(e as RainbowError);
logger.error(new RainbowError(e.name), { message: e.message });
} finally {
setWaitingForDevice(false);
setLoading(false);
Expand Down
8 changes: 6 additions & 2 deletions src/entries/popup/pages/send/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
} from '../../components/ExplainerSheet/ExplainerSheet';
import { Navbar } from '../../components/Navbar/Navbar';
import { TransactionFee } from '../../components/TransactionFee/TransactionFee';
import { isLedgerConnectionError } from '../../handlers/ledger';
import { getWallet, sendTransaction } from '../../handlers/wallet';
import { useSendAsset } from '../../hooks/send/useSendAsset';
import { useSendInputs } from '../../hooks/send/useSendInputs';
Expand Down Expand Up @@ -234,8 +235,11 @@ export function Send() {
chainId,
});
}
} catch (e) {
alert('Transaction failed');
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
if (!isLedgerConnectionError(e)) {
alert('Transaction failed');
}
logger.error(new RainbowError('send: error executing send'), {
message: (e as Error)?.message,
});
Expand Down
1 change: 1 addition & 0 deletions src/entries/popup/pages/unlock/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function Unlock() {
setError(i18n.t('passwords.wrong_password'));
setLoading(false);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e) {
logger.info('Unlock error: exception while trying to unlock');
logger.error(e as RainbowError);
Expand Down
5 changes: 3 additions & 2 deletions src/entries/popup/pages/welcome/ImportOrCreateWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ export function ImportOrCreateWallet() {
const seedPhrase = await wallet.exportWallet(newWalletAddress, '');
setImportWalletSecrets([seedPhrase]);
navigate(ROUTES.SEED_BACKUP_PROMPT);
} catch (e) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
logger.info('Onboarding error: creating new wallet failed');
logger.error(e as RainbowError);
logger.error(new RainbowError(e?.name), { message: e?.message });
setLoading(false);
}
}, [loading, navigate, setCurrentAddress]);
Expand Down

0 comments on commit ec1d9cb

Please sign in to comment.