// Network hub report (shadow mode, Marty 2026-11-13: "run it in shadow mode across everything today"). Downline Banners has no // email accounts, so every on-chain member (MemberJoined) goes to the hub as a contract bind by wallet: // POST /v1/contract-bind { contract: 'downlinebanners', wallet, sponsorRef: }, signed as HUB_SITE (bannerline) // with HUB_SECRET (X-Hub-Ts + X-Hub-Sig = hex HMAC-SHA256(ts + newline + raw body)). One way only: nothing the hub says // changes anything here. A wallet the hub does not know (404 unknown_wallet) is kept in `unknown` and asked again every 6 hours, // since the member may link it on InstantAdPay later; that list is part of the shadow comparison. Off unless HUB_ENABLED. 'use strict'; const crypto = require('crypto'); const fs = require('fs'); const path = require('path'); 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.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 || 'bannerline').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(); for (const m of R.members()) { const a = m.address; if (s.sent[a]) continue; if (s.unknown[a] && now - s.unknown[a].at < RECHECK) continue; let r; try { r = await post('/v1/contract-bind', { contract: 'downlinebanners', wallet: a, sponsorRef: m.sponsor || '', at: m.joinedAt || now }); } catch (e) { s.errors[a] = { at: now, error: e.message }; break; } // hub unreachable: try again next tick if (r.status === 200) { s.sent[a] = { at: now, hubId: r.body && r.body.id || null }; delete s.unknown[a]; delete s.errors[a]; note({ sent: a }); } else if (r.status === 404) { s.unknown[a] = { at: now, sponsor: m.sponsor || null }; note({ unknown: a }); } else { s.errors[a] = { at: now, status: r.status, error: r.body && (r.body.error || r.body.message) }; note({ error: a, status: r.status }); if (r.status >= 500) break; } } save(); } finally { busy = false; } } // deps: { dataDir, members: () => [{ address, sponsor, joinedAt }] } function init(deps) { R = deps; if (!enabled()) return; load(); save(); setTimeout(() => tick().catch(e => console.error('hubreport', e.message)), 20000); setInterval(() => tick().catch(e => console.error('hubreport', e.message)), 60000); } function view() { const s = load(); return { enabled: enabled(), sent: Object.keys(s.sent).length, unknown: s.unknown, errors: s.errors, log: s.log.slice(-30) }; } module.exports = { init, tick, view, enabled };