aboutsummaryrefslogtreecommitdiffstats
path: root/build.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 /build.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 'build.js')
-rw-r--r--build.js92
1 files changed, 57 insertions, 35 deletions
diff --git a/build.js b/build.js
index 7870075..f542533 100644
--- a/build.js
+++ b/build.js
@@ -60,40 +60,11 @@ 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('./');
- })());
-});
+// NWC push handling lives in src/sw-nwc.js (bundled and appended below when
+// the nwc+ark features ship): it tries to ANSWER the request from the pouch
+// and falls back to a notification. {{NWC_WAKE_FALLBACK}} carries a
+// notify-only handler for builds without those features.
+{{NWC_WAKE_FALLBACK}}
self.addEventListener('fetch', (e) => {
const req = e.request;
@@ -163,6 +134,50 @@ export async function buildJsQr({ minify = true } = {}) {
return result.outputs[0].text();
}
+// The notify-only push handler for builds without the ark+nwc features (the
+// full builds get the bundled auto-answering handler from src/sw-nwc.js).
+const SW_WAKE_ONLY = `self.addEventListener('push', (e) => {
+ let data = {};
+ try { data = e.data ? e.data.json() : {}; } catch (_) {}
+ if (data.type !== 'nwc') return;
+ e.waitUntil((async () => {
+ 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('./');
+ })());
+});`;
+
+// The auto-answering NWC worker module, bundled for the SW global scope.
+export async function buildSwNwc({ minify = true } = {}) {
+ const result = await Bun.build({
+ entrypoints: ['./src/sw-nwc.js'],
+ target: 'browser',
+ minify,
+ });
+ if (!result.success) {
+ for (const log of result.logs) console.error(log);
+ throw new Error('sw-nwc bundle failed');
+ }
+ return result.outputs[0].text();
+}
+
// The silent-payment scanning worker, bundled separately (its own global scope)
// and embedded as base64 so the single-file app can spawn it from a Blob URL.
export async function buildSpWorker({ minify = true } = {}) {
@@ -263,7 +278,14 @@ if (import.meta.main) {
// PWA sidecars.
await Bun.write('dist/manifest.webmanifest', JSON.stringify(MANIFEST, null, 2));
const version = Bun.hash(html).toString(36);
- await Bun.write('dist/sw.js', SW.replace('{{VERSION}}', version));
+ // Full builds get the auto-answering NWC worker; feature-stripped builds
+ // keep the notify-only handler (the responder would just dead-weight them).
+ const feats = enabledFeatures();
+ const nwcSw = feats.includes('nwc') && feats.includes('ark');
+ const swBody = SW
+ .replace('{{VERSION}}', version)
+ .replace('{{NWC_WAKE_FALLBACK}}', nwcSw ? '' : SW_WAKE_ONLY);
+ await Bun.write('dist/sw.js', nwcSw ? swBody + '\n' + await buildSwNwc() : swBody);
for (const f of STATIC) await Bun.write('dist/' + f, Bun.file('static/' + f));
const kb = (Buffer.byteLength(html) / 1024).toFixed(0);