-
-
Notifications
You must be signed in to change notification settings - Fork 365
feat: MetaMask Connect #433
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
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
e20047c
docs: plan connect evm integration
wenfix fcd8f3d
docs: add connect evm implementation plan
wenfix 66e4394
chore: ignore local worktrees
wenfix eb73c47
chore: fix connect evm plan baseline lint
wenfix 420d1d2
feat: add connect evm dependency
wenfix e32cae2
chore: require node 20
wenfix 7aab376
chore: update node version file
wenfix 846fda3
feat: add connect evm client helper
wenfix f61acc6
fix: harden connect evm helper
wenfix 885eb5e
fix: allow connect evm init retry
wenfix a2b6a36
feat: add connect evm button
wenfix cd8ef9a
fix: tighten connect evm button text
wenfix 6b894f6
feat: wire connect evm button
wenfix aa5e91c
feat: support eip1193 provider initialization
wenfix 6d3360d
fix: clean up active provider listeners
wenfix a02ed65
fix: keep walletconnect disconnect state cleared
wenfix 261d329
fix: clear active provider teardown state
wenfix 03faa76
fix: resolve process browser shim
wenfix 0bd2df0
fix: tighten provider disconnect lifecycle
wenfix 42e5f95
chore: remove plan docs
wenfix 06c2818
fix: rename connect evm button label
wenfix 9ee1850
fix: drop unused connect evm provider tracking
wenfix cd66c91
fix: tidy connect evm icon and install event dispatch
wenfix 004f076
ci: drop node 18 from build-lint-test matrix
wenfix 1f3fb25
chore: allowlist install scripts for connect evm deps
wenfix 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 |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ node_modules | |
| dist | ||
| .eslintcache | ||
| .DS_Store | ||
| .worktrees | ||
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 |
|---|---|---|
| @@ -1 +1 @@ | ||
| v18 | ||
| v20.19.1 |
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
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,121 @@ | ||
| import { createEVMClient } from '@metamask/connect-evm'; | ||
| import dappMetadata from './dapp-metadata'; | ||
| import globalContext, { | ||
| handleNewAccounts, | ||
| handleNewProviderDetail, | ||
| removeProviderDetail, | ||
| setActiveProviderDetail, | ||
| updateFormElements, | ||
| } from '.'; | ||
|
|
||
| export const CONNECT_EVM_PROVIDER_UUID = 'connect-evm'; | ||
|
|
||
| export const CONNECT_EVM_SUPPORTED_NETWORKS = { | ||
| '0x1': 'https://ethereum.publicnode.com', | ||
| '0xaa36a7': 'https://ethereum-sepolia.publicnode.com', | ||
| '0xa': 'https://optimism.publicnode.com', | ||
| '0x89': 'https://polygon-bor-rpc.publicnode.com', | ||
| '0x2105': 'https://base.publicnode.com', | ||
| '0xa4b1': 'https://arbitrum-one.publicnode.com', | ||
| '0xa86a': 'https://avalanche-c-chain-rpc.publicnode.com', | ||
| '0x38': 'https://bsc-dataseed.binance.org', | ||
| '0x539': 'http://127.0.0.1:8545', | ||
| '0x53a': 'http://127.0.0.1:8546', | ||
| }; | ||
|
|
||
| export const CONNECT_EVM_CHAIN_IDS = Object.keys( | ||
| CONNECT_EVM_SUPPORTED_NETWORKS, | ||
| ); | ||
|
|
||
| let connectEvmClientPromise; | ||
|
|
||
| const noop = () => undefined; | ||
|
|
||
| async function getConnectEvmClient() { | ||
| if (!connectEvmClientPromise) { | ||
| connectEvmClientPromise = createEVMClient({ | ||
| dapp: dappMetadata, | ||
| api: { | ||
| supportedNetworks: CONNECT_EVM_SUPPORTED_NETWORKS, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| try { | ||
| return await connectEvmClientPromise; | ||
| } catch (err) { | ||
| connectEvmClientPromise = undefined; | ||
| throw err; | ||
| } | ||
| } | ||
|
|
||
| function getConnectEvmProviderDetail(provider, name) { | ||
| return { | ||
| info: { | ||
| uuid: CONNECT_EVM_PROVIDER_UUID, | ||
| name, | ||
| icon: './metamask-fox.svg', | ||
| rdns: 'io.metamask', | ||
| }, | ||
| provider, | ||
| }; | ||
| } | ||
|
|
||
| function setConnectedButtonState(button) { | ||
| button.innerText = 'MetaMask Connect - Disconnect'; | ||
| button.classList.remove('btn-primary'); | ||
| button.classList.add('btn-danger'); | ||
| } | ||
|
|
||
| function setDisconnectedButtonState(button) { | ||
| button.innerText = 'MetaMask Connect'; | ||
| button.classList.add('btn-primary'); | ||
| button.classList.remove('btn-danger'); | ||
| } | ||
|
|
||
| export async function handleConnectEvm( | ||
| name, | ||
| button, | ||
| isConnected, | ||
| updateConnectionState = noop, | ||
| ) { | ||
| button.disabled = true; | ||
|
|
||
| try { | ||
| const client = await getConnectEvmClient(); | ||
|
|
||
| if (isConnected) { | ||
| await client.disconnect(); | ||
| updateConnectionState(false); | ||
| const activeProviderRemoved = removeProviderDetail(name); | ||
| setDisconnectedButtonState(button); | ||
| if (activeProviderRemoved) { | ||
| updateFormElements(); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| const { accounts } = await client.connect({ | ||
| chainIds: CONNECT_EVM_CHAIN_IDS, | ||
| }); | ||
| const provider = client.getProvider(); | ||
|
|
||
| const providerDetail = getConnectEvmProviderDetail(provider, name); | ||
| await setActiveProviderDetail(providerDetail); | ||
| handleNewProviderDetail(providerDetail); | ||
| updateConnectionState(true); | ||
| setConnectedButtonState(button); | ||
| updateFormElements(); | ||
| handleNewAccounts(accounts); | ||
| globalContext.connected = true; | ||
| } catch (err) { | ||
| console.error('Error connecting with MetaMask Connect EVM', err); | ||
| if (isConnected) { | ||
| setConnectedButtonState(button); | ||
| } else { | ||
| setDisconnectedButtonState(button); | ||
| } | ||
| } finally { | ||
| button.disabled = false; | ||
| } | ||
| } |
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,7 @@ | ||
| const dappMetadata = { | ||
| name: 'E2e Test Dapp', | ||
| description: 'This is the E2e Test Dapp', | ||
| url: 'https://metamask.github.io/test-dapp/', | ||
| }; | ||
|
|
||
| export default dappMetadata; |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mm-connect requires node version
>=20.19.0