-
Notifications
You must be signed in to change notification settings - Fork 394
bug(clerk-js): Use EIP6963 standard to get web3 provider #4059
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
EmmanouelaPothitou
merged 11 commits into
main
from
emmanouela/user-563-properly-support-multiple-ethereum-providers
Sep 2, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3c55c33
bug(clerk-js): Use EIP6963 standard to get web3 provider
EmmanouelaPothitou 55175bb
Create four-tips-push.md
EmmanouelaPothitou f1f4620
chore(clerk-js): Bump bundlewatch
nikosdouvlis 2e69a13
bug(clerk-js): Add comments
EmmanouelaPothitou 0cdc944
Update .changeset/four-tips-push.md
EmmanouelaPothitou 2ade8e0
bug(clerk-js): Add comments
EmmanouelaPothitou 4fe74e5
Merge branch 'emmanouela/user-563-properly-support-multiple-ethereum-…
EmmanouelaPothitou 4df67da
Merge branch 'main' into emmanouela/user-563-properly-support-multipl…
EmmanouelaPothitou f4ba8b0
fix(clerk-js): Check before using window identifier
EmmanouelaPothitou 78dc768
Merge branch 'emmanouela/user-563-properly-support-multiple-ethereum-…
EmmanouelaPothitou 2b51f9a
Merge branch 'main' into emmanouela/user-563-properly-support-multipl…
EmmanouelaPothitou 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@clerk/clerk-js": patch | ||
--- | ||
|
||
Bug fix: Use the EIP-6963 standard to get a Web3 provider when more than one provider is injected. |
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,57 @@ | ||
import type { Web3Provider } from '@clerk/types'; | ||
//https://eips.ethereum.org/EIPS/eip-6963 | ||
|
||
interface EIP6963ProviderInfo { | ||
walletId: string; | ||
uuid: string; | ||
name: string; | ||
icon: string; | ||
} | ||
|
||
interface EIP1193Provider { | ||
isStatus?: boolean; | ||
host?: string; | ||
path?: string; | ||
sendAsync?: ( | ||
request: { method: string; params?: [] }, | ||
callback: (error: Error | null, response: unknown) => void, | ||
) => void; // For sending asynchronous requests | ||
send?: (request: { method: string; params?: [] }, callback: (error: Error | null, response: unknown) => void) => void; // For sending synchronous requests | ||
request: (request: { method: string; params?: string[] }) => Promise<string>; // Standard method for sending requests per EIP-1193 | ||
} | ||
|
||
interface EIP6963ProviderDetail { | ||
info: EIP6963ProviderInfo; | ||
provider: EIP1193Provider; | ||
} | ||
|
||
type EIP6963AnnounceProviderEvent = CustomEvent; | ||
|
||
class InjectedWeb3Providers { | ||
#providers: EIP6963ProviderDetail[] = []; | ||
#providerIdMap: Record<Web3Provider, string> = { | ||
coinbase: 'Coinbase Wallet', | ||
metamask: 'MetaMask', | ||
} as const; | ||
|
||
constructor() { | ||
if (typeof window === 'undefined') { | ||
return; | ||
} | ||
window.addEventListener('eip6963:announceProvider', this.#onAnnouncement as EventListener); | ||
window.dispatchEvent(new Event('eip6963:requestProvider')); | ||
} | ||
|
||
get = (provider: Web3Provider) => { | ||
return this.#providers.find(p => p.info.name === this.#providerIdMap[provider])?.provider; | ||
}; | ||
|
||
#onAnnouncement = (event: EIP6963AnnounceProviderEvent) => { | ||
if (this.#providers.some(p => p.info.uuid === event.detail.info.uuid)) { | ||
return; | ||
} | ||
this.#providers.push(event.detail); | ||
}; | ||
} | ||
|
||
export const injectedWeb3Providers = new InjectedWeb3Providers(); |
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.