Skip to content

Comments

Poc/sdk client isolation#159

Draft
chakra-guy wants to merge 3 commits intomainfrom
poc/sdk-client-isolation
Draft

Poc/sdk client isolation#159
chakra-guy wants to merge 3 commits intomainfrom
poc/sdk-client-isolation

Conversation

@chakra-guy
Copy link
Contributor

@chakra-guy chakra-guy commented Feb 4, 2026

POC: SDK Client Isolation

Problem

When a dApp uses multiple MetaMask Connect SDK clients (Multichain, EVM, Solana, Wagmi), they can interfere with each other and enter weird states. This happens because:

  • Multiple clients read/write to the same storage keys without coordination
  • Clients don't know about each other's existence or state changes
  • One client's actions (connect/disconnect) can break another client's state

Approach

This POC explores storage-level isolation using key prefixing. Each SDK type gets its own storage namespace so they don't overwrite each other's data.

What this design does:

  • Generates a deterministic instanceId from dapp.name + SDK type (e.g., myapp-evm, myapp-multichain)
  • Wraps storage adapters with PrefixedStoreAdapter that prefixes all keys
  • Works across all platforms (web/IndexedDB, React Native/AsyncStorage, Node)
  • Same dapp.name in different browser tabs shares state (multi-tab consistency)
  • Different SDK types are isolated from each other

Files changed:

  • PrefixedStoreAdapter - new adapter wrapper for key prefixing
  • createIsolatedStorage - utility to set up prefixed storage
  • sdkType option added to MultichainOptions
  • EVM client passes sdkType: 'evm' to multichain core
  • Browser playground gets experiments framework for testing

Key Findings

  1. Extension creates one connection per website - The MetaMask browser extension does not support separate sessions per SDK client. All clients share the same session. This means storage isolation alone doesn't fully solve the problem when using the extension.

  2. MWP (QR code) flow supports true isolation - Each QR scan creates an independent session. Storage prefixing works correctly here.

  3. Inconsistent behavior between flows - Extension and mobile flows behave differently, which may need further investigation.

Testing

The browser playground now includes an experiments page (?experiments URL param) with dedicated test scenarios:

  • Experiment 1: Single multichain client
  • Experiment 2: Two multichain clients (same type, should share)
  • Experiment 3: Multichain + EVM (different types, should isolate)
  • Experiments 4-6: Wagmi integration scenarios (placeholders)

Next Steps

  • Decide if singleton pattern is better than isolation for extension flow
  • Investigate state coordination between clients
  • Complete Wagmi integration experiments

- Add PrefixedStoreAdapter to prefix all storage keys with instanceId
- Add createIsolatedStorage utility for consistent storage creation
- Add generateInstanceId for deterministic instance IDs
- Add instanceId option to MultichainOptions for explicit control
- Fix beforeunload listener bug (was using different function references)
- Update tests to use instanceId: '' for backwards compatibility

This enables multiple SDK clients to coexist without storage collisions.
Each client instance gets its own storage namespace based on dapp.name
and SDK type (e.g., 'mydapp-multichain:', 'mydapp-evm:').
- Add sdkType option to MultichainOptions for proper isolation
- Fix EVM client to pass sdkType: 'evm' to multichain core
- Increment IndexedDB version to force schema upgrade
- Add experiments pages to browser playground for testing isolation
- Add StateVisualizer component for inspecting storage state
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant