Files
viraladblitz/hubreport.js
T
Marty Bostick 001878bdae ViralAdBlitz v1 (viraladblitz.com rebuild, Marty approved 2026-11-14): the network's text-ad home, local build, not deployed
- Free text ad for network members (InstantAdPay / LinkSpin sign-in or wallet, membership through the hub), one per member
- Member pages viraladblitz.com/<username>: own ad + the first 5 people above in the hub line with a live ad (by place) + 2 Boosted places
- Front page: amber ticker (Boosted starred), Top ads this week by verified clicks, today's numbers, ledger, contract proof
- Verified visitors / views / clicks (ticket + dwell + per-visitor dedupe + per-connection cap + bot filter, owner excluded)
- Boost $5 = 500 guaranteed views through VABShop: keeper-signed EIP-712 line voucher from the hub line, watcher, delivery
  counted per verified view with an audit row each, Network Ad Space text-ad finishing after 30 days (vabnas.js)
- Trust pack: /ledger, /contract, toasts, Telegram card per Boost (once), Terms / Earnings Disclaimer / Privacy (TrustClickz),
  network footer from the hub with tracked links, promo kit (21 creatives) + /promote, LinkSpin zone placeholders, hub report
- Old YFW links via the oldrefs map (admin load route); docstore MySQL documents + row tables
- qa/smoke.mjs 162/162 (stand-in hub, chain, Telegram, NAS; Playwright desktop + phone), qa/chain.mjs 20/20 (real VABShop on anvil)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 13:52:03 -06:00

76 lines
5.0 KiB
JavaScript

// Network hub report (the Downline Banners / VBB hubreport.js pattern, 2026-11-14). ViralAdBlitz has no email accounts, so every VABShop Boost
// goes to the hub by wallet:
// the buyer's first purchase: POST /v1/contract-bind { contract: 'viraladblitz', wallet, sponsorRef: <sponsor wallet>, spentCents, tx, at }
// every later purchase: POST /v1/contract-spend { contract: 'viraladblitz', wallet, cents, tx, at }
// signed as HUB_SITE (default viraladblitz) 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 = 'viraladblitz';
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 || 'viraladblitz').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 || 'viraladblitz').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 };