|
5 | 5 | type TypedTransaction, |
6 | 6 | } from '@ethereumjs/tx'; |
7 | 7 | import { publicToAddress } from '@ethereumjs/util'; |
| 8 | +import { TransportStatusError } from '@ledgerhq/hw-transport'; |
8 | 9 | import type { MessageTypes, TypedMessage } from '@metamask/eth-sig-util'; |
9 | 10 | import { |
10 | 11 | recoverPersonalSignature, |
@@ -424,27 +425,29 @@ export class LedgerKeyring implements Keyring { |
424 | 425 | tx: remove0x(rawTxHex), |
425 | 426 | hdPath, |
426 | 427 | }); |
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 | + } |
447 | 449 | } |
| 450 | + |
448 | 451 | throw error instanceof Error |
449 | 452 | ? error |
450 | 453 | : new Error('Ledger: Unknown error while signing transaction'); |
@@ -479,7 +482,19 @@ export class LedgerKeyring implements Keyring { |
479 | 482 | hdPath, |
480 | 483 | message: remove0x(message), |
481 | 484 | }); |
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 | + } |
483 | 498 | /** |
484 | 499 | * for user rejected the transaction error |
485 | 500 | */ |
@@ -582,26 +597,26 @@ export class LedgerKeyring implements Keyring { |
582 | 597 | message, |
583 | 598 | }, |
584 | 599 | }); |
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 | + } |
605 | 620 | } |
606 | 621 | throw error instanceof Error |
607 | 622 | ? error |
|
0 commit comments