ποΈ Architecture Overview
Understand SuperSafe Wallet's Professionally Standardized Service Worker architecture and how it provides secure, efficient dApp integration.
Architecture Overviewβ
SuperSafe Wallet implements a Professionally Standardized Service Worker architecture that provides a secure, efficient, and scalable foundation for Web3 wallet functionality.
Key Architectural Principlesβ
- Single Source of Truth: Service worker as the central authority
- Thin Client Pattern: Frontend focuses on UI, backend handles logic
- Stream-Based Communication: Efficient inter-process communication
- Zero Frontend Crypto: No cryptographic operations in frontend
- Professionally Standardized Controllers: Familiar architecture for developers
System Architectureβ
High-Level Architectureβ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Web Pages (dApps) β
β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββ β
β β Uniswap β β OpenSea β β Other β β
β β dApp β β dApp β β dApps β β
β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
β EIP-1193 Provider
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Extension Context β
β βββββββββββββββββββ βββββββββββββββββββββββ β
β β Popup UI β β Background Service β β
β β (Frontend) βββββββββββββββΊβ Worker (Backend) β β
β β β Streams β β β
β βββββββββββββββββββ βββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
β Chrome Storage API
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Storage Layer β
β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββ β
β β Vault β β Session β β Local β β
β β (Encrypted) β β (Memory) β β Storage β β
β βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Component Architectureβ
Background Service Workerβ
The background service worker is the single source of truth for all wallet state and operations:
Background Service Worker
βββ BackgroundSessionController (3,979 lines)
β βββ Vault Management
β βββ Wallet Management
β βββ Session Management
β βββ Connected Sites
β βββ Auto-Lock System
β βββ Network Coordination
βββ BackgroundControllers (497 lines)
β βββ TokenController
β βββ NetworkController
β βββ TransactionController
β βββ NetworkSwitchService
βββ Stream Handlers
β βββ SessionStreamHandler
β βββ ProviderStreamHandler
β βββ SwapStreamHandler
β βββ SendStreamHandler
β βββ BlockchainStreamHandler
β βββ ApiStreamHandler
βββ Managers
βββ SigningRequestManager
βββ PopupManager
βββ EIP1193EventsManager
βββ AutoEscalationManager
βββ StreamPersistenceManager
Frontend Applicationβ
The frontend is a thin client that focuses on UI and user interaction:
Frontend Application
βββ App.jsx (Main Application)
β βββ Routing
β βββ Modal Management
β βββ Confirmation Dialogs
βββ Components
β βββ Dashboard
β βββ Wallet Management
β βββ Transaction Interface
β βββ Swap Interface
β βββ Settings
βββ Screens
β βββ Create Wallet
β βββ Import Wallet
β βββ Send Transaction
β βββ Receive Funds
β βββ Swap Tokens
βββ Hooks
βββ useSessionWallet
βββ useSwapLogic
βββ useNetworkSwitch
Stream-Based Communicationβ
Communication Architectureβ
SuperSafe uses stream-based communication for efficient inter-process communication:
Communication Flow:
Frontend ββStreamβββΊ Background Service Worker
β β
β β
βββββResponseββββββββββ
β β
β β
βββββEventsββββββββββββ
Stream Channelsβ
- Session Channel: Session management and authentication
- Provider Channel: EIP-1193 provider operations
- Swap Channel: Token swapping operations
- Send Channel: Transaction sending
- Blockchain Channel: Blockchain operations
- API Channel: External API calls
Stream Handler Architectureβ
Session Stream Handlerβ
// Session management operations
const sessionOperations = {
GET_SESSION_STATE: 'getSessionState',
UNLOCK: 'unlock',
CREATE_WALLET: 'createWallet',
SWITCH_WALLET: 'switchWallet',
LOCK: 'lock'
};
Provider Stream Handlerβ
// EIP-1193 provider operations
const providerOperations = {
ETH_REQUEST_ACCOUNTS: 'eth_requestAccounts',
ETH_ACCOUNTS: 'eth_accounts',
ETH_CHAIN_ID: 'eth_chainId',
ETH_SEND_TRANSACTION: 'eth_sendTransaction',
PERSONAL_SIGN: 'personal_sign',
ETH_SIGN_TYPED_DATA_V4: 'eth_signTypedData_v4',
WALLET_SWITCH_ETHEREUM_CHAIN: 'wallet_switchEthereumChain'
};
Security Architectureβ
Defense-in-Depth Modelβ
SuperSafe implements multiple layers of security:
Security Layers:
βββ Browser Isolation
β βββ Extension Sandbox
β βββ Content Script Isolation
β βββ Service Worker Isolation
βββ Context Separation
β βββ Frontend/Backend Separation
β βββ Memory Isolation
β βββ Process Isolation
βββ Cryptographic Protection
β βββ AES-256-GCM Encryption
β βββ PBKDF2 Key Derivation
β βββ Double Encryption
βββ Session Management
β βββ Auto-Lock System
β βββ Memory-Only Storage
β βββ Session Persistence
βββ Access Control
β βββ AllowList System
β βββ Permission Management
β βββ User Consent
βββ Attack Mitigation
βββ Rate Limiting
βββ Request Deduplication
βββ Phishing Protection
Trust Boundariesβ
Trust Boundary 1: Browser Isolationβ
- Extension Sandbox: Isolated from web pages
- Content Script: Controlled access to web pages
- Service Worker: Isolated from web pages
Trust Boundary 2: Context Separationβ
- Frontend/Backend: Clear separation of concerns
- Memory Isolation: Sensitive data in memory only
- Process Isolation: Isolated processes
Trust Boundary 3: Cryptographic Protectionβ
- Vault Encryption: All sensitive data encrypted
- Key Derivation: Secure key derivation
- Memory Security: Sensitive data in memory only
Data Flow Patternsβ
Connection Request Flowβ
Connection Request Flow:
1. dApp requests connection
2. Content script intercepts request
3. Background service worker processes request
4. AllowList validation
5. Network compatibility check
6. User consent request
7. Connection established
8. Provider events emitted
Transaction Signing Flowβ
Transaction Signing Flow:
1. dApp requests transaction
2. Background service worker validates
3. SigningRequestManager processes
4. User confirmation dialog
5. Transaction signing
6. Transaction broadcast
7. Confirmation events
Network Switch Flowβ
Network Switch Flow:
1. dApp requests network switch
2. Background service worker validates
3. NetworkSwitchService processes
4. User consent request
5. Network switch execution
6. Provider events emitted
7. dApp notification
Technology Stackβ
Core Technologiesβ
Frontendβ
- React 18.2.0: Modern React with hooks
- Vite 6.3.6: Fast build tool
- TailwindCSS 3.3.3: Utility-first CSS
- Chrome Extension Manifest V3: Latest extension API
Backendβ
- Service Worker: Background processing
- Streams API: Inter-process communication
- Chrome Storage API: Local storage
- Crypto API: Cryptographic operations
Blockchainβ
- ethers.js 6.13.0: Ethereum library
- EIP-1193: Provider standard
- EIP-6963: Wallet discovery
- WalletConnect V2: Mobile connectivity
Performance Metricsβ
Response Timesβ
- Average Response: <150ms
- Connection Time: <200ms
- Transaction Signing: <300ms
- Network Switch: <250ms
Bundle Sizesβ
- Popup Bundle: 2.1MB
- Background Bundle: 1.8MB
- Content Script: 0.5MB
- Total Size: 4.4MB
Development Architectureβ
Project Structureβ
SuperSafe/
βββ src/
β βββ background/ # Background service worker
β β βββ background.js # Main background script
β β βββ controllers/ # Background controllers
β β βββ handlers/ # Stream handlers
β β βββ managers/ # Background managers
β β βββ utils/ # Background utilities
β βββ components/ # React components
β β βββ Dashboard/ # Dashboard components
β β βββ Wallet/ # Wallet components
β β βββ Transaction/ # Transaction components
β β βββ Swap/ # Swap components
β βββ screens/ # Screen components
β β βββ CreateWallet/ # Create wallet screen
β β βββ ImportWallet/ # Import wallet screen
β β βββ SendTransaction/ # Send transaction screen
β βββ hooks/ # Custom React hooks
β β βββ useSessionWallet.js
β β βββ useSwapLogic.js
β β βββ useNetworkSwitch.js
β βββ utils/ # Utility functions
β βββ networks.js # Network configuration
β βββ provider.js # Provider utilities
β βββ security.js # Security utilities
βββ public/ # Static assets
β βββ assets/ # Images and icons
β βββ allowlist.json # AllowList configuration
βββ docs/ # Documentation
Build Systemβ
Vite Configurationβ
// vite.config.js
export default defineConfig({
plugins: [react()],
build: {
rollupOptions: {
input: {
popup: 'src/popup.html',
background: 'src/background/background.js',
content: 'src/content-script.js'
}
}
}
});
Build Outputβ
dist/
βββ popup.html # Popup HTML
βββ popup.js # Popup JavaScript
βββ background.js # Background script
βββ content-script.js # Content script
βββ assets/ # Static assets
βββ manifest.json # Extension manifest
Integration Patternsβ
dApp Integrationβ
Provider Injectionβ
// Content script injects provider
const injectProvider = () => {
const script = document.createElement('script');
script.src = chrome.runtime.getURL('provider.js');
script.onload = () => script.remove();
document.head.appendChild(script);
};
Framework Detectionβ
// Detect dApp framework
const detectFramework = () => {
if (window.rainbowkit) return 'rainbowkit';
if (window.wagmi) return 'wagmi';
if (window.web3React) return 'web3-react';
if (window.dynamic) return 'dynamic';
return 'unknown';
};
Mobile Integrationβ
WalletConnect V2β
// WalletConnect integration
const walletConnect = new WalletConnect({
projectId: 'your-project-id',
chains: [1, 10, 5330],
showQrModal: true
});
Best Practicesβ
Development Guidelinesβ
Security Firstβ
- Validate Inputs: Always validate user inputs
- Check Permissions: Verify required permissions
- Handle Errors: Implement proper error handling
- Test Thoroughly: Test all security scenarios
Performance Optimizationβ
- Stream Communication: Use streams for efficiency
- Memory Management: Manage memory carefully
- Bundle Optimization: Optimize bundle sizes
- Lazy Loading: Use lazy loading where appropriate
Code Qualityβ
- TypeScript: Use TypeScript for type safety
- Testing: Write comprehensive tests
- Documentation: Document all APIs
- Code Review: Regular code reviews
Troubleshootingβ
Common Issuesβ
Architecture Issuesβ
- Service Worker: Check service worker status
- Stream Communication: Verify stream handlers
- Memory Leaks: Check for memory leaks
- Performance: Monitor performance metrics
Integration Issuesβ
- Provider Injection: Check provider injection
- Framework Detection: Verify framework detection
- Event Handling: Check event handlers
- Error Handling: Verify error handling
Next Stepsβ
Now that you understand the architecture:
- Advanced Topics - Deep dive into architecture
- Main Components - Learn about main components
- State Management - Understand state management
- Integration Overview - Review integration guide
Ready to learn more about advanced topics? Continue to Advanced Topics!