-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: Improve link-handling for internally-sourced links (carousel / in-app browser) #22012
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
smilingkylan
wants to merge
12
commits into
main
Choose a base branch
from
inapp-deep-links
base: main
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.
+221
−13
Open
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2538abd
Allow deeplinking from within in-app browser
smilingkylan b246c06
Merge branch 'main' into inapp-deep-links
smilingkylan 7a4f251
Fixes to inapp link handling
smilingkylan cb752c5
Minor formatting fixes
smilingkylan ecfa813
Fix formatting issue
smilingkylan a937172
Remove deeplink test component
smilingkylan 70882b2
Catch unhandled promise error in onShouldStartLoadWithRequest
smilingkylan 191fdce
Fix some inapp browser deeplink tests
smilingkylan 7999377
Clean up inapp browser link-handling
smilingkylan d81223f
Merge branch 'main' into inapp-deep-links
smilingkylan f4b3c83
Remove some unused domains from isInternalDeeplink method
smilingkylan 924a4bc
Remove obsolete unit tests from isInternalDeepLink called with obsole…
smilingkylan 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { isInternalDeepLink } from './index'; | ||
| import AppConstants from '../../core/AppConstants'; | ||
|
|
||
| describe('deeplinks utils', () => { | ||
| describe('isInternalDeepLink', () => { | ||
| it('should identify MetaMask custom scheme deeplinks', () => { | ||
| expect(isInternalDeepLink('metamask://connect')).toBe(true); | ||
| expect(isInternalDeepLink('metamask://wc?uri=...')).toBe(true); | ||
| expect(isInternalDeepLink('metamask://dapp/uniswap.org')).toBe(true); | ||
| }); | ||
|
|
||
| it('should identify Ethereum scheme deeplinks', () => { | ||
| expect(isInternalDeepLink('ethereum://pay-0x1234')).toBe(true); | ||
| expect(isInternalDeepLink('ethereum://0x1234?value=1e18')).toBe(true); | ||
| }); | ||
|
|
||
| it('should identify dapp scheme deeplinks', () => { | ||
| expect(isInternalDeepLink('dapp://app.uniswap.org')).toBe(true); | ||
| expect(isInternalDeepLink('dapp://portfolio.metamask.io')).toBe(true); | ||
| }); | ||
|
|
||
| it('should identify MetaMask universal links', () => { | ||
| expect(isInternalDeepLink('https://link.metamask.io/swap')).toBe(true); | ||
| expect(isInternalDeepLink('https://link.metamask.io/buy-crypto')).toBe( | ||
| true, | ||
| ); | ||
| expect( | ||
| isInternalDeepLink('https://link.metamask.io/dapp/uniswap.org'), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('should identify MetaMask test universal links', () => { | ||
| expect(isInternalDeepLink('https://link-test.metamask.io/swap')).toBe( | ||
| true, | ||
| ); | ||
| expect(isInternalDeepLink('https://link-test.metamask.io/send')).toBe( | ||
| true, | ||
| ); | ||
| }); | ||
|
|
||
| it('should identify MetaMask branch links', () => { | ||
| expect(isInternalDeepLink('https://metamask.app.link/swap')).toBe(true); | ||
| expect(isInternalDeepLink('https://metamask.test-app.link/home')).toBe( | ||
| true, | ||
| ); | ||
| expect( | ||
| isInternalDeepLink('https://metamask-alternate.app.link/buy'), | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('should NOT identify external URLs as internal', () => { | ||
| expect(isInternalDeepLink('https://google.com')).toBe(false); | ||
| expect(isInternalDeepLink('https://uniswap.org')).toBe(false); | ||
| expect(isInternalDeepLink('https://portfolio.metamask.io')).toBe(false); | ||
| expect(isInternalDeepLink('http://example.com')).toBe(false); | ||
| }); | ||
|
|
||
| it('should NOT identify other protocols as internal', () => { | ||
| expect(isInternalDeepLink('mailto:[email protected]')).toBe(false); | ||
| expect(isInternalDeepLink('tel:+1234567890')).toBe(false); | ||
| expect(isInternalDeepLink('wc://session')).toBe(false); | ||
| expect(isInternalDeepLink('https://wc.example.com')).toBe(false); | ||
| }); | ||
|
|
||
| it('handles edge cases gracefully', () => { | ||
| expect(isInternalDeepLink('')).toBe(false); | ||
| expect(isInternalDeepLink(null)).toBe(false); | ||
| expect(isInternalDeepLink(undefined)).toBe(false); | ||
| expect(isInternalDeepLink('not-a-valid-url')).toBe(false); | ||
| expect(isInternalDeepLink('metamask://')).toBe(true); // Still a valid MetaMask scheme | ||
| }); | ||
|
|
||
| it('should handle URLs with query parameters and fragments', () => { | ||
| expect( | ||
| isInternalDeepLink( | ||
| 'https://link.metamask.io/swap?chainId=1&token=0x...', | ||
| ), | ||
| ).toBe(true); | ||
| expect( | ||
| isInternalDeepLink('metamask://connect?channelId=123#fragment'), | ||
| ).toBe(true); | ||
| expect(isInternalDeepLink('https://google.com?metamask=true')).toBe( | ||
| false, | ||
| ); | ||
| }); | ||
|
|
||
| it('should respect AppConstants configuration', () => { | ||
| // Test with custom constants if they're defined | ||
| const customHost = AppConstants.MM_UNIVERSAL_LINK_HOST; | ||
| if (customHost) { | ||
| expect(isInternalDeepLink(`https://${customHost}/action`)).toBe(true); | ||
| } | ||
| }); | ||
| }); | ||
| }); |
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,51 @@ | ||
| import AppConstants from '../../core/AppConstants'; | ||
|
|
||
| /** | ||
| * Checks if a URL is an internal MetaMask deeplink that should be handled | ||
| * within the app rather than passed to the OS | ||
| * | ||
| * @param url - The URL to check | ||
| * @returns true if the URL is a MetaMask internal deeplink | ||
| */ | ||
| export const isInternalDeepLink = (url: string | null | undefined): boolean => { | ||
| if (!url) return false; | ||
|
|
||
| const metamaskHosts = [ | ||
| AppConstants.MM_UNIVERSAL_LINK_HOST || 'link.metamask.io', | ||
| AppConstants.MM_IO_UNIVERSAL_LINK_HOST || 'link.metamask.io', | ||
| AppConstants.MM_IO_UNIVERSAL_LINK_TEST_HOST || 'link-test.metamask.io', | ||
| 'metamask.app.link', | ||
| 'metamask.test-app.link', | ||
| 'metamask-alternate.app.link', | ||
| 'metamask-alternate.test-app.link', | ||
| ].filter(Boolean); | ||
smilingkylan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| try { | ||
| // Check custom schemes first (more efficient for these cases) | ||
| const internalSchemes = ['metamask:', 'ethereum:', 'dapp:']; | ||
| if (internalSchemes.some((scheme) => url.startsWith(scheme))) { | ||
| return true; | ||
| } | ||
|
|
||
| // Parse URL for host checking | ||
| const urlObj = new URL(url); | ||
|
|
||
| // Check if it's a MetaMask universal link | ||
| return metamaskHosts.includes(urlObj.hostname); | ||
| } catch { | ||
| // If URL parsing fails, check if it's a simple scheme match | ||
| return ['metamask:', 'ethereum:', 'dapp:'].some((scheme) => | ||
| url.startsWith(scheme), | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Determines if a URL should be opened externally (outside the app) | ||
| * This is the inverse of isInternalDeepLink but kept separate for clarity | ||
| * | ||
| * @param url - The URL to check | ||
| * @returns true if the URL should be opened externally | ||
| */ | ||
| export const shouldOpenExternally = (url: string): boolean => | ||
| !isInternalDeepLink(url); | ||
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.