Skip to content
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

refactor(sdk): modernize wallet provider detection with EIP-6963 #1126

Merged
merged 3 commits into from
Nov 13, 2024

Conversation

abretonc7s
Copy link
Collaborator

@abretonc7s abretonc7s commented Nov 12, 2024

Description

Overview

This PR modernizes our wallet provider detection by prioritizing the EIP-6963 standard while maintaining backwards compatibility. It simplifies our codebase by removing complex wallet-specific detection logic and improves reliability across different browser environments, particularly on iOS.

Key Changes

  • ✨ Prioritize EIP-6963 for wallet provider detection
  • 🔥 Remove legacy wallet detection heuristics (flags like isMetaMask, isBraveWallet, etc.)
  • 🛠️ Simplify fallback logic to use window.ethereum only when necessary
  • 🐛 Add error handling for Brave iOS where window properties are read-only
  • 📝 Improve test coverage and reduce test complexity

Technical Implementation

  • Restructured provider detection flow:
    1. Attempt EIP-6963 provider detection first
    2. Fallback to window.ethereum only for non-MetaMask scenarios
    3. Throw clear error messages when no provider is found
  • Added try-catch blocks around window modifications to handle read-only properties
  • Removed ~70 lines of wallet-specific detection code
  • Simplified test suite while maintaining coverage

Impact

Better

  • More reliable wallet detection across browsers
  • Cleaner codebase with less maintenance overhead
  • Better compatibility with EIP-6963 compliant wallets
  • More resilient error handling

Breaking Changes

None. This change maintains backward compatibility while improving the primary detection path.

Testing Instructions

  1. Test with different wallet configurations:

    • MetaMask only
    • Multiple wallets installed (e.g., MetaMask + Coinbase)
    • Non-MetaMask wallets
  2. Test on different platforms:

    • Desktop browsers (Chrome, Firefox, Safari)
    • Mobile browsers
    • Brave browser (especially iOS)

Special Considerations

  • This change relies on the EIP-6963 standard. While we maintain fallback support, modern wallets should be encouraged to implement this standard
  • Brave iOS users may see logged warnings about read-only properties, this is expected and handled gracefully

• Add try-catch blocks around window.ethereum and window.web3 modifications
• Prevent crashes on Brave iOS where window properties are read-only
• Allow SDK initialization to continue when provider setup fails
• Fix typo in "conflict" comment
• Remove legacy wallet detection logic in favor of EIP-6963 standard
• Simplify fallback to window.ethereum when EIP-6963 is unavailable
• Remove complex provider detection for Brave and other wallet impersonators
• Improve reliability by reducing dependency on wallet-specific flags
• Prioritize EIP-6963 provider detection
• Streamline fallback logic for non-MetaMask providers
• Reduce test complexity and improve clarity
• Consolidate error handling paths
@abretonc7s abretonc7s changed the title Combined changes (3 commits) refactor(sdk): modernize wallet provider detection with EIP-6963 Nov 12, 2024
@abretonc7s abretonc7s marked this pull request as ready for review November 12, 2024 14:03
@abretonc7s abretonc7s requested a review from a team as a code owner November 12, 2024 14:03
Copy link

codecov bot commented Nov 12, 2024

Codecov Report

Attention: Patch coverage is 53.84615% with 6 lines in your changes missing coverage. Please review.

Project coverage is 76.99%. Comparing base (655a7be) to head (aec4551).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
packages/sdk/src/services/Ethereum.ts 25.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1126      +/-   ##
==========================================
- Coverage   77.16%   76.99%   -0.17%     
==========================================
  Files         180      180              
  Lines        4256     4238      -18     
  Branches     1048     1039       -9     
==========================================
- Hits         3284     3263      -21     
- Misses        972      975       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@christopherferreira9 christopherferreira9 left a comment

Choose a reason for hiding this comment

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

LGTM

@christopherferreira9 christopherferreira9 merged commit a954015 into main Nov 13, 2024
34 of 36 checks passed
@christopherferreira9 christopherferreira9 deleted the fix/bravebrowser branch November 13, 2024 10:53
@abretonc7s abretonc7s mentioned this pull request Nov 15, 2024
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