Service Worker support is a new feature in v0.3. It enables your wallet to run in the background, providing persistent connectivity and event handling.
Create a service worker file that handles communication:
Copy
Ask AI
// service-worker.jsimport { Worker } from '@arkade-os/sdk'// Worker handles communication between the main thread and service workernew Worker().start()
That’s all you need! The Worker class automatically:
IndexedDB is recommended for service workers because it’s accessible from both the main thread and service worker context, and can store larger amounts of data.
// ✅ Good - IndexedDB works in service worker contextconst storage = new IndexedDBStorageAdapter('wallet-db', 1)// ❌ Bad - LocalStorage not available in service workersconst storage = new LocalStorageAdapter()
Identity Management
Store your identity securely and load it on initialization:
Copy
Ask AI
const storage = new IndexedDBStorageAdapter('wallet-db', 1)// Always check if identity exists before creating new onelet privateKeyHex = await storage.getItem('private-key')if (!privateKeyHex) { privateKeyHex = SingleKey.fromRandomBytes().toHex() await storage.setItem('private-key', privateKeyHex)}
Error Handling
Implement proper error handling for service worker operations:
Copy
Ask AI
try { const wallet = await ServiceWorkerWallet.setup({ serviceWorkerPath: '/service-worker.js', arkServerUrl: 'https://mutinynet.arkade.sh', identity })} catch (error) { console.error('Failed to setup service worker wallet:', error) // Fallback to regular wallet}