From d561d4110c7b484bee077678b8469188a3541753 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sat, 1 Aug 2026 11:44:43 -0700 Subject: Wake hal for wallet-connect requests while it is closed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Step 3, first half. Android Chrome freezes backgrounded PWAs and closes their relay sockets, so a phone with hal "open" answers nothing — which is what made Amethyst spin. Push is the only way to reach a frozen app. - service worker gains a `push` handler. It deliberately does NOT spend: if a window is open it nudges it to re-listen, otherwise it raises a notification that opens hal on tap. Putting a spend path in the worker is the remaining half and wants its own testing. - hal-side: request permission, subscribe with the notifier's VAPID key, and register its service pubkeys with nwcpush.coinos.io. Turning it off deregisters, unsubscribes, and clears the pouch — no reason to leave spendable keys where a worker can read them if nothing will. - a Settings toggle, and the ark feature exposes the ASP context the pouch record needs. Co-Authored-By: Claude Fable 5 --- build.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'build.js') diff --git a/build.js b/build.js index db07ed2..7870075 100644 --- a/build.js +++ b/build.js @@ -60,6 +60,41 @@ self.addEventListener('activate', (e) => { ); }); +// NWC push: the notifier wakes us when a wallet-connect request arrives. +// This handler deliberately does NOT spend. It surfaces the request so the +// user can open Hal, which then answers it from the relay. Auto-answering +// from the worker needs the pouch spend path and is a separate step. +self.addEventListener('push', (e) => { + let data = {}; + try { data = e.data ? e.data.json() : {}; } catch (_) {} + if (data.type !== 'nwc') return; + e.waitUntil((async () => { + // If a window is already open it can handle this itself — tell it, and + // stay quiet rather than nagging the user. + const clients = await self.clients.matchAll({ type: 'window', includeUncontrolled: true }); + if (clients.length) { + for (const c of clients) c.postMessage({ type: 'nwc-wake', servicePubkey: data.servicePubkey }); + return; + } + await self.registration.showNotification('Payment request', { + body: 'An app is asking your wallet to pay. Open Hal to approve.', + icon: 'icon-192.png', badge: 'icon-192.png', + tag: 'nwc-' + (data.servicePubkey || 'req'), renotify: false, + data: { url: './' }, + }); + })()); +}); + +self.addEventListener('notificationclick', (e) => { + e.notification.close(); + e.waitUntil((async () => { + const all = await self.clients.matchAll({ type: 'window', includeUncontrolled: true }); + const open = all.find((c) => 'focus' in c); + if (open) return open.focus(); + if (self.clients.openWindow) return self.clients.openWindow('./'); + })()); +}); + self.addEventListener('fetch', (e) => { const req = e.request; if (req.method !== 'GET') return; -- cgit v1.2.3