// Build a single, self-contained dist/index.html (plus PWA sidecar files). // // Everything — app code, the @scure/@noble crypto libs, the QR generator, and // all CSS — is bundled and inlined so the result is one file you can save and // open offline straight from the filesystem (file://). No server, no network. // // For the hosted site we also emit a web manifest, icons, and a small service // worker so the page can be installed as a PWA and still work offline once // installed. index.html stays self-contained; these are optional extras that // simply 404 (harmlessly) when the file is opened on its own from file://. import { mkdir, readdir } from 'node:fs/promises'; const FAVICON = 'data:image/svg+xml,' + encodeURIComponent( `` ); // Static assets copied verbatim into dist/ (icons referenced by the manifest). const STATIC = ['icon-192.png', 'icon-512.png', 'icon-maskable-512.png', 'icon.svg']; const MANIFEST = { id: '/', name: 'Hal', short_name: 'Hal', description: 'Self-custody Bitcoin wallet that runs entirely in your browser.', start_url: './', scope: './', display: 'standalone', background_color: '#eef0f3', theme_color: '#f7931a', icons: [ { src: 'icon-192.png', sizes: '192x192', type: 'image/png', purpose: 'any' }, { src: 'icon-512.png', sizes: '512x512', type: 'image/png', purpose: 'any' }, { src: 'icon-maskable-512.png', sizes: '512x512', type: 'image/png', purpose: 'maskable' }, ], }; // Service worker: precache the app shell, network-first for the page (so a new // deploy shows up), cache-first for the icons/manifest, and never touch the // cross-origin explorer/Nostr requests. CACHE carries a build token so each // deploy supersedes the previous cache. {{VERSION}} is filled in at build time. const SW = `const CACHE = 'cold-{{VERSION}}'; // jsqr.js is precached so the lazy QR decoder is available offline too (for // installed PWAs on browsers without a native BarcodeDetector). const SHELL = ['./', 'manifest.webmanifest', 'icon-192.png', 'icon-512.png', 'jsqr.js']; self.addEventListener('install', (e) => { self.skipWaiting(); e.waitUntil(caches.open(CACHE).then((c) => c.addAll(SHELL))); }); self.addEventListener('activate', (e) => { e.waitUntil( caches .keys() .then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k)))) .then(() => self.clients.claim()) ); }); self.addEventListener('fetch', (e) => { const req = e.request; if (req.method !== 'GET') return; if (new URL(req.url).origin !== self.location.origin) return; // explorer/ws → network if (req.mode === 'navigate') { e.respondWith( fetch(req) .then((res) => { const copy = res.clone(); caches.open(CACHE).then((c) => c.put('./', copy)); return res; }) .catch(() => caches.match('./')) ); return; } // Cache-first, but cache same-origin assets (e.g. the chosen locale, icons) // on first fetch so they're available offline without precaching them all. e.respondWith( caches.match(req).then( (hit) => hit || fetch(req).then((res) => { if (res.ok) { const copy = res.clone(); caches.open(CACHE).then((c) => c.put(req, copy)); } return res; }) ) ); }); `; const PWA_HEAD = ` `; // Register with updateViaCache:'none' so the browser always re-fetches sw.js // (never from HTTP cache) and detects a new build immediately. When the new SW // takes control (it skipWaiting + clients.claim), reload once so the fresh // inlined app runs — but only if a SW was already controlling this page, so a // first-ever visit doesn't reload. const SW_REGISTER = ``; // Bundle the standalone jsQR decoder (sets window.jsQR) for lazy loading. export async function buildJsQr({ minify = true } = {}) { const result = await Bun.build({ entrypoints: ['./src/jsqr-global.js'], target: 'browser', minify, }); if (!result.success) { for (const log of result.logs) console.error(log); throw new Error('jsqr 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 } = {}) { const result = await Bun.build({ entrypoints: ['./src/sp-worker.js'], target: 'browser', minify, }); if (!result.success) { for (const log of result.logs) console.error(log); throw new Error('sp-worker bundle failed'); } return result.outputs[0].text(); } // Optional-feature selection: HAL_FEATURES is a comma list of enabled // features (gifts,swaps,ark,zaps,sp,nwc). Unset means all; "none"/"" means a minimal // on-chain-only wallet. A build plugin swaps src/features/index.js for a // generated module that only imports the enabled features, so a disabled // feature's code (and its network endpoints) never enters the bundle. // NB order matters and must match src/features/index.js — nwc sits after ark // because it drives ark's headless pay/balance seam. const ALL_FEATURES = { gifts: 'giftsFeature', swaps: 'swapsFeature', ark: 'arkFeature', zaps: 'zapsFeature', sp: 'spFeature', nwc: 'nwcFeature', sync: 'syncFeature' }; export function enabledFeatures(spec = process.env.HAL_FEATURES) { if (spec == null) return Object.keys(ALL_FEATURES); return spec.split(',').map((x) => x.trim().toLowerCase()).filter((x) => x in ALL_FEATURES); } function featureIndexSource(enabled) { return enabled.map((f) => `import { ${ALL_FEATURES[f]} } from './${f}.js';`).join('\n') + `\nexport function buildFeatures(ctx) { return [${enabled.map((f) => `${ALL_FEATURES[f]}(ctx)`).join(', ')}]; }\n`; } const featurePlugin = (enabled) => ({ name: 'hal-features', setup(b) { b.onLoad({ filter: /src[\/\\]features[\/\\]index\.js$/ }, () => ({ contents: featureIndexSource(enabled), loader: 'js', })); }, }); export async function buildHtml({ minify = true, pwa = minify, features = process.env.HAL_FEATURES } = {}) { const result = await Bun.build({ entrypoints: ['./src/app.js'], target: 'browser', minify, plugins: [featurePlugin(enabledFeatures(features))], }); if (!result.success) { for (const log of result.logs) console.error(log); throw new Error('bundle failed'); } let js = await result.outputs[0].text(); // Guard against a literal inside the bundle closing our tag early. js = js.replaceAll('