From a0f843d9215baca89f7b30ded8bb943f15f733e4 Mon Sep 17 00:00:00 2001 From: martbost Date: Thu, 8 Oct 2026 07:25:17 -0500 Subject: [PATCH] Signed /api/partner/registry: InstantAdPay pushes member rows live; new people become shell accounts with their line, existing shells gain username/wallet/sponsor Co-Authored-By: Claude Fable 5.1 --- server.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/server.js b/server.js index a6e0ad5..85cd1a4 100644 --- a/server.js +++ b/server.js @@ -2014,6 +2014,30 @@ const server = http.createServer(async (req, res) => { if (!isAdmin(req)) return json(res, 401, { error: 'auth' }); const b = await readBody(req); return json(res, 200, await carry.seedAll(Number(b.limit) || 0)); } + // -- live registry sync from InstantAdPay (2026-10-08): signed rows (HMAC LS_SYNC_SECRET over ts + newline + raw body), + // upserted into the registry; new people become shell accounts with their line, existing shells pick up a username, + // wallet or sponsor they did not have. Only the posted rows are touched. + if (p === '/api/partner/registry' && req.method === 'POST') { + const secret = String(process.env.LS_SYNC_SECRET || '').trim(); if (secret.length < 32) return json(res, 503, { error: 'sync not configured' }); + const raw = await new Promise((ok, no) => { let d = ''; req.on('data', c => { d += c; if (d.length > 2e6) { req.destroy(); no(new Error('too big')); } }); req.on('end', () => ok(d)); req.on('error', no); }); + const ts = String(req.headers['x-ls-ts'] || ''), sig = String(req.headers['x-ls-sig'] || ''); + if (!/^\d{13}$/.test(ts) || Math.abs(Date.now() - Number(ts)) > 10 * 60000) return json(res, 401, { error: 'stale' }); + const want = crypto.createHmac('sha256', secret).update(ts + '\n' + raw).digest('hex'); + if (want.length !== sig.length || !crypto.timingSafeEqual(Buffer.from(want), Buffer.from(sig))) return json(res, 401, { error: 'bad signature' }); + let b; try { b = JSON.parse(raw || '{}'); } catch (e) { return json(res, 400, { error: 'bad json' }); } + const rows = Array.isArray(b.rows) ? b.rows.slice(0, 500) : []; + const n = await registry.importRows(rows, 'instantadpay'); let shells = 0, updated = 0; + for (const r of rows) { + const e = String(r.email || '').trim().toLowerCase(); if (!e) continue; + let a = await accounts.byEmail(e); + if (!a) { const x = await accounts.ensure(e, '', 'carried', 'network'); if (x.created) shells++; a = await accounts.byEmail(e); } + if (!a) continue; + if (r.username && !a.username) { try { await accounts.setUsername(e, r.username); updated++; } catch (err) {} } + if (r.wallet && !a.address) { try { await accounts.linkWallet(e, r.wallet); updated++; } catch (err) {} } + if (r.sponsorEmail && !a.sponsorRef) { const sp = await accounts.byEmail(r.sponsorEmail); if (sp && (sp.username || sp.code)) { try { await accounts.setSponsorRef(e, sp.username || sp.code); updated++; } catch (err) {} } } + } + return json(res, 200, { ok: true, rows: rows.length, imported: n, shells, updated, registry: await registry.count() }); + } if (p === '/api/admin/registry/import' && req.method === 'POST') { if (!isAdmin(req)) return json(res, 401, { error: 'auth' }); const b = await readBody(req); const n = await registry.importRows(b.rows || [], b.property || 'instantadpay');