Files
yourprogrambuilder/hubreport.js
T
martbost a8a1a8eec7 YourProgramBuilder, the Seed Bed (Marty 2026-11-17: "keep fleshing it out"): STAGING build
Plant a program free (referral link with {id}, link check green / yellow / red, daily recheck, reports), members save their
referral IDs in its packet, a sponsor-first link (member page / ?r= / the hub sponsor line, then the planter), the sprout meter
to REVIEW_SAVES (DEFAULT 25) -> Marty's review queue -> approved packets exported for the CTB builder / DownlineHydra wild heads
with every saved ID. Dormant old programs confirmed by their owner, old-link redirects (oldrefs), Spotlight on YPBShop (NOT
deployed; DEFAULT $10 / 1,000 views, 20/5/5/5/5, company 60), verified views with Spotlight delivery, trust pack (ledger,
contract, toasts, Telegram card, terms / disclaimer / privacy, TrustClickz only), network footer row, chatbot, promo kit,
Guides, PolHunter slot, LinkSpin tracked-link and zone hooks, AI writer through the hub. Seed-catalog look from the approved
mockup. Tests: qa/unit.test.cjs 33/33, qa/e2e.mjs 121/121 on a local anvil chain.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 09:30:59 -06:00

76 lines
5.0 KiB
JavaScript

// Network hub report (the Downline Banners / VBB hubreport.js pattern, 2026-11-14). YourProgramBuilder members are wallets here, so every YPBShop Spotlight
// goes to the hub by wallet:
// the buyer's first purchase: POST /v1/contract-bind { contract: 'yourprogrambuilder', wallet, sponsorRef: <sponsor wallet>, spentCents, tx, at }
// every later purchase: POST /v1/contract-spend { contract: 'yourprogrambuilder', wallet, cents, tx, at }
// signed as HUB_SITE (default viraladsource) 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 = 'yourprogrambuilder';
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 || 'yourprogrambuilder').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 || 'yourprogrambuilder').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 };