aboutsummaryrefslogtreecommitdiffstats
path: root/src/sw-nwc.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-08-01 19:02:25 -0700
committerAdam Soltys <[email protected]>2026-08-01 19:02:25 -0700
commitb73bb9ee51b9be33bc1859fd56d3e11905c3d456 (patch)
tree8a008d57e450961d1ab203a663e9d8fa233a2686 /src/sw-nwc.js
parent2a7a825ce5850a2a836effae820adbae0b66d093 (diff)
downloadcoinosv3-b73bb9ee51b9be33bc1859fd56d3e11905c3d456.tar.gz
coinosv3-b73bb9ee51b9be33bc1859fd56d3e11905c3d456.zip
Background NWC: the worker now answers — and pays — from the pouch
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 <[email protected]>
Diffstat (limited to 'src/sw-nwc.js')
-rw-r--r--src/sw-nwc.js49
1 files changed, 49 insertions, 0 deletions
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('./');
+ })());
+});