Skip to content

Commit 8d08f2e

Browse files
committed
Improve error handling to handle TransportStatusError.
1 parent 2d5399c commit 8d08f2e

1 file changed

Lines changed: 56 additions & 41 deletions

File tree

packages/keyring-eth-ledger-bridge/src/ledger-keyring.ts

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
type TypedTransaction,
66
} from '@ethereumjs/tx';
77
import { publicToAddress } from '@ethereumjs/util';
8+
import { TransportStatusError } from '@ledgerhq/hw-transport';
89
import type { MessageTypes, TypedMessage } from '@metamask/eth-sig-util';
910
import {
1011
recoverPersonalSignature,
@@ -424,27 +425,29 @@ export class LedgerKeyring implements Keyring {
424425
tx: remove0x(rawTxHex),
425426
hdPath,
426427
});
427-
} catch (error) {
428-
/**
429-
* for user rejected the transaction error
430-
*/
431-
if (
432-
LedgerKeyring.#isLedgerError(error) &&
433-
error.errorCode === '6985' &&
434-
error.message === 'Condition not satisfied'
435-
) {
436-
throw new Error('Ledger: User rejected the transaction');
437-
}
438-
/**
439-
* for blind signing disabled error
440-
*/
441-
if (
442-
LedgerKeyring.#isLedgerError(error) &&
443-
error.errorCode === '6a80' &&
444-
error.message === 'Invalid data'
445-
) {
446-
throw new Error('Ledger: Blind signing must be enabled');
428+
} catch (error: unknown) {
429+
// check whether error is TransportError
430+
if (error instanceof TransportStatusError) {
431+
// cast error to TransportError
432+
const transportError: TransportStatusError = error;
433+
if (
434+
transportError.statusCode === 27013 &&
435+
transportError.message.includes('(denied by the user?) (0x6985)')
436+
) {
437+
throw new Error('Ledger: User rejected the transaction');
438+
}
439+
440+
/**
441+
* for user rejected the transaction error
442+
*/
443+
if (
444+
transportError.statusCode === 27013 &&
445+
transportError.message.includes('(denied by the user?) (0x6985)')
446+
) {
447+
throw new Error('Ledger: User rejected the transaction');
448+
}
447449
}
450+
448451
throw error instanceof Error
449452
? error
450453
: new Error('Ledger: Unknown error while signing transaction');
@@ -479,7 +482,19 @@ export class LedgerKeyring implements Keyring {
479482
hdPath,
480483
message: remove0x(message),
481484
});
482-
} catch (error) {
485+
} catch (error: unknown) {
486+
if (error instanceof TransportStatusError) {
487+
const transportError: TransportStatusError = error;
488+
/**
489+
* for user rejected the transaction error
490+
*/
491+
if (
492+
transportError.statusCode === 27013 &&
493+
transportError.message.includes('(denied by the user?) (0x6985)')
494+
) {
495+
throw new Error('Ledger: User rejected the transaction');
496+
}
497+
}
483498
/**
484499
* for user rejected the transaction error
485500
*/
@@ -582,26 +597,26 @@ export class LedgerKeyring implements Keyring {
582597
message,
583598
},
584599
});
585-
} catch (error) {
586-
/**
587-
* for user rejected the transaction error
588-
*/
589-
if (
590-
LedgerKeyring.#isLedgerError(error) &&
591-
error.errorCode === '6985' &&
592-
error.message === 'Condition not satisfied'
593-
) {
594-
throw new Error('Ledger: User rejected the transaction');
595-
}
596-
/**
597-
* for blind signing disabled error
598-
*/
599-
if (
600-
LedgerKeyring.#isLedgerError(error) &&
601-
error.errorCode === '6a80' &&
602-
error.message === 'Invalid data'
603-
) {
604-
throw new Error('Ledger: Blind signing must be enabled');
600+
} catch (error: unknown) {
601+
if (error instanceof TransportStatusError) {
602+
// cast error to TransportError
603+
const transportError: TransportStatusError = error;
604+
if (
605+
transportError.statusCode === 27013 &&
606+
transportError.message.includes('(denied by the user?) (0x6985)')
607+
) {
608+
throw new Error('Ledger: User rejected the transaction');
609+
}
610+
611+
/**
612+
* for user rejected the transaction error
613+
*/
614+
if (
615+
transportError.statusCode === 27013 &&
616+
transportError.message.includes('(denied by the user?) (0x6985)')
617+
) {
618+
throw new Error('Ledger: User rejected the transaction');
619+
}
605620
}
606621
throw error instanceof Error
607622
? error

0 commit comments

Comments
 (0)