Overview
The SubWallet Extension background runs as a service worker that handles all core wallet operations including keyring management, blockchain interactions, transaction processing, and state management. It operates independently of the UI and manages communication with both content scripts and the extension popup.Entry Point
The background service initializes inpackages/extension-koni/src/background.ts:
packages/extension-koni/src/background.ts:25-59
Core Components
1. ActionHandler
TheActionHandler is a singleton that manages the lifecycle of port connections and message routing.
Location: packages/extension-koni/src/helper/ActionHandler.ts
packages/extension-koni/src/helper/ActionHandler.ts:16-148
Key Responsibilities:
- Manages port connections from content scripts and UI
- Tracks active connections and handles sleep/wake cycles
- Routes messages to the appropriate handler
- Implements a 60-second timeout before entering sleep mode
2. SWHandler
TheSWHandler orchestrates all background handlers and routes requests.
Location: packages/extension-base/src/koni/background/handlers/index.ts
packages/extension-base/src/koni/background/handlers/index.ts:14-88
3. KoniState
The central state manager that coordinates all services. Location:packages/extension-base/src/koni/background/handlers/State.ts
packages/extension-base/src/koni/background/handlers/State.ts:96-100
Background Services
KoniState manages multiple specialized services:Core Services
- EventService: Event bus for inter-service communication
- ChainService: Manages blockchain connections and metadata
- BalanceService: Tracks account balances across chains
- PriceService: Fetches and caches token prices
- KeyringService: Manages accounts and keys
- TransactionService: Handles transaction creation and submission
Feature Services
- EarningService: Manages staking and yield positions
- SwapService: Handles token swaps across protocols
- NftService: NFT collection and item management
- WalletConnectService: WalletConnect protocol integration
- RequestService: Manages dApp authorization and signing requests
- HistoryService: Transaction history tracking
- MultisigService: Multisig account operations
Port Communication
The background service communicates through Chrome runtime ports:Port Types
packages/extension-base/src/defaults.ts:17-19
Connection Lifecycle
- Connection: Content script or UI connects via
chrome.runtime.connect() - Registration: ActionHandler registers the port and assigns an ID
- Activation: First message triggers service wakeup
- Message Handling: Messages routed through SWHandler to appropriate handler
- Disconnection: Port disconnect triggers cleanup and sleep timeout
- Sleep: After 60 seconds of no connections, services enter sleep mode
Message Flow
Service Lifecycle
Initialization
Sleep/Wake Cycle
Wake Conditions:- First port connection
- Message requiring active services
- Full wake for
pri()ormobile()messages
- All ports disconnected
- 60-second timeout elapsed
- Services pause background operations
Global Variables (Development)
In non-production mode, debug helpers are exposed:packages/extension-koni/src/background.ts:74-77
Access in console: KoniState, KoniHandler
Best Practices
- Service Initialization: Always check if services are initialized before use
- Error Handling: Wrap service calls in try-catch blocks
- Memory Management: Services should clean up subscriptions on sleep
- Port Management: Always handle port disconnect events
- State Synchronization: Use event service for cross-service communication
Common Patterns
Adding a New Service
- Create service class extending base service
- Add service instance to KoniState
- Initialize service in
KoniState.init() - Implement
wakeup()andsleep()methods - Register message handlers in appropriate handler class
Service Communication
Related Documentation
- Message Passing - Inter-component communication
- State Management - State storage and persistence
- Extension UI - UI component architecture