-
-
Notifications
You must be signed in to change notification settings - Fork 13
feat: use hw wallet error for ledger keyring and add getAppNameAndVersion and getAppConfiguration #446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: use hw wallet error for ledger keyring and add getAppNameAndVersion and getAppConfiguration #446
Changes from 7 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
cf82fac
feat: use hw wallet error
montelaidev 412b185
fix: yarn lock
montelaidev db92337
chore: update change log
montelaidev 365d8b2
fix: use of workspace version
montelaidev 9a97d9f
chore: update readme
montelaidev 42ed4a8
fix: export
montelaidev bc365d1
feat: add appConfiguration
montelaidev ab1f690
Merge remote-tracking branch 'origin' into feat/ledger-use-hardware-w…
montelaidev 01fc524
fix: jest config
montelaidev ce8b9b5
fix: duplicate
montelaidev 45b6958
fix: use utlil
montelaidev 85b4f75
fix: lower jest config statement coverage
montelaidev 9e80e46
refactor: move mapping type
montelaidev 89a0ed9
fix: redundant
montelaidev a1f1df3
fix: update coverage
montelaidev 5ba4671
chore: update change log
montelaidev f42dc3b
Merge remote-tracking branch 'origin/main' into feat/ledger-use-hardw…
montelaidev d87e4d4
fix: update to changed from fix
montelaidev 996c4b3
fix: readd prefix
montelaidev 2081e88
fix: change log
montelaidev 454049a
Merge remote-tracking branch 'origin/main' into feat/ledger-use-hardw…
montelaidev 5d2a5c0
fix: lower coverage
montelaidev cf23a74
fix: jest coverage
montelaidev 61d895d
fix: changelog
montelaidev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { | ||
| HardwareWalletError, | ||
| ErrorCode as ErrorCodeEnum, | ||
| } from '@metamask/hw-wallet-sdk'; | ||
|
|
||
| import { | ||
| createLedgerError, | ||
| isKnownLedgerError, | ||
| getLedgerErrorMapping, | ||
| } from './errors'; | ||
|
|
||
| describe('createLedgerError', () => { | ||
| describe('known error codes', () => { | ||
| it('creates HardwareWalletError for user rejection (0x6985)', () => { | ||
| const error = createLedgerError('0x6985'); | ||
|
|
||
| expect(error).toBeInstanceOf(HardwareWalletError); | ||
| expect(error.code).toBe(ErrorCodeEnum.UserRejected); | ||
| expect(error.message).toBe('User rejected action on device'); | ||
| }); | ||
|
|
||
| it('creates HardwareWalletError for device locked (0x5515)', () => { | ||
| const error = createLedgerError('0x5515'); | ||
|
|
||
| expect(error).toBeInstanceOf(HardwareWalletError); | ||
| expect(error.code).toBe(ErrorCodeEnum.AuthenticationDeviceLocked); | ||
| expect(error.message).toBe('Device is locked'); | ||
| }); | ||
|
|
||
| it('includes context in message when provided', () => { | ||
| const error = createLedgerError('0x6985', 'during sign transaction'); | ||
|
|
||
| expect(error.message).toBe( | ||
| 'User rejected action on device (during sign transaction)', | ||
| ); | ||
| }); | ||
|
|
||
| it('uses error message as userMessage when userMessage is not provided in mapping', () => { | ||
| // 0x9000 is success code which doesn't have a userMessage in the mapping | ||
| const error = createLedgerError('0x9000'); | ||
|
|
||
| expect(error).toBeInstanceOf(HardwareWalletError); | ||
| // userMessage should fallback to the message | ||
| expect(error.userMessage).toBeDefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('unknown error codes', () => { | ||
| it('creates fallback error for unknown code without context', () => { | ||
| const error = createLedgerError('0x9999'); | ||
|
|
||
| expect(error).toBeInstanceOf(HardwareWalletError); | ||
| expect(error.code).toBe(ErrorCodeEnum.Unknown); | ||
| expect(error.message).toBe('Unknown Ledger error: 0x9999'); | ||
| }); | ||
|
|
||
| it('creates fallback error for unknown code with context', () => { | ||
| const error = createLedgerError('0x9999', 'during operation'); | ||
|
|
||
| expect(error).toBeInstanceOf(HardwareWalletError); | ||
| expect(error.code).toBe(ErrorCodeEnum.Unknown); | ||
| expect(error.message).toBe( | ||
| 'Unknown Ledger error: 0x9999 (during operation)', | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('isKnownLedgerError', () => { | ||
| it('returns true for known error codes', () => { | ||
| expect(isKnownLedgerError('0x6985')).toBe(true); | ||
| expect(isKnownLedgerError('0x5515')).toBe(true); | ||
| expect(isKnownLedgerError('0x6a80')).toBe(true); | ||
| }); | ||
|
|
||
| it('returns false for unknown error codes', () => { | ||
| expect(isKnownLedgerError('0x9999')).toBe(false); | ||
| expect(isKnownLedgerError('invalid')).toBe(false); | ||
| expect(isKnownLedgerError('')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getLedgerErrorMapping', () => { | ||
| it('returns mapping for known error codes', () => { | ||
| const mapping = getLedgerErrorMapping('0x6985'); | ||
|
|
||
| expect(mapping).toBeDefined(); | ||
| expect(mapping?.code).toBe(ErrorCodeEnum.UserRejected); | ||
| expect(mapping?.message).toBe('User rejected action on device'); | ||
| }); | ||
|
|
||
| it('returns undefined for unknown error codes', () => { | ||
| const mapping = getLedgerErrorMapping('0x9999'); | ||
|
|
||
| expect(mapping).toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { | ||
| type ErrorCode, | ||
| type Severity, | ||
| type Category, | ||
| HardwareWalletError, | ||
| LEDGER_ERROR_MAPPINGS, | ||
| ErrorCode as ErrorCodeEnum, | ||
| Severity as SeverityEnum, | ||
| Category as CategoryEnum, | ||
| } from '@metamask/hw-wallet-sdk'; | ||
|
|
||
| type LedgerErrorMapping = { | ||
| code: ErrorCode; | ||
| message: string; | ||
| severity: Severity; | ||
| category: Category; | ||
| userMessage?: string; | ||
| }; | ||
|
montelaidev marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Factory function to create a HardwareWalletError from a Ledger error code. | ||
| * | ||
| * @param ledgerErrorCode - The Ledger error code (e.g., '0x6985', '0x5515') | ||
| * @param context - Optional additional context to append to the error message | ||
| * @returns A HardwareWalletError instance with mapped error details | ||
| */ | ||
| export function createLedgerError( | ||
| ledgerErrorCode: string, | ||
| context?: string, | ||
| ): HardwareWalletError { | ||
| const mappings = LEDGER_ERROR_MAPPINGS as { | ||
| [key: string]: LedgerErrorMapping; | ||
| }; | ||
| const errorMapping = mappings[ledgerErrorCode]; | ||
|
montelaidev marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (errorMapping) { | ||
| const message = context | ||
| ? `${errorMapping.message} (${context})` | ||
| : errorMapping.message; | ||
|
|
||
| return new HardwareWalletError(message, { | ||
| code: errorMapping.code, | ||
| severity: errorMapping.severity, | ||
| category: errorMapping.category, | ||
| userMessage: errorMapping.userMessage ?? message, | ||
| }); | ||
| } | ||
|
|
||
| // Fallback for unknown error codes | ||
| const fallbackMessage = context | ||
| ? `Unknown Ledger error: ${ledgerErrorCode} (${context})` | ||
| : `Unknown Ledger error: ${ledgerErrorCode}`; | ||
|
|
||
| return new HardwareWalletError(fallbackMessage, { | ||
| code: ErrorCodeEnum.Unknown, | ||
| severity: SeverityEnum.Err, | ||
| category: CategoryEnum.Unknown, | ||
| userMessage: fallbackMessage, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if a Ledger error code exists in the error mappings. | ||
| * | ||
| * @param ledgerErrorCode - The Ledger error code to check | ||
| * @returns True if the error code is mapped, false otherwise | ||
| */ | ||
| export function isKnownLedgerError(ledgerErrorCode: string): boolean { | ||
| return ledgerErrorCode in LEDGER_ERROR_MAPPINGS; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the error mapping details for a Ledger error code without creating an error instance. | ||
| * | ||
| * @param ledgerErrorCode - The Ledger error code to look up | ||
| * @returns The error mapping details or undefined if not found | ||
| */ | ||
| export function getLedgerErrorMapping( | ||
| ledgerErrorCode: string, | ||
| ): LedgerErrorMapping | undefined { | ||
| const mappings = LEDGER_ERROR_MAPPINGS as { | ||
| [key: string]: LedgerErrorMapping; | ||
| }; | ||
| return mappings[ledgerErrorCode]; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.