-
Notifications
You must be signed in to change notification settings - Fork 3
refactor: typed Trezor device-busy handling #617
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
Open
jvsena42
wants to merge
4
commits into
master
Choose a base branch
from
chore/bump-bitkit-core-0.3.9
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b01ea92
refactor: use the new typed error to identify device busy cases after…
jvsena42 aea4314
Merge branch 'master' into chore/bump-bitkit-core-0.3.9
jvsena42 7e02c7e
fix: scope Trezor pairing-code error match to avoid mislabeling messa…
jvsena42 76d9189
Merge branch 'chore/bump-bitkit-core-0.3.9' of github.com:synonymdev/…
jvsena42 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import BitkitCore | ||
| import Foundation | ||
|
|
||
| /// Maps Trezor-related errors to user-facing messages, shared by `TrezorManager` and `TrezorViewModel`. | ||
| enum TrezorErrorPresenter { | ||
| static func userMessage(from error: Error) -> String { | ||
| // Classify the typed busy error before falling back to matching the stringified message. | ||
| if error.isTrezorDeviceBusy() { | ||
| return t("hardware__device_busy") | ||
| } | ||
|
|
||
| if let appError = error as? AppError { | ||
| if let debugMessage = appError.debugMessage, !debugMessage.isEmpty { | ||
| return mapMessage(debugMessage) | ||
| } | ||
| return appError.message | ||
| } | ||
|
|
||
| if let trezorError = error as? TrezorError { | ||
| return trezorError.localizedDescription | ||
| } | ||
|
|
||
| if let bleError = error as? TrezorBLEError { | ||
| return bleError.localizedDescription | ||
| } | ||
|
|
||
| if let transportError = error as? TrezorTransportError { | ||
| return transportError.localizedDescription | ||
| } | ||
|
|
||
| let description = error.localizedDescription | ||
| if description == "The operation couldn't be completed." || description.isEmpty { | ||
| return "Connection failed. Please ensure your Trezor is in pairing mode and try again." | ||
| } | ||
| return description | ||
| } | ||
|
|
||
| static func mapMessage(_ message: String) -> String { | ||
| let cleanedMessage = message | ||
| .replacingOccurrences(of: "Transport error: ", with: "") | ||
| .replacingOccurrences(of: "Connection error: ", with: "") | ||
| .replacingOccurrences(of: "Protocol error: ", with: "") | ||
| .replacingOccurrences(of: "Device error: ", with: "") | ||
| .replacingOccurrences(of: "Session error: ", with: "") | ||
| .replacingOccurrences(of: "IO error: ", with: "") | ||
|
|
||
| if message.contains("Stale Bluetooth pairing") || message.contains("Peer removed pairing") { | ||
| return "Stale Bluetooth pairing detected. Go to iOS Settings → Bluetooth, forget your Trezor device, " | ||
| + "then put it back in pairing mode and try again." | ||
| } | ||
| if message.contains("Unable to open device") || message.contains("Failed to connect") { | ||
| return "Failed to connect to Trezor. Please ensure it's in pairing mode and try again." | ||
| } | ||
| if message.contains("Pairing required") { | ||
| return "Bluetooth pairing required. Please put your Trezor in pairing mode." | ||
| } | ||
| if message.contains("Code verification failed") { | ||
| return t("hardware__pairing_code_invalid") | ||
| } | ||
| if message.contains("DeviceBusy") || message.contains("Device is busy") { | ||
| return t("hardware__device_busy") | ||
| } | ||
|
jvsena42 marked this conversation as resolved.
|
||
| if message.contains("Pairing failed") || message.contains("Invalid credentials") { | ||
| return "Pairing failed. Please try putting your Trezor back in pairing mode." | ||
| } | ||
| if message.contains("THP handshake failed") { | ||
| return "Connection handshake failed. Please disconnect and try again." | ||
| } | ||
| if message.contains("timed out") || message.contains("Timeout") { | ||
| return "Connection timed out. Please try again." | ||
| } | ||
| if message.contains("Device disconnected") { | ||
| return "Trezor disconnected. Please reconnect and try again." | ||
| } | ||
| if message.contains("Action cancelled") { | ||
| return "Action was cancelled on the device." | ||
| } | ||
|
|
||
| return cleanedMessage | ||
| } | ||
| } | ||
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,54 @@ | ||
| @testable import Bitkit | ||
| import BitkitCore | ||
| import XCTest | ||
|
|
||
| /// Truth table for `Error.isTrezorDeviceBusy()` — the typed `TrezorError.DeviceBusy` a locked | ||
| /// device surfaces since bitkit-core 0.3.9. | ||
| final class TrezorDeviceBusyTests: XCTestCase { | ||
| func testDeviceBusyReturnsTrue() { | ||
| XCTAssertTrue(TrezorError.DeviceBusy.isTrezorDeviceBusy()) | ||
| } | ||
|
|
||
| func testNonBusyErrorsReturnFalse() { | ||
| XCTAssertFalse(TrezorError.Timeout.isTrezorDeviceBusy()) | ||
| XCTAssertFalse(TrezorError.DeviceDisconnected.isTrezorDeviceBusy()) | ||
| XCTAssertFalse(TrezorError.UserCancelled.isTrezorDeviceBusy()) | ||
| XCTAssertFalse(Bitkit.AppError(message: "sign failed", debugMessage: nil).isTrezorDeviceBusy()) | ||
| XCTAssertFalse(CancellationError().isTrezorDeviceBusy()) | ||
| } | ||
|
|
||
| /// `ServiceQueue` boxes the raw `TrezorError` into an `AppError`, so the check must see through it. | ||
| /// `AppError` is qualified as `Bitkit.AppError` because `Errors.swift` is also compiled into this | ||
| /// test target, so an unqualified name would resolve to the duplicate and fail the cast. | ||
| func testDeviceBusyWrappedInAppErrorReturnsTrue() { | ||
| XCTAssertTrue(Bitkit.AppError(error: TrezorError.DeviceBusy).isTrezorDeviceBusy()) | ||
| } | ||
|
|
||
| func testNonBusyWrappedInAppErrorReturnsFalse() { | ||
| XCTAssertFalse(Bitkit.AppError(error: TrezorError.Timeout).isTrezorDeviceBusy()) | ||
| XCTAssertFalse(Bitkit.AppError(error: TrezorError.UserCancelled).isTrezorDeviceBusy()) | ||
| } | ||
|
|
||
| func testPresenterMapsDeviceBusyToUnlockPrompt() { | ||
| XCTAssertEqual( | ||
| TrezorErrorPresenter.userMessage(from: Bitkit.AppError(error: TrezorError.DeviceBusy)), | ||
| t("hardware__device_busy") | ||
| ) | ||
| } | ||
|
|
||
| func testPresenterPassesUnrelatedMessageThrough() { | ||
| XCTAssertEqual(TrezorErrorPresenter.mapMessage("some unmapped detail"), "some unmapped detail") | ||
| } | ||
|
|
||
| func testPresenterMapsPairingCodeFailureToInvalidPrompt() { | ||
| XCTAssertEqual(TrezorErrorPresenter.mapMessage("Code verification failed"), t("hardware__pairing_code_invalid")) | ||
| } | ||
|
|
||
| /// A message-signature verification error shares the "verification failed" phrasing but is not a | ||
| /// pairing failure, so it must fall through rather than showing the pairing-code prompt. | ||
| func testPresenterDoesNotMapMessageVerificationToPairingPrompt() { | ||
| let message = "Bitcoin message verification failed" | ||
| XCTAssertNotEqual(TrezorErrorPresenter.mapMessage(message), t("hardware__pairing_code_invalid")) | ||
| XCTAssertEqual(TrezorErrorPresenter.mapMessage(message), message) | ||
| } | ||
| } |
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.