aboutsummaryrefslogtreecommitdiffstats
path: root/build.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-08-01 11:44:43 -0700
committerAdam Soltys <[email protected]>2026-08-01 11:44:43 -0700
commitd561d4110c7b484bee077678b8469188a3541753 (patch)
tree7b4ce64345cdc2a0081dcdb24a82d3a608b507a2 /build.js
parent0319e2fd392fc7e8569fbb6a88a6a1dd65e9d539 (diff)
downloadcoinosv3-d561d4110c7b484bee077678b8469188a3541753.tar.gz
coinosv3-d561d4110c7b484bee077678b8469188a3541753.zip
Wake hal for wallet-connect requests while it is closed
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 <[email protected]>
Diffstat (limited to 'build.js')
-rw-r--r--build.js35
1 files changed, 35 insertions, 0 deletions
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;