From b73bb9ee51b9be33bc1859fd56d3e11905c3d456 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sat, 1 Aug 2026 19:02:25 -0700 Subject: Background NWC: the worker now answers — and pays — from the pouch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The no-tap half of background wallet-connect. The push payload carries the (encrypted, client-signed) request event; a bundled service-worker responder (src/sw-nwc.js -> nwc-respond.js) finds the matching pouch in IndexedDB, decrypts with the connection's service key, and answers get_info/get_balance/list_transactions from pouch state. pay_invoice pays through the swap bridge with an ArkManager running over the pouch's chain-6 key window (one HTLC lock, one HTTP swap), enforcing per-payment and daily budgets from the pouch's own spend log, checking the notifier's /answered endpoint so it never double-pays or shouts over an open device, and persisting the swap before handing it off so the app can refund a crashed flight. Anything it can't handle falls back to the wake-the-user notification. The notifier grows /publish (verified 23195s only, rate-limited) as the worker's reply pipe and /answered for race avoidance. The app gains pouch funding (mailbox-less arkoor self-send whose recipient bytes we keep), emptying, spend/budget reconciliation on open, and refund recovery with the pouch's own keys. Co-Authored-By: Claude Fable 5 --- src/sw-nwc.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/sw-nwc.js (limited to 'src/sw-nwc.js') diff --git a/src/sw-nwc.js b/src/sw-nwc.js new file mode 100644 index 0000000..db57b99 --- /dev/null +++ b/src/sw-nwc.js @@ -0,0 +1,49 @@ +// Service-worker side of background NWC. Bundled by build.js and appended to +// the generated sw.js, replacing the notify-only push handler: a push now +// first tries to ANSWER the request from the pouch (see nwc-respond.js), and +// only falls back to the wake-the-user notification when it can't. + +import { respondFromPouch } from './nwc-respond.js'; + +const NOTIFIER = 'https://nwcpush.coinos.io'; + +self.addEventListener('push', (e) => { + let data = {}; + try { data = e.data ? e.data.json() : {}; } catch (_) {} + if (data.type !== 'nwc') return; + e.waitUntil((async () => { + // An open window handles requests itself with full wallet state — nudge + // it and stay out of the way. + 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; + } + let handled = false; + try { + handled = await respondFromPouch(data, { + notifier: NOTIFIER, + log: (m) => console.log('[sw-nwc]', m), + }); + } catch (err) { + console.warn('[sw-nwc] auto-answer failed:', err && err.message); + } + if (handled) 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('./'); + })()); +}); -- cgit v1.2.3