Skip to main content

πŸ—οΈ 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:

  1. Advanced Topics - Deep dive into architecture
  2. Main Components - Learn about main components
  3. State Management - Understand state management
  4. Integration Overview - Review integration guide

Ready to learn more about advanced topics? Continue to Advanced Topics!