// UnifiedPush: the way a phone without Google Play Services can be reached. // // The Android wrapper asks a distributor (ntfy and friends) for an endpoint // and hands it to the web app on the launch URL as ?up=. The web app is what // knows which pubkeys to watch, so it does the registering; the notifier then // POSTs payloads straight to that endpoint instead of through Firebase. // // This checks the web half: the endpoint is picked up, kept, registered with // the notifier, and used in place of a browser subscription that could never // have worked on such a phone. // // Run: bun tools/unifiedpush-test.js import puppeteer from './puppeteer.js'; import { generateMnemonic } from '@scure/bip39'; import { wordlist } from '@scure/bip39/wordlists/english'; import { buildHtml } from '../build.js'; import { cacheKeyFor } from '../src/wallet.js'; const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); let ok = true; const check = (n, c, d = '') => { console.log(` ${c ? '✓' : '✗'} ${n}${d ? ' — ' + d : ''}`); if (!c) ok = false; }; const html = await buildHtml({ minify: true, pwa: false }); const server = Bun.serve({ port: 5263, fetch: () => new Response(html, { headers: { 'content-type': 'text/html' } }) }); const browser = await puppeteer.launch({ executablePath: '/usr/bin/google-chrome', headless: 'new', args: ['--no-sandbox'] }); const page = await browser.newPage(); // a phone with no push service at all, which is the whole point of the exercise await page.evaluateOnNewDocument(() => { const reg = { pushManager: { getSubscription: () => Promise.resolve(null), subscribe: () => Promise.reject(new DOMException('Registration failed - push service error', 'AbortError')), }, showNotification: () => Promise.resolve(), }; Object.defineProperty(navigator.serviceWorker, 'ready', { get: () => Promise.resolve(reg) }); Notification.requestPermission = () => Promise.resolve('granted'); Object.defineProperty(Notification, 'permission', { get: () => 'granted' }); }); // watch what it tries to tell the notifier const registrations = []; await page.setRequestInterception(true); page.on('request', (r) => { if (r.url().includes('nwcpush.coinos.io/register') && r.method() === 'POST') { try { registrations.push(JSON.parse(r.postData() || '{}')); } catch {} return r.respond({ status: 200, contentType: 'application/json', body: '{"ok":true}' }); } if (r.url().includes('nwcpush.coinos.io/vapid')) return r.respond({ status: 200, contentType: 'application/json', body: '{"publicKey":"BKd0FOtD3Y9gAB3ZFDkCPlBtm0IhEbiKUeYUCE7Tuh9nWDOAhBKiZ0dPYjYQkHhVYyPNPUn0E-gLRPPqzYyAWmA"}' }); return r.continue(); }); const click = (t) => page.evaluate((x) => { const e = [...document.querySelectorAll('button')].find((n) => n.textContent.trim().toLowerCase().includes(x)); if (e) { e.click(); return true; } return false; }, t); const waitText = async (x, ms = 25000) => { for (let i = 0; i < ms/250; i++) { if ((await page.evaluate(() => document.body.innerText)).toLowerCase().includes(x)) return true; await sleep(250); } return false; }; const ENDPOINT = 'https://ntfy.sh/upTESTendpoint123'; try { await page.setViewport({ width: 390, height: 844 }); await page.goto('http://localhost:5263/', { waitUntil: 'domcontentloaded' }); await page.evaluate(() => localStorage.setItem('btc-wallet-network', 'regtest')); await page.reload({ waitUntil: 'domcontentloaded' }); await sleep(400); await click('create a new wallet'); await sleep(500); await click('to import'); await sleep(400); await page.waitForSelector('textarea'); const mn = generateMnemonic(wordlist); await page.type('textarea', mn); await click('open wallet'); await waitText('receive', 20000); // the wrapper launches the web app with the endpoint on the URL await page.goto('http://localhost:5263/?up=' + encodeURIComponent(ENDPOINT), { waitUntil: 'domcontentloaded' }); await waitText('receive', 20000); await sleep(2500); const kept = await page.evaluate(() => localStorage.getItem('coinos-unifiedpush')); check('the endpoint is taken off the launch URL and kept', kept === ENDPOINT, kept || 'nothing stored'); check('...and the URL is tidied afterwards', !(await page.url()).includes('up='), await page.url()); const up = registrations.find((r) => r.subscription && r.subscription.unifiedpush); check('the notifier is told to use it', !!up, JSON.stringify(registrations.map((r) => Object.keys(r))) || 'no registration'); check('...with the endpoint, marked as UnifiedPush', up && up.subscription.endpoint === ENDPOINT, up ? up.subscription.endpoint : ''); check('...and the watch list rides along', !!(up && up.notify), up ? Object.keys(up.notify || {}).join(',') : ''); const txt = await page.evaluate(() => document.body.innerText); check('the browser\'s own dead push is no longer the story', !/no push service/i.test(txt), (txt.match(/[^\n]*push service[^\n]*/i) || [''])[0].slice(0, 50)); // Inside the Android build, the advice about push is about the // distributor, not about browsers — it IS the browser. await page.evaluateOnNewDocument(() => { window.CoinosHost = { env: () => JSON.stringify({ app: 'graphene', distributors: 0, distributor: null, endpoint: false }) }; }); // a phone that has never managed a push: no endpoint, and the browser's // own push already known to be dead await page.evaluate((k) => { localStorage.removeItem('coinos-unifiedpush'); const st = JSON.parse(localStorage.getItem(k) || '{}'); st.push = false; st.noPushService = true; localStorage.setItem(k, JSON.stringify(st)); }, cacheKeyFor(mn + '\n') + ':messages'); await page.goto('http://localhost:5263/', { waitUntil: 'domcontentloaded' }); await waitText('receive', 20000); await page.evaluate(() => { const b = [...document.querySelectorAll('button')].find((e) => /message/i.test(e.getAttribute('aria-label') || '')); if (b) b.click(); }); await sleep(2500); const advice = await page.evaluate(() => document.body.innerText); check('with no distributor installed it says to install one', /ntfy/i.test(advice), (advice.match(/[^\n]*ntfy[^\n]*/i) || ['nothing about a distributor'])[0].slice(0, 60)); check('...and not to go and use Firefox', !/Firefox/.test(advice)); // The WebView build wakes the wallet off-screen with ?wake= and a // CoinosHost bridge. The page has to notice, tidy the URL, and say when // it's finished so the service can stop. await page.evaluateOnNewDocument(() => { window.__done = 0; window.CoinosHost = { done: () => { window.__done++; }, notify: () => {} }; }); await page.goto('http://localhost:5263/?wake=' + encodeURIComponent(JSON.stringify({ type: 'nwc', servicePubkey: 'a'.repeat(64) })), { waitUntil: 'domcontentloaded' }); await waitText('receive', 20000); check('a woken run takes the payload off the URL', !(await page.url()).includes('wake='), await page.url()); await sleep(8000); // a wallet with no connections has nothing to wait for const done = await page.evaluate(() => window.__done); check('...and tells the service when to stop', done > 0, done + ' call(s) to CoinosHost.done()'); } finally { await browser.close(); server.stop(true); } console.log(ok ? '\n✅ a de-Googled phone can be reached' : '\n❌ failed'); process.exit(ok ? 0 : 1); 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330