// Network hub report (the Downline Banners / VBB hubreport.js pattern, 2026-11-14). 5xYourTeam coaches are wallets, so every DraftPosition purchase // goes to the hub by wallet: // the buyer's first purchase: POST /v1/contract-bind { contract: 'fivex', wallet, sponsorRef: , spentCents, tx, at } // every later purchase: POST /v1/contract-spend { contract: 'fivex', wallet, cents, tx, at } // signed as HUB_SITE (default fivex) with HUB_SECRET (X-Hub-Site, X-Hub-Ts, X-Hub-Sig = hex HMAC-SHA256(ts + newline + raw body)). // One way only: nothing the hub answers changes anything here. A wallet the hub does not know (404) is kept in `unknown` and asked // again every 6 hours (the member may link it on InstantAdPay later). Off unless HUB_ENABLED=1 with HUB_URL and a 32+ char HUB_SECRET. // State lives in hubreport.json (a MySQL document through docstore when DOCSTORE_URL is set). 'use strict'; const crypto = require('crypto'); const fs = require('fs'); const path = require('path'); const CONTRACT = 'fivex'; const ZERO = '0x0000000000000000000000000000000000000000'; const enabled = () => /^(1|true|yes|on)$/i.test(String(process.env.HUB_ENABLED || '').trim()) && !!String(process.env.HUB_URL || '').trim() && String(process.env.HUB_SECRET || '').trim().length >= 32; const RECHECK = 6 * 3600000; let R = null, st = null, busy = false; const FILE = () => path.join(R.dataDir, 'hubreport.json'); function load() { if (st) return st; try { st = JSON.parse(fs.readFileSync(FILE(), 'utf8')); } catch (e) { st = {}; } st.sent = st.sent || {}; st.bound = st.bound || {}; st.unknown = st.unknown || {}; st.errors = st.errors || {}; st.log = st.log || []; return st; } function save() { try { fs.writeFileSync(FILE() + '.tmp', JSON.stringify(load())); fs.renameSync(FILE() + '.tmp', FILE()); } catch (e) { console.error('hubreport save', e.message); } } function note(x) { const s = load(); s.log.push(Object.assign({ ts: Date.now() }, x)); if (s.log.length > 200) s.log = s.log.slice(-200); } async function post(route, body) { const raw = JSON.stringify(body); const ts = String(Date.now()); const sig = crypto.createHmac('sha256', String(process.env.HUB_SECRET).trim()).update(ts + '\n' + raw).digest('hex'); const ac = new AbortController(); const t = setTimeout(() => ac.abort(), 15000); try { const r = await fetch(String(process.env.HUB_URL).trim().replace(/\/+$/, '') + route, { method: 'POST', body: raw, signal: ac.signal, headers: { 'Content-Type': 'application/json', 'X-Hub-Site': String(process.env.HUB_SITE || 'fivex').trim(), 'X-Hub-Ts': ts, 'X-Hub-Sig': sig } }); let j = null; try { j = await r.json(); } catch (e) {} return { status: r.status, body: j }; } finally { clearTimeout(t); } } async function tick() { if (!enabled() || busy) return; busy = true; try { const s = load(); const now = Date.now(); const list = R.purchases().slice().sort((a, b) => (a.block || 0) - (b.block || 0) || (a.at || 0) - (b.at || 0)); for (const p of list) { if (s.sent[p.key]) continue; const a = p.buyer; if (s.unknown[a] && now - s.unknown[a].at < RECHECK) continue; const tx = String(p.key).split(':')[0]; const bind = !s.bound[a]; const body = bind ? { contract: CONTRACT, wallet: a, sponsorRef: p.sponsor && p.sponsor !== ZERO ? p.sponsor : '', spentCents: p.usdCents || 0, tx, at: p.at || now } : { contract: CONTRACT, wallet: a, cents: p.usdCents || 0, tx, at: p.at || now }; let r; try { r = await post(bind ? '/v1/contract-bind' : '/v1/contract-spend', body); } catch (e) { s.errors[p.key] = { at: now, error: e.message }; break; } // hub unreachable: try again next tick if (r.status === 200) { s.sent[p.key] = { at: now, route: bind ? 'bind' : 'spend' }; if (bind) s.bound[a] = now; delete s.unknown[a]; delete s.errors[p.key]; note({ sent: p.key, bind }); } else if (r.status === 404) { s.unknown[a] = { at: now, sponsor: p.sponsor || null }; note({ unknown: a }); } else { s.errors[p.key] = { at: now, status: r.status, error: r.body && (r.body.error || r.body.message) }; note({ error: p.key, status: r.status }); if (r.status >= 500) break; } } save(); } finally { busy = false; } } // deps: { dataDir, purchases: () => [{ key, buyer, sponsor, usdCents, block, at }] } function init(deps) { R = deps; if (!enabled()) return; load(); save(); const every = Math.max(200, Number(process.env.HUB_TICK_MS) || 60000); setTimeout(() => tick().catch(e => console.error('hubreport', e.message)), Math.min(20000, every)).unref(); setInterval(() => tick().catch(e => console.error('hubreport', e.message)), every).unref(); } function view() { if (!R) return { enabled: false }; const s = load(); return { enabled: enabled(), site: String(process.env.HUB_SITE || 'fivex').trim(), sent: Object.keys(s.sent).length, bound: Object.keys(s.bound).length, unknown: s.unknown, errors: s.errors, log: s.log.slice(-30) }; } module.exports = { init, tick, view, enabled, CONTRACT };