Skip to content

Wallet Connect external browser redirect#918

Merged
lealobanov merged 7 commits intodevelopfrom
wc-debugging
May 2, 2025
Merged

Wallet Connect external browser redirect#918
lealobanov merged 7 commits intodevelopfrom
wc-debugging

Conversation

@lealobanov
Copy link
Copy Markdown
Contributor

@lealobanov lealobanov commented May 1, 2025

Related Issue

  • When WalletConnect connection request via deep link is missing redirect URL,handle the session connection and show a toast fallback indicating to return back to the browser
  • Add a "Connecting..." toast which is shown from the beginning of the deep linking flow until the FCL Auth Dialog is fully rendered

Summary of Changes

Need Regression Testing

  • Yes
  • No

Risk Assessment

  • Low
  • Medium
  • High

Additional Notes

Screenshots (if applicable)

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 1, 2025

PR Summary

Added support for handling WalletConnect deep linking with external browser redirection. Improved connection state management and user feedback during the WalletConnect pairing process. Added fallback mechanism with toast notifications when redirect URL is missing. Enhanced error handling and logging throughout the WalletConnect flow.

Changes

File Summary
app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnect.kt Added toast notification to show connecting status. Enhanced pairing logic with better error handling and logging. Improved connection state management with retry mechanism.
app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnectDelegate.kt Added support for external browser redirection and session approval handling. Implemented fallback toast notifications when redirect URL is missing. Added connection state recovery logic.
app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnectRequestDispatcher.kt Enhanced authentication response handling with better error management and logging. Added delay before session settlement to ensure proper response processing.
app/src/main/java/com/flowfoundation/wallet/page/component/deeplinking/DeepLinkingActivity.kt Moved deep linking dispatch to coroutine scope for better async handling. Added error handling and improved activity lifecycle management.
app/src/main/java/com/flowfoundation/wallet/page/component/deeplinking/PendingActionHelper.kt Added logging for pending deep link actions to improve debugging capabilities.
app/src/main/java/com/flowfoundation/wallet/page/component/deeplinking/Utils.kt Improved WalletConnect URI handling with better error management. Added timeout handling for WalletConnect initialization. Enhanced logging throughout the dispatch process.
app/src/main/java/com/flowfoundation/wallet/widgets/webview/fcl/dialog/FclAuthnDialog.kt Removed unused EVMWalletManager import.
app/src/main/res/values/strings.xml Added new string resources for connection status and browser return instructions.

autogenerated by presubmit.ai

@lealobanov lealobanov changed the title WIP - integration WIP - Wallet Connect external browser redirect May 1, 2025
Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 Pull request needs attention.

Review Summary

Commits Considered (1)
Files Processed (7)
  • app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnect.kt (3 hunks)
  • app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnectDelegate.kt (6 hunks)
  • app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnectRequestDispatcher.kt (1 hunk)
  • app/src/main/java/com/flowfoundation/wallet/page/component/deeplinking/DeepLinkingActivity.kt (2 hunks)
  • app/src/main/java/com/flowfoundation/wallet/page/component/deeplinking/PendingActionHelper.kt (2 hunks)
  • app/src/main/java/com/flowfoundation/wallet/page/component/deeplinking/Utils.kt (4 hunks)
  • app/src/main/java/com/flowfoundation/wallet/widgets/webview/fcl/dialog/FclAuthnDialog.kt (2 hunks)
Actionable Comments (1)
  • app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnectDelegate.kt [134-134]

    possible issue: "Potential race condition in activity state check"

Skipped Comments (3)
  • app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnectDelegate.kt [62-63]

    best practice: "Improve error handling for connection state changes"

  • app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnect.kt [61-62]

    possible bug: "Potential memory leak in session management"

  • app/src/main/java/com/flowfoundation/wallet/page/component/deeplinking/DeepLinkingActivity.kt [32-42]

    enhancement: "Improve deep linking error recovery"

var attempts = 0
val maxAttempts = 5
var activity: BaseActivity? = null

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The activity state check is done in a loop with a fixed number of attempts, but there's no guarantee the activity will be ready even after the maximum attempts. Consider implementing a more robust activity lifecycle-aware solution or handling the null activity case more gracefully.


logd(TAG, "Attempting to redirect to: $redirectUrl")
try {
redirectUrl.openInSystemBrowser(activity, true)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user might not really coming form system browser, for example they could deeplink from telegram to flow wallet. If we navigate them to browser, it's wired.

sessionProposal.redirect
} else {
logd(TAG, "No redirect provided, using DApp URL: ${sessionProposal.url}")
sessionProposal.url
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need double check on this, for example:
if user comes from flow port A, now if we navigate user back with flow port url.
Will it open flow port B ? But our app was just authn the flow port A.

@lealobanov
Copy link
Copy Markdown
Contributor Author

Screen.Recording.2025-05-01.at.8.40.44.PM.mov

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 Pull request needs attention.

Review Summary

Commits Considered (1)
Files Processed (3)
  • app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnect.kt (5 hunks)
  • app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnectDelegate.kt (5 hunks)
  • app/src/main/res/values/strings.xml (2 hunks)
Actionable Comments (1)
  • app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnectDelegate.kt [284-292]

    best practice: "Missing URL validation before redirect"

Skipped Comments (2)
  • app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnectDelegate.kt [137-143]

    performance: "Potential performance bottleneck in activity retrieval"

  • app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnect.kt [67-82]

    maintainability: "Code duplication in connection handling"

Comment on lines +284 to +292
try {
redirectUrl.openInSystemBrowser(activity, true)
logd(TAG, "Successfully opened URL in system browser")
pendingRedirectUrl = null
isRedirecting = false
} catch (e: Exception) {
loge(TAG, "Failed to open URL in system browser: ${e.message}")
loge(e)
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The redirect URL is opened without any validation. Consider adding URL validation to prevent potential security issues with malicious URLs. Also, add a try-catch block specifically for the openInSystemBrowser call to handle potential ActivityNotFoundException.

@lealobanov lealobanov changed the title WIP - Wallet Connect external browser redirect Wallet Connect external browser redirect May 1, 2025
<string name="blocked_tip">This app has been flagged as dangerous.</string>
<string name="switch_to">Switch to %1$s</string>
<string name="deeplink_login_failed">Failed to open, please create a wallet first.</string>
<string name="return_to_browser_to_continue">Please return to the browser to continue your session.</string>
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To-do: add translations

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 Pull request needs attention.

Review Summary

Commits Considered (1)
Files Processed (1)
  • app/src/main/java/com/flowfoundation/wallet/page/component/deeplinking/Utils.kt (4 hunks)
Actionable Comments (1)
  • app/src/main/java/com/flowfoundation/wallet/page/component/deeplinking/Utils.kt [124-133]

    possible issue: "Potential timeout issue in initialization retry logic"

Skipped Comments (2)
  • app/src/main/java/com/flowfoundation/wallet/page/component/deeplinking/Utils.kt [57-64]

    enhancement: "Missing user feedback for failed connection attempts"

  • app/src/main/java/com/flowfoundation/wallet/page/component/deeplinking/Utils.kt [189-198]

    possible bug: "Potential URI decoding edge case"

Comment on lines 124 to 133
val initResult = withTimeoutOrNull(3000) {
var waitTime = 50L
var attempts = 0
val maxAttempts = 15

while (!WalletConnect.isInitialized() && attempts < maxAttempts) {
logd(TAG, "Waiting for WalletConnect initialization (attempt $attempts)")
delay(waitTime)
attempts++
waitTime = minOf(waitTime * 2, 500)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exponential backoff implementation could potentially exceed the 3000ms timeout due to the delay accumulation. Consider adding a check to ensure the total elapsed time doesn't exceed the timeout value, or adjust the delay calculation to account for the total time limit.

}
return@runCatching true
result
} else {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The result variable is set inside the ioScope block but returned outside of it. Since ioScope is asynchronous, the result value might not be set when it's returned. Consider restructuring the code to properly handle the asynchronous result, possibly by making the entire initialization block synchronous or by using suspendCoroutine.

val maxAttempts = 15
val maxAttempts = 5

while (!WalletConnect.isInitialized() && attempts < maxAttempts) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The while loop condition checks attempts but doesn't increment the counter, which could lead to an infinite loop. Add attempts++ inside the loop body.

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Review Summary

Commits Considered (1)
Files Processed (1)
  • app/src/main/java/com/flowfoundation/wallet/widgets/webview/fcl/dialog/FclAuthnDialog.kt (1 hunk)
Actionable Comments (0)
Skipped Comments (0)

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 Pull request needs attention.

Review Summary

Commits Considered (1)
Files Processed (3)
  • app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnectDelegate.kt (5 hunks)
  • app/src/main/java/com/flowfoundation/wallet/page/component/deeplinking/Utils.kt (6 hunks)
  • app/src/main/java/com/flowfoundation/wallet/widgets/webview/fcl/dialog/FclAuthnDialog.kt (1 hunk)
Actionable Comments (1)
  • app/src/main/java/com/flowfoundation/wallet/page/component/deeplinking/Utils.kt [171-178]

    possible issue: "Improve URL decoding robustness"

Skipped Comments (2)
  • app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnectDelegate.kt [73-74]

    best practice: "Improve error handling for browser intent"

  • app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnectDelegate.kt [136-141]

    best practice: "Implement exponential backoff for reconnection attempts"

Comment on lines 171 to 178
wcUriEncoded?.let {
if (it.contains("%")) {
URLDecoder.decode(it, StandardCharsets.UTF_8.name())
val decoded = URLDecoder.decode(it, StandardCharsets.UTF_8.name())
decoded
} else {
it
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The URL decoding is only performed when the URI contains '%', but there might be other URL-encoded characters that don't use percent encoding. Consider always decoding the URI to ensure all encoded characters are properly handled. Also, the error handling for decoding should be more specific to catch UnsupportedEncodingException and IllegalArgumentException.

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 Pull request needs attention.

Review Summary

Commits Considered (1)
  • 010aa2d: Hide toast when on inapp browser auth
Files Processed (1)
  • app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnectDelegate.kt (5 hunks)
Actionable Comments (1)
  • app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnectDelegate.kt [45-58]

    performance: "Potential infinite reconnection loop"

Skipped Comments (2)
  • app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnectDelegate.kt [127-133]

    possible issue: "Potential race condition in activity retrieval loop"

  • app/src/main/java/com/flowfoundation/wallet/manager/walletconnect/WalletConnectDelegate.kt [180-191]

    possible bug: "Race condition in UI visibility check"

Comment on lines +45 to +58
if (!state.isAvailable) {
logd(TAG, "Connection lost, attempting to reconnect")
ioScope {
delay(1000) // Wait before reconnecting
try {
CoreClient.Relay.connect { error: Core.Model.Error ->
loge(TAG, "CoreClient.Relay connect error: $error")
}
} catch (e: Exception) {
loge(TAG, "Error reconnecting: ${e.message}")
loge(e)
}
}
} else if (isRedirecting) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reconnection logic doesn't have a maximum retry limit or exponential backoff. This could lead to aggressive reconnection attempts that drain battery or network resources if the connection keeps failing.

@lmcmz lmcmz marked this pull request as ready for review May 2, 2025 02:53
@lmcmz lmcmz requested a review from a team as a code owner May 2, 2025 02:53
@lealobanov lealobanov merged commit 894b8bd into develop May 2, 2025
3 checks passed
@lealobanov lealobanov deleted the wc-debugging branch May 2, 2025 02:56
@github-actions github-actions Bot mentioned this pull request May 13, 2025
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants