Skip to content

Commit 88bd7f5

Browse files
chore: MMConnect Configuration Improvements (#91)
Makes improvements to MM Connect configurability and testabilty - **Scope selection for MMConnect**: Auto Connect via MM Connect now passes selected scopes from checkboxes instead of hardcoding `['eip155:1']`. - **Network checkboxes**: Removed `disabled` attribute so scopes can be selected before connecting. - **Supported networks configuration**: Added RPC URLs for all featured networks (Ethereum, Linea, Arbitrum, Avalanche, BNB, Optimism, Polygon, zkSync, Base, Localhost, Solana) in `MetaMaskConnectProvider`. - **Disconnect button**: Added red "Disconnect MMConnect Session" button that appears when a MMConnect session is active. This enables users to select desired scopes via checkboxes before auto-connecting via MMConnect, improving the test dapp's flexibility for multi-chain testing. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Enables scope-selectable MM Connect auto-connect, configures supported networks, and adds a session disconnect button. > > - **UI (App.tsx)**: > - Auto Connect via `MM_CONNECT_ID` now passes selected scopes from checkboxes/custom inputs. > - Network checkboxes enabled for pre-connection scope selection. > - Shows red "Disconnect MMConnect Session" button when an MM Connect session is active. > - **SDK**: > - `SDK.setExtensionIdAndConnect` accepts optional `scopes: CaipChainId[]` and forwards to provider for MM Connect. > - `useSDK.connect` signature updated to accept optional scopes and propagate to SDK. > - **MetaMaskConnectProvider**: > - `connect(scopes?: Scope[])` accepts scopes; defaults to `['eip155:1']` when none provided. > - Adds `supportedNetworks` RPC URLs for featured chains (Ethereum, Linea, Arbitrum, Avalanche, BNB, OP, Polygon, zkSync, Base, Localhost, Solana). > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 243190e. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: ffmcgee <joao.carlos@consensys.net>
1 parent e948733 commit 88bd7f5

4 files changed

Lines changed: 61 additions & 10 deletions

File tree

src/App.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,14 @@ function App() {
859859
</button>
860860
<button
861861
onClick={() => {
862-
connect(MM_CONNECT_ID).catch((error) => {
862+
const selectedScopesArray = [
863+
...Object.keys(selectedScopes).filter((scope) => {
864+
const caipChainId = scope as CaipChainId;
865+
return selectedScopes[caipChainId];
866+
}),
867+
...customScopes.filter((scope) => scope.length),
868+
] as CaipChainId[];
869+
connect(MM_CONNECT_ID, selectedScopesArray).catch((error) => {
863870
console.error('Auto-connect via MM Connect failed:', error);
864871
});
865872
}}
@@ -869,6 +876,26 @@ function App() {
869876
>
870877
Auto Connect via MM Connect
871878
</button>
879+
{extensionId === MM_CONNECT_ID &&
880+
isExternallyConnectableConnected &&
881+
currentSession && (
882+
<button
883+
onClick={() => {
884+
disconnect();
885+
setExtensionId('');
886+
localStorage.removeItem('extensionId');
887+
}}
888+
style={{
889+
backgroundColor: '#dc2626',
890+
color: 'white',
891+
border: 'none',
892+
}}
893+
data-testid="disconnect-mm-connect-button"
894+
id="disconnect-mm-connect-button"
895+
>
896+
Disconnect MMConnect Session
897+
</button>
898+
)}
872899
</div>
873900
</section>
874901
<section>
@@ -907,7 +934,6 @@ function App() {
907934
[chainId]: evt.target.checked,
908935
}))
909936
}
910-
disabled={!isExternallyConnectableConnected}
911937
data-testid={`network-checkbox-${escapeHtmlId(
912938
chainId,
913939
)}`}

src/sdk/SDK.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ export class SDK {
109109
this.#provider?.disconnect();
110110
}
111111

112-
public async setExtensionIdAndConnect(extensionId: string): Promise<boolean> {
112+
public async setExtensionIdAndConnect(
113+
extensionId: string,
114+
scopes?: CaipChainId[],
115+
): Promise<boolean> {
113116
// TODO add logic once we have CAIP-294 wallet discovery + or hardcode the stable extensionId
114117
let connected;
115118
if (extensionId === WINDOW_POST_MESSAGE_ID) {
@@ -118,7 +121,9 @@ export class SDK {
118121
} else if (extensionId === MM_CONNECT_ID) {
119122
this.#provider =
120123
new MetaMaskConnectProvider() as unknown as MetaMaskMultichainBaseProvider;
121-
connected = await this.#provider.connect();
124+
// Convert CaipChainId[] to Scope[] for MetaMaskConnectProvider
125+
// Scope is from @metamask/connect-multichain and is compatible with CaipChainId
126+
connected = await this.#provider.connect(scopes as unknown as any);
122127
} else {
123128
this.#provider = new MetaMaskMultichainExternallyConnectableProvider();
124129
connected = await this.#provider.connect(extensionId);

src/sdk/providers/MetaMaskConnectProvider.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,27 @@ class MetaMaskConnectProvider implements Provider {
1313

1414
#walletSession: unknown = { sessionScopes: {} };
1515

16-
async connect(): Promise<boolean> {
16+
async connect(scopes?: Scope[]): Promise<boolean> {
1717
if (this.#mmConnect) {
1818
this.disconnect();
1919
}
2020

2121
this.#mmConnect = await createMetamaskConnect({
2222
api: {
23-
supportedNetworks: {},
23+
supportedNetworks: {
24+
'eip155:1': 'https://eth.llamarpc.com',
25+
'eip155:59144': 'https://rpc.linea.build',
26+
'eip155:42161': 'https://arb1.arbitrum.io/rpc',
27+
'eip155:43114': 'https://avalanche.public-rpc.com',
28+
'eip155:56': 'https://bsc-dataseed.binance.org',
29+
'eip155:10': 'https://0xrpc.io/op',
30+
'eip155:137': 'https://polygon-rpc.com',
31+
'eip155:324': 'https://mainnet.era.zksync.io',
32+
'eip155:8453': 'https://mainnet.base.org',
33+
'eip155:1337': 'http://localhost:8545',
34+
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp':
35+
'https://api.mainnet-beta.solana.com',
36+
},
2437
},
2538
dapp: {
2639
name: 'MultichainTest Dapp',
@@ -39,7 +52,11 @@ class MetaMaskConnectProvider implements Provider {
3952
},
4053
});
4154

42-
await this.#mmConnect?.connect(['eip155:1'], []);
55+
const scopesToUse =
56+
scopes && scopes.length > 0
57+
? scopes
58+
: (['eip155:1'] as unknown as Scope[]);
59+
await this.#mmConnect?.connect(scopesToUse, []);
4360

4461
return true;
4562
}

src/sdk/useSDK.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type UseSDKReturn = {
77
isConnected: boolean;
88
currentSession: any;
99
extensionId: string;
10-
connect: (extensionId: string) => Promise<void>;
10+
connect: (extensionId: string, scopes?: CaipChainId[]) => Promise<void>;
1111
disconnect: () => void;
1212
createSession: (
1313
scopes: CaipChainId[],
@@ -131,10 +131,13 @@ export function useSDK({
131131
}, [sdk, isConnected]);
132132

133133
const connect = useCallback(
134-
async (newExtensionId: string) => {
134+
async (newExtensionId: string, scopes?: CaipChainId[]) => {
135135
if (sdk) {
136136
try {
137-
const connected = await sdk.setExtensionIdAndConnect(newExtensionId);
137+
const connected = await sdk.setExtensionIdAndConnect(
138+
newExtensionId,
139+
scopes,
140+
);
138141
setIsConnected(connected);
139142
if (connected) {
140143
localStorage.setItem('extensionId', newExtensionId);

0 commit comments

Comments
 (0)