// LinkSpin registry sync (Marty 2026-10-08: "members in InstantAdPay synchronized with those waiting inside LinkSpin"). // Two paths, same signed POST to LinkSpin /api/partner/registry (HMAC-SHA256 of ts + newline + raw body, LS_SYNC_SECRET): // touch(email) right after an account changes (created, named, wallet linked, sponsor bound): that row goes within ~3 s // tick() every 2 minutes: every account is hashed (username, wallet, sponsor, joined) against the last pushed // hash in DATA_DIR/lssync.json and only changed rows go, 100 per request. Catches anything touch() missed. // LinkSpin upserts the rows and creates shell accounts with their sponsor line, so a new InstantAdPay member is already // "waiting" on LinkSpin before they ever sign in there. Nothing here moves money; the registry is identity + line only. 'use strict'; const crypto = require('crypto'); const fs = require('fs'); const path = require('path'); let R = null, state = null, pending = new Map(), timer = null, ticking = false; const URL_ = () => String(process.env.LS_URL || 'https://linkspin.app').replace(/\/+$/, ''); const SECRET = () => String(process.env.LS_SYNC_SECRET || '').trim(); const on = () => SECRET().length >= 32; const FILE = () => path.join(R.dataDir, 'lssync.json'); function load() { if (state) return state; try { state = JSON.parse(fs.readFileSync(FILE(), 'utf8')); } catch (e) { state = { hashes: {}, pushed: 0, last: null, errors: 0 }; } return state; } function save() { try { fs.writeFileSync(FILE(), JSON.stringify(load())); } catch (e) {} } function init(deps) { R = deps; load(); } async function rowFor(acct, byRef) { const ref = String(acct.sponsorRef || '').toLowerCase(); let sponsorEmail = null; if (ref) { const sp = byRef ? byRef.get(ref) : (await R.accounts.byUsername(ref) || await R.accounts.byCode(ref)); if (sp && sp.email !== acct.email) sponsorEmail = sp.email; } return { email: acct.email, username: acct.username || null, wallet: acct.address || null, sponsorEmail, joinedAt: Number(acct.created) || null, memberId: acct.memberId || null }; } const hashOf = r => crypto.createHash('sha1').update([r.username || '', r.wallet || '', r.sponsorEmail || '', r.joinedAt || '', r.memberId || ''].join('|')).digest('hex').slice(0, 16); async function post(rows) { const raw = JSON.stringify({ rows }); const ts = String(Date.now()); const sig = crypto.createHmac('sha256', SECRET()).update(ts + '\n' + raw).digest('hex'); const r = await fetch(URL_() + '/api/partner/registry', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-LS-Ts': ts, 'X-LS-Sig': sig }, body: raw, signal: AbortSignal.timeout(20000) }); if (!r.ok) throw new Error('linkspin ' + r.status + ' ' + (await r.text()).slice(0, 120)); return r.json(); } async function push(rows) { const st = load(); let sent = 0; for (let i = 0; i < rows.length; i += 100) { const chunk = rows.slice(i, i + 100); await post(chunk); sent += chunk.length; for (const r of chunk) st.hashes[r.email] = hashOf(r); } st.pushed += sent; st.last = { at: Date.now(), rows: sent }; save(); return sent; } // an account just changed: send its row shortly (debounced so a join + username + wallet in a row go as one) function touch(email) { if (!on() || !email) return; pending.set(String(email).toLowerCase(), Date.now()); if (!timer) timer = setTimeout(async () => { timer = null; const emails = [...pending.keys()]; pending.clear(); try { const rows = []; for (const e of emails) { const a = await R.accounts.byEmail(e); if (a) rows.push(await rowFor(a)); } if (rows.length) await push(rows); } catch (e) { load().errors++; save(); console.error('lssync touch', e.message); } }, 3000); } // the reconcile pass: only rows whose hash changed since the last push async function tick(force) { if (!on() || ticking) return { skipped: true }; ticking = true; try { const all = await R.accounts.listAll(50000); const st = load(); const byRef = new Map(); for (const a of all) { if (a.username) byRef.set(String(a.username).toLowerCase(), a); if (a.code) byRef.set(String(a.code).toLowerCase(), a); } const rows = []; for (const a of all) { if (!a.email) continue; const r = await rowFor(a, byRef); if (force || st.hashes[r.email] !== hashOf(r)) rows.push(r); } const sent = rows.length ? await push(rows) : 0; return { accounts: all.length, changed: rows.length, sent }; } catch (e) { load().errors++; save(); console.error('lssync tick', e.message); return { error: e.message }; } finally { ticking = false; } } function adminView() { const st = load(); return { on: on(), url: URL_(), tracked: Object.keys(st.hashes).length, pushed: st.pushed, last: st.last, errors: st.errors, pendingTouches: pending.size }; } module.exports = { init, touch, tick, adminView, on };