From bdb2aacf2709f6f4a3445a1d30c842cbff8f1ce0 Mon Sep 17 00:00:00 2001 From: martbost Date: Fri, 13 Nov 2026 08:00:18 -0600 Subject: [PATCH] Network hub shadow mode (Marty 2026-11-13): hubreport.js reports every on-chain member to the hub as a contract bind by wallet (one way; unknown wallets kept and re-asked every 6 h), visible in /api/admin/status; off unless HUB_ENABLED Co-Authored-By: Claude Opus 5.5 --- chain.js | 3 ++- hubreport.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ server.js | 4 +++- 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 hubreport.js diff --git a/chain.js b/chain.js index 83e37e2..d94d407 100644 --- a/chain.js +++ b/chain.js @@ -186,6 +186,7 @@ function spots() { return [...d().spots.values()].sort((a, b) => b.id - a.id); } function spot(id) { return d().spots.get(Number(id)) || null; } function spotsOf(addr) { const a = String(addr).toLowerCase(); return spots().filter(s => s.owner === a); } function member(addr) { return d().members.get(String(addr).toLowerCase()) || null; } +function allMembers() { return [...d().members.values()]; } function isMember(addr) { const a = String(addr || '').toLowerCase(); if (a === String(cfgMod.get().contract).toLowerCase()) return false; return d().members.has(a) || spots().some(s => s.owner === a && s.kind !== 3); } function directs(addr) { const a = String(addr).toLowerCase(); return [...d().members.values()].filter(m => m.sponsor === a); } function walletTotals(addr) { const w = d().wallets.get(String(addr).toLowerCase()); return w ? { inWei: w.inWei.toString(), outWei: w.outWei.toString(), byRole: w.byRole, payouts: w.payouts } : { inWei: '0', outWei: '0', byRole: {}, payouts: 0 }; } @@ -218,4 +219,4 @@ function init(opts) { } module.exports = { init, tail, rpc, read, tryRead, quote, decodeLog, receipt, suggestedFees, spots, spot, spotsOf, member, isMember, - directs, walletTotals, paidOf, fundBuys, totals, status, recent, ZERO }; + allMembers, directs, walletTotals, paidOf, fundBuys, totals, status, recent, ZERO }; diff --git a/hubreport.js b/hubreport.js new file mode 100644 index 0000000..9807eac --- /dev/null +++ b/hubreport.js @@ -0,0 +1,60 @@ +// 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 }; diff --git a/server.js b/server.js index 18ab703..7b17f33 100644 --- a/server.js +++ b/server.js @@ -13,6 +13,7 @@ const board = require('./board'); const safety = require('./safety'); const keeper = require('./keeper'); const { iface } = require('./abi'); +const hubreport = require('./hubreport'); // network hub, shadow mode: members reported by wallet (2026-11-13), off unless HUB_ENABLED const networksso = require('./networksso'); // network sign-in: arrive signed in from InstantAdPay or LinkSpin (2026-11-13), see networksso.js const SEL = { buy: iface.getFunction('buy').selector, buyFor: iface.getFunction('buyFor').selector }; // the browser builds calldata from these (ABI stays in abi.js) @@ -20,6 +21,7 @@ const C = cfgMod.get(); fs.mkdirSync(path.join(C.dataDir, 'art'), { recursive: true }); store.load(); chain.init(); +hubreport.init({ dataDir: C.dataDir, members: () => chain.allMembers() }); keeper.init(); const PUB = path.join(__dirname, 'public'); @@ -340,7 +342,7 @@ const server = http.createServer(async (req, res) => { // -- admin (ADMIN_TOKEN header): keeper preview + index status. No key material is ever returned. if (p === '/api/admin/status' && req.method === 'GET') { const tok = process.env.ADMIN_TOKEN; if (!tok || req.headers['x-admin-token'] !== tok) return json(res, 401, { error: 'no' }); - return json(res, 200, { index: chain.status(), totals: chain.totals(), keeper: keeper.statusInfo() }); + return json(res, 200, { index: chain.status(), totals: chain.totals(), keeper: keeper.statusInfo(), hub: hubreport.view() }); } if (p.startsWith('/api/')) return json(res, 404, { error: 'Not found' });