Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,14 @@ function App() {
</button>
<button
onClick={() => {
connect(MM_CONNECT_ID).catch((error) => {
const selectedScopesArray = [
...Object.keys(selectedScopes).filter((scope) => {
const caipChainId = scope as CaipChainId;
return selectedScopes[caipChainId];
}),
...customScopes.filter((scope) => scope.length),
] as CaipChainId[];
connect(MM_CONNECT_ID, selectedScopesArray).catch((error) => {
console.error('Auto-connect via MM Connect failed:', error);
});
}}
Expand All @@ -869,6 +876,26 @@ function App() {
>
Auto Connect via MM Connect
</button>
{extensionId === MM_CONNECT_ID &&
isExternallyConnectableConnected &&
currentSession && (
<button
onClick={() => {
disconnect();
setExtensionId('');
localStorage.removeItem('extensionId');
}}
style={{
backgroundColor: '#dc2626',
color: 'white',
border: 'none',
}}
data-testid="disconnect-mm-connect-button"
id="disconnect-mm-connect-button"
>
Disconnect MMConnect Session
</button>
)}
</div>
</section>
<section>
Expand Down Expand Up @@ -907,7 +934,6 @@ function App() {
[chainId]: evt.target.checked,
}))
}
disabled={!isExternallyConnectableConnected}
data-testid={`network-checkbox-${escapeHtmlId(
chainId,
)}`}
Expand Down
9 changes: 7 additions & 2 deletions src/sdk/SDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ export class SDK {
this.#provider?.disconnect();
}

public async setExtensionIdAndConnect(extensionId: string): Promise<boolean> {
public async setExtensionIdAndConnect(
extensionId: string,
scopes?: CaipChainId[],
): Promise<boolean> {
// TODO add logic once we have CAIP-294 wallet discovery + or hardcode the stable extensionId
let connected;
if (extensionId === WINDOW_POST_MESSAGE_ID) {
Expand All @@ -118,7 +121,9 @@ export class SDK {
} else if (extensionId === MM_CONNECT_ID) {
this.#provider =
new MetaMaskConnectProvider() as unknown as MetaMaskMultichainBaseProvider;
connected = await this.#provider.connect();
// Convert CaipChainId[] to Scope[] for MetaMaskConnectProvider
// Scope is from @metamask/connect-multichain and is compatible with CaipChainId
connected = await this.#provider.connect(scopes as unknown as any);
} else {
this.#provider = new MetaMaskMultichainExternallyConnectableProvider();
connected = await this.#provider.connect(extensionId);
Expand Down
23 changes: 20 additions & 3 deletions src/sdk/providers/MetaMaskConnectProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,27 @@ class MetaMaskConnectProvider implements Provider {

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

async connect(): Promise<boolean> {
async connect(scopes?: Scope[]): Promise<boolean> {
if (this.#mmConnect) {
this.disconnect();
}

this.#mmConnect = await createMetamaskConnect({
api: {
supportedNetworks: {},
supportedNetworks: {
'eip155:1': 'https://eth.llamarpc.com',
'eip155:59144': 'https://rpc.linea.build',
'eip155:42161': 'https://arb1.arbitrum.io/rpc',
'eip155:43114': 'https://avalanche.public-rpc.com',
'eip155:56': 'https://bsc-dataseed.binance.org',
'eip155:10': 'https://0xrpc.io/op',
'eip155:137': 'https://polygon-rpc.com',
'eip155:324': 'https://mainnet.era.zksync.io',
'eip155:8453': 'https://mainnet.base.org',
'eip155:1337': 'http://localhost:8545',
'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp':
'https://api.mainnet-beta.solana.com',
},
},
dapp: {
name: 'MultichainTest Dapp',
Expand All @@ -39,7 +52,11 @@ class MetaMaskConnectProvider implements Provider {
},
});

await this.#mmConnect?.connect(['eip155:1'], []);
const scopesToUse =
scopes && scopes.length > 0
? scopes
: (['eip155:1'] as unknown as Scope[]);
await this.#mmConnect?.connect(scopesToUse, []);

return true;
}
Expand Down
9 changes: 6 additions & 3 deletions src/sdk/useSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type UseSDKReturn = {
isConnected: boolean;
currentSession: any;
extensionId: string;
connect: (extensionId: string) => Promise<void>;
connect: (extensionId: string, scopes?: CaipChainId[]) => Promise<void>;
disconnect: () => void;
createSession: (
scopes: CaipChainId[],
Expand Down Expand Up @@ -131,10 +131,13 @@ export function useSDK({
}, [sdk, isConnected]);

const connect = useCallback(
async (newExtensionId: string) => {
async (newExtensionId: string, scopes?: CaipChainId[]) => {
if (sdk) {
try {
const connected = await sdk.setExtensionIdAndConnect(newExtensionId);
const connected = await sdk.setExtensionIdAndConnect(
newExtensionId,
scopes,
);
setIsConnected(connected);
if (connected) {
localStorage.setItem('extensionId', newExtensionId);
Expand Down
Loading