Skip to content

Commit 2d5399c

Browse files
committed
feat: enhance error handling for Ledger transactions
Added static method to identify Ledger-specific errors and improved error handling in the LedgerKeyring class. Now, specific error messages are thrown for user-rejected transactions and when blind signing is disabled, providing clearer feedback to users during transaction signing.
1 parent f9f56e8 commit 2d5399c

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ export class LedgerKeyring implements Keyring {
9494

9595
deviceId = '';
9696

97+
static #isLedgerError(
98+
error: unknown,
99+
): error is { errorCode: string; message: string } {
100+
return (
101+
typeof error === 'object' &&
102+
error !== null &&
103+
'errorCode' in error &&
104+
'message' in error
105+
);
106+
}
107+
97108
readonly type: string = keyringType;
98109

99110
page = 0;
@@ -414,6 +425,26 @@ export class LedgerKeyring implements Keyring {
414425
hdPath,
415426
});
416427
} 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');
447+
}
417448
throw error instanceof Error
418449
? error
419450
: new Error('Ledger: Unknown error while signing transaction');
@@ -449,6 +480,17 @@ export class LedgerKeyring implements Keyring {
449480
message: remove0x(message),
450481
});
451482
} catch (error) {
483+
/**
484+
* for user rejected the transaction error
485+
*/
486+
if (
487+
LedgerKeyring.#isLedgerError(error) &&
488+
error.errorCode === '6985' &&
489+
error.message === 'Condition not satisfied'
490+
) {
491+
throw new Error('Ledger: User rejected the transaction');
492+
}
493+
452494
throw error instanceof Error
453495
? error
454496
: new Error('Ledger: Unknown error while signing message');
@@ -541,6 +583,26 @@ export class LedgerKeyring implements Keyring {
541583
},
542584
});
543585
} 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');
605+
}
544606
throw error instanceof Error
545607
? error
546608
: new Error('Ledger: Unknown error while signing message');

0 commit comments

Comments
 (0)